Skip to content

Commit

Permalink
Allow set type to receive re as field name (#26)
Browse files Browse the repository at this point in the history
* Allow set type to receive re as field name

* v0.0.18
  • Loading branch information
akariv committed Oct 10, 2018
1 parent 6222f05 commit c763111
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion PROCESSORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def set_type(name, resources=-1, **options):
pass
```

- `name` - the name of the field to modify
- `name` - the name of the field to modify (or a regular expression to match multiple fields)
- `resources`
- A name of a resource to operate on
- A regular expression matching resource names
Expand Down
2 changes: 1 addition & 1 deletion dataflows/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.17
0.0.18
7 changes: 4 additions & 3 deletions dataflows/processors/set_type.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from ..helpers.resource_matcher import ResourceMatcher
from .. import DataStreamProcessor, schema_validator

Expand All @@ -6,7 +8,7 @@ class set_type(DataStreamProcessor):

def __init__(self, name, resources=-1, **options):
super(set_type, self).__init__()
self.name = name
self.name = re.compile(name)
self.options = options
self.resources = resources

Expand All @@ -24,9 +26,8 @@ def process_datapackage(self, dp):
for res in dp.descriptor['resources']:
if self.matcher.match(res['name']):
for field in res['schema']['fields']:
if field['name'] == self.name:
if self.name.match(field['name']):
field.update(self.options)
added = True
break
assert added, 'Failed to find field {} in schema'.format(self.name)
return dp
6 changes: 3 additions & 3 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,15 @@ def test_set_type_resources():
f = Flow(
[dict(a=str(i)) for i in range(10)],
[dict(b=str(i)) for i in range(10)],
[dict(c=str(i)) for i in range(10)],
[dict(c='0_' + str(i)) for i in range(10)],
set_type('a', resources='res_[1]', type='integer'),
set_type('b', resources=['res_2'], type='integer'),
set_type('c', resources=-1, type='integer'),
set_type('[cd]', resources=-1, type='number', groupChar='_'),
validate()
)
results, dp, stats = f.results()
print(dp.descriptor)
assert results[0][1]['a'] == 1
assert results[1][3]['b'] == 3
assert results[2][8]['c'] == 8
assert results[2][8]['c'] == 8.0

0 comments on commit c763111

Please sign in to comment.