Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Standard" way of creating config file suitable for Python and Java together

My application was 100% developed in pure Python. I found very useful and easy to create a config file with .py extension and than simply load it in every code. Something like this:

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 settings, this way I have one file that easy to manage and that contains all aprameters I need.

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

Question:

What are the "standard" way to create a config that would be easily accessible from both languages? (My Java skills are very limited, if you can five me a brief example in Java it would be great)

like image 227
Vor Avatar asked Sep 09 '13 14:09

Vor


People also ask

Does Python have a config file?

Python has a standard configuration library called “configparser”, which is very convenient and powerful, but does not address many common problems.

How do you make a good config file?

The most common and standardized formats are YAML, JSON, TOML and INI. A good configuration file should meet at least these 3 criteria: Easy to read and edit: It should be text-based and structured in such a way that is easy to understand. Even non-developers should be able to read.


1 Answers

Have a look at ConfigParser in Python

The syntax on this file looks like this:

[Section]
my_option = 12.2
my_option2 = other_value

[Section2]
my_voption = my_value

You read it this way:

import ConfigParser

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

It works for a couple of types and if you can't find a type, you can use the eval function in Python. Finding an equivalent to Java shouldn't be too hard. Or even parsing this file using Java shouldn't be too hard.

like image 146
Paco Avatar answered Oct 06 '22 01:10

Paco