forked from datahq/dataflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to add_computed_fields, allow custom functions as data s…
…ource (datahq#78) * Add custom functions to add_computed_field (+ some better interface and beter documentation) * Rewrite add_field with add_computed_field, allow custom callables * Fix tests
- Loading branch information
Showing
4 changed files
with
116 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,17 @@ | ||
from dataflows.helpers.resource_matcher import ResourceMatcher | ||
|
||
|
||
def column_adder(rows, k, v): | ||
for row in rows: | ||
row[k] = v | ||
yield row | ||
from .add_computed_field import add_computed_field | ||
|
||
|
||
def add_field(name, type, default=None, resources=None, **options): | ||
|
||
def func(package): | ||
matcher = ResourceMatcher(resources, package.pkg) | ||
for resource in package.pkg.descriptor['resources']: | ||
if matcher.match(resource['name']): | ||
resource['schema']['fields'].append(dict( | ||
name=name, | ||
type=type, | ||
**options | ||
)) | ||
yield package.pkg | ||
for res in package: | ||
if matcher.match(res.res.name): | ||
yield column_adder(res, name, default) | ||
else: | ||
yield res | ||
|
||
return func | ||
return add_computed_field( | ||
target=dict( | ||
name=name, | ||
type=type, | ||
**options | ||
), | ||
resources=resources, | ||
operation=( | ||
default | ||
if callable(default) else | ||
(lambda row: default) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters