Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading