mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-08-24 22:21:43 +02:00
Add support for Python with statement
(see PEP 343 for more details)
This commit is contained in:
parent
7a2a60f347
commit
0e75cb42b5
@ -32,7 +32,7 @@ try:
|
||||
elif TEST_TREXIO_BACKEND == trexio.TREXIO_TEXT:
|
||||
shutil.rmtree(output_filename)
|
||||
except:
|
||||
print ('Nothing to remove.')
|
||||
print('Nothing to remove.')
|
||||
|
||||
#=========================================================#
|
||||
#============ WRITE THE DATA IN THE TEST FILE ============#
|
||||
@ -40,6 +40,17 @@ except:
|
||||
|
||||
trexio.info()
|
||||
|
||||
|
||||
# test with ... as ... block
|
||||
with trexio.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND) as tfile:
|
||||
trexio.write_metadata_description(tfile, "Test file produced by the Python API")
|
||||
assert trexio.has_metadata_description(tfile)
|
||||
assert tfile.isOpen
|
||||
|
||||
# the file handle can remain existing but the file itself is closed upon exit from the `with` block
|
||||
assert not tfile.isOpen
|
||||
|
||||
|
||||
# create TREXIO file and open it for writing
|
||||
test_file = trexio.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND)
|
||||
assert test_file.exists
|
||||
@ -281,7 +292,7 @@ assert rpoint_group==point_group
|
||||
|
||||
# another way to read only if the variable exists
|
||||
if trexio.has_ao_num(test_file2):
|
||||
rmo_num = trexio.read_ao_num(test_file2)
|
||||
rao_num = trexio.read_ao_num(test_file2)
|
||||
else:
|
||||
print("Pass on reading the non-existing variable ao_num: checked")
|
||||
|
||||
@ -290,12 +301,12 @@ else:
|
||||
|
||||
# cleaning (remove the TREXIO file)
|
||||
try:
|
||||
if TEST_TREXIO_BACKEND == trexio.TREXIO_HDF5:
|
||||
os.remove(output_filename)
|
||||
elif TEST_TREXIO_BACKEND == trexio.TREXIO_TEXT:
|
||||
shutil.rmtree(output_filename)
|
||||
if TEST_TREXIO_BACKEND == trexio.TREXIO_HDF5:
|
||||
os.remove(output_filename)
|
||||
elif TEST_TREXIO_BACKEND == trexio.TREXIO_TEXT:
|
||||
shutil.rmtree(output_filename)
|
||||
except:
|
||||
print (f'No output file {output_filename} has been produced')
|
||||
print(f'No output file {output_filename} has been produced')
|
||||
|
||||
#==========================================================#
|
||||
|
||||
|
@ -720,8 +720,7 @@ class File:
|
||||
|
||||
def __init__(self, filename, mode='r', back_end=TREXIO_HDF5,
|
||||
pytrexio_s=None, info=None):
|
||||
"""This is a TREXIO File constructor."""
|
||||
|
||||
"""TREXIO File class constructor."""
|
||||
self.filename = filename
|
||||
self.mode = mode
|
||||
self.back_end = back_end
|
||||
@ -740,9 +739,19 @@ class File:
|
||||
self.info = info
|
||||
|
||||
|
||||
def __enter__(self):
|
||||
"""Enter statement for with ... as ... handling."""
|
||||
return self
|
||||
|
||||
|
||||
def __exit__(self, *args):
|
||||
"""Exit statement for with ... as ... handling."""
|
||||
if self.isOpen:
|
||||
self.close()
|
||||
|
||||
|
||||
def close(self):
|
||||
"""Close a TREXIO File."""
|
||||
|
||||
if self.isOpen:
|
||||
_close(self.pytrexio_s)
|
||||
self.isOpen = False
|
||||
@ -752,13 +761,11 @@ class File:
|
||||
|
||||
def inquire(self):
|
||||
"""Inquire whether a TREXIO file exists."""
|
||||
|
||||
self.exists = _inquire(self.filename)
|
||||
|
||||
|
||||
def __del__(self):
|
||||
"""This is a TREXIO File destructor."""
|
||||
|
||||
"""TREXIO File class destructor."""
|
||||
if self.isOpen:
|
||||
_close(self.pytrexio_s)
|
||||
elif self.isOpen is None:
|
||||
|
Loading…
Reference in New Issue
Block a user