py_logic¶
shortfx.fxPython.py_logic
¶
Logical operations and conditional functions for Python collections.
Functions¶
and_all(*args: bool) -> bool
¶
Returns True if all arguments are True; False otherwise.
Description
Python implementation of logical AND operation that evaluates multiple boolean values. Returns True only when all provided arguments evaluate to True, similar to Excel's AND function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
bool
|
One or more boolean values to evaluate. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if all arguments are True, False otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no arguments are provided. |
TypeError
|
If any argument is not a boolean. |
Usage Example
and_all(True, True, True) True and_all(True, False, True) False and_all(5 > 3, 10 < 20, "a" == "a") True
Cost: O(n) where n is the number of arguments
Source code in shortfx/fxPython/py_logic.py
coalesce(*values: Any) -> Any
¶
Return the first non-None value from arguments.
Description
Scans values left-to-right and returns the first that is not None. Returns None if all values are None. Equivalent to SQL COALESCE / VBA Nz.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*values
|
Any
|
Any number of values to check. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
First non-None value, or None. |
Example
coalesce(None, None, 42, 'hello') 42 coalesce(None, 'first') 'first' coalesce(None, None) is None True
Complexity: O(n)
Source code in shortfx/fxPython/py_logic.py
if_error(value: Any, value_if_error: Any) -> Any
¶
Returns value if not an error; otherwise returns alternative value.
Description
Attempts to return the provided value. If value is callable and raises an exception, or if value itself is an exception, returns the alternative error value. Similar to Excel's IFERROR function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value or callable to check for errors. |
required |
value_if_error
|
Any
|
The value to return if an error occurs. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result of value if no error, otherwise value_if_error. |
Usage Example
if_error(10 / 2, "Error!") 5.0 if_error(lambda: 10 / 0, "Error!") 'Error!' if_error(lambda: int("abc"), "Invalid") 'Invalid'
Cost: O(1)
Source code in shortfx/fxPython/py_logic.py
if_then_else(logical_test: bool, value_if_true: Any, value_if_false: Any) -> Any
¶
Returns one value if condition is True, another if False.
Description
Conditional function that evaluates a logical test and returns different values based on the result. Equivalent to Excel's IF function or Python's ternary operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logical_test
|
bool
|
The condition to test. |
required |
value_if_true
|
Any
|
The value to return if logical_test is True. |
required |
value_if_false
|
Any
|
The value to return if logical_test is False. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Either value_if_true or value_if_false based on the test. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If logical_test is not a boolean. |
Usage Example
if_then_else(True, "Yes", "No") 'Yes' if_then_else(5 > 3, "Greater", "Smaller") 'Greater' if_then_else(False, 10, 20) 20
Cost: O(1)
Source code in shortfx/fxPython/py_logic.py
ifs(*args) -> Any
¶
Evaluates multiple conditions and returns the first matching result.
Description
Accepts pairs of (condition, value). Returns the value corresponding to the first True condition. Equivalent to Excel IFS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Alternating condition/value pairs. Must be an even count. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The value associated with the first True condition. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an odd number of arguments is provided or no condition is True. |
Example
ifs(False, "a", True, "b", True, "c") 'b' ifs(1 > 2, "no", 3 > 2, "yes") 'yes'
Complexity: O(n) where n is the number of condition/value pairs
Source code in shortfx/fxPython/py_logic.py
is_blank(value: Any) -> bool
¶
Returns True if value is blank (None, empty string, or empty collection).
Description
Checks whether a value is considered blank. Returns True for None, empty strings, and empty collections. Equivalent to Excel's ISBLANK.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the value is considered blank, False otherwise. |
Usage Example
is_blank(None) True is_blank("") True is_blank([]) True is_blank("Hello") False is_blank(0) False
Cost: O(1) for most cases; O(n) for checking empty collections
Source code in shortfx/fxPython/py_logic.py
is_error(value: Any) -> bool
¶
Returns True if evaluating value produces an error.
Description
Checks if a value (typically a callable) raises an exception when evaluated. Returns True for errors, False otherwise. Similar to Excel's ISERROR function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value or callable to check for errors. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if an error occurs, False otherwise. |
Usage Example
is_error(lambda: 10 / 2) False is_error(lambda: 10 / 0) True is_error(lambda: int("abc")) True
Cost: O(1) plus the cost of executing the callable
Source code in shortfx/fxPython/py_logic.py
is_logical(value: Any) -> bool
¶
Returns True if value is a boolean.
Description
Type checking function that determines if a value is a logical boolean. Equivalent to Excel's ISLOGICAL function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the value is a boolean, False otherwise. |
Usage Example
is_logical(True) True is_logical(False) True is_logical(0) False is_logical("True") False
Cost: O(1)
Source code in shortfx/fxPython/py_logic.py
is_number(value: Any) -> bool
¶
Returns True if value is a number (int or float, not bool).
Description
Type checking function that determines if a value is numeric. Excludes booleans even though they are technically integers in Python. Equivalent to Excel's ISNUMBER function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the value is an int or float, False otherwise. |
Usage Example
is_number(123) True is_number(3.14) True is_number("hello") False is_number(True) False
Cost: O(1)
Source code in shortfx/fxPython/py_logic.py
is_text(value: Any) -> bool
¶
Returns True if value is a string.
Description
Type checking function that determines if a value is text. Equivalent to Excel's ISTEXT function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The value to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the value is a string, False otherwise. |
Usage Example
is_text("hello") True is_text(123) False is_text(True) False
Cost: O(1)
Source code in shortfx/fxPython/py_logic.py
not_value(logical_value: bool) -> bool
¶
Reverses the logical value of its argument.
Description
Returns the opposite boolean value. If True, returns False; if False, returns True. Equivalent to Excel's NOT function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logical_value
|
bool
|
The boolean value to negate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
The opposite boolean value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the argument is not a boolean. |
Usage Example
not_value(True) False not_value(False) True not_value(5 > 10) True
Cost: O(1)
Source code in shortfx/fxPython/py_logic.py
or_any(*args: bool) -> bool
¶
Returns True if any argument is True; False if all are False.
Description
Python implementation of logical OR operation that evaluates multiple boolean values. Returns True when at least one argument evaluates to True, similar to Excel's OR function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
bool
|
One or more boolean values to evaluate. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if any argument is True, False otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no arguments are provided. |
TypeError
|
If any argument is not a boolean. |
Usage Example
or_any(True, False, False) True or_any(False, False, False) False or_any(5 < 3, 10 > 20, "a" == "a") True
Cost: O(n) where n is the number of arguments
Source code in shortfx/fxPython/py_logic.py
switch_case(expression: Any, *args: Any) -> Any
¶
Evaluates expression against values and returns matching result.
Description
Switch-case implementation that compares an expression against multiple value-result pairs. Returns the result for the first matching value, or a default if no match found. Similar to Excel's SWITCH function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
Any
|
The value to compare. |
required |
*args
|
Any
|
Pairs of (value, result), optionally ending with default. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The result corresponding to the first match, or default value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If insufficient arguments or no match without default. |
Usage Example
switch_case(3, 1, "One", 2, "Two", 3, "Three", "Other") 'Three' switch_case("apple", "orange", "Fruit", "apple", "Favorite", "Unknown") 'Favorite' switch_case(5, 1, "One", 2, "Two", "Not Found") 'Not Found'
Cost: O(n) where n is the number of value-result pairs
Source code in shortfx/fxPython/py_logic.py
xor_all(*args: bool) -> bool
¶
Returns True if an odd number of arguments are True.
Description
Logical exclusive OR operation. Returns True when an odd number of arguments evaluate to True, False otherwise. Equivalent to Excel's XOR function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
bool
|
One or more boolean values to evaluate. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if an odd number of arguments are True, False otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no arguments are provided. |
TypeError
|
If any argument is not a boolean. |
Usage Example
xor_all(True, True) False xor_all(True, False) True xor_all(True, True, True) True
Cost: O(n) where n is the number of arguments