extra_ds_tools.format
Module Contents
Functions
|
Returns information about the arguments of a function and its inputted values. |
|
Returns a dictionairy with comprehensive information about a single argument. |
|
Return a string representation of a value, truncated when it's over the str_limit. This is useful for when you don't want to print a long list but would like to see some output. |
|
Returns the class of the instance as a string representation. |
|
Makes an empty string or None printable. |
- extra_ds_tools.format.args_and_kwargs_repr(func: Callable, *args, **kwargs) List[dict]
Returns information about the arguments of a function and its inputted values.
- Parameters
func (Callable) – A function.
*args (Tuple[Any]) – Positional arguments for the function.
**kwargs (dict[Any]) – Named key word arguments for the function.
- Returns
Information about the arguments of a function and its inputted values.
- Return type
List[dict]
Examples
>>> def multiply_two_texts(text1: str, text2: str, n: int = 1): >>> return f"{text1} {text2}" * int >>> >>> args_and_kwargs_repr(multiply_text, *('arrow','knee'), **{'n': 2}) [{'param': 'text', 'type_hint': 'str', 'default_value': '', 'arg_type': 'str', 'arg_value': 'arrow', 'arg_len': 5}, {'param': 'n', 'type_hint': 'int', 'default_value': 1, 'arg_type': 'str', 'arg_value': 'knee', 'arg_len': 4}, {'param': "kwarg['p']", 'arg_type': 'int', 'arg_value': '2', 'arg_len': ''}]
>>> def print_every_arg(text: str, *args): >>> print(text) >>> for arg in args: >>> print(arg) >>> >>> args_and_kwargs_repr(print_every_arg, *('perfect', 2, ['combine', 'with', 'decorator'])) [{'param': 'text', 'type_hint': 'str', 'default_value': '', 'arg_type': 'str', 'arg_value': 'perfect', 'arg_len': 7}, {'param': 'args[1]', 'arg_type': 'int', 'arg_value': '2', 'arg_len': ''}, {'param': 'args[2]', 'arg_type': 'list', 'arg_value': "['combine' .. ecorator']", 'arg_len': 3}]
- extra_ds_tools.format.arg_info(key: str, arg: Any, sign: inspect.signature, fullargspec: inspect.FullArgSpec) dict
Returns a dictionairy with comprehensive information about a single argument.
- Parameters
key (str) – The name of the keyword argument of a function.
arg (Any) – The argument value passed into the function.
sign (inspect.signature) – The signature of the function from the inspect module.
fullargspec (inspect.FullArgSpec) – The FullArgSpec class of the function from the inspect module.
- Returns
A comprehensive dict with information about the argument.
- Return type
dict
Examples
>>> from inspect import getfullargspec, signature >>> >>> def multiply_text(text: str, n: int = 1): >>> return text * n >>> >>> sign = signature(multiply_text) >>> fullargspec = getfullargspec(multiply_text) >>> >>> arg_info(key='text', arg='hello', sign=sign, fullargspec=fullargspec) {'param': 'text', 'type_hint': 'str', 'default_value': '', 'arg_type': 'str', 'arg_value': 'hello', 'arg_len': 5}
>>> arg_info(key='n', arg=[1,2], sign=sign, fullargspec=fullargspec) {'param': 'n', 'type_hint': 'int', 'default_value': 1, 'arg_type': 'list', 'arg_value': '[1, 2]', 'arg_len': 2}
See also
Uses:
class_as_str_repr()
truncated_value()
make_empty_value_printable()
- extra_ds_tools.format.truncated_value(arg: Any, str_limit: int = 20) str
Return a string representation of a value, truncated when it’s over the str_limit. This is useful for when you don’t want to print a long list but would like to see some output.
- Parameters
arg (Any) – Any argument.
str_limit (int, optional) – The limit of the summary, by default 20
- Returns
Arg as a string, truncated so that if it’s string representation’s length > str_limit it’s string representation gets truncated.
- Return type
str
- Raises
ValueError – If str_limit <= 0
Examples
>>> truncated_value(42) 42
Not that interesting. Until we get to strings, lists or other objects with a large length.
>>> truncated_value(list(range(100)), str_limit = 20) [0, 1, 2, .. 7, 98, 99]
- extra_ds_tools.format.class_as_str_repr(instance: Any) str
Returns the class of the instance as a string representation.
- Parameters
instance (Any) – Any instance.
- Returns
The string representation of a class.
- Return type
str
Examples
>>> class_as_str_repr([3,4]) 'list'
>>> import numpy as np >>> array = np.array([1,2]) >>> class_as_str_repr(array) 'numpy.ndarray'
>>> from typing import Union, List >>> class_as_str_repr(Union[List[str], None]) 'Union[List[str], NoneType]'
- extra_ds_tools.format.make_empty_value_printable(value: Any) Any
Makes an empty string or None printable.
- Parameters
value (Any) – Any value.
- Returns
Same as value unless value was an empty string or None.
- Return type
Any
Examples
Print an empty string will print nothing:
>>> print('')
Printing the output of make_empty_value_printable shows an empty string.
>>> print(make_empty_value_printable('')) ''
Printing None will print nothing:
>>> print(None)
Printing the output of make_empty_value_printable shows None.
>>> print(make_empty_value_printable(None)) None