py_tools¶
shortfx.fxPython.py_tools
¶
shortfx - fxPython: Python Tools Module
This module provides high-level utility functions for Python programming patterns. It includes functions for: - Dictionary creation from key-value pairs - Higher-order function execution (function_call, loop_for, loop_while) - Switch-case pattern implementation - Collection rotation and shifting operations - Dynamic expression evaluation on iterables
All functions follow PEP standards with complete documentation including complexity analysis.
Functions¶
apply_expression(expression: str, iterable: Iterable[Any]) -> list[Any]
¶
Applies a string expression to every item in an iterable and returns a list.
This function dynamically creates a lambda function from a string expression, providing a concise way to perform transformations on a collection of data without manually writing a lambda function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expression
|
str
|
A string representing a Python expression (e.g., 'x * 2'). |
required |
iterable
|
Iterable[Any]
|
A collection of items to which the expression will be applied. |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
A new list containing the results of applying the expression to each item. |
Raises:
| Type | Description |
|---|---|
SyntaxError
|
If the expression string is not a valid Python expression. |
Example of use
my_numbers = [1, 2, 3, 4] results = apply_expression('x * x', my_numbers) print(results) [1, 4, 9, 16]
my_strings = ['hello', 'world'] capitalized_strings = apply_expression('x.capitalize()', my_strings) print(capitalized_strings) ['Hello', 'World']
Cost: O(n), where n is the number of items in the iterable.
Source code in shortfx/fxPython/py_tools.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
create_key_value_dictionary(p_key_columns, p_values)
¶
Creates a dictionary by mapping key column names to their corresponding values.
This function takes a string or list of key column names and a value or tuple of values, then combines them into a dictionary. It handles cases where p_key_columns is a comma-separated string or a list, ensuring that column names are properly cleaned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p_key_columns
|
str or list
|
A comma-separated string of column names (e.g., "id,name") or a list of column names (e.g., ["id", "name"]). |
required |
p_values
|
any or tuple
|
A single value or a tuple of values to be associated with the key columns. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A dictionary where keys are the column names and values are the corresponding input values. Returns None if inputs are empty or invalid. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of key columns does not match the number of values. |
Example of use
create_key_value_dictionary("id,name", (1, "Alice")) {'id': 1, 'name': 'Alice'} create_key_value_dictionary(["product_id"], "P123")
Cost: O(n), where n is the number of key columns.
Source code in shortfx/fxPython/py_tools.py
function_call(func: callable, *args, **kwargs) -> Any
¶
Higher-order function that executes a callable with optional arguments.
This function provides a simple abstraction for executing any callable object, allowing you to pass both positional and keyword arguments. It's useful for building functional programming patterns and creating flexible execution flows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
The function to execute. |
required |
*args
|
Positional arguments to pass to the function. |
()
|
|
**kwargs
|
Keyword arguments to pass to the function. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The return value of the executed function. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If func is not callable. |
Example of use
def greet(name, greeting="Hello"): ... return f"{greeting}, {name}!" function_call(greet, "Alice") 'Hello, Alice!' function_call(greet, "Bob", greeting="Hi") 'Hi, Bob!'
Cost: O(1) plus the cost of executing the provided function.
Source code in shortfx/fxPython/py_tools.py
loop_for(iterable, func)
¶
Executes a function for each item in an iterable, simulating a for loop.
This function abstracts the common pattern of iterating over a sequence and applying a specific operation to each element. It promotes code reusability by separating the iteration logic from the operation itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
An iterable object (e.g., a list, tuple, string) to loop over. |
required | |
func
|
A callable function that takes a single argument (the current item from the iterable) and performs an action. |
required |
Example of use
my_list = [1, 2, 3] def print_item(item): ... print(f"Current item: {item}") loop_for(my_list, print_item) Current item: 1 Current item: 2 Current item: 3
Cost: O(n), where n is the number of items in the iterable.
Source code in shortfx/fxPython/py_tools.py
loop_while(condition, func, *args, **kwargs)
¶
Executes a function repeatedly as long as a specified condition is true.
This function abstracts a while loop, allowing you to define the loop's continuation condition and the action to perform in each iteration as separate, reusable components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition
|
A callable function that takes a variable number of positional and keyword arguments and returns a boolean value. The loop continues as long as this function returns True. |
required | |
func
|
A callable function that is executed in each iteration of the loop. It should also accept the same arguments. |
required | |
*args
|
Positional arguments to pass to both the condition and func functions. |
()
|
|
**kwargs
|
Keyword arguments to pass to both the condition and func functions. |
{}
|
Example of use
count = [0] def is_less_than_five(c): ... return c[0] < 5 def increment_count(c): ... print(f"Current count: {c[0]}") ... c[0] += 1 loop_while(is_less_than_five, increment_count, count) Current count: 0 Current count: 1 Current count: 2 Current count: 3 Current count: 4
Cost: O(k), where k is the number of iterations until condition becomes False.
Source code in shortfx/fxPython/py_tools.py
pipe(value: Any, *functions: Callable) -> Any
¶
Threads a value through a sequence of functions.
Applies each function to the result of the previous one, left to right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The initial value. |
required |
*functions
|
Callable
|
One or more callables to apply in order. |
()
|
Returns:
| Type | Description |
|---|---|
Any
|
The final result after all functions have been applied. |
Example
pipe(5, lambda x: x * 2, lambda x: x + 3, str) '13' pipe(" hello ", str.strip, str.upper) 'HELLO'
Cost: O(k) where k is the number of functions.
Source code in shortfx/fxPython/py_tools.py
retry(func: Callable, max_attempts: int = 3, delay: float = 1.0) -> Any
¶
Retries a function on failure up to a maximum number of attempts.
Waits delay seconds between attempts using time.sleep.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
A zero-argument callable to execute. |
required |
max_attempts
|
int
|
Maximum number of tries (default 3). |
3
|
delay
|
float
|
Seconds to wait between retries (default 1.0). |
1.0
|
Returns:
| Type | Description |
|---|---|
Any
|
The return value of |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If all attempts fail, wrapping the last exception. |
Example
counter = {"n": 0} def flaky(): ... counter["n"] += 1 ... if counter["n"] < 3: ... raise ValueError("not yet") ... return "ok" retry(flaky, max_attempts=5, delay=0) 'ok'
Cost: O(k) where k is max_attempts.
Source code in shortfx/fxPython/py_tools.py
rotate(iterable, steps=1)
¶
Rotates the elements of an iterable.
This function rotates the elements of an iterable by a specified number of steps.
A positive steps value rotates elements to the right, and a negative value
rotates them to the left. The function returns a new list and does not modify
the original iterable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
The collection (e.g., list, tuple) to be rotated. |
required | |
steps
|
The number of positions to rotate. A positive integer shifts elements to the right, and a negative integer shifts to the left. |
1
|
Returns:
| Type | Description |
|---|---|
|
A new list with the elements rotated. |
Example of use
my_list = [1, 2, 3, 4]
Rotate right by one step (default)¶
rotate_right = rotate(my_list) print(f"Rotate right: {rotate_right}") Rotate right: [4, 1, 2, 3]
Rotate left by two steps¶
rotate_left = rotate(my_list, steps=-2) print(f"Rotate left: {rotate_left}") Rotate left: [3, 4, 1, 2]
Rotate right by three steps¶
rotate_right_three = rotate(my_list, steps=3) print(f"Rotate right three: {rotate_right_three}") Rotate right three: [2, 3, 4, 1]
Cost: O(n), where n is the number of elements in the iterable.
Source code in shortfx/fxPython/py_tools.py
shift(iterable: Iterable[Any], direction: str = None, new_elements: Iterable[Any] = None) -> list[Any]
¶
Shifts an iterable, adding or removing elements.
This function provides a flexible way to modify an iterable by shifting its elements and optionally adding multiple new elements from another iterable. It does not modify the original iterable, but returns a new one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
Iterable[Any]
|
The collection (e.g., list, tuple) to be shifted. |
required |
direction
|
str
|
A string indicating the shift direction ('left' or 'right'). If None, it removes one element from the left. |
None
|
new_elements
|
Iterable[Any]
|
An iterable of elements to add to the collection. If None, the function removes one element from the specified direction. |
None
|
Returns:
| Type | Description |
|---|---|
list[Any]
|
A new list representing the shifted collection. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an invalid direction is provided. |
Example of use
my_list = [1, 2, 3, 4]
Remove the first element (default behavior)¶
removed_first = shift(my_list) print(f"Removed first: {removed_first}") Removed first: [2, 3, 4]
Remove the last element¶
removed_last = shift(my_list, direction='right') print(f"Removed last: {removed_last}") Removed last: [1, 2, 3]
Shift left, adding multiple elements from a tuple¶
elements_to_add = (5, 6, 7) shifted_left = shift(my_list, direction='left', new_elements=elements_to_add) print(f"Shift left and add: {shifted_left}") Shift left and add: [2, 3, 4, 5, 6, 7]
Shift right, adding multiple elements from a list¶
shifted_right = shift(my_list, direction='right', new_elements=[0, -1]) print(f"Shift right and add: {shifted_right}") Shift right and add: [0, -1, 1, 2, 3]
Cost: O(n + m), where n is the size of the iterable and m is the number of new elements.
Source code in shortfx/fxPython/py_tools.py
switch_case(value, *cases, default=None)
¶
Acts like a generic switch-case statement.
Delegates to :func:~shortfx.fxPython.py_logic.switch_case.
Accepts an explicit default keyword instead of the positional
odd-argument convention used by the core implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
The value to be evaluated. |
required | |
*cases
|
A sequence of cases where each pair consists of a key and a value. Example: key1, value1, key2, value2, ... |
()
|
|
default
|
The value to return if no match is found. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
|
The value associated with the matching key, or the default value if no match. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of items in *cases is not an even number. |
Usage Example
switch_case(2, 1, "January", 2, "February", 3, "March", default="Unknown Month") 'February'
Cost: O(n) where n is the number of case pairs