pdftex; texter (graph is currently broken); data docstrings --- minor other stuff...
[PyX/mjg.git] / pyx / helper.py
blob3fca9147f6addb36e099d844ba6c855b43c728b2
1 #!/usr/bin/env python
4 # Copyright (C) 2002 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2002 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 import base
27 class nodefault: pass
30 def isstring(arg):
31 "arg is string-like (cf. python cookbook 3.2)"
32 try: arg + ''
33 except: return 0
34 return 1
37 def isnumber(arg):
38 "arg is number-like"
39 try: arg + 0
40 except: return 0
41 return 1
44 def isinteger(arg):
45 "arg is integer-like"
46 try:
47 if type(arg + 0.0) is type(arg):
48 return 0
49 return 1
50 except: return 0
53 def issequence(arg):
54 """arg is sequence-like (e.g. has a len)
55 a string is *not* considered to be a sequence"""
56 if isstring(arg): return 0
57 try: len(arg)
58 except: return 0
59 return 1
62 def ensuresequence(arg):
63 """return arg or (arg,) depending on the result of issequence,
64 None is converted to ()"""
65 if isstring(arg): return (arg,)
66 if arg is None: return ()
67 if issequence(arg): return arg
68 return (arg,)
70 def ensurelist(arg):
71 """return list(arg) or [arg] depending on the result of isequence,
72 None is converted to []"""
73 if isstring(arg): return [arg]
74 if arg is None: return []
75 if issequence(arg): return list(arg)
76 return [arg]
78 def getitemno(arg, n):
79 if issequence(arg):
80 try: return arg[n]
81 except: return None
82 else:
83 return arg
86 def issequenceofsequences(arg):
87 """check if arg has a sequence or None as it's first entry"""
88 return issequence(arg) and len(arg) and (issequence(arg[0]) or arg[0] is None)
91 def getsequenceno(arg, n):
92 """get sequence number n if arg is a sequence of sequences,
93 otherwise it gets just arg"""
94 if issequenceofsequences(arg):
95 try: return arg[n]
96 except: return None
97 else:
98 return arg
102 class AttrError(base.PyXExcept): pass
105 def checkattr(attrs, allowonce=(), allowmulti=()):
106 """checks the sequence attrs for occurencies of instances
107 the classes provided as a tuple to allowonce are allowed only once
108 the classes provided as a tuple to allowonce are allowed multiple times"""
109 hadonce = []
110 for attr in attrs:
111 for once in allowonce:
112 if isinstance(attr, once):
113 if once in hadonce:
114 raise AttrError("only a single instance of %r allowed" % once)
115 else:
116 hadonce += [once]
117 break
118 else:
119 for multi in allowmulti:
120 if isinstance(attr, multi):
121 break
122 else:
123 raise AttrError("%r not allowed" % attr)
125 def getattrs(attrs, get, default=nodefault):
126 """creates a list of instances of class get out of the sequence attrs
127 when no instances are found it returns default when set (whatever it is)
128 when no instances are found it raises AttrError when default is not set"""
129 first = 1
130 for attr in attrs:
131 if isinstance(attr, get):
132 if first:
133 result = [attr]
134 first = 0
135 else:
136 result.append(attr)
137 if first:
138 if default is nodefault:
139 raise AttrError
140 else:
141 return default
142 return result
144 def countattrs(attrs, check):
145 "count the occurancies of instances of class get out of the sequence attrs"
146 return len(getattrs(attrs, check, ()))
148 def getattr(attrs, get, default=nodefault):
149 """get the instance of class get out of the sequence attrs
150 when no instance is found it returns default when set (whatever it is)
151 when no instance is found it raises AttrError when default is not set
152 when no multiple instances are found it always raises AttrError"""
153 try:
154 result = getattrs(attrs, get)
155 except AttrError:
156 if default is nodefault:
157 raise AttrError
158 else:
159 return default
160 if len(result) > 1:
161 raise AttrError
162 return result[0]
164 def getfirstattr(attrs, get, default=nodefault):
165 """get the first instance of class get out of the sequence attrs
166 when no instances are found it returns default when set (whatever it is)
167 when no instances are found it raises AttrError when default is not set"""
168 try:
169 result = getattrs(attrs, get)
170 except AttrError:
171 if default is nodefault:
172 raise AttrError
173 else:
174 return default
175 return result[0]
177 def getlastattr(attrs, get, default=nodefault):
178 """get the last instance of class get out of the sequence attrs
179 when no instances are found it returns default when set (whatever it is)
180 when no instances are found it raises AttrError when default is not set"""
181 try:
182 result = getattrs(attrs, get)
183 except AttrError:
184 if default is nodefault:
185 raise AttrError
186 else:
187 return default
188 return result[-1]
190 def delattr(attrs, remove):
191 """create a new list of instances out of the sequence attrs
192 where all instances of class remove are removed"""
193 result = []
194 for attr in attrs:
195 if not isinstance(attr, remove):
196 result.append(attr)
197 return result
200 if __name__=="__main__":
201 class a: pass
202 class b: pass
203 class c(b): pass
204 class A: pass
205 class B: pass
206 class C(B): pass
207 checkattr((a(), A(), A()), (a, b), (A, B))
208 checkattr((c(), A(), A()), (a, b), (A, B))
209 try:
210 checkattr((a(), A(), A(), a()), (a, b), (A, B))
211 print "error"
212 except AttrError: pass
213 try:
214 checkattr((c(), A(), A(), c()), (a, b), (A, B))
215 print "error"
216 except AttrError: pass
217 x1, x2 = a(), a()
218 if getattrs((x1, A(), A()), a) != [x1]:
219 print "error"
220 if getattrs((x1, A(), A(), x2), a) != [x1, x2]:
221 print "error"
222 if getattr((x1, A(), A()), a) != x1:
223 print "error"
224 try:
225 getattr((x1, A(), A(), x2), a)
226 print "error"
227 except AttrError: pass
228 if getfirstattr((x1, A(), A(), x2), a) != x1:
229 print "error"
230 if getlastattr((x1, A(), A(), x2), a) != x2:
231 print "error"
232 if getattr((x1, A(), A()), a, x2) != x1:
233 print "error"
234 if getattr((A(), A()), a, x2) != x2:
235 print "error"