Is there a way to create a list of classes, but not call them until you loop through them?
I've created a list of classes that I'm importing from another file, there are about 50 which is why I'm trying to do it this way. The below list is in my __init__.py
file:
TABLES = [
Accounts(),
Activities()
]
but I want to actually call them here because I need to add company
names for each table.
for table in TABLES:
snowflake = SnowflakeLoadOperator(
task_id=f'load_{table.name}_to_snowflake',
table=table(company='company_name_1'),
partition=table.get_location_partition(ds)
)
for table in TABLES:
snowflake = SnowflakeLoadOperator(
task_id=f'load_{table.name}_to_snowflake',
table=table(company='company_name_2'),
partition=table.get_location_partition(ds)
)
but I keep seeing the error:
table=table(company='company_name_1'), TypeError: 'Accounts' object is not callable
This is what one of my classes look like, similar methods but different values:
class Accounts(Table):
name = None
schema = 'accounts'
def __init__(self, company=None):
self.name = f'{company}_accounts'
self.location_base = f's3://my_bucket/{self.schema.upper()}/{company}/accounts/'
Omit the parentheses in the definition of TABLES
and you'll store references to the classes themselves, rather than constructing instances of them:
TABLES = [
Accounts,
Activities
]
Then you'll be able to call them when looping later, using table(arg)
the same way you'd use Accounts(arg)
or Activities(arg)
.
Very simple fix. Your list right now contains instances of the classes, because you're instantiating them.
Just drop the parentheses after the class name to get a list of classes:
TABLES = [Accounts, Activities]
and then it should work, because now TABLES
contains the classes themselves, not some instantiation of them.