-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathutil.py
144 lines (110 loc) · 3.65 KB
/
util.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
"""
Utilities for testing within quantecon
@author : Spencer Lyon
@date : 2014-08-01 10:56:32
"""
import sys
import os
from os.path import join, exists
from contextlib import contextmanager
import numpy as np
import tables
if sys.version_info[0] == 2:
from cStringIO import StringIO
else: # python 3
from io import StringIO
@contextmanager
def capture(command, *args, **kwargs):
"""
A context manager to capture std out, so we can write tests that
depend on messages that are printed to stdout
References
----------
http://schinckel.net/2013/04/15/capture-and-test-sys.stdout-sys.
stderr-in-unittest.testcase/
Examples
--------
class FooTest(unittest.TestCase):
def test_printed_msg(self):
with capture(func, *args, **kwargs) as output:
self.assertRegexpMatches(output, 'should be in print msg')
"""
out, sys.stdout = sys.stdout, StringIO()
command(*args, **kwargs)
sys.stdout.seek(0)
yield sys.stdout.read()
sys.stdout = out
def get_data_dir():
"Return directory where data is stored"
this_dir = os.path.dirname(__file__)
# this_dir = os.path.abspath(".")
data_dir = os.path.join(this_dir, "data")
return data_dir
def get_h5_data_file():
"""
<<<<<<< HEAD
return the data file used for holding test data. If the data
directory or file do not exist, they are created.
=======
return the data file used for holding test data
>>>>>>> master
Notes
-----
This should ideally be called from a context manage as so::
with get_h5_data_file() as f:
# do stuff
This way we know the file will be closed and cleaned up properly
"""
data_dir = get_data_dir()
if not exists(data_dir):
os.mkdir(data_dir)
data_file = join(data_dir, "testing_data.h5")
return tables.open_file(data_file, "a", "Data for quantecon tests")
def get_h5_data_group(grp_name, parent="/", f=get_h5_data_file()):
"""
Try to fetch the group named grp_name from the file f. If it doesn't
yet exist, it is created
Parameters
----------
grp_name : str
A string specifying the name of the new group. This should be
only the group name, not including any information about the
group's parent (path)
parent : str, optional(default="/")
The parent or path for where the group should live. If nothing
is given, the group will be created at the root node `"/"`
f : hdf5 file, optional(default=get_h5_data_file())
The file where this should happen. The default is the data file
for these tests
Returns
-------
existed : bool
A boolean specifying whether the group existed or was created
group : tables.Group
The requested group
Examples
--------
with get_h5_data_file() as f:
my_group = get_h5_data_group("jv") # data for jv tests
Notes
-----
As with other code dealing with I/O from files, it is best to call
this function within a context manager as shown in the example.
"""
existed = True
try:
group = f.getNode(parent + grp_name)
except:
# doesn't exist
existed = False
msg = "data for {} tests".format(grp_name + ".py")
group = f.create_group(parent, grp_name, msg)
return existed, group
def write_array(f, grp, array, name):
"stores array in into group grp of h5 file f under name name"
atom = tables.Atom.from_dtype(array.dtype)
ds = f.createCArray(grp, name, atom, array.shape)
ds[:] = array
def max_abs_diff(a1, a2):
"return max absolute difference between two arrays"
return np.max(np.abs(a1 - a2))