have you tried mapper.iterate_properties ?   

On Dec 21, 2010, at 11:14 AM, Hector Blanco wrote:

> I found a "maybe" way... but I don't know if it's good idea... the
> "propertyProxy" instances have a field called "descriptor" which the
> "InstrumentedAttribute" don't have... so I can always do this:
> 
> --------------------------------------------------
> def getProperties4(cls):
>       properties = list()
> 
>       for varKey in vars(cls):
>               varVal = getattr(cls, varKey)
>               try:
>                       if "descriptor" in vars(varVal):
>                               if isinstance(getattr(varVal, "descriptor"), 
> property):
>                                       properties.append(varKey)
>               except TypeError:
>                       pass
>       print("Properties found: '%s'" % properties)
>       return properties
> --------------------------------------------------
> That works...
>       Properties found: '['id', password', 'userName']'
> 
> ...but I don't really know what I'm exactly touching here... (and how
> "dangerous"... or correct it may be)
> 
> 2010/12/21 Hector Blanco <[email protected]>:
>> First of all, thank you for replying.
>> 
>> I don't really know if I understood your idea.
>> 
>> I dug a bit more in the User class (not the instance, but what it
>> would be self.__class__) and the problem is that both "password" and
>> "_password" have a "__get__":
>> 
>> 
>> I changed the getProperties method a bit, to introspect the __class__ thing:
>> 
>> def getProperties(cls):
>>        properties = list()
>> 
>>        for varName in vars(cls):
>>                log.debug("Studying prop '%s' of type: %s" %(varName,
>>                                type(getattr(cls, varName))))
>>                if varName == "password" or varName == "_password":
>>                        valTmp = getattr(cls, varName)
>>                        print(" \t Has %s a __get()__? %s" % (varName, 
>> getattr(valTmp, "__get__")))
>>                        print(" \t Contents of %s" % varName)
>>                        for key, val in valTmp.__dict__.iteritems():
>>                                print(" \t\t %s: %s" % (key, val))
>> 
>>        return None
>>        #return properties
>> 
>> ----- And it outputs this (showing only the password thing:) ---------
>> 
>> Studying prop '_password' of type: <class
>> 'sqlalchemy.orm.attributes.InstrumentedAttribute'>
>>         Has _password a __get()__? <bound method
>>                                InstrumentedAttribute.__get__ of
>>                                
>> <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0xa26e46c>>
>>         Contents of _password
>>                 parententity: Mapper|User|users
>>                 __doc__: None
>>                 impl: <sqlalchemy.orm.attributes.ScalarAttributeImpl object 
>> at 0xa3d758c>
>>                 key: _password
>>                 comparator: <sqlalchemy.orm.properties.Comparator object at 
>> 0xa26e44c>
>>  Studying prop 'password' of type: <class
>> 'sqlalchemy.orm.attributes.propertyProxy'>
>>         Has password a __get()__? <bound method propertyProxy.__get__ of
>>                                <sqlalchemy.orm.attributes.propertyProxy 
>> object at 0xa26eacc>>
>>         Contents of password
>>                 _comparator: <function comparator at 0xa269bc4>
>>                 key: password
>>                 descriptor: <property object at 0xa25ef7c>
>>                 _parententity: Mapper|User|users
>>                 user_prop: <property object at 0xa25ef7c>
>>                 __doc__: Get password
>>                 impl: <sqlalchemy.orm.attributes._ProxyImpl object at 
>> 0xa26eaec>
>> 
>> ------------------
>> 
>> I have also tried to check isinstance(getattr(cls, varName),
>> sqlalchemy.orm.attributes.InstrumentedAttribute) (even though it may
>> not be the best option, but...) and the problem is that both
>> "password" and "_password" happen to be InstrumentedAttributes
>> (propertyProxy extends from InstrumentedAttribute).
>> 
>> I've seen in the "attributes.py" file an "is_instrumented" method...
>> Maybe I could get the vars of an instance (not the class, no... an
>> instance) which would give me:
>> (["_sa_instance_state", "_id", "_userName", "_password"]),
>> then check if these variables are instrumented ("_sa_instance_state"
>> isn't) and then check if the class has the attributes ["id",
>> "userName" and "password"] but in order to do that I need to remove
>> the first character of the attribute name (to get  "userName" from
>> "_userName") and that seems it's going to mess up with the
>> performance...
>> 
>> Thank you!
>> 
>> 2010/12/20 Michael Bayer <[email protected]>:
>>> 
>>> On Dec 20, 2010, at 7:30 PM, Hector Blanco wrote:
>>> 
>>>> Hello all!
>>>> 
>>>> I have an application running under Python2.6 and the classes are set
>>>> up with properties (in a Python2.4 style, though).
>>                [ . . . ]
>>>> So here's the question:
>>>> Is there any way to get the properties of a class mapped with SqlAlchemy?
>>> 
>>> I'd look at the object to see if it has a __get__()  method, since that's 
>>> what defines a "descriptor" in Python, not just isinstance(x, property).   
>>> duck typing
>>> 
>>> 
>>> --
>>> You received this message because you are subscribed to the Google Groups 
>>> "sqlalchemy" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to 
>>> [email protected].
>>> For more options, visit this group at 
>>> http://groups.google.com/group/sqlalchemy?hl=en.
>>> 
>>> 
>> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to