Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | from __future__ import print_function |
| 3 | import sys |
| 4 | sys.path.append('.') |
| 5 | |
| 6 | from example import ExampleVirt, runExampleVirt, runExampleVirtVirtual, runExampleVirtBool |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame^] | 7 | from example import A_Repeat, B_Repeat, C_Repeat, D_Repeat, A_MI, B_MI, C_MI, D_MI, A_Tpl, B_Tpl, C_Tpl, D_Tpl |
Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 8 | |
| 9 | class ExtendedExampleVirt(ExampleVirt): |
| 10 | def __init__(self, state): |
| 11 | super(ExtendedExampleVirt, self).__init__(state + 1) |
| 12 | self.data = "Hello world" |
| 13 | |
| 14 | def run(self, value): |
| 15 | print('ExtendedExampleVirt::run(%i), calling parent..' % value) |
| 16 | return super(ExtendedExampleVirt, self).run(value + 1) |
| 17 | |
| 18 | def run_bool(self): |
| 19 | print('ExtendedExampleVirt::run_bool()') |
| 20 | return False |
| 21 | |
| 22 | def pure_virtual(self): |
| 23 | print('ExtendedExampleVirt::pure_virtual(): %s' % self.data) |
| 24 | |
| 25 | |
| 26 | ex12 = ExampleVirt(10) |
| 27 | print(runExampleVirt(ex12, 20)) |
| 28 | try: |
| 29 | runExampleVirtVirtual(ex12) |
| 30 | except Exception as e: |
| 31 | print("Caught expected exception: " + str(e)) |
| 32 | |
| 33 | ex12p = ExtendedExampleVirt(10) |
| 34 | print(runExampleVirt(ex12p, 20)) |
| 35 | print(runExampleVirtBool(ex12p)) |
| 36 | runExampleVirtVirtual(ex12p) |
Jason Rhinelander | 0ca96e2 | 2016-08-05 17:02:33 -0400 | [diff] [blame^] | 37 | |
| 38 | sys.stdout.flush() |
| 39 | |
| 40 | class VI_AR(A_Repeat): |
| 41 | def unlucky_number(self): |
| 42 | return 99 |
| 43 | class VI_AMI(A_MI): |
| 44 | def unlucky_number(self): |
| 45 | return 990 |
| 46 | def say_something(self, times): |
| 47 | return A_MI.say_something(self, 2*times) |
| 48 | class VI_AT(A_Tpl): |
| 49 | def unlucky_number(self): |
| 50 | return 999 |
| 51 | |
| 52 | class VI_CR(C_Repeat): |
| 53 | def lucky_number(self): |
| 54 | return C_Repeat.lucky_number(self) + 1.25 |
| 55 | class VI_CMI(C_MI): |
| 56 | def lucky_number(self): |
| 57 | return 1.75 |
| 58 | class VI_CT(C_Tpl): |
| 59 | pass |
| 60 | class VI_CCR(VI_CR): |
| 61 | def lucky_number(self): |
| 62 | return VI_CR.lucky_number(self) * 10 |
| 63 | class VI_CCMI(VI_CMI): |
| 64 | def lucky_number(self): |
| 65 | return VI_CMI.lucky_number(self) * 100 |
| 66 | class VI_CCT(VI_CT): |
| 67 | def lucky_number(self): |
| 68 | return VI_CT.lucky_number(self) * 1000 |
| 69 | |
| 70 | |
| 71 | class VI_DR(D_Repeat): |
| 72 | def unlucky_number(self): |
| 73 | return 123 |
| 74 | def lucky_number(self): |
| 75 | return 42.0 |
| 76 | class VI_DMI(D_MI): |
| 77 | def unlucky_number(self): |
| 78 | return 1230 |
| 79 | def lucky_number(self): |
| 80 | return -9.5 |
| 81 | class VI_DT(D_Tpl): |
| 82 | def say_something(self, times): |
| 83 | print("VI_DT says:" + (' quack' * times)) |
| 84 | def unlucky_number(self): |
| 85 | return 1234 |
| 86 | def lucky_number(self): |
| 87 | return -4.25 |
| 88 | |
| 89 | classes = [ |
| 90 | # A_Repeat, A_MI, A_Tpl, # abstract (they have a pure virtual unlucky_number) |
| 91 | VI_AR, VI_AMI, VI_AT, |
| 92 | B_Repeat, B_MI, B_Tpl, |
| 93 | C_Repeat, C_MI, C_Tpl, |
| 94 | VI_CR, VI_CMI, VI_CT, VI_CCR, VI_CCMI, VI_CCT, |
| 95 | D_Repeat, D_MI, D_Tpl, VI_DR, VI_DMI, VI_DT |
| 96 | ] |
| 97 | |
| 98 | for cl in classes: |
| 99 | print("\n%s:" % cl.__name__) |
| 100 | obj = cl() |
| 101 | obj.say_something(3) |
| 102 | print("Unlucky = %d" % obj.unlucky_number()) |
| 103 | if hasattr(obj, "lucky_number"): |
| 104 | print("Lucky = %.2f" % obj.lucky_number()) |
| 105 | |