Hello,
Is it possible to tell, which instance was used to call the classmethod
that is currently running?
Background: I have a class called DatabaseConnect ion and it has a
classmethod called process_create_ tables. This method should create some
database tables defined by a database definition object. The
DatabaseConnect ion has many descendants, for example
PostgreSQLConne ction. Descendants know how to create tables in a given
RDBMS type. I also use subclasses of the 'SQLProcessor' class, that
processes SQL commands in different ways (print to stdout, write to
file, execute directly in the database etc.) I would like to use the
process_create_ tables classmethod as is, because sometimes I only need
to save a SQL script. However, I also want to use the same classmethod
to create tables directly into an existing database. That database is
presented as a DatabaseConnect ion instance. In that case, I only want to
create the tables that do not exists yet. Examples:
processor = SQLProcessors.S tdOutProcessor( ) # Print to stdout
PostgreSQLConne ction.process_c reate_tables(pr ocessor,dbdef) # This
sould create all tables, using the processor
processor = SQLProcessors.D irectProcessor( conn) # Execute directly
conn.process_cr eate_tables(pro cessor,dbdef) # This should create
non-existing tables only, using the processor
Is this possible? Maybe there is a better way to achieve this, I'm not
sure. I was thinking about this construct:
@classsmethod
def process_create_ tables(cls,proc essor,dbdef,con n=None)
and then calling it as
conn.process_cr eate_tables(pro cessor,dbdef,co nn)
but this looks very ugly to me. It would be much easier if I could tell
which instance (if any) was used to call the classmethod.
Thanks,
Les
Comment