-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
113 lines (80 loc) · 2.85 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
supermutes
==========
This library works with python 2.6, 2.7 and 3.2.
It defines three kinds of mutables.
dot
---
The ``dot`` module contains classes that allow dot-notation to be used for
when accessing a ``list`` or ``dict`` object.
eg::
>> 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
--------
The ``readonly`` module contains classes that transform ``dict`` and ``list``
objects into ones that cannot have any values changed on them.
eg::
>> 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.
A decorator function is also available for readonly objects. It will
readonly-fy the output of the decorated function/method
eg::
from supermutes.decorators import return_readonly
@return_readonly
def get_list():
return ['12']
OrderedDefaultDict
------------------
The ``ordered`` module contains the ``OrderedDefaultDict`` class. It is an
implementation that is meant to be the child of an ``OrderedDict`` and a
``defaultdict`` from the python standard library.
Creating Sub Classes
--------------------
Upon declaration of a sub class of any of the supermutes, that class will be
set as the defacto class for recursively changing data sets.
To reset the classes back to the original set, use the ``reset_mapping`` method
inside the module
eg::
>>> 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'>
Writing your own ``Supermutable``
---------------------------------
If you would like to contribute, and write a supermutable that behaves in a
particular fashion, just try to follow these guidelines:
* It should inherit from the mutable type that it is adapting (eg ``dict``
``list`` etc.)
* It should also inherit from ``base.SuperMutable``. 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.
Building
--------
After cloning the repo::
$ pip install -r test-requirements.txt
$ nosetests
``supermutes`` has a build job at http://travis-ci.org/alexcouper/supermutes