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

How to fine tune the redundant code in python

I am trying to create two set of databases every time inside my python script for the same of which I have written the below set of code which looks redundant to me since I am initializing the variable ext 2 times and hence if anyone can suggest some better alternatives, that would be really helpful.

def create_datasets(database, ext):
    try:
        dataset = "bq --location=US mk -d " + database + ext

        try:
            return_cd, out, err = run_sys_command(dataset)

        except Exception as e:
            print(e)

    except Exception as e:
        print(e)
        raise

ext = ''
create_datasets(database, ext)
ext = '_stg'
create_datasets(database, ext)

>Solution :

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

Use a loop?

for ext in ['', '_stg']:
    create_datasets(database, ext)

About your function:

def create_datasets(database, ext):
    try:
        dataset = f"bq --location=US mk -d {database}{ext}"
        return_cd, out, err = run_sys_command(dataset)
    except Exception as e:  # <- you should catch sub exception!
        print(e)
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