forked from scroberts/Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Match.py
152 lines (138 loc) · 4.4 KB
/
Match.py
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
#!/usr/bin/env python3
# external modules
import sys
# my modules
import Config as CF
import DCC
import FileSys
debug = False
def in_dict(key, val, dict, **kwargs):
debug = False
# checks if key has a value that matches val
# kwargs:
# Match = 'eq' or 'in'
# eq means exact match (==)
# in means in string (in)
if debug: print('checking key = ', key, 'val = ', val)
if debug: print('in dict = ',dict)
match = kwargs.get('Match', 'eq')
if not key in dict:
if debug: print('key not in dict, returning False')
return(False)
if match == 'eq' and dict[key] == val:
if debug: print('match eq = True')
return(True)
elif match == 'in' and val in dict[key]:
if debug: print('match in = True')
return(True)
else:
if debug: print('match = False')
return(False)
def in_list(val, list, **kwargs):
# checks if key has a value that matches val
# kwargs:
# Match = 'eq' or 'in'
# eq means exact match (==)
# in means in string (in)
match = kwargs.get('Match', 'eq')
for item in list:
if match == 'eq' and item == val:
return(True)
elif match == 'in' and val in item:
return(True)
return(False)
def parse(p,dict):
# p contains the criteria
# dict contains the dictionary to check
# parse returns True if the criteria are met in the dictionary
# parse expects a dictionary with on key.
# the value will be either a dictionary or a list
# key, val = next(iter(p.items()))
for key, val in p.items():
if key == 'AND':
if debug: print('AND Parsing: val = ', val)
# expects a list of dicts
for d in val:
if debug: print('AND Parsing: d = ', d)
if not parse(d, dict):
return(False)
return(True)
if key == 'OR':
# expects a list of dicts
for d in val:
if parse(d, dict):
return(True)
return(False)
if key == 'XOR':
# XOR is true if only one input is true
# expects a list of dicts
count = 0
for d in val:
if parse(d, dict):
count += 1
if count == 1:
return(True)
return(False)
elif key == 'NOT':
return(not parse(val, dict))
elif key == 'InDict':
k = val['key']
v = val['val']
m = val['match']
if debug: print('checking k, v',k,v)
return(in_dict(k, v, dict, Match = m))
elif key == 'InList':
v = val['val']
l = val['list']
m = val['match']
return(in_list(v,l,Match = m))
else:
print('key = ', key)
print('Error - key not found')
sys.exit(1)
def test_in_list():
list = ['Scott', 'Cameron', 'Roberts']
print(in_list('Scott', list))
print(in_list('Cam', list))
print(in_list('Cam', list, Match = 'in'))
def test_parse():
dict = {}
dict['name'] = 'Scott Roberts'
dict['home'] = 'Victoria'
dict['number'] = 42
key = 'name'
val = 'Scott'
match = 'in'
c = {'InDict' : {'key' : key, 'val' : val, 'match' : match}}
key = 'home'
val = 'V'
match = 'in'
d = {'InDict' : {'key' : key, 'val' : val, 'match' : match}}
e = {'NOT' : d}
f = {'NOT' : e}
g = {'NOT' : f}
h = {'AND' : [c,d]}
print('c',parse(c, dict))
print('d',parse(d, dict))
print('g',parse(g, dict))
print('h',parse(h, dict))
# list = ['Scott', 'Cameron', 'Roberts']
# val = 'Camp'
# j = {'InList' : {'list' : list, 'val' : val, 'match' : 'in'}}
# print('j', parse(j))
def test_in_dict():
dict = {}
dict['name'] = 'Scott Roberts'
dict['home'] = 'Victoria'
dict['number'] = 42
print('Test 1:',in_dict('nokey','test', dict))
print('Test 2:',in_dict('name','Scott', dict))
print('Test 3:',in_dict('name','Scott Roberts', dict))
print('Test 4:',in_dict('name','Scott Roberts', dict, Match = 'eq'))
print('Test 5:',in_dict('name','Scott', dict, Match = 'in'))
print('Test 6:',in_dict('name','Scott Roberts', dict, Match = 'bad'))
if __name__ == '__main__':
print("Running module test code for",__file__)
# test_in_dict()
test_parse()
# test_in_list()