bytewax.operators.helpers#

Helper functions for using operators.

Data#

K: TypeVar#
V: TypeVar#

Functions#

map_dict_value(
key: K,
mapper: Callable[[V], V],
) Callable[[Dict[K, V]], Dict[K, V]]#

Build a function to map an item in a dict and return the dict.

Use this to help build mapper functions for the map operator that work on a specific value in a dict, but leave the other values untouched.

 1>>> import bytewax.operators as op
 2>>> from bytewax.testing import run_main, TestingSource
 3>>> from bytewax.dataflow import Dataflow
 4>>> flow = Dataflow("lens_item_map_eg")
 5>>> s = op.input(
 6...     "inp",
 7...     flow,
 8...     TestingSource(
 9...         [
10...             {"name": "Rachel White", "email": "rachel@white.com"},
11...             {"name": "John Smith", "email": "john@smith.com"},
12...         ]
13...     ),
14... )
15>>> def normalize(name):
16...     return name.upper()
17>>> s = op.map("normalize", s, map_dict_value("name", normalize))
18>>> _ = op.inspect("out", s)
19>>> run_main(flow)
20lens_item_map_eg.out: {'name': 'RACHEL WHITE', 'email': 'rachel@white.com'}
21lens_item_map_eg.out: {'name': 'JOHN SMITH', 'email': 'john@smith.com'}

This type of “do an operation on a known nested structure” is called a lens. If you’d like to produce more complex lenses, see the lenses package. It handles many more nuances of this problem like mutable vs immutable data types, attributes vs keys, and mutating methods vs returning functions. You can use it to build mappers for Bytewax operators.

Parameters:
  • key – Dictionary key.

  • mapper – Function to run on the value for that key.

Returns:

A function which will perform that mapping operation when called.

Join our community Slack channel

Need some help? Join our community!

If you have any trouble with the process or have ideas about how to improve this document, come talk to us in the #questions-answered Slack channel!

Join now