How to organize all the different parameters in a separated file

Viewed 154

I am writing a long code that uses many constants, file names, flags, etc. I want to organize all of them neatly in a separate text file named parameters.py and hopefully call them as

import parameters as p

print(p.Constants.speed_of_light)
print(p.Constants.mass_of_sun)
print(p.Constants.hour2seconds)

print(p.Flags.make_plot)
print(p.Flags.save_results)
print(p.Flags.print_results)

print(p.File_names.results)
print(p.File_names.backup)
print(p.File_names.diagnostic)

I expect the output of above to be something like:

3e8
2e30
3600

True
False
True

'file1.txt'
'file2.txt'
'file3.txt'

I am still a beginner at Python so I am not sure how to achieve it. Any help is appreciated!

2 Answers

So, parameters.py should look like

class Constants:
    speed_of_light = 3e8

class Flags:
   make_plot = True

class File_names:
    results = 'file1.txt'

You can do that just as you said. Create parameters.py in the same directory and declare classes(constants, flags etc) with variables in it. Then you can use them with the code you wrote here.

Related