| Conditions | 10 | 
| Total Lines | 62 | 
| Lines | 0 | 
| Ratio | 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 traceroutedb.run_runner() 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 python | ||
| 91 | def run_runner(config): | ||
| 92 | NUMPROCS = config.procs | ||
| 93 | |||
| 94 | if config.debug: | ||
| 95 | logging.basicConfig(level=logging.DEBUG) | ||
| 96 |         logging.getLogger("requests").setLevel(logging.DEBUG) | ||
| 97 | else: | ||
| 98 | logging.basicConfig(level=logging.WARNING) | ||
| 99 |         logging.getLogger("requests").setLevel(logging.WARNING) | ||
| 100 | start_time = time.time() | ||
| 101 | |||
| 102 |     logging.info("Traceroute runner starting") | ||
| 103 | |||
| 104 |     if config.get("server_url", False): | ||
| 105 | URL = config.server_url + "/trace" | ||
| 106 | else: | ||
| 107 | URL = "http://127.0.0.1:9001" + "/trace" | ||
| 108 | |||
| 109 |     if config.get("ips", False): | ||
| 110 | ips = config.ips | ||
| 111 |         if config.get("ips_file", False): | ||
| 112 |             logging.warning("-i overrides ips from file with -f") | ||
| 113 | elif config.ips_file: | ||
| 114 | ips = [] | ||
| 115 | with open(config.ips_file) as f: | ||
| 116 | for line in f: | ||
| 117 | ips.append(line.strip()) | ||
| 118 | else: | ||
| 119 |         logging.error("No ips defined, exiting") | ||
| 120 | sys.exit(1) | ||
| 121 | |||
| 122 | # The only reason we need to pack all this info into a list is that | ||
| 123 | # we only get to pass one iterable to function from map_async or | ||
| 124 | # thats how I (poorly) understand it | ||
| 125 | ips_to_iter = [] | ||
| 126 | detected_ext_ip = ext_ip() | ||
| 127 | for ip in ips: | ||
| 128 |         ip_dict = {} | ||
| 129 | ip = str(ip) | ||
| 130 |         ip_dict["note"] = config.get("note", None) | ||
| 131 | ip_dict["ext_ip"] = detected_ext_ip | ||
| 132 | ip_dict["url"] = URL | ||
| 133 | ip_dict["ip"] = ip | ||
| 134 |         ip_dict["simulate"] = config.get("simulate", None) | ||
| 135 | ips_to_iter.append(ip_dict) | ||
| 136 | |||
| 137 |     logging.debug('IP addresses: ' + str(ips)) | ||
| 138 | logging.debug(str(ips_to_iter)) | ||
| 139 | |||
| 140 | try: | ||
| 141 | pool = Pool(NUMPROCS, init_worker) | ||
| 142 | pool.map_async(run_trace, ips_to_iter, 1).get(9999999) | ||
| 143 | pool.close() | ||
| 144 | pool.join() | ||
| 145 | except KeyboardInterrupt: | ||
| 146 |         print("Caught KeyboardInterrupt, terminating workers") | ||
| 147 | pool.terminate() | ||
| 148 | pool.join() | ||
| 149 | sys.exit(1) | ||
| 150 | |||
| 151 | end_time = time.time() | ||
| 152 |     logging.info("Traceroute runner done, Took: " + str(int(end_time - start_time)) + " seconds") | ||
| 153 |