Show More
@@ -1,167 +1,167 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Older utilities that are not being used. |
|
4 | 4 | |
|
5 | 5 | WARNING: IF YOU NEED TO USE ONE OF THESE FUNCTIONS, PLEASE FIRST MOVE IT |
|
6 | 6 | TO ANOTHER APPROPRIATE MODULE IN IPython.utils. |
|
7 | 7 | """ |
|
8 | 8 | |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | # Copyright (C) 2008-2011 The IPython Development Team |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | 13 | # the file COPYING, distributed as part of this software. |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | # Imports |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | |
|
20 | 20 | import sys |
|
21 | 21 | import warnings |
|
22 | 22 | |
|
23 | 23 | from IPython.utils.warn import warn |
|
24 | 24 | |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | # Code |
|
27 | 27 | #----------------------------------------------------------------------------- |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | def mutex_opts(dict,ex_op): |
|
31 | 31 | """Check for presence of mutually exclusive keys in a dict. |
|
32 | 32 | |
|
33 | 33 | Call: mutex_opts(dict,[[op1a,op1b],[op2a,op2b]...]""" |
|
34 | 34 | for op1,op2 in ex_op: |
|
35 | 35 | if op1 in dict and op2 in dict: |
|
36 | 36 | raise ValueError('\n*** ERROR in Arguments *** '\ |
|
37 | 37 | 'Options '+op1+' and '+op2+' are mutually exclusive.') |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | class EvalDict: |
|
41 | 41 | """ |
|
42 | 42 | Emulate a dict which evaluates its contents in the caller's frame. |
|
43 | 43 | |
|
44 | 44 | Usage: |
|
45 | 45 | >>> number = 19 |
|
46 | 46 | |
|
47 | 47 | >>> text = "python" |
|
48 | 48 | |
|
49 |
>>> print |
|
|
49 | >>> print("%(text.capitalize())s %(number/9.0).1f rules!" % EvalDict()) | |
|
50 | 50 | Python 2.1 rules! |
|
51 | 51 | """ |
|
52 | 52 | |
|
53 | 53 | # This version is due to sismex01@hebmex.com on c.l.py, and is basically a |
|
54 | 54 | # modified (shorter) version of: |
|
55 | 55 | # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66018 by |
|
56 | 56 | # Skip Montanaro (skip@pobox.com). |
|
57 | 57 | |
|
58 | 58 | def __getitem__(self, name): |
|
59 | 59 | frame = sys._getframe(1) |
|
60 | 60 | return eval(name, frame.f_globals, frame.f_locals) |
|
61 | 61 | |
|
62 | 62 | EvalString = EvalDict # for backwards compatibility |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | def all_belong(candidates,checklist): |
|
66 | 66 | """Check whether a list of items ALL appear in a given list of options. |
|
67 | 67 | |
|
68 | 68 | Returns a single 1 or 0 value.""" |
|
69 | 69 | |
|
70 | 70 | return 1-(0 in [x in checklist for x in candidates]) |
|
71 | 71 | |
|
72 | 72 | |
|
73 | 73 | def belong(candidates,checklist): |
|
74 | 74 | """Check whether a list of items appear in a given list of options. |
|
75 | 75 | |
|
76 | 76 | Returns a list of 1 and 0, one for each candidate given.""" |
|
77 | 77 | |
|
78 | 78 | return [x in checklist for x in candidates] |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | def with_obj(object, **args): |
|
82 | 82 | """Set multiple attributes for an object, similar to Pascal's with. |
|
83 | 83 | |
|
84 | 84 | Example: |
|
85 | 85 | with_obj(jim, |
|
86 | 86 | born = 1960, |
|
87 | 87 | haircolour = 'Brown', |
|
88 | 88 | eyecolour = 'Green') |
|
89 | 89 | |
|
90 | 90 | Credit: Greg Ewing, in |
|
91 | 91 | http://mail.python.org/pipermail/python-list/2001-May/040703.html. |
|
92 | 92 | |
|
93 | 93 | NOTE: up until IPython 0.7.2, this was called simply 'with', but 'with' |
|
94 | 94 | has become a keyword for Python 2.5, so we had to rename it.""" |
|
95 | 95 | |
|
96 | 96 | object.__dict__.update(args) |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | def map_method(method,object_list,*argseq,**kw): |
|
100 | 100 | """map_method(method,object_list,*args,**kw) -> list |
|
101 | 101 | |
|
102 | 102 | Return a list of the results of applying the methods to the items of the |
|
103 | 103 | argument sequence(s). If more than one sequence is given, the method is |
|
104 | 104 | called with an argument list consisting of the corresponding item of each |
|
105 | 105 | sequence. All sequences must be of the same length. |
|
106 | 106 | |
|
107 | 107 | Keyword arguments are passed verbatim to all objects called. |
|
108 | 108 | |
|
109 | 109 | This is Python code, so it's not nearly as fast as the builtin map().""" |
|
110 | 110 | |
|
111 | 111 | out_list = [] |
|
112 | 112 | idx = 0 |
|
113 | 113 | for object in object_list: |
|
114 | 114 | try: |
|
115 | 115 | handler = getattr(object, method) |
|
116 | 116 | except AttributeError: |
|
117 | 117 | out_list.append(None) |
|
118 | 118 | else: |
|
119 | 119 | if argseq: |
|
120 | 120 | args = map(lambda lst:lst[idx],argseq) |
|
121 | 121 | #print 'ob',object,'hand',handler,'ar',args # dbg |
|
122 | 122 | out_list.append(handler(args,**kw)) |
|
123 | 123 | else: |
|
124 | 124 | out_list.append(handler(**kw)) |
|
125 | 125 | idx += 1 |
|
126 | 126 | return out_list |
|
127 | 127 | |
|
128 | 128 | |
|
129 | 129 | def import_fail_info(mod_name,fns=None): |
|
130 | 130 | """Inform load failure for a module.""" |
|
131 | 131 | |
|
132 | 132 | if fns == None: |
|
133 | 133 | warn("Loading of %s failed." % (mod_name,)) |
|
134 | 134 | else: |
|
135 | 135 | warn("Loading of %s from %s failed." % (fns,mod_name)) |
|
136 | 136 | |
|
137 | 137 | |
|
138 | 138 | class NotGiven: pass |
|
139 | 139 | |
|
140 | 140 | def popkey(dct,key,default=NotGiven): |
|
141 | 141 | """Return dct[key] and delete dct[key]. |
|
142 | 142 | |
|
143 | 143 | If default is given, return it if dct[key] doesn't exist, otherwise raise |
|
144 | 144 | KeyError. """ |
|
145 | 145 | |
|
146 | 146 | try: |
|
147 | 147 | val = dct[key] |
|
148 | 148 | except KeyError: |
|
149 | 149 | if default is NotGiven: |
|
150 | 150 | raise |
|
151 | 151 | else: |
|
152 | 152 | return default |
|
153 | 153 | else: |
|
154 | 154 | del dct[key] |
|
155 | 155 | return val |
|
156 | 156 | |
|
157 | 157 | |
|
158 | 158 | def wrap_deprecated(func, suggest = '<nothing>'): |
|
159 | 159 | def newFunc(*args, **kwargs): |
|
160 | 160 | warnings.warn("Call to deprecated function %s, use %s instead" % |
|
161 | 161 | ( func.__name__, suggest), |
|
162 | 162 | category=DeprecationWarning, |
|
163 | 163 | stacklevel = 2) |
|
164 | 164 | return func(*args, **kwargs) |
|
165 | 165 | return newFunc |
|
166 | 166 | |
|
167 | 167 |
@@ -1,391 +1,391 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """A dict subclass that supports attribute style access. |
|
3 | 3 | |
|
4 | 4 | Authors: |
|
5 | 5 | |
|
6 | 6 | * Fernando Perez (original) |
|
7 | 7 | * Brian Granger (refactoring to a dict subclass) |
|
8 | 8 | """ |
|
9 | 9 | |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | # Copyright (C) 2008-2011 The IPython Development Team |
|
12 | 12 | # |
|
13 | 13 | # Distributed under the terms of the BSD License. The full license is in |
|
14 | 14 | # the file COPYING, distributed as part of this software. |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | # Imports |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 | 21 | __all__ = ['Struct'] |
|
22 | 22 | |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | # Code |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | class Struct(dict): |
|
29 | 29 | """A dict subclass with attribute style access. |
|
30 | 30 | |
|
31 | 31 | This dict subclass has a a few extra features: |
|
32 | 32 | |
|
33 | 33 | * Attribute style access. |
|
34 | 34 | * Protection of class members (like keys, items) when using attribute |
|
35 | 35 | style access. |
|
36 | 36 | * The ability to restrict assignment to only existing keys. |
|
37 | 37 | * Intelligent merging. |
|
38 | 38 | * Overloaded operators. |
|
39 | 39 | """ |
|
40 | 40 | _allownew = True |
|
41 | 41 | def __init__(self, *args, **kw): |
|
42 | 42 | """Initialize with a dictionary, another Struct, or data. |
|
43 | 43 | |
|
44 | 44 | Parameters |
|
45 | 45 | ---------- |
|
46 | 46 | args : dict, Struct |
|
47 | 47 | Initialize with one dict or Struct |
|
48 | 48 | kw : dict |
|
49 | 49 | Initialize with key, value pairs. |
|
50 | 50 | |
|
51 | 51 | Examples |
|
52 | 52 | -------- |
|
53 | 53 | |
|
54 | 54 | >>> s = Struct(a=10,b=30) |
|
55 | 55 | >>> s.a |
|
56 | 56 | 10 |
|
57 | 57 | >>> s.b |
|
58 | 58 | 30 |
|
59 | 59 | >>> s2 = Struct(s,c=30) |
|
60 | 60 | >>> sorted(s2.keys()) |
|
61 | 61 | ['a', 'b', 'c'] |
|
62 | 62 | """ |
|
63 | 63 | object.__setattr__(self, '_allownew', True) |
|
64 | 64 | dict.__init__(self, *args, **kw) |
|
65 | 65 | |
|
66 | 66 | def __setitem__(self, key, value): |
|
67 | 67 | """Set an item with check for allownew. |
|
68 | 68 | |
|
69 | 69 | Examples |
|
70 | 70 | -------- |
|
71 | 71 | |
|
72 | 72 | >>> s = Struct() |
|
73 | 73 | >>> s['a'] = 10 |
|
74 | 74 | >>> s.allow_new_attr(False) |
|
75 | 75 | >>> s['a'] = 10 |
|
76 | 76 | >>> s['a'] |
|
77 | 77 | 10 |
|
78 | 78 | >>> try: |
|
79 | 79 | ... s['b'] = 20 |
|
80 | 80 | ... except KeyError: |
|
81 |
... print |
|
|
81 | ... print('this is not allowed') | |
|
82 | 82 | ... |
|
83 | 83 | this is not allowed |
|
84 | 84 | """ |
|
85 | 85 | if not self._allownew and key not in self: |
|
86 | 86 | raise KeyError( |
|
87 | 87 | "can't create new attribute %s when allow_new_attr(False)" % key) |
|
88 | 88 | dict.__setitem__(self, key, value) |
|
89 | 89 | |
|
90 | 90 | def __setattr__(self, key, value): |
|
91 | 91 | """Set an attr with protection of class members. |
|
92 | 92 | |
|
93 | 93 | This calls :meth:`self.__setitem__` but convert :exc:`KeyError` to |
|
94 | 94 | :exc:`AttributeError`. |
|
95 | 95 | |
|
96 | 96 | Examples |
|
97 | 97 | -------- |
|
98 | 98 | |
|
99 | 99 | >>> s = Struct() |
|
100 | 100 | >>> s.a = 10 |
|
101 | 101 | >>> s.a |
|
102 | 102 | 10 |
|
103 | 103 | >>> try: |
|
104 | 104 | ... s.get = 10 |
|
105 | 105 | ... except AttributeError: |
|
106 |
... print |
|
|
106 | ... print("you can't set a class member") | |
|
107 | 107 | ... |
|
108 | 108 | you can't set a class member |
|
109 | 109 | """ |
|
110 | 110 | # If key is an str it might be a class member or instance var |
|
111 | 111 | if isinstance(key, str): |
|
112 | 112 | # I can't simply call hasattr here because it calls getattr, which |
|
113 | 113 | # calls self.__getattr__, which returns True for keys in |
|
114 | 114 | # self._data. But I only want keys in the class and in |
|
115 | 115 | # self.__dict__ |
|
116 | 116 | if key in self.__dict__ or hasattr(Struct, key): |
|
117 | 117 | raise AttributeError( |
|
118 | 118 | 'attr %s is a protected member of class Struct.' % key |
|
119 | 119 | ) |
|
120 | 120 | try: |
|
121 | 121 | self.__setitem__(key, value) |
|
122 | 122 | except KeyError as e: |
|
123 | 123 | raise AttributeError(e) |
|
124 | 124 | |
|
125 | 125 | def __getattr__(self, key): |
|
126 | 126 | """Get an attr by calling :meth:`dict.__getitem__`. |
|
127 | 127 | |
|
128 | 128 | Like :meth:`__setattr__`, this method converts :exc:`KeyError` to |
|
129 | 129 | :exc:`AttributeError`. |
|
130 | 130 | |
|
131 | 131 | Examples |
|
132 | 132 | -------- |
|
133 | 133 | |
|
134 | 134 | >>> s = Struct(a=10) |
|
135 | 135 | >>> s.a |
|
136 | 136 | 10 |
|
137 | 137 | >>> type(s.get) |
|
138 | 138 | <... 'builtin_function_or_method'> |
|
139 | 139 | >>> try: |
|
140 | 140 | ... s.b |
|
141 | 141 | ... except AttributeError: |
|
142 |
... print |
|
|
142 | ... print("I don't have that key") | |
|
143 | 143 | ... |
|
144 | 144 | I don't have that key |
|
145 | 145 | """ |
|
146 | 146 | try: |
|
147 | 147 | result = self[key] |
|
148 | 148 | except KeyError: |
|
149 | 149 | raise AttributeError(key) |
|
150 | 150 | else: |
|
151 | 151 | return result |
|
152 | 152 | |
|
153 | 153 | def __iadd__(self, other): |
|
154 | 154 | """s += s2 is a shorthand for s.merge(s2). |
|
155 | 155 | |
|
156 | 156 | Examples |
|
157 | 157 | -------- |
|
158 | 158 | |
|
159 | 159 | >>> s = Struct(a=10,b=30) |
|
160 | 160 | >>> s2 = Struct(a=20,c=40) |
|
161 | 161 | >>> s += s2 |
|
162 | 162 | >>> sorted(s.keys()) |
|
163 | 163 | ['a', 'b', 'c'] |
|
164 | 164 | """ |
|
165 | 165 | self.merge(other) |
|
166 | 166 | return self |
|
167 | 167 | |
|
168 | 168 | def __add__(self,other): |
|
169 | 169 | """s + s2 -> New Struct made from s.merge(s2). |
|
170 | 170 | |
|
171 | 171 | Examples |
|
172 | 172 | -------- |
|
173 | 173 | |
|
174 | 174 | >>> s1 = Struct(a=10,b=30) |
|
175 | 175 | >>> s2 = Struct(a=20,c=40) |
|
176 | 176 | >>> s = s1 + s2 |
|
177 | 177 | >>> sorted(s.keys()) |
|
178 | 178 | ['a', 'b', 'c'] |
|
179 | 179 | """ |
|
180 | 180 | sout = self.copy() |
|
181 | 181 | sout.merge(other) |
|
182 | 182 | return sout |
|
183 | 183 | |
|
184 | 184 | def __sub__(self,other): |
|
185 | 185 | """s1 - s2 -> remove keys in s2 from s1. |
|
186 | 186 | |
|
187 | 187 | Examples |
|
188 | 188 | -------- |
|
189 | 189 | |
|
190 | 190 | >>> s1 = Struct(a=10,b=30) |
|
191 | 191 | >>> s2 = Struct(a=40) |
|
192 | 192 | >>> s = s1 - s2 |
|
193 | 193 | >>> s |
|
194 | 194 | {'b': 30} |
|
195 | 195 | """ |
|
196 | 196 | sout = self.copy() |
|
197 | 197 | sout -= other |
|
198 | 198 | return sout |
|
199 | 199 | |
|
200 | 200 | def __isub__(self,other): |
|
201 | 201 | """Inplace remove keys from self that are in other. |
|
202 | 202 | |
|
203 | 203 | Examples |
|
204 | 204 | -------- |
|
205 | 205 | |
|
206 | 206 | >>> s1 = Struct(a=10,b=30) |
|
207 | 207 | >>> s2 = Struct(a=40) |
|
208 | 208 | >>> s1 -= s2 |
|
209 | 209 | >>> s1 |
|
210 | 210 | {'b': 30} |
|
211 | 211 | """ |
|
212 | 212 | for k in other.keys(): |
|
213 | 213 | if k in self: |
|
214 | 214 | del self[k] |
|
215 | 215 | return self |
|
216 | 216 | |
|
217 | 217 | def __dict_invert(self, data): |
|
218 | 218 | """Helper function for merge. |
|
219 | 219 | |
|
220 | 220 | Takes a dictionary whose values are lists and returns a dict with |
|
221 | 221 | the elements of each list as keys and the original keys as values. |
|
222 | 222 | """ |
|
223 | 223 | outdict = {} |
|
224 | 224 | for k,lst in data.items(): |
|
225 | 225 | if isinstance(lst, str): |
|
226 | 226 | lst = lst.split() |
|
227 | 227 | for entry in lst: |
|
228 | 228 | outdict[entry] = k |
|
229 | 229 | return outdict |
|
230 | 230 | |
|
231 | 231 | def dict(self): |
|
232 | 232 | return self |
|
233 | 233 | |
|
234 | 234 | def copy(self): |
|
235 | 235 | """Return a copy as a Struct. |
|
236 | 236 | |
|
237 | 237 | Examples |
|
238 | 238 | -------- |
|
239 | 239 | |
|
240 | 240 | >>> s = Struct(a=10,b=30) |
|
241 | 241 | >>> s2 = s.copy() |
|
242 | 242 | >>> type(s2) is Struct |
|
243 | 243 | True |
|
244 | 244 | """ |
|
245 | 245 | return Struct(dict.copy(self)) |
|
246 | 246 | |
|
247 | 247 | def hasattr(self, key): |
|
248 | 248 | """hasattr function available as a method. |
|
249 | 249 | |
|
250 | 250 | Implemented like has_key. |
|
251 | 251 | |
|
252 | 252 | Examples |
|
253 | 253 | -------- |
|
254 | 254 | |
|
255 | 255 | >>> s = Struct(a=10) |
|
256 | 256 | >>> s.hasattr('a') |
|
257 | 257 | True |
|
258 | 258 | >>> s.hasattr('b') |
|
259 | 259 | False |
|
260 | 260 | >>> s.hasattr('get') |
|
261 | 261 | False |
|
262 | 262 | """ |
|
263 | 263 | return key in self |
|
264 | 264 | |
|
265 | 265 | def allow_new_attr(self, allow = True): |
|
266 | 266 | """Set whether new attributes can be created in this Struct. |
|
267 | 267 | |
|
268 | 268 | This can be used to catch typos by verifying that the attribute user |
|
269 | 269 | tries to change already exists in this Struct. |
|
270 | 270 | """ |
|
271 | 271 | object.__setattr__(self, '_allownew', allow) |
|
272 | 272 | |
|
273 | 273 | def merge(self, __loc_data__=None, __conflict_solve=None, **kw): |
|
274 | 274 | """Merge two Structs with customizable conflict resolution. |
|
275 | 275 | |
|
276 | 276 | This is similar to :meth:`update`, but much more flexible. First, a |
|
277 | 277 | dict is made from data+key=value pairs. When merging this dict with |
|
278 | 278 | the Struct S, the optional dictionary 'conflict' is used to decide |
|
279 | 279 | what to do. |
|
280 | 280 | |
|
281 | 281 | If conflict is not given, the default behavior is to preserve any keys |
|
282 | 282 | with their current value (the opposite of the :meth:`update` method's |
|
283 | 283 | behavior). |
|
284 | 284 | |
|
285 | 285 | Parameters |
|
286 | 286 | ---------- |
|
287 | 287 | __loc_data : dict, Struct |
|
288 | 288 | The data to merge into self |
|
289 | 289 | __conflict_solve : dict |
|
290 | 290 | The conflict policy dict. The keys are binary functions used to |
|
291 | 291 | resolve the conflict and the values are lists of strings naming |
|
292 | 292 | the keys the conflict resolution function applies to. Instead of |
|
293 | 293 | a list of strings a space separated string can be used, like |
|
294 | 294 | 'a b c'. |
|
295 | 295 | kw : dict |
|
296 | 296 | Additional key, value pairs to merge in |
|
297 | 297 | |
|
298 | 298 | Notes |
|
299 | 299 | ----- |
|
300 | 300 | |
|
301 | 301 | The `__conflict_solve` dict is a dictionary of binary functions which will be used to |
|
302 | 302 | solve key conflicts. Here is an example:: |
|
303 | 303 | |
|
304 | 304 | __conflict_solve = dict( |
|
305 | 305 | func1=['a','b','c'], |
|
306 | 306 | func2=['d','e'] |
|
307 | 307 | ) |
|
308 | 308 | |
|
309 | 309 | In this case, the function :func:`func1` will be used to resolve |
|
310 | 310 | keys 'a', 'b' and 'c' and the function :func:`func2` will be used for |
|
311 | 311 | keys 'd' and 'e'. This could also be written as:: |
|
312 | 312 | |
|
313 | 313 | __conflict_solve = dict(func1='a b c',func2='d e') |
|
314 | 314 | |
|
315 | 315 | These functions will be called for each key they apply to with the |
|
316 | 316 | form:: |
|
317 | 317 | |
|
318 | 318 | func1(self['a'], other['a']) |
|
319 | 319 | |
|
320 | 320 | The return value is used as the final merged value. |
|
321 | 321 | |
|
322 | 322 | As a convenience, merge() provides five (the most commonly needed) |
|
323 | 323 | pre-defined policies: preserve, update, add, add_flip and add_s. The |
|
324 | 324 | easiest explanation is their implementation:: |
|
325 | 325 | |
|
326 | 326 | preserve = lambda old,new: old |
|
327 | 327 | update = lambda old,new: new |
|
328 | 328 | add = lambda old,new: old + new |
|
329 | 329 | add_flip = lambda old,new: new + old # note change of order! |
|
330 | 330 | add_s = lambda old,new: old + ' ' + new # only for str! |
|
331 | 331 | |
|
332 | 332 | You can use those four words (as strings) as keys instead |
|
333 | 333 | of defining them as functions, and the merge method will substitute |
|
334 | 334 | the appropriate functions for you. |
|
335 | 335 | |
|
336 | 336 | For more complicated conflict resolution policies, you still need to |
|
337 | 337 | construct your own functions. |
|
338 | 338 | |
|
339 | 339 | Examples |
|
340 | 340 | -------- |
|
341 | 341 | |
|
342 | 342 | This show the default policy: |
|
343 | 343 | |
|
344 | 344 | >>> s = Struct(a=10,b=30) |
|
345 | 345 | >>> s2 = Struct(a=20,c=40) |
|
346 | 346 | >>> s.merge(s2) |
|
347 | 347 | >>> sorted(s.items()) |
|
348 | 348 | [('a', 10), ('b', 30), ('c', 40)] |
|
349 | 349 | |
|
350 | 350 | Now, show how to specify a conflict dict: |
|
351 | 351 | |
|
352 | 352 | >>> s = Struct(a=10,b=30) |
|
353 | 353 | >>> s2 = Struct(a=20,b=40) |
|
354 | 354 | >>> conflict = {'update':'a','add':'b'} |
|
355 | 355 | >>> s.merge(s2,conflict) |
|
356 | 356 | >>> sorted(s.items()) |
|
357 | 357 | [('a', 20), ('b', 70)] |
|
358 | 358 | """ |
|
359 | 359 | |
|
360 | 360 | data_dict = dict(__loc_data__,**kw) |
|
361 | 361 | |
|
362 | 362 | # policies for conflict resolution: two argument functions which return |
|
363 | 363 | # the value that will go in the new struct |
|
364 | 364 | preserve = lambda old,new: old |
|
365 | 365 | update = lambda old,new: new |
|
366 | 366 | add = lambda old,new: old + new |
|
367 | 367 | add_flip = lambda old,new: new + old # note change of order! |
|
368 | 368 | add_s = lambda old,new: old + ' ' + new |
|
369 | 369 | |
|
370 | 370 | # default policy is to keep current keys when there's a conflict |
|
371 | 371 | conflict_solve = dict.fromkeys(self, preserve) |
|
372 | 372 | |
|
373 | 373 | # the conflict_solve dictionary is given by the user 'inverted': we |
|
374 | 374 | # need a name-function mapping, it comes as a function -> names |
|
375 | 375 | # dict. Make a local copy (b/c we'll make changes), replace user |
|
376 | 376 | # strings for the three builtin policies and invert it. |
|
377 | 377 | if __conflict_solve: |
|
378 | 378 | inv_conflict_solve_user = __conflict_solve.copy() |
|
379 | 379 | for name, func in [('preserve',preserve), ('update',update), |
|
380 | 380 | ('add',add), ('add_flip',add_flip), |
|
381 | 381 | ('add_s',add_s)]: |
|
382 | 382 | if name in inv_conflict_solve_user.keys(): |
|
383 | 383 | inv_conflict_solve_user[func] = inv_conflict_solve_user[name] |
|
384 | 384 | del inv_conflict_solve_user[name] |
|
385 | 385 | conflict_solve.update(self.__dict_invert(inv_conflict_solve_user)) |
|
386 | 386 | for key in data_dict: |
|
387 | 387 | if key not in self: |
|
388 | 388 | self[key] = data_dict[key] |
|
389 | 389 | else: |
|
390 | 390 | self[key] = conflict_solve[key](self[key],data_dict[key]) |
|
391 | 391 |
@@ -1,68 +1,68 b'' | |||
|
1 | 1 | """String dispatch class to match regexps and dispatch commands. |
|
2 | 2 | """ |
|
3 | 3 | |
|
4 | 4 | # Stdlib imports |
|
5 | 5 | import re |
|
6 | 6 | |
|
7 | 7 | # Our own modules |
|
8 | 8 | from IPython.core.hooks import CommandChainDispatcher |
|
9 | 9 | |
|
10 | 10 | # Code begins |
|
11 | 11 | class StrDispatch(object): |
|
12 | 12 | """Dispatch (lookup) a set of strings / regexps for match. |
|
13 | 13 | |
|
14 | 14 | Example: |
|
15 | 15 | |
|
16 | 16 | >>> dis = StrDispatch() |
|
17 | 17 | >>> dis.add_s('hei',34, priority = 4) |
|
18 | 18 | >>> dis.add_s('hei',123, priority = 2) |
|
19 | 19 | >>> dis.add_re('h.i', 686) |
|
20 |
>>> print |
|
|
20 | >>> print(list(dis.flat_matches('hei'))) | |
|
21 | 21 | [123, 34, 686] |
|
22 | 22 | """ |
|
23 | 23 | |
|
24 | 24 | def __init__(self): |
|
25 | 25 | self.strs = {} |
|
26 | 26 | self.regexs = {} |
|
27 | 27 | |
|
28 | 28 | def add_s(self, s, obj, priority= 0 ): |
|
29 | 29 | """ Adds a target 'string' for dispatching """ |
|
30 | 30 | |
|
31 | 31 | chain = self.strs.get(s, CommandChainDispatcher()) |
|
32 | 32 | chain.add(obj,priority) |
|
33 | 33 | self.strs[s] = chain |
|
34 | 34 | |
|
35 | 35 | def add_re(self, regex, obj, priority= 0 ): |
|
36 | 36 | """ Adds a target regexp for dispatching """ |
|
37 | 37 | |
|
38 | 38 | chain = self.regexs.get(regex, CommandChainDispatcher()) |
|
39 | 39 | chain.add(obj,priority) |
|
40 | 40 | self.regexs[regex] = chain |
|
41 | 41 | |
|
42 | 42 | def dispatch(self, key): |
|
43 | 43 | """ Get a seq of Commandchain objects that match key """ |
|
44 | 44 | if key in self.strs: |
|
45 | 45 | yield self.strs[key] |
|
46 | 46 | |
|
47 | 47 | for r, obj in self.regexs.items(): |
|
48 | 48 | if re.match(r, key): |
|
49 | 49 | yield obj |
|
50 | 50 | else: |
|
51 | 51 | #print "nomatch",key # dbg |
|
52 | 52 | pass |
|
53 | 53 | |
|
54 | 54 | def __repr__(self): |
|
55 | 55 | return "<Strdispatch %s, %s>" % (self.strs, self.regexs) |
|
56 | 56 | |
|
57 | 57 | def s_matches(self, key): |
|
58 | 58 | if key not in self.strs: |
|
59 | 59 | return |
|
60 | 60 | for el in self.strs[key]: |
|
61 | 61 | yield el[1] |
|
62 | 62 | |
|
63 | 63 | def flat_matches(self, key): |
|
64 | 64 | """ Yield all 'value' targets, without priority """ |
|
65 | 65 | for val in self.dispatch(key): |
|
66 | 66 | for el in val: |
|
67 | 67 | yield el[1] # only value, no priority |
|
68 | 68 | return |
@@ -1,147 +1,143 b'' | |||
|
1 | 1 | """Some tests for the wildcard utilities.""" |
|
2 | 2 | |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Library imports |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Stdlib |
|
7 | 7 | import unittest |
|
8 | 8 | |
|
9 | 9 | # Our own |
|
10 | 10 | from IPython.utils import wildcard |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Globals for test |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | class obj_t(object): |
|
17 | 17 | pass |
|
18 | 18 | |
|
19 | 19 | root = obj_t() |
|
20 | 20 | l = ["arna","abel","ABEL","active","bob","bark","abbot"] |
|
21 | 21 | q = ["kate","loop","arne","vito","lucifer","koppel"] |
|
22 | 22 | for x in l: |
|
23 | 23 | o = obj_t() |
|
24 | 24 | setattr(root,x,o) |
|
25 | 25 | for y in q: |
|
26 | 26 | p = obj_t() |
|
27 | 27 | setattr(o,y,p) |
|
28 | 28 | root._apan = obj_t() |
|
29 | 29 | root._apan.a = 10 |
|
30 | 30 | root._apan._a = 20 |
|
31 | 31 | root._apan.__a = 20 |
|
32 | 32 | root.__anka = obj_t() |
|
33 | 33 | root.__anka.a = 10 |
|
34 | 34 | root.__anka._a = 20 |
|
35 | 35 | root.__anka.__a = 20 |
|
36 | 36 | |
|
37 | 37 | root._APAN = obj_t() |
|
38 | 38 | root._APAN.a = 10 |
|
39 | 39 | root._APAN._a = 20 |
|
40 | 40 | root._APAN.__a = 20 |
|
41 | 41 | root.__ANKA = obj_t() |
|
42 | 42 | root.__ANKA.a = 10 |
|
43 | 43 | root.__ANKA._a = 20 |
|
44 | 44 | root.__ANKA.__a = 20 |
|
45 | 45 | |
|
46 | 46 | #----------------------------------------------------------------------------- |
|
47 | 47 | # Test cases |
|
48 | 48 | #----------------------------------------------------------------------------- |
|
49 | 49 | |
|
50 | 50 | class Tests (unittest.TestCase): |
|
51 | 51 | def test_case(self): |
|
52 | 52 | ns=root.__dict__ |
|
53 | 53 | tests=[ |
|
54 | 54 | ("a*", ["abbot","abel","active","arna",]), |
|
55 | 55 | ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",]), |
|
56 | 56 | ("_a*", []), |
|
57 | 57 | ("_*anka", ["__anka",]), |
|
58 | 58 | ("_*a*", ["__anka",]), |
|
59 | 59 | ] |
|
60 | 60 | for pat,res in tests: |
|
61 | 61 | res.sort() |
|
62 | a=wildcard.list_namespace(ns,"all",pat,ignore_case=False, | |
|
63 | show_all=False).keys() | |
|
64 | a.sort() | |
|
62 | a=sorted(wildcard.list_namespace(ns,"all",pat,ignore_case=False, | |
|
63 | show_all=False).keys()) | |
|
65 | 64 | self.assertEqual(a,res) |
|
66 | 65 | |
|
67 | 66 | def test_case_showall(self): |
|
68 | 67 | ns=root.__dict__ |
|
69 | 68 | tests=[ |
|
70 | 69 | ("a*", ["abbot","abel","active","arna",]), |
|
71 | 70 | ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop",]), |
|
72 | 71 | ("_a*", ["_apan"]), |
|
73 | 72 | ("_*anka", ["__anka",]), |
|
74 | 73 | ("_*a*", ["__anka","_apan",]), |
|
75 | 74 | ] |
|
76 | 75 | for pat,res in tests: |
|
77 | 76 | res.sort() |
|
78 | a=wildcard.list_namespace(ns,"all",pat,ignore_case=False, | |
|
79 | show_all=True).keys() | |
|
80 | a.sort() | |
|
77 | a=sorted(wildcard.list_namespace(ns,"all",pat,ignore_case=False, | |
|
78 | show_all=True).keys()) | |
|
81 | 79 | self.assertEqual(a,res) |
|
82 | 80 | |
|
83 | 81 | |
|
84 | 82 | def test_nocase(self): |
|
85 | 83 | ns=root.__dict__ |
|
86 | 84 | tests=[ |
|
87 | 85 | ("a*", ["abbot","abel","ABEL","active","arna",]), |
|
88 | 86 | ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop", |
|
89 | 87 | "ABEL.koppel","ABEL.loop",]), |
|
90 | 88 | ("_a*", []), |
|
91 | 89 | ("_*anka", ["__anka","__ANKA",]), |
|
92 | 90 | ("_*a*", ["__anka","__ANKA",]), |
|
93 | 91 | ] |
|
94 | 92 | for pat,res in tests: |
|
95 | 93 | res.sort() |
|
96 | a=wildcard.list_namespace(ns,"all",pat,ignore_case=True, | |
|
97 | show_all=False).keys() | |
|
98 | a.sort() | |
|
94 | a=sorted(wildcard.list_namespace(ns,"all",pat,ignore_case=True, | |
|
95 | show_all=False).keys()) | |
|
99 | 96 | self.assertEqual(a,res) |
|
100 | 97 | |
|
101 | 98 | def test_nocase_showall(self): |
|
102 | 99 | ns=root.__dict__ |
|
103 | 100 | tests=[ |
|
104 | 101 | ("a*", ["abbot","abel","ABEL","active","arna",]), |
|
105 | 102 | ("?b*.?o*",["abbot.koppel","abbot.loop","abel.koppel","abel.loop", |
|
106 | 103 | "ABEL.koppel","ABEL.loop",]), |
|
107 | 104 | ("_a*", ["_apan","_APAN"]), |
|
108 | 105 | ("_*anka", ["__anka","__ANKA",]), |
|
109 | 106 | ("_*a*", ["__anka","__ANKA","_apan","_APAN"]), |
|
110 | 107 | ] |
|
111 | 108 | for pat,res in tests: |
|
112 | 109 | res.sort() |
|
113 | a=wildcard.list_namespace(ns,"all",pat,ignore_case=True, | |
|
114 | show_all=True).keys() | |
|
110 | a=sorted(wildcard.list_namespace(ns,"all",pat,ignore_case=True, | |
|
111 | show_all=True).keys()) | |
|
115 | 112 | a.sort() |
|
116 | 113 | self.assertEqual(a,res) |
|
117 | 114 | |
|
118 | 115 | def test_dict_attributes(self): |
|
119 | 116 | """Dictionaries should be indexed by attributes, not by keys. This was |
|
120 | 117 | causing Github issue 129.""" |
|
121 | 118 | ns = {"az":{"king":55}, "pq":{1:0}} |
|
122 | 119 | tests = [ |
|
123 | 120 | ("a*", ["az"]), |
|
124 | 121 | ("az.k*", ["az.keys"]), |
|
125 | 122 | ("pq.k*", ["pq.keys"]) |
|
126 | 123 | ] |
|
127 | 124 | for pat, res in tests: |
|
128 | 125 | res.sort() |
|
129 | a = wildcard.list_namespace(ns, "all", pat, ignore_case=False, | |
|
130 | show_all=True).keys() | |
|
131 | a.sort() | |
|
126 | a = sorted(wildcard.list_namespace(ns, "all", pat, ignore_case=False, | |
|
127 | show_all=True).keys()) | |
|
132 | 128 | self.assertEqual(a, res) |
|
133 | 129 | |
|
134 | 130 | def test_dict_dir(self): |
|
135 | 131 | class A(object): |
|
136 | 132 | def __init__(self): |
|
137 | 133 | self.a = 1 |
|
138 | 134 | self.b = 2 |
|
139 | 135 | def __getattribute__(self, name): |
|
140 | 136 | if name=="a": |
|
141 | 137 | raise AttributeError |
|
142 | 138 | return object.__getattribute__(self, name) |
|
143 | 139 | |
|
144 | 140 | a = A() |
|
145 | 141 | adict = wildcard.dict_dir(a) |
|
146 | 142 | assert "a" not in adict # change to assertNotIn method in >= 2.7 |
|
147 | 143 | self.assertEqual(adict["b"], 2) |
General Comments 0
You need to be logged in to leave comments.
Login now