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