Nathan B
Nathan B

Reputation: 11

Python variable when importing second file in Python

I'm very new to Python and am working to redo my groovy/PowerShell code in Python. I've hit a snag when trying to import from another python file.

I have a list of dictionaries in a second python file that I call with the code below

from testdict import Dev1

in this case I am only importing a single Dictionary called Dev1, but I have 15 dictionaries in that file. I need to be able to variablize that and I think I can do it one of 2 ways.

either import the entire file (would rather not but I can) and then only select the dictionary I need. However I hit a snag when I try to reference the specific dictionary.

import testdict
dictionary =  testdict.env -- angry

Or I can only import the single dictionary, but I hit a snag in the import statement

from testdict import env -- angry

Again, the code works without a variable for the dictionary, but I need to variablize it so my code works in more than one situation.

Below is the code I currently have, and all I'm looking to do is replace Dev1 with a variable I can pass into the method.

The script is doing a find and replace on T-SQL files based on the dictionary selected.

Any help would be appreciated!

    def main(inputfile, outputfile, env):
        from testdict import Dev1
        #import testdict
        print (env)
            

        with open(inputfile, 'r') as file :
            filedata = file.read()


        dictionary =  Dev1
        for key in dictionary.keys():
            filedata = filedata.replace(key, dictionary[key])

        # Write the file out again
        with open(outputfile, 'w') as file:
            file.write(filedata)

    main('DropandAddDBUsers.sql','DropandAddDBUsers2.sql', 'Dev1')

Upvotes: 1

Views: 58

Answers (3)

Nathan B
Nathan B

Reputation: 11

Thanks so much for both responses!! I went with the method from Sal as I can keep my dictionaries readable (from my limited knowledge it looks like I need to make each sub dictionary on a single line for Barmar's approach and I have 60+ values).

For any interested in the the final result here it is:

    def main(inputfile, outputfile,env):
        #from testdict import alldicts
        import testdict
        print (env)
        env1 = getattr(testdict, env)
        


        


        with open(inputfile, 'r') as file :
            filedata = file.read()


        dictionary =  env1
        for key in dictionary.keys():
            filedata = filedata.replace(key, dictionary[key])

        # Write the file out again
        with open(outputfile, 'w') as file:
            file.write(filedata)

    main('DropandAddDBUsers.sql','DropandAddDBUsers2.sql', 'Dev1')

Upvotes: 0

salparadise
salparadise

Reputation: 5805

You can get to a module's top level attributes/functions/classes as keys using getattr:

You can do something like this:

import testdict

Dev1  = getattr(testdict, 'Dev1')

or:

for data in ('Dev1', 'Dev2'):
   current_dict = getattr(testdict, data)

Upvotes: 0

Barmar
Barmar

Reputation: 781004

In testdict.py put all your dictionaries in another dictionary. Then you can select an entry from that dictionary.

alldicts = {'dev1': Dev1, 'dev2': Dev2, ...}

Then in the main script:

from testdict import alldicts

dictionary = alldicts[env]

Upvotes: 2

Related Questions