##// END OF EJS Templates
Updated ipstruct to fix doctest failures.
Fernando Perez -
Show More
@@ -55,24 +55,32 b' class Struct:'
55 Define a dictionary and initialize both with dict and k=v pairs:
55 Define a dictionary and initialize both with dict and k=v pairs:
56 >>> d={'a':1,'b':2}
56 >>> d={'a':1,'b':2}
57 >>> s=Struct(d,hi=10,ho=20)
57 >>> s=Struct(d,hi=10,ho=20)
58
58 The return of __repr__ can be used to create a new instance:
59 The return of __repr__ can be used to create a new instance:
59 >>> s
60 >>> s
60 Struct({'ho': 20, 'b': 2, 'hi': 10, 'a': 1})
61 Struct({'__allownew': True, 'a': 1, 'b': 2, 'hi': 10, 'ho': 20})
62
63 Note: the special '__allownew' key is used for internal purposes.
64
61 __str__ (called by print) shows it's not quite a regular dictionary:
65 __str__ (called by print) shows it's not quite a regular dictionary:
62 >>> print s
66 >>> print s
63 Struct {a: 1, b: 2, hi: 10, ho: 20}
67 Struct({'__allownew': True, 'a': 1, 'b': 2, 'hi': 10, 'ho': 20})
68
64 Access by explicitly named key with dot notation:
69 Access by explicitly named key with dot notation:
65 >>> s.a
70 >>> s.a
66 1
71 1
72
67 Or like a dictionary:
73 Or like a dictionary:
68 >>> s['a']
74 >>> s['a']
69 1
75 1
76
70 If you want a variable to hold the key value, only dictionary access works:
77 If you want a variable to hold the key value, only dictionary access works:
71 >>> key='hi'
78 >>> key='hi'
72 >>> s.key
79 >>> s.key
73 Traceback (most recent call last):
80 Traceback (most recent call last):
74 File "<stdin>", line 1, in ?
81 File "<stdin>", line 1, in ?
75 AttributeError: Struct instance has no attribute 'key'
82 AttributeError: Struct instance has no attribute 'key'
83
76 >>> s[key]
84 >>> s[key]
77 10
85 10
78
86
@@ -81,13 +89,16 b' class Struct:'
81 accessed using the dictionary syntax. Again, an example:
89 accessed using the dictionary syntax. Again, an example:
82
90
83 This doesn't work:
91 This doesn't work:
84 >>> s=Struct(4='hi')
92 >>> s=Struct(4='hi') #doctest: +IGNORE_EXCEPTION_DETAIL
93 Traceback (most recent call last):
94 ...
85 SyntaxError: keyword can't be an expression
95 SyntaxError: keyword can't be an expression
96
86 But this does:
97 But this does:
87 >>> s=Struct()
98 >>> s=Struct()
88 >>> s[4]='hi'
99 >>> s[4]='hi'
89 >>> s
100 >>> s
90 Struct({4: 'hi'})
101 Struct({4: 'hi', '__allownew': True})
91 >>> s[4]
102 >>> s[4]
92 'hi'
103 'hi'
93 """
104 """
@@ -318,7 +329,8 b' class Struct:'
318 if __conflict_solve:
329 if __conflict_solve:
319 inv_conflict_solve_user = __conflict_solve.copy()
330 inv_conflict_solve_user = __conflict_solve.copy()
320 for name, func in [('preserve',preserve), ('update',update),
331 for name, func in [('preserve',preserve), ('update',update),
321 ('add',add), ('add_flip',add_flip), ('add_s',add_s)]:
332 ('add',add), ('add_flip',add_flip),
333 ('add_s',add_s)]:
322 if name in inv_conflict_solve_user.keys():
334 if name in inv_conflict_solve_user.keys():
323 inv_conflict_solve_user[func] = inv_conflict_solve_user[name]
335 inv_conflict_solve_user[func] = inv_conflict_solve_user[name]
324 del inv_conflict_solve_user[name]
336 del inv_conflict_solve_user[name]
@@ -369,14 +381,14 b' class Struct:'
369 return ret
381 return ret
370
382
371 def get(self,attr,val=None):
383 def get(self,attr,val=None):
372 """S.get(k[,d]) -> S[k] if S.has_key(k), else d. d defaults to None."""
384 """S.get(k[,d]) -> S[k] if k in S, else d. d defaults to None."""
373 try:
385 try:
374 return self[attr]
386 return self[attr]
375 except KeyError:
387 except KeyError:
376 return val
388 return val
377
389
378 def setdefault(self,attr,val=None):
390 def setdefault(self,attr,val=None):
379 """S.setdefault(k[,d]) -> S.get(k,d), also set S[k]=d if not S.has_key(k)"""
391 """S.setdefault(k[,d]) -> S.get(k,d), also set S[k]=d if k not in S"""
380 if not self.has_key(attr):
392 if not self.has_key(attr):
381 self[attr] = val
393 self[attr] = val
382 return self.get(attr,val)
394 return self.get(attr,val)
@@ -384,8 +396,8 b' class Struct:'
384 def allow_new_attr(self, allow = True):
396 def allow_new_attr(self, allow = True):
385 """ Set whether new attributes can be created inside struct
397 """ Set whether new attributes can be created inside struct
386
398
387 This can be used to catch typos by verifying that the attribute user tries to
399 This can be used to catch typos by verifying that the attribute user
388 change already exists in this Struct.
400 tries to change already exists in this Struct.
389 """
401 """
390 self['__allownew'] = allow
402 self['__allownew'] = allow
391
403
General Comments 0
You need to be logged in to leave comments. Login now