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)
<sqlalchemy.sql.elements.BooleanClauseList object at 0x7f2a79b835d0>


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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to