bidsschematools.types.namespace

Namespace types

The purpose of the Namespace type is to make a directory of YAML files available as a single dictionary and allow attribute (.) lookups.

Classes

class bidsschematools.types.namespace.MappingEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: JSONEncoder

default(o)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
encode(o)

Return a JSON string representation of a Python data structure.

>>> from json.encoder import JSONEncoder
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
iterencode(o, _one_shot=False)

Encode the given object and yield each string representation as available.

For example:

for chunk in JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)
class bidsschematools.types.namespace.Namespace(*args, **kwargs)

Bases: MutableMapping

Provides recursive attribute style access to a dict-like structure

Examples

>>> ns = Namespace.build({"a": 1, "b.c": "val"})
>>> ns.a
1
>>> ns["a"]
1
>>> ns.b
<Namespace {'c': 'val'}>
>>> ns["b"]
<Namespace {'c': 'val'}>
>>> ns.b.c
'val'
>>> ns["b.c"]
'val'
>>> ns["b"]["c"]
'val'
>>> ns.b["c"]
'val'
>>> ns["b"].c
'val'

.keys(), .values() and .items() can take an optional level argument:

>>> list(ns.keys())
['a', 'b']
>>> list(ns.keys(level=2))
['b.c']
>>> 'b.c' in ns.keys()
False
>>> 'b.c' in ns.keys(level=2)
True
>>> list(ns.values())
[1, <Namespace {'c': 'val'}>]
>>> list(ns.values(level=2))
['val']
>>> 'val' in ns.values()
False
>>> 'val' in ns.values(level=2)
True
>>> list(ns.items())
[('a', 1), ('b', <Namespace {'c': 'val'}>)]
>>> list(ns.items(level=2))
[('b.c', 'val')]
>>> ("b.c", "val") in ns.items()
False
>>> ("b.c", "val") in ns.items(level=2)
True
>>> ns["d.e.f"] = "val2"
>>> ns.d.e
<Namespace {'f': 'val2'}>
>>> ns.d.e.f
'val2'
>>> del ns['d']
classmethod build(mapping)

Expand mapping recursively and return as namespace

clear() None.  Remove all items from D.
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
update([E, ]**F) None.  Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() an object providing a view on D's values
class bidsschematools.types.namespace.NsItemsView(namespace, level)

Bases: ItemsView

isdisjoint(other)

Return True if two sets have a null intersection.

class bidsschematools.types.namespace.NsKeysView(namespace, level)

Bases: KeysView

isdisjoint(other)

Return True if two sets have a null intersection.

class bidsschematools.types.namespace.NsValuesView(namespace, level)

Bases: ValuesView

Functions

bidsschematools.types.namespace.expand(element)

Expand a dict, recursively, to replace dots in keys with recursive dictionaries

Parameters:

element (dict) – The dictionary to expand

Returns:

The expanded dictionary

Return type:

dict

Examples

>>> expand({"a": 1, "b.c": 2, "d": [{"e": 3, "f.g": 4}]})
{'a': 1, 'b': {'c': 2}, 'd': [{'e': 3, 'f': {'g': 4}}]}