mirror of
https://github.com/TREX-CoE/trexio.git
synced 2024-12-22 20:35:44 +01:00
add has_ functions to the Python API
This commit is contained in:
parent
77e3f1ac11
commit
2bd4ef9bfd
@ -41,7 +41,6 @@ except:
|
||||
|
||||
|
||||
# create TREXIO file and open it for writing
|
||||
#test_file = trexio.open(output_filename, 'w', TEST_TREXIO_BACKEND)
|
||||
test_file = trexio.File(output_filename, mode='w', back_end=TEST_TREXIO_BACKEND)
|
||||
|
||||
# Print docstring of the trexio.open function
|
||||
@ -65,7 +64,7 @@ warnings.filterwarnings("error")
|
||||
try:
|
||||
trexio.write_nucleus_num(test_file, nucleus_num*2)
|
||||
except UserWarning:
|
||||
print("Attemp to overwrite nucleus_num: checked.")
|
||||
print("Attempt to overwrite nucleus_num: checked.")
|
||||
|
||||
# initialize charge arrays as a list and convert it to numpy array
|
||||
charges = [6., 6., 6., 6., 6., 6., 1., 1., 1., 1., 1., 1.]
|
||||
@ -127,22 +126,28 @@ labels = [
|
||||
trexio.write_nucleus_label(test_file,labels)
|
||||
|
||||
# close TREXIO file
|
||||
# [TODO:] this functional call is no longer needed as we introduced TREXIO_File class which has a desctructor that closes the file
|
||||
# this call is no longer needed as we introduced TREXIO_File class which has a desctructor that closes the file
|
||||
#trexio.close(test_file)
|
||||
# [TODO:] without calling destructor on test_file the TREXIO_FILE is not getting created and the data is not written when using TEXT back end. This, the user still has to explicitly call destructor on test_file object instead
|
||||
# trexio.close function. This is only an issue when the data is getting written and read in the same session (e.g. in Jupyter notebook)
|
||||
# without calling destructor on test_file the TREXIO_FILE is not getting created and the data is not written when using TEXT back end.
|
||||
# This, the user still has to explicitly call destructor on test_file object instead of the trexio.close function.
|
||||
# This is only an issue when the data is getting written and read in the same session (e.g. in Jupyter notebook)
|
||||
del test_file
|
||||
|
||||
|
||||
|
||||
#==========================================================#
|
||||
#============ READ THE DATA FROM THE TEST FILE ============#
|
||||
#==========================================================#
|
||||
|
||||
# open previously created TREXIO file, now in 'read' mode
|
||||
#test_file2 = trexio.open(output_filename, 'r', TEST_TREXIO_BACKEND)
|
||||
test_file2 = trexio.File(output_filename, 'r', TEST_TREXIO_BACKEND)
|
||||
|
||||
# check for existence of some of the previously written variables
|
||||
assert trexio.has_nucleus_num
|
||||
assert trexio.has_nucleus_charge
|
||||
assert trexio.has_nucleus_coord
|
||||
assert trexio.has_nucleus_label
|
||||
assert trexio.has_nucleus_point_group
|
||||
|
||||
# read nucleus_num from file
|
||||
rnum = trexio.read_nucleus_num(test_file2)
|
||||
assert rnum==nucleus_num
|
||||
@ -194,6 +199,12 @@ for i in range(nucleus_num):
|
||||
rpoint_group = trexio.read_nucleus_point_group(test_file2)
|
||||
assert rpoint_group==point_group
|
||||
|
||||
# another way to read only if the variable exists
|
||||
if trexio.has_mo_num(test_file2):
|
||||
rmo_num = trexio.read_mo_num(test_file2)
|
||||
else:
|
||||
print("Not reading the non-existing variable mo_num.")
|
||||
|
||||
# close TREXIO file
|
||||
#trexio.close(test_file2)
|
||||
|
||||
|
@ -1444,6 +1444,33 @@ def read_$group_num$(trexio_file) -> int:
|
||||
return num_r
|
||||
#+end_src
|
||||
|
||||
#+begin_src python :tangle has_num_front.py
|
||||
def has_$group_num$(trexio_file) -> bool:
|
||||
"""Check that $group_num$ variable exists in the TREXIO file.
|
||||
|
||||
Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.
|
||||
|
||||
Returns:
|
||||
True if the variable exists, False otherwise
|
||||
|
||||
Raises:
|
||||
- Exception from trexio.Error class if TREXIO return code ~rc~ is TREXIO_FAILURE and prints the error message using string_of_error.
|
||||
- Exception from some other error (e.g. RuntimeError).
|
||||
"""
|
||||
|
||||
try:
|
||||
rc = pytr.trexio_has_$group_num$(trexio_file.pytrexio_s)
|
||||
if rc == TREXIO_FAILURE:
|
||||
raise Error(rc)
|
||||
except:
|
||||
raise
|
||||
|
||||
if rc == TREXIO_SUCCESS:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
#+end_src
|
||||
|
||||
** Templates for front end has/read/write a dataset of numerical data
|
||||
|
||||
This section concerns API calls related to datasets.
|
||||
@ -2148,6 +2175,34 @@ def read_$group_dset$(trexio_file, dim = None, doReshape = None, dtype = None):
|
||||
else:
|
||||
return dset_64
|
||||
#+end_src
|
||||
|
||||
#+begin_src python :tangle has_dset_data_front.py
|
||||
def has_$group_dset$(trexio_file) -> bool:
|
||||
"""Check that $group_dset$ variable exists in the TREXIO file.
|
||||
|
||||
Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.
|
||||
|
||||
Returns:
|
||||
True if the variable exists, False otherwise
|
||||
|
||||
Raises:
|
||||
- Exception from trexio.Error class if TREXIO return code ~rc~ is TREXIO_FAILURE and prints the error message using string_of_error.
|
||||
- Exception from some other error (e.g. RuntimeError).
|
||||
"""
|
||||
|
||||
try:
|
||||
rc = pytr.trexio_has_$group_dset$(trexio_file.pytrexio_s)
|
||||
if rc == TREXIO_FAILURE:
|
||||
raise Error(rc)
|
||||
except:
|
||||
raise
|
||||
|
||||
if rc == TREXIO_SUCCESS:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
#+end_src
|
||||
|
||||
** Sparse data structures
|
||||
|
||||
Sparse data structures are used typically for large tensors such as
|
||||
@ -2693,6 +2748,34 @@ def read_$group_dset$(trexio_file, dim = None) -> list:
|
||||
|
||||
return dset_2d_r
|
||||
#+end_src
|
||||
|
||||
#+begin_src python :tangle has_dset_str_front.py
|
||||
def has_$group_dset$(trexio_file) -> bool:
|
||||
"""Check that $group_dset$ variable exists in the TREXIO file.
|
||||
|
||||
Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.
|
||||
|
||||
Returns:
|
||||
True if the variable exists, False otherwise
|
||||
|
||||
Raises:
|
||||
- Exception from trexio.Error class if TREXIO return code ~rc~ is TREXIO_FAILURE and prints the error message using string_of_error.
|
||||
- Exception from some other error (e.g. RuntimeError).
|
||||
"""
|
||||
|
||||
try:
|
||||
rc = pytr.trexio_has_$group_dset$(trexio_file.pytrexio_s)
|
||||
if rc == TREXIO_FAILURE:
|
||||
raise Error(rc)
|
||||
except:
|
||||
raise
|
||||
|
||||
if rc == TREXIO_SUCCESS:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
#+end_src
|
||||
|
||||
** Templates for front end has/read/write a single string attribute
|
||||
*** Introduction
|
||||
|
||||
@ -2932,6 +3015,33 @@ def read_$group_str$(trexio_file) -> str:
|
||||
return str_r
|
||||
#+end_src
|
||||
|
||||
#+begin_src python :tangle has_attr_str_front.py
|
||||
def has_$group_str$(trexio_file) -> bool:
|
||||
"""Check that $group_str$ variable exists in the TREXIO file.
|
||||
|
||||
Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.
|
||||
|
||||
Returns:
|
||||
True if the variable exists, False otherwise
|
||||
|
||||
Raises:
|
||||
- Exception from trexio.Error class if TREXIO return code ~rc~ is TREXIO_FAILURE and prints the error message using string_of_error.
|
||||
- Exception from some other error (e.g. RuntimeError).
|
||||
"""
|
||||
|
||||
try:
|
||||
rc = pytr.trexio_has_$group_str$(trexio_file.pytrexio_s)
|
||||
if rc == TREXIO_FAILURE:
|
||||
raise Error(rc)
|
||||
except:
|
||||
raise
|
||||
|
||||
if rc == TREXIO_SUCCESS:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
#+end_src
|
||||
|
||||
* Fortran helper/wrapper functions
|
||||
|
||||
The function below adapts the original C-based ~trexio_open~ for Fortran.
|
||||
|
Loading…
Reference in New Issue
Block a user