CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pydash

A comprehensive Python utility library for functional programming inspired by JavaScript's Lo-Dash

Pending
Overview
Eval results
Files

objects.mddocs/

Objects Module

The Objects module provides 49 functions for manipulating objects and dictionaries. These functions cover property access, merging, cloning, transformation, and type conversion operations.

Property Access Functions

get

def get(obj: Any, path: Union[str, List], default: Any = None) -> Any

Gets the value at path of object. If the resolved value is None or doesn't exist, the default value is returned in its place.

Parameters:

  • obj (Any): Object to query.
  • path (Union[str, List]): Path of the property to get.
  • default (Any): Value returned for None resolved values. Defaults to None.

Returns:

  • Any: Resolved value.

Example:

from pydash import get

obj = {'a': [{'b': {'c': 3}}]}
get(obj, 'a[0].b.c')
# 3

get(obj, 'a.b.c', 'default')
# 'default'

get(obj, ['a', 0, 'b', 'c'])
# 3

has

def has(obj: Any, path: Union[str, List]) -> bool

Checks if path is a direct property of object.

Parameters:

  • obj (Any): Object to query.
  • path (Union[str, List]): Path to check.

Returns:

  • bool: True if path exists, else False.

Example:

from pydash import has

obj = {'a': {'b': 2}}
has(obj, 'a')
# True

has(obj, 'a.b')
# True

has(obj, 'c')
# False

set_

def set_(obj: T, path: Union[str, List], value: Any) -> T

Sets the value at path of object. If a portion of path doesn't exist, it's created. Arrays are created for missing index properties while objects are created for all other missing properties.

Parameters:

  • obj (T): Object to modify.
  • path (Union[str, List]): Path of the property to set.
  • value (Any): Value to set.

Returns:

  • T: obj.

Example:

from pydash import set_

obj = {'a': [{'b': {'c': 3}}]}
set_(obj, 'a[0].b.c', 4)
# obj is now {'a': [{'b': {'c': 4}}]}

set_(obj, 'x[0].y.z', 5)
# obj is now {'a': [{'b': {'c': 4}}], 'x': [{'y': {'z': 5}}]}

set_with

def set_with(obj: Any, path: Union[str, List], value: Any, customizer: Callable = None) -> Any

Like set_ except that it accepts customizer which is invoked to produce the objects of path.

Parameters:

  • obj (Any): Object to modify.
  • path (Union[str, List]): Path of the property to set.
  • value (Any): Value to set.
  • customizer (Callable, optional): Function to customize assigned values.

Returns:

  • Any: obj.

unset

def unset(obj: Union[List, Dict], path: Union[str, List]) -> bool

Removes the property at path of object.

Parameters:

  • obj (Union[List, Dict]): Object to modify.
  • path (Union[str, List]): Path of the property to unset.

Returns:

  • bool: True if the property is deleted, else False.

Example:

from pydash import unset

obj = {'a': [{'b': {'c': 7}}]}
unset(obj, 'a[0].b.c')
# True
# obj is now {'a': [{'b': {}}]}

update

def update(obj: Any, path: Union[str, List], updater: Callable) -> Any

This method is like set_ except that it accepts updater to produce the value to set.

Parameters:

  • obj (Any): Object to modify.
  • path (Union[str, List]): Path of the property to set.
  • updater (Callable): Function to produce the updated value.

Returns:

  • Any: obj.

Example:

from pydash import update

obj = {'a': [{'b': {'c': 3}}]}
update(obj, 'a[0].b.c', lambda x: x * 2)
# obj is now {'a': [{'b': {'c': 6}}]}

update_with

def update_with(obj: Any, path: Union[str, List], updater: Callable, customizer: Callable = None) -> Any

Like update except that it accepts customizer which is invoked to produce the objects of path.

Parameters:

  • obj (Any): Object to modify.
  • path (Union[str, List]): Path of the property to set.
  • updater (Callable): Function to produce the updated value.
  • customizer (Callable, optional): Function to customize assigned values.

