From 18a15022a5d078261ac312a4db04b548ae655235 Mon Sep 17 00:00:00 2001 From: James Turk Date: Mon, 28 Jun 2010 15:47:25 -0400 Subject: [PATCH] Subrecord with non-modifying keys --- saucebrush/filters.py | 34 +++++++++++++++++++++------------- saucebrush/tests/filters.py | 17 +++++++++++++---- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/saucebrush/filters.py b/saucebrush/filters.py index 4f27b69..4f7786f 100644 --- a/saucebrush/filters.py +++ b/saucebrush/filters.py @@ -146,22 +146,30 @@ class SubrecordFilter(Filter): """ def __init__(self, field_path, filter_): - #if '.' in field_path: - # self.field_path, self.key = field_path.rsplit('.', 1) - #else: - # self.field_path = None - # self.key = field_path - self.field_path = field_path + if '.' in field_path: + self.field_path, self.key = field_path.rsplit('.', 1) + else: + self.field_path = None + self.key = field_path self.filter = filter_ - def process_record(self, record): - #if self.field_path: - subrecord = _dotted_get(record, self.field_path) - if isinstance(subrecord, (tuple, list)): - for p in subrecord: - self.filter.process_record(p) + def process_subrecord(self, parent): + if isinstance(parent[self.key], (tuple, list)): + parent[self.key] = [self.filter.process_record(r) for r in parent[self.key]] else: - self.filter.process_record(subrecord) + parent[self.key] = self.filter.process_record(parent[self.key]) + + def process_record(self, record): + if self.field_path: + subrecord_parent = _dotted_get(record, self.field_path) + else: + subrecord_parent = record + + if isinstance(subrecord_parent, (tuple, list)): + for r in subrecord_parent: + self.process_subrecord(r) + else: + self.process_subrecord(subrecord_parent) return record ##################### diff --git a/saucebrush/tests/filters.py b/saucebrush/tests/filters.py index 31c92c0..e138f32 100644 --- a/saucebrush/tests/filters.py +++ b/saucebrush/tests/filters.py @@ -34,6 +34,15 @@ class FieldDoubler(FieldFilter): def process_field(self, item): return item*2 +class NonModifyingFieldDoubler(Filter): + def __init__(self, key): + self.key = key + + def process_record(self, record): + record = dict(record) + record[self.key] *= 2 + return record + class ConditionalOddRemover(ConditionalFilter): def test_record(self, record): # return True for even values @@ -111,7 +120,7 @@ class FilterTestCase(unittest.TestCase): {'a': [{'b': 10}]}, {'a': [{'b': 16}, {'b':4}, {'b':2}]}] - sf = SubrecordFilter('a', FieldDoubler('b')) + sf = SubrecordFilter('a', NonModifyingFieldDoubler('b')) result = sf.attach(data) self.assertEquals(list(result), expected) @@ -125,7 +134,7 @@ class FilterTestCase(unittest.TestCase): {'a': {'d':[{'b': 10}]}}, {'a': {'d':[{'b': 16}, {'b':4}, {'b':2}]}}] - sf = SubrecordFilter('a.d', FieldDoubler('b')) + sf = SubrecordFilter('a.d', NonModifyingFieldDoubler('b')) result = sf.attach(data) self.assertEquals(list(result), expected) @@ -143,7 +152,7 @@ class FilterTestCase(unittest.TestCase): {'a':{'b':{'c':6}}}, ] - sf = SubrecordFilter('a.b', FieldDoubler('c')) + sf = SubrecordFilter('a.b', NonModifyingFieldDoubler('c')) result = sf.attach(data) self.assertEquals(list(result), expected) @@ -161,7 +170,7 @@ class FilterTestCase(unittest.TestCase): {'a': [{'b': {'c': 4}} ]} ] - sf = SubrecordFilter('a.b', FieldDoubler('c')) + sf = SubrecordFilter('a.b', NonModifyingFieldDoubler('c')) result = sf.attach(data) self.assertEquals(list(result), expected)