Conditions | 12 |
Total Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 autolookup_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 | #!/usr/bin/env python3 |
||
110 | @decorators.wrap_keyboard_except |
||
111 | def autolookup_main(osversion, loop=False, log=False, |
||
112 | autogen=False, inc=3, sql=False, |
||
113 | quiet=False, ceiling=9996, mailer=False): |
||
114 | """ |
||
115 | Lookup a software release from an OS. Can iterate. |
||
116 | |||
117 | :param osversion: OS version, 10.x.y.zzzz. |
||
118 | :type osversion: str |
||
119 | |||
120 | :param loop: Whether or not to automatically lookup. Default is false. |
||
121 | :type loop: bool |
||
122 | |||
123 | :param log: Whether to log. Default is false. |
||
124 | :type log: bool |
||
125 | |||
126 | :param autogen: Whether to create text links. Default is false. |
||
127 | :type autogen: bool |
||
128 | |||
129 | :param inc: Lookup inc. Default is 3. |
||
130 | :type inc: int |
||
131 | |||
132 | :param sql: Whether to add valid lookups to a database. Default is false. |
||
133 | :type sql: bool |
||
134 | |||
135 | :param quiet: Whether to only output if release exists. Default is false. |
||
136 | :type quiet: bool |
||
137 | |||
138 | :param ceiling: When to stop loop. Default is 9996 (i.e. 10.x.y.9996). |
||
139 | :type ceiling: int |
||
140 | |||
141 | :param mailer: Whether to email new valid links. Default is false. |
||
142 | :type mailer: bool |
||
143 | """ |
||
144 | if mailer: |
||
145 | sql = True |
||
146 | smtpc = smtputils.smtp_config_loader() |
||
147 | smtpc = smtputils.smtp_config_generator(smtpc) |
||
148 | smtpc['homepath'] = None |
||
149 | pword = smtpc['password'] |
||
150 | smtputils.smtp_config_writer(**smtpc) |
||
1 ignored issue
–
show
|
|||
151 | else: |
||
152 | pword = None |
||
153 | scriptutils.slim_preamble("AUTOLOOKUP") |
||
154 | record = utilities.prep_logfile() if log else None |
||
155 | while True: |
||
156 | if loop and int(osversion.split(".")[3]) > ceiling: |
||
157 | raise KeyboardInterrupt |
||
158 | print("NOW SCANNING: {0}".format(osversion), end="\r") |
||
159 | results = networkutils.sr_lookup_bootstrap(osversion) |
||
160 | if results is None: |
||
161 | raise KeyboardInterrupt |
||
162 | a1rel, a1av = networkutils.clean_availability(results, 'a1') |
||
163 | a2rel, a2av = networkutils.clean_availability(results, 'a2') |
||
164 | b1rel, b1av = networkutils.clean_availability(results, 'b1') |
||
165 | b2rel, b2av = networkutils.clean_availability(results, 'b2') |
||
166 | prel, pav, avail = scriptutils.prod_avail(results, mailer, osversion, pword) |
||
167 | avpack = (a1av, a2av, b1av, b2av, pav) |
||
168 | swrelease = scriptutils.clean_swrel(set([a1rel, a2rel, b1rel, b2rel, prel])) |
||
169 | if swrelease != "": |
||
170 | out = scriptutils.autolookup_output(osversion, swrelease, avail, avpack, sql) |
||
171 | scriptutils.autolookup_printer(out, avail, log, quiet, record) |
||
172 | if autogen and avail == "Available": |
||
173 | rad = utilities.increment(osversion, 1) |
||
174 | scriptutils.linkgen(osversion, rad, prel) |
||
175 | if not loop: |
||
176 | raise KeyboardInterrupt # hack, but whatever |
||
177 | else: |
||
178 | if int(osversion.split(".")[3]) > ceiling: |
||
179 | raise KeyboardInterrupt |
||
180 | else: |
||
181 | osversion = utilities.increment(osversion, inc) |
||
182 | swrelease = "" |
||
183 | continue |
||
184 | |||
188 |
Generally, there is nothing wrong with usage of
*
or**
arguments. For readability of the code base, we suggest to not over-use these language constructs though.For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.