| Conditions | 51 |
| Total Lines | 232 |
| Code Lines | 171 |
| Lines | 24 |
| Ratio | 10.34 % |
| 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 |
||
| 38 | def calc_schedule(schedules, holds, skips): |
||
| 39 | """ |
||
| 40 | Process schedules, holds, and skips into projected transactions |
||
| 41 | |||
| 42 | Args: |
||
| 43 | schedules: List of Schedule objects (pre-filtered for user) |
||
| 44 | holds: List of Hold objects (pre-filtered for user) |
||
| 45 | skips: List of Skip objects (pre-filtered for user) |
||
| 46 | |||
| 47 | Returns: |
||
| 48 | DataFrame of all projected transactions |
||
| 49 | """ |
||
| 50 | months = 13 |
||
| 51 | weeks = 53 |
||
| 52 | years = 1 |
||
| 53 | quarters = 4 |
||
| 54 | biweeks = 27 |
||
| 55 | |||
| 56 | # Create lookup dictionaries to avoid re-querying |
||
| 57 | schedule_objects = {s.name: s for s in schedules} |
||
| 58 | skip_objects = {s.name: s for s in skips} |
||
| 59 | |||
| 60 | # Convert schedules to DataFrame |
||
| 61 | if schedules: |
||
| 62 | df = pd.DataFrame([{ |
||
| 63 | 'name': s.name, |
||
| 64 | 'startdate': s.startdate.strftime('%Y-%m-%d') if s.startdate else None, |
||
| 65 | 'firstdate': s.firstdate.strftime('%Y-%m-%d') if s.firstdate else None, |
||
| 66 | 'frequency': s.frequency, |
||
| 67 | 'amount': s.amount, |
||
| 68 | 'type': s.type |
||
| 69 | } for s in schedules]) |
||
| 70 | else: |
||
| 71 | # Empty DataFrame if no schedules |
||
| 72 | df = pd.DataFrame(columns=['name', 'startdate', 'firstdate', 'frequency', 'amount', 'type']) |
||
| 73 | |||
| 74 | total_dict = {} |
||
| 75 | |||
| 76 | # loop through the schedule and create transactions in a table out to the future number of years |
||
| 77 | todaydate = datetime.today().date() |
||
| 78 | for i in df.itertuples(index=False): |
||
| 79 | format = '%Y-%m-%d' |
||
| 80 | name = i.name |
||
| 81 | startdate = i.startdate |
||
| 82 | firstdate = i.firstdate |
||
| 83 | frequency = i.frequency |
||
| 84 | amount = i.amount |
||
| 85 | type = i.type |
||
| 86 | existing = schedule_objects.get(name) |
||
| 87 | if not existing: |
||
| 88 | continue # Skip if schedule object not found |
||
| 89 | if not firstdate: |
||
| 90 | existing.firstdate = datetime.strptime(startdate, format).date() |
||
| 91 | firstdate = existing.firstdate.strftime(format) |
||
| 92 | db.session.commit() |
||
| 93 | if frequency == 'Monthly': |
||
| 94 | for k in range(months): |
||
| 95 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=k) |
||
| 96 | futuredateday = futuredate.day |
||
| 97 | firstdateday = datetime.strptime(firstdate, format).date().day |
||
| 98 | if firstdateday > futuredateday: |
||
| 99 | try: |
||
| 100 | for m in range(3): |
||
| 101 | futuredateday += 1 |
||
| 102 | if firstdateday >= futuredateday: |
||
| 103 | futuredate = futuredate.replace(day=futuredateday) |
||
| 104 | except ValueError: |
||
| 105 | pass |
||
| 106 | View Code Duplication | if futuredate <= todaydate and datetime.today().weekday() < 5: |
|
|
|
|||
| 107 | existing.startdate = futuredate + relativedelta(months=1) |
||
| 108 | daycheckdate = futuredate + relativedelta(months=1) |
||
| 109 | daycheck = daycheckdate.day |
||
| 110 | if firstdateday > daycheck: |
||
| 111 | try: |
||
| 112 | for m in range(3): |
||
| 113 | daycheck += 1 |
||
| 114 | if firstdateday >= daycheck: |
||
| 115 | existing.startdate = daycheckdate.replace(day=daycheck) |
||
| 116 | except ValueError: |
||
| 117 | pass |
||
| 118 | if type == 'Income': |
||
| 119 | rollbackdate = datetime.combine(futuredate, datetime.min.time()) |
||
| 120 | # Create a new row |
||
| 121 | new_row = { |
||
| 122 | 'type': type, |
||
| 123 | 'name': name, |
||
| 124 | 'amount': amount, |
||
| 125 | 'date': pd.tseries.offsets.BDay(1).rollback(rollbackdate).date() |
||
| 126 | } |
||
| 127 | # Append the row to the DataFrame |
||
| 128 | total_dict[len(total_dict)] = new_row |
||
| 129 | else: |
||
| 130 | # Create a new row |
||
| 131 | new_row = { |
||
| 132 | 'type': type, |
||
| 133 | 'name': name, |
||
| 134 | 'amount': amount, |
||
| 135 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
| 136 | } |
||
| 137 | # Append the row to the DataFrame |
||
| 138 | total_dict[len(total_dict)] = new_row |
||
| 139 | elif frequency == 'Weekly': |
||
| 140 | for k in range(weeks): |
||
| 141 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=k) |
||
| 142 | if futuredate <= todaydate and datetime.today().weekday() < 5: |
||
| 143 | existing.startdate = futuredate + relativedelta(weeks=1) |
||
| 144 | # Create a new row |
||
| 145 | new_row = { |
||
| 146 | 'type': type, |
||
| 147 | 'name': name, |
||
| 148 | 'amount': amount, |
||
| 149 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
| 150 | } |
||
| 151 | # Append the row to the DataFrame |
||
| 152 | total_dict[len(total_dict)] = new_row |
||
| 153 | elif frequency == 'Yearly': |
||
| 154 | for k in range(years): |
||
| 155 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(years=k) |
||
| 156 | if futuredate <= todaydate and datetime.today().weekday() < 5: |
||
| 157 | existing.startdate = futuredate + relativedelta(years=1) |
||
| 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_dict[len(total_dict)] = new_row |
||
| 167 | elif frequency == 'Quarterly': |
||
| 168 | for k in range(quarters): |
||
| 169 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=3 * k) |
||
| 170 | futuredateday = futuredate.day |
||
| 171 | firstdateday = datetime.strptime(firstdate, format).date().day |
||
| 172 | if firstdateday > futuredateday: |
||
| 173 | try: |
||
| 174 | for m in range(3): |
||
| 175 | futuredateday += 1 |
||
| 176 | if firstdateday >= futuredateday: |
||
| 177 | futuredate = futuredate.replace(day=futuredateday) |
||
| 178 | except ValueError: |
||
| 179 | pass |
||
| 180 | View Code Duplication | if futuredate <= todaydate and datetime.today().weekday() < 5: |
|
| 181 | existing.startdate = futuredate + relativedelta(months=3) |
||
| 182 | daycheckdate = futuredate + relativedelta(months=3) |
||
| 183 | daycheck = daycheckdate.day |
||
| 184 | if firstdateday > daycheck: |
||
| 185 | try: |
||
| 186 | for m in range(3): |
||
| 187 | daycheck += 1 |
||
| 188 | if firstdateday >= daycheck: |
||
| 189 | existing.startdate = daycheckdate.replace(day=daycheck) |
||
| 190 | except ValueError: |
||
| 191 | pass |
||
| 192 | # Create a new row |
||
| 193 | new_row = { |
||
| 194 | 'type': type, |
||
| 195 | 'name': name, |
||
| 196 | 'amount': amount, |
||
| 197 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
| 198 | } |
||
| 199 | # Append the row to the DataFrame |
||
| 200 | total_dict[len(total_dict)] = new_row |
||
| 201 | elif frequency == 'BiWeekly': |
||
| 202 | for k in range(biweeks): |
||
| 203 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=2 * k) |
||
| 204 | if futuredate <= todaydate and datetime.today().weekday() < 5: |
||
| 205 | existing.startdate = futuredate + relativedelta(weeks=2) |
||
| 206 | # Create a new row |
||
| 207 | new_row = { |
||
| 208 | 'type': type, |
||
| 209 | 'name': name, |
||
| 210 | 'amount': amount, |
||
| 211 | 'date': (futuredate - pd.tseries.offsets.BDay(0)).date() |
||
| 212 | } |
||
| 213 | # Append the row to the DataFrame |
||
| 214 | total_dict[len(total_dict)] = new_row |
||
| 215 | elif frequency == 'Onetime': |
||
| 216 | futuredate = datetime.strptime(startdate, format).date() |
||
| 217 | if futuredate < todaydate: |
||
| 218 | db.session.delete(existing) |
||
| 219 | else: |
||
| 220 | # Create a new row |
||
| 221 | new_row = { |
||
| 222 | 'type': type, |
||
| 223 | 'name': name, |
||
| 224 | 'amount': amount, |
||
| 225 | 'date': futuredate |
||
| 226 | } |
||
| 227 | # Append the row to the DataFrame |
||
| 228 | total_dict[len(total_dict)] = new_row |
||
| 229 | db.session.commit() |
||
| 230 | |||
| 231 | # add the hold items |
||
| 232 | for hold in holds: |
||
| 233 | # Create a new row |
||
| 234 | new_row = { |
||
| 235 | 'type': hold.type, |
||
| 236 | 'name': hold.name, |
||
| 237 | 'amount': hold.amount, |
||
| 238 | 'date': todaydate + relativedelta(days=1) |
||
| 239 | } |
||
| 240 | # Append the row to the DataFrame |
||
| 241 | total_dict[len(total_dict)] = new_row |
||
| 242 | |||
| 243 | # add the skip items |
||
| 244 | for skip in skips: |
||
| 245 | format = '%Y-%m-%d' |
||
| 246 | skip_date = skip.date if isinstance(skip.date, date) else datetime.strptime(skip.date, format).date() |
||
| 247 | |||
| 248 | if skip_date < todaydate: |
||
| 249 | # Delete past skip items |
||
| 250 | db.session.delete(skip) |
||
| 251 | else: |
||
| 252 | # Create a new row |
||
| 253 | new_row = { |
||
| 254 | 'type': skip.type, |
||
| 255 | 'name': skip.name, |
||
| 256 | 'amount': skip.amount, |
||
| 257 | 'date': skip_date |
||
| 258 | } |
||
| 259 | # Append the row to the DataFrame |
||
| 260 | total_dict[len(total_dict)] = new_row |
||
| 261 | |||
| 262 | # Create DataFrame from total_dict |
||
| 263 | if total_dict: |
||
| 264 | total = pd.DataFrame.from_dict(total_dict, orient="index") |
||
| 265 | else: |
||
| 266 | # Return empty DataFrame with expected columns |
||
| 267 | total = pd.DataFrame(columns=['type', 'name', 'amount', 'date']) |
||
| 268 | |||
| 269 | return total |
||
| 270 | |||
| 378 | return minbalance, graphJSON |