Commit ffd5bc76 by Celine Mercier

Cython utils: functions convert to bytes or str and to remove all empty

objects from a complex object
parent 704d9b04
......@@ -8,6 +8,10 @@ cdef bytes str2bytes(str string)
cdef str bytes2str(bytes string)
cdef bytes tobytes(object string)
cdef str tostr(object string)
cdef object bytes2str_object(object value)
cdef object str2bytes_object(object value)
cdef object clean_empty_values_from_object(object value, exclude=*)
cdef obitype_t get_obitype_single_value(object value)
cdef obitype_t update_obitype(obitype_t obitype, object new_value)
......
......@@ -87,6 +87,70 @@ cdef str tostr(object string):
return bytes2str(string)
cdef object bytes2str_object(object value): # Only works if complex types are dict or list
if isinstance(value, dict):
items = [(k,v) for k,v in value.items()]
for k, v in items:
if isinstance(v, list) or isinstance(v, dict):
value[k] = bytes2str_object(v)
else:
if type(v) == bytes:
value[k] = bytes2str(v)
if type(k) == bytes:
value[bytes2str(k)] = value.pop(k)
elif isinstance(value, list):
for i in range(len(value)):
if isinstance(value[i], list) or isinstance(value[i], dict):
value[i] = bytes2str_object(value[i])
if type(value[i]) == bytes:
value[i] = bytes2str(value[i])
elif type(value) == bytes:
value = bytes2str(value)
return value
cdef object str2bytes_object(object value): # Only works if complex types are dict or list
if isinstance(value, dict):
items = [(k,v) for k,v in value.items()]
for k, v in items:
if isinstance(v, list) or isinstance(v, dict):
value[k] = str2bytes_object(v)
else:
if type(v) == str:
value[k] = str2bytes(v)
if type(k) == str:
value[str2bytes(k)] = value.pop(k)
elif isinstance(value, list):
for i in range(len(value)):
if isinstance(value[i], list) or isinstance(value[i], dict):
value[i] = str2bytes_object(value[i])
if type(value[i]) == str:
value[i] = str2bytes(value[i])
elif type(value) == str:
value = str2bytes(value)
return value
cdef object clean_empty_values_from_object(object value, exclude=[]): # Only works if complex types are dict or list
if isinstance(value, dict):
items = [(k,v) for k,v in value.items()]
for k, v in items:
if isinstance(v, list) or isinstance(v, dict):
value[k] = clean_empty_values_from_object(v)
if (k not in exclude) and (v is None or (hasattr(v, '__len__') and len(v) == 0)):
value.pop(k)
elif isinstance(value, list):
to_remove=[]
for i in range(len(value)):
if isinstance(value[i], list) or isinstance(value[i], dict):
value[i] = clean_empty_values_from_object(value[i])
if value[i] is None or (hasattr(value[i], '__len__') and len(value[i]) == 0):
to_remove.append(value[i])
for v in to_remove:
value.remove(v)
return value
cdef obitype_t get_obitype_single_value(object value) :
cdef type value_type
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment