12
12
tables = pytest .importorskip ('tables' )
13
13
14
14
15
- def safe_remove (path ):
16
- if path is not None :
17
- try :
18
- os .remove (path )
19
- except OSError :
20
- pass
21
-
22
-
23
- def create_tempfile (path ):
24
- return os .path .join (tempfile .gettempdir (), path )
25
-
26
-
27
- @contextmanager
28
- def ensure_clean_path (path ):
29
- try :
30
- if isinstance (path , list ):
31
- filenames = [create_tempfile (p ) for p in path ]
32
- yield filenames
33
- else :
34
- filenames = [create_tempfile (path )]
35
- yield filenames [0 ]
36
- finally :
37
- for f in filenames :
38
- safe_remove (f )
39
-
40
-
41
15
@pytest .fixture
42
- def pytables_hdf5_file ():
43
- """Use PyTables to create a simple HDF5 file."""
16
+ def pytables_hdf5_file (tmp_path ):
17
+ """Use PyTables to create a simple HDF5 file.
18
+
19
+ There is no need for temporary file cleanup, pytest makes sure that there is
20
+ no collision between tests and that storage needs to not grow indefinitely.
21
+ """
44
22
45
23
table_schema = {
46
24
'c0' : tables .Time64Col (pos = 0 ),
@@ -59,16 +37,18 @@ def pytables_hdf5_file():
59
37
60
38
objname = 'pandas_test_timeseries'
61
39
62
- with ensure_clean_path ('pytables_hdf5_file' ) as path :
63
- # The `ensure_clean_path` context mgr removes the temp file upon exit.
64
- with tables .open_file (path , mode = 'w' ) as f :
65
- t = f .create_table ('/' , name = objname , description = table_schema )
66
- for sample in testsamples :
67
- for key , value in sample .items ():
68
- t .row [key ] = value
69
- t .row .append ()
40
+ # The `tmp_path` fixture provides a temporary directory unique to the
41
+ # individual test invocation. Create a file in there.
42
+ h5path = tmp_path / 'written_with_pytables.h5'
43
+
44
+ with tables .open_file (h5path , mode = 'w' ) as f :
45
+ t = f .create_table ('/' , name = objname , description = table_schema )
46
+ for sample in testsamples :
47
+ for key , value in sample .items ():
48
+ t .row [key ] = value
49
+ t .row .append ()
70
50
71
- yield path , objname , pd .DataFrame (testsamples )
51
+ return h5path , objname , pd .DataFrame (testsamples )
72
52
73
53
74
54
class TestReadPyTablesHDF5 :
0 commit comments