Skip to content

Conversation

@davidhassell
Copy link
Contributor

Hi @sadielbartholomew here are some thoughts for refactoring the constants access situation so that we can most effortlessly use it in cf-python. The user-facing API is unchanged. What do you think?

The on-line docs are unchanged. There is a small change to the help(cfdm.rtol) docs, but tolerable, I think.

cf-python would use it as follows:

# in cf/functions.py

class rtol(cfdm.rtol):
    _CONSTANTS = CONSTANTS
    _Constant = Constant


rtol.__doc__ = cfdm.rtol.__doc__.replace('cfdm.', 'cf.')

So cf-python need not know anything about the functionality nor API nor docs from cfdm.

This is just one idea of many, I'm sure.

@davidhassell davidhassell marked this pull request as draft November 20, 2020 14:23
@codecov
Copy link

codecov bot commented Nov 20, 2020

Codecov Report

Merging #104 (7113add) into master (723b5bc) will decrease coverage by 0.04%.
The diff coverage is 95.24%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #104      +/-   ##
==========================================
- Coverage   88.68%   88.65%   -0.03%     
==========================================
  Files         101      101              
  Lines       10008    10050      +42     
==========================================
+ Hits         8875     8909      +34     
- Misses       1133     1141       +8     
Flag Coverage Δ
unittests 88.65% <95.24%> (-0.03%) ⬇️

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

Impacted Files Coverage Δ
cfdm/functions.py 97.77% <95.24%> (-0.22%) ⬇️
cfdm/domain.py 89.84% <0.00%> (-1.39%) ⬇️
cfdm/mixin/properties.py 89.40% <0.00%> (-0.60%) ⬇️
cfdm/coordinatereference.py 87.70% <0.00%> (-0.58%) ⬇️
cfdm/decorators.py 94.37% <0.00%> (-0.57%) ⬇️
cfdm/mixin/propertiesdata.py 82.80% <0.00%> (-0.35%) ⬇️
cfdm/mixin/propertiesdatabounds.py 85.54% <0.00%> (-0.22%) ⬇️
cfdm/field.py 86.70% <0.00%> (-0.11%) ⬇️
cfdm/__init__.py 90.00% <0.00%> (ø)
cfdm/core/functions.py 100.00% <0.00%> (ø)
... and 1 more

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 723b5bc...de4ceb5. Read the comment docs.

@sadielbartholomew
Copy link
Member

Hi @davidhassell, I think that is an excellent solution: minimal code required to implement the inheritance, as per your example snippet, and it can keep the logic loosely coupled:

So cf-python need not know anything about the functionality nor API nor docs from cfdm.

Good thinking.

I was trying to work out whether the __doc__ replacement would work in light of us running the docstring rewriting logic on all of the docstrings (even if they don't themselves have anything that would be replaced by it), but I think it shouldn't be a problem since the docstring rewriting is done via metaclass which should happen before the replacement happens. I could be wrong though, have you checked it works in this respect (if it happened the other way round the replacement might be futile, if that makes sense)?

@davidhassell
Copy link
Contributor Author

Thanks. Good point on doc string rewriting. Currently it is not in place for these new classes, but I reckon if we add simply the metaclass to ConstantAccess the magic will just happen ... I'll give it a go. The doc string overwriting is somewhat clunky, so it's nice to get rid of it.

@sadielbartholomew
Copy link
Member

Currently it is not in place for these new classes

Oh, I didn't consider this, so I guess a simple option is just to not apply it to those classes, if it does end up causing issue!

On the topic the atol and rtol docstrings are pretty similar so to enforce consistency they may be a good candidate for one or more common text snippets to process in, though being only two cases it might be overkill.

All good. If you would like review once this is ready just ping me. Thanks.

@davidhassell
Copy link
Contributor Author

Will do. I've just remembered, after a bit of tinkering, that the docstring rewriting only happens on methods of the class, rather than the class itself.

It may/may not be trivial to extend it to the __doc__ of the class. For methods, the original method gets copied, the copy's doc string is rewritten and then the new method replaces the old one. This is so that docstring changes do not affect other classes that inherit the same method.

Can we somehow copy the class itself in the same way. I have no idea, but it'll fun(?) finding out.

If this is not easy to fix, I suggest that we implement it as it stands and dwell on the more elegant solution at our leisure.

@davidhassell
Copy link
Contributor Author

(I should say that adding the metaclass absolutely did the trick for method docstring rewriting on the rtol class)

@sadielbartholomew
Copy link
Member

sadielbartholomew commented Nov 20, 2020

Can we somehow copy the class itself in the same way. I have no idea, but it'll fun(?) finding out.

I think that should be do-able via an approach not dissimilar to one used for copying the methods. But I could be wrong. Would not some investigation, Good luck, if you want to try it.

If this is not easy to fix, I suggest that we implement it as it stands and dwell on the more elegant solution at our leisure.

Sounds like a good plan to me. I think the class copying for class docstring re-writing, as above, constitutes another issue entirely, so perhaps we can throw an issue up for it to tackle at a good time.

@davidhassell
Copy link
Contributor Author

Class docstring rewriting is a reality . I think it's all there, now, for now.

@davidhassell davidhassell marked this pull request as ready for review November 23, 2020 15:45
Copy link
Member

@sadielbartholomew sadielbartholomew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a small number of minor comments, but overall this is great and it works perfectly (as far as I can reasonably determine) to retain the behaviour and documentation of the cfdm constants and functions for getting & setting them, while changing the underlying implementation so maintenance is easier and inheritance by cf-python is more robust. 🎉

@davidhassell
Copy link
Contributor Author

HI Sadie - I think it's OK to merge now?

@sadielbartholomew
Copy link
Member

HI Sadie - I think it's OK to merge now?

I found a hash symbol lurking, as in my above comment, but otherwise the minor feedback is all addressed and everything is shipshape, so please merge! I'll start reviewing the cf-python follow-on PR this afternoon.

Co-authored-by: Sadie L. Bartholomew <sadie.bartholomew@ncas.ac.uk>
@davidhassell davidhassell merged commit 789e338 into NCAS-CMS:master Nov 24, 2020
@davidhassell
Copy link
Contributor Author

Great - thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants