Skip to main content

nested dictionaries with paths

Project description

Pure python. Path keys. ChainedMap on steroids.


Pure python.

pip install multiplane


Contexts are python dictionaries with path lookups:

import multiplane

context = multiplane.Context()

# normal dictionary operations
context["foo"] = "bar"
assert "bar" == context["foo"]
del context["foo"]

# paths
context[""] = "baz"
assert "baz" == context[""]
del context[""]

# length, iteration
n = 10
context.update((i, i+1) for i in range(n))
assert len(context) == n

for key, value in context.items():
    assert key + 1 == value

But they also track contextual changes:

ctx = multiplane.Context()
ctx["root.layer.key"] = "root_value"

with ctx("layer1"):
    # read through
    assert "root_value" == ctx["root.layer.key"]
    # local to layer1
    ctx["layer1.key"] = "layer1_value"

# layer1 not active
assert "layer1.key" not in ctx

# manually push layer1 back on
assert "layer1_value" == ctx["layer1.key"]

# global context
# WARNING: be careful in the reserved area of the global root,
# as you can break the context tracking.
assert ctx.g is ctx.g["_.root"]
assert ctx.g is ctx.g["_.current"]

# all contexts are available from the root node
assert "layer1" in ctx.g["_.contexts"]
assert "layer1.key" in ctx.g["_.contexts.layer1"]
assert "layer1_value" == ctx.g["_.contexts.layer1.layer1.key"]

# nesting:
ctx["root_key"] = "root_value"

with ctx("layer1"):
    ctx["layer1_key"] = "layer1_value"

    with ctx("layer2"):
        ctx["layer2_key"] = "layer2_value"

        assert "root_key" in ctx
        assert "layer1_key" in ctx
        assert "layer2_key" in ctx

    # Lost layer2
    assert "root_key" in ctx
    assert "layer1_key" in ctx
    assert "layer2_key" not in ctx

# Lost layer1, layer2
assert "root_key" in ctx
assert "layer1_key" not in ctx
assert "layer2_key" not in ctx

Project details

Release history Release notifications | RSS feed

This version


Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page