Re: [sqlalchemy] How to get `func` attribute from hybrid_method

2017-03-14 Thread Антонио Антуан


вторник, 14 марта 2017 г., 17:15:25 UTC+3 пользователь Mike Bayer написал:
>
>
>
> On 03/14/2017 10:02 AM, Антонио Антуан wrote: 
> > Hi. 
> > I use sqlalchemy 1.0.17, thanks for you great library :) 
> > 
> > I have such code: 
> > | 
> > 
> > 
> > classFlagsMixin(object): 
> > @hybrid_method 
> > defexist_flag(self,flag): 
> > ifnotself.flags ornotflag: 
> > returnFalse 
> > returnself.flags  ==flag 
> > 
> > @exist_flag.expression 
> > defexist_flag(self,flag): 
> > returnand_(flag !=0,self.flags !=0,self.flags.op('&')(flag)!=0) 
> > 
> > 
> > classSomeModel(Base,FlagsMixin,AnyOtherClass): 
> > # class attrs and methods 
> > 
> > | 
> > 
> > 
> > In case of optimization, I want to create sqlalchemy-free class, 
>
> what sort of optimization?  are you having a performance problem? 
>


Just hardcode optimization, no preformance problems, don't worry :)

>
>
> which 
> > contains all methods and attributes of class. So, i wrote that code: 
> > 
> > | 
> > 
> > 
> > @staticmethod 
> > def_get_sa_free_class(klass,with_mro=False): 
> > sa_free_cls =getattr(klass,'__sa_free_cls',None) 
> > 
> > ifsa_free_cls isNone: 
> > items ={} 
> > klasses =[klass]ifnotwith_mro elseklass.__mro__ 
> > fork inklasses: 
> > forname,val ink.__dict__.items(): 
> > ifname notinitems andname 
> > 
> notin['__mapper__','__dict__','__class__','_sa_class_manager','query','metadata']:
>  
>
> > ifisinstance(val,InstrumentedAttribute): 
> > items[name]=None 
> > else: 
> > items[name]=val 
> > formethod_name in('exist_flag',): 
> > items[method_name]=getattr(klass,method_name) 
> > sa_free_cls 
> =type('Unbound'+klass.__name__,(object,),dict(items)) 
> > sa_free_cls._sa_free =True 
> > 
> > sa_free_cls.__init__ 
> =klass._sa_class_manager.original_init.im_func 
> > 
> > setattr(klass,'__sa_free_cls',sa_free_cls) 
> > returnsa_free_cls 
>
> So what you're trying to do is:  1. create a class   2. map it   3. then 
> extract it into something unmapped. this seems backwards.  Why not 
> create your class as "unmapped", then make a subclass of it that is 
> mapped? 
>
>
> class MyOptimizedClass(FlagsMixin): 
>  #  stuff 
>
>
> class MyMappedClass(MyOptimizedClass, Base): 
>  __tablename__ = ... 
>
>  # stuff 
>
>
> Or something like that, where you make use of your descriptor 
> declaration directly instead of reverse engineering it. 
>
> > Result class should has `exist_flag()` method, and it has to be used 
> > always with `func` attribute of hybrid_property, neither `expr` (as I 
> > want to have SQLA-free class). 
> > 
> > As you can see, I want to ensure `exist_flag()` method existence in 
> > result `klass` object, and if method called with `with_mro=False`. 
> > But, of course, such a way can't allow this, because of descriptor logic 
> > - return `expr` if instance not specified: 
> > 
> > | 
> > klass.exist_flag(1) 
> >  
> > 
> > | 
> > 
> > Also, I tried to use this versions with same result - 
> AttributeError(''): 
> > 
> > 
> > | 
> > klass.exist_flag.func 
> > klass.exist_flag.__class__.func 
>
> well when you access the hybrid like that it gets invoked.   you'd need 
> to get it either as: 
>
> hybrid = klass.__class__.__dict__['exist_flag'] 
>
> >>> getattr(klass, 'exist_flag') 
> 
>>>  
>>> klass.__class__.__dict__['exist_flag']  
Traceback (most recent call last): 
 File "", line 1, in  
KeyError: 'exist_flag'

 

> or 
>
> from sqlalchemy import inspect 
> mapper = inspect(klass) 
> hybrid = mapper.all_orm_descriptors.exist_flag 
>
>
> you probably want to be using mapper.all_orm_descriptors to help with 
> your inspection. 
>

Yes, it helps, thank you :) 

>
> > ... 
> > 
> > AttributeError: 'function' object has no attribute 'func' 
> > 
> > | 
> > 
> > Is there any way to get `func` attribute of hybrid_method? 
> > And also, maybe sqlalchemy already has some alternative to get 
> > sqla-free-class? 
> > 
> > Thank you again. 
> > Anton. 
> > 
> > -- 
> > 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+...@googlegroups.com  
> > . 
> > To post to this group, send email to sqlal...@googlegroups.com 
>  
> > . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit 

Re: [sqlalchemy] How to get `func` attribute from hybrid_method

2017-03-14 Thread mike bayer



On 03/14/2017 10:02 AM, Антонио Антуан wrote:

Hi.
I use sqlalchemy 1.0.17, thanks for you great library :)

I have such code:
|


classFlagsMixin(object):
@hybrid_method
defexist_flag(self,flag):
ifnotself.flags ornotflag:
returnFalse
returnself.flags  ==flag

