A comprehensive Python utility library for functional programming inspired by JavaScript's Lo-Dash
—
The Predicates module provides 59 functions for testing and type checking. These functions enable value comparison, type detection, content validation, and sequence analysis with comprehensive predicate and comparator functions.
def eq(a: Any, b: Any) -> boolPerforms SameValueZero comparison between two values to determine if they are equivalent.
Parameters:
a (Any): First value to compare.b (Any): Second value to compare.Returns:
bool: True if values are equivalent, else False.Example:
from pydash import eq
eq('a', 'a')
# True
eq('a', 'b')
# False
eq(1, 1.0)
# Truedef eq_cmp(a: Any) -> CallableCreates a comparator function that performs SameValueZero equality comparison with the given value.
Parameters:
a (Any): Value to compare against.Returns:
Callable: Comparator function.Example:
from pydash import eq_cmp
is_five = eq_cmp(5)
is_five(5) # True
is_five(10) # Falsedef gt(a: Any, b: Any) -> boolChecks if a is greater than b.
Parameters:
a (Any): First value to compare.b (Any): Second value to compare.Returns:
bool: True if a > b, else False.Example:
from pydash import gt
gt(3, 1)
# True
gt(3, 3)
# Falsedef gt_cmp(a: Any) -> CallableCreates a comparator function for greater than comparison.
Parameters:
a (Any): Value to compare against.Returns:
Callable: Comparator function.def gte(a: Any, b: Any) -> boolChecks if a is greater than or equal to b.
Parameters:
a (Any): First value to compare.b (Any): Second value to compare.Returns:
bool: True if a >= b, else False.Example:
from pydash import gte
gte(3, 1)
# True
gte(3, 3)
# Truedef gte_cmp(a: Any) -> CallableCreates a comparator function for greater than or equal comparison.
Parameters:
a (Any): Value to compare against.Returns:
Callable: Comparator function.def lt(a: Any, b: Any) -> boolChecks if a is less than b.
Parameters:
a (Any): First value to compare.b (Any): Second value to compare.Returns:
bool: True if a < b, else False.Example:
from pydash import lt
lt(1, 3)
# True
lt(3, 3)
# Falsedef lt_cmp(a: Any) -> CallableCreates a comparator function for less than comparison.
Parameters:
a (Any): Value to compare against.Returns:
Callable: Comparator function.def lte(a: Any, b: Any) -> boolChecks if a is less than or equal to b.
Parameters:
a (Any): First value to compare.b (Any): Second value to compare.Returns:
bool: True if a <= b, else False.Example:
from pydash import lte
lte(1, 3)
# True
lte(3, 3)
# Truedef lte_cmp(a: Any) -> CallableCreates a comparator function for less than or equal comparison.
Parameters:
a (Any): Value to compare against.Returns:
Callable: Comparator function.def is_equal(a: Any, b: Any) -> boolPerforms a deep comparison to determine if two values are equivalent.
Parameters:
a (Any): First value to compare.b (Any): Second value to compare.Returns:
bool: True if values are equivalent, else False.Example:
from pydash import is_equal
is_equal({'a': 1}, {'a': 1})
# True
is_equal([1, 2, 3], [1, 2, 3])
# True
is_equal({'a': {'b': 1}}, {'a': {'b': 1}})
# Truedef is_equal_cmp(a: Any) -> CallableCreates a comparator function for deep equality.
Parameters:
a (Any): Value to compare against.Returns:
Callable: Comparator function.def is_equal_with(a: Any, b: Any, customizer: Callable = None) -> boolLike is_equal except that it accepts customizer which is invoked to compare values.
Parameters:
a (Any): First value to compare.b (Any): Second value to compare.customizer (Callable, optional): Function to customize comparisons.Returns:
bool: True if values are equivalent, else False.def is_equal_with_cmp(a: Any, customizer: Callable = None) -> CallableCreates a comparator function for custom deep equality.
Parameters:
a (Any): Value to compare against.customizer (Callable, optional): Function to customize comparisons.Returns:
Callable: Comparator function.def is_match(obj: Any, src: Any) -> boolPerforms a partial deep comparison between obj and src to determine if obj contains equivalent property values.
Parameters:
obj (Any): Object to inspect.src (Any): Object of property values to match.Returns:
bool: True if obj matches, else False.Example:
from pydash import is_match
obj = {'a': 1, 'b': 2, 'c': 3}
is_match(obj, {'a': 1, 'c': 3})
# True
is_match(obj, {'a': 1, 'c': 4})
# Falsedef is_match_cmp(src: Any) -> CallableCreates a comparator function for partial deep comparison.
Parameters:
src (Any): Object of property values to match.Returns:
Callable: Comparator function.def is_match_with(obj: Any, src: Any, customizer: Callable = None) -> boolLike is_match except that it accepts customizer which is invoked to compare values.
Parameters:
obj (Any): Object to inspect.src (Any): Object of property values to match.customizer (Callable, optional): Function to customize comparisons.Returns:
bool: True if obj matches, else False.def is_match_with_cmp(src: Any, customizer: Callable = None) -> CallableCreates a comparator function for custom partial deep comparison.
Parameters:
src (Any): Object of property values to match.customizer (Callable, optional): Function to customize comparisons.Returns:
Callable: Comparator function.def is_associative(value: Any) -> boolChecks if value is an associative object (dict-like).
Parameters:
value (Any): Value to check.Returns:
bool: True if value is associative, else False.Example:
from pydash import is_associative
is_associative({})
# True
is_associative({'a': 1})
# True
is_associative([])
# Falsedef is_boolean(value: Any) -> boolChecks if value is a boolean primitive or object.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a boolean, else False.Example:
from pydash import is_boolean
is_boolean(False)
# True
is_boolean(True)
# True
is_boolean(0)
# Falsedef is_builtin(value: Any) -> boolChecks if value is a built-in function.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a built-in function, else False.Example:
from pydash import is_builtin
is_builtin(len)
# True
is_builtin(lambda x: x)
# Falsedef is_date(value: Any) -> boolChecks if value is a date object.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a date object, else False.Example:
from pydash import is_date
from datetime import datetime
is_date(datetime.now())
# True
is_date('2021-01-01')
# Falsedef is_dict(value: Any) -> boolChecks if value is a dictionary.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a dictionary, else False.Example:
from pydash import is_dict
is_dict({})
# True
is_dict({'a': 1})
# True
is_dict([])
# Falsedef is_error(value: Any) -> boolChecks if value is an Error object.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is an error, else False.Example:
from pydash import is_error
is_error(Exception('Error'))
# True
is_error('Error')
# Falsedef is_float(value: Any) -> boolChecks if value is a float.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a float, else False.Example:
from pydash import is_float
is_float(1.5)
# True
is_float(1)
# Falsedef is_function(value: Any) -> boolChecks if value is a function.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a function, else False.Example:
from pydash import is_function
is_function(lambda x: x)
# True
is_function(len)
# True
is_function('string')
# Falsedef is_indexed(value: Any) -> boolChecks if value is indexed (array-like with integer indexing).
Parameters:
value (Any): Value to check.Returns:
bool: True if value is indexed, else False.Example:
from pydash import is_indexed
is_indexed([1, 2, 3])
# True
is_indexed((1, 2, 3))
# True
is_indexed({'a': 1})
# Falsedef is_instance_of(value: Any, types: Union[type, Tuple[type, ...]]) -> boolChecks if value is an instance of types.
Parameters:
value (Any): Value to check.types (Union[type, Tuple[type, ...]]): Type or tuple of types to check against.Returns:
bool: True if value is an instance of types, else False.Example:
from pydash import is_instance_of
is_instance_of('hello', str)
# True
is_instance_of(42, (int, float))
# True
is_instance_of([], str)
# Falsedef is_instance_of_cmp(types: Union[type, Tuple[type, ...]]) -> CallableCreates a comparator function for instance checking.
Parameters:
types (Union[type, Tuple[type, ...]]): Type or tuple of types to check against.Returns:
Callable: Comparator function.def is_integer(value: Any) -> boolChecks if value is an integer.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is an integer, else False.Example:
from pydash import is_integer
is_integer(1)
# True
is_integer(1.0)
# False
is_integer('1')
# Falsedef is_iterable(value: Any) -> boolChecks if value is iterable.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is iterable, else False.Example:
from pydash import is_iterable
is_iterable([1, 2, 3])
# True
is_iterable('hello')
# True
is_iterable(42)
# Falsedef is_list(value: Any) -> boolChecks if value is a list.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a list, else False.Example:
from pydash import is_list
is_list([1, 2, 3])
# True
is_list((1, 2, 3))
# Falsedef is_none(value: Any) -> boolChecks if value is None.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is None, else False.Example:
from pydash import is_none
is_none(None)
# True
is_none(0)
# False
is_none('')
# Falsedef is_number(value: Any) -> boolChecks if value is a number (int or float).
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a number, else False.Example:
from pydash import is_number
is_number(1)
# True
is_number(1.5)
# True
is_number('1')
# Falsedef is_object(value: Any) -> boolChecks if value is an object (not a primitive type).
Parameters:
value (Any): Value to check.Returns:
bool: True if value is an object, else False.Example:
from pydash import is_object
is_object({})
# True
is_object([])
# True
is_object('string')
# Falsedef is_reg_exp(value: Any) -> boolChecks if value is a regular expression object.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a regular expression, else False.Example:
from pydash import is_reg_exp
import re
is_reg_exp(re.compile(r'\d+'))
# True
is_reg_exp(r'\d+')
# Falsedef is_set(value: Any) -> boolChecks if value is a set.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a set, else False.Example:
from pydash import is_set
is_set({1, 2, 3})
# True
is_set([1, 2, 3])
# Falsedef is_string(value: Any) -> boolChecks if value is a string.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a string, else False.Example:
from pydash import is_string
is_string('hello')
# True
is_string(123)
# Falsedef is_tuple(value: Any) -> boolChecks if value is a tuple.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is a tuple, else False.Example:
from pydash import is_tuple
is_tuple((1, 2, 3))
# True
is_tuple([1, 2, 3])
# Falsedef is_blank(value: Any) -> boolChecks if value is blank (empty string or whitespace only).
Parameters:
value (Any): Value to check.Returns:
bool: True if value is blank, else False.Example:
from pydash import is_blank
is_blank('')
# True
is_blank(' ')
# True
is_blank('hello')
# Falsedef is_empty(value: Any) -> boolChecks if value is empty. A value is considered empty if it's falsey or an empty iterable.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is empty, else False.Example:
from pydash import is_empty
is_empty([])
# True
is_empty({})
# True
is_empty('')
# True
is_empty([1, 2, 3])
# Falsedef is_json(value: Any) -> boolChecks if value is a valid JSON string.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is valid JSON, else False.Example:
from pydash import is_json
is_json('{"a": 1}')
# True
is_json('[1, 2, 3]')
# True
is_json('{invalid json}')
# Falsedef is_even(value: Any) -> boolChecks if value is an even number.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is even, else False.Example:
from pydash import is_even
is_even(2)
# True
is_even(3)
# False
is_even(0)
# Truedef is_odd(value: Any) -> boolChecks if value is an odd number.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is odd, else False.Example:
from pydash import is_odd
is_odd(1)
# True
is_odd(2)
# False
is_odd(3)
# Truedef is_nan(value: Any) -> boolChecks if value is NaN (Not a Number).
Parameters:
value (Any): Value to check.Returns:
bool: True if value is NaN, else False.Example:
from pydash import is_nan
import math
is_nan(float('nan'))
# True
is_nan(math.nan)
# True
is_nan(1)
# Falsedef is_negative(value: Any) -> boolChecks if value is a negative number.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is negative, else False.Example:
from pydash import is_negative
is_negative(-1)
# True
is_negative(0)
# False
is_negative(1)
# Falsedef is_positive(value: Any) -> boolChecks if value is a positive number.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is positive, else False.Example:
from pydash import is_positive
is_positive(1)
# True
is_positive(0)
# False
is_positive(-1)
# Falsedef is_zero(value: Any) -> boolChecks if value is zero.
Parameters:
value (Any): Value to check.Returns:
bool: True if value is zero, else False.Example:
from pydash import is_zero
is_zero(0)
# True
is_zero(0.0)
# True
is_zero(1)
# Falsedef in_range(value: Union[int, float], start: Union[int, float] = 0, end: Union[int, float] = None) -> boolChecks if value is between start and up to, but not including, end.
Parameters:
value (Union[int, float]): Number to check.start (Union[int, float]): Start of range. Defaults to 0.end (Union[int, float], optional): End of range.Returns:
bool: True if value is in range, else False.Example:
from pydash import in_range
in_range(3, 2, 4)
# True
in_range(4, 8)
# True (equivalent to in_range(4, 0, 8))
in_range(2, 2)
# False (start = 2, end = 0, so range is empty)def in_range_cmp(start: Union[int, float] = 0, end: Union[int, float] = None) -> CallableCreates a comparator function for range checking.
Parameters:
start (Union[int, float]): Start of range. Defaults to 0.end (Union[int, float], optional): End of range.Returns:
Callable: Comparator function.def is_decreasing(value: Iterable) -> boolChecks if value is a monotonically decreasing sequence.
Parameters:
value (Iterable): Sequence to check.Returns:
bool: True if sequence is decreasing, else False.Example:
from pydash import is_decreasing
is_decreasing([5, 4, 3, 2, 1])
# True
is_decreasing([5, 4, 4, 2, 1])
# True (allows equal values)
is_decreasing([5, 4, 5, 2, 1])
# Falsedef is_increasing(value: Iterable) -> boolChecks if value is a monotonically increasing sequence.
Parameters:
value (Iterable): Sequence to check.Returns:
bool: True if sequence is increasing, else False.Example:
from pydash import is_increasing
is_increasing([1, 2, 3, 4, 5])
# True
is_increasing([1, 2, 2, 4, 5])
# True (allows equal values)
is_increasing([1, 2, 1, 4, 5])
# Falsedef is_monotone(value: Iterable, op: Callable = None) -> boolChecks if value is a monotonic sequence using the given operator.
Parameters:
value (Iterable): Sequence to check.op (Callable, optional): Operator function for comparison.Returns:
bool: True if sequence is monotonic, else False.Example:
from pydash import is_monotone
import operator
is_monotone([1, 2, 3, 4, 5], operator.le)
# True (non-decreasing)
is_monotone([5, 4, 3, 2, 1], operator.ge)
# True (non-increasing)def is_monotone_cmp(op: Callable) -> CallableCreates a comparator function for monotone checking.
Parameters:
op (Callable): Operator function for comparison.Returns:
Callable: Comparator function.def is_strictly_decreasing(value: Iterable) -> boolChecks if value is a strictly decreasing sequence (no equal adjacent elements).
Parameters:
value (Iterable): Sequence to check.Returns:
bool: True if sequence is strictly decreasing, else False.Example:
from pydash import is_strictly_decreasing
is_strictly_decreasing([5, 4, 3, 2, 1])
# True
is_strictly_decreasing([5, 4, 4, 2, 1])
# False (has equal adjacent elements)def is_strictly_increasing(value: Iterable) -> boolChecks if value is a strictly increasing sequence (no equal adjacent elements).
Parameters:
value (Iterable): Sequence to check.Returns:
bool: True if sequence is strictly increasing, else False.Example:
from pydash import is_strictly_increasing
is_strictly_increasing([1, 2, 3, 4, 5])
# True
is_strictly_increasing([1, 2, 2, 4, 5])
# False (has equal adjacent elements)from pydash import is_string, is_number, is_empty, is_json, in_range
def validate_user_data(data):
errors = []
# Check required fields
if is_empty(data.get('name')):
errors.append('Name is required')
elif not is_string(data.get('name')):
errors.append('Name must be a string')
# Validate age
age = data.get('age')
if not is_number(age):
errors.append('Age must be a number')
elif not in_range(age, 0, 150):
errors.append('Age must be between 0 and 150')
# Validate JSON preferences
prefs = data.get('preferences')
if prefs and not is_json(prefs):
errors.append('Preferences must be valid JSON')
return errors
# Usage
user_data = {'name': 'John', 'age': 25, 'preferences': '{"theme": "dark"}'}
errors = validate_user_data(user_data) # []from pydash import is_list, is_dict, is_string, is_number
def safe_process(data):
if is_list(data):
return [item * 2 if is_number(item) else item for item in data]
elif is_dict(data):
return {k: v.upper() if is_string(v) else v for k, v in data.items()}
elif is_string(data):
return data.upper()
elif is_number(data):
return data * 2
else:
return data
# Usage
safe_process([1, 2, 'hello']) # [2, 4, 'hello']
safe_process({'a': 'hello', 'b': 42}) # {'a': 'HELLO', 'b': 42}from pydash import is_even, is_positive, gt, filter_
def advanced_filter(numbers):
# Filter positive even numbers greater than 10
return filter_(numbers, lambda x: is_even(x) and is_positive(x) and gt(x, 10))
numbers = [-5, 2, 12, -8, 15, 20, 3]
result = advanced_filter(numbers) # [12, 20]from pydash import is_increasing, is_decreasing, is_strictly_increasing
def analyze_trend(data):
if is_strictly_increasing(data):
return "Strong upward trend"
elif is_increasing(data):
return "Upward trend (with plateaus)"
elif is_strictly_decreasing(data):
return "Strong downward trend"
elif is_decreasing(data):
return "Downward trend (with plateaus)"
else:
return "No clear trend"
# Usage
stock_prices = [100, 105, 110, 115, 120]
trend = analyze_trend(stock_prices) # "Strong upward trend"from pydash import is_match_cmp, filter_
# Create reusable predicate functions
is_adult = lambda person: person.get('age', 0) >= 18
is_active_user = is_match_cmp({'status': 'active'})
is_premium = is_match_cmp({'plan': 'premium'})
users = [
{'name': 'John', 'age': 25, 'status': 'active', 'plan': 'basic'},
{'name': 'Jane', 'age': 17, 'status': 'active', 'plan': 'premium'},
{'name': 'Bob', 'age': 30, 'status': 'inactive', 'plan': 'premium'}
]
# Filter using composed predicates
active_adults = filter_(users, lambda u: is_adult(u) and is_active_user(u))
premium_users = filter_(users, is_premium)This Predicates module provides comprehensive testing and validation capabilities with 56 functions covering all aspects of type checking, value comparison, content validation, and sequence analysis for robust data validation and filtering operations.
Install with Tessl CLI
npx tessl i tessl/pypi-pydash