This repository was archived by the owner on Jun 27, 2019. It is now read-only.
forked from devmanorg/3_bars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbars.py
More file actions
70 lines (50 loc) · 2.12 KB
/
bars.py
File metadata and controls
70 lines (50 loc) · 2.12 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
from json import load
from math import sqrt
from argparse import ArgumentParser
from os.path import exists
from sys import exit
def distance_to(bar, longitude, latitude):
x2 = (longitude - bar['geoData']['coordinates'][0]) ** 2
y2 = (latitude - bar['geoData']['coordinates'][1]) ** 2
return sqrt(x2 + y2)
def get_closest_bar(bars, longitude, latitude):
distance = lambda bar: distance_to(bar, longitude, latitude)
return min(bars, key=distance)
def get_biggest_bar(bars):
return max(bars, key=lambda bar: int(bar['SeatsCount']))
def get_smallest_bar(bars):
return min(bars, key=lambda bar: int(bar['SeatsCount']))
def load_data(filepath):
with open(filepath, encoding='cp1251') as f:
return load(f)
def print_bar(bar):
print('Название: {0}'.format(bar['Name']))
print('Адрес: {0}, {1}'.format(bar['District'], bar['Address']))
print('Телефон: {0}'.format(bar['PublicPhone'][0]['PublicPhone']))
def get_argument_parser():
parser = ArgumentParser()
parser.add_argument('what_to_look_for', type=str,
help='говорит скрипту, что нужно найти: '\
'biggest, smallest или closest')
parser.add_argument('json_filepath', help='файл с данными о барах')
return parser
if __name__ == '__main__':
options = {
'biggest': get_biggest_bar,
'smallest': get_smallest_bar,
'closest': get_closest_bar
}
args = get_argument_parser().parse_args()
option = options.get(args.what_to_look_for)
if not option:
exit('Неизвестный аргумент {0}'.format(args.what_to_look_for))
if not exists(args.json_filepath):
exit('Файл с барами не найден.')
bars = load_data(args.json_filepath)
kwargs = {'bars': bars}
if args.what_to_look_for == 'closest':
message = 'Введите долготу и ширину: '
longitude, latitude = [float(s) for s in input(message).split()]
kwargs['longitude'], kwargs['latitude'] = longitude, latitude
bar = option(**kwargs)
print_bar(bar)