def __getitem__(self, key): if isinstance(key, slice): return self._values.__getitem__(key) elif is_int(key): return self._values.__getitem__(key) elif key in self._keyhash: index = self._keyhash[key] return self._values.__getitem__(index) else: raise Exception("Couldn't find value {0} in IndexDict".format(key))
def __setitem__(self, key, value): if is_int(key): #'key' is actually an index, #must be an already existing item self._values[key] = value else: len_vals = len(self._values) index = self._keyhash.get(key, len_vals) if index >= len(self._values): self._keyhash[key] = len(self._values) self._values.append(value) else: self._values[index] = value
def process_dict_list_obj(dict_list_obj, path): """json-style nested objects consisting of a (dictionary OR list) of (dictionary OR list) of (dictionary OR list) etc """ if not isinstance(path, list): t = type(path) raise Exception("process_dict_list_obj function argument path requires a list, received '{path}', type {t}".format(**vars())) end_node = follow_path(dict_list_obj, path) if isinstance(end_node, list): cols = set() for n in end_node: if isinstance(n, dict): cols = cols.union(six.viewkeys(n)) elif isinstance(n, (list,tuple)): #write a csv from a list of lists #by using a dummy header (eg 0,1,2,...) #@example: #[["a","b"],["c","d"],["e","f"]] -> #0,1 #a,b #c,d #e,f cols = cols.union(range(len(n))) else: raise Exception("Can't convert simple list to csv.") # else isinstance(n, list): # raise Exception("can't convert nested lists to csv -- try using a deeper path") cols = sorted(list(cols)) yield cols for n in end_node: if isinstance(n, dict): r = [n.get(c,"") for c in cols] #don't use dumps on a string or it'll create an extra pair of quotes r = [json.dumps(x,ensure_ascii = False) if isinstance(x,(tuple,list,dict)) else x for x in r] yield r elif isinstance(n,(list,tuple)): r = [n[c] if (jtutils.is_int(c) and c < len(n)) else "" for c in cols] #don't use dumps on a string or it'll create an extra pair of quotes r = [json.dumps(x,ensure_ascii = False) if isinstance(x,(tuple,list,dict)) else x for x in r] yield r else: raise Exception("Not a dict, list or tuple!") elif isinstance(end_node, dict): cols = list(six.viewkeys(end_node)) yield cols r = [end_node.get(c,"") for c in cols] #don't use dumps on a string or it'll create an extra pair of quotes r = [json.dumps(x,ensure_ascii = False) if isinstance(x,(list,dict)) else x for x in r] yield r else: raise_path_error()
def gen_outhdr(hdr, add_list, keep_list, drop_list): outhdr = hdr[:] if keep_list: if not add_list: add_list = [x for x in keep_list if x not in hdr and not is_int(x)] #example tmp_dict = {0:"col1",1:"col2",2:"col3","col1":"col1","col2":"col2","col3":"col3"} tmp_dict = dict( list(enumerate(outhdr)) + list(zip(outhdr, outhdr)) + list(zip(add_list, add_list))) outhdr = [tmp_dict[x] for x in keep_list ] #first include already existing columns from -c argument if add_list: outhdr += [x for x in add_list if x not in outhdr] if drop_list: outhdr = [ x for ix, x in enumerate(outhdr) if (ix not in drop_list and x not in drop_list) ] return outhdr