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:
  • stdin_data (bytes) – Data to pass to stdin

  • log_stdout (bool) – Write process stdout to the logger?

  • log_stdout_to_error_on_error (bool) – Write output to logger.error if the command fails?

  • cmd (str | Sequence[str]) –

  • kwargs (Any) –

Raises:

subprocess.CalledProcessError – If the return code is nonzero

Return type:

str

camelcase_to_snakecase(s)[source]#

Convert CamelCase -> snake_case.

Parameters:

s (str) –

Return type:

str

snakecase_to_camelcase(s)[source]#

Convert snake_case -> CamelCase

Double underscores are escaped, e.g. one__two becomes One_Two

Parameters:

s (str) –

Return type:

str

camelcase_to_screaming_snakecase(s)[source]#

Convert CamelCase -> SCREAMING_SNAKE_CASE

Parameters:

s (str) –

Return type:

str

str_replace_all(s, replacements)[source]#

Call str.replace(old, new) for every pair (old, new) in replacements

Parameters:
Return type:

str

str_removeprefix(s, prefix)[source]#

Backport of str.removeprefix, from Python3.9

https://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.

Parameters:
  • s (str) –

  • prefix (str) –

Return type:

str

str_removesuffix(s, suffix)[source]#

Backport of str.removesuffix, from Python3.9

https://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.

Parameters:
  • s (str) –

  • suffix (str) –

Return type:

str

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.

Parameters:

s (str) –

Return type:

str

files_in_dir(dirname, relative=False)[source]#

Return a list of files in the given directory.

Parameters:
Return type:

Iterator[str]

id_generator(size=6, chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')[source]#

Generate a random string within a character set - for example "6U1S75".

This is not cryptographically secure.

Parameters:
  • size (int) –

  • chars (str) –

Return type:

str

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

Parameters:
Return type:

Any

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 of indexed_array that does not contain either [ or ]; indices is the list of integer indices indexing into the array denoted by base.

indices will be the empty list if indexed_array has no indices.

Raises:

InvalidKeyError – if indexed_array is not matched by the regular expression r"[\[\]]*(\[[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:

Tuple[str, List[int]]

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

Parameters:
  • singular (str) –

  • count (int) –

  • plural (str | None) –

Return type:

str

get_func_from_maybe_bound_function(func)[source]#

Get the original function, from a function possibly bound by functools.partial

Parameters:

func (Callable) –

Return type:

Callable

get_class_for_method(func)[source]#

Get the class from an instance method func

See https://stackoverflow.com/a/25959545

Parameters:

func (Callable) –

Return type:

Type

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) –