Returns:

  • Any: obj.

Key/Value Operations

keys

def keys(obj: Any) -> List[Any]

Creates a list of the own enumerable property names of object.

Parameters:

  • obj (Any): Object to query.

Returns:

  • List[Any]: List of property names.

Example:

from pydash import keys

keys({'a': 1, 'b': 2, 'c': 3})
# ['a', 'b', 'c']

keys([1, 2, 3])
# [0, 1, 2]

values

def values(obj: Any) -> List[Any]

Creates a list of the own enumerable property values of object.

Parameters:

  • obj (Any): Object to query.

Returns:

  • List[Any]: List of property values.

Example:

from pydash import values

values({'a': 1, 'b': 2, 'c': 3})
# [1, 2, 3]

values([1, 2, 3])
# [1, 2, 3]

to_pairs

def to_pairs(obj: Any) -> List[Tuple[Any, Any]]

Creates a list of key-value pairs from object.

Parameters:

  • obj (Any): Object to query.

Returns:

  • List[Tuple[Any, Any]]: List of key-value pairs.

Example:

from pydash import to_pairs

to_pairs({'a': 1, 'b': 2})
# [('a', 1), ('b', 2)]

invert

def invert(obj: Union[Mapping, Iterable]) -> Dict[Any, Any]

Creates an object composed of the inverted keys and values of object. If object contains duplicate values, subsequent values overwrite property assignments of previous values.

Parameters:

  • obj (Union[Mapping, Iterable]): Object to invert.

Returns:

  • Dict[Any, Any]: New inverted object.

Example:

from pydash import invert

invert({'a': 1, 'b': 2, 'c': 1})
# {1: 'c', 2: 'b'}

invert(['a', 'b', 'c'])
# {'a': 0, 'b': 1, 'c': 2}

invert_by

def invert_by(obj: Mapping, iteratee: Callable = None) -> Dict[Any, List[Any]]

Like invert except that the inverted object is generated from the results of running each element of object through iteratee. The corresponding inverted value of each inverted key is a list of keys responsible for generating the inverted value.

Parameters:

  • obj (Mapping): Object to invert.
  • iteratee (Callable, optional): Function invoked per element.

Returns:

  • Dict[Any, List[Any]]: New inverted object.

Example:

from pydash import invert_by

invert_by({'a': 1, 'b': 2, 'c': 1})
# {1: ['a', 'c'], 2: ['b']}

map_keys

def map_keys(obj: Any, iteratee: Callable = None) -> Dict[Any, Any]

Creates an object with the same values as object and keys generated by running each own enumerable property of object through iteratee.

Parameters:

  • obj (Any): Object to iterate over.
  • iteratee (Callable, optional): Function invoked per iteration.

Returns:

  • Dict[Any, Any]: New mapped object.

Example:

from pydash import map_keys

map_keys({'a': 1, 'b': 2}, lambda val, key: key + val)
# {'a1': 1, 'b2': 2}

map_values

def map_values(obj: Any, iteratee: Callable = None) -> Dict[Any, Any]

Creates an object with the same keys as object and values generated by running each own enumerable property of object through iteratee.

Parameters:

  • obj (Any): Object to iterate over.
  • iteratee (Callable, optional): Function invoked per iteration.

Returns:

  • Dict[Any, Any]: New mapped object.

Example:

from pydash import map_values

map_values({'a': 1, 'b': 2}, lambda x: x * 2)
# {'a': 2, 'b': 4}

# Using property shorthand
users = {'john': {'age': 30}, 'jane': {'age': 25}}
map_values(users, 'age')
# {'john': 30, 'jane': 25}

map_values_deep

def map_values_deep(obj: Any, iteratee: Callable = None) -> Any

Like map_values but recursively maps nested values.

