1

Cannot clearly understand right way to create CRUD interface for entity.
For example: I've got entity Event with variables date, place and name. I've got basic constructor for this entity with default (nullablle) values.

class Event:
    def __init__(self, date=None, name=None, place=None):
        self.date = date
        self.name = name
        self.place = place

Trying to figure out pythonic way to create update handler for this entity, which allows me to update custom number of entity fields (for example: update name and place, without specifying date).

How I see this:
Use list of custom attributes in update method like update_event(**kwargs), then parse **kwargs list and match them with entity using .hasattr() or .getattr() functions.
Its seems working (and able to find actual diff between entities, which is useful too), but looking pretty harsh.
Basic version with setting all values from kwargs looks like this:

def update_event(self, **kwargs):
    for key, value in kwargs.items():
        self.__setattr__(key, value)
    return self

If we need to check if for diff, we already got:

def update_event(self, **kwargs):
    for key, value in kwargs.items():
        if self.__getattribute__(key) != value:
            # We got diff
            self.__setattr__(key, value)
        else:
            # We got same value stored already
    return self 

Maybe im missing something and there are more elegant and simple solutions to this problem, or some design patterns to look for?

Tnx for answers and sorry for dummy questions.

UPD: One more point I see: simple __setattr__ doesn't check that such variable exists. So, if **kwargs contains invalid argument name I get Attribute Error.
The obvious fix for that is to create check for .hasattr() but as I see it will return False if variable declared but has None value.

5
  • Please show your code for: Its seems working (and able to find actual diff between entities, which is useful too), but looking pretty harsh.. Commented Jan 26, 2018 at 15:27
  • Perhaps you could update your question with an attempt at this update_event() method, using .hasattr() etc. Commented Jan 26, 2018 at 15:28
  • Tnx for feedback, guys. Added update_event() code to the topic. Commented Jan 26, 2018 at 15:41
  • How general of an answer are you looking for? Should it work with any class? Or do you want something that will work only with Event. Commented Jan 26, 2018 at 15:43
  • I want use it to specific entity\model (Event for example), but if it possible to create something more universal - it will be fine :D Commented Jan 26, 2018 at 15:47

2 Answers 2

2

View this documentation link try to use it this way, I may be wrong, but I think it's better.

def update_event(self, **kwargs):
    for key, value in kwargs.items():
        setattr(self, key, value)
    return self
Sign up to request clarification or add additional context in comments.

Comments

0

To check that you are only overwriting existing attributes:

def update_event(self, **kwargs):
    for key, value in kwargs.items():
        if hasattr(self, key):
            setattr(self, key, value)
    return self

def __repr__(self):
    return 'Event: ' + str(self.date)

f=Event()
print(f)
f.update_event(date=1)
print(f)

Output:

Event: None
Event: 1

2 Comments

Thanks, i ve generally came to same code while writing this post. Is far i can see, it doesnt work if variable declared but doesnt has value (setted to None).
Sorry, thats my bad, i was using .__getattr__ instead hasattr(), and its expectedly return None.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.