L
Laszlo Zsolt Nagy
Hello,
Is it possible to tell, which instance was used to call the classmethod
that is currently running?
Background: I have a class called DatabaseConnection and it has a
classmethod called process_create_tables. This method should create some
database tables defined by a database definition object. The
DatabaseConnection has many descendants, for example
PostgreSQLConnection. 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 DatabaseConnection instance. In that case, I only want to
create the tables that do not exists yet. Examples:
processor = SQLProcessors.StdOutProcessor() # Print to stdout
PostgreSQLConnection.process_create_tables(processor,dbdef) # This
sould create all tables, using the processor
processor = SQLProcessors.DirectProcessor(conn) # Execute directly
conn.process_create_tables(processor,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,processor,dbdef,conn=None)
and then calling it as
conn.process_create_tables(processor,dbdef,conn)
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
Is it possible to tell, which instance was used to call the classmethod
that is currently running?
Background: I have a class called DatabaseConnection and it has a
classmethod called process_create_tables. This method should create some
database tables defined by a database definition object. The
DatabaseConnection has many descendants, for example
PostgreSQLConnection. 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 DatabaseConnection instance. In that case, I only want to
create the tables that do not exists yet. Examples:
processor = SQLProcessors.StdOutProcessor() # Print to stdout
PostgreSQLConnection.process_create_tables(processor,dbdef) # This
sould create all tables, using the processor
processor = SQLProcessors.DirectProcessor(conn) # Execute directly
conn.process_create_tables(processor,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,processor,dbdef,conn=None)
and then calling it as
conn.process_create_tables(processor,dbdef,conn)
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