A set of Python regularly used classes/functions
Project description
Dev4py-utils
A set of Python regularly used classes/functions
Table of contents
Project template
This project is based on pymsdl_template
Project links
Dev4py-utils modules
dev4py.utils.AsyncJOptional
Note: AsyncJOptional class is designed in order to simplify JOptional with async mapper
Note: AsyncJOptional support T or Awaitable[T] values. That's why some checks are done when terminal operation is called with
await
Examples:
import asyncio
from dev4py.utils import AsyncJOptional
def sync_mapper(i: int) -> int:
return i * 2
async def async_mapper(i: int) -> str:
return f"The value is {i}"
async def async_sample() -> None:
value: int = 1
await AsyncJOptional.of_noneable(value)\
.map(sync_mapper)\
.map(async_mapper)\
.if_present(print)
asyncio.run(async_sample())
dev4py.utils.awaitables
Examples:
import asyncio
from dev4py.utils import awaitables, JOptional
# is_awaitable sample
awaitables.is_awaitable(asyncio.sleep(2)) # True
awaitables.is_awaitable(print('Hello')) # False
# to_sync_or_async_param_function sample
def mapper(s: str) -> str:
return s + '_suffix'
async def async_mapper(s: str) -> str:
await asyncio.sleep(1)
return s + '_async_suffix'
async def async_test():
# Note: mapper parameter is str and async_mapper returns an Awaitable[str] so we have to manage it
# Note: !WARNING! Since 3.0.0 see AsyncJOptional / JOptional to_async_joptional method
result: str = await JOptional.of("A value")\
.map(async_mapper)\
.map(awaitables.to_sync_or_async_param_function(mapper))\
.get()
print(result) # A value_async_suffix_suffix
asyncio.run(async_test())
dev4py.utils.dicts
Examples:
from dev4py.utils import dicts
from dev4py.utils.types import Supplier
# is_dict sample
dicts.is_dict("A str") # False
dicts.is_dict({'key': 'A dict value'}) # True
# get_value sample
int_supplier: Supplier[int] = lambda: 3
dictionary: dict[str, int] = {'key_1': 1, 'key_2': 2}
dicts.get_value(dictionary, 'key_1') # 1
dicts.get_value(dictionary, 'key_3') # None
dicts.get_value(dictionary, 'key_3', int_supplier) # 3
# get_value_from_path sample
str_supplier: Supplier[str] = lambda: "a3"
deep_dictionary: dict[str, dict[int, str]] = { \
'a': {1: 'a1', 2: 'a2'}, \
'b': {1: 'b1', 2: 'b2'} \
}
dicts.get_value_from_path(deep_dictionary, ["a", 1]) # 'a1'
dicts.get_value_from_path(deep_dictionary, ["c", 1]) # None
dicts.get_value_from_path(deep_dictionary, ["a", 3]) # None
dicts.get_value_from_path(deep_dictionary, ["a", 3], str_supplier) # 'a3'
dev4py.utils.JOptional
Note: JOptional class is inspired from java.util.Optional class with some adds (like
peek
method).
Examples:
from dev4py.utils import JOptional
value: int = 1
JOptional.of_noneable(value)\
.map(lambda v: f"The value is {v}")\
.if_present(print)
dev4py.utils.objects
Note: The objects module is inspired from java.util.Objects class.
Examples:
from dev4py.utils import objects
# non_none sample
value = None
objects.non_none(value)
# require_non_none sample
value = "A value"
objects.require_non_none(value)
# to_string sample
value = None
default_value: str = "A default value"
objects.to_string(value, default_value)
dev4py.utils.types
Note: The types module is inspired from java.util.function package
Examples:
from dev4py.utils.types import Function, Predicate, Consumer
# Function sample
int_to_str: Function[int, str] = lambda i: str(i)
str_result: str = int_to_str(1)
# Predicate sample
str_predicate: Predicate[str] = lambda s: s == "A value"
pred_result = str_predicate("Value to test")
# Consumer sample
def sample(consumer: Consumer[str], value: str) -> None:
consumer(value)
def my_consumer(arg: str) -> None:
print(arg)
sample(my_consumer, "My value")
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 dev4py_utils-3.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 407fe41fc00e7e36bf5a68c9dead1a8fceb93dd28a1cb30c99d53f816cf11864 |
|
MD5 | 1aad7d27cff84a94a86661c111f551e2 |
|
BLAKE2b-256 | 8cdcc3088c56ed7d755f9d0081ddae8c34444d5a60519d49e24fb9ccf5d11f46 |