This repository was archived by the owner on Dec 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipl_processing.py
77 lines (62 loc) · 2.47 KB
/
pipl_processing.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
from piplapis.data import Person, Name, Address
def address_match(tree, address):
"""
Checks if supplied address matches comparison tree, with the tree set-up according to:
n = even --> tree[n] = address dict key
tree[n + 1] = comparison value to compare to address dict key
(please note: Address is a class object but we're reading the value directly with
address.__dict__[key] due to the way that python classes are setup)
"""
length = len(tree)
# Check we have a valid compare tree
if not tree or length % 2 != 0:
raise ValueError("Invalid compare tree!")
index = 0
while index < length:
key = tree[index]
comp_val = tree[index+1]
# If the address object doesn't have a value for the required key,
# or the corresponding value is different, return False
if not address.__dict__[key] or address.__dict__[key] != comp_val:
return False
# step 2 along to the next key in compare tree
index += 2
return True
def get_matching_addresses(addresses = list(), house = "", apt = "", street = "", city = "", state = "", zip = "", type = ""):
"""
For a supplied list of Address objects, returns only those that match
all of the supplied (non-empty) address attributes
"""
matching_addrs = list()
# return if nothing to iterate
if len(addresses) == 0: return matching_addrs
# create comparison tree for looking at address attributes
# (comparison tree is probably a really bad name for what this is
# but i can't think of another way to explain it :P)
compare_tree = list()
if house != "":
compare_tree.append("house")
compare_tree.append(house)
if street != "":
compare_tree.append("street")
compare_tree.append(street)
if apt != "":
compare_tree.append("apt")
compare_tree.append(apt)
if city != "":
compare_tree.append("city")
compare_tree.append(city)
if state != "":
compare_tree.append("state")
compare_tree.append(state)
if zip != "":
compare_tree.append("zip")
compare_tree.append(zip)
if type != "":
compare_tree.append("type")
compare_tree.append(type)
# Compare each of the supplied addresses with the comparison tree
for address in addresses:
if address_match(compare_tree, address):
matching_addrs.append(address)
return matching_addrs