Here is the print immediately after my original class definition:

print 'sample nsa log mstar', Sample.nsa_logmstar 

and the result

sample nsa log mstar None

When I run your script exactly as is, I get the same output as you.  

When I replace my class definition with yours, inside my code, as follows

class Sample(Base):
    __tablename__ = 'sample'
    __table_args__ = {'autoload' : True, 'schema' : 'mangadatadb'}  (I 
needed to add this line in)
    
    pk = Column(Integer, primary_key=True)
    nsa_mstar = Column(Float)

    @hybrid_property
    def nsa_logmstar(self):
        try:
            return math.log10(self.nsa_mstar)
        except ValueError:
            return -9999.0
        except TypeError:
            return None

    @nsa_logmstar.expression
    def nsa_logmstar(cls):
        return func.log(cls.nsa_mstar)

now the print statement :  print 'sample nsa log mstar', Sample.nsa_logmstar
returns 

sample nsa log mstar log(mangadatadb.sample.nsa_mstar)


On Friday, January 15, 2016 at 4:28:31 PM UTC-5, Simon King wrote:
>
> Does my test script produce the right output for you in your installation? 
>
> What does the print statement immediately after the class definition 
> produce? 
>
> Simon 
>
> > On 15 Jan 2016, at 19:10, Brian Cherinka <havo...@gmail.com 
> <javascript:>> wrote: 
> > 
> > Actually, the class definition is entirely what I posted in the original 
> message.  I didn't cut anything out of that.  I don't define the columns in 
> mine, as you did.  The property nsa_logmstar is not defined anywhere else 
> in the class or in any other place in this code, or in any code that 
> interacts with this code.     
> > 
> > class Sample(Base,ArrayOps): 
> >    __tablename__ = 'sample' 
> >    __table_args__ = {'autoload' : True, 'schema' : 'datadb'} 
> > 
> >    def __repr__(self): 
> >        return '<Sample (pk={0},cube={1})'.format(self.pk,self.cube) 
> > 
> >    @hybrid_property 
> >    def nsa_logmstar(self): 
> >        try: return math.log10(self.nsa_mstar) 
> >        except ValueError as e: 
> >            return -9999.0 
> >        except TypeError as e: 
> >            return None 
> > 
> >    @nsa_logmstar.expression 
> >    def nsa_logmstar(cls): 
> >        return func.log(cls.nsa_mstar)   
> > 
> > My database connection is a singleton and my base is defined inside 
> that, essentially 
> > 
> > engine = create_engine(database_connection_string) 
> > Base = declarative_base(bind=engine) 
> > 
> > Brian 
> > 
> > On Friday, January 15, 2016 at 9:43:39 AM UTC-5, Simon King wrote: 
> > What happens if you put the print statement immediately after the class 
> definition? Is there any chance that you've got "nsa_logmstar = None" 
> somewhere in your class definition? 
> > 
> > Here's a test script which appears to work: 
> > 
> > import math 
> > 
> > import sqlalchemy as sa 
> > import sqlalchemy.orm as saorm 
> > from sqlalchemy.ext.hybrid import hybrid_property 
> > from sqlalchemy.ext.declarative import declarative_base 
> > 
> > Base = declarative_base() 
> > 
> > class Sample(Base): 
> >     __tablename__ = 'sample' 
> > 
> >     pk = sa.Column(sa.Integer, primary_key=True) 
> >     nsa_mstar = sa.Column(sa.Float) 
> > 
> >     @hybrid_property 
> >     def nsa_logmstar(self): 
> >         try: 
> >             return math.log10(self.nsa_mstar) 
> >         except ValueError: 
> >             return -9999.0 
> >         except TypeError: 
> >             return None 
> > 
> >     @nsa_logmstar.expression 
> >     def nsa_logmstar(cls): 
> >         return sa.func.log(cls.nsa_mstar) 
> > 
> > 
> > if __name__ == '__main__': 
> >     sm = saorm.sessionmaker() 
> >     session = sm() 
> >     print session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) 
> > 
> > 
> > And here's the output: 
> > 
> > 
> > SELECT sample.pk AS sample_pk 
> > FROM sample 
> > WHERE log(sample.nsa_mstar) < :log_1 
> > 
> > 
> > Simon 
> > 
> > 
> > On Fri, Jan 15, 2016 at 2:23 PM, Brian Cherinka <havo...@gmail.com> 
> wrote: 
> > Ahh.  Thanks.  Here is the class side then.  Still None. 
> > 
> > In [14]: print datadb.Sample.nsa_logmstar 
> > None 
> > 
> > Brian 
> > 
> > On Friday, January 15, 2016 at 8:48:30 AM UTC-5, Simon King wrote: 
> > "Sample()" is an instance. "Sample" is the class. Try: 
> > 
> >     print datadb.Sample.nsa_logmstar 
> > 
> > Simon 
> > 
> > On Fri, Jan 15, 2016 at 1:46 PM, Brian Cherinka <havo...@gmail.com> 
> wrote: 
> > Hi Simon, 
> > 
> > Printing on the class side, I get 
> > 
> > In [11]: print datadb.Sample().nsa_logmstar 
> > None 
> > 
> > It looks like it's getting set to None (or remaining None).  I'm not 
> quite sure what this tells me, except that it's not working.  Printing on 
> in the instance side, I get 
> > 
> > In [12]: print cube.sample[0].nsa_mstar 
> > 1386160000.0 
> > 
> > In [13]: print cube.sample[0].nsa_logmstar 
> > 9.14181336239 
> > 
> > nsa_mstar is a column in my database table, and nsa_logmstar I want to 
> be simply the log-base10 of that quantity.   
> > 
> > If this doesn't give any insight, then it will take me some time to 
> provide a small script.  This code is embedded into a bunch of stuff.  But 
> I'll work on it.   
> > 
> > Brian 
> > 
> > 
> > On Friday, January 15, 2016 at 5:00:51 AM UTC-5, Simon King wrote: 
> > On Fri, Jan 15, 2016 at 6:16 AM, Brian Cherinka <havo...@gmail.com> 
> wrote: 
> > I'm trying to set up a hybrid property / expression in a custom class, 
> that I can use in queries. I think I have the syntax correct, however the 
> query returns the entire table, instead of the correct subset of results. 
>  And the where clause just indicates True rather than the correct 
> expression. 
> > 
> > 
> > 
> > Here is my hybrid property/expression definition 
> > 
> > 
> > class Sample(Base,ArrayOps): 
> >    __tablename__ = 'sample' 
> >    __table_args__ = {'autoload' : True, 'schema' : 'datadb'} 
> > 
> >    def __repr__(self): 
> >        return '<Sample (pk={0},cube={1})'.format(self.pk,self.cube) 
> > 
> >    @hybrid_property 
> >    def nsa_logmstar(self): 
> >        try: return math.log10(self.nsa_mstar) 
> >        except ValueError as e: 
> >            return -9999.0 
> >        except TypeError as e: 
> >            return None 
> > 
> >    @nsa_logmstar.expression 
> >    def nsa_logmstar(cls): 
> >        return func.log(cls.nsa_mstar)   
> > 
> > The session query is 
> > 
> > session.query(Sample.pk).filter(Sample.nsa_logmstar < 9) 
> > 
> > But printing it does not show the appropriate condition. I get 
> > 
> > SELECT datadb.sample.pk AS datadb_sample_pk, 
> > FROM datadb.sample 
> > WHERE true 
> > 
> > and the results return the entire table of ~11000 rows instead of the 
> expected 272 rows. What's going on here?  Everything looks correct to me, 
> but I can't figure it out.   
> > 
> > I'm expecting the SQL statement to look like this 
> > 
> > select s.pk 
> > from datadb.sample as s 
> > where log(s.nsa_mstar) < 9; 
> > 
> > Any thoughts?  Thanks. 
> > 
> > 
> > I can't see anything obviously wrong with your code, but it looks like 
> Sample.nsa_logmstar is not actually resolving to the hybrid property in 
> your query. What happens if you "print Sample.nsa_logmstar" just before the 
> query? 
> > 
> > Otherwise, please provide a small runnable script that demonstrates the 
> problem. 
> > 
> > Simon 
> > 
> > -- 
> > 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 https://groups.google.com/d/optout. 
> > 
> > 
> > -- 
> > 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 https://groups.google.com/d/optout. 
> > 
> > 
> > -- 
> > 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 <javascript:>. 
> > To post to this group, send email to sqlal...@googlegroups.com 
> <javascript:>. 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>
>

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

Reply via email to