@exist_flag.expression
defexist_flag(self,flag):
returnand_(flag !=0,self.flags !=0,self.flags.op('&')(flag)!=0)


classSomeModel(Base,FlagsMixin,AnyOtherClass):
# class attrs and methods

|


In case of optimization, I want to create sqlalchemy-free class,


what sort of optimization?  are you having a performance problem?


which

contains all methods and attributes of class. So, i wrote that code:

|


@staticmethod
def_get_sa_free_class(klass,with_mro=False):
sa_free_cls =getattr(klass,'__sa_free_cls',None)

ifsa_free_cls isNone:
items ={}
klasses =[klass]ifnotwith_mro elseklass.__mro__
fork inklasses:
forname,val ink.__dict__.items():
ifname notinitems andname
notin['__mapper__','__dict__','__class__','_sa_class_manager','query','metadata']:
ifisinstance(val,InstrumentedAttribute):
items[name]=None
else:
items[name]=val
formethod_name in('exist_flag',):
items[method_name]=getattr(klass,method_name)
sa_free_cls =type('Unbound'+klass.__name__,(object,),dict(items))
sa_free_cls._sa_free =True

sa_free_cls.__init__ =klass._sa_class_manager.original_init.im_func

setattr(klass,'__sa_free_cls',sa_free_cls)
returnsa_free_cls


So what you're trying to do is:  1. create a class   2. map it   3. then 
extract it into something unmapped. this seems backwards.  Why not 
create your class as "unmapped", then make a subclass of it that is mapped?



class MyOptimizedClass(FlagsMixin):
#  stuff


class MyMappedClass(MyOptimizedClass, Base):
__tablename__ = ...

# stuff


Or something like that, where you make use of your descriptor 
declaration directly instead of reverse engineering it.



Result class should has `exist_flag()` method, and it has to be used
always with `func` attribute of hybrid_property, neither `expr` (as I
want to have SQLA-free class).

As you can see, I want to ensure `exist_flag()` method existence in
result `klass` object, and if method called with `with_mro=False`.
But, of course, such a way can't allow this, because of descriptor logic
- return `expr` if instance not specified:

|
klass.exist_flag(1)


|

Also, I tried to use this versions with same result - AttributeError(''):


|
klass.exist_flag.func
klass.exist_flag.__class__.func


well when you access the hybrid like that it gets invoked.   you'd need 
to get it either as:


hybrid = klass.__class__.__dict__['exist_flag']

or

from sqlalchemy import inspect
mapper = inspect(klass)
hybrid = mapper.all_orm_descriptors.exist_flag


you probably want to be using mapper.all_orm_descriptors to help with 
your inspection.




...

AttributeError: 'function' object has no attribute 'func'

|

Is there any way to get `func` attribute of hybrid_method?
And also, maybe sqlalchemy already has some alternative to get
sqla-free-class?

Thank you again.
Anton.

--
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.


[sqlalchemy] How to get `func` attribute from hybrid_method

2017-03-14 Thread Антонио Антуан
Hi.
I use sqlalchemy 1.0.17, thanks for you great library :)

I have such code:


class FlagsMixin(object):
@hybrid_method
def exist_flag(self, flag):
if not self.flags or not flag:
return False
return self.flags & flag == flag

@exist_flag.expression
def exist_flag(self, flag):
return and_(flag != 0, self.flags != 0, self.flags.op('&')(flag) != 0)


class SomeModel(Base, FlagsMixin, AnyOtherClass):
# class attrs and methods



In case of optimization, I want to create sqlalchemy-free class, which 
contains all methods and attributes of class. So, i wrote that code:


@staticmethod
def _get_sa_free_class(klass, with_mro=False):
sa_free_cls = getattr(klass, '__sa_free_cls', None)

if sa_free_cls is None:
items = {}
klasses = [klass] if not with_mro else klass.__mro__
for k in klasses:
for name, val in k.__dict__.items():
if name not in items and name not in ['__mapper__', '__dict__', 
'__class__', '_sa_class_manager', 'query', 'metadata']:
if isinstance(val, InstrumentedAttribute):
items[name] = None
else:
items[name] = val
for method_name in ('exist_flag', ):
items[method_name] = getattr(klass, method_name)
sa_free_cls = type('Unbound' + klass.__name__, (object, ), dict(items))
sa_free_cls._sa_free = True

sa_free_cls.__init__ = klass._sa_class_manager.original_init.im_func

setattr(klass, '__sa_free_cls', sa_free_cls)
return sa_free_cls



Result class should has `exist_flag()` method, and it has to be used always 
with `func` attribute of hybrid_property, neither `expr` (as I want to have 
SQLA-free class).

As you can see, I want to ensure `exist_flag()` method existence in result 
`klass` object, and if method called with `with_mro=False`.
But, of course, such a way can't allow this, because of descriptor logic - 
return `expr` if instance not specified:

klass.exist_flag(1)



Also, I tried to use this versions with same result - AttributeError(''):


klass.exist_flag.func
klass.exist_flag.__class__.func
...

AttributeError: 'function' object has no attribute 'func'


Is there any way to get `func` attribute of hybrid_method?
And also, maybe sqlalchemy already has some alternative to get 
sqla-free-class?

Thank you again.
Anton.

-- 
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.