"标准"创建适合Python和Java的配置文件的方法 [英] "Standard" way of creating config file suitable for Python and Java together

查看:159
本文介绍了"标准"创建适合Python和Java的配置文件的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序100%是用纯Python开发的。我发现使用 .py 扩展名创建配置文件非常有用且容易,而不是简单地在每个代码中加载它。这样的事情:

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.

最近我不得不把部分代码移到Java上。而现在我正在努力配置。

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

创建可以从两者轻松访问的配置的标准方法是什么?语言? (我的Java技能非常有限,如果你能用Java给我一个简单的例子就会很棒)

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)

推荐答案

有一个查看Python中的 ConfigParser

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

你这样读了:

import ConfigParser

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

它适用于几种类型,如果找不到类型,可以在Python中使用 eval 函数。找到与Java相当的东西应该不会太难。甚至使用Java解析这个文件应该不会太难。

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.

这篇关于"标准"创建适合Python和Java的配置文件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