DictFlattener

This commit is contained in:
James Turk 2008-11-17 20:53:04 +00:00
parent 8d71088e9a
commit aff9f2295c
2 changed files with 20 additions and 5 deletions

View File

@ -314,6 +314,17 @@ class Flattener(FieldFilter):
result.append(rec) result.append(rec)
return result return result
class DictFlattener(Filter):
def __init__(self, keys, separator='_'):
super(DictFlattener, self).__init__()
self._keys = keys
self._separator = separator
def process_record(self, record):
utils.flatten(record, keys=self._keys, separator=self._separator)
class Unique(ConditionalFilter): class Unique(ConditionalFilter):
""" Filter that ensures that all records passing through are unique. """ Filter that ensures that all records passing through are unique.
""" """

View File

@ -33,7 +33,7 @@ def string_dig(element, separator=''):
for child in element.findAll(True)]) for child in element.findAll(True)])
def recursive_flatten(item, prefix=''): def flatten(item, prefix='', separator='_', keys=None):
""" """
Flatten nested dictionary into one with its keys concatenated together. Flatten nested dictionary into one with its keys concatenated together.
@ -47,14 +47,18 @@ def recursive_flatten(item, prefix=''):
if isinstance(item, dict): if isinstance(item, dict):
# don't prepend a leading _ # don't prepend a leading _
if prefix != '': if prefix != '':
prefix += '_' prefix += separator
retval = {} retval = {}
for key, value in item.iteritems(): for key, value in item.iteritems():
retval.update(recursive_flatten(value, prefix + key)) if (not keys) or (key in keys):
retval.update(flatten(value, prefix + key, separator, keys))
else:
retval[prefix + key] = value
return retval return retval
elif isinstance(item, (tuple, list)): #elif isinstance(item, (tuple, list)):
return {prefix: [recursive_flatten(i) for i in item]} # return {prefix: [flatten(i, prefix, separator, keys) for i in item]}
else: else:
print item, prefix
return {prefix: item} return {prefix: item}
def dotted_key_lookup(dict_, dotted_key, default=KeyError, separator='.'): def dotted_key_lookup(dict_, dotted_key, default=KeyError, separator='.'):