-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelper.py
More file actions
185 lines (156 loc) · 5.02 KB
/
helper.py
File metadata and controls
185 lines (156 loc) · 5.02 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import datetime
import pandas as pd
category_dict = {1:'Groceries', 2:'Entertainment', 3:'Travel', 4:'Shopping', 5:'Bills', 6:'Investments'}
def input_category():
# Category input string
return input('Enter Category as an Integer as follows: \n \
1 : Groceries \n \
2 : Entertainment \n \
3 : Travel \n \
4 : Shopping \n \
5 : Bills \n \
6 : Investments \n \
Category: ')
def validate_date(string):
'''
Given an input string, validate whether
it is a valid DD-MM-YYYY format
Returns True if it is valid, False otherwise
'''
try:
return bool(datetime.datetime.strptime(string, '%d-%m-%Y'))
except ValueError:
return False
def validate_month(string):
'''
Given an input string, validate whether
it is a valid MM-YYYY format
Returns True if it is valid, False otherwise
'''
try:
return bool(datetime.datetime.strptime(string, '%m-%Y'))
except ValueError:
return False
def validate_year(string):
'''
Given an input string, validate whether
it is a valid YYYY format
Returns True if it is valid, False otherwise
'''
try:
return bool(datetime.datetime.strptime(string, '%Y'))
except ValueError:
return False
def validate_amount(string):
'''
Validation of input as a valid float
'''
try:
amount = float(string)
return (amount > 0), amount
except:
return False, -1
def validate_category(string):
'''
Given input string, validate
whether it is an integer between 1 and 6
'''
try:
category = int(string)
if 0 < category <= 6:
return True
except:
pass
return False
def validate_column_names(df):
'''
Parameters
----------
df : Panda dataframe
any panda dataframe.
Returns
-------
Returns False if a column is missing.
'''
header_list = ['Title', 'Category', 'Date', 'Amount']
try:
import_headers = df.axes[1] #==> 1 is to identify columns
miss_match_header = [i for i in import_headers if i not in header_list]
# assert (len(miss_match_header) == 0)
if len(miss_match_header) != 0:
raise NameError("The first row of your ./Expense.csv file is not excepted header list.")
except:
print('The first row of your ./Expense.csv file is not excepted header list.')
print('Check your csv if the first row looks like this: \n \
Title,Category,Date,Amount')
print('or delete csv.')
return False
return True
def validate_row_data(df):
'''
Checks if every row in every column has the right format
Parameters
----------
df : Panda Dataframe
Dataframe with the columns Title, Category, Date and Amount.
Returns
-------
Prints out a error message where the faulty entry can be found.
Terminates the execution of the code.
'''
all_columns = ['Title', 'Category', 'Date', 'Amount']
faulty_date_rows = []
faulty_category_rows = []
faulty_amount_rows = []
# Check every row for wrong formats
for row in range(len(df)):
if not validate_category(df['Category'][row]):
faulty_category_rows.append(row + 1)
if not validate_date(df['Date'][row]):
faulty_date_rows.append(row + 1)
if not validate_amount(df['Amount'][row])[0]:
faulty_amount_rows.append(row + 1)
cat_bool = faulty_category_rows == []
date_bool = faulty_date_rows == []
amount_bool = faulty_amount_rows == []
# Return which rows contain the entries with the wrong format
if date_bool == False or cat_bool == False or amount_bool == False:
wrong_format = ' in the following rows are not in the right format:'
ask_str = 'Please change the format to:'
print('Your csv contains entries, that are not in the right format')
if cat_bool == False:
print('The Category' + wrong_format)
print(faulty_category_rows)
print(ask_str)
print('An whole number from 1 to 6')
if date_bool == False:
print('The Date' + wrong_format)
print(faulty_date_rows)
print(ask_str)
print('DD-MM-YYYY')
if amount_bool == False:
print('The Amount' + wrong_format)
print(faulty_amount_rows)
print(ask_str)
print('A number that can be converted to a float')
return False
return True
def days_in_month(month, year):
'''
Returns the number of days in the input Month
'''
def leap_year(year):
if year % 400 == 0:
return True
if year % 100 == 0:
return False
if year % 4 == 0:
return True
return False
if month in {1, 3, 5, 7, 8, 10, 12}:
return 31
if month == 2:
if leap_year(year):
return 29
return 28
return 30