-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionMapTest.py
More file actions
154 lines (136 loc) · 4.51 KB
/
FunctionMapTest.py
File metadata and controls
154 lines (136 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import re
class FunctionMap:
def __init__(self):
self._funcMap = None
def registerFunctionMap(self, funcMap):
self._funcMap = funcMap
def callFunction(self, name):
if self._funcMap:
if self._funcMap.get(name):
return self._funcMap[name]()
class Foo:
def __init__(self):
self._funcMap = dict()
self.__initFunctionMap__()
def __initFunctionMap__(self):
self._funcMap["test1"] = self.test1
self._funcMap["test2"] = self.test2
def test1(self):
print ("Test case 1")
def test2(self):
print ("Test Case 2")
def getFunctionMap(self):
return self._funcMap
def testFunctionMap():
functionMap = FunctionMap()
foo = Foo()
functionMap.registerFunctionMap(foo.getFunctionMap())
functionMap.callFunction("test1")
functionMap.callFunction("test2")
class IBoo:
TEST1=1
TEST2=2
def __init__(self):
self._name = "IBoo"
def test1(self):
pass
def test2(self,line):
pass
class Boo(IBoo):
def __init__(self):
self._name = "Boo"
def test1(self):
print ("%s: Test1 called" % self._name)
def test2(self,line):
print ("%s: Test2 called with para %s" % (self._name, line))
class BooFactory:
@staticmethod
def createBooMapping(IBoo):
test = dict()
test[IBoo.TEST1] = (IBoo.test1, IBoo.test2)
test[IBoo.TEST2] = (IBoo.test2, IBoo.test1)
return test
def testBooFactory():
bInstance = Boo()
mapping = BooFactory.createBooMapping(bInstance)
mapping[IBoo.TEST1][0]()
mapping[IBoo.TEST1][1]("Test2")
mapping[IBoo.TEST2][0]("Test2")
# ways to access base class methond/constructor
class A(object):
def __init__(self):
print "Constructor A was called"
class B(A):
def __init__(self):
super(B,self).__init__()
print "Constructor B was called"
class C(B):
def __init__(self):
# super(C,self).__init__()
B.__init__(self)
print "Constructor C was called"
class D:
x = 1
def test(self):
print ("D:test called: %d" % self.x)
class E (D):
x = 5
def test(self):
D.test(self)
print ("E:test called: %d" % self.x)
class F:
def __init__(self):
self._test = 1
def test(self):
pass
class G (F):
def test(self):
print (self._test)
def testLongNameRegEx(value, testText):
longNameRegExTemp = "(?P<prefix>^(>> | ))(?P<name>[^ ]{%d,}) $"
longNameRegEx = longNameRegExTemp % (value-1)
print longNameRegEx
result = re.search(longNameRegEx, testText)
if result:
print (result.group('prefix'))
print (result.group('name'))
def testNameValueRegEx(value, testText):
nameValueRegExTemp = "(?P<prefix>^(>> | ))(?P<name>[^ ]{1,%d})[ ]{0,%d}(?P<value>[^ ]+$)"
nameValueRegEx = nameValueRegExTemp % (value-1, value-2)
# nameValueRegExTemp = "(?P<prefix>^(>> | ))(?P<name>[^ ]{1,%d}) ?(?P<value>[^ ]+$)"
# nameValueRegEx = nameValueRegExTemp % (value-1)
print nameValueRegEx
result = re.search(nameValueRegEx, testText)
if result:
print (result.group('prefix'))
print (result.group('name'))
print (result.group('value'))
else:
print ("No match for %s" % testText)
def testLongNameRegularExp(testText):
longNameTemplate = re.compile(r"(?P<prefix>^(>> | ))(?P<name>[^ \"]+(\"[^\"]+\")?[^ \"]?) $")
result = longNameTemplate.search(testText)
if result:
print result.group('prefix')
print result.group('name')
else:
print ("No Match for %s" % testText)
if __name__ == '__main__':
testFunctionMap()
testBooFactory()
c = C()
e = E()
e.test()
g = G()
g.test()
testLongNameRegEx(14, ''' KEYWORD("CONDITION" ''')
testNameValueRegEx(21, ''' ^XTMP("PRCA219P" PRCA219P*!''')
testNameValueRegEx(21, ''' ^XTMP("MAGEVALSTUDY"RULES+36,RULES+90*''')
testNameValueRegEx(14, ''' ADJUST ADJNUM+1~,ADJNUM+3*,ADJNUM+4''')
testNameValueRegEx(14, ''' ^RC("PRC ABJ" RCCPCBAK+15''')
testLongNameRegularExp(''' ZTSAVE("PRCAIO" ''')
print ("Test"[0:2])
line ='''>> ^XTMP("MAGEVALSTUDY"RULES+36,RULES+90*'''
print ("Prefix: [%s]" % line[0:3])
print ("Name: [%s]" % line[3:23])
print ("Value: [%s]" % line[23:])