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

predicates.mddocs/

Predicates Module

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.

Value Comparison Predicates

eq

def eq(a: Any, b: Any) -> bool

Performs 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)
# True

eq_cmp

def eq_cmp(a: Any) -> Callable

Creates 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)  # False

gt

def gt(a: Any, b: Any) -> bool

Checks 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)
# False

gt_cmp

def gt_cmp(a: Any) -> Callable

Creates a comparator function for greater than comparison.

Parameters:

  • a (Any): Value to compare against.

Returns:

  • Callable: Comparator function.

gte

def gte(a: Any, b: Any) -> bool

Checks 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)
# True

gte_cmp

def gte_cmp(a: Any) -> Callable

Creates a comparator function for greater than or equal comparison.

Parameters:

  • a (Any): Value to compare against.

Returns:

  • Callable: Comparator function.

lt

def lt(a: Any, b: Any) -> bool

Checks 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)
# False

lt_cmp

def lt_cmp(a: Any) -> Callable

Creates a comparator function for less than comparison.

Parameters:

  • a (Any): Value to compare against.

Returns:

  • Callable: Comparator function.

lte

def lte(a: Any, b: Any) -> bool

Checks 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)
# True

lte_cmp

def lte_cmp(a: Any) -> Callable

Creates a comparator function for less than or equal comparison.

Parameters:

  • a (Any): Value to compare against.

Returns:

  • Callable: Comparator function.

Equality and Matching Predicates

is_equal

def is_equal(a: Any, b: Any) -> bool

Performs 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}})
# True

is_equal_cmp

def is_equal_cmp(a: Any) -> Callable

Creates a comparator function for deep equality.

Parameters:

  • a (Any): Value to compare against.

Returns:

  • Callable: Comparator function.

is_equal_with

def is_equal_with(a: Any, b: Any, customizer: Callable = None) -> bool

Like 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.

is_equal_with_cmp

def is_equal_with_cmp(a: Any, customizer: Callable = None) -> Callable

Creates 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.

is_match

def is_match(obj: Any, src: Any) -> bool

Performs 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})
# False

is_match_cmp

def is_match_cmp(src: Any) -> Callable

Creates a comparator function for partial deep comparison.

Parameters:

  • src (Any): Object of property values to match.

Returns:

  • Callable: Comparator function.

is_match_with

def is_match_with(obj: Any, src: Any, customizer: Callable = None) -> bool

Like 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.

is_match_with_cmp

def is_match_with_cmp(src: Any, customizer: Callable = None) -> Callable

Creates 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.

Type Checking Predicates

is_associative

def is_associative(value: Any) -> bool

Checks 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([])
# False

is_boolean

def is_boolean(value: Any) -> bool

Checks 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)
# False

is_builtin

def is_builtin(value: Any) -> bool

Checks 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)
# False

is_date

def is_date(value: Any) -> bool

Checks 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')
# False

is_dict

def is_dict(value: Any) -> bool

Checks 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([])
# False

is_error

def is_error(value: Any) -> bool

Checks 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')
# False

is_float

def is_float(value: Any) -> bool

Checks 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)
# False

is_function

def is_function(value: Any) -> bool

Checks 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')
# False

is_indexed

def is_indexed(value: Any) -> bool

Checks 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})
# False

is_instance_of

def is_instance_of(value: Any, types: Union[type, Tuple[type, ...]]) -> bool

Checks 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)
# False

is_instance_of_cmp

def is_instance_of_cmp(types: Union[type, Tuple[type, ...]]) -> Callable

Creates a comparator function for instance checking.

Parameters:

  • types (Union[type, Tuple[type, ...]]): Type or tuple of types to check against.

Returns:

  • Callable: Comparator function.

is_integer

def is_integer(value: Any) -> bool

Checks 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')
# False

is_iterable

def is_iterable(value: Any) -> bool

Checks 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)
# False

is_list

def is_list(value: Any) -> bool

Checks 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))
# False

is_none

def is_none(value: Any) -> bool

Checks 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('')
# False

is_number

def is_number(value: Any) -> bool

Checks 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')
# False

is_object

def is_object(value: Any) -> bool

Checks 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')
# False

is_reg_exp

def is_reg_exp(value: Any) -> bool

Checks 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+')
# False

is_set

def is_set(value: Any) -> bool

Checks 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])
# False

is_string

def is_string(value: Any) -> bool

Checks 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)
# False

is_tuple

def is_tuple(value: Any) -> bool

Checks 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])
# False

Content and State Predicates

is_blank

def is_blank(value: Any) -> bool

Checks 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')
# False

is_empty

def is_empty(value: Any) -> bool

Checks 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])
# False

is_json

def is_json(value: Any) -> bool

Checks 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}')
# False

Numerical Predicates

is_even

def is_even(value: Any) -> bool

Checks 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)
# True

is_odd

def is_odd(value: Any) -> bool

Checks 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)
# True

is_nan

def is_nan(value: Any) -> bool

Checks 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)
# False

is_negative

def is_negative(value: Any) -> bool

Checks 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)
# False

is_positive

def is_positive(value: Any) -> bool

Checks 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)
# False

is_zero

def is_zero(value: Any) -> bool

Checks 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)
# False

in_range

def in_range(value: Union[int, float], start: Union[int, float] = 0, end: Union[int, float] = None) -> bool

Checks 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)

in_range_cmp

def in_range_cmp(start: Union[int, float] = 0, end: Union[int, float] = None) -> Callable

Creates 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.

Sequence Analysis Predicates

is_decreasing

def is_decreasing(value: Iterable) -> bool

Checks 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])
# False

is_increasing

def is_increasing(value: Iterable) -> bool

Checks 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])
# False

is_monotone

def is_monotone(value: Iterable, op: Callable = None) -> bool

Checks 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)

is_monotone_cmp

def is_monotone_cmp(op: Callable) -> Callable

Creates a comparator function for monotone checking.

Parameters:

  • op (Callable): Operator function for comparison.

Returns:

  • Callable: Comparator function.

is_strictly_decreasing

def is_strictly_decreasing(value: Iterable) -> bool

Checks 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)

is_strictly_increasing

def is_strictly_increasing(value: Iterable) -> bool

Checks 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)

Usage Examples

Data Validation

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)  # []

Type-Safe Operations

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}

Filtering with Complex Predicates

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]

Sequence Analysis

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"

Custom Comparators

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

docs

arrays.md

chaining.md

collections.md

functions.md

index.md

numerical.md

objects.md

predicates.md

strings.md

utilities.md

tile.json