Conditions | 12 |
Total Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 1 | Features | 2 |
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 main() 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 | """A sample CLI with API interaction.""" |
||
21 | def main(): |
||
22 | logging.basicConfig(level=logging.INFO) |
||
23 | |||
24 | ARGPARSER = argparse.ArgumentParser() |
||
25 | ARGPARSER.add_argument('-l', '--loglevel', dest='loglevel', default=DEFAULT_LOGLEVEL, |
||
26 | action='store', required=False, |
||
27 | help='Level for logging (strings from logging python package: "warning", "info", "debug")') |
||
28 | ARGPARSER.add_argument('-a', '--api', dest='api', default=None, |
||
29 | action='store', required=True, choices=['airtable'], |
||
30 | help='Execute specified API: only "airtable" is currently supported') |
||
31 | ARGPARSER.add_argument('-k', '--api-key', dest='apikey', default="", |
||
32 | action='store', required=True, |
||
33 | help='Specify API key where appropriate (e.g. -k keyAnIuYcufa3dD)') |
||
34 | ARGPARSER.add_argument('-b', '--base', dest='base', default="", |
||
35 | action='store', required=True, |
||
36 | help='Specify Base ID where appropriate (e.g. -b appA8ZuLosBV4GDSd)') |
||
37 | ARGPARSER.add_argument('-t', '--table', dest='table', default="", |
||
38 | action='store', required=True, |
||
39 | help='Specify Table ID where appropriate (e.g. -t Tasks)') |
||
40 | ARGPARSER.add_argument('-v', '--view', dest='view', default="", |
||
41 | action='store', required=True, |
||
42 | help='Specify Table View where appropriate (e.g. -v Work)') |
||
43 | # ARGPARSER.add_argument('-o', '--output', dest='output', default=DEFAULT_OUTPUT, |
||
44 | # action='store', required=False, |
||
45 | # help='Output .tjp file for task-juggler') |
||
46 | ARGS = ARGPARSER.parse_args() |
||
47 | |||
48 | set_logging_level(ARGS.loglevel) |
||
49 | |||
50 | # PASSWORD = getpass('Enter generic password for {user}: '.format(user=ARGS.username)) |
||
51 | |||
52 | airtable = Airtable(ARGS.base, ARGS.table, api_key=ARGS.apikey) |
||
53 | |||
54 | data = [x["fields"] for x in airtable.get_all(view=ARGS.view)] |
||
55 | for rec in data: |
||
56 | preference = 0 |
||
57 | if "preference" in rec: |
||
58 | preference = int(rec['preference']) |
||
59 | if "priority" in rec: |
||
60 | if rec["priority"] == "Low": |
||
61 | pri = preference + 100 |
||
62 | elif rec["priority"] == "High": |
||
63 | pri = preference + 200 |
||
64 | elif rec["priority"] == "CRITICAL": |
||
65 | pri = preference + 300 |
||
66 | else: |
||
67 | pri = 1 |
||
68 | else: |
||
69 | pri = preference + 100 # low |
||
70 | rec["priority"] = pri |
||
71 | if 'appointment' in rec: |
||
72 | rec['start'] = rec['appointment'] |
||
73 | if 'depends' in rec: |
||
74 | rec['depends'] = [int(x) for x in re.findall(r"[\w']+", rec["depends"])] |
||
75 | |||
76 | JUGGLER = DictJuggler(data) |
||
77 | JUGGLER.run() |
||
78 | |||
79 | for t in JUGGLER.walk(juggler.JugglerTask): |
||
80 | airtable.update_by_field("id", t.get_id(), {"booking": t.walk(juggler.JugglerBooking)[0].decode()[0].isoformat()}) |
||
81 | |||
84 |