4.1 Namespaces

A namespace is a collection of named variables. There is not much exotic about them, in fact most Python programmers use them daily in the form of dictionaries or classes. Draco uses namespaces for a lot of things, making it easy to transfer data from one part of Draco to another. Namespaces are indexed by a key which yields its correspondings value.

Namespaces have an API identical to Python dictionaries. The following table lists the operations that are defined on namespaces (where a and b are namespaces, k is a string key and v and x are arbitrary objects). It is an abbreviated version from the Python Library Reference:

Operation  Result 
len(a) the number of items in a
a[k] the item of a with key k
a[k] = v set a[k] to v
del a[k] remove a[k] from a
a.clear() remove all items from a
a.copy() a (shallow) copy of a
a.has_key(k) 1 if a has a key k, else 0
k in a Equivalent to a.has_key(k)
k not in a Equivalent to not a.has_key(k)
a.items() a copy of a's list of (key, value) pairs
a.keys() a copy of a's list of keys
a.update(b) for k in b.keys(): a[k] = b[k]
a.values() a copy of a's list of values
a.get(k[, x]) a[k] if k in a, else x
a.setdefault(k[, x]) a[k] if k in a, else x (also setting it)
a.pop(k) remove specified key and return corresponding value
a.popitem() remove and return an arbitrary (key, value) pair

Keys in Draco namespaces must be strings. Also, not all namespaces support arbitrary values. The persistent namespaces all use Pickle to serialize the data to a database, meaning that the stored objects must be pickleable. Some namespaces provide multiple scopes. This means that there are multiple independent versions of the same namespace.

The following namespaces are available in Draco:

It is easy to define additional namespaces. For example, the following namespaces might be helpful: