-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jizong
committed
Aug 28, 2019
1 parent
4f1a9b7
commit f9e3561
Showing
5 changed files
with
263 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .general import * | ||
from .type_check import * | ||
from .warnings import _warnings | ||
from .yaml_parser import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
import collections | ||
import numbers | ||
import sys | ||
import types | ||
|
||
import numpy as np | ||
import six | ||
|
||
|
||
def is_np_array(val): | ||
""" | ||
Checks whether a variable is a numpy array. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
out : bool | ||
True if the variable is a numpy array. Otherwise False. | ||
""" | ||
# using np.generic here via isinstance(val, (np.ndarray, np.generic)) seems to also fire for scalar numpy values | ||
# even though those are not arrays | ||
return isinstance(val, np.ndarray) | ||
|
||
|
||
def is_np_scalar(val): | ||
""" | ||
Checks whether a variable is a numpy scalar. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
out : bool | ||
True if the variable is a numpy scalar. Otherwise False. | ||
""" | ||
# Note that isscalar() alone also fires for thinks like python strings | ||
# or booleans. | ||
# The isscalar() was added to make this function not fire for non-scalar | ||
# numpy types. Not sure if it is necessary. | ||
return isinstance(val, np.generic) and np.isscalar(val) | ||
|
||
|
||
def is_single_integer(val): | ||
""" | ||
Checks whether a variable is an integer. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True if the variable is an integer. Otherwise False. | ||
""" | ||
return isinstance(val, numbers.Integral) and not isinstance(val, bool) | ||
|
||
|
||
def is_single_float(val): | ||
""" | ||
Checks whether a variable is a float. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True if the variable is a float. Otherwise False. | ||
""" | ||
return isinstance(val, numbers.Real) and not is_single_integer(val) and not isinstance(val, bool) | ||
|
||
|
||
def is_single_number(val): | ||
""" | ||
Checks whether a variable is a number, i.e. an integer or float. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True if the variable is a number. Otherwise False. | ||
""" | ||
return is_single_integer(val) or is_single_float(val) | ||
|
||
|
||
def is_iterable(val): | ||
""" | ||
Checks whether a variable is iterable. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True if the variable is an iterable. Otherwise False. | ||
""" | ||
return isinstance(val, collections.Iterable) | ||
|
||
|
||
# TODO convert to is_single_string() or rename is_single_integer/float/number() | ||
def is_string(val): | ||
""" | ||
Checks whether a variable is a string. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True if the variable is a string. Otherwise False. | ||
""" | ||
return isinstance(val, six.string_types) | ||
|
||
|
||
def is_single_bool(val): | ||
""" | ||
Checks whether a variable is a boolean. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True if the variable is a boolean. Otherwise False. | ||
""" | ||
return type(val) == type(True) | ||
|
||
|
||
def is_integer_array(val): | ||
""" | ||
Checks whether a variable is a numpy integer array. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True if the variable is a numpy integer array. Otherwise False. | ||
""" | ||
return is_np_array(val) and issubclass(val.dtype.type, np.integer) | ||
|
||
|
||
def is_float_array(val): | ||
""" | ||
Checks whether a variable is a numpy float array. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True if the variable is a numpy float array. Otherwise False. | ||
""" | ||
return is_np_array(val) and issubclass(val.dtype.type, np.floating) | ||
|
||
|
||
def is_callable(val): | ||
""" | ||
Checks whether a variable is a callable, e.g. a function. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True if the variable is a callable. Otherwise False. | ||
""" | ||
# python 3.x with x <= 2 does not support callable(), apparently | ||
if sys.version_info[0] == 3 and sys.version_info[1] <= 2: | ||
return hasattr(val, '__call__') | ||
else: | ||
return callable(val) | ||
|
||
|
||
def is_generator(val): | ||
""" | ||
Checks whether a variable is a generator. | ||
Parameters | ||
---------- | ||
val | ||
The variable to check. | ||
Returns | ||
------- | ||
bool | ||
True is the variable is a generator. Otherwise False. | ||
""" | ||
return isinstance(val, types.GeneratorType) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters