Subrecord with non-modifying keys

This commit is contained in:
James Turk 2010-06-28 15:47:25 -04:00
parent 07f50fb6a1
commit 18a15022a5
2 changed files with 34 additions and 17 deletions

View File

@ -146,22 +146,30 @@ class SubrecordFilter(Filter):
""" """
def __init__(self, field_path, filter_): def __init__(self, field_path, filter_):
#if '.' in field_path: if '.' in field_path:
# self.field_path, self.key = field_path.rsplit('.', 1) self.field_path, self.key = field_path.rsplit('.', 1)
#else: else:
# self.field_path = None self.field_path = None
# self.key = field_path self.key = field_path
self.field_path = field_path
self.filter = filter_ self.filter = filter_
def process_record(self, record): def process_subrecord(self, parent):
#if self.field_path: if isinstance(parent[self.key], (tuple, list)):
subrecord = _dotted_get(record, self.field_path) parent[self.key] = [self.filter.process_record(r) for r in parent[self.key]]
if isinstance(subrecord, (tuple, list)):
for p in subrecord:
self.filter.process_record(p)
else: 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 return record
##################### #####################

View File

@ -34,6 +34,15 @@ class FieldDoubler(FieldFilter):
def process_field(self, item): def process_field(self, item):
return item*2 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): class ConditionalOddRemover(ConditionalFilter):
def test_record(self, record): def test_record(self, record):
# return True for even values # return True for even values
@ -111,7 +120,7 @@ class FilterTestCase(unittest.TestCase):
{'a': [{'b': 10}]}, {'a': [{'b': 10}]},
{'a': [{'b': 16}, {'b':4}, {'b':2}]}] {'a': [{'b': 16}, {'b':4}, {'b':2}]}]
sf = SubrecordFilter('a', FieldDoubler('b')) sf = SubrecordFilter('a', NonModifyingFieldDoubler('b'))
result = sf.attach(data) result = sf.attach(data)
self.assertEquals(list(result), expected) self.assertEquals(list(result), expected)
@ -125,7 +134,7 @@ class FilterTestCase(unittest.TestCase):
{'a': {'d':[{'b': 10}]}}, {'a': {'d':[{'b': 10}]}},
{'a': {'d':[{'b': 16}, {'b':4}, {'b':2}]}}] {'a': {'d':[{'b': 16}, {'b':4}, {'b':2}]}}]
sf = SubrecordFilter('a.d', FieldDoubler('b')) sf = SubrecordFilter('a.d', NonModifyingFieldDoubler('b'))
result = sf.attach(data) result = sf.attach(data)
self.assertEquals(list(result), expected) self.assertEquals(list(result), expected)
@ -143,7 +152,7 @@ class FilterTestCase(unittest.TestCase):
{'a':{'b':{'c':6}}}, {'a':{'b':{'c':6}}},
] ]
sf = SubrecordFilter('a.b', FieldDoubler('c')) sf = SubrecordFilter('a.b', NonModifyingFieldDoubler('c'))
result = sf.attach(data) result = sf.attach(data)
self.assertEquals(list(result), expected) self.assertEquals(list(result), expected)
@ -161,7 +170,7 @@ class FilterTestCase(unittest.TestCase):
{'a': [{'b': {'c': 4}} ]} {'a': [{'b': {'c': 4}} ]}
] ]
sf = SubrecordFilter('a.b', FieldDoubler('c')) sf = SubrecordFilter('a.b', NonModifyingFieldDoubler('c'))
result = sf.attach(data) result = sf.attach(data)
self.assertEquals(list(result), expected) self.assertEquals(list(result), expected)