mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 03:24:20 +00:00
✨ Create type that allows some values to be skipped in update and set
This commit is contained in:
parent
816e9194d1
commit
a09afbc9fe
1 changed files with 22 additions and 0 deletions
|
@ -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",
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue