-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEntityChecks.py
146 lines (124 loc) · 5.79 KB
/
EntityChecks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
""":module EntityChecks: Module for entity checks. """
##########################################################################
# #
# Copyright (C) 2015-2018 Carsten Fortmann-Grote #
# Contact: Carsten Fortmann-Grote <carsten.grote@xfel.eu> #
# #
# This file is part of simex_platform. #
# simex_platform is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# simex_platform is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# Include needed directories in sys.path. #
# #
##########################################################################
#from SimEx import PhysicalQuantity
def checkAndSetInstance(cls, var=None, default=None):
"""
Utility to check if the passed object is an instance of the given class (or derived class).
:param cls : The class against which to check.
:param var : The object to check.
:param default : The default to use if no var is given.
:return : The checked object or default.
:raises TypeError: if var is not an instance of cls or a derived class.
"""
if var is None:
if default is None:
return None
elif not isinstance(default, cls):
raise TypeError("The default is not of correct type.")
else: return default
elif not isinstance(var, cls):
raise TypeError("The parameter 'var' is not of correct type.")
return var
def checkAndSetInteger(var=None, default=None):
"""
Utility to check if the passed value is a positive integer.
@param var : The object to check.
@param default : The default to use if no var is given.
@return : The checked object or default.
@throw : TypeError if check fails.
"""
if var is None:
if not (isinstance( default, int) ):
raise TypeError("The default must be an integer.")
return default
if not isinstance(var, int):
raise TypeError("The parameter must be of type integer.")
return var
def checkAndSetPositiveInteger(var=None, default=None):
"""
Utility to check if the passed value is a positive integer.
@param var : The object to check.
@param default : The default to use if no var is given.
@return : The checked object or default.
@throw : TypeError if check fails.
"""
if var is None:
if not (isinstance( default, int) and default > 0):
raise TypeError("The default must be a positive integer.")
return default
if not isinstance(var, int):
raise TypeError("The parameter must be of type integer.")
if var <= 0:
raise TypeError("The parameter must be a positive integer.")
return var
def checkAndSetNonNegativeInteger(var=None, default=None):
"""
Utility to check if the passed value is a non-negative integer.
@param var : The object to check.
@param default : The default to use if no var is given.
@return : The checked object or default.
@throw : TypeError if check fails.
"""
if var is None:
if not (isinstance( default, int) and default >= 0):
raise TypeError("The default must be a non-negative integer.")
return default
if not isinstance(var, int):
raise TypeError("The parameter must be of type integer.")
if var < 0:
raise TypeError("The parameter must be a non-negative integer.")
return var
def checkAndSetNumber(var=None, default=None):
""" Check if input is a numerical type. """
if var is not None:
if not isinstance( var, (int, float) ):
raise TypeError("The given value must be a numerical type, got %s" % (type(var)))
return var
if not isinstance( default, (int, float)):
raise TypeError("The default value must be a numerical type, got %s" % (type(default)))
return default
def checkAndSetIterable(var=None, default=None):
""" Check if input is iterable (list, tuple, or array type). """
if not hasattr( var, "__iter__" ):
raise AttributeError( "The parameter must be iterable (e.g. a tuple, list, or numpy.array).")
return var
"""
def checkAndSetPhysicalQuantity(var, default, unit):
#Check if input is a PhysicalQuantity and has the correct unit.
if var is not None:
if not isinstance(var, PhysicalQuantity):
raise TypeError("%s is not a PhysicalQuantity." % (repr(var)) )
if not var.units == unit.units:
raise TypeError("Incorrect unit (%s), expected %s." % (var.units, unit.units) )
else:
if default is not None:
if isinstance(default, PhysicalQuantity):
if default.units == unit.units:
return default
else:
return default.to(unit.units)
else:
return default*unit
return default
return var
"""