def main(data_fol, data_file): """Extract and write air traffic information between countries Parameters: ----------- data_fol : partial path.join function Partial path function that links to the data directory data_file : str File name of the input file """ yr_range = tuple(map(str, range(2006, 2016))) fname = data_fol(data_file) air_traffic = defaultdict(float) mean_func = compose(np.mean, list, cfilter(lambda x: x is not None), cmap(catch_int_except)) with open(fname, 'r') as fid: header_finfo, *header_tinfo = next(fid).split('\t') header_tinfo = [d.strip() for d in header_tinfo] yr_inds = tuple( filter(lambda x: header_tinfo[x] in yr_range, range(len(header_tinfo)))) for row in fid: location, *flow_rate = row.split('\t') flow_rate = [d.strip() for d in flow_rate] unit, code, source, dest = location.split(',') if unit != 'PAS' or code != 'PAS_CRD' or \ any(map(lambda x: 'EU' in x or 'EA' in x, (source, dest))): continue air_traffic[(source, dest)] += mean_func(flow_rate[i] for i in yr_inds) print(source, dest, air_traffic[(source, dest)]) write_file(air_traffic, data_fol) return air_traffic
def paths_to_major_minors(self, device_paths): """ Create a list of device major minors for a list of device paths from _path_to_major_minor dict. If any of the paths come back as None, continue to the next. :param device_paths: The list of paths to get the list of major minors for. :return: list of dev_major_minors, or an empty list if any device_path is not found. """ return pipe(device_paths, cmap(self.path_to_major_minor), cfilter(None), list)
def paths_to_major_minors(node_block_devices, ndt, device_paths): """ Create a list of device major minors for a list of device paths from _path_to_major_minor dict. If any of the paths come back as None, continue to the next. :param node_block_devices: dict of major-minor ids keyed on path :param ndt: normalised device table :param device_paths: The list of paths to get the list of major minors for. :return: list of dev_major_minors, or an empty list if any device_path is not found. """ c_path_to_major_minor = path_to_major_minor(node_block_devices, ndt) return pipe(device_paths, cmap(c_path_to_major_minor), cfilter(None), list)
def check(self, skip_keys, expect, result, x): from toolz import pipe from toolz.curried import map as cmap, filter as cfilter def cmpval(key): expected = expect[x][key] actual = result[x][key] if type(expected) is dict: self.check(skip_keys, expect[x], result[x], key) else: self.assertEqual( actual, expected, "item {} ({}) in {} does not match expected ({})".format( key, actual, x, expected), ) pipe(expect[x].keys(), cfilter(lambda y: y not in skip_keys), cmap(cmpval), list)
def create_device_list(device_dict): return pipe(device_dict.itervalues(), cmap(as_device), cfilter(filter_device), list)
def fetch_device_list(): AgentShell.run(["udevadm", "settle"]) info = scanner_cmd("info") return pipe(info.itervalues(), cmap(as_device), cfilter(filter_device), list)