On Mon, Sep 3, 2018 at 3:25 AM Jacco van Dorp <j.van.d...@deonet.nl> wrote:

> This feels really useful to me to make some quick changes to a database -
> perhaps a database layer could return an class of type Recordclass, and
> then you just simply mutate it and shove it back into the database.
> Pseudocode:
>
> record = database.execute("SELECT * FROM mytable WHERE primary_key = 15")
> record.mostRecentLoggedInTime = time.time()
> database.execute(f"UPDATE mytable SET mostRecentLoggedInTime =
> {record.mostRecentLoggedInTime} WHERE primary_key = {record.primary_key}":)
>
> Or any smart database wrapper might just go:
>
> database.updateOrInsert(table = mytable, record = record)
>
> And be smart enough to figure out that we already have a primary key
> unequal to some sentinel value like None, and do an update, while it could
> do an insert if the primary key WAS some kind of sentinel value.
>

SQLAlchemy.orm solves for this (with evented objects with evented
attributes):

http://docs.sqlalchemy.org/en/latest/orm/session_state_management.html#session-object-states
- Transient, Pending, Persistent, Deleted, Detached
http://docs.sqlalchemy.org/en/latest/orm/session_api.html#sqlalchemy.orm.attributes.flag_modified
- flag_modified isn't necessary in most cases because attribute mutation on
mapped classes deriving from Base(declarative_base()) is evented
http://docs.sqlalchemy.org/en/latest/orm/session_events.html#attribute-change-events
http://docs.sqlalchemy.org/en/latest/orm/tutorial.html

There are packages for handling attribute states with the Django ORM, as
well:

- https://github.com/romgar/django-dirtyfields
- https://github.com/Suor/django-dirty


What would be the performance impact of instead subclassing from
recordclass? IDK.


pyrsistent.PRecord(PMap) is immutable and supports .attribute access:
https://github.com/tobgu/pyrsistent#precord
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to