A "standard" way to create a configuration file suitable for Python and Java together

My application was 100% developed in pure Python. It was very convenient and convenient for me to create a configuration file with the extension .py , and not just load it into each code. Something like that:

 ENV = 'Dev' def get_settings(): return eval(ENV) class Dev(): ''' Development Settings ''' # AWS settings aws_key = 'xxxxxxxxxxxxx' aws_secret = 'xxxxxxxxxxxxxxxxx' # S3 settings s3_bucket = 'xxxxxxxxxx' ... ... 

And than in my code, I just import this file and use the settings, so I have one file that is easily managed and which contains all the parameters that I need.

Recently, I had to move part of my Java code. And now I'm struggling with configurations.

Question:

What is the โ€œstandardโ€ way to create a configuration that will be easily accessible from both languages? (My Java skills are very limited, if you can give me a fifth example in Java, that would be great)

+6
source share
2 answers

Check out ConfigParser in Python

The syntax for this file is as follows:

 [Section] my_option = 12.2 my_option2 = other_value [Section2] my_voption = my_value 

You read it as follows:

 import ConfigParser config = ConfigParser.ConfigParser() config.read('example.cfg') config.getfloat('Section', 'my_option') # returns 12.2 

It works for several types, and if you cannot find the type, you can use the eval function in Python. Finding the Java equivalent should not be too complicated. Or even parsing this file using Java doesn't have to be too complicated.

+6
source

Use standard configuration files: for example XML or .properties.

For XML, you can use JDOM in Java, minidom in Python.

For .properties, Java processes this nativaly through the Properties class, and Python can handle this through ConfigParser

+2
source

Source: https://habr.com/ru/post/953491/


All Articles