Package advene ::
Package util
|
|
1 from inspect import stack
2
4 """
5 This function must be called in the body of a class.
6 It looks for _get_X, _set_X and _del_X to create the X property.
7 """
8 loc = stack()[1][0].f_locals
9 getter = loc.get("_get_%s" % name)
10 setter = loc.get("_set_%s" % name)
11 deller = loc.get("_del_%s" % name)
12 doc = getter.__doc__
13 loc[name] = property(getter, setter, deller, doc)
14
16 """
17 This function must be called in the body of a subclass of tuple.
18 It creates methods __new__, __repr__, and as many methods as the names
19 provided, returning the corresponding element of the tuple.
20
21 e.g.
22
23 class MyPair(tuple):
24 "a lisp-like pair"
25 make_tuple_methods("car", "cdr")
26 """
27 loc = stack()[1][0].f_locals
28
29 def __new__(cls, *values):
30 if len(values) != len(names):
31 raise TypeError("%s() takes exactly %s argument(s) (%s given)"
32 % (cls.__name__, len(names), len(values)))
33 return tuple.__new__(cls, values)
34 loc["__new__"] = __new__
35
36 def __repr__(self):
37 return "%s%s" % (self.__class__.__name__, tuple.__repr__(self))
38 loc["__repr__"] = __repr__
39
40 for i,n in enumerate(names):
41 loc[n] = property(lambda self, _i=i: self[_i])
42
44 """
45 This callable accepts a dict and return a read only view of that dict.
46 """
47
50
53
55 return key in self.__d
56
59
62
64 return "dict_view(%r)" % (self.__d,)
65
66 - def get(self, key, default=None):
68
71
74
77
79 """
80 This callable accepts a dict and a list (supposed to contain the same
81 elements) and return a mixt read-only view of them.
82 """
84 self.__l = l
85 self.__d = d
86
89
92
95
98
100 return "smart_list_view(%r)" % (self.__d,)
101
102 - def get(self, key, default=None):
104
106 for i in self.__l:
107 yield i.id
108
110 return iter(self.__l)
111
113 for i in self._l:
114 yield i.id, i
115