Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#491 Context manager for resetting fields #494

Merged
merged 12 commits into from
Oct 8, 2021

Conversation

tumb1er
Copy link
Contributor

@tumb1er tumb1er commented Aug 29, 2021

Problem

Solution

This PR introduces FieldsContext context manager which allows to postpone state reset till outermost context exit

Commandments

  • Write PEP8 compliant code.
  • Cover it with tests.
  • Update CHANGES.rst file to describe the changes, and quote according issue with GH-<issue_number>.
  • Pay attention to backward compatibility, or if it breaks it, explain why.
  • Update documentation (if relevant).

Closes #491

@codecov
Copy link

codecov bot commented Aug 29, 2021

Codecov Report

Merging #494 (3249a5c) into master (9deb39d) will increase coverage by 0.37%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #494      +/-   ##
==========================================
+ Coverage   94.70%   95.07%   +0.37%     
==========================================
  Files           6        6              
  Lines         774      812      +38     
==========================================
+ Hits          733      772      +39     
+ Misses         41       40       -1     
Impacted Files Coverage Δ
model_utils/tracker.py 84.78% <100.00%> (+3.53%) ⬆️

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 9deb39d...3249a5c. Read the comment docs.

Copy link
Member

@MRigal MRigal left a comment

Choose a reason for hiding this comment

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

This is really brilliant @tumb1er !!! Sorry for having taken so long to make the review. I had given a first look the first day, but I wanted to read it a couple more time to be sure to get the whole picture.

I've added various suggestions, feel free to take the ones you like! :-)

IMHO this deserves a 5.0 release

docs/utilities.rst Outdated Show resolved Hide resolved
class FieldsContext:
def __init__(self, tracker, *fields, state=None):
if state is None:
state = {}
Copy link
Member

Choose a reason for hiding this comment

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

Or maybe a defaultdict(0) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

state may be passed as a parameter to FieldsContext.__init__, it will be not so obvious that there should be defaultdict instead of dict.

Copy link
Member

Choose a reason for hiding this comment

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

You're right, but what is the real purpose of being able to do so, as the behaviour is very specific to a dict of ints? Is it really desirable that this gets passed as parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nested contexts for same object should share same state (1), while contexts for another objects should have independent states (2). We can't use singleton because of (2), but need some shared storage for (1).

For (1) we can use single FieldsContext instance, implement a stack with field lists, but it looks too complicated both for FieldsContext implementation and helper methods that return FieldsContext. May be I'm wrong here and implementing re-entrance will be easier.

Could you think for some time about FIeldsContext fields stack and re-entrance? If it's good idea from your point of view, I'll refactor this at weekend.

Copy link
Member

Choose a reason for hiding this comment

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

Hi @tumb1er I'm not really sure this will reduce the complexity, so I guess you'd better leave it as it is. Maybe you could also here add a docstring that explains the architecture.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Complexity - bad, docstrings - good. I'll do it.

Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to split this pr in smaller ones?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this PR:

  • Two classes for new mechanics were introduced for fields state tracking
  • Some helper methods added that simplify using new mechanics in new code
  • default monkey-patched methods refactored to use new mechanics

I can split it, but each one will be more or less useless without another one.

Copy link
Member

Choose a reason for hiding this comment

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

@auvipy it makes no sense to split this in smaller PRs! This is providing a new very flexible functionality to the FieldTracker

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added docstrings for FieldsContext methods.

model_utils/tracker.py Show resolved Hide resolved
model_utils/tracker.py Show resolved Hide resolved
model_utils/tracker.py Show resolved Hide resolved
CHANGES.rst Show resolved Hide resolved
... # f1 is reset after outer context exit
>>>

* Note that fields are countedbe passing same state dict
Copy link
Member

Choose a reason for hiding this comment

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

I'm not really getting what you meant here, maybe rephrase/correct it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now I'm not also :) I guess I meant "counted by".

I'll fix this after another discussion #494 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed typo here.

Copy link
Contributor

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

and what about breaking changes?

class FieldsContext:
def __init__(self, tracker, *fields, state=None):
if state is None:
state = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to split this pr in smaller ones?

@tumb1er
Copy link
Contributor Author

tumb1er commented Oct 8, 2021

and what about breaking changes?

Breaking changes were introduced earlier, with 4.1.0 release. This PR changes implementation, introduces new functionality but preserves 4.1.0 behavior for existing code.

@tumb1er
Copy link
Contributor Author

tumb1er commented Oct 8, 2021

@auvipy @MRigal still one typo and docstring are waiting to be fixed.

@tumb1er tumb1er marked this pull request as draft October 8, 2021 08:20
@tumb1er tumb1er marked this pull request as ready for review October 8, 2021 08:32
@tumb1er
Copy link
Contributor Author

tumb1er commented Oct 8, 2021

@auvipy @MRigal still one typo and docstring are waiting to be fixed.

Docstrings and typo fixed.

@MRigal
Copy link
Member

MRigal commented Oct 8, 2021

LGTM. @auvipy up to you to check another time this MR, but IMHO it can be merged and is a great addition.

@MRigal MRigal added this to the 4.2.0 milestone Oct 8, 2021
@auvipy
Copy link
Contributor

auvipy commented Oct 8, 2021

yes let me revew

@auvipy auvipy merged commit 79a7793 into jazzband:master Oct 8, 2021
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.

Undocument breaking changes in FieldTracker in Version 4.1
3 participants