rez.utils.scope#

class rez.utils.scope.RecursiveAttribute#

Bases: UserDict, StringFormatMixin

An object that can have new attributes added recursively:

>>> a = RecursiveAttribute()
>>> a.foo.bah = 5
>>> a.foo['eek'] = 'hey'
>>> a.fee = 1

>>> print(a.to_dict())
{'foo': {'bah': 5, 'eek': 'hey'}, 'fee': 1}

A recursive attribute can also be created from a dict, and made read-only:

>>> d = {'fee': {'fi': {'fo': 'fum'}}, 'ho': 'hum'}
>>> a = RecursiveAttribute(d, read_only=True)
>>> print(str(a))
{'fee': {'fi': {'fo': 'fum'}}, 'ho': 'hum'}
>>> print(a.ho)
hum
>>> a.new = True
AttributeError: 'RecursiveAttribute' object has no attribute 'new'
format_expand = 3#
__init__(data=None, read_only=False)#
to_dict()#

Get an equivalent dict representation.

copy()#
update(data)#

Dict-like update operation.

clear() None.  Remove all items from D.#
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.

format_pretty = True#
classmethod fromkeys(iterable, value=None)#
get(k[, d]) D[k] if k in D, else d.  d defaults to None.#
items() a set-like object providing a view on D's items#
keys() a set-like object providing a view on D's keys#
pop(k[, d]) v, remove specified key and return the corresponding value.#

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair#

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D#
values() an object providing a view on D's values#
class rez.utils.scope.ScopeContext#

Bases: object

A context manager for creating nested dictionaries:

>>> scope = ScopeContext()
>>>
>>> with scope("animal"):
>>>     count = 2
>>>     with scope("cat"):
>>>         friendly = False
>>>     with scope("dog") as d:
>>>         friendly = True
>>>         d.num_legs = 4
>>>         d.breed.sub_breed = 'yorkshire terrier'
>>> with scope("animal"):
>>>     count = 3
>>>     with scope("cat"):
>>>         num_legs = 4
>>>     with scope("ostrich"):
>>>         friendly = False
>>>         num_legs = 2

The dictionaries can then be retrieved:

>>> print(pprint.pformat(scope.to_dict()))
{'animal': {'count': 3,
            'cat': {'friendly': False,
                    'num_legs': 4},
            'dog': {'breed': {'sub_breed': 'yorkshire terrier'},
                    'friendly': True,
                    'num_legs': 4},
            'ostrich': {'friendly': False,
                        'num_legs': 2}}}

Note that scopes and recursive attributes can be referenced multiple times, and the assigned properties will be merged. If the same property is set multiple times, it will be overwritten.

__init__()#
to_dict()#

Get an equivalent dict representation.

rez.utils.scope.scoped_formatter(**objects)#

Format a string with respect to a set of objects’ attributes.

Use this rather than scoped_format when you need to reuse the formatter.

rez.utils.scope.scoped_format(txt, **objects)#

Format a string with respect to a set of objects’ attributes.

Example

>>> Class Foo(object):
>>>     def __init__(self):
>>>         self.name = "Dave"
>>> print(scoped_format("hello {foo.name}", foo=Foo()))
hello Dave
Parameters:
  • objects (dict) – Dict of objects to format with. If a value is a dict, its values, and any further neted dicts, will also format with dot notation.

  • pretty (bool) – See ObjectStringFormatter.

  • expand (bool) – See ObjectStringFormatter.