Skip to main content

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cpyberry-pytransform-2.0.0.tar.gz (8.1 kB view hashes)

Uploaded Source

Built Distribution

cpyberry_pytransform-2.0.0-py3-none-any.whl (8.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page