1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 11:34:18 +00:00

Create type that allows some values to be skipped in update and set

This commit is contained in:
Steffo 2020-11-08 02:16:29 +01:00
parent 816e9194d1
commit a09afbc9fe

View file

@ -7,10 +7,32 @@ class Updatable:
def update(self, **kwargs): def update(self, **kwargs):
"""Set attributes from the kwargs, ignoring non-existant key/columns.""" """Set attributes from the kwargs, ignoring non-existant key/columns."""
for key, value in kwargs.items(): for key, value in kwargs.items():
if value is DoNotUpdate:
continue
if hasattr(self, key): if hasattr(self, key):
setattr(self, key, value) setattr(self, key, value)
def set(self, **kwargs): def set(self, **kwargs):
"""Set attributes from the kwargs, without checking for non-existant key/columns.""" """Set attributes from the kwargs, without checking for non-existant key/columns."""
for key, value in kwargs.items(): for key, value in kwargs.items():
if value is DoNotUpdate:
continue
setattr(self, key, value) setattr(self, key, value)
class DoNotUpdateType:
"""
A type, similar to NoneType, used to mark fields that should be skipped in update and set operations.
"""
__slots__ = ()
DoNotUpdate = DoNotUpdateType()
"""The constant instance of the DoNotUpdateType."""
__all__ = (
"Updatable",
"DoNotUpdateType",
"DoNotUpdate",
)