NamedValueAccess

NamedValueAccess.py

NamedValueAccess provides functions for accessing Python objects by keys and named attributes. A ‘key’ is a single identifier such as ‘foo’. A ‘name’ could be a key, or a qualified key, such as ‘foo.bar.boo’. Names are generally more convenient and powerful, while the key-oriented function is more efficient and provide the atomic functionality that the name-oriented function is built upon.

CREDIT

Chuck Esterbrook <echuck@mindspring.com> Tavis Rudd <tavis@calrudd.com>

exception MiscUtils.NamedValueAccess.NamedValueAccessError

Bases: LookupError

General named value access error.

__init__(*args, **kwargs)
args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception MiscUtils.NamedValueAccess.ValueForKeyError

Bases: NamedValueAccessError

No value for key found error.

__init__(*args, **kwargs)
args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

MiscUtils.NamedValueAccess.valueForKey(obj, key, default=<class 'MiscUtils.NoDefault'>)

Get the value of the object named by the given key.

This method returns the value with the following precedence:
  1. Methods before non-methods

  2. Attributes before keys (__getitem__)

  3. Public things before private things (private being denoted by a preceding underscore)

Suppose key is ‘foo’, then this method returns one of these:
  • obj.foo()

  • obj._foo()

  • obj.foo

  • obj._foo

  • obj[‘foo’]

  • default # only if specified

If all of these fail, a ValueForKeyError is raised.

NOTES

  • valueForKey() works on dictionaries and dictionary-like objects.

  • See valueForName() which is a more advanced version of this function that allows multiple, qualified keys.

MiscUtils.NamedValueAccess.valueForName(obj, name, default=<class 'MiscUtils.NoDefault'>)

Get the value of the object that is named.

The name can use dotted notation to traverse through a network/graph of objects. Since this function relies on valueForKey() for each individual component of the name, you should be familiar with the semantics of that notation.

Example: valueForName(obj, ‘department.manager.salary’)