带空格的 Python 方法参数
我想创建一个简单的文件格式/DSL,允许我的用户输入数据。 我的系统是Python 的,使用Python 的解析器很有吸引力。 像这样定义数据元素的语法似乎非常方便。
Allocation(Param1 = Val1, Param2 = Val2 )
但是,它不支持带空格的参数名称。
Allocation(Param 1 = Val1, Param 2 = Val2 )
Python 解析器友好版本如下所示,但对用户不太友好。
Allocation(("Param 1",Val1), ("Param 2",Val1) )
Allocation(**{"Param 1":Val1, "Param 2":Val1} )
有没有办法让 python 更具可读性?
I would like to create a simple file format/DSL which would allow my users to input data. My system is in python and using python's parser is appealing.
Syntax like this for defining a data element seems quite convenient.
Allocation(Param1 = Val1, Param2 = Val2 )
However, it does not support param names with spaces.
Allocation(Param 1 = Val1, Param 2 = Val2 )
Python parser friendly versions can look as follows, but not very user friendly.
Allocation(("Param 1",Val1), ("Param 2",Val1) )
Allocation(**{"Param 1":Val1, "Param 2":Val1} )
Is there a way to make this more readable in python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想会有某种方法可以做到这一点。 但我不得不问,
To this:
的可读性是否真的有足够大的差异才能产生如此大的差异? 我确信有一种方法可以做你想做的事,但我首先关心的是所付出的努力是否值得结果。
我对你现在想要做什么有了更好的理解,但我仍然认为你最终可能不得不牺牲一些可读性才能得到你想要的东西。 就我个人而言,我会选择这样的东西:
如果这不是您可以接受的东西,那么您可能需要重新考虑是否要使用 Python 作为 DSL,还是使用自行开发的东西。 对于大多数编程语言来说,允许空格会产生太多歧义。
如果您仍然想使用 python 来实现此目的,您可能需要考虑发布到 C-API SIG (SIGS)或者可能是 python-dev 列表(如最后的手段)。 我能想到的唯一方法是将 python 解释器嵌入到 C/C++ 程序中,并用它进行某种黑客攻击(这可能很困难!)。
I'd imagine that there would be some way to do it. But I feel compelled to ask, is there really a big enough difference in readability from this
To this:
to make that big a difference? I'm sure there's a way to do what you want to do, but my first concern is if the effort involved would be worth the result.
I have a better understanding of what you want to do now, but I still think that you might end up having to sacrifice some readability to get what you want. Personally, I would go with something like:
If that's not something you can go with, then you might want to reconsider whether you want to use Python for your DSL or go with something home-grown. Allowing whitespace allows too many ambiguities for most programming languages to allow it.
If you still want to pursue this with using python, you might want to consider posting to the C-API SIG (SIGS) or maybe the python-dev list (as a last resort). The only way that I can see to do this would be to embed the python interpreter into a C/C++ program and do some kind of hacking with it (which can be difficult!).
这是我的偏好。
这些是相对容易创建的类声明。 由此产生的结构也易于加工。
Here's my preference.
These are relatively easy class declarations to create. The resulting structure is pleasant to process, too.
您可以执行以下操作:
编辑:
您的编辑现在包含我的答案,所以不,没有更简单的方法可以在关键字参数中包含空格。
You can do this:
Edit:
Your edit now includes my answer so no, there is no easier way to have spaces in keyword arguments.
除非我在这里误解了您的基本前提,否则没有什么可以阻止您编写一个解析您自己的自定义语法的类,然后使用该自定义语法作为单参数字符串:
在本例中,分号充当名称-值-对分隔符,equals 代表名称-值分隔符。 此外,您可以轻松配置解析器以接受自定义分隔符作为对象构造函数的一部分。
如果编写解析器看起来太令人畏惧,请考虑(对于这样的语法)您可以通过简单地分割字符串
然后
快速获取名称-值对来获取值。 您还可以从已为 Python 编写的多个参数解析器中进行选择。
不可否认,这并不使用 Python 作为参数解析器,这是您必须在诸如此类的方法的简单性之间进行权衡的考虑因素。
Unless I am mistaking your basic premise here, there's nothing to stop you from writing a class that parses your own custom syntax, and then using that custom syntax as a single-argument string:
In this example, semicolons act as the name-value-pair separators, and equals represents the name-value separator. Moreover, you can easily configure your parser to accept custom delimiters as part of the object constructor.
If it seems too daunting to write a parser, consider that (for a syntax such as this) you could obtain your values by simply splitting the string on
and then on
to quickly obtain the name-value pairs. You also have the option to choose from any of several argument parsers already written for Python.
Admittedly, this does not use Python as the argument parser, which is a consideration you will have to balance against the simplicity of an approach such as this.