Merge pull request #300 from KPilnacek/285_update_all_fields_in_a_row

Add MapFields transformation
This commit is contained in:
Romain Dorgueil
2018-10-29 17:45:37 +01:00
committed by GitHub
5 changed files with 74 additions and 0 deletions

View File

@ -29,6 +29,7 @@ from bonobo._api import (
LdjsonReader,
LdjsonWriter,
Limit,
MapFields,
OrderFields,
PickleReader,
PickleWriter,

View File

@ -149,6 +149,7 @@ api.register_group(
LdjsonReader,
LdjsonWriter,
Limit,
MapFields,
OrderFields,
PickleReader,
PickleWriter,

View File

@ -48,6 +48,10 @@ class UnrecoverableTypeError(UnrecoverableError, TypeError):
pass
class UnrecoverableAttributeError(UnrecoverableError, AttributeError):
pass
class UnrecoverableValueError(UnrecoverableError, ValueError):
pass

View File

@ -10,6 +10,7 @@ from bonobo.config import Configurable, Method, Option, use_context, use_no_inpu
from bonobo.config.functools import transformation_factory
from bonobo.config.processors import ContextProcessor, use_context_processor
from bonobo.constants import NOT_MODIFIED
from bonobo.errors import UnrecoverableAttributeError
from bonobo.util.objects import ValueHolder
from bonobo.util.term import CLEAR_EOL
@ -18,6 +19,7 @@ __all__ = [
"Format",
"Limit",
"OrderFields",
"MapFields",
"PrettyPrinter",
"Rename",
"SetFields",
@ -314,6 +316,46 @@ def Format(**formats):
return _Format
@transformation_factory
def MapFields(function, key=True):
"""
Transformation factory that maps `function` on the values of a row.
It can be applied either to
1. all columns (`key=True`),
2. no column (`key=False`), or
3. a subset of columns by passing a callable, which takes column name and returns `bool`
(same as the parameter `function` in `filter`).
:param function: callable
:param key: bool or callable
:return: callable
"""
@use_raw_input
def _MapFields(bag):
try:
factory = type(bag)._make
except AttributeError:
factory = type(bag)
if callable(key):
try:
fields = bag._fields
except AttributeError as e:
raise UnrecoverableAttributeError(
'This transformation works only on objects with named'
' fields (namedtuple, BagType, ...).') from e
return factory(
function(value) if key(key_) else value for key_, value in zip(fields, bag)
)
elif key:
return factory(function(value) for value in bag)
else:
return NOT_MODIFIED
return _MapFields
def _count(self, context):
counter = yield ValueHolder(0)
context.send(counter.get())