Parameters:

  • obj (Any): Object to iterate over.
  • iteratee (Callable, optional): Function invoked per iteration.

Returns:

  • Any: New mapped object.

Example:

from pydash import map_values_deep

obj = {'a': {'b': {'c': 1}}, 'd': [{'e': 2}]}
map_values_deep(obj, lambda x: x * 2 if isinstance(x, int) else x)
# {'a': {'b': {'c': 2}}, 'd': [{'e': 4}]}

Object Filtering and Selection

omit

def omit(obj: Any, *properties: Union[str, List]) -> Dict[Any, Any]

Creates an object composed of the own and inherited enumerable properties of object that are not omitted.

Parameters:

  • obj (Any): Source object.
  • properties (*Union[str, List]): Properties to omit.

Returns:

  • Dict[Any, Any]: New object with omitted properties.

Example:

from pydash import omit

omit({'a': 1, 'b': 2, 'c': 3}, 'a', 'c')
# {'b': 2}

omit({'a': {'b': 2}, 'c': 3}, 'a.b')
# {'a': {}, 'c': 3}

omit_by

def omit_by(obj: Any, iteratee: Callable = None) -> Dict[Any, Any]

Creates an object composed of the own and inherited enumerable properties of object that iteratee doesn't return truthy for.

Parameters:

  • obj (Any): Source object.
  • iteratee (Callable, optional): Function invoked per property.

Returns:

  • Dict[Any, Any]: New object with omitted properties.

Example:

from pydash import omit_by

omit_by({'a': 1, 'b': '2', 'c': 3}, lambda x: isinstance(x, str))
# {'a': 1, 'c': 3}

pick

def pick(obj: Any, *properties: Union[str, List]) -> Dict[Any, Any]

Creates an object composed of the picked object properties.

Parameters:

  • obj (Any): Source object.
  • properties (*Union[str, List]): Properties to pick.

Returns:

  • Dict[Any, Any]: New object with picked properties.

Example:

from pydash import pick

pick({'a': 1, 'b': 2, 'c': 3}, 'a', 'c')
# {'a': 1, 'c': 3}

pick({'a': {'b': 2}, 'c': 3}, 'a.b')
# {'a': {'b': 2}}

pick_by

def pick_by(obj: Any, iteratee: Callable = None) -> Dict[Any, Any]

Creates an object composed of the own and inherited enumerable properties of object that iteratee returns truthy for.

Parameters:

  • obj (Any): Source object.
  • iteratee (Callable, optional): Function invoked per property.

Returns:

  • Dict[Any, Any]: New object with picked properties.

Example:

from pydash import pick_by

pick_by({'a': 1, 'b': '2', 'c': 3}, lambda x: isinstance(x, int))
# {'a': 1, 'c': 3}

find_key

def find_key(obj: Any, predicate: Callable = None) -> Any

Returns the key of the first element predicate returns truthy for.

Parameters:

  • obj (Any): Object to inspect.
  • predicate (Callable, optional): Function invoked per iteration.

Returns:

  • Any: Key of matched element, else None.

Example:

from pydash import find_key

users = {
    'barney': {'age': 36, 'active': True},
    'fred': {'age': 40, 'active': False},
    'pebbles': {'age': 1, 'active': True}
}
find_key(users, lambda x: x['age'] < 40)
# 'barney'

find_last_key

def find_last_key(obj: Any, predicate: Callable = None) -> Any

Like find_key except that it iterates over elements in the opposite order.

Parameters:

  • obj (Any): Object to inspect.
  • predicate (Callable, optional): Function invoked per iteration.

Returns:

  • Any: Key of matched element, else None.

Object Merging and Assignment

assign

def assign(obj: Any, *sources: Any) -> Any

Assigns own enumerable string keyed properties of source objects to the destination object. Source objects are applied from left to right.

Parameters:

  • obj (Any): Destination object.
  • sources (*Any): Source objects.

Returns:

  • Any: obj.

Example:

from pydash import assign

