1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Temporary module for allowing relax to support both Python 2 and 3."""
24
25
26 try:
27 import platform
28 except ImportError:
29 print("The platform module cannot be imported. For Python <= 2.2, try copying the platform.py file from http://hg.python.org/cpython/file/2.3/Lib/platform.py into your lib/pythonX.X/ directory.")
30 raise
31
32
33 try:
34 import bz2
35 from bz2 import BZ2File
36 bz2_module = True
37 except ImportError:
38 BZ2File = object
39 bz2_module = False
40 message = sys.exc_info()[1]
41 bz2_module_message = message.args[0]
42 try:
43 import gzip
44 gzip_module = True
45 except ImportError:
46 gzip = object
47 gzip_module = False
48 message = sys.exc_info()[1]
49 gzip_module_message = message.args[0]
50 try:
51 import io
52 io_module = True
53 from io import IOBase
54 except ImportError:
55 io_module = False
56 IOBase = None
57 import os
58 import platform
59 import sys
60 import threading
61
62
63
64 SYSTEM = platform.uname()[0]
65 if SYSTEM == 'Microsoft':
66 SYSTEM == 'Windows'
67
68
69 PY_VERSION = sys.version_info[0]
70
71
72
73
74
75
76 if PY_VERSION == 2:
77 import __builtin__ as builtins
78 else:
79 import builtins
80
81
82 if PY_VERSION == 2:
83 import Queue as queue
84 else:
85 import queue
86
87
88 if PY_VERSION == 2:
89 from Queue import Queue as Queue2
90 else:
91 from queue import Queue as Queue3
92 Queue2 = Queue3
93
94
95 if PY_VERSION == 2:
96 from cStringIO import StringIO
97 elif io_module:
98 from io import StringIO
99 else:
100 StringIO = None
101
102
103 try:
104 from unittest import TextTestResult
105 except ImportError:
106 from unittest import _TextTestResult as TextTestResult
107
108
109 if PY_VERSION == 2:
110 import cPickle as pickle
111 else:
112 import pickle
113
114
115 import numpy
116 try:
117 numpy.linalg.norm(numpy.ones((3, 3)), axis=1)
118 numpy_norm_axis = True
119 except:
120 numpy_norm_axis = False
121
122
124 """Abstract the numerous ways BZ2 files are handled in Python.
125
126 @param file: The file path to open.
127 @type file: str
128 @keyword mode: The mode to open the file with. Only the values of 'r' and 'w' for reading and writing respectively are supported.
129 @type mode: str
130 @return: The bzip2 file object.
131 @rtype: file object
132 """
133
134
135 if mode not in ['r', 'w']:
136 raise RelaxError("The mode '%s' must be one or 'r' or 'w'." % mode)
137
138
139 if not bz2_module:
140 if mode == 'r':
141 raise RelaxError("Cannot open the file %s, try uncompressing first. %s." % (file, bz2_module_message))
142 else:
143 raise RelaxError("Cannot create bzip2 file %s, the bz2 Python module cannot be found." % file)
144
145
146 if mode == 'r':
147
148 if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
149 file_obj = bz2.open(file, 't')
150
151
152 elif sys.version_info[0] == 3 and sys.version_info[1] < 3:
153 file_obj = io.TextIOWrapper(Bzip2Fixed(file, 'r'))
154
155
156 else:
157 file_obj = bz2.BZ2File(file, 'r')
158
159
160 elif mode == 'w':
161
162 if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
163 file_obj = bz2.open(file, 'wt')
164
165
166 elif sys.version_info[0] == 3 and sys.version_info[1] < 3:
167 file_obj = io.TextIOWrapper(Bzip2Fixed(file, 'w'))
168
169
170 else:
171 file_obj = bz2.BZ2File(file, 'w')
172
173
174 return file_obj
175
176
178 """Abstract the numerous ways gzipped files are handled in Python.
179
180 @param file: The file path to open.
181 @type file: str
182 @keyword mode: The mode to open the file with. Only the values of 'r' and 'w' for reading and writing respectively are supported.
183 @type mode: str
184 @return: The gzipped file object.
185 @rtype: file object
186 """
187
188
189 if mode not in ['r', 'w']:
190 raise RelaxError("The mode '%s' must be one or 'r' or 'w'." % mode)
191
192
193 if not gzip_module:
194 if mode == 'r':
195 raise RelaxError("Cannot open the file %s, try uncompressing first. %s." % (file, gzip_module_message))
196 else:
197 raise RelaxError("Cannot create gzipped file %s, the bz2 Python module cannot be found." % file)
198
199
200 if mode == 'r':
201
202 if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
203 file_obj = gzip.open(file, 'rt')
204
205
206 elif sys.version_info[0] == 3 and sys.version_info[1] < 3:
207 file_obj = io.TextIOWrapper(GzipFixed(file, 'r'))
208
209
210 else:
211 file_obj = gzip.GzipFile(file, 'r')
212
213
214 elif mode == 'w':
215
216 if sys.version_info[0] == 3 and sys.version_info[1] >= 3:
217 file_obj = gzip.open(file, 'wt')
218
219
220 elif sys.version_info[0] == 3 and sys.version_info[1] < 3:
221 file_obj = io.TextIOWrapper(GzipFixed(file, 'w'))
222
223
224 else:
225 file_obj = gzip.GzipFile(file, 'w')
226
227
228 return file_obj
229
230
231 -def norm(x, ord=None, axis=None):
232 """Replacement numpy.linalg.norm() function to handle the axis argument for old numpy.
233 @param x: Input array. If `axis` is None, `x` must be 1-D or 2-D.
234 @type x: array_like
235 @keyword ord: Order of the norm (see table under ``Notes``). inf means numpy's `inf` object.
236 @type ord: {non-zero int, inf, -inf, 'fro'}, optional
237 @keyword axis: If `axis` is an integer, it specifies the axis of `x` along which to compute the vector norms. If `axis` is a 2-tuple, it specifies the axes that hold 2-D matrices, and the matrix norms of these matrices are computed. If `axis` is None then either a vector norm (when `x` is 1-D) or a matrix norm (when `x` is 2-D) is returned.
238 @type axis: {int, 2-tuple of ints, None}, optional
239 """
240
241
242 if axis == None:
243 return numpy.linalg.norm(x, ord=ord)
244
245
246 if numpy_norm_axis:
247 return numpy.linalg.norm(x, ord=ord, axis=axis)
248
249
250 return numpy.apply_along_axis(numpy.linalg.norm, axis, x)
251
252
253
255 """Incredibly nasty hack for bzip2 files support in Python 3.0, 3.1 and 3.2."""
256
259
262
265
268
271
272
273
275 """Incredibly nasty hack for gzipped files support in Python 3.0, 3.1 and 3.2."""
276
277 closed = False
278
281
284
287
290
291
292
294 """Python 2.4 and earlier Queuing class replacement.
295
296 This code comes from http://code.activestate.com/recipes/475160/ and is part of the Python sources from 2.5 onwards.
297 """
298
300 Queue2.__init__(self)
301 self.all_tasks_done = threading.Condition(self.mutex)
302 self.unfinished_tasks = 0
303
304 - def _put(self, item):
305 Queue2._put(self, item)
306 self.unfinished_tasks += 1
307
309 """Indicate that a formerly enqueued task is complete.
310
311 Used by Queue consumer threads. For each get() used to fetch a task,
312 a subsequent call to task_done() tells the queue that the processing
313 on the task is complete.
314
315 If a join() is currently blocking, it will resume when all items
316 have been processed (meaning that a task_done() call was received
317 for every item that had been put() into the queue).
318
319 Raises a ValueError if called more times than there were items
320 placed in the queue.
321 """
322 self.all_tasks_done.acquire()
323 try:
324 unfinished = self.unfinished_tasks - 1
325 if unfinished <= 0:
326 if unfinished < 0:
327 raise ValueError('task_done() called too many times')
328 self.all_tasks_done.notifyAll()
329 self.unfinished_tasks = unfinished
330 finally:
331 self.all_tasks_done.release()
332
334 """Blocks until all items in the Queue have been gotten and processed.
335
336 The count of unfinished tasks goes up whenever an item is added to the
337 queue. The count goes down whenever a consumer thread calls task_done()
338 to indicate the item was retrieved and all work on it is complete.
339
340 When the count of unfinished tasks drops to zero, join() unblocks.
341 """
342 self.all_tasks_done.acquire()
343 try:
344 while self.unfinished_tasks:
345 self.all_tasks_done.wait()
346 finally:
347 self.all_tasks_done.release()
348
349
350
351 if PY_VERSION == 2 and sys.version_info[1] <= 4:
352 Queue = TaskQueue
353 elif PY_VERSION == 2:
354 Queue = Queue2
355 else:
356 Queue = Queue3
357
358
359
360 if PY_VERSION == 2:
361
362
363
364
365
366 if sys.version_info[1] <= 3:
367 if SYSTEM == 'Linux':
368 os.devnull = '/dev/null'
369 elif SYSTEM == 'Windows':
370 os.devnull = 'nul'
371 elif SYSTEM == 'Darwin':
372 os.devnull = 'Dev:Null'
373 else:
374 os.devnull = None
375
376
377 unicode = builtins.unicode
378
379
380 from codecs import unicode_escape_decode
382 """Create a unicode string for Python 2.
383
384 @param text: The text to convert.
385 @type text: str
386 @return: The text converted to unicode.
387 @rtype: unicode
388 """
389
390 return unicode_escape_decode(text)[0]
391
392
393
394 if PY_VERSION == 3:
395
396 unicode = builtins.str
397
398
400 """Create a unicode string for Python 3.
401
402 @param text: The text to convert.
403 @type text: str
404 @return: The unmodified text, as all strings in Python 3 are unicode and the unicode type does not exist.
405 @rtype: str
406 """
407
408 return text
409