| Conditions | 46 |
| Total Lines | 210 |
| Code Lines | 168 |
| 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 |
||
| 26 | def calc_schedule(): |
||
| 27 | months = 13 |
||
| 28 | weeks = 53 |
||
| 29 | years = 1 |
||
| 30 | quarters = 4 |
||
| 31 | biweeks = 27 |
||
| 32 | |||
| 33 | try: |
||
| 34 | engine = db.create_engine(os.environ.get('DATABASE_URL')).connect() |
||
| 35 | except: |
||
| 36 | engine = db.create_engine('sqlite:///db.sqlite').connect() |
||
| 37 | |||
| 38 | # pull the schedule information |
||
| 39 | df = pd.read_sql('SELECT * FROM schedule;', engine) |
||
| 40 | total = pd.DataFrame(columns=['type', 'name', 'amount', 'date']) |
||
| 41 | |||
| 42 | # loop through the schedule and create transactions in a table out to the future number of years |
||
| 43 | todaydate = datetime.today().date() |
||
| 44 | for i in range(len(df.index)): |
||
| 45 | format = '%Y-%m-%d' |
||
| 46 | name = df['name'][i] |
||
| 47 | startdate = df['startdate'][i] |
||
| 48 | firstdate = df['firstdate'][i] |
||
| 49 | frequency = df['frequency'][i] |
||
| 50 | amount = df['amount'][i] |
||
| 51 | type = df['type'][i] |
||
| 52 | existing = Schedule.query.filter_by(name=name).first() |
||
| 53 | if not firstdate: |
||
| 54 | existing.firstdate = datetime.strptime(startdate, format).date() |
||
| 55 | firstdate = existing.firstdate.strftime(format) |
||
| 56 | db.session.commit() |
||
| 57 | if frequency == 'Monthly': |
||
| 58 | for k in range(months): |
||
| 59 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=k) |
||
| 60 | futuredateday = futuredate.day |
||
| 61 | firstdateday = datetime.strptime(firstdate, format).date().day |
||
| 62 | if firstdateday > futuredateday: |
||
| 63 | try: |
||
| 64 | for m in range(3): |
||
| 65 | futuredateday += 1 |
||
| 66 | if firstdateday >= futuredateday: |
||
| 67 | futuredate = futuredate.replace(day=futuredateday) |
||
| 68 | except ValueError: |
||
| 69 | pass |
||
| 70 | View Code Duplication | if futuredate <= todaydate and datetime.today().weekday() < 5: |
|
|
|
|||
| 71 | existing.startdate = futuredate + relativedelta(months=1) |
||
| 72 | daycheckdate = futuredate + relativedelta(months=1) |
||
| 73 | daycheck = daycheckdate.day |
||
| 74 | if firstdateday > daycheck: |
||
| 75 | try: |
||
| 76 | for m in range(3): |
||
| 77 | daycheck += 1 |
||
| 78 | if firstdateday >= daycheck: |
||
| 79 | existing.startdate = daycheckdate.replace(day=daycheck) |
||
| 80 | except ValueError: |
||
| 81 | pass |
||
| 82 | if type == 'Income': |
||
| 83 | rollbackdate = datetime.combine(futuredate, datetime.min.time()) |
||
| 84 | |||
| 85 | # Create a new row |
||
| 86 | new_row = { |
||
| 87 | 'type': type, |
||
| 88 | 'name': name, |
||
| 89 | 'amount': amount, |
||
| 90 | 'date': pd.tseries.offsets.BDay(1).rollback(rollbackdate).date() |
||
| 91 | } |
||
| 92 | # Append the row to the DataFrame |
||
| 93 | total = pd.concat([total, pd.DataFrame([new_row])], ignore_index=True) |
||
| 94 | else: |
||
| 95 | # Create a new row |
||
| 96 | new_row = { |
||
| 97 | 'type': type, |
||
| 98 | 'name': name, |
||
| 99 | 'amount': amount, |
||
| 100 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
| 101 | } |
||
| 102 | # Append the row to the DataFrame |
||
| 103 | total = pd.concat([total, pd.DataFrame([new_row])], ignore_index=True) |
||
| 104 | elif frequency == 'Weekly': |
||
| 105 | for k in range(weeks): |
||
| 106 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=k) |
||
| 107 | if futuredate <= todaydate and datetime.today().weekday() < 5: |
||
| 108 | existing.startdate = futuredate + relativedelta(weeks=1) |
||
| 109 | # Create a new row |
||
| 110 | new_row = { |
||
| 111 | 'type': type, |
||
| 112 | 'name': name, |
||
| 113 | 'amount': amount, |
||
| 114 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
| 115 | } |
||
| 116 | # Append the row to the DataFrame |
||
| 117 | total = pd.concat([total, pd.DataFrame([new_row])], ignore_index=True) |
||
| 118 | elif frequency == 'Yearly': |
||
| 119 | for k in range(years): |
||
| 120 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(years=k) |
||
| 121 | if futuredate <= todaydate and datetime.today().weekday() < 5: |
||
| 122 | existing.startdate = futuredate + relativedelta(years=1) |
||
| 123 | # Create a new row |
||
| 124 | new_row = { |
||
| 125 | 'type': type, |
||
| 126 | 'name': name, |
||
| 127 | 'amount': amount, |
||
| 128 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
| 129 | } |
||
| 130 | |||
| 131 | # Append the row to the DataFrame |
||
| 132 | total = pd.concat([total, pd.DataFrame([new_row])], ignore_index=True) |
||
| 133 | elif frequency == 'Quarterly': |
||
| 134 | for k in range(quarters): |
||
| 135 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=3 * k) |
||
| 136 | futuredateday = futuredate.day |
||
| 137 | firstdateday = datetime.strptime(firstdate, format).date().day |
||
| 138 | if firstdateday > futuredateday: |
||
| 139 | try: |
||
| 140 | for m in range(3): |
||
| 141 | futuredateday += 1 |
||
| 142 | if firstdateday >= futuredateday: |
||
| 143 | futuredate = futuredate.replace(day=futuredateday) |
||
| 144 | except ValueError: |
||
| 145 | pass |
||
| 146 | View Code Duplication | if futuredate <= todaydate and datetime.today().weekday() < 5: |
|
| 147 | existing.startdate = futuredate + relativedelta(months=3) |
||
| 148 | daycheckdate = futuredate + relativedelta(months=3) |
||
| 149 | daycheck = daycheckdate.day |
||
| 150 | if firstdateday > daycheck: |
||
| 151 | try: |
||
| 152 | for m in range(3): |
||
| 153 | daycheck += 1 |
||
| 154 | if firstdateday >= daycheck: |
||
| 155 | existing.startdate = daycheckdate.replace(day=daycheck) |
||
| 156 | except ValueError: |
||
| 157 | pass |
||
| 158 | # Create a new row |
||
| 159 | new_row = { |
||
| 160 | 'type': type, |
||
| 161 | 'name': name, |
||
| 162 | 'amount': amount, |
||
| 163 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
| 164 | } |
||
| 165 | # Append the row to the DataFrame |
||
| 166 | total = pd.concat([total, pd.DataFrame([new_row])], ignore_index=True) |
||
| 167 | elif frequency == 'BiWeekly': |
||
| 168 | for k in range(biweeks): |
||
| 169 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=2 * k) |
||
| 170 | if futuredate <= todaydate and datetime.today().weekday() < 5: |
||
| 171 | existing.startdate = futuredate + relativedelta(weeks=2) |
||
| 172 | # Create a new row |
||
| 173 | new_row = { |
||
| 174 | 'type': type, |
||
| 175 | 'name': name, |
||
| 176 | 'amount': amount, |
||
| 177 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
| 178 | } |
||
| 179 | # Append the row to the DataFrame |
||
| 180 | total = pd.concat([total, pd.DataFrame([new_row])], ignore_index=True) |
||
| 181 | elif frequency == 'Onetime': |
||
| 182 | futuredate = datetime.strptime(startdate, format).date() |
||
| 183 | if futuredate < todaydate: |
||
| 184 | db.session.delete(existing) |
||
| 185 | else: |
||
| 186 | # Create a new row |
||
| 187 | new_row = { |
||
| 188 | 'type': type, |
||
| 189 | 'name': name, |
||
| 190 | 'amount': amount, |
||
| 191 | 'date': futuredate |
||
| 192 | } |
||
| 193 | # Append the row to the DataFrame |
||
| 194 | total = pd.concat([total, pd.DataFrame([new_row])], ignore_index=True) |
||
| 195 | db.session.commit() |
||
| 196 | |||
| 197 | # add the hold items |
||
| 198 | df = pd.read_sql('SELECT * FROM hold;', engine) |
||
| 199 | for i in range(len(df.index)): |
||
| 200 | name = df['name'][i] |
||
| 201 | amount = df['amount'][i] |
||
| 202 | type = df['type'][i] |
||
| 203 | # Create a new row |
||
| 204 | new_row = { |
||
| 205 | 'type': type, |
||
| 206 | 'name': name, |
||
| 207 | 'amount': amount, |
||
| 208 | 'date': todaydate + relativedelta(days=1) |
||
| 209 | } |
||
| 210 | # Append the row to the DataFrame |
||
| 211 | total = pd.concat([total, pd.DataFrame([new_row])], ignore_index=True) |
||
| 212 | |||
| 213 | # add the skip items |
||
| 214 | df = pd.read_sql('SELECT * FROM skip;', engine) |
||
| 215 | for i in range(len(df.index)): |
||
| 216 | format = '%Y-%m-%d' |
||
| 217 | name = df['name'][i] |
||
| 218 | amount = df['amount'][i] |
||
| 219 | type = df['type'][i] |
||
| 220 | date = df['date'][i] |
||
| 221 | if datetime.strptime(date, format).date() < todaydate: |
||
| 222 | skip = Skip.query.filter_by(name=name).first() |
||
| 223 | db.session.delete(skip) |
||
| 224 | else: |
||
| 225 | # Create a new row |
||
| 226 | new_row = { |
||
| 227 | 'type': type, |
||
| 228 | 'name': name, |
||
| 229 | 'amount': amount, |
||
| 230 | 'date': datetime.strptime(date, format).date() |
||
| 231 | } |
||
| 232 | # Append the row to the DataFrame |
||
| 233 | total = pd.concat([total, pd.DataFrame([new_row])], ignore_index=True) |
||
| 234 | |||
| 235 | return total |
||
| 236 | |||
| 328 | return minbalance, graphJSON |