I’m going to track the evolution of some values with a database via SQLAlchemy: I get them as a sequence of (key, value) pairs and I expect the values for each key to change rarely.
I’m using SQLAlchemy with declarative, the model looks like:
class Item(Base):
timestamp = Column(DateTime)
key = Column(Unicode(16))
value = Column(something...)
I’d like to store rows like (timestamp, key, value) in the database and avoid storing new entries if the value didn’t change since the last one recorded, while keeping the whole history of past values. In pseudo python:
for foo in new_items:
query = session.query(Item).filter_by(key=foo.key)
latest = query.order_by(Item.timestamp.desc()).first()
if foo.value == latest.value:
continue # nothing changed, ignore the new item
else:
session.add(foo)
session.commit()
Should I go with a naive approach like the one above? It somehow strikes me as not-good-looking code. At least I’d maybe add the test as a method on Item.
Is there some better way to do that? Better does not necessarily mean quicker, could be just more pythonic or better looking code.
I am aware of two ways to do that. There’s your approach:
or
The first one has a race condition. I tend to use the second pattern.