Last active
September 7, 2017 09:39
-
-
Save numberoverzero/e67b767ace0571d4fd7aa044e0ad314c to your computer and use it in GitHub Desktop.
sample impl of tracking marked columns and also columns with default values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_marked(obj): | |
"""Returns the set of marked columns for an object""" | |
return set(_obj_tracking[obj]["marked"]) | |
def get_marked_or_default(obj): | |
marked = get_marked(obj) | |
for column in obj.Meta.columns: | |
if column.default is not missing: | |
marked.add(column) | |
return marked |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_obj_tracking = WeakDefaultDictionary(lambda: { | |
"marked": set(), | |
"snapshot": None, | |
"new_object": True, | |
}) | |
def is_new(obj): | |
return _obj_tracking[obj]["new_object"] | |
def clear_new(obj): | |
_obj_tracking[obj]["new_object"] = False | |
def get_marked_or_default(obj): | |
marked = get_marked(obj) | |
# Since this is a new object never persisted to DynamoDB before, | |
# eagerly evaluate all default Columns as well | |
if is_new(obj): | |
for column in obj.Meta.columns: | |
if column.default is not missing: | |
marked.add(column) | |
return marked | |
# deleted/loaded/saved fire after a sync to/from Dynamo | |
def on_object_[deleted|loaded|saved](...): | |
... # Whatever those methods already do | |
clear_new(obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment