1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Special module for checking types."""
24
25
26 from numpy import complex64, complex128, float32, float64, int16, int32
27 try:
28 from numpy import complex256
29 except ImportError:
30 complex256 = complex128
31 try:
32 from numpy import float16
33 except ImportError:
34 float16 = float32
35 try:
36 from numpy import float128
37 except ImportError:
38 float128 = float64
39 try:
40 from numpy import int8
41 except ImportError:
42 int8 = int16
43 try:
44 from numpy import int64
45 except ImportError:
46 int64 = int32
47
48
49
50 from lib.compat import IOBase, StringIO, unicode
51
52
54 """Check if the given number is a Python or numpy complex.
55
56 @param num: The number to check.
57 @type num: anything.
58 @return: True if the number is a complex, False otherwise.
59 @rtype: bool
60 """
61
62
63 if isinstance(num, complex):
64 return True
65
66
67 if isinstance(num, complex64):
68 return True
69 if isinstance(num, complex128):
70 return True
71 if isinstance(num, complex256):
72 return True
73
74
75 return False
76
77
79 """Check if the given Python object is a file.
80
81 @param obj: The Python object.
82 @type obj: anything
83 @return: True if the object is a file, False otherwise.
84 @rtype: bool
85 """
86
87
88 if IOBase != None:
89 return isinstance(obj, IOBase)
90
91
92 else:
93 return isinstance(obj, file)
94
95
97 """Check if the given Python object can operate as a readable file.
98
99 @param obj: The Python object.
100 @type obj: anything
101 @return: True if the object can operate as a readable file, False otherwise.
102 @rtype: bool
103 """
104
105
106 if hasattr(obj, 'readable'):
107 return obj.readable()
108
109
110 if hasattr(obj, 'mode'):
111 if obj.mode == 'r':
112 return True
113 else:
114 return False
115
116
117 if repr(type(obj)) == "<type 'cStringIO.StringO'>":
118 return True
119 try:
120 if isinstance(obj, StringIO):
121 return True
122 except TypeError:
123 pass
124
125
126 dir_obj = dir(obj)
127 for method in ['read', 'readline', 'readlines']:
128 if method not in dir_obj:
129 return False
130
131
132 return True
133
134
136 """Check if the given Python object can operate as both a readable and writable file.
137
138 @param obj: The Python object.
139 @type obj: anything
140 @return: True if the object can operate as both a readable and writable file, False otherwise.
141 @rtype: bool
142 """
143
144
145 if hasattr(obj, 'readable') and hasattr(obj, 'writable'):
146 return obj.readable() and obj.writable()
147
148
149 if hasattr(obj, 'mode'):
150 if obj.mode == 'rw':
151 return True
152 else:
153 return False
154
155
156 if repr(type(obj)) == "<type 'cStringIO.StringO'>":
157 return True
158 try:
159 if isinstance(obj, StringIO):
160 return True
161 except TypeError:
162 pass
163
164
165 return False
166
167
169 """Check if the given Python object can operate as a writable file.
170
171 @param obj: The Python object.
172 @type obj: anything
173 @return: True if the object can operate as a writable file, False otherwise.
174 @rtype: bool
175 """
176
177
178 if hasattr(obj, 'writable'):
179 return obj.writable()
180
181
182 if hasattr(obj, 'mode'):
183 if obj.mode == 'w':
184 return True
185 else:
186 return False
187
188
189 if repr(type(obj)) == "<type 'cStringIO.StringO'>":
190 return True
191 try:
192 if isinstance(obj, StringIO):
193 return True
194 except TypeError:
195 pass
196
197
198 dir_obj = dir(obj)
199 for method in ['write', 'writelines']:
200 if method not in dir_obj:
201 return False
202
203
204 return True
205
206
208 """Check if the given number is a Python or numpy float.
209
210 @param num: The number to check.
211 @type num: anything.
212 @return: True if the number is a float, False otherwise.
213 @rtype: bool
214 """
215
216
217 if isinstance(num, float):
218 return True
219
220
221 if isinstance(num, float16):
222 return True
223 if isinstance(num, float32):
224 return True
225 if isinstance(num, float64):
226 return True
227 if isinstance(num, float128):
228 return True
229
230
231 return False
232
233
235 """Check if the given number is a Python or numpy int.
236
237 @param num: The number to check.
238 @type num: anything.
239 @return: True if the number is a int, False otherwise.
240 @rtype: bool
241 """
242
243
244 if isinstance(num, int):
245 return True
246
247
248 if isinstance(num, int8):
249 return True
250 if isinstance(num, int16):
251 return True
252 if isinstance(num, int32):
253 return True
254 if isinstance(num, int64):
255 return True
256
257
258 return False
259
260
262 """Check if the given value is a Python list.
263
264 @param val: The value to check.
265 @type val: anything.
266 @return: True if the value is a list, False otherwise.
267 @rtype: bool
268 """
269
270
271 if not isinstance(val, list):
272 return False
273
274
275 return True
276
277
279 """Check if the given value is a Python list of lists.
280
281 @param val: The value to check.
282 @type val: anything.
283 @return: True if the value is a list of lists, False otherwise.
284 @rtype: bool
285 """
286
287
288 if not isinstance(val, list):
289 return False
290
291
292 if not isinstance(val[0], list):
293 return False
294
295
296 return True
297
298
300 """Check if the given number is a Python or numpy int or float.
301
302 @param num: The number to check.
303 @type num: anything.
304 @return: True if the number is an int or float, False otherwise.
305 @rtype: bool
306 """
307
308
309 if is_float(num):
310 return True
311
312
313 if is_int(num):
314 return True
315
316
317 return False
318
319
321 """Check if the given Python object is a unicode string.
322
323 @param obj: The Python object.
324 @type obj: anything
325 @return: True if the object is a unicode string, False otherwise.
326 @rtype: bool
327 """
328
329
330 return isinstance(obj, unicode)
331