rez.utils.formatting#

Utilities related to formatting output or translating input.

rez.utils.formatting.is_valid_package_name(name, raise_error=False)#

Test the validity of a package name string.

Parameters:
  • name (str) – Name to test.

  • raise_error (bool) – If True, raise an exception on failure

Returns:

bool.

class rez.utils.formatting.PackageRequest#

A package request parser.

Valid requests include:

  • Any standard request, eg ‘foo-1.2.3’, ‘!foo-1’, etc

  • “Ephemeral” request, eg ‘.foo-1.2.3’

Example

>>> pr = PackageRequest("foo-1.3+")
>>> print(pr.name, pr.range)
foo 1.3+
__init__(s)#
class rez.utils.formatting.StringFormatType#

Behaviour of key expansion when using ObjectStringFormatter.

class rez.utils.formatting.ObjectStringFormatter#

String formatter for objects.

This formatter will expand any reference to an object’s attributes.

__init__(instance, pretty=False, expand=<StringFormatType.error: 1>)#

Create a formatter.

Parameters:
  • instance – The object to format with.

  • pretty – If True, references to non-string attributes such as lists are converted to basic form, with characters such as brackets and parentheses removed.

  • expandStringFormatType.

class rez.utils.formatting.StringFormatMixin#

Turn any object into a string formatter.

An object inheriting this mixin will have a format function added, that is able to format using attributes of the object.

format(s, pretty=None, expand=None)#

Format a string.

Parameters:
  • s (str) – String to format, eg “hello {name}”

  • pretty (bool) – If True, references to non-string attributes such as lists are converted to basic form, with characters such as brackets and parenthesis removed. If None, defaults to the object’s ‘format_pretty’ attribute.

  • expand (StringFormatType) – Expansion mode. If None, will default to the object’s ‘format_expand’ attribute.

Returns:

The formatting string.

rez.utils.formatting.expand_abbreviations(txt, fields)#

Expand abbreviations in a format string.

If an abbreviation does not match a field, or matches multiple fields, it is left unchanged.

Example

>>> fields = ("hey", "there", "dude")
>>> expand_abbreviations("hello {d}", fields)
'hello dude'
Parameters:
  • txt (str) – Format string.

  • fields (list of str) – Fields to expand to.

Returns:

Expanded string.

rez.utils.formatting.expandvars(text, environ=None)#

Expand shell variables of form $var and ${var}.

Unknown variables are left unchanged.

Parameters:
  • text (str) – String to expand.

  • environ (dict) – Environ dict to use for expansions, defaults to os.environ.

Returns:

The expanded string.

rez.utils.formatting.indent(txt)#

Indent the given text by 4 spaces.

rez.utils.formatting.dict_to_attributes_code(dict_)#

Given a nested dict, generate a python code equivalent.

Example

>>> d = {'foo': 'bah', 'colors': {'red': 1, 'blue': 2}}
>>> print(dict_to_attributes_code(d))
foo = 'bah'
colors.red = 1
colors.blue = 2
Returns:

str.

rez.utils.formatting.columnise(rows, padding=2)#

Print rows of entries in aligned columns.

rez.utils.formatting.print_colored_columns(printer, rows, padding=2)#

Like columnise, but with colored rows.

Parameters:

printer (colorize.Printer) – Printer object.

Note

The last entry in each row is the row color, or None for no coloring.

rez.utils.formatting.readable_time_duration(secs)#

Convert number of seconds into human readable form, eg ‘3.2 hours’.

rez.utils.formatting.readable_memory_size(bytes_)#

Convert number of bytes into human readable form, eg ‘1.2 Kb’.

rez.utils.formatting.get_epoch_time_from_str(s)#

Convert a string into epoch time. Examples of valid strings:

1418350671 # already epoch time -12s # 12 seconds ago -5.4m # 5.4 minutes ago

rez.utils.formatting.positional_number_string(n)#

Print the position string equivalent of a positive integer. Examples:

0: zeroeth 1: first 2: second 14: 14th 21: 21st

rez.utils.formatting.expanduser(path)#

Expand ‘~’ to home directory in the given string.

Note that this function deliberately differs from the builtin os.path.expanduser() on Linux systems, which expands strings such as ‘~sclaus’ to that user’s homedir. This is problematic in rez because the string ‘~packagename’ may inadvertently convert to a homedir, if a package happens to match a username.

rez.utils.formatting.as_block_string(txt)#

Return a string formatted as a python block comment string, like the one you’re currently reading. Special characters are escaped if necessary.

rez.utils.formatting.header_comment(executor, txt)#

Convenience for creating header-like comment in a rex executor.

Parameters:
  • executor (RexExecutor) – Executor.

  • txt (str) – Comment text.