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