Conditions | 46 |
Total Lines | 210 |
Code Lines | 169 |
Lines | 24 |
Ratio | 11.43 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like app.cashflow.calc_schedule() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from app import db |
||
25 | def calc_schedule(): |
||
26 | months = 13 |
||
27 | weeks = 53 |
||
28 | years = 1 |
||
29 | quarters = 4 |
||
30 | biweeks = 27 |
||
31 | |||
32 | try: |
||
33 | engine = db.create_engine(os.environ.get('DATABASE_URL')).connect() |
||
34 | except: |
||
35 | engine = db.create_engine('sqlite:///db.sqlite').connect() |
||
36 | |||
37 | # pull the schedule information |
||
38 | df = pd.read_sql('SELECT * FROM schedule;', engine) |
||
39 | total_dict = {} |
||
40 | |||
41 | # loop through the schedule and create transactions in a table out to the future number of years |
||
42 | todaydate = datetime.today().date() |
||
43 | for i in df.itertuples(index=False): |
||
44 | format = '%Y-%m-%d' |
||
45 | name = i.name |
||
46 | startdate = i.startdate |
||
47 | firstdate = i.firstdate |
||
48 | frequency = i.frequency |
||
49 | amount = i.amount |
||
50 | type = i.type |
||
51 | existing = Schedule.query.filter_by(name=name).first() |
||
52 | if not firstdate: |
||
53 | existing.firstdate = datetime.strptime(startdate, format).date() |
||
54 | firstdate = existing.firstdate.strftime(format) |
||
55 | db.session.commit() |
||
56 | if frequency == 'Monthly': |
||
57 | for k in range(months): |
||
58 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=k) |
||
59 | futuredateday = futuredate.day |
||
60 | firstdateday = datetime.strptime(firstdate, format).date().day |
||
61 | if firstdateday > futuredateday: |
||
62 | try: |
||
63 | for m in range(3): |
||
64 | futuredateday += 1 |
||
65 | if firstdateday >= futuredateday: |
||
66 | futuredate = futuredate.replace(day=futuredateday) |
||
67 | except ValueError: |
||
68 | pass |
||
69 | View Code Duplication | if futuredate <= todaydate and datetime.today().weekday() < 5: |
|
|
|||
70 | existing.startdate = futuredate + relativedelta(months=1) |
||
71 | daycheckdate = futuredate + relativedelta(months=1) |
||
72 | daycheck = daycheckdate.day |
||
73 | if firstdateday > daycheck: |
||
74 | try: |
||
75 | for m in range(3): |
||
76 | daycheck += 1 |
||
77 | if firstdateday >= daycheck: |
||
78 | existing.startdate = daycheckdate.replace(day=daycheck) |
||
79 | except ValueError: |
||
80 | pass |
||
81 | if type == 'Income': |
||
82 | rollbackdate = datetime.combine(futuredate, datetime.min.time()) |
||
83 | # Create a new row |
||
84 | new_row = { |
||
85 | 'type': type, |
||
86 | 'name': name, |
||
87 | 'amount': amount, |
||
88 | 'date': pd.tseries.offsets.BDay(1).rollback(rollbackdate).date() |
||
89 | } |
||
90 | # Append the row to the DataFrame |
||
91 | total_dict[len(total_dict)] = new_row |
||
92 | else: |
||
93 | # Create a new row |
||
94 | new_row = { |
||
95 | 'type': type, |
||
96 | 'name': name, |
||
97 | 'amount': amount, |
||
98 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
99 | } |
||
100 | # Append the row to the DataFrame |
||
101 | total_dict[len(total_dict)] = new_row |
||
102 | elif frequency == 'Weekly': |
||
103 | for k in range(weeks): |
||
104 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=k) |
||
105 | if futuredate <= todaydate and datetime.today().weekday() < 5: |
||
106 | existing.startdate = futuredate + relativedelta(weeks=1) |
||
107 | # Create a new row |
||
108 | new_row = { |
||
109 | 'type': type, |
||
110 | 'name': name, |
||
111 | 'amount': amount, |
||
112 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
113 | } |
||
114 | # Append the row to the DataFrame |
||
115 | total_dict[len(total_dict)] = new_row |
||
116 | elif frequency == 'Yearly': |
||
117 | for k in range(years): |
||
118 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(years=k) |
||
119 | if futuredate <= todaydate and datetime.today().weekday() < 5: |
||
120 | existing.startdate = futuredate + relativedelta(years=1) |
||
121 | # Create a new row |
||
122 | new_row = { |
||
123 | 'type': type, |
||
124 | 'name': name, |
||
125 | 'amount': amount, |
||
126 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
127 | } |
||
128 | # Append the row to the DataFrame |
||
129 | total_dict[len(total_dict)] = new_row |
||
130 | elif frequency == 'Quarterly': |
||
131 | for k in range(quarters): |
||
132 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=3 * k) |
||
133 | futuredateday = futuredate.day |
||
134 | firstdateday = datetime.strptime(firstdate, format).date().day |
||
135 | if firstdateday > futuredateday: |
||
136 | try: |
||
137 | for m in range(3): |
||
138 | futuredateday += 1 |
||
139 | if firstdateday >= futuredateday: |
||
140 | futuredate = futuredate.replace(day=futuredateday) |
||
141 | except ValueError: |
||
142 | pass |
||
143 | View Code Duplication | if futuredate <= todaydate and datetime.today().weekday() < 5: |
|
144 | existing.startdate = futuredate + relativedelta(months=3) |
||
145 | daycheckdate = futuredate + relativedelta(months=3) |
||
146 | daycheck = daycheckdate.day |
||
147 | if firstdateday > daycheck: |
||
148 | try: |
||
149 | for m in range(3): |
||
150 | daycheck += 1 |
||
151 | if firstdateday >= daycheck: |
||
152 | existing.startdate = daycheckdate.replace(day=daycheck) |
||
153 | except ValueError: |
||
154 | pass |
||
155 | # Create a new row |
||
156 | new_row = { |
||
157 | 'type': type, |
||
158 | 'name': name, |
||
159 | 'amount': amount, |
||
160 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
161 | } |
||
162 | # Append the row to the DataFrame |
||
163 | total_dict[len(total_dict)] = new_row |
||
164 | elif frequency == 'BiWeekly': |
||
165 | for k in range(biweeks): |
||
166 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=2 * k) |
||
167 | if futuredate <= todaydate and datetime.today().weekday() < 5: |
||
168 | existing.startdate = futuredate + relativedelta(weeks=2) |
||
169 | # Create a new row |
||
170 | new_row = { |
||
171 | 'type': type, |
||
172 | 'name': name, |
||
173 | 'amount': amount, |
||
174 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
175 | } |
||
176 | # Append the row to the DataFrame |
||
177 | total_dict[len(total_dict)] = new_row |
||
178 | elif frequency == 'Onetime': |
||
179 | futuredate = datetime.strptime(startdate, format).date() |
||
180 | if futuredate < todaydate: |
||
181 | db.session.delete(existing) |
||
182 | else: |
||
183 | # Create a new row |
||
184 | new_row = { |
||
185 | 'type': type, |
||
186 | 'name': name, |
||
187 | 'amount': amount, |
||
188 | 'date': futuredate |
||
189 | } |
||
190 | # Append the row to the DataFrame |
||
191 | total_dict[len(total_dict)] = new_row |
||
192 | db.session.commit() |
||
193 | |||
194 | # add the hold items |
||
195 | df = pd.read_sql('SELECT * FROM hold;', engine) |
||
196 | for i in df.itertuples(index=False): |
||
197 | name = i.name |
||
198 | amount = i.amount |
||
199 | type = i.type |
||
200 | # Create a new row |
||
201 | new_row = { |
||
202 | 'type': type, |
||
203 | 'name': name, |
||
204 | 'amount': amount, |
||
205 | 'date': todaydate + relativedelta(days=1) |
||
206 | } |
||
207 | # Append the row to the DataFrame |
||
208 | total_dict[len(total_dict)] = new_row |
||
209 | |||
210 | # add the skip items |
||
211 | df = pd.read_sql('SELECT * FROM skip;', engine) |
||
212 | for i in df.itertuples(index=False): |
||
213 | format = '%Y-%m-%d' |
||
214 | name = i.name |
||
215 | amount = i.amount |
||
216 | type = i.type |
||
217 | date = i.date |
||
218 | if datetime.strptime(date, format).date() < todaydate: |
||
219 | skip = Skip.query.filter_by(name=name).first() |
||
220 | db.session.delete(skip) |
||
221 | else: |
||
222 | # Create a new row |
||
223 | new_row = { |
||
224 | 'type': type, |
||
225 | 'name': name, |
||
226 | 'amount': amount, |
||
227 | 'date': datetime.strptime(date, format).date() |
||
228 | } |
||
229 | # Append the row to the DataFrame |
||
230 | total_dict[len(total_dict)] = new_row |
||
231 | |||
232 | total = pd.DataFrame.from_dict(total_dict, orient="index") |
||
233 | |||
234 | return total |
||
235 | |||
332 | return minbalance, graphJSON |