ytilities
Copyright (c) 2023 Anthony Mugendi
This software is released under the MIT License. https://opensource.org/licenses/MIT
Submodules
Package Contents
Functions
|
Ensure that value is of a valid type . |
|
Return True if the value is a class . |
|
Returns True if the given class is initialize . |
|
List all (non default) properties of a class . |
|
Get property values of a class/object . |
|
Convert a value into a list. |
|
Flatten a nested list/set/tuple/numpy array into a single iterable . |
|
Flattens a list/set/tuple/numpy array to a 1D list/set/tuple/numpy array . |
|
Chunk an array into n - sized chunks . |
|
Return True if the value is a class . |
|
Returns True if the given class is initialize . |
|
Convert a value into a list. |
|
Returns a subset of items from a class, dict, list, tuple or set based on their keys/props. |
|
Return True if all unique values in l1 are also in l2. |
|
Return a human - readable representation of a number . |
|
Ensure that value is of a valid type . |
|
Truncate a path to a given length . |
|
Log the method that made the call |
|
Convert a value into a list. |
|
Return the type name of a Python object. This returns full names for types so that str=>"string" and so on... |
|
Ensure that value is of a valid type . |
Attributes
- ytilities.ensure_type(val: Any, _types, val_name: str = None) None
Ensure that value is of a valid type .
- Parameters:
val (Any) – value you want to ensure type for
_types (type, tuple(type)) – the data types you want to validate against. E.g (str,int,float)
val_name (str, optional) – the actual name of the variable you are validating. Example `ensure_type(var_1, (int, float), ‘var_1’). Defaults to None.
- Raises:
TypeError – if value does not match the validation types entered
- ytilities.is_class(cls) bool
Return True if the value is a class .
- Returns:
True if class
- Return type:
bool
- ytilities.is_initialized(cls) bool
Returns True if the given class is initialize .
- Raises:
TypeError – if cls argument is not a class
- Returns:
True if class has been initialized
- Return type:
bool
- ytilities.get_props(cls, filter_callable: bool = False, with_meta: bool = False) list | dict
List all (non default) properties of a class .
- Parameters:
cls (Any) – a valid class
filter_callable (bool, optional) – if True, only callable properties of the class are returned. Defaults to False.
with_meta (bool, optional) – if True, a dict of properties and metadata about them is returned. Defaults to False.
- Returns:
list or dict of properties of the class
- Return type:
list|dict
- ytilities.get_prop_values(obj: Any, decode_bytes: bool = True, run_callable: bool = False, skip_callable: bool = True) dict
Get property values of a class/object .
- Parameters:
obj (Any) – the object to get properties from
decode_bytes (bool, optional) – try and decode any byte values to utf-8. Defaults to True.
run_callable (bool, optional) – try and run callable properties. Defaults to False.
skip_callable (bool, optional) – determines if to include callable values in response. Defaults to True.
- Returns:
a dict of {property:value} pairs
- Return type:
dict
- ytilities.listify(val: Any) list
Convert a value into a list.
- Parameters:
v (Any) – your value
- Returns:
list either containing your input value or value if it was already a list
- Return type:
list
- ytilities.__flatten(val: set | list | tuple | numpy.ndarray) iter
Flatten a nested list/set/tuple/numpy array into a single iterable .
- Parameters:
val (set, list, tuple, np.ndarray) –
- Raises:
TypeError – if val is one of list/set/tuple/numpy array
- Returns:
iterabable
- Return type:
iter
- Yields:
Iterator[iter] – iterabable
- ytilities.flatten(val: set | list | tuple | numpy.ndarray) set | list | tuple | numpy.ndarray
Flattens a list/set/tuple/numpy array to a 1D list/set/tuple/numpy array .
- Parameters:
val (set, list, tuple, np.ndarray) – your value
- Raises:
TypeError – if val is one of list/set/tuple/numpy array
- Returns:
[description]
- Return type:
Union[set, list, tuple, np.ndarray]
- ytilities.chunk_arr(arr: list, n: int = 4) list
Chunk an array into n - sized chunks .
- Parameters:
arr (list) – list/array to chunk
n (int, optional) – length of each chunk. Defaults to 4.
- Returns:
chunked array
- Return type:
list
- ytilities.is_class(cls) bool
Return True if the value is a class .
- Returns:
True if class
- Return type:
bool
- ytilities.is_initialized(cls) bool
Returns True if the given class is initialize .
- Raises:
TypeError – if cls argument is not a class
- Returns:
True if class has been initialized
- Return type:
bool
- ytilities.listify(val: Any) list
Convert a value into a list.
- Parameters:
v (Any) – your value
- Returns:
list either containing your input value or value if it was already a list
- Return type:
list
- ytilities.pick(obj, keys: list, sort_keys: bool = True) dict | list | tuple | set
Returns a subset of items from a class, dict, list, tuple or set based on their keys/props.
- Parameters:
obj (class, dict, list, tuple, set) – value to pick items from
keys (list) – keys/props to pick
sort_keys (bool, optional) – sort picked keys/props before returning?. Defaults to True.
- Raises:
TypeError – if obj is not one of class, dict, list, tuple or set
- Returns:
items picked from class, dict, list, tuple or set
- Return type:
(dict, list, tuple, set)
- ytilities.unique_eq(l1: list | tuple | set, l2: list | tuple | set) bool
Return True if all unique values in l1 are also in l2.
- Raises:
TypeError – if values entered are not list, tuple or sets
- Returns:
True if all unique values in l1 match unique values in l2
- Return type:
[type]
- ytilities.human_format(value) str
Return a human - readable representation of a number .
- Parameters:
value ([int,float]) – Value you wish to format
- Raises:
Exception – TypeError
- Returns:
A string representing the number in easy human readable form e.g “2.345K”
- Return type:
str
- ytilities.ensure_type(val: Any, _types, val_name: str = None) None
Ensure that value is of a valid type .
- Parameters:
val (Any) – value you want to ensure type for
_types (type, tuple(type)) – the data types you want to validate against. E.g (str,int,float)
val_name (str, optional) – the actual name of the variable you are validating. Example `ensure_type(var_1, (int, float), ‘var_1’). Defaults to None.
- Raises:
TypeError – if value does not match the validation types entered
- ytilities.path_truncate(path_str: str, max_len: int = 50) str
Truncate a path to a given length .
- Parameters:
path_str (str) – Path to truncate
max_len (int, optional) – Maximum length (chars) to truncate to. Defaults to 50.
- Returns:
Truncated path
- Return type:
str
- ytilities.trace_caller(return_trace: bool = False) None | tuple
Log the method that made the call
- Parameters:
return_trace (bool, optional) – if true, the trace details are returned as a tuple. Defaults to False.
- Returns:
prints the trace details or outputs the same as a tuple
- Return type:
(None, tuple)
- ytilities.importing_script()
- ytilities.listify(val: Any) list
Convert a value into a list.
- Parameters:
v (Any) – your value
- Returns:
list either containing your input value or value if it was already a list
- Return type:
list
- ytilities.types_obj
- ytilities.get_type_name(_type) str
Return the type name of a Python object. This returns full names for types so that str=>”string” and so on…
- Parameters:
_type (type) – the type you want to get the name of. Example get_type_name(str) -> “string”
- Returns:
full name of the type
- Return type:
str
- ytilities.ensure_type(val: Any, _types, val_name: str = None) None
Ensure that value is of a valid type .
- Parameters:
val (Any) – value you want to ensure type for
_types (type, tuple(type)) – the data types you want to validate against. E.g (str,int,float)
val_name (str, optional) – the actual name of the variable you are validating. Example `ensure_type(var_1, (int, float), ‘var_1’). Defaults to None.
- Raises:
TypeError – if value does not match the validation types entered