Re: [sqlalchemy] Using __getattr__ and __setattr__ with sqlalchemy

2018-03-30 Thread Mike Bayer
On Fri, Mar 30, 2018 at 3:14 AM, Tolstov Sergey  wrote:
> I'm sorry, not it work.
> I cannot undestand how work this commands:
> 1)if (key in self.__dict__ or key in self.__class__.__dict__ or not
> hasattr(self, '_sa_instance_state') or 'AssociationProxy' in key):
> Base.__setattr__(self, key, value)

the original author is attempting to allow all attributes that seem to
originate from SQLAlchemy's instrumentation to pass through without
any alternate behavior.   It appears that by looking for "key in
self.__class__.__dict__" they are testing to see if this attribute
name is an exisitng Python descriptor
(https://docs.python.org/3/howto/descriptor.html).  "not hasattr(self,
'_sa_instance_state')" I guess is trying to see if the object hasn't
been instrumented yet.   in any case, the script way at the top of
this thread still works in SQLAlchemy 1.2 as long as you take out the
"mutable=True" from the PickleType.

Here's the original script simplified.  perhaps you can work from this
for whatever it is you're trying to do.

from sqlalchemy import Integer, String, Column, create_engine, PickleType
from sqlalchemy.orm import Session
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.mutable import MutableDict

Base = declarative_base()


class Foo(Base):
__tablename__ = 'foo'
id = Column(Integer, primary_key=True)
_instance_dict = Column(MutableDict.as_mutable(PickleType()))
name = Column(String)

def __init__(self, name):
self._instance_dict = {}
self.name = name

def __setattr__(self, key, value):
if key == '_sa_instance_state' or key in dir(self.__class__):
# handle class-level attrs (like Columns) using default behavior
print('setattr normal', key, value)
Base.__setattr__(self, key, value)
else:
# regular python objects attached to as an attribute go to
# pickled instance dict
print('setattr other', key, value)
self._instance_dict[key] = value

def __getattr__(self, key):
try:
# check for the key in regular python object dict
# and sqlobjects dict
return self._instance_dict[key]
except KeyError:
# normal lookup
return object.__getattribute__(self, key)


engine = create_engine('sqlite://', echo=True)
Base.metadata.create_all(engine)

session = Session(engine)


# create a new animal
a = Foo('aardvark')
a.test = 10

session.add(a)
session.commit()
session.close()

for foo in session.query(Foo):
print(foo, foo.name, foo.test)

assert hasattr(foo, 'test')
assert hasattr(foo, 'name')
assert not hasattr(foo, 'blah')







> 2) self._sa_instance_state.initialize(key)
>
> Can you tell me, please
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Using __getattr__ and __setattr__ with sqlalchemy

2018-03-30 Thread Tolstov Sergey
I'm sorry, not it work.
I cannot undestand how work this commands:
1)if (key in self.__dict__ or key in self.__class__.__dict__ or not 
hasattr(self, '_sa_instance_state') or 'AssociationProxy' in key):
Base.__setattr__(self, key, value)
2) self._sa_instance_state.initialize(key)

Can you tell me, please

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.