forked from devmanorg/3_bars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbars.py
executable file
·123 lines (105 loc) · 3.94 KB
/
bars.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
#!/usr/bin/env python3
import json
import re
import sys
import os
from geopy.distance import vincenty
def load_data(json_filepath):
with open(json_filepath, "r") as file_contaning_json:
object_representing_json = json.load(file_contaning_json)
bars_list = object_representing_json["features"]
return bars_list
def get_biggest_bar(bars_list):
biggest_bar = max(
bars_list,
key=lambda bar: bar["properties"]["Attributes"]["SeatsCount"]
)
return biggest_bar
def get_smallest_bar(bars_list):
smallest_bar = min(
bars_list,
key=lambda bar: bar["properties"]["Attributes"]["SeatsCount"]
)
return smallest_bar
def calculate_geo_distance(
bar,
user_defined_longitude,
user_defined_latitude):
distance = vincenty(
(float(user_defined_longitude), float(user_defined_latitude)),
bar["geometry"]["coordinates"]).meters
return distance
def get_closest_bar(
bars_list,
user_defined_longitude,
user_defined_latitude):
closest_bar = min(
bars_list,
key=lambda bar: calculate_geo_distance(
bar,
user_defined_longitude,
user_defined_latitude
)
)
distance = calculate_geo_distance(
closest_bar,
user_defined_longitude,
user_defined_latitude
)
return closest_bar, distance
def request_user_defined_coordinates():
print("Введите GPS координаты подыщем ближайший бар "
"(в формате int или float)")
user_defined_longitude = input("Широта : ")
if not re.match("\d+(\.\d+)?$", user_defined_longitude):
user_defined_longitude = None
user_defined_latitude = input("Долгота : ")
if not re.match("\d+(\.\d+)?$", user_defined_latitude):
user_defined_latitude = None
return user_defined_longitude, user_defined_latitude
def print_pretty_output(bar_type, bar, distance=None):
bar_info_output_template = (
"Самый {} бар: {}"
"\nКоторый расположен по адресу: {}"
)
distance_to_bar_output_template = (
"Находится на расстоянии: {} километров"
)
bar_name = bar["properties"]["Attributes"]["Name"]
bar_address = bar["properties"]["Attributes"]["Address"]
print(bar_info_output_template.format(bar_type, bar_name, bar_address))
if distance:
print(
distance_to_bar_output_template.format(
int(distance/1000)
)
)
if __name__ == "__main__":
shell_input = sys.argv
if len(shell_input) < 2:
sys.exit(
"Не указан аргумент - файл."
"\nПерезапустите скрипт командой в формате "
"'python3 bars.py <path to file>'")
json_filepath = sys.argv[1]
if not os.path.exists(json_filepath):
print("Такой файл не существует")
else:
bars_list = load_data(json_filepath)
user_defined_longitude, user_defined_latitude = (
request_user_defined_coordinates()
)
if user_defined_longitude is None or user_defined_latitude is None:
sys.exit("Некорректный формат значений GPS координат."
"\nПерезапустите программу и "
"введите аргументы в корректном формате")
biggest_bar = get_biggest_bar(bars_list)
smallest_bar = get_smallest_bar(bars_list)
closest_bar, distance = get_closest_bar(
bars_list,
user_defined_longitude,
user_defined_latitude
)
print_pretty_output("большой", biggest_bar)
print_pretty_output("маленький", smallest_bar)
print_pretty_output("ближайший", closest_bar, distance)