| Conditions | 11 |
| Total Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 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 | try: |
||
| 92 | r = post(ip_dict["url"], data=json.dumps(result)) |
||
| 93 | logger.debug(r) |
||
| 94 | except ConnectionError as e: |
||
| 95 | print(e) |
||
| 96 | |||
| 97 | |||
| 98 | def run_trace(ip_pack): |
||
| 99 | |||
| 100 | ip = ip_pack["ip"] |
||
| 101 | if ip in OWN_IPS: |
||
| 102 | return None |
||
| 103 | |||
| 104 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
||
| 105 | try: |
||
| 106 | proc = Popen(["/usr/sbin/traceroute", ip, "30"], stdout=PIPE, stderr=PIPE) # noqa |
||
| 107 | out, err = proc.communicate() |
||
| 108 | except KeyboardInterrupt: |
||
| 109 | proc.terminate() |
||
| 110 | proc.kill() |
||
| 111 | return None |
||
| 112 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
||
| 113 | src_ip = check_output(["ip", "route", "get", ip]).splitlines()[0].split()[-1] # noqa |
||
| 114 | trp = tracerouteparser.TracerouteParser() |
||
| 115 | trp.parse_data(out) |
||
| 116 | data = {} |
||
| 117 | data["dst_ip"] = trp.dest_ip |
||
| 118 | data["src_ip"] = src_ip |
||
| 119 | data["dst_name"] = trp.dest_name |
||
| 120 | data["hops"] = {} |
||
| 121 | |||
| 122 | for hop in trp.hops: |
||
| 123 | data["hops"][int(hop.idx)] = [] |
||
| 124 | for probe in hop.probes: |
||
| 125 | data["hops"][int(hop.idx)].append({"name": probe.name, |
||
| 126 | "ip": probe.ipaddr, |
||
| 127 | "rtt": probe.rtt, |
||
| 128 | "anno": probe.anno}) |
||
| 129 | ret = {"reporter": socket.gethostname(), |
||
| 130 | "note": ip_pack["note"], |
||
| 131 | "ext_ip": ip_pack["ext_ip"], |
||
| 132 | "data": data} |
||
| 133 | submit_trace(ip_pack, ret) |
||
| 134 | |||
| 135 | |||
| 136 | def runner_entry(config): |
||
| 137 | if config.get("server_url", False): |
||
| 138 | URL = config.server_url + "/trace" |
||
| 139 | else: |
||
| 140 | URL = "http://127.0.0.1:9001" + "/trace" |
||
| 141 | |||
| 142 | start_time = time.time() |
||
| 143 | |||
| 144 | logger.warn("Traceroute runner starting") |
||
| 145 | |||
| 146 | ips = calc_ips(config) |
||
| 147 | ips_pack = pack_ips(ips, config, URL) |
||
| 148 | |||
| 149 | logger.debug('IP addresses: ' + str(ips)) |
||
| 150 | logger.debug(str(ips_pack)) |
||
| 151 | |||
| 152 | pool = Pool(config.procs, init_worker) |
||
| 153 | try: |
||
| 154 | logger.warn("Starting workers") |
||
| 155 | res = pool.map_async(run_trace, ips_pack, 1) |
||
| 167 |
It is generally discouraged to redefine built-ins as this makes code very hard to read.