From a09afbc9feb4c7284ac35bf3ff86404a1953cca2 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 8 Nov 2020 02:16:29 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Create=20type=20that=20allows=20som?= =?UTF-8?q?e=20values=20to=20be=20skipped=20in=20update=20and=20set?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- royalnet/alchemist/update.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/royalnet/alchemist/update.py b/royalnet/alchemist/update.py index f77eea9e..fc513637 100644 --- a/royalnet/alchemist/update.py +++ b/royalnet/alchemist/update.py @@ -7,10 +7,32 @@ class Updatable: def update(self, **kwargs): """Set attributes from the kwargs, ignoring non-existant key/columns.""" for key, value in kwargs.items(): + if value is DoNotUpdate: + continue if hasattr(self, key): setattr(self, key, value) def set(self, **kwargs): """Set attributes from the kwargs, without checking for non-existant key/columns.""" for key, value in kwargs.items(): + if value is DoNotUpdate: + continue 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", +)