initial subrecord filter
This commit is contained in:
parent
38096b3903
commit
cb29e77a05
@ -122,6 +122,29 @@ class ValidationError(Exception):
|
|||||||
super(ValidationError, self).__init__(repr(record))
|
super(ValidationError, self).__init__(repr(record))
|
||||||
self.record = 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 ##
|
## Generic Filters ##
|
||||||
#####################
|
#####################
|
||||||
|
@ -2,6 +2,7 @@ import unittest
|
|||||||
import operator
|
import operator
|
||||||
import types
|
import types
|
||||||
from saucebrush.filters import (Filter, YieldFilter, FieldFilter,
|
from saucebrush.filters import (Filter, YieldFilter, FieldFilter,
|
||||||
|
SubrecordFilter,
|
||||||
ConditionalFilter, FieldModifier,
|
ConditionalFilter, FieldModifier,
|
||||||
FieldRemover, FieldMerger, FieldAdder,
|
FieldRemover, FieldMerger, FieldAdder,
|
||||||
FieldCopier, FieldRenamer, Unique)
|
FieldCopier, FieldRenamer, Unique)
|
||||||
@ -99,6 +100,24 @@ class FilterTestCase(unittest.TestCase):
|
|||||||
# ensure only even numbers remain
|
# ensure only even numbers remain
|
||||||
self.assertEquals(list(result), [0,2,4,6,8])
|
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):
|
def test_field_modifier(self):
|
||||||
# another version of FieldDoubler
|
# another version of FieldDoubler
|
||||||
fm = FieldModifier(['a', 'c'], lambda x: x*2)
|
fm = FieldModifier(['a', 'c'], lambda x: x*2)
|
||||||
|
Loading…
Reference in New Issue
Block a user