tools.py

Mon, 04 Feb 2008 19:49:48 +0100

author
David Douard <david.douard@logilab.fr>
date
Mon, 04 Feb 2008 19:49:48 +0100
changeset 39
8becd52c2171
parent 36
cb4124e3e75b
permissions
-rw-r--r--

refactoring in progress

36
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
1 #
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
2 """
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
3 Helper functions and classes
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
4 """
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
5 import re
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
6
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
7 def str_num_cmp(s1,s2):
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
8 """
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
9 string comparator function that will put 'toto_10' after 'toto_2'
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
10 Also works for strings like 'toto_1_et_23er25'.
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
11 """
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
12 r = re.compile(r'((?<=\d)\D+|(?<=\D)\d+)')
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
13 r1= r.split(s1)
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
14 r2= r.split(s2)
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
15 r1=[not x.isdigit() and x or int(x) for x in r1 if x!='']
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
16 r2=[not x.isdigit() and x or int(x) for x in r2 if x!='']
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
17 return cmp(r1,r2)
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
18
cb4124e3e75b some improvements; can now give a bunch of files to open on cmd line; removed print statements
David Douard <david.douard@logilab.fr>
parents: 35
diff changeset
19
35
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20 class AbstractRegister(object):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21 _instance = None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
22 _registered_type = None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
23 def __new__(cls):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
24 # implements a singleton
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
25 if cls._instance is None:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
26 #debug('Instanciating %s', cls.__name__)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
27 cls._instance = super(AbstractRegister, cls).__new__(cls)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
28 cls._instance.registered = {}
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
29 cls._instance.accepts = set()
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
30 return cls._instance
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
31
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
32 def add(self, cls):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
33 assert issubclass(cls, self._registered_type)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
34 if cls is self._registered_type:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
35 return
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
36 if cls._accepts is None:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
37 return
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
38 #assert isinstance(cls._accepts, (basestring, tuple))
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
39
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
40 #debug("Registerered %s for %s", cls.__name__)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
41 if not isinstance(cls._accepts, tuple):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
42 cls._accepts = (cls._accepts,)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
43 for key in cls._accepts:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
44 key = self._get_typ(key)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
45 #debug(" new key = %s", key)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
46 self.registered.setdefault(key, []).append(cls)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
47 self.accepts.add(key)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
48
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
49 def _get_typ(self, typ):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
50 if not isinstance(typ, basestring):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
51 if not isinstance(typ, type):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
52 return None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53 typ = typ.__name__
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
54 return typ
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
55
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
56 def __contains__(self, val):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
57 val = self._get_typ(val)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
58 return val in self.accepts
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
59
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
60 def get_class(self, typ):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
61 item = typ
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
62 if not isinstance(typ, type):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
63 typ = typ.__class__
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
64 name = typ.__name__
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
65 #debug("Looking a widget for %s", typ.__name__)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
66 orig_typ = typ
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
67 while typ is not None:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
68 if typ.__name__ in self.registered:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
69 for w in self.registered[typ.__name__]:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
70 if w._filter is None or w._filter(item):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
71 #debug("Widget for %s is %s", typ.__name__, w)
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
72 return w#self.registered[typ.__name__]
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
73 if typ.__bases__:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
74 typ = typ.__bases__[0]
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
75 if typ == object:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
76 typ = None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
77 elif typ.__name__ in ("ObjectItem_", "AbstractListItem_"):
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
78 typ = orig_typ._type
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
79 else:
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
80 typ = None
3b7a38af5c42 Refactored the preference system for the qt gpib plotter app. One can now set the colors for plotting pens.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
81 raise ValueError('No registered class for %s' % (name))

mercurial