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