| 1 | #!/usr/bin/env python2
 | 
| 2 | """
 | 
| 3 | modules.py
 | 
| 4 | 
 | 
| 5 | PYTHONPATH=.:mycpp is required to run this one
 | 
| 6 | """
 | 
| 7 | from __future__ import print_function
 | 
| 8 | 
 | 
| 9 | import os
 | 
| 10 | from mylib import log
 | 
| 11 | 
 | 
| 12 | from testpkg import module1
 | 
| 13 | from testpkg.module2 import func2
 | 
| 14 | 
 | 
| 15 | 
 | 
| 16 | def run_tests():
 | 
| 17 |     # type: () -> None
 | 
| 18 |     module1.func1()
 | 
| 19 |     func2()
 | 
| 20 | 
 | 
| 21 |     dog = Dog('white')
 | 
| 22 |     dog.Speak()
 | 
| 23 | 
 | 
| 24 |     cat = module1.Cat()
 | 
| 25 |     cat.Speak()
 | 
| 26 | 
 | 
| 27 |     cat2 = Sphinx('brown')
 | 
| 28 |     cat2.Speak()
 | 
| 29 | 
 | 
| 30 |     # Test inheritance
 | 
| 31 |     cat = cat2
 | 
| 32 |     cat.Speak()
 | 
| 33 | 
 | 
| 34 |     cat.AbstractMethod()
 | 
| 35 | 
 | 
| 36 | 
 | 
| 37 | def run_benchmarks():
 | 
| 38 |     # type: () -> None
 | 
| 39 |     i = 0
 | 
| 40 |     n = 2000000
 | 
| 41 |     result = 0
 | 
| 42 |     while i < n:
 | 
| 43 |         result += module1.fortytwo()
 | 
| 44 |         i = i + 1
 | 
| 45 |     log('result = %d', result)
 | 
| 46 | 
 | 
| 47 | 
 | 
| 48 | # This is at the bottom to detect order.
 | 
| 49 | class Dog(object):
 | 
| 50 | 
 | 
| 51 |     def __init__(self, color):
 | 
| 52 |         # type: (str) -> None
 | 
| 53 |         self.color = color
 | 
| 54 | 
 | 
| 55 |     def Speak(self):
 | 
| 56 |         # type: () -> None
 | 
| 57 |         log('%s dog: meow', self.color)
 | 
| 58 | 
 | 
| 59 | 
 | 
| 60 | class Sphinx(module1.Cat):
 | 
| 61 | 
 | 
| 62 |     def __init__(self, color):
 | 
| 63 |         # type: (str) -> None
 | 
| 64 |         module1.Cat.__init__(self)
 | 
| 65 |         self.color = color
 | 
| 66 | 
 | 
| 67 |     def Speak(self):
 | 
| 68 |         # type: () -> None
 | 
| 69 |         log('%s sphinx', self.color)
 | 
| 70 | 
 | 
| 71 |     def AbstractMethod(self):
 | 
| 72 |         # type: () -> None
 | 
| 73 |         log('abstract')
 | 
| 74 | 
 | 
| 75 | 
 | 
| 76 | if __name__ == '__main__':
 | 
| 77 |     if os.getenv('BENCHMARK'):
 | 
| 78 |         log('Benchmarking...')
 | 
| 79 |         run_benchmarks()
 | 
| 80 |     else:
 | 
| 81 |         run_tests()
 |