from itertools import zip_longest def file_len(fname): with open(fname, encoding="utf8") as f: i = 1 for i, l in enumerate(f): pass return i+1 def split_zip_dict(s1, s2): keys = s1.split(',') values = s2.split(',') return dict(zip(keys, values)) def zip_dict(keys, values): return dict(zip(keys, values)) def var_to_bool(s): if type(s) is bool: return s returnVar = None if (type(s) is not str and type(s) is not int) or s is None: raise ValueError("Cannot covert {} to a bool".format(s)) else: if (type(s) is int and int(s) == 0) or s.lower() == 'false': returnVar = False elif (type(s) is int and int(s) == 1) or s.lower() == 'true': returnVar = True else: raise ValueError("Cannot covert {} to a bool".format(s)) return returnVar def primitive_dict(obj): return {f: k for f, k in vars(obj).items() if ((type(k) is str or type(k) is int or type(k) is bool or type(k) is list or type(k) is dict or type(k) is set or type(k) is float or type(k) is None) and '__' not in f[:2])} def remap_transitional_territories(terr_list): return_list = set() for i in terr_list: # ['AN','CS','FX','GZ','TP','ZZ'] if i == 'AN': return_list.add('BQ') return_list.add('CW') return_list.add('SX') elif i == 'FX': return_list.add('FR') elif i == 'CS': return_list.add('ME') return_list.add('RS') elif i == 'GZ': return_list.add('PS') elif i == 'TP': return_list.add('TL') elif i == 'ZZ': pass else: return_list.add(i) return list(return_list) def grouper(iterable, n, fillvalue=None): "Collect data into fixed-length list_chunks or blocks" # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return zip_longest(*args, fillvalue=fillvalue) def list_chunks(l, n): """Yield successive n-sized list_chunks from l.""" l = list(l) for i in range(0, len(l), n): yield l[i:i + n] def prep_insert_sql(asset, field_list, table_name, sql): write_out = {} for k, v in asset.items(): if k in field_list: write_out[k] = v # TODO: Add the non-asset_report fields, like reason and/or filename, etc. write_out['id'] = None placeholders = ', '.join(['%s'] * len(write_out)) columns = ', '.join(write_out.keys()) sql = sql % \ (table_name, columns, placeholders) return sql, write_out def dedupe_list(seq): seen = set() seen_add = seen.add return [x for x in seq if not (x in seen or seen_add(x))]