خرید بک لینک

Vote count: 0

For a small project I'm implementing a wrapper with ORM behavior. Objects fetched from a coected database are stored inside a buffer and then forwarded to other program parts.

If attributes of these "buffer items" are changed I want to set a flag that later enables me to quickly check which items need synchronization with the database. I'm currently doing this by overwriting the __setattr__() method with a parent class for all buffer classes (representing rows stored corresponding tables).

def __setattr__(self, key, value):
    try:
        if self.__dict__[key] != value
            self.__dict__["_changed"] = True
    except KeyError:
        pass
    super().__setattr__(key, value)

I have to ways of creating a buffer item: using the init() method that creates the object from a tuple (row in database) and a classmethod new() that is used to create items without an entry in the database (yet).

During initialization __setattr__ is called and at the end of __init__ I have to set the flag False again. However in the case of using new() I want the flag to be True. I'm currently achieving this by doing this:

@classmethod
def new(cls, x):
    temp = cls(x)
    temp._flag = True
    retu temp

With program efficiency and speed in mind: is this really the most phytonic way of implementing this? I find my solution quite gruesome because during initialization an exception is thrown for each class attribute.

I know that there are tools like sqlalchemy out there. However I don't want to rely packages not included in the standard library.

asked 50 secs ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 10 خرداد 1395 ساعت: 9:52

صفحه بندی