collection.ChainMap的用途是什么? [英] What is the purpose of collections.ChainMap?

查看:327
本文介绍了collection.ChainMap的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 3.3中, ChainMap 课程添加到 collections 模块:

In Python 3.3 a ChainMap class was added to the collections module:


提供了一个ChainMap类,用于快速链接多个映射
,因此它们可以被视为一个单元。它通常比
快得多,创建一个新字典并运行多个update()调用。

A ChainMap class is provided for quickly linking a number of mappings so they can be treated as a single unit. It is often much faster than creating a new dictionary and running multiple update() calls.

示例:

>>> from collections import ChainMap
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 10, 'c': 11}
>>> z = ChainMap(y, x)
>>> for k, v in z.items():
        print(k, v)
a 1
c 11
b 10

它是由此问题激励,并由这一个(未创建 PEP )。

It was motivated by this issue and made public by this one (no PEP was created).

据我所知,这是一个替代方法,有一个额外的字典,并维护它与 update()

As far as I understand, it is an alternative to having an extra dictionary and maintaining it with update()s.

问题是:


  • ChainMap 封面有什么用例?

  • ChainMap 的真实世界示例?

  • 是否用于切换到python3的第三方库?

  • What use cases does ChainMap cover?
  • Are there any real world examples of ChainMap?
  • Is it used in third-party libraries that switched to python3?

有没有办法在Python2.x上使用它?

Bonus question: is there a way to use it on Python2.x?

我在将代码转换为美丽的惯用Python PyCon谈话由Raymond Hettinger和我喜欢添加到我的工具包,但我缺乏理解什么时候应该使用它。

I've heard about it in Transforming Code into Beautiful, Idiomatic Python PyCon talk by Raymond Hettinger and I'd like to add it to my toolkit, but I lack in understanding when should I use it.

推荐答案

我喜欢@ b4hand的例子,我确实使用过去的ChainMap类结构他提到的两个目的:多层配置覆盖和变量栈/范围仿真。

I like @b4hand's examples, and indeed I have used in the past ChainMap-like structures (but not ChainMap itself) for the two purposes he mentions: multi-layered configuration overrides, and variable stack/scope emulation.

我想指出另外两个动机/优点/ code> ChainMap 相比,使用dict-update循环,因此只存储final版本:

I'd like to point out two other motivations/advantages/differences of ChainMap, compared to using a dict-update loop, thus only storing the "final" version":


  1. 详细信息:,因为ChainMap结构是分层的,它支持回答问题:我获得默认值或覆盖原始(默认)值?什么级别的值被重写(借用@ b4hand的配置示例:user-config或命令行覆盖)?使用简单的dict,回答这些问题所需的信息已经丢失。

  1. More information: since a ChainMap structure is "layered", it supports answering question like: Am I getting the "default" value, or an overridden one? What is the original ("default") value? At what level did the value get overridden (borrowing @b4hand's config example: user-config or command-line-overrides)? Using a simple dict, the information needed for answering these questions is already lost.

速度折衷:假设您拥有 N code> M 键,构造ChainMap需要 O(N)和每个查找 O ) worst-case [*],而使用更新循环的dict的构造 O(NM),每个查找 O(1)。这意味着如果你经常构造并且每次只执行一些查找,或者 M 很大,ChainMap的延迟构造方法对你有利。

Speed tradeoff: suppose you have N layers and at most M keys in each, constructing a ChainMap takes O(N) and each lookup O(N) worst-case[*], while construction of a dict using an update-loop takes O(NM) and each lookup O(1). This means that if you construct often and only perform a few lookups each time, or if M is big, ChainMap's lazy-construction approach works in your favor.

[*](2)中的分析假设dict-访问 O(1),平均起来是 O(1),最坏的情况是 O(M)详情请参阅这里

[*] The analysis in (2) assumes dict-access is O(1), when in fact it is O(1) on average, and O(M) worst case. See more details here.

这篇关于collection.ChainMap的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