assign({'a': 1}, {'b': 2}, {'c': 3})
# {'a': 1, 'b': 2, 'c': 3}

assign_with

def assign_with(obj: Any, *sources: Any, customizer: Callable = None) -> Any

Like assign except that it accepts customizer which is invoked to produce the assigned values.

Parameters:

  • obj (Any): Destination object.
  • sources (*Any): Source objects.
  • customizer (Callable, optional): Function to customize assigned values.

Returns:

  • Any: obj.

defaults

def defaults(obj: Any, *sources: Any) -> Any

Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to None or are not defined.

Parameters:

  • obj (Any): Destination object.
  • sources (*Any): Source objects.

Returns:

  • Any: obj.

Example:

from pydash import defaults

defaults({'a': 1}, {'b': 2}, {'a': 3, 'c': 3})
# {'a': 1, 'b': 2, 'c': 3}

defaults_deep

def defaults_deep(obj: Any, *sources: Any) -> Any

Like defaults except that it recursively assigns default properties.

Parameters:

  • obj (Any): Destination object.
  • sources (*Any): Source objects.

Returns:

  • Any: obj.

Example:

from pydash import defaults_deep

defaults_deep({'a': {'b': 2}}, {'a': {'b': 1, 'c': 3}})
# {'a': {'b': 2, 'c': 3}}

merge

def merge(obj: Any, *sources: Any) -> Any

Recursively merges own and inherited enumerable string keyed properties of source objects into the destination object.

Parameters:

  • obj (Any): Destination object.
  • sources (*Any): Source objects.

Returns:

  • Any: obj.

Example:

from pydash import merge

obj = {'a': [{'b': 2}, {'d': 4}]}
other = {'a': [{'c': 3}, {'e': 5}]}
merge(obj, other)
# {'a': [{'b': 2, 'c': 3}, {'d': 4, 'e': 5}]}

merge_with

def merge_with(obj: Any, *sources: Any, customizer: Callable = None) -> Any

Like merge except that it accepts customizer which is invoked to produce the merged values.

Parameters:

  • obj (Any): Destination object.
  • sources (*Any): Source objects.
  • customizer (Callable, optional): Function to customize assigned values.

Returns:

  • Any: obj.

Object Cloning

clone

def clone(value: T) -> T

Creates a shallow clone of value.

Parameters:

  • value (T): Value to clone.

Returns:

  • T: Cloned value.

Example:

from pydash import clone

objects = [{'a': 1}, {'b': 2}]
shallow = clone(objects)
# shallow == objects but shallow is not objects
# shallow[0] is objects[0] (shallow clone)

clone_deep

def clone_deep(value: T) -> T

Creates a deep clone of value.

Parameters:

  • value (T): Value to clone.

Returns:

  • T: Deep cloned value.

Example:

from pydash import clone_deep

objects = [{'a': 1}, {'b': 2}]
deep = clone_deep(objects)
# deep == objects but deep is not objects
# deep[0] is not objects[0] (deep clone)

clone_with

def clone_with(value: Any, customizer: Callable = None) -> Any

Like clone except that it accepts customizer which is invoked to produce the cloned value.

Parameters:

  • value (Any): Value to clone.
  • customizer (Callable, optional): Function to customize cloning.

Returns:

  • Any: Cloned value.

clone_deep_with

def clone_deep_with(value: Any, customizer: Callable = None) -> Any

Like clone_deep except that it accepts customizer which is invoked to produce the cloned value.

Parameters:

  • value (Any): Value to clone.
  • customizer (Callable, optional): Function to customize cloning.

Returns:

  • Any: Deep cloned value.

Object Transformation

transform

def transform(obj: Any, iteratee: Callable = None, accumulator: Any = None) -> Any

An alternative to reduce for objects. This method transforms obj to a new accumulator object which is the result of running each of its own enumerable string keyed properties through iteratee.

