WikiStart: util.py

File util.py, 1.1 kB (added by jbosch, 1 year ago)

support file for geometry.py

Line 
1 class AttributeDict(object):
2    
3     def __init__(self,obj):
4         self.obj = obj
5
6     def __getitem__(self,key):
7         return getattr(self.obj,key)
8    
9     def __setitem__(self,key,value):
10         setattr(self.obj,key,value)
11
12     def __delitem__(self,key):
13         delattr(self.obj,key)
14
15     def __contains__(self,key):
16         return hasattr(self.obj,key)
17
18     def keys(self):
19         return dir(self.obj)
20
21     def values(self):
22         return [getattr(self.obj,key) for key in self.keys()]
23
24     def items(self):
25         return [(key,getattr(self.obj,key)) for key in self.keys()]
26
27     def __len__(self):
28         return len(dir(self.obj))
29
30 class BoundClass(object):
31     """Descriptor that wraps a class that can be initialized
32     by an instance of the owner class as an attribute.
33
34     Example:
35
36     class Owner(object):
37
38         class Nested(object):
39             def __init__(self,owner):
40                 self.owner = owner
41         nested = BoundClass(Nested)
42     """
43    
44     def __init__(self,cls,doc=None):
45         self.__cls = cls
46         if doc is None: doc = cls.__doc__
47
48     def __get__(self,instance,type=None):
49         return self.__cls(instance)