-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtrip.py
More file actions
76 lines (59 loc) · 2.1 KB
/
Copy pathtrip.py
File metadata and controls
76 lines (59 loc) · 2.1 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
from kivy.app import App
from kivy.lang import Builder
class Country:
def __init__(self, name, code, symbol):
self.name = name
self.code = code
self.symbol = symbol
def formatted_amount(self, amount):
return self.symbol + str(round(amount, 2))
def __str__(self):
return '{} ({})'.format(self.name, self.code)
@classmethod
def make(cls, data_list):
if not data_list:
raise Error("can't make country")
return Country(*data_list)
class Details:
def __init__(self):
self.locations = []
def add(self, country_name, start_date, end_date):
if start_date > end_date:
raise Error('invalid trip dates: {} {}'.format(start_date, end_date))
for location in self.locations:
if location[0] == start_date:
raise Error('{}-{} already added'.format(start_date, end_date))
self.locations.append((start_date, end_date, country_name))
def current_country(self, date_string):
for location in self.locations:
if location[0] <= date_string <= location[1]:
return location[2]
raise Error('invalid date')
def empty(self):
return len(self.locations) == 0
class Error(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
if __name__ == '__main__':
from currency import get_details
import time
print('test country class')
country = Country('Australia', 'AUD', '$')
print(country.formatted_amount(10.95))
country = Country.make(get_details("Turkey"))
print(country.formatted_amount(10.95))
print('test tripdetails class')
trip = Details()
trip.add(country, "2015/09/05", "2015/09/20")
trip.add(country, "2015/09/21", "2016/09/20")
try:
print(trip.current_country("2015/09/01"))
except Error as error:
print(error.value)
print(trip.current_country(time.strftime('%Y/%m/%d')))
try:
trip.add(country, "2015/09/05", "2015/09/20")
except Error as error:
print(error.value)