From aff9f2295c962da12fbbabff2b78f1c7e29ef91c Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 17 Nov 2008 20:53:04 +0000 Subject: [PATCH] DictFlattener --- saucebrush/filters.py | 11 +++++++++++ saucebrush/utils.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/saucebrush/filters.py b/saucebrush/filters.py index 836e7e2..b39d0df 100644 --- a/saucebrush/filters.py +++ b/saucebrush/filters.py @@ -313,7 +313,18 @@ class Flattener(FieldFilter): rec.update(values) result.append(rec) 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): """ Filter that ensures that all records passing through are unique. """ diff --git a/saucebrush/utils.py b/saucebrush/utils.py index 90ec8e4..4499070 100644 --- a/saucebrush/utils.py +++ b/saucebrush/utils.py @@ -33,7 +33,7 @@ def string_dig(element, separator=''): 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. @@ -47,14 +47,18 @@ def recursive_flatten(item, prefix=''): if isinstance(item, dict): # don't prepend a leading _ if prefix != '': - prefix += '_' + prefix += separator retval = {} 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 - elif isinstance(item, (tuple, list)): - return {prefix: [recursive_flatten(i) for i in item]} + #elif isinstance(item, (tuple, list)): + # return {prefix: [flatten(i, prefix, separator, keys) for i in item]} else: + print item, prefix return {prefix: item} def dotted_key_lookup(dict_, dotted_key, default=KeyError, separator='.'):