| Conditions | 41 |
| Total Lines | 144 |
| Code Lines | 132 |
| Lines | 24 |
| Ratio | 16.67 % |
| 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 |
||
| 68 | def calc_schedule(): |
||
| 69 | months = 13 |
||
| 70 | weeks = 53 |
||
| 71 | years = 1 |
||
| 72 | quarters = 4 |
||
| 73 | biweeks = 27 |
||
| 74 | |||
| 75 | try: |
||
| 76 | engine = db.create_engine(os.environ.get('DATABASE_URL')).connect() |
||
| 77 | except: |
||
| 78 | engine = db.create_engine('sqlite:///db.sqlite').connect() |
||
| 79 | |||
| 80 | # pull the schedule information |
||
| 81 | df = pd.read_sql('SELECT * FROM schedule;', engine) |
||
| 82 | |||
| 83 | # loop through the schedule and create transactions in a table out to the future number of years |
||
| 84 | todaydate = datetime.today().date() |
||
| 85 | for i in range(len(df.index)): |
||
| 86 | format = '%Y-%m-%d' |
||
| 87 | name = df['name'][i] |
||
| 88 | startdate = df['startdate'][i] |
||
| 89 | firstdate = df['firstdate'][i] |
||
| 90 | frequency = df['frequency'][i] |
||
| 91 | amount = df['amount'][i] |
||
| 92 | type = df['type'][i] |
||
| 93 | existing = Schedule.query.filter_by(name=name).first() |
||
| 94 | if not firstdate: |
||
| 95 | existing.firstdate = datetime.strptime(startdate, format).date() |
||
| 96 | firstdate = existing.firstdate.strftime(format) |
||
| 97 | db.session.commit() |
||
| 98 | if frequency == 'Monthly': |
||
| 99 | for k in range(months): |
||
| 100 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=k) |
||
| 101 | futuredateday = futuredate.day |
||
| 102 | firstdateday = datetime.strptime(firstdate, format).date().day |
||
| 103 | if firstdateday > futuredateday: |
||
| 104 | try: |
||
| 105 | for m in range(3): |
||
| 106 | futuredateday += 1 |
||
| 107 | if firstdateday >= futuredateday: |
||
| 108 | futuredate = futuredate.replace(day=futuredateday) |
||
| 109 | except ValueError: |
||
| 110 | pass |
||
| 111 | View Code Duplication | if futuredate <= todaydate: |
|
|
|
|||
| 112 | existing.startdate = futuredate + relativedelta(months=1) |
||
| 113 | daycheckdate = futuredate + relativedelta(months=1) |
||
| 114 | daycheck = daycheckdate.day |
||
| 115 | if firstdateday > daycheck: |
||
| 116 | try: |
||
| 117 | for m in range(3): |
||
| 118 | daycheck += 1 |
||
| 119 | if firstdateday >= daycheck: |
||
| 120 | existing.startdate = daycheckdate.replace(day=daycheck) |
||
| 121 | except ValueError: |
||
| 122 | pass |
||
| 123 | if type == 'Income': |
||
| 124 | rollbackdate = datetime.combine(futuredate, datetime.min.time()) |
||
| 125 | total = Total(type=type, name=name, amount=amount, |
||
| 126 | date=pd.tseries.offsets.BDay(1).rollback(rollbackdate).date()) |
||
| 127 | else: |
||
| 128 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 129 | db.session.add(total) |
||
| 130 | elif frequency == 'Weekly': |
||
| 131 | for k in range(weeks): |
||
| 132 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=k) |
||
| 133 | if futuredate <= todaydate: |
||
| 134 | existing.startdate = futuredate + relativedelta(weeks=1) |
||
| 135 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 136 | db.session.add(total) |
||
| 137 | elif frequency == 'Yearly': |
||
| 138 | for k in range(years): |
||
| 139 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(years=k) |
||
| 140 | if futuredate <= todaydate: |
||
| 141 | existing.startdate = futuredate + relativedelta(years=1) |
||
| 142 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 143 | db.session.add(total) |
||
| 144 | elif frequency == 'Quarterly': |
||
| 145 | for k in range(quarters): |
||
| 146 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=3 * k) |
||
| 147 | futuredateday = futuredate.day |
||
| 148 | firstdateday = datetime.strptime(firstdate, format).date().day |
||
| 149 | if firstdateday > futuredateday: |
||
| 150 | try: |
||
| 151 | for m in range(3): |
||
| 152 | futuredateday += 1 |
||
| 153 | if firstdateday >= futuredateday: |
||
| 154 | futuredate = futuredate.replace(day=futuredateday) |
||
| 155 | except ValueError: |
||
| 156 | pass |
||
| 157 | View Code Duplication | if futuredate <= todaydate: |
|
| 158 | existing.startdate = futuredate + relativedelta(months=3) |
||
| 159 | daycheckdate = futuredate + relativedelta(months=3) |
||
| 160 | daycheck = daycheckdate.day |
||
| 161 | if firstdateday > daycheck: |
||
| 162 | try: |
||
| 163 | for m in range(3): |
||
| 164 | daycheck += 1 |
||
| 165 | if firstdateday >= daycheck: |
||
| 166 | existing.startdate = daycheckdate.replace(day=daycheck) |
||
| 167 | except ValueError: |
||
| 168 | pass |
||
| 169 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 170 | db.session.add(total) |
||
| 171 | elif frequency == 'BiWeekly': |
||
| 172 | for k in range(biweeks): |
||
| 173 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=2 * k) |
||
| 174 | if futuredate <= todaydate: |
||
| 175 | existing.startdate = futuredate + relativedelta(weeks=2) |
||
| 176 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 177 | db.session.add(total) |
||
| 178 | elif frequency == 'Onetime': |
||
| 179 | futuredate = datetime.strptime(startdate, format).date() |
||
| 180 | if futuredate < todaydate: |
||
| 181 | db.session.delete(existing) |
||
| 182 | else: |
||
| 183 | total = Total(type=type, name=name, amount=amount, date=futuredate) |
||
| 184 | db.session.add(total) |
||
| 185 | db.session.commit() |
||
| 186 | |||
| 187 | # add the hold items |
||
| 188 | df = pd.read_sql('SELECT * FROM hold;', engine) |
||
| 189 | for i in range(len(df.index)): |
||
| 190 | name = df['name'][i] |
||
| 191 | amount = df['amount'][i] |
||
| 192 | type = df['type'][i] |
||
| 193 | total = Total(type=type, name=name, amount=amount, date=todaydate + relativedelta(days=1)) |
||
| 194 | db.session.add(total) |
||
| 195 | db.session.commit() |
||
| 196 | |||
| 197 | # add the skip items |
||
| 198 | df = pd.read_sql('SELECT * FROM skip;', engine) |
||
| 199 | for i in range(len(df.index)): |
||
| 200 | format = '%Y-%m-%d' |
||
| 201 | name = df['name'][i] |
||
| 202 | amount = df['amount'][i] |
||
| 203 | type = df['type'][i] |
||
| 204 | date = df['date'][i] |
||
| 205 | if datetime.strptime(date, format).date() < todaydate: |
||
| 206 | skip = Skip.query.filter_by(name=name).first() |
||
| 207 | db.session.delete(skip) |
||
| 208 | else: |
||
| 209 | total = Total(type=type, name=name, amount=amount, date=datetime.strptime(date, format).date()) |
||
| 210 | db.session.add(total) |
||
| 211 | db.session.commit() |
||
| 212 | |||
| 301 | return minbalance, graphJSON |