-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.py
82 lines (69 loc) · 2.38 KB
/
task.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
from PyInquirer import prompt
from PyInquirer import Validator, ValidationError
from utils import count_sundays, check_day, format_output
OUTPUT_FILENAME = 'result.log'
class DateValidator(Validator):
def validate(self, document):
try:
day, month, year = map(int, document.text.split('.'))
assert 1 <= day <= 31
assert 1 <= month <= 12
assert 1900 <= year <= 2500
if year == 2500 and (month != 1 or day != 1):
raise ValidationError(
message='Date ranges from 1 Jan 1900 to 1 Jan 2500',
cursor_position=len(document.text) # Move cursor to the end
)
except (ValueError, AssertionError):
raise ValidationError(
message='Please enter a valid date in format dd.mm.yyyy',
cursor_position=len(document.text) # Move cursor to the end
)
def date_filter(value):
day, month, year = map(int, value.split('.'))
filtered_value = {
'day': day,
'month': month,
'year': year,
'formatted_date': value,
'day_of_week': check_day(day, month, year)
}
return filtered_value
questions = [
{
'type': 'input',
'name': 'start_date',
'message': 'Give me the first date:',
'validate': DateValidator,
'filter': date_filter,
},
{
'type': 'input',
'name': 'end_date',
'message': 'Give me the second date:',
'validate': DateValidator,
'filter': date_filter,
},
{
'type': 'list',
'name': 'output_type',
'message': 'Would you like to get information on screen, in file or both?',
'choices': ['screen', 'file', 'both'],
},
{
'type': 'list',
'name': 'order',
'message': 'Do you want this information in ascending or descending order?',
'choices': ['ascending', 'descending'],
}
]
if __name__ == '__main__':
answers = prompt(questions)
total_sundays = count_sundays(answers['start_date'], answers['end_date'])
result_output = format_output(answers, total_sundays)
output_type = answers['output_type']
if output_type in ('screen', 'both'):
print(result_output)
if output_type in ('file', 'both'):
with open(OUTPUT_FILENAME, 'w+') as file:
file.write(result_output)