Conditions | 7 |
Total Lines | 60 |
Code Lines | 35 |
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:
1 | import falcon |
||
27 | @staticmethod |
||
28 | def on_get(req, resp): |
||
29 | print(req.params) |
||
30 | reporting_start_datetime_local = req.params.get('reportingperiodstartdatetime') |
||
31 | reporting_end_datetime_local = req.params.get('reportingperiodenddatetime') |
||
32 | |||
33 | ################################################################################################################ |
||
34 | # Step 1: valid parameters |
||
35 | ################################################################################################################ |
||
36 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
37 | if config.utc_offset[0] == '-': |
||
38 | timezone_offset = -timezone_offset |
||
39 | |||
40 | if reporting_start_datetime_local is None: |
||
41 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
42 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
43 | else: |
||
44 | reporting_start_datetime_local = str.strip(reporting_start_datetime_local) |
||
45 | try: |
||
46 | reporting_start_datetime_utc = datetime.strptime(reporting_start_datetime_local, |
||
47 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
48 | timedelta(minutes=timezone_offset) |
||
49 | except ValueError: |
||
50 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
51 | description="API.INVALID_REPORTING_PERIOD_START_DATETIME") |
||
52 | |||
53 | if reporting_end_datetime_local is None: |
||
54 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
55 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
56 | else: |
||
57 | reporting_end_datetime_local = str.strip(reporting_end_datetime_local) |
||
58 | try: |
||
59 | reporting_end_datetime_utc = datetime.strptime(reporting_end_datetime_local, |
||
60 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
61 | timedelta(minutes=timezone_offset) |
||
62 | except ValueError: |
||
63 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
64 | description="API.INVALID_REPORTING_PERIOD_END_DATETIME") |
||
65 | |||
66 | if reporting_start_datetime_utc >= reporting_end_datetime_utc: |
||
67 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
68 | description='API.INVALID_REPORTING_PERIOD_END_DATETIME') |
||
69 | ################################################################################################################ |
||
70 | # Step 2: query the distribution system |
||
71 | ################################################################################################################ |
||
72 | |||
73 | ################################################################################################################ |
||
74 | # Step 3: query meters |
||
75 | ################################################################################################################ |
||
76 | |||
77 | ################################################################################################################ |
||
78 | # Step 4: query reporting period energy input |
||
79 | ################################################################################################################ |
||
80 | |||
81 | ################################################################################################################ |
||
82 | # Step 5: construct the report |
||
83 | ################################################################################################################ |
||
84 | |||
85 | result = {} |
||
86 | resp.body = json.dumps(result) |
||
87 |