Parameters:

  • obj (Any): Object to iterate over.
  • iteratee (Callable, optional): Function invoked per iteration.
  • accumulator (Any, optional): Custom accumulator value.

Returns:

  • Any: Accumulated value.

Example:

from pydash import transform

transform([1, 2, 3, 4], lambda acc, val, key: acc.append(val * 2), [])
# [2, 4, 6, 8]

transform({'a': 1, 'b': 2, 'c': 1}, lambda acc, val, key: acc.setdefault(val, []).append(key), {})
# {1: ['a', 'c'], 2: ['b']}

rename_keys

def rename_keys(obj: Dict[Any, Any], key_map: Dict[Any, Any]) -> Dict[Any, Any]

Rename the keys of obj using the mapping in key_map and return new object.

Parameters:

  • obj (Dict[Any, Any]): Object to rename.
  • key_map (Dict[Any, Any]): Mapping from old keys to new keys.

Returns:

  • Dict[Any, Any]: Object with renamed keys.

Example:

from pydash import rename_keys

rename_keys({'a': 1, 'b': 2}, {'a': 'x', 'b': 'y'})
# {'x': 1, 'y': 2}

Function Application

apply

def apply(obj: T, func: Callable[[T], T2]) -> T2

Apply function func to obj.

Parameters:

  • obj (T): Object to apply function to.
  • func (Callable[[T], T2]): Function to apply.

Returns:

  • T2: Result of applying func to obj.

Example:

from pydash import apply

apply([1, 2, 3], sum)
# 6

apply_if

def apply_if(obj: T, func: Callable[[T], T2], predicate: Callable[[T], bool]) -> Union[T, T2]

Apply function func to obj only if predicate returns True for obj.

Parameters:

  • obj (T): Object to apply function to.
  • func (Callable[[T], T2]): Function to apply.
  • predicate (Callable[[T], bool]): Predicate to determine whether to apply function.

Returns:

  • Union[T, T2]: Result of applying func to obj if predicate is truthy, else obj.

apply_if_not_none

def apply_if_not_none(obj: Optional[T], func: Callable[[T], T2]) -> Optional[T2]

Apply function func to obj only if obj is not None.

Parameters:

  • obj (Optional[T]): Object to apply function to.
  • func (Callable[[T], T2]): Function to apply.

Returns:

  • Optional[T2]: Result of applying func to obj if obj is not None, else None.

apply_catch

def apply_catch(obj: Any, func: Callable, exceptions: Union[Exception, Tuple[Exception, ...]] = Exception, default: Any = None) -> Any

Apply function func to obj and catch any exceptions raised.

Parameters:

  • obj (Any): Object to apply function to.
  • func (Callable): Function to apply.
  • exceptions (Union[Exception, Tuple[Exception, ...]]): Exception types to catch. Defaults to Exception.
  • default (Any): Default value to return if exception is caught. Defaults to None.

Returns:

  • Any: Result of applying func to obj or default if exception is caught.

invoke

def invoke(obj: Any, path: Union[str, List], *args: Any, **kwargs: Any) -> Any

Invokes the method at path of object.

Parameters:

  • obj (Any): Object to query.
  • path (Union[str, List]): Path of the method to invoke.
  • args (*Any): Arguments to invoke the method with.
  • kwargs (**Any): Keyword arguments to invoke the method with.

Returns:

  • Any: Result of the invoked method.

Example:

from pydash import invoke

obj = {'a': [{'b': {'c': [1, 2, 3, 4]}}]}
invoke(obj, 'a[0].b.c.pop', 1)
# 2

Object Iteration

for_in

def for_in(obj: Any, iteratee: Callable = None) -> Any

Iterates over own and inherited enumerable properties of an object and invokes iteratee for each property.

Parameters:

  • obj (Any): Object to iterate over.
  • iteratee (Callable, optional): Function invoked per iteration.

Returns:

  • Any: obj.

for_in_right

