initial subrecord filter

This commit is contained in:
James Turk 2010-06-28 10:43:19 -04:00
parent 38096b3903
commit cb29e77a05
2 changed files with 42 additions and 0 deletions

View File

@ -122,6 +122,29 @@ class ValidationError(Exception):
super(ValidationError, self).__init__(repr(record))
self.record = record
def _dotted_get(d, path):
if path:
key_pieces = path.split('.', 1)
piece = d[key_pieces[0]]
if isinstance(piece, (tuple, list)):
return [_dotted_get(i, '.'.join(key_pieces[1:])) for i in piece]
elif isinstance(piece, (dict)):
return _dotted_get(piece, '.'.join(key_pieces[1:]))
else:
return d
class SubrecordFilter(Filter):
def __init__(self, field_path, filter_):
self.field_path = field_path
self.filter = filter_
def process_record(self, record):
subrecord = _dotted_get(record, self.field_path)
for p in subrecord:
p = self.filter.process_record(p)
return record
#####################
## Generic Filters ##
#####################

View File

@ -2,6 +2,7 @@ import unittest
import operator
import types
from saucebrush.filters import (Filter, YieldFilter, FieldFilter,
SubrecordFilter,
ConditionalFilter, FieldModifier,
FieldRemover, FieldMerger, FieldAdder,
FieldCopier, FieldRenamer, Unique)
@ -99,6 +100,24 @@ class FilterTestCase(unittest.TestCase):
# ensure only even numbers remain
self.assertEquals(list(result), [0,2,4,6,8])
### FILTERS FOR Subrecord
def test_subrecord_filter(self):
data = [{'a': [{'b': 2}, {'b': 4}]},
{'a': [{'b': 5}]},
{'a': [{'b': 8}, {'b':2}, {'b':1}]}]
expected = [{'a': [{'b': 4}, {'b': 8}]},
{'a': [{'b': 10}]},
{'a': [{'b': 16}, {'b':4}, {'b':2}]}]
sf = SubrecordFilter('a', FieldDoubler('b'))
result = sf.attach(data)
self.assertEquals(list(result), expected)
def test_field_modifier(self):
# another version of FieldDoubler
fm = FieldModifier(['a', 'c'], lambda x: x*2)