带路径的嵌套词典

multiplane的Python项目详细描述


纯Python。路径键。类固醇链式地图。

安装

纯Python。

pip install multiplane

用法

上下文是具有路径查找功能的python字典:

import multiplane

context = multiplane.Context()

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

# paths
context["foo.bar"] = "baz"
assert "baz" == context["foo.bar"]
del context["foo.bar"]

# 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

但它们也跟踪上下文的变化:

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
ctx.push_context("layer1")
assert "layer1_value" == ctx["layer1.key"]
ctx.pop_context()

# 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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在Java中运行时在两个枚举类之间切换   java如何让PicoContainer启动/停止/处置工厂注入的组件?   带有Recyclerview onClick的java多活动   java如何从TestNG tests和Selenium接口调用默认方法?   java无法在Eclipse3.5.2中折叠注释   RR和SJF CPU调度算法的Java代码   java从属性文件配置记录器   java Notify传输在字符更改后超过20个字节完成   java阵列究竟是如何工作的   java跨类/包维护全局但可变的变量   java向setMessageListener注册侦听器服务   java按钮单击不在片段中工作   java GSSExException:使用spnego在GSSAPI上未指定故障(机制级别:不支持/启用带有HMAC SHA196的加密类型AES256CTS模式)   用java绘制虚线的图形   java从networkdrive启动windows捆绑包使用不包括JRE?   多线程java线程体系结构与应用程序设计