Commit 3cee3ff0 authored by Marco Mariani's avatar Marco Mariani

simpler _getDict with guards; correctly skip logger serialization

parent 056ceb46
...@@ -152,38 +152,35 @@ def netmaskToPrefixIPv6(netmask): ...@@ -152,38 +152,35 @@ def netmaskToPrefixIPv6(netmask):
netaddr.strategy.ipv6.str_to_int(netmask)] netaddr.strategy.ipv6.str_to_int(netmask)]
def _getDict(instance): def _getDict(obj):
""" """
Serialize an object instance into dictionaries. List and dict will remains Serialize an object into dictionaries. List and dict will remains
the same, basic type too. But encapsulated object will be returned as dict. the same, basic type too. But encapsulated object will be returned as dict.
Set, collections and other aren't handle for now. Set, collections and other aren't handle for now.
Args: Args:
instance: an object of any type. obj: an object of any type.
Returns: Returns:
A dictionary if the given object wasn't a list, a list otherwise. A dictionary if the given object wasn't a list, a list otherwise.
""" """
if isinstance(instance, list): if isinstance(obj, list):
return [_getDict(item) for item in instance] return [_getDict(item) for item in obj]
elif isinstance(instance, dict):
result = {}
for key in instance:
result[key] = _getDict(instance[key])
return result
if isinstance(obj, dict):
dikt = obj
else: else:
try: try:
dikt = instance.__dict__ dikt = obj.__dict__
except AttributeError: except AttributeError:
return instance return obj
result = {}
for key, value in dikt.iteritems(): return {
key: _getDict(value)
for key, value in dikt.iteritems()
# do not attempt to serialize logger: it is both useless and recursive. # do not attempt to serialize logger: it is both useless and recursive.
if not isinstance(instance, logging.Logger): if not isinstance(value, logging.Logger)
result[key] = _getDict(value) }
return result
class Computer(object): class Computer(object):
......
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