On 06/07/2017 06:19 PM, Greg Silverman wrote:
Thanks, it worked!

I tried something similar using the @hybrid_property decorator, but could not get it to work. What exactly is the difference between that and the @property decorator?


@property is super normal Python stuff that's in all the Python tutorials and is something everyone should use all the time.

@hybrid_property is more of a magic trick that's specific to the kind of query interface that SQLAlchemy has - in that in an ORM like SQLAlchemy (and lots of others), you access attributes off of classes, as well as instances of those classes, as part of regular use of the API. This is not a standard object-oriented thing, it's kind of odd. The @hybrid_property allows you to build functions that act a lot like @property, but they accommodate for accessing an attribute both at the "instance" (e.g. 'self') level and at the "class" level. Which translated to SQLAlchemy means, at the "row I've loaded into memory" level and the "SQL expression I want to execute on the database" level. The kinds of functions we can use in these two very different contexts are quite different.







Greg--

On Wed, Jun 7, 2017 at 5:08 PM, mike bayer <mike...@zzzcomputing.com <mailto:mike...@zzzcomputing.com>> wrote:



    On 06/07/2017 02:31 PM, GMS wrote:

        I am sure this is easier than I am making it, but I just want to
        add a property to a class so that decimal representations get
        truncated at 3 decimal digits.

        My class is this:

        class Measures(Model):
        __tablename__= 'xcelera_measures'
        id= Column(Numeric,primary_key=True)
              studyidk= Column(Numeric, ForeignKey('xcel.studyidk'))
             xceleragroup= relationship("Xcelera")
              explainstring= Column(String(255))
              mrn= Column(String(255))
              value= Column(Numeric)

              __mapper_args__= {
                  "order_by": [mrn]
              }


        and 'value' is the property/attribute that I want to also have a
        truncated version available in the class.

        I tried adding this to the class value_new =
        column_property("%.3f" % value())

        but got an error that

        value_new = column_property("%.3f" % value)


"%" is a Python function, that doesn't execute on the database. This works as a normal Python descriptor:

    class MyClass(Base):
         # ...

         @property
         def value_new(self):
             return "%.3f" % self.value








        TypeError: float argument required, not Column

        I also tried this as a hybrid_property, but I don't think I was
        using it correctly.

        All I want to do is have the truncated version of my attribute
        available in my class.

        Thanks!

        Greg--


-- 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
        <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
        <mailto:sqlalchemy%2bunsubscr...@googlegroups.com>
        <mailto:sqlalchemy+unsubscr...@googlegroups.com
        <mailto:sqlalchemy%2bunsubscr...@googlegroups.com>>.
        To post to this group, send email to sqlalchemy@googlegroups.com
        <mailto:sqlalchemy@googlegroups.com>
        <mailto:sqlalchemy@googlegroups.com
        <mailto:sqlalchemy@googlegroups.com>>.
        Visit this group at https://groups.google.com/group/sqlalchemy
        <https://groups.google.com/group/sqlalchemy>.
        For more options, visit https://groups.google.com/d/optout
        <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
    <http://stackoverflow.com/help/mcve> for a full description.
    --- You received this message because you are subscribed to a topic
    in the Google Groups "sqlalchemy" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/sqlalchemy/3FFm1stwEoU/unsubscribe
    <https://groups.google.com/d/topic/sqlalchemy/3FFm1stwEoU/unsubscribe>.
    To unsubscribe from this group and all its topics, send an email to
    sqlalchemy+unsubscr...@googlegroups.com
    <mailto:sqlalchemy%2bunsubscr...@googlegroups.com>.
    To post to this group, send email to sqlalchemy@googlegroups.com
    <mailto:sqlalchemy@googlegroups.com>.
    Visit this group at https://groups.google.com/group/sqlalchemy
    <https://groups.google.com/group/sqlalchemy>.
    For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.




--
Greg M. Silverman

  › flora-script <http://flora-script.grenzi.org/> ‹
**› grenzi.org <http://grenzi.org/> ›

--
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 <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto: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.

Reply via email to