| Conditions | 24 |
| Total Lines | 99 |
| Code Lines | 87 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 65 | def calc_schedule(): |
||
| 66 | months = 13 |
||
| 67 | weeks = 53 |
||
| 68 | years = 1 |
||
| 69 | quarters = 4 |
||
| 70 | biweeks = 27 |
||
| 71 | |||
| 72 | try: |
||
| 73 | engine = db.create_engine(os.environ.get('DATABASE_URL')).connect() |
||
| 74 | except: |
||
| 75 | engine = db.create_engine('sqlite:///db.sqlite').connect() |
||
| 76 | |||
| 77 | # pull the schedule information |
||
| 78 | df = pd.read_sql('SELECT * FROM schedule;', engine) |
||
| 79 | |||
| 80 | # loop through the schedule and create transactions in a table out to the future number of years |
||
| 81 | todaydate = datetime.today().date() |
||
| 82 | for i in range(len(df.index)): |
||
| 83 | format = '%Y-%m-%d' |
||
| 84 | name = df['name'][i] |
||
| 85 | startdate = df['startdate'][i] |
||
| 86 | frequency = df['frequency'][i] |
||
| 87 | amount = df['amount'][i] |
||
| 88 | type = df['type'][i] |
||
| 89 | existing = Schedule.query.filter_by(name=name).first() |
||
| 90 | if frequency == 'Monthly': |
||
| 91 | for k in range(months): |
||
| 92 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=k) |
||
| 93 | if futuredate <= todaydate: |
||
| 94 | existing.startdate = futuredate + relativedelta(months=1) |
||
| 95 | if type == 'Income': |
||
| 96 | rollbackdate = datetime.combine(futuredate, datetime.min.time()) |
||
| 97 | total = Total(type=type, name=name, amount=amount, |
||
| 98 | date=pd.tseries.offsets.BDay(1).rollback(rollbackdate).date()) |
||
| 99 | else: |
||
| 100 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 101 | db.session.add(total) |
||
| 102 | elif frequency == 'Weekly': |
||
| 103 | for k in range(weeks): |
||
| 104 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=k) |
||
| 105 | if futuredate <= todaydate: |
||
| 106 | existing.startdate = futuredate + relativedelta(weeks=1) |
||
| 107 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 108 | db.session.add(total) |
||
| 109 | elif frequency == 'Yearly': |
||
| 110 | for k in range(years): |
||
| 111 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(years=k) |
||
| 112 | if futuredate <= todaydate: |
||
| 113 | existing.startdate = futuredate + relativedelta(years=1) |
||
| 114 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 115 | db.session.add(total) |
||
| 116 | elif frequency == 'Quarterly': |
||
| 117 | for k in range(quarters): |
||
| 118 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(months=3 * k) |
||
| 119 | if futuredate <= todaydate: |
||
| 120 | existing.startdate = futuredate + relativedelta(months=3) |
||
| 121 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 122 | db.session.add(total) |
||
| 123 | elif frequency == 'BiWeekly': |
||
| 124 | for k in range(biweeks): |
||
| 125 | futuredate = datetime.strptime(startdate, format).date() + relativedelta(weeks=2 * k) |
||
| 126 | if futuredate <= todaydate: |
||
| 127 | existing.startdate = futuredate + relativedelta(weeks=2) |
||
| 128 | total = Total(type=type, name=name, amount=amount, date=futuredate - pd.tseries.offsets.BDay(0)) |
||
| 129 | db.session.add(total) |
||
| 130 | elif frequency == 'Onetime': |
||
| 131 | futuredate = datetime.strptime(startdate, format).date() |
||
| 132 | if futuredate < todaydate: |
||
| 133 | db.session.delete(existing) |
||
| 134 | else: |
||
| 135 | total = Total(type=type, name=name, amount=amount, date=futuredate) |
||
| 136 | db.session.add(total) |
||
| 137 | db.session.commit() |
||
| 138 | |||
| 139 | # add the hold items |
||
| 140 | df = pd.read_sql('SELECT * FROM hold;', engine) |
||
| 141 | for i in range(len(df.index)): |
||
| 142 | name = df['name'][i] |
||
| 143 | amount = df['amount'][i] |
||
| 144 | type = df['type'][i] |
||
| 145 | total = Total(type=type, name=name, amount=amount, date=todaydate + relativedelta(days=1)) |
||
| 146 | db.session.add(total) |
||
| 147 | db.session.commit() |
||
| 148 | |||
| 149 | # add the skip items |
||
| 150 | df = pd.read_sql('SELECT * FROM skip;', engine) |
||
| 151 | for i in range(len(df.index)): |
||
| 152 | format = '%Y-%m-%d' |
||
| 153 | name = df['name'][i] |
||
| 154 | amount = df['amount'][i] |
||
| 155 | type = df['type'][i] |
||
| 156 | date = df['date'][i] |
||
| 157 | if datetime.strptime(date, format).date() < todaydate: |
||
| 158 | skip = Skip.query.filter_by(name=name).first() |
||
| 159 | db.session.delete(skip) |
||
| 160 | else: |
||
| 161 | total = Total(type=type, name=name, amount=amount, date=datetime.strptime(date, format).date()) |
||
| 162 | db.session.add(total) |
||
| 163 | db.session.commit() |
||
| 164 | |||
| 253 | return minbalance, graphJSON |