From 3243b041ad2f906c4b8e7c7782065bae253869f0 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Sat, 20 Feb 2010 18:09:00 +0000 Subject: ENH: Add some tools to numpy.compat --- numpy/compat/_inspect.py | 2 +- numpy/compat/py3k.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'numpy/compat') diff --git a/numpy/compat/_inspect.py b/numpy/compat/_inspect.py index 8be0b7696..612d1e147 100644 --- a/numpy/compat/_inspect.py +++ b/numpy/compat/_inspect.py @@ -7,7 +7,7 @@ no overhead. import types -__all__ = ['getarspec', 'formatargspec'] +__all__ = ['getargspec', 'formatargspec'] # ----------------------------------------------------------- type-checking def ismethod(object): diff --git a/numpy/compat/py3k.py b/numpy/compat/py3k.py index b6113b661..534ae4d29 100644 --- a/numpy/compat/py3k.py +++ b/numpy/compat/py3k.py @@ -4,23 +4,25 @@ Python 3 compatibility tools. """ __all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar', - 'asunicode'] + 'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested'] import sys if sys.version_info[0] >= 3: import io bytes = bytes + unicode = str + asunicode = str def asbytes(s): if isinstance(s, bytes): return s return s.encode('iso-8859-1') - asunicode = str def isfileobj(f): return isinstance(f, io.IOBase) strchar = 'U' else: bytes = str + unicode = unicode asbytes = str strchar = 'S' def isfileobj(f): @@ -33,3 +35,14 @@ else: def getexception(): return sys.exc_info()[1] +def asbytes_nested(x): + if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)): + return [asbytes_nested(y) for y in x] + else: + return asbytes(x) + +def asunicode_nested(x): + if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)): + return [asunicode_nested(y) for y in x] + else: + return asunicode(x) -- cgit v1.2.1