use listings.sty
[PyX/mjg.git] / pyx / helper.py
blob8965594e9a2b8aee131e80f56420da7f4ab2c995
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 class _nodefault: pass
27 def _isstring(arg):
28 "arg is string-like (cf. python cookbook 3.2)"
29 try: arg + ''
30 except: return None
31 return 1
34 def _isnumber(arg):
35 "arg is number-like"
36 try: arg + 0
37 except: return None
38 return 1
41 def _isinteger(arg):
42 "arg is integer-like"
43 try:
44 if type(arg + 0.0) is type(arg):
45 return None
46 return 1
47 except: return None
50 def _issequence(arg):
51 """arg is sequence-like (e.g. has a len)
52 a string is *not* considered to be a sequence"""
53 if _isstring(arg): return None
54 try: len(arg)
55 except: return None
56 return 1
59 def _ensuresequence(arg):
60 """return arg or (arg,) depending on the result of _issequence,
61 None is converted to ()"""
62 if _isstring(arg): return (arg,)
63 if arg is None: return ()
64 if _issequence(arg): return arg
65 return (arg,)
68 def _getitemno(arg, n):
69 if _issequence(arg):
70 try: return arg[n]
71 except: return None
72 else:
73 return arg
76 def _issequenceofsequences(arg):
77 """check if arg has a sequence or None as it's first entry"""
78 return _issequence(arg) and len(arg) and (_issequence(arg[0]) or arg[0] is None)
81 def _getsequenceno(arg, n):
82 """get sequence number n if arg is a sequence of sequences,
83 otherwise it gets just arg"""
84 if _issequenceofsequences(arg):
85 try: return arg[n]
86 except: return None
87 else:
88 return arg