symforce.python_util module#
General python utilities.
- remove_if_exists(path)[source]#
Delete a file or directory if it exists.
- Parameters:
path (Path) –
- Return type:
None
- async execute_subprocess(cmd, stdin_data=None, log_stdout=True, log_stdout_to_error_on_error=True, **kwargs)[source]#
Execute subprocess and log command as well as stdout/stderr.
- Parameters:
- Raises:
subprocess.CalledProcessError – If the return code is nonzero
- Return type:
- snakecase_to_camelcase(s)[source]#
Convert snake_case -> CamelCase
Double underscores are escaped, e.g. one__two becomes One_Two
- str_replace_all(s, replacements)[source]#
Call
str.replace(old, new)
for every pair (old, new) in replacements
- str_removeprefix(s, prefix)[source]#
Backport of
str.removeprefix
, from Python3.9https://docs.python.org/3/library/stdtypes.html#str.removeprefix
If the string starts with the prefix string and that prefix is not empty, return
string[len(prefix):]
. Otherwise, return a copy of the original string.
- str_removesuffix(s, suffix)[source]#
Backport of
str.removesuffix
, from Python3.9https://docs.python.org/3/library/stdtypes.html#str.removesuffix
If the string ends with the suffix string and that suffix is not empty, return
string[:-len(suffix)]
. Otherwise, return a copy of the original string.
- dots_and_brackets_to_underscores(s)[source]#
Converts all
.
and[]
in the given string to underscores such that the resulting string is a valid/readable variable name.
- id_generator(size=6, chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')[source]#
Generate a random string within a character set - for example
"6U1S75"
.This is not cryptographically secure.
- getattr_recursive(obj, attrs)[source]#
Recursively calls getattr on obj with the attributes in attrs and returns the output.
If attr is empty, returns obj.
Example:
get_attr_recursive(obj, ["A", "B", "C"])
returns the same thing as:
obj.A.B.C
- exception InvalidKeyError[source]#
Bases:
ValueError
- exception InvalidPythonIdentifierError[source]#
Bases:
InvalidKeyError
- base_and_indices(indexed_array)[source]#
Decomposes indexed_array into
(base, indices)
in the sense that,"arr[1][2]" -> ("arr", [1, 2])
.base
is the initial substring ofindexed_array
that does not contain either[
or]
;indices
is the list of integer indices indexing into the array denoted bybase
.indices
will be the empty list ifindexed_array
has no indices.- Raises:
InvalidKeyError – if
indexed_array
is not matched by the regular expressionr"[\[\]]*(\[[0-9]+\])*"
, i.e., is not a string with no square brackets, followed by 0 or more integers wrapped in square brackets.- Parameters:
indexed_array (str) –
- Return type:
Example
>>> assert ("arr", []) == base_and_indices("arr") >>> assert ("arr", [1, 2, 3]) == base_and_indices("arr[1][2][3]") >>> try: >>> base_and_indices("arr[1].bad[2]") >>> assert False >>> except ValueError: >>> pass >>> except: >>> assert False
- plural(singular, count, plural=None)[source]#
Return the singular or plural form of a word based on count
Adds an s to singular by default for the plural form, or uses
plural
if provided
- get_func_from_maybe_bound_function(func)[source]#
Get the original function, from a function possibly bound by
functools.partial
- class AttrDict(**kwargs)[source]#
Bases:
dict
A simple attr-dict, i.e. a dictionary whose keys are also accessible directly as fields
Based on http://stackoverflow.com/a/14620633/53997
- Parameters:
kwargs (Any) –