cape.tnakit.typeutils: Python 2/3 type-check utils¶
cape.typeutils: Python 3 type-check utils¶
This module contains convenience methods to check types of objects. For the
most part, this is a set of predefined calls to isinstance(), but with
several groups of objects. This module did contain several aspects that help
address differences between Python 2 and 3. For example, it defined the
unicode class for Python 3 by simply setting it equal to str.
As of CAPE 1.1, all Python 2 support has been removed.
- cape.tnakit.typeutils.isarray(x)¶
Check if a variable is a list or similar
Accepted types are
list,numpy.ndarray,tuple, or any subclass thereof.- Call:
>>> q = isarray(x)
- Inputs:
- x:
any Any variable
- x:
- Outputs:
- q:
True|False Whether or not x is of type
list,tuple,np.ndarray, or any subclass of these three
- q:
- Versions:
2019-03-04
@ddalle: First version
- cape.tnakit.typeutils.isfile(f)¶
Check if an object is an instance of a file-like class
This is complicated by the removal of the
filetype from Python 3. The basic class isio.IOBase, but this is not a subclass offile(or vice versa) in Python 2.- Call:
>>> q = isfile(f)
- Inputs:
- f:
any Any variable
- f:
- Outputs:
- q:
True|False Whether or not f is an instance of
file,io.IOBase, or any subclass
- q:
- Versions:
2019-12-06
@ddalle: First version2023-06-26
@jmeeroff: Removed Python2 support
- cape.tnakit.typeutils.isinstancen(x, types)¶
Special version of
isinstance()that allowsNone
- cape.tnakit.typeutils.isstr(x)¶
Check if a variable is a string (or derivative)
- Call:
>>> q = isstr(x)
- Inputs:
- x:
any Any variable
- x:
- Outputs:
- q:
True|False Whether or not x is of type
strorunicodeor any subclass thereof (and averting disaster caused by lack ofunicodeclass in Python 3+)
- q:
- Versions:
2019-03-04
@ddalle: First version2023-06-26
@jmeeroff: Removed Python2 support