rename context->currentpoint to reflect what has become now
[PyX/mjg.git] / pyx / helper.py
blobea7549c17a3f448e25243dae766809d71b2b810d
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2002-2004 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 class nodefault: pass
28 def isstring(arg):
29 "arg is string-like (cf. python cookbook 3.2)"
30 try: arg + ''
31 except: return 0
32 return 1
35 def isnumber(arg):
36 "arg is number-like"
37 try: arg + 0
38 except: return 0
39 return 1
42 def isinteger(arg):
43 "arg is integer-like"
44 try:
45 if type(arg + 0.0) is type(arg):
46 return 0
47 return 1
48 except: return 0
51 def issequence(arg):
52 """arg is sequence-like (e.g. has a len)
53 a string is *not* considered to be a sequence"""
54 if isstring(arg): return 0
55 try: len(arg)
56 except: return 0
57 return 1
60 def ensuresequence(arg):
61 """return arg or (arg,) depending on the result of issequence,
62 None is converted to ()"""
63 if isstring(arg): return (arg,)
64 if arg is None: return ()
65 if issequence(arg): return arg
66 return (arg,)
69 def ensurelist(arg):
70 """return list(arg) or [arg] depending on the result of isequence,
71 None is converted to []"""
72 if isstring(arg): return [arg]
73 if arg is None: return []
74 if issequence(arg): return list(arg)
75 return [arg]
77 def getitemno(arg, n):
78 """get item number n if arg is a sequence (when the sequence
79 is not long enough, None is returned), otherweise arg is
80 returned"""
81 if issequence(arg):
82 try: return arg[n]
83 except: return None
84 else:
85 return arg
88 def issequenceofsequences(arg):
89 """check if arg has a sequence or None as it's first entry"""
90 return issequence(arg) and len(arg) and (issequence(arg[0]) or arg[0] is None)
93 def getsequenceno(arg, n):
94 """get sequence number n if arg is a sequence of sequences (when
95 the sequence is not long enough, None is returned), otherwise
96 arg is returned"""
97 if issequenceofsequences(arg):
98 try: return arg[n]
99 except: return None
100 else:
101 return arg