rs-station / reciprocalspaceship

Tools for exploring reciprocal space
https://rs-station.github.io/reciprocalspaceship/
MIT License
28 stars 12 forks source link

Add decorators for gemmi argument checking #94

Closed JBGreisman closed 3 years ago

JBGreisman commented 3 years ago

Fixes #92.

It is common in rs to write functions that take gemmi.SpaceGroup or gemmi.UnitCell instances as arguments. In order to make these methods more flexible, it is often useful to add error checking code so that if an int is provided instead of a gemmi.SpaceGroup, the method generates the equivalent gemmi object instance. This was done for the DataSet.cell and DataSet.spacegroup setter methods to address #18.

This PR generalizes this idea using @spacegroupify and @cellify decorators to avoid needing to write boiler plate argument checking code. This allows one to write a function as if it is guaranteed that the input argument is a gemmi object, while still being able to input that argument flexibly. For example:

from reciprocalspaceship.decorators import spacegroupify

@spacegroupify
def get_xhm(spacegroup=None):
    return spacegroup.xhm()

import gemmi
print(get_xhm(gemmi.SpaceGroup("1"))) # prints 'P 1'
print(get_xhm(1))                     # prints 'P 1'
print(get_xhm("P1"))                  # prints 'P 1'
JBGreisman commented 3 years ago

To do:

codecov-commenter commented 3 years ago

Codecov Report

Merging #94 (9fb3a52) into main (dfdae4e) will increase coverage by 0.09%. The diff coverage is 99.03%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #94      +/-   ##
==========================================
+ Coverage   98.87%   98.97%   +0.09%     
==========================================
  Files          40       41       +1     
  Lines        1512     1561      +49     
==========================================
+ Hits         1495     1545      +50     
+ Misses         17       16       -1     
Flag Coverage Δ
unittests 98.97% <99.03%> (+0.09%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
reciprocalspaceship/decorators.py 98.61% <98.61%> (ø)
reciprocalspaceship/dataset.py 99.01% <100.00%> (+0.17%) :arrow_up:
reciprocalspaceship/utils/asu.py 100.00% <100.00%> (ø)
reciprocalspaceship/utils/cell.py 100.00% <100.00%> (ø)
reciprocalspaceship/utils/phases.py 100.00% <100.00%> (ø)
reciprocalspaceship/utils/stats.py 100.00% <100.00%> (ø)
reciprocalspaceship/utils/structurefactors.py 100.00% <100.00%> (+2.00%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update dfdae4e...9fb3a52. Read the comment docs.