def for_in_right(obj: Any, iteratee: Callable = None) -> Any

Like for_in except that it iterates over properties in the opposite order.

Parameters:

  • obj (Any): Object to iterate over.
  • iteratee (Callable, optional): Function invoked per iteration.

Returns:

  • Any: obj.

callables

def callables(obj: Any) -> List[Any]

Creates a list of function property names from own enumerable properties of object.

Parameters:

  • obj (Any): Object to inspect.

Returns:

  • List[Any]: List of function property names.

Example:

from pydash import callables

class MyClass:
    def method1(self): pass
    def method2(self): pass
    attr = 'value'

callables(MyClass())
# ['method1', 'method2']

Type Conversion Functions

to_boolean

def to_boolean(obj: Any) -> bool

Converts value to a boolean.

Parameters:

  • obj (Any): Value to convert.

Returns:

  • bool: Converted boolean.

Example:

from pydash import to_boolean

to_boolean(1)
# True

to_boolean(0)
# False

to_boolean('true')
# True

to_boolean('')
# False

to_dict

def to_dict(obj: Any) -> Dict[Any, Any]

Converts value to a dictionary.

Parameters:

  • obj (Any): Value to convert.

Returns:

  • Dict[Any, Any]: Converted dictionary.

Example:

from pydash import to_dict

to_dict([1, 2, 3])
# {0: 1, 1: 2, 2: 3}

to_dict('abc')
# {0: 'a', 1: 'b', 2: 'c'}

to_integer

def to_integer(obj: Any) -> int

Converts value to an integer.

Parameters:

  • obj (Any): Value to convert.

Returns:

  • int: Converted integer.

Example:

from pydash import to_integer

to_integer(3.2)
# 3

to_integer('3')
# 3

to_integer('abc')
# 0

to_list

def to_list(obj: Any, split_strings: bool = True) -> List[Any]

Converts value to a list.

Parameters:

  • obj (Any): Value to convert.
  • split_strings (bool): Whether to split strings into characters. Defaults to True.

Returns:

  • List[Any]: Converted list.

Example:

from pydash import to_list

to_list({'a': 1, 'b': 2})
# [1, 2]

to_list('abc')
# ['a', 'b', 'c']

to_list('abc', split_strings=False)
# ['abc']

to_number

def to_number(obj: Any, precision: int = 0) -> Union[float, None]

Converts value to a number.

Parameters:

  • obj (Any): Value to convert.
  • precision (int): Precision to round to. Defaults to 0.

Returns:

  • Union[float, None]: Converted number or None.

Example:

from pydash import to_number

to_number('3.2')
# 3.2

to_number('3.258', 2)
# 3.26

to_string

def to_string(obj: Any) -> str

Converts value to a string.

Parameters:

  • obj (Any): Value to convert.

Returns:

  • str: Converted string.

Example:

from pydash import to_string

to_string([1, 2, 3])
# '1,2,3'

to_string({'a': 1})
# "{'a': 1}"

parse_int

def parse_int(value: Any, radix: Union[int, None] = None) -> Union[int, None]

Converts string to an integer of the specified radix. If radix is falsy, a radix of 10 is used unless value is a hexadecimal, in which case a radix of 16 is used.

Parameters:

  • value (Any): String to convert.
  • radix (Union[int, None]): Radix to interpret value by.

Returns:

  • Union[int, None]: Converted integer.

Example:

from pydash import parse_int

parse_int('08')
# 8

parse_int('0x20')
# 32

parse_int('08', 10)
# 8

parse_int('1010', 2)
# 10

This Objects module provides comprehensive functionality for manipulating objects and dictionaries with 48 functions covering property access, transformation, cloning, merging, and type conversion operations.

Install with Tessl CLI

npx tessl i tessl/pypi-pydash

docs

arrays.md

chaining.md

collections.md

functions.md

index.md

numerical.md

objects.md

predicates.md

strings.md

utilities.md

tile.json