setters and getters on an automapped table

66 views
Skip to first unread message

Robert Minsk

unread,
Aug 25, 2016, 9:24:29 PM8/25/16
to sqlalchemy
I am using automap_base to reflect a table.

metadata = MetaData()
metadata.reflect(bind=engine, only=['hosts'])
automap = automap_base(metadata=metadata)
automap.prepare()

Hosts = automap.classes.hosts

The mac_address column in Hosts is  binary and would like to to replace the mac_address with some sort of getter/setter to decode/encode the address.  I have not found an example of doing this with an automapped table.  Is there a easy way to do this?

Simon King

unread,
Aug 26, 2016, 4:33:31 AM8/26/16
to sqlal...@googlegroups.com
You can override specific columns on an automapped table by
pre-declaring the class:

http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html#specifying-classes-explicitly

I haven't tried it, but I would assume that you can use any of the
normal facilities for modifying column behaviour at that point. For
example, I think you could do something like this:

class Hosts(automap):
__tablename__ = 'hosts'
_mac_address = Column('mac_address', BINARY)
@property
def mac_address(self):
return decode_mac_address(self._mac_address)

@mac_address.setter
def mac_address(self, value):
self._mac_address = encode_mac_address(value)

If you want to be able to query using the decoded values, you could
also try hybrid properties.

Hope that helps,

Simon

Robert Minsk

unread,
Aug 29, 2016, 7:09:43 PM8/29/16
to sqlal...@googlegroups.com
When I try to do that I get

 File "/home/CORP/robert.minsk/venvs/lfw/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 212, in __getattr__

    raise AttributeError(key)

AttributeError: hosts



--
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/uxto-cNocFw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscribe@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.

Simon King

unread,
Aug 30, 2016, 4:39:31 AM8/30/16
to sqlal...@googlegroups.com
Which line is triggering that error?

Here's a working example:

import sqlalchemy as sa
import sqlalchemy.orm as saorm
from sqlalchemy.ext.automap import automap_base

Base = automap_base()


class Host(Base):
__tablename__ = 'hosts'

_mac_address = sa.Column('mac_address', sa.Integer)

@property
def mac_address(self):
return '%012x' % self._mac_address

@mac_address.setter
def mac_address(self, value):
self._mac_address = int(value, 16)


def main():
engine = sa.create_engine('sqlite://')

engine.execute("""
CREATE TABLE hosts(
id INTEGER PRIMARY KEY,
mac_address INTEGER
)
""")

Base.prepare(engine, reflect=True)

session = saorm.sessionmaker(bind=engine)()

host = Host(mac_address='001122334455')
session.add(host)
session.commit()

host = session.query(Host).one()
print host.mac_address
print host._mac_address

if __name__ == '__main__':
main()
>> 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.

Robert Minsk

unread,
Aug 30, 2016, 5:55:33 PM8/30/16
to sqlal...@googlegroups.com
That worked.  My problem is I was using Base.classes.hosts and not the class Hosts.


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

> 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 a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/uxto-cNocFw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages