Re: [sqlalchemy] Extending sqlalchemy.schema.Table
It's a horrible hack, but did you know that you can change the class of an instance by assigning to its __class__ attribute? I've no idea if SA does anything that would stop this from working, but you could try it. Start by creating a subclass of Table with your extra methods, then iterate over each table in the metadata, setting the __class__ attribute to your subclass. Hope that helps, Simon On Thu, Aug 1, 2013 at 12:17 AM, Gustavo Baratto gbara...@gmail.com wrote: Thanks for the reply Michael... I had a hunch this wouldn't be easy to tackle, but this is more than I can chew at the moment: ) For now, I'll just keeping doing what I'm already doing which is to instantiate a new class taking the table as an argument, and then within my class reference the table's attributes most likely to be used (See below). If there is a more elegant way of doing this, I'm all ears ;) Thanks! Class MyGenericTable(object): def __init__(self, table): self.tablename = table @property def metadata(self): return DB.instance().metadata @property def table(self): return self.metadata.tables[self.tablename] @property def columns(self): return self.table.c @property def c(self): return self.columns @property def primary_key(self): return self.table.primary_key def select(self, data): my select some custom code def insert(self, data): my insert some custom code On Wed, Jul 31, 2013 at 10:57 AM, Michael Bayer mike...@zzzcomputing.com wrote: oh, except you might have problems with tables in there that are reflected due to a foreign key.Table is not really intended for subclassing, unless you want to do SQLAlchemy-Migrate's approach of monkeypatching Table.__bases__ at the global level, I'd seek some other way to achieve what you want. On Jul 31, 2013, at 1:56 PM, Michael Bayer mike...@zzzcomputing.com wrote: You'd probably implement your own reflect_all function/method: from sqlalchemy import inspect def reflect(metadata, bind): inspector = inspect(bind) for tname in inspector.get_table_names(): MySpecialTable(tname, metadata, autoload=True, autoload_with=bind) On Jul 31, 2013, at 1:34 PM, tiadobatima gbara...@gmail.com wrote: Hello there, When this application starts, we reflect the DB into a MetaData() object and this is made available for everyone to use. I'd like to add a few more methods to the table objects within that MetaData(). Is there any easy way to extend these already instantiated sqlalchemy.schema.Table objects? Thanks! :) -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Extending sqlalchemy.schema.Table
No offence but it feels like direct route to the hell. Monkeypatching is acknowledged to be really bad practise in general with few exemptions. On Thursday, August 1, 2013 9:47:45 AM UTC-4, Simon King wrote: It's a horrible hack, but did you know that you can change the class of an instance by assigning to its __class__ attribute? I've no idea if SA does anything that would stop this from working, but you could try it. Start by creating a subclass of Table with your extra methods, then iterate over each table in the metadata, setting the __class__ attribute to your subclass. Hope that helps, Simon On Thu, Aug 1, 2013 at 12:17 AM, Gustavo Baratto gbar...@gmail.comjavascript: wrote: Thanks for the reply Michael... I had a hunch this wouldn't be easy to tackle, but this is more than I can chew at the moment: ) For now, I'll just keeping doing what I'm already doing which is to instantiate a new class taking the table as an argument, and then within my class reference the table's attributes most likely to be used (See below). If there is a more elegant way of doing this, I'm all ears ;) Thanks! Class MyGenericTable(object): def __init__(self, table): self.tablename = table @property def metadata(self): return DB.instance().metadata @property def table(self): return self.metadata.tables[self.tablename] @property def columns(self): return self.table.c @property def c(self): return self.columns @property def primary_key(self): return self.table.primary_key def select(self, data): my select some custom code def insert(self, data): my insert some custom code On Wed, Jul 31, 2013 at 10:57 AM, Michael Bayer mik...@zzzcomputing.comjavascript: wrote: oh, except you might have problems with tables in there that are reflected due to a foreign key.Table is not really intended for subclassing, unless you want to do SQLAlchemy-Migrate's approach of monkeypatching Table.__bases__ at the global level, I'd seek some other way to achieve what you want. On Jul 31, 2013, at 1:56 PM, Michael Bayer mik...@zzzcomputing.comjavascript: wrote: You'd probably implement your own reflect_all function/method: from sqlalchemy import inspect def reflect(metadata, bind): inspector = inspect(bind) for tname in inspector.get_table_names(): MySpecialTable(tname, metadata, autoload=True, autoload_with=bind) On Jul 31, 2013, at 1:34 PM, tiadobatima gbar...@gmail.comjavascript: wrote: Hello there, When this application starts, we reflect the DB into a MetaData() object and this is made available for everyone to use. I'd like to add a few more methods to the table objects within that MetaData(). Is there any easy way to extend these already instantiated sqlalchemy.schema.Table objects? Thanks! :) -- 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.comjavascript:. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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.comjavascript:. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Extending sqlalchemy.schema.Table
as I mentioned earlier, SQLAlchemy-Migrate has done it this way for years. I'm not a fan of it which was one of several reasons I wrote Alembic, but it does work for them. On Aug 1, 2013, at 10:21 AM, askel dummy...@mail.ru wrote: No offence but it feels like direct route to the hell. Monkeypatching is acknowledged to be really bad practise in general with few exemptions. On Thursday, August 1, 2013 9:47:45 AM UTC-4, Simon King wrote: It's a horrible hack, but did you know that you can change the class of an instance by assigning to its __class__ attribute? I've no idea if SA does anything that would stop this from working, but you could try it. Start by creating a subclass of Table with your extra methods, then iterate over each table in the metadata, setting the __class__ attribute to your subclass. Hope that helps, Simon On Thu, Aug 1, 2013 at 12:17 AM, Gustavo Baratto gbar...@gmail.com wrote: Thanks for the reply Michael... I had a hunch this wouldn't be easy to tackle, but this is more than I can chew at the moment: ) For now, I'll just keeping doing what I'm already doing which is to instantiate a new class taking the table as an argument, and then within my class reference the table's attributes most likely to be used (See below). If there is a more elegant way of doing this, I'm all ears ;) Thanks! Class MyGenericTable(object): def __init__(self, table): self.tablename = table @property def metadata(self): return DB.instance().metadata @property def table(self): return self.metadata.tables[self.tablename] @property def columns(self): return self.table.c @property def c(self): return self.columns @property def primary_key(self): return self.table.primary_key def select(self, data): my select some custom code def insert(self, data): my insert some custom code On Wed, Jul 31, 2013 at 10:57 AM, Michael Bayer mik...@zzzcomputing.com wrote: oh, except you might have problems with tables in there that are reflected due to a foreign key.Table is not really intended for subclassing, unless you want to do SQLAlchemy-Migrate's approach of monkeypatching Table.__bases__ at the global level, I'd seek some other way to achieve what you want. On Jul 31, 2013, at 1:56 PM, Michael Bayer mik...@zzzcomputing.com wrote: You'd probably implement your own reflect_all function/method: from sqlalchemy import inspect def reflect(metadata, bind): inspector = inspect(bind) for tname in inspector.get_table_names(): MySpecialTable(tname, metadata, autoload=True, autoload_with=bind) On Jul 31, 2013, at 1:34 PM, tiadobatima gbar...@gmail.com wrote: Hello there, When this application starts, we reflect the DB into a MetaData() object and this is made available for everyone to use. I'd like to add a few more methods to the table objects within that MetaData(). Is there any easy way to extend these already instantiated sqlalchemy.schema.Table objects? Thanks! :) -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. signature.asc Description: Message signed with OpenPGP using GPGMail
Re: [sqlalchemy] Extending sqlalchemy.schema.Table
I did this to quickly test my own extended ResultProxy object, but as soon figured out how easy was to properly instantiate a new ResultProxy subclass from an existing ResultProxy object, I got rid of this monkeypatching. I'd like to avoid that route if possible. Thanks! :) g. On Thu, Aug 1, 2013 at 6:47 AM, Simon King si...@simonking.org.uk wrote: It's a horrible hack, but did you know that you can change the class of an instance by assigning to its __class__ attribute? I've no idea if SA does anything that would stop this from working, but you could try it. Start by creating a subclass of Table with your extra methods, then iterate over each table in the metadata, setting the __class__ attribute to your subclass. Hope that helps, Simon On Thu, Aug 1, 2013 at 12:17 AM, Gustavo Baratto gbara...@gmail.com wrote: Thanks for the reply Michael... I had a hunch this wouldn't be easy to tackle, but this is more than I can chew at the moment: ) For now, I'll just keeping doing what I'm already doing which is to instantiate a new class taking the table as an argument, and then within my class reference the table's attributes most likely to be used (See below). If there is a more elegant way of doing this, I'm all ears ;) Thanks! Class MyGenericTable(object): def __init__(self, table): self.tablename = table @property def metadata(self): return DB.instance().metadata @property def table(self): return self.metadata.tables[self.tablename] @property def columns(self): return self.table.c @property def c(self): return self.columns @property def primary_key(self): return self.table.primary_key def select(self, data): my select some custom code def insert(self, data): my insert some custom code On Wed, Jul 31, 2013 at 10:57 AM, Michael Bayer mike...@zzzcomputing.com wrote: oh, except you might have problems with tables in there that are reflected due to a foreign key.Table is not really intended for subclassing, unless you want to do SQLAlchemy-Migrate's approach of monkeypatching Table.__bases__ at the global level, I'd seek some other way to achieve what you want. On Jul 31, 2013, at 1:56 PM, Michael Bayer mike...@zzzcomputing.com wrote: You'd probably implement your own reflect_all function/method: from sqlalchemy import inspect def reflect(metadata, bind): inspector = inspect(bind) for tname in inspector.get_table_names(): MySpecialTable(tname, metadata, autoload=True, autoload_with=bind) On Jul 31, 2013, at 1:34 PM, tiadobatima gbara...@gmail.com wrote: Hello there, When this application starts, we reflect the DB into a MetaData() object and this is made available for everyone to use. I'd like to add a few more methods to the table objects within that MetaData(). Is there any easy way to extend these already instantiated sqlalchemy.schema.Table objects? Thanks! :) -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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/EoTA-H1s-bQ/unsubscribe. To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
[sqlalchemy] Extending sqlalchemy.schema.Table
Hello there, When this application starts, we reflect the DB into a MetaData() object and this is made available for everyone to use. I'd like to add a few more methods to the table objects within that MetaData(). Is there any easy way to extend these already instantiated sqlalchemy.schema.Table objects? Thanks! :) -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Extending sqlalchemy.schema.Table
You'd probably implement your own reflect_all function/method: from sqlalchemy import inspect def reflect(metadata, bind): inspector = inspect(bind) for tname in inspector.get_table_names(): MySpecialTable(tname, metadata, autoload=True, autoload_with=bind) On Jul 31, 2013, at 1:34 PM, tiadobatima gbara...@gmail.com wrote: Hello there, When this application starts, we reflect the DB into a MetaData() object and this is made available for everyone to use. I'd like to add a few more methods to the table objects within that MetaData(). Is there any easy way to extend these already instantiated sqlalchemy.schema.Table objects? Thanks! :) -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. signature.asc Description: Message signed with OpenPGP using GPGMail
Re: [sqlalchemy] Extending sqlalchemy.schema.Table
oh, except you might have problems with tables in there that are reflected due to a foreign key.Table is not really intended for subclassing, unless you want to do SQLAlchemy-Migrate's approach of monkeypatching Table.__bases__ at the global level, I'd seek some other way to achieve what you want. On Jul 31, 2013, at 1:56 PM, Michael Bayer mike...@zzzcomputing.com wrote: You'd probably implement your own reflect_all function/method: from sqlalchemy import inspect def reflect(metadata, bind): inspector = inspect(bind) for tname in inspector.get_table_names(): MySpecialTable(tname, metadata, autoload=True, autoload_with=bind) On Jul 31, 2013, at 1:34 PM, tiadobatima gbara...@gmail.com wrote: Hello there, When this application starts, we reflect the DB into a MetaData() object and this is made available for everyone to use. I'd like to add a few more methods to the table objects within that MetaData(). Is there any easy way to extend these already instantiated sqlalchemy.schema.Table objects? Thanks! :) -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. signature.asc Description: Message signed with OpenPGP using GPGMail
Re: [sqlalchemy] Extending sqlalchemy.schema.Table
Thanks for the reply Michael... I had a hunch this wouldn't be easy to tackle, but this is more than I can chew at the moment: ) For now, I'll just keeping doing what I'm already doing which is to instantiate a new class taking the table as an argument, and then within my class reference the table's attributes most likely to be used (See below). If there is a more elegant way of doing this, I'm all ears ;) Thanks! Class MyGenericTable(object): def __init__(self, table): self.tablename = table @property def metadata(self): return DB.instance().metadata @property def table(self): return self.metadata.tables[self.tablename] @property def columns(self): return self.table.c @property def c(self): return self.columns @property def *primary_key*(self): return self.table.primary_key def select(self, data): my select some custom code def insert(self, data): my insert some custom code On Wed, Jul 31, 2013 at 10:57 AM, Michael Bayer mike...@zzzcomputing.comwrote: oh, except you might have problems with tables in there that are reflected due to a foreign key.Table is not really intended for subclassing, unless you want to do SQLAlchemy-Migrate's approach of monkeypatching Table.__bases__ at the global level, I'd seek some other way to achieve what you want. On Jul 31, 2013, at 1:56 PM, Michael Bayer mike...@zzzcomputing.com wrote: You'd probably implement your own reflect_all function/method: from sqlalchemy import inspect def reflect(metadata, bind): inspector = inspect(bind) for tname in inspector.get_table_names(): MySpecialTable(tname, metadata, autoload=True, autoload_with=bind) On Jul 31, 2013, at 1:34 PM, tiadobatima gbara...@gmail.com wrote: Hello there, When this application starts, we reflect the DB into a MetaData() object and this is made available for everyone to use. I'd like to add a few more methods to the table objects within that MetaData(). Is there any easy way to extend these already instantiated sqlalchemy.schema.Table objects? Thanks! :) -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.