-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
123 lines (107 loc) · 3.95 KB
/
engine.py
File metadata and controls
123 lines (107 loc) · 3.95 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
from prettytable import PrettyTable
from base import Filter, Human, Crime
from datetime import datetime, time, date
def get_humans():
humans = []
with open('Humans', 'r') as h:
for human in h.readlines():
humans.append(human[:-1].split(' '))
return humans
def get_crimes():
crimes = []
with open('Crimes', 'r') as c:
for crime in c.readlines():
crimes.append(crime[:-1].split(' '))
return crimes
def print_humans(humans):
if humans:
table = PrettyTable()
table.field_names = ['ID#', 'First Name', 'Last Name', 'Date of birth', 'Date added to base']
for human in humans:
table.add_row([human[0], human[1], human[2], human[3], human[4]])
return table
else:
return 'Base of humans is empty'
def print_crimes(crimes):
if crimes:
table = PrettyTable()
table.field_names = ['ID#', 'Date', 'Type', 'Address']
for crime in crimes:
table.add_row([crime[0], crime[1], crime[2], crime[3]])
return table
else:
return 'Base of crimes is empty'
def add_human(f_name, l_name, b_date, base):
new_human = Human(f_name, l_name, b_date)
humans = get_humans()
exists = new_human.exists(humans)
if humans:
if exists:
print('This human is already exists on ID# {}'.format(exists))
else:
f1 = Filter(name=f_name, base=base)
f2 = Filter(name=l_name, base=base)
names = [name[1] for name in humans] + [name[2] for name in humans]
distances = [f1.levenshtein_distance(dist) for dist in names] + [f2.levenshtein_distance(dist) for dist in names]
lev = dict(zip(names, distances))
rec_names = []
for key, value in lev.items():
if int(value) <= 5:
rec_names.append(key)
if rec_names:
rec_humans = []
for h in humans:
if h[1] in rec_names and h[2] in rec_names:
rec_humans.append(h)
if rec_humans:
print('Do you mean these humans?')
print(print_humans(rec_humans))
answer = input('Input y/n: ')
if answer == 'y':
return
elif answer == 'n':
new_human.add_to_base()
else:
print('Wrong input')
else:
new_human.add_to_base()
else:
new_human.add_to_base()
else:
new_human.add_to_base()
def add_crime(date, adress, type):
new_crime = Crime(date, adress, type)
crimes = get_crimes()
exists = new_crime.exists(crimes)
if exists:
print('This crime is already exists on ID# {}'.format(exists))
else:
new_crime.add_to_base()
def search_human(base, name):
humans = get_humans()
if humans:
f = Filter(base=base, name=name)
found = f.humans_by_name(humans)
if found:
return print_humans(found)
else:
names = [name[1] for name in humans] + [name[2] for name in humans]
distances = [f.levenshtein_distance(dist) for dist in names]
lev = dict(zip(names, distances))
rec_names = []
for key, value in lev.items():
if int(value) <= 3:
rec_names.append(key)
if rec_names:
return 'Do you mean: ' + ', '.join(rec_names).strip(', ') + ' ?'
else:
return 'Person with name: {} not found.'.format(f.name)
def get_humans_by_period(from_date, to_date, humans):
found_humans = []
for human in humans:
get_date = datetime.strptime(human[4], '%d/%m/%Y')
diff1 = to_date - get_date
diff2 = to_date - from_date
if diff1 <= diff2:
found_humans.append(human)
return found_humans