A library that applies arbitrary functions to each element of list type and dict type objects
Project description
pytransform
A library that applies arbitrary functions to each element of list type and dict type objects
Functions that defines the conversion process, which takes each element before conversion as an argument and returns each element after conversion, is called recursively, so it also supports nested list and dictionary.
Requirements
- python 3.5, 3.6, 3.7, 3.8, 3.9
Installation
pip install cpyberry-pytransform
Usage
When you want to multiply each element of the list by 2, you can write:
import pytransform
def operation_double(element: int, origin: list) -> int:
# The first argument contains each element of the original list.
# The original list is stored in the second argument.
return element * 2
pytransform.transform_list([1, 2, 3], operation_double)
# return [2, 4, 6]
pytransform.transform_list(
origin=[1, 2, 3],
operation=operation_double
)
# return [2, 4, 6]
When you want to insert "_neko" at the end of each key in the dictionary and "_inu" at the end of each value, you can write:
def operation_key(key: str, origin: dict, value: str):
# The first argument contains each key of the original dictionary.
# The original dictionary is stored in the second argument.
# The third argument contains the value corresponding to each key in the original dictionary.
return key + "_neko"
def operation_value(value: str, origin: dict, key: str):
# The first argument contains each key of the original dictionary.
# The original dictionary is stored in the second argument.
# The third argument contains the value corresponding to each key in the original dictionary.
return value + "_inu"
pytransform.transform_dictionary(
origin={"meow": "woof"},
operation_key=operation_key,
operation_value=operation_value
)
# return {"meow_neko", "woof_inu"}
The above functions are called recursively, so they also support nested list and dictionary.
pytransform.transform_list([1, 2, [3, 4, 5]], operation_double)
# return [2, 4, [6, 8, 10]]
pytransform.transform_dictionary(
origin={"meow": {"pows": "woof"}},
operation_key=operation_key,
operation_value=operation_value
)
# return {"meow_neko": {"pows_neko": "woof_inu"}}
Founder
-
email: cpyberry222@gmail.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for cpyberry-pytransform-2.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6a1043b34e6d2639230dd4d9d5268f4a1ab02937e056fdd1784e09f07b68b92 |
|
MD5 | be56979befead0f1c1f4cb624810fee2 |
|
BLAKE2b-256 | 5803571e3eaa0adf232bbb078bdf7ae5a4823e65cf583db5033c2758e38de1bf |
Hashes for cpyberry_pytransform-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 460d0f578a5404f544c14b40f379cb47b30b693c5bbb34794f1ce1b97b9fa139 |
|
MD5 | 14cae1c5fde6ca0df372eb4b5dbec64a |
|
BLAKE2b-256 | 6cf13df9c7e1db6699cf27645992a09fba4bb41826c9df8fc8f24f28858eae8b |