attrdict.py (6652:f24b06320444) | attrdict.py (6653:9e27313312e6) |
---|---|
1# Copyright (c) 2006 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 44 unchanged lines hidden (view full) --- 53 54class multiattrdict(attrdict): 55 """Wrap attrdict so that nested attribute accesses automatically create 56 nested dictionaries.""" 57 def __getattr__(self, attr): 58 try: 59 return super(multiattrdict, self).__getattr__(attr) 60 except AttributeError: | 1# Copyright (c) 2006 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 44 unchanged lines hidden (view full) --- 53 54class multiattrdict(attrdict): 55 """Wrap attrdict so that nested attribute accesses automatically create 56 nested dictionaries.""" 57 def __getattr__(self, attr): 58 try: 59 return super(multiattrdict, self).__getattr__(attr) 60 except AttributeError: |
61 d = optiondict() | 61 d = multiattrdict() |
62 setattr(self, attr, d) 63 return d 64 65class optiondict(attrdict): 66 """Modify attrdict so that a missing attribute just returns None""" 67 def __getattr__(self, attr): 68 try: 69 return super(optiondict, self).__getattr__(attr) --- 11 unchanged lines hidden (view full) --- 81 82 print 83 84 del x['y'] 85 del x.z 86 print dir(x) 87 print(x) 88 | 62 setattr(self, attr, d) 63 return d 64 65class optiondict(attrdict): 66 """Modify attrdict so that a missing attribute just returns None""" 67 def __getattr__(self, attr): 68 try: 69 return super(optiondict, self).__getattr__(attr) --- 11 unchanged lines hidden (view full) --- 81 82 print 83 84 del x['y'] 85 del x.z 86 print dir(x) 87 print(x) 88 |
89 print 90 print "multiattrdict" |
|
89 x = multiattrdict() | 91 x = multiattrdict() |
92 x.x.x.x = 9 |
|
90 x.y.z = 9 91 print x 92 print x.y 93 print x.y.z | 93 x.y.z = 9 94 print x 95 print x.y 96 print x.y.z |
97 print x.z.z |
|