<repo_name>Chadarr/3_bars<file_sep>/README.md
# 3_bars

- First of all, you have to download data file from this URL: http://data.mos.ru/opendata/export/1796/json/2/1
- Unzip it somewhere and input it's filepath to program
- Enter your longitude and latitude to find the closest bar.<file_sep>/bars.py
import json
import os
from math import sqrt


def load_bar_data(filepath):
    if not os.path.exists(filepath):
        return None
    with open(filepath, encoding='utf-8') as handle:
        return json.load(handle)


def bar_name(_data_to_use):
    return _data_to_use['Cells']['Name']


def bar_capacity(_data_to_use):
    return _data_to_use['Cells']['SeatsCount']


def bar_coordinates(_data_to_use):
    return _data_to_use['Cells']['geoData']['coordinates'][0], _data_to_use['Cells']['geoData']['coordinates'][1]


def get_biggest_bar_name(_data_to_use):
    return bar_name(max(_data_to_use, key=bar_capacity))


def get_smallest_bar_name(_data_to_use):
    return bar_name(min(_data_to_use, key=bar_capacity))


def get_closest_bar_name(_data, longitude, latitude):
    def get_distance(_data):
        x, y = bar_coordinates(_data)
        return sqrt((float(longitude) - x) ** 2 + (float(latitude) - y) ** 2)

    return bar_name(min(_data, key=get_distance))

if __name__ == '__main__':
    path = input("Please tell me the path to your JSON file\r\n")
    data_to_use = load_bar_data(path)
    biggest_bar = get_biggest_bar_name(data_to_use)
    print('The biggest bar(-s): "{}"\n'.format(get_biggest_bar_name(data_to_use)))
    print('The smallest bar(-s): "{}"\n'.format(get_smallest_bar_name(data_to_use)))
    _coords = input(
        'Type 2 numbers - that would be your latitude and longtitude. Fractional numbers should be entered through dot.\r\n')
    _coords = _coords.split(' ')
    print(
        'The closest bar(-s): "{}"\r\n'.format(get_closest_bar_name(data_to_use, float(_coords[0]), float(_coords[1]))))
