py_convertions¶
shortfx.fxPython.py_convertions
¶
shortfx - fxPython: Python Collection Conversions Module
This module provides utility functions for converting between different Python collection types (lists, tuples, sets, dictionaries) and strings. It includes functions for: - Converting between lists, tuples, and sets - Converting collections to and from strings - Dictionary key/value extractions and transformations - Advanced type conversions with customizable formatting
All functions follow PEP standards with complete documentation including complexity analysis.
Functions¶
convert_collection(data_collection: Union[List[Any], Tuple[Any, ...], Set[Any]], target_type: Type[Union[List, Tuple, Set]]) -> Union[List[Any], Tuple[Any, ...], Set[Any]]
¶
Converts a list, tuple, or set to a specified target collection type.
This function takes an input collection (list, tuple, or set) and converts it to the desired target type. It handles type conversions between these three common collection types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_collection
|
Union[List[Any], Tuple[Any, ...], Set[Any]]
|
The input collection to convert. It can be a list, tuple, or set. |
required |
target_type
|
Type[Union[List, Tuple, Set]]
|
The desired type for the output collection. Must be list, tuple, or set. |
required |
Returns:
| Type | Description |
|---|---|
Union[List[Any], Tuple[Any, ...], Set[Any]]
|
Union[List[Any], Tuple[Any, ...], Set[Any]]: The converted collection
of the specified |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Examples of use
convert_collection([1, 'a', 3.5], tuple) (1, 'a', 3.5) convert_collection((1, 2, 2, 'a'), set) {1, 2, 'a'} # Order not guaranteed convert_collection({'red', 'green', 'blue'}, list) ['green', 'blue', 'red'] # Order not guaranteed convert_collection([1, 2, 3], list) [1, 2, 3]
Cost: The cost of this function is O(n), where n is the number of elements in the input collection, due to the iteration required for conversion by built-in constructors (list(), tuple(), set()).
Source code in shortfx/fxPython/py_convertions.py
dict_values_to_list(data_dict: Dict[Any, Any]) -> List[Any]
¶
Returns the values of a dictionary as a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dict
|
Dict[Any, Any]
|
The input dictionary. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
List[Any]: A list containing all values from the dictionary. |
Example of use
dict_values_to_list({"item": "Laptop", "price": 1200}) ['Laptop', 1200]
Cost: O(n), where n is the number of values in the dictionary.
Source code in shortfx/fxPython/py_convertions.py
dictionary_items_to_list_of_tuples(data_dict: Dict[Any, Any]) -> List[Tuple[Any, Any]]
¶
Returns the key-value pairs (items) of a dictionary as a list of tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dict
|
Dict[Any, Any]
|
The input dictionary. |
required |
Returns:
| Type | Description |
|---|---|
List[Tuple[Any, Any]]
|
List[Tuple[Any, Any]]: A list where each element is a |
Example of use
dictionary_items_to_list_of_tuples({"city": "London", "population": 9000000}) [('city', 'London'), ('population', 9000000)]
Cost: O(n), where n is the number of items in the dictionary.
Source code in shortfx/fxPython/py_convertions.py
dictionary_items_to_set_of_tuples(data_dict: Dict[Any, Any]) -> Set[Tuple[Any, Any]]
¶
Returns the key-value pairs (items) of a dictionary as a set of tuples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dict
|
Dict[Any, Any]
|
The input dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Set[Tuple[Any, Any]]
|
Set[Tuple[Any, Any]]: A set where each element is a unique |
Example of use
dictionary_items_to_set_of_tuples({"id": 101, "status": "active"}) {('id', 101), ('status', 'active')} # Order not guaranteed
Cost: O(n), where n is the number of items in the dictionary.
Source code in shortfx/fxPython/py_convertions.py
dictionary_keys_to_list(data_dict: Dict[Any, Any]) -> List[Any]
¶
Returns the keys of a dictionary as a list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dict
|
Dict[Any, Any]
|
The input dictionary. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
List[Any]: A list containing all keys from the dictionary. |
Example of use
dictionary_keys_to_list({"name": "Alice", "age": 30}) ['name', 'age']
Cost: O(n), where n is the number of keys in the dictionary.
Source code in shortfx/fxPython/py_convertions.py
dictionary_keys_to_set(data_dict: Dict[Any, Any]) -> Set[Any]
¶
Returns the keys of a dictionary as a set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dict
|
Dict[Any, Any]
|
The input dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Set[Any]
|
Set[Any]: A set containing all unique keys from the dictionary. |
Example of use
dict_keys_to_set({"color": "red", "value": 50}) {'color', 'value'} # Order not guaranteed
Cost: O(n), where n is the number of keys in the dictionary.
Source code in shortfx/fxPython/py_convertions.py
dictionary_to_object(dictionary: dict) -> object
¶
Converts a dictionary into a simple object.
This function dynamically creates a class and assigns the dictionary's key-value pairs as attributes to an instance of that class. This is useful for accessing dictionary data using dot notation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dictionary
|
dict
|
The dictionary to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
An object with attributes corresponding to the dictionary's keys. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input is not a dictionary. |
Example
my_dict = {'name': 'Alice', 'age': 30} my_object = dictionary_to_object(my_dict) print(my_object.name) Alice
Cost: O(n), where n is the number of keys in the dictionary.
Source code in shortfx/fxPython/py_convertions.py
dictionary_to_string(input_dict: dict) -> str
¶
Converts a dictionary into a string representation.
This function serializes a dictionary into a string where keys and values are separated by colons and key-value pairs are separated by semicolons. It's useful for simple persistence or transmission when more robust serialization (like JSON or pickle) is overkill.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dict
|
dict
|
The dictionary to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A string representation of the input dictionary. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input_dict is not a dictionary. |
Example of usage
my_dict = {"name": "Alice", "age": "30", "city": "New York"} dictionary_to_string(my_dict) 'name:Alice;age:30;city:New York'
Cost: O(N) where N is the total number of characters in the dictionary's keys and values.
Source code in shortfx/fxPython/py_convertions.py
dictionary_values_to_set(data_dict: Dict[Any, Any]) -> Set[Any]
¶
Returns the values of a dictionary as a set, which inherently removes duplicate values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_dict
|
Dict[Any, Any]
|
The input dictionary. |
required |
Returns:
| Type | Description |
|---|---|
Set[Any]
|
Set[Any]: A set containing all unique values from the dictionary. |
Example of use
dictionary_values_to_set({"a": 1, "b": 2, "c": 1}) {1, 2} # Order not guaranteed
Cost: O(n), where n is the number of values in the dictionary.
Source code in shortfx/fxPython/py_convertions.py
list_of_dicts_to_merged_dict(list_of_dicts: List[Dict[Any, Any]]) -> Dict[Any, Any]
¶
Merges a list of dictionaries into a single dictionary.
If there are duplicate keys across the dictionaries, the value from the last dictionary in the list will take precedence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_of_dicts
|
List[Dict[Any, Any]]
|
A list of dictionaries to be merged. |
required |
Returns:
| Type | Description |
|---|---|
Dict[Any, Any]
|
Dict[Any, Any]: The single merged dictionary. |
Example of use
list_of_dicts_to_merged_dict([{"a": 1, "b": 2}, {"b": 3, "c": 4}, {"d": 5}])
Cost: O(n*m), where n is the number of dictionaries and m is the average number of keys per dictionary.
Source code in shortfx/fxPython/py_convertions.py
list_of_tuples_to_dict(data_list: List[Tuple[Any, Any]]) -> Dict[Any, Any]
¶
Converts a list of tuples (where each tuple is a key-value pair) to a dictionary.
If there are duplicate keys in the list, the value from the last tuple with that key will prevail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_list
|
List[Tuple[Any, Any]]
|
The input list of |
required |
Returns:
| Type | Description |
|---|---|
Dict[Any, Any]
|
Dict[Any, Any]: The converted dictionary. |
Example of use
list_of_tuples_to_dict([("a", 1), ("b", 2), ("a", 3)])
Cost: O(n), where n is the number of tuples in the list.
Source code in shortfx/fxPython/py_convertions.py
list_to_set(input_list: List[Any]) -> Set[Any]
¶
Converts a given list into a set.
This function takes a list as input and returns a new set containing all the unique elements from the list. The order of elements is not preserved in the resulting set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_list
|
List[Any]
|
The list to be converted to a set. |
required |
Returns:
| Type | Description |
|---|---|
Set[Any]
|
A set containing the unique elements from the input list. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input 'input_list' is not a list. |
Example of use
list_to_set([1, 2, 2, 3, 4, 4, 5])
list_to_set(['apple', 'banana', 'apple'])
list_to_set([]) set()
Cost: O(n), where n is the number of elements in the list.
Source code in shortfx/fxPython/py_convertions.py
list_to_string(lst: List[Any], separator: str = ' ', use_quotes: bool = False) -> Optional[str]
¶
Converts a list to its string representation.
This function takes a list and converts its elements into a single string, joined by a specified separator. Optionally, string elements can be wrapped in single quotes. Returns None if the input list is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lst
|
List[Any]
|
The list to be converted. |
required |
separator
|
str
|
The string used to join the elements. Defaults to a single space. |
' '
|
use_quotes
|
bool
|
If True, wraps string elements in single quotes. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The string representation of the list, or None if the input list is empty. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input 'lst' is not a list. |
Example of use
list_to_string(['apple', 'banana', 'orange'], separator=', ', use_quotes=True) "'apple', 'banana', 'orange'"
list_to_string([1, 2, 3], separator='-') "1-2-3"
list_to_string([]) None
Cost: O(n), where n is the number of elements in the list.
Source code in shortfx/fxPython/py_convertions.py
list_to_tuple(input_list: List[Any]) -> Tuple[Any, ...]
¶
Converts a given list into a tuple.
This function takes a list as input and returns a new tuple containing all the elements from the list, maintaining their order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_list
|
List[Any]
|
The list to be converted to a tuple. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Any, ...]
|
A tuple containing the elements from the input list. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input 'input_list' is not a list. |
Example of use
list_to_tuple([1, 2, 3, 4, 5]) (1, 2, 3, 4, 5)
list_to_tuple(['apple', 'banana', 'cherry']) ('apple', 'banana', 'cherry')
list_to_tuple([]) ()
Cost: O(n), where n is the number of elements in the list.
Source code in shortfx/fxPython/py_convertions.py
set_to_list(input_set: Set[Any]) -> List[Any]
¶
Converts a given set into a list.
This function takes a set as input and returns a new list containing all the elements from the set. The order of elements in the resulting list is not guaranteed, as sets are inherently unordered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_set
|
Set[Any]
|
The set to be converted to a list. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
A list containing the elements from the input set. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input 'input_set' is not a set. |
Example of use
set_to_list({1, 2, 3}) # Order may vary [1, 2, 3]
set_to_list({'apple', 'banana'}) # Order may vary ['banana', 'apple']
set_to_list(set()) []
Cost: O(n), where n is the number of elements in the set.
Source code in shortfx/fxPython/py_convertions.py
set_to_string(input_set: Set[Any], separator: str = ' ', use_quotes: bool = False) -> Optional[str]
¶
Converts a given set to its string representation.
This function takes a set and converts its elements into a single string, joined by a specified separator. Optionally, string elements can be wrapped in single quotes. The order of elements in the resulting string is not guaranteed, as sets are inherently unordered. Returns None if the input set is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_set
|
Set[Any]
|
The set to be converted. |
required |
separator
|
str
|
The string used to join the elements. Defaults to a single space. |
' '
|
use_quotes
|
bool
|
If True, wraps string elements in single quotes. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The string representation of the set, or None if the input set is empty. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input 'input_set' is not a set. |
Example of use
set_to_string({'apple', 'banana'}, separator=', ', use_quotes=True) # Order may vary "'apple', 'banana'"
set_to_string({1, 2, 3}, separator='-') # Order may vary "1-2-3"
set_to_string(set()) None
Cost: O(n), where n is the number of elements in the set.
Source code in shortfx/fxPython/py_convertions.py
set_to_tuple(input_set: Set[Any]) -> Tuple[Any, ...]
¶
Converts a given set into a tuple.
This function takes a set as input and returns a new tuple containing all the elements from the set. The order of elements in the resulting tuple is not guaranteed, as sets are inherently unordered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_set
|
Set[Any]
|
The set to be converted to a tuple. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Any, ...]
|
A tuple containing the elements from the input set. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input 'input_set' is not a set. |
Example of use
set_to_tuple({1, 2, 3}) # Order may vary (1, 2, 3)
set_to_tuple({'red', 'green', 'blue'}) # Order may vary ('blue', 'red', 'green')
set_to_tuple(set()) ()
Cost: O(n), where n is the number of elements in the set.
Source code in shortfx/fxPython/py_convertions.py
string_to_dictionary(input_string: str) -> dict
¶
Converts a string representation back into a dictionary.
This function deserializes a string, previously created by dictionary_to_string(),
back into a dictionary. It's the inverse operation, useful for reconstructing
the original dictionary from its string form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_string
|
str
|
The string to convert. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary reconstructed from the input string. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input_string is not a string. |
ValueError
|
If the input_string format is invalid (e.g., missing a colon). |
Example of usage
my_string = 'name:Alice;age:30;city:New York' string_to_dictionary(my_string)
Cost: O(N) where N is the length of the input string.
Source code in shortfx/fxPython/py_convertions.py
string_to_list(input_string: Optional[str], split_by_character: Optional[str] = None) -> Optional[List[str]]
¶
Converts a string into a list of substrings or individual characters.
This function offers two primary modes of operation:
1. If split_by_character is provided, the string will be split into
a list of substrings using that character as a delimiter.
2. If split_by_character is None (default), the string will be converted
into a list of its individual characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_string
|
Optional[str]
|
The string to be converted. Can be None. |
required |
split_by_character
|
Optional[str]
|
An optional string to use as a delimiter for splitting. If None, the string is split into individual characters. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[List[str]]
|
Optional[List[str]]: A list of strings (either substrings or characters).
Returns an empty list if |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Example of use
convert_string_to_list("hello world") ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
convert_string_to_list("apple,banana,orange", split_by_character=",") ['apple', 'banana', 'orange']
convert_string_to_list("one two three", split_by_character=" ") ['one', 'two', 'three']
convert_string_to_list("") []
convert_string_to_list(None) None
convert_string_to_list("word") ['w', 'o', 'r', 'd']
Cost: O(n), where n is the length of the input string.
Source code in shortfx/fxPython/py_convertions.py
tuple_to_list(input_tuple: Tuple[Any, ...]) -> List[Any]
¶
Converts a given tuple into a list.
This function takes a tuple as input and returns a new list containing all the elements from the tuple, maintaining their original order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_tuple
|
Tuple[Any, ...]
|
The tuple to be converted to a list. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
A list containing the elements from the input tuple. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input 'input_tuple' is not a tuple. |
Example of use
tuple_to_list((1, 2, 3, 4, 5)) [1, 2, 3, 4, 5]
tuple_to_list(('red', 'green', 'blue')) ['red', 'green', 'blue']
tuple_to_list(()) []
Cost: O(n), where n is the number of elements in the tuple.
Source code in shortfx/fxPython/py_convertions.py
tuple_to_set(input_tuple: Tuple[Any, ...]) -> Set[Any]
¶
Converts a given tuple into a set.
This function takes a tuple as input and returns a new set containing all the unique elements from the tuple. The order of elements is not preserved in the resulting set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_tuple
|
Tuple[Any, ...]
|
The tuple to be converted to a set. |
required |
Returns:
| Type | Description |
|---|---|
Set[Any]
|
A set containing the unique elements from the input tuple. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input 'input_tuple' is not a tuple. |
Example of use
tuple_to_set((1, 2, 2, 3, 4, 4, 5))
tuple_to_set(('apple', 'banana', 'apple'))
tuple_to_set(()) set()
Cost: O(n), where n is the number of elements in the tuple.
Source code in shortfx/fxPython/py_convertions.py
tuple_to_string(input_tuple: Tuple[Any, ...], separator: str = ' ', use_quotes: bool = False) -> Optional[str]
¶
Converts a given tuple to its string representation.
This function takes a tuple and converts its elements into a single string, joined by a specified separator. Optionally, string elements can be wrapped in single quotes. Returns None if the input tuple is empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_tuple
|
Tuple[Any, ...]
|
The tuple to be converted. |
required |
separator
|
str
|
The string used to join the elements. Defaults to a single space. |
' '
|
use_quotes
|
bool
|
If True, wraps string elements in single quotes. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The string representation of the tuple, or None if the input tuple is empty. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input 'input_tuple' is not a tuple. |
Example of use
tuple_to_string(('apple', 'banana', 'orange'), separator=', ', use_quotes=True) "'apple', 'banana', 'orange'"
tuple_to_string((1, 2, 3), separator='-') "1-2-3"
tuple_to_string(()) None
Cost: O(n), where n is the number of elements in the tuple.