Last active
December 19, 2015 07:39
-
-
Save esseti/5919838 to your computer and use it in GitHub Desktop.
Function that i created to manage some data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __condition(o_value,operator,value): | |
if operator == '==': | |
return (o_value == value) | |
elif operator == '!=': | |
return (o_value != value) | |
elif operator == '<': | |
return (o_value < value) | |
elif operator == '>': | |
return (o_value > value) | |
else: | |
return False | |
def __satisfy(obj,condition): | |
field=str(condition['field']) | |
operator=str(condition['operator']) | |
value=str(condition['value']) | |
if obj: | |
if __condition(str(obj[field]),operator,value): | |
return obj | |
else: | |
return None | |
return None | |
def filterData(objects, conditions, condition_operator='and'): | |
''' | |
takes a list and a condition, return a sublist | |
condition is list of dic containing: | |
- field | |
- operator | |
- value | |
''' | |
listret = [] | |
for obj in objects: | |
obj_t_l = [] | |
for condition in conditions: | |
obj_t = __satisfy(obj,condition) | |
if condition_operator=='and': | |
obj=obj_t | |
else: | |
if obj_t is not None: | |
obj_t_l.append(obj) | |
if condition_operator=='and' and obj: | |
listret.append(obj) | |
elif len(obj_t_l) > 0: | |
obj=obj_t_l[0] | |
listret.append(obj) | |
ret={} | |
ret['result']=listret | |
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def joinObjects(objects, field): | |
''' | |
takes a list of objects and merge them. | |
objects are dicts | |
if objects have same keys, values are joined into a list. | |
[{'id':1,'tag':'ciao1'}, | |
{'id':2,'tag':['ciao1',ciao3]}, | |
{'id':1,'smt':'else2'}, | |
{'id':2,'tag':'ciao2'}] | |
becomes | |
[{'id': 1,'smt': 'else2', 'tag': 'ciao1'}, {'id': 2,'tag': ['ciao2', 'ciao1','ciao3']}] | |
''' | |
ret = [] | |
res = {} | |
for obj in objects: | |
# find the index | |
idx = obj[field] | |
# find the object | |
if idx in res: | |
t_obj = res[idx] | |
else: | |
t_obj = {} | |
for k,v in obj.iteritems(): | |
# if key is the field don't do anything | |
if k is not field: | |
if k in t_obj: | |
# if it's a list then append it, so we avoid list into lists | |
if isinstance(t_obj[k], list): | |
t_obj[k].append(v) | |
else: | |
t_obj[k]=[v]+t_obj[k] | |
else: | |
if isinstance(v, list): | |
t_obj[k]=v | |
else: | |
t_obj[k]=[v] | |
else: | |
t_obj[k]=v | |
# store in the map | |
res[idx] = t_obj | |
r = [] | |
# for all the elements in the list (which are dict) | |
for k, v in res.iteritems(): | |
obj={} | |
# take all the fields of an element | |
for k1,v1 in v.iteritems(): | |
if isinstance(v1, list): | |
if len(v1)==1: | |
obj[k1]=v1[0] | |
else: | |
obj[k1]=v1 | |
else: | |
obj[k1]=v1 | |
r.append(obj) | |
return r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mergeData(data): | |
''' | |
Merge Lists into a single list: | |
[[1, 2, 3], [5, 6], 7] -> [1, 2, 3, 5, 6, 7] | |
''' | |
ret = [] | |
for d in data: | |
value = d | |
# if list then concat element | |
if isinstance(value, list): | |
ret=ret+value | |
# if not append the element | |
else: | |
ret.append(value) | |
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def splitObjects(objects, shared, fields): | |
''' | |
takes a list of objects and split in two lists. | |
shared are the fields shared by both objects | |
fields are the fields of the first object. | |
remaining fields are for the second object. | |
data=[{'id':1,'tag':['ciao1','ciao2'],'smt':"else"},{'id':2,'tag':['ciao1','ciao2','ciao3']}] | |
result=[ | |
[{'tag': ['ciao1', 'ciao2'], 'id': 1}, | |
{'tag': ['ciao1', 'ciao2', 'ciao3'], 'id': 2} | |
], | |
[ | |
{'smt': 'else', 'id': 1}, | |
{'id': 2} | |
] | |
] | |
''' | |
list1=[] | |
list2=[] | |
for obj in objects: | |
item1={} | |
item2={} | |
for key, value in obj.iteritems(): | |
if key in shared: | |
item1[key]=value | |
item2[key]=value | |
elif key in fields: | |
item1[key]=value | |
else: | |
item2[key]=value | |
list1.append(item1) | |
list2.append(item2) | |
ret = [] | |
ret.append(list1) | |
ret.append(list2) | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment