我有一个项目,在JSON文件中指定了一些常见的配置,这些配置由简单的Python解析器处理,该文件以各种语言生成源文件,其中包含JSON文件中指定的一堆常数。简而言之,一个配置文件之一指定了一堆参数:
{
"param1":
{
"type": "bool",
"displayName": "Name 1",
"defaultValue": false
},
"param2":
{
"type": "float",
"displayName": "Name 2",
"defaultValue": 0.0,
"valueRange": [-24.0, 24.0],
"interval": 0.1
},
"param3":
{
"type": "float",
"displayName": "Name 3",
"defaultValue": 0.0,
"valueRange": [-24.0, 24.0],
"interval": 0.1
},
"param4":
{
"type": "float",
"displayName": "Name 4",
"defaultValue": 0.0,
"valueRange": [-24.0, 24.0],
"interval": 0.1
}
现在,在我的真实情况下,最多可以有50个参数,每个参数具有更多的属性。如上面的示例所示, param2
, param3
和 param4
具有相同的属性,除了其 display> displayname
。
我很想找到一些可以在这里而不是JSON使用的替代配置语言,这使我可以指定类似类,也许像这个伪代码smippet:
class GenericBoolParam (name_, defaultValue_)
{
"type": "bool",
"displayName": name_,
"defaultValue": defaultValue_
}
class SpecialFloatParam (name_)
{
"type": "float",
"displayName": name_,
"defaultValue": 0.0,
"valueRange": [-24.0, 24.0],
"interval": 0.1
}
{
"param1" : GenericBoolParam ("Name1, false),
"param2" : SpecialFloatParam ("Name2),
"param3" : SpecialFloatParam ("Name3),
"param4" : SpecialFloatParam ("Name4)
}
确切的语法并不像表达这些表达这些的可能性那样重要重复模式。用Python解释该语言的现成使用实施将是一个加号,但不是强制性的。我不知道任何合适的语言,所以我希望在这里找到一些合适语言的提示。
I have a project where some common configurations are specified in json files which are processed by a simple python parser which generates source files in various languages which contain a bunch of constants specified in the json file. As a simplified example, one of the config files specify a bunch of parameters:
{
"param1":
{
"type": "bool",
"displayName": "Name 1",
"defaultValue": false
},
"param2":
{
"type": "float",
"displayName": "Name 2",
"defaultValue": 0.0,
"valueRange": [-24.0, 24.0],
"interval": 0.1
},
"param3":
{
"type": "float",
"displayName": "Name 3",
"defaultValue": 0.0,
"valueRange": [-24.0, 24.0],
"interval": 0.1
},
"param4":
{
"type": "float",
"displayName": "Name 4",
"defaultValue": 0.0,
"valueRange": [-24.0, 24.0],
"interval": 0.1
}
Now in my real-world case, there can be up to 50 parameters with each single parameter having a lot more properties. As you see in the example above, param2
, param3
and param4
have the same properties, except for their displayName
.
I'd love to find some alternative configuration language that can be used here instead of json, which would allow me to specify something like classes, maybe like this pseudo-code snippet:
class GenericBoolParam (name_, defaultValue_)
{
"type": "bool",
"displayName": name_,
"defaultValue": defaultValue_
}
class SpecialFloatParam (name_)
{
"type": "float",
"displayName": name_,
"defaultValue": 0.0,
"valueRange": [-24.0, 24.0],
"interval": 0.1
}
{
"param1" : GenericBoolParam ("Name1, false),
"param2" : SpecialFloatParam ("Name2),
"param3" : SpecialFloatParam ("Name3),
"param4" : SpecialFloatParam ("Name4)
}
The exact syntax is not as important as the possibility to express these repeated pattern. A ready-to-use implementation to interpret the language with python would be a plus but is not mandatory. I am not aware of any suitable language, so I'm hoping to find some hints for a suitable language here.
发布评论
评论(2)
jsonnet 想到:它几乎具有您描述的内容,称它们为'函数'而不是'classs'。有多个实现,一个在C ++中,一个在GO中。前者具有a 简单的python binding 。
示例:您可以将其馈送到现有程序。
以主页: “变为:
JSONNET程序的输出为JSON”文档的
Jsonnet comes to mind: it has almost exactly what you describe, calling them 'functions' rather than 'classes'. Multiple implementations are available, one in C++ and one in Go; the former has a simple Python binding.
Taking an example from the homepage:
becomes:
Since the output of a Jsonnet program is a JSON document, you can feed that to your existing program.
config4*我开发的,可在C ++和Java中使用,以及其
@copyfrom 语句将所有值从指定范围复制到当前范围中。
以config4*格式重写示例配置文件的一种方法如下:
如果要读取config4*的概述,我建议您阅读第2章(“ config4* andtax概述”)和第3章(“ “ config4*入门”指南的config4* api”)(手动的pdf ,,)。
不存在python的python实现。如果您确定config4*非常适合您的需求,而不是将其移植到python(我来宾需要超过1个月的努力),那么在Java或C ++上编写实用程序会更简单API可以解析配置文件,导航分析的信息并使用打印语句来写出详细信息,例如Python或JSON格式。您的Python应用程序可以执行实用程序并解析其输出的文件。
如果您决定编写这样的实用程序,这里有一些提示可以使您朝正确的方向启动:
解析配置文件后,在根范围内开始并使用
listellyscopedNames()
或listLocallyScopedNames()
操作获取该范围内的条目列表。
使用
type()
操作确定条目是否为字符串变量,列表变量或子尺寸。如果是子镜,那么
使用递归检查子镜。递归时,
mergenames()
操作对于将子尺寸的名称附加到
当前范围的名称..
呼叫
lookupstring()
检索字符串变量的值,并且lookuplist()
检索列表变量的值。我的来宾是,写这样的实用程序将不到一天。
Config4*, which I developed, is available in C++ and Java, and its
@copyFrom
statement copies all the values from the specified scope into the current scope.One way to rewrite your example configuration file in Config4* format is as follows:
If you want to read an overview of Config4*, then I suggest you read Chapter 2 ("Overview of Config4* Syntax") and Chapter 3 ("Overview of Config4* API") of the "Config4* Getting Started" guide (PDF of manual, HTML Chapter 2, HTML Chapter 3).
A Python implementation of Config4* does not exist. If you decide that Config4* is a good fit for your needs, then rather than porting it to Python (which I guestimate would take more than 1 month of effort), it would be simpler to write a utility in Java or C++ that uses the API to parse a configuration file, navigates the parsed information and uses print statements to write out the details in, say, Python or JSON format. Your Python application could execute the utility and parse the file it outputs.
If you decide to write such a utility, here are some hints to get you started in the right direction:
After parsing a configuration file, start at the root scope, and use
the
listFullyScopedNames()
orlistLocallyScopedNames()
operationto obtain a list of the entries within that scope.
Use the
type()
operation to determine whether an entry is a stringvariable, a list variable or a sub-scope. If it is a sub-scope, then
use recursion to examine the sub-scope. When recursing, the
mergeNames()
operation will be useful to append the name of a sub-scope onto the
name of the current scope..
Call
lookupString()
to retrieve the value of string variables, andlookupList()
to retrieve the value of list variables.My guestimate is that it would take less than a day to write such a utility.