超可变数的集合
supermutes的Python项目详细描述
这个库适用于Python2.6、2.7和3.2。
它定义了三种可变表。
点
dot模块包含允许使用点表示法的类 访问list或dict对象时。
例如:
>> from supermutes.dot import dotify >> d = dotify({'a':[1, 2, 3, 4, 5], 'b': {'c': 5}}) >> d.a._0 1 >> d.b.c 5 >> d.c = {'f': 9} >> d.c.f 9
只读
readonly模块包含转换dict和^{tt2}的类$ 对象转换为不能对其进行任何值更改的对象。
例如:
>> from supermutes.readonly import readonly >> r = readonly({'a':[1, 2, 3, 4, 5], 'b': {'c': 5}}) >> r {'a': [1, 2, 3, 4, 5], 'b': {'c': 5}} >> r['a'].append(5) supermutes.readonly.ReadOnlyClassException: Cannot write to object. >> r['b']['d'] = 6 supermutes.readonly.ReadOnlyClassException: Cannot write to object.
decorator函数也可用于只读对象。会的 readonly fy修饰函数/方法的输出
例如:
from supermutes.decorators import return_readonly @return_readonly def get_list(): return ['12']
orderedDefaultDict
ordered模块包含OrderedDefaultDict类。它是一个 要成为OrderedDict和 defaultdict来自python标准库。
创建子类
当任何超人的子类被声明时,该类将是 设置为递归更改数据集的事实类。
若要将类重置回原始集,请使用reset_mapping方法 在模块内部
例如:
>>> from supermutes.dot import DotDict, DotList, reset_mapping >>> class MySubClass(DotDict): pass >>> d = MySubClass({'a': {'b': {'c': 3}}}) >>> d.a.b {'c': 3} >>> d.a.b.__class__ <class '__main__.MySubClass'> >>> f = DotList([1, {}]) >>> f[1].__class__ <class '__main__.MySubClass'> >>> reset_mapping() >>> f = DotList([1, {}]) >>> f[1].__class__ <class 'supermutes.dot.DotDict'>
自己写Supermutable
如果你想贡献,写一个表现在 特别时尚,请遵循以下指导原则:
- It should inherit from the mutable type that it is adapting (eg ^{tt3}$ ^{tt2}$ etc.)
- It should also inherit from ^{tt15}$. This takes care of all of the registering of any subclasses so that for example, all sub dicts added to the SuperMutable are changed accordingly. See example.py for a working sample.
建筑
复制回购协议后:
$ pip install -r test-requirements.txt $ nosetests
supermutes在http://travis-ci.org/alexcouper/supermutes有一个生成作业