utils
General utility functions.
Removed
tuple2strandlist2string. Usedomdf_python_tools.utils.list2str()instead.Removed
as_textandword_join. Import fromdomdf_python_tools.wordsinstead.Removed
splitLen. Usedomdf_python_tools.iterative.split_len()instead.
Changed in version 2.0.0: chunks(),
permutations(),
split_len(),
Len(), and
double_chain()
moved to domdf_python_tools.iterative().
Changed in version 2.3.0: Removed domdf_python_tools.utils.deprecated().
Use the new deprecation-alias package instead.
Data:
The |
|
Object that provides an ellipsis string |
|
The current major python version. |
Functions:
|
Implementation of |
|
Convert indentation at the start of lines in |
|
Divide a string into two parts, about the given string. |
|
Like |
|
Adds single quotes ( |
|
Returns the head of the given object. |
|
Convert an iterable, such as a list, to a comma separated string. |
|
Returns the magnitude of the given value. |
|
Convert the positional args in |
|
Alias of |
|
Print the |
|
Print the type of an object. |
|
Context manager to redirect stdout and stderr to two |
|
Divide a string into two parts, splitting on the given regular expression. |
|
Replace nonprinting (control) characters in |
|
Print |
|
Convert a comma-separated string of integers into a tuple. |
|
|
|
Trim the precision of the given floating point value. |
|
Returns an ordered list of unique items from |
-
cmp(x, y)[source] Implementation of
cmpfor Python 3.Compare the two objects x and y and return an integer according to the outcome.
The return value is negative if
x < y, zero ifx == yand strictly positive ifx > y.- Return type
-
convert_indents(text, tab_width=4, from_='\t', to=' ')[source] Convert indentation at the start of lines in
textfrom tabs to spaces.
-
divide(string, sep)[source] Divide a string into two parts, about the given string.
New in version 2.7.0.
-
double_repr_string(string)[source] Like
repr(str), but tries to use double quotes instead.New in version 2.5.0.
-
enquote_value(value)[source] Adds single quotes (
') to the given value, suitable for use in a templating system such as Jinja2.Floats,integers,booleans,None, and the strings'True','False'and'None'are returned as-is.
-
etc= ... Type:
_EtceteraObject that provides an ellipsis string
New in version 0.8.0.
-
head(obj, n=10)[source] Returns the head of the given object.
New in version 0.8.0.
- Parameters
See also
textwrap.shorten(), which truncates a string to fit within a given number of characters.itertools.islice(), which returns the firstnelements from an iterator.
-
list2str(the_list, sep=',')[source] Convert an iterable, such as a list, to a comma separated string.
-
magnitude(x)[source] Returns the magnitude of the given value.
For negative numbers the absolute magnitude is returned.
For decimal numbers below
1the magnitude will be negative.
New in version 2.0.0.
-
posargs2kwargs(args, posarg_names, kwargs=None)[source] Convert the positional args in
argsto kwargs, based on the relative order ofargsandposarg_names.Important
Python 3.8’s Positional-Only Parameters (PEP 570) are not supported.
New in version 0.4.10.
- Parameters
args (
Iterable[Any]) – List of positional arguments provided to a function.posarg_names (
Union[Iterable[str],Callable]) – Either a list of positional argument names for the function, or the function object.kwargs (
Optional[Dict[str,Any]]) – Optional mapping of keyword argument names to values. The arguments will be added to this dictionary if provided. Default{}.
- Return type
- Returns
Dictionary mapping argument names to values.
Changed in version 2.8.0: The “self” argument for bound methods is ignored. For unbound methods (which are just functions) the behaviour is unchanged.
-
printe(*values, sep=' ', end='\n') Alias of
stderr_writer()
-
printr(obj, *values, sep=' ', end='\n', file=None, flush=False)[source] Print the
repr()of an object.If no objects are given,
printr()will just writeend.- Parameters
obj (
Any)*values (
object) – Additional values to print. These are printed verbatim.sep (
Optional[str]) – The separator between values. Default'␣'.end (
Optional[str]) – The final value to print. Setting to''will leave the insertion point at the end of the printed text. Default'\n'.file (
Optional[IO]) – The file to write to. If not present orNone,sys.stdoutwill be used.flush (
bool) – IfTruethe stream is forcibly flushed after printing. DefaultFalse.
-
printt(obj, *values, sep=' ', end='\n', file=None, flush=False)[source] Print the type of an object.
If no objects are given,
printt()will just writeend.- Parameters
obj (
Any)*values (
object) – Additional values to print. These are printed verbatim.sep (
Optional[str]) – The separator between values. Default'␣'.end (
Optional[str]) – The final value to print. Setting to''will leave the insertion point at the end of the printed text. Default'\n'.file (
Optional[IO]) – The file to write to. If not present orNone,sys.stdoutwill be used.flush (
bool) – IfTruethe stream is forcibly flushed after printing. DefaultFalse.
-
redirect_output(combine=False)[source] Context manager to redirect stdout and stderr to two
io.StringIOobjects.These are assigned (as a
tuple) to the target theasexpression.Example:
with redirect_output() as (stdout, stderr): ...
New in version 2.6.0.
-
redivide(string, pat)[source] Divide a string into two parts, splitting on the given regular expression.
New in version 2.7.0.
-
replace_nonprinting(string, exclude=None)[source] Replace nonprinting (control) characters in
stringwith^andM-notation.New in version 3.3.0.
- Parameters
- Return type
See also
C0 and C1 control codes on Wikipedia
-
stderr_writer(*values, sep=' ', end='\n')[source] Print
*valuestosys.stderr, separated bysepand followed byend.sys.stdoutis flushed before printing, andsys.stderris flushed afterwards.If no objects are given,
stderr_writer()will just writeend.- Parameters
Changed in version 3.0.0: The only permitted keyword arguments are
sepandend. Previous versions allowed other keywords arguments supported byprint()but they had no effect.
-
str2tuple(input_string, sep=',')[source] Convert a comma-separated string of integers into a tuple.
Important
The input string must represent a comma-separated series of integers.
-
strtobool(val)[source] Convert a string representation of truth to
TrueorFalse.If val is an integer then its boolean representation is returned. If val is a boolean it is returned as-is.
Truevalues are'y','yes','t','true','on','1', and1.Falsevalues are'n','no','f','false','off','0', and0.- Raises
ValueErrorifvalis anything else.- Return type
-
trim_precision(value, precision=4)[source] Trim the precision of the given floating point value.
For example, if you have the value 170.10000000000002 but really only care about it being
≈ 179.1:>>> trim_precision(170.10000000000002, 2) 170.1 >>> type(trim_precision(170.10000000000002, 2)) <class 'float'>
New in version 2.0.0.
-
unique_sorted(elements, *, key=None, reverse=False)[source] Returns an ordered list of unique items from
elements.New in version 3.0.0.
- Parameters
elements (
Iterable)key (
Optional[Callable]) – A function of one argument used to extract a comparison key from each item when sorting. For example,key=str.lower. The default value isNone, which will compare the elements directly. DefaultNone.reverse (
bool) – IfTruethe list elements are sorted as if each comparison were reversed. DefaultFalse.
- Return type
- Overloads
unique_sorted(elements:Iterable[~SupportsLessThanT], key:None= …, reverse = … ) ->List[~SupportsLessThanT]unique_sorted(elements:Iterable[~_T], key:Callable[[~_T],SupportsLessThan], reverse = … ) ->List[~_T]