Skip to content

Commit

Permalink
#494 fix typo and add some more docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
tumb1er committed Oct 8, 2021
1 parent eb8df82 commit 3249a5c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion model_utils/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,46 @@ class FieldsContext:
... # f1 is reset after outer context exit
>>>
* Note that fields are countedbe passing same state dict
* Note that fields are counted by passing same state dict
* FieldsContext is instantiated using FieldInstanceTracker (`obj.tracker`)
* Different objects has own state stack
"""

def __init__(self, tracker, *fields, state=None):
"""
:param tracker: FieldInstanceTracker instance to be reset after
context exit
:param fields: a list of field names to be tracked in current context
:param state: shared state dict used to count number of field
occurrences in context stack.
On context enter each field mentioned in `fields` has +1 in shared
state, and on exit it receives -1. Fields that have zero after context
exit are reset in tracker instance.
"""
if state is None:
state = {}
self.tracker = tracker
self.fields = fields
self.state = state

def __enter__(self):
"""
Increments tracked fields occurrences count in shared state.
"""
for f in self.fields:
self.state.setdefault(f, 0)
self.state[f] += 1
return self

def __exit__(self, exc_type, exc_val, exc_tb):
"""
Decrements tracked fields occurrences count in shared state.
If any field has no more occurrences in shared state, this field is
being reset by tracker.
"""
reset_fields = []
for f in self.fields:
self.state[f] -= 1
Expand Down

0 comments on commit 3249a5c

Please sign in to comment.