Conditions | 14 |
Total Lines | 87 |
Code Lines | 46 |
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 bbarchivist.scripts.autolookup.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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | #!/usr/bin/env python3 |
||
138 | @decorators.wrap_keyboard_except |
||
139 | def autolookup_main(osversion, loop=False, log=False, autogen=False, inc=3, sql=False, quiet=False, ceiling=9996, mailer=False, prod=False, no2=False): |
||
140 | """ |
||
141 | Lookup a software release from an OS. Can iterate. |
||
142 | |||
143 | :param osversion: OS version, 10.x.y.zzzz. |
||
144 | :type osversion: str |
||
145 | |||
146 | :param loop: Whether or not to automatically lookup. Default is false. |
||
147 | :type loop: bool |
||
148 | |||
149 | :param log: Whether to log. Default is false. |
||
150 | :type log: bool |
||
151 | |||
152 | :param autogen: Whether to create text links. Default is false. |
||
153 | :type autogen: bool |
||
154 | |||
155 | :param inc: Lookup increment. Default is 3. |
||
156 | :type inc: int |
||
157 | |||
158 | :param sql: Whether to add valid lookups to a database. Default is false. |
||
159 | :type sql: bool |
||
160 | |||
161 | :param quiet: Whether to only output if release exists. Default is false. |
||
162 | :type quiet: bool |
||
163 | |||
164 | :param ceiling: When to stop loop. Default is 9996 (i.e. 10.x.y.9996). |
||
165 | :type ceiling: int |
||
166 | |||
167 | :param mailer: Whether to email new valid links. Default is false. |
||
168 | :type mailer: bool |
||
169 | |||
170 | :param prod: Whether to check only the production server. Default is false. |
||
171 | :type prod: bool |
||
172 | |||
173 | :param no2: Whether to skip Alpha2/Beta2 servers. Default is false. |
||
174 | :type no2: bool |
||
175 | """ |
||
176 | if mailer: |
||
177 | sql = True |
||
178 | smtpc = smtputils.smtp_config_loader() |
||
179 | smtpc = smtputils.smtp_config_generator(smtpc) |
||
180 | smtpc['homepath'] = None |
||
181 | pword = smtpc['password'] |
||
182 | smtputils.smtp_config_writer(**smtpc) |
||
183 | else: |
||
184 | pword = None |
||
185 | argutils.slim_preamble("AUTOLOOKUP") |
||
186 | record = utilities.prep_logfile() if log else None |
||
187 | sess = requests.Session() |
||
188 | while True: |
||
189 | if loop and int(osversion.split(".")[3]) > ceiling: |
||
190 | raise KeyboardInterrupt |
||
191 | print("NOW SCANNING: {0}".format(osversion), end="\r") |
||
192 | if not prod: |
||
193 | results = networkutils.sr_lookup_bootstrap(osversion, sess, no2) |
||
194 | else: |
||
195 | res = networkutils.sr_lookup(osversion, networkutils.SERVERS["p"], sess) |
||
196 | results = {"p": res, "a1": None, "a2": None, "b1": None, "b2": None} |
||
197 | if results is None: |
||
198 | raise KeyboardInterrupt |
||
199 | a1rel, a1av = networkutils.clean_availability(results, 'a1') |
||
200 | if not no2: |
||
201 | a2rel, a2av = networkutils.clean_availability(results, 'a2') |
||
202 | else: |
||
203 | a2rel = "SR not in system" |
||
204 | a2av = " " |
||
205 | b1rel, b1av = networkutils.clean_availability(results, 'b1') |
||
206 | b2rel, b2av = networkutils.clean_availability(results, 'b2') |
||
207 | prel, pav, avail = scriptutils.prod_avail(results, mailer, osversion, pword) |
||
208 | avpack = (a1av, a2av, b1av, b2av, pav) |
||
209 | swrelease = scriptutils.clean_swrel(set([a1rel, a2rel, b1rel, b2rel, prel])) |
||
210 | if swrelease != "": |
||
211 | out = scriptutils.autolookup_output(osversion, swrelease, avail, avpack, sql) |
||
212 | scriptutils.autolookup_printer(out, avail, log, quiet, record) |
||
213 | if autogen and avail == "Available": |
||
214 | rad = utilities.increment(osversion, 1) |
||
215 | scriptutils.linkgen(osversion, rad, prel) |
||
216 | if not loop: |
||
217 | raise KeyboardInterrupt # hack, but whatever |
||
218 | else: |
||
219 | if int(osversion.split(".")[3]) > ceiling: |
||
220 | raise KeyboardInterrupt |
||
221 | else: |
||
222 | osversion = utilities.increment(osversion, inc) |
||
223 | swrelease = "" |
||
224 | continue |
||
225 | |||
229 |