-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_country.py
56 lines (39 loc) · 1.46 KB
/
random_country.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
# https://restcountries.com/
import random
import requests
BASE_URL = "https://restcountries.com/v3"
class Country:
def __str__(self):
return "{}".format(self.name)
def __init__(self, country_data):
capital = country_data.get("capital")
self.code = country_data.get("cca3")
self.name = country_data.get("name").get("common")
self.continent = country_data.get("continents")[0]
self.population = country_data.get("population")
self.capital = capital[0] if capital != None else None
"""api wrappers"""
def get_country(country_code):
response = requests.get(f"{BASE_URL}/alpha/{country_code}")
country_data = response.json()[0]
return Country(country_data)
def get_all_countries():
response = requests.get(f"{BASE_URL}/all")
countries = response.json()
all_countries = map(lambda country_data: Country(country_data), countries)
return list(all_countries)
def get_random_country():
countries = get_all_countries()
random_code = random.choice(countries).code
return get_country(random_code)
def human_format(num):
"""
number formatter
https://stackoverflow.com/questions/579310/formatting-long-numbers-as-strings-in-python
"""
num = float('{:.2g}'.format(num))
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
return '{} {}'.format('{:f}'.format(num).rstrip('0').rstrip('.'), ['', 'K', 'M', 'B', 'T'][magnitude])