sidekick.pred

This module contains a collection of predicate functions that implement common tests. Since all functions are fn-enabled, it accepts bitwise logic to compound predicates into more interesting expressions, like in the example bellow.

>>> sk.filter(sk.is_odd & sk.is_positive, range(-5, 10))
sk.iter([1, 3, 5, 7, 9])

Composing predicates

cond Conditional evaluation.
any_pred(*predicates) Return a new predicate function that performs a logic OR to all arguments.
all_pred(*predicates) Return a new predicate that performs an logic AND to all predicate functions.

Testing values

is_a Check if x is an instance of cls.
is_equal Check if x == y.
is_identical Check if x is y.
is_false Check if argument is Falseby identity.
is_true Check if argument is Trueby identity.
is_none Check if argument is Noneby identity.
is_truthy Check if argument is truthy.
is_falsy Check if argument is falsy.

Numeric tests

is_even Check if argument is even.
is_odd Check if argument is odd.
is_negative Check if argument is negative.
is_positive Check if argument is positive.
is_strictly_negative Check if argument is strictly negative.
is_strictly_positive Check if argument is strictly positive.
is_zero Check if argument is zero.
is_nonzero Check if argument is nonzero.
is_divisible_by Check if x is divisible by n.

Sequences

is_distinct Test if all elements in sequence are distinct.
is_iterable Test if argument is iterable.
is_seq_equal Return True if the two sequences are equal.

String tests

has_pattern(pattern[, st]) Check if string contains pattern.

API reference

sidekick.pred.cond[source]

Conditional evaluation.

Return a function that tests the argument with the cond function, and then executes either the then or else_ branches.

Examples

>>> collatz = sk.cond(sk.is_even, _ // 2, (3 * _) + 1)
>>> [collatz(1), collatz(2), collatz(3), collatz(4)]
[4, 1, 10, 2]
sidekick.pred.any_pred(*predicates)[source]

Return a new predicate function that performs a logic OR to all arguments.

This behaves in a short-circuit manner and returns the first truthy result, if found, or the last falsy result, otherwise.

sidekick.pred.all_pred(*predicates)[source]

Return a new predicate that performs an logic AND to all predicate functions.

This behaves in a short-circuit manner and returns the first falsy result, if found, or the last truthy result, otherwise.

sidekick.pred.is_a[source]

Check if x is an instance of cls.

Equivalent to isinstance, but auto-curried and with the order or arguments flipped.

Parameters:
  • cls – Type or tuple of types to test for.
  • x – Instance.

Examples

>>> is_int = sk.is_a(int)
>>> is_int(42), is_int(42.0)
(True, False)
sidekick.pred.is_equal[source]

Check if x == y.

sidekick.pred.is_identical[source]

Check if x is y.

sidekick.pred.is_false

Check if argument is Falseby identity.

Examples

>>> sk.is_false("not_false")
False
>>> sk.is_false(False)
True
sidekick.pred.is_true

Check if argument is Trueby identity.

Examples

>>> sk.is_true("not_true")
False
>>> sk.is_true(True)
True
sidekick.pred.is_none

Check if argument is Noneby identity.

Examples

>>> sk.is_none("not_none")
False
>>> sk.is_none(None)
True
sidekick.pred.is_truthy[source]

Check if argument is truthy.

This is the same as calling bool(x)

sidekick.pred.is_falsy[source]

Check if argument is falsy.

This is the same as calling not bool(x)

sidekick.pred.is_even

Check if argument is even.

Examples

>>> sk.is_even(1)
False
>>> sk.is_even(42)
True
sidekick.pred.is_odd

Check if argument is odd.

Examples

>>> sk.is_odd(42)
False
>>> sk.is_odd(1)
True
sidekick.pred.is_negative

Check if argument is negative.

Examples

>>> sk.is_negative(42)
False
>>> sk.is_negative(-10)
True
sidekick.pred.is_positive

Check if argument is positive.

Examples

>>> sk.is_positive(-10)
False
>>> sk.is_positive(42)
True
sidekick.pred.is_strictly_negative

Check if argument is strictly negative.

Examples

>>> sk.is_strictly_negative(0)
False
>>> sk.is_strictly_negative(-10)
True
sidekick.pred.is_strictly_positive

Check if argument is strictly positive.

Examples

>>> sk.is_strictly_positive(0)
False
>>> sk.is_strictly_positive(42)
True
sidekick.pred.is_zero

Check if argument is zero.

Examples

>>> sk.is_zero(42)
False
>>> sk.is_zero(0)
True
sidekick.pred.is_nonzero

Check if argument is nonzero.

Examples

>>> sk.is_nonzero(0)
False
>>> sk.is_nonzero(42)
True
sidekick.pred.is_divisible_by[source]

Check if x is divisible by n.

Examples

>>> sk.is_divisible_by(2, 42)  # 42 is divisible by 2
True
>>> even = sk.is_divisible_by(2)
>>> even(4), even(3)
(True, False)
sidekick.pred.has_pattern(pattern, st=NOT_GIVEN)[source]

Check if string contains pattern.

This function performs a pattern scan. If you want to match the beginning of the string, make it start with a “^”. Add a “$” if it needs to match the end.

Examples

>>> sk.has_pattern("\d{2}", "year, 1942")
True
>>> sk.has_pattern("^\d{2}$", "year, 1942")
False

This function is also very useful to filter or process string data.

>>> is_date = sk.has_pattern("^\d{4}-\d{2}-\d{2}$")
>>> is_date("1917-03-08")
True
>>> is_date("08/03/1917")
False
sidekick.pred.is_distinct[source]

Test if all elements in sequence are distinct.

sidekick.pred.is_iterable[source]

Test if argument is iterable.

sidekick.pred.is_seq_equal[source]

Return True if the two sequences are equal.