Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize all the different parameters in a separated file

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!

like image 727
Stefano Avatar asked Feb 03 '26 10:02

Stefano


1 Answers

So, parameters.py should look like

class Constants:
    speed_of_light = 3e8

class Flags:
   make_plot = True

class File_names:
    results = 'file1.txt'
like image 85
sehigle Avatar answered Feb 05 '26 00:02

sehigle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!