1

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')
2
  • 2
    What do you mean by "angry"? Do you have a variable in testdict named env or not? If env is a variable that contains the name of the variable, you can use getattr(testdict, env), but you should probably have a single dict of dicts in testdict instead. Commented Aug 16, 2021 at 20:35
  • 1
    Whenever you need to export/import/share a collection of things, use a list (if numerically indexed) or a dict (otherwise). Commented Aug 16, 2021 at 20:38

3 Answers 3

2

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]
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.