Add MapFields transformation
This commit is contained in:
@ -29,6 +29,7 @@ from bonobo._api import (
|
||||
LdjsonReader,
|
||||
LdjsonWriter,
|
||||
Limit,
|
||||
MapFields,
|
||||
OrderFields,
|
||||
PickleReader,
|
||||
PickleWriter,
|
||||
|
||||
@ -149,6 +149,7 @@ api.register_group(
|
||||
LdjsonReader,
|
||||
LdjsonWriter,
|
||||
Limit,
|
||||
MapFields,
|
||||
OrderFields,
|
||||
PickleReader,
|
||||
PickleWriter,
|
||||
|
||||
@ -48,6 +48,10 @@ class UnrecoverableTypeError(UnrecoverableError, TypeError):
|
||||
pass
|
||||
|
||||
|
||||
class UnrecoverableAttributeError(UnrecoverableError, AttributeError):
|
||||
pass
|
||||
|
||||
|
||||
class UnrecoverableValueError(UnrecoverableError, ValueError):
|
||||
pass
|
||||
|
||||
|
||||
@ -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,40 @@ 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 as e:
|
||||
raise UnrecoverableAttributeError('This transformation works only on objects with named'
|
||||
' fields (namedtuple, BagType, ...).') from e
|
||||
|
||||
if callable(key):
|
||||
return factory(
|
||||
function(value) if key(key_) else value for key_, value in zip(bag._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())
|
||||
|
||||
Reference in New Issue
Block a user