带空格的 Python 方法参数

发布于 2024-07-11 02:57:35 字数 453 浏览 6 评论 0原文

我想创建一个简单的文件格式/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谜兔 2024-07-18 02:57:35

我想会有某种方法可以做到这一点。 但我不得不问,

Allocation(Param1 = Val1, Param2 = Val2 )

To this:

Allocation(Param 1 = Val1, Param 2 = Val2 )

的可读性是否真的有足够大的差异才能产生如此大的差异? 我确信有一种方法可以做你想做的事,但我首先关心的是所付出的努力是否值得结果。

我的目标是提供一个可用于将数据输入系统的 DSL。 在上面的场景中,参数将是人名,值将是百分比。

我对你现在想要做什么有了更好的理解,但我仍然认为你最终可能不得不牺牲一些可读性才能得到你想要的东西。 就我个人而言,我会选择这样的东西:

Allocation(
    { 'name1' : value1,
      'name1' : value2, }
)

如果这不是您可以接受的东西,那么您可能需要重新考虑是否要使用 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

Allocation(Param1 = Val1, Param2 = Val2 )

To this:

Allocation(Param 1 = Val1, Param 2 = Val2 )

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.

my goal is to provide a DSL which can be used for data entry into the system. In the above scenario, params would be people names and values would be percentages.

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:

Allocation(
    { 'name1' : value1,
      'name1' : value2, }
)

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!).

私藏温柔 2024-07-18 02:57:35

这是我的偏好。

AllocationSet(
    Alloc( name="some name", value=1.23 ),
    Alloc( name="another name", value=2.34 ),
    Alloc( name="yet another name", value=4.56 ),
)

这些是相对容易创建的类声明。 由此产生的结构也易于加工。

Here's my preference.

AllocationSet(
    Alloc( name="some name", value=1.23 ),
    Alloc( name="another name", value=2.34 ),
    Alloc( name="yet another name", value=4.56 ),
)

These are relatively easy class declarations to create. The resulting structure is pleasant to process, too.

天荒地未老 2024-07-18 02:57:35

您可以执行以下操作:

def Allocation(**kwargs):
    print kwargs

myargs = {"Param 1":Val1, "Param 2":Val1}
Allocation(**myargs)

编辑:
您的编辑现在包含我的答案,所以不,没有更简单的方法可以在关键字参数中包含空格。

You can do this:

def Allocation(**kwargs):
    print kwargs

myargs = {"Param 1":Val1, "Param 2":Val1}
Allocation(**myargs)

Edit:
Your edit now includes my answer so no, there is no easier way to have spaces in keyword arguments.

梦晓ヶ微光ヅ倾城 2024-07-18 02:57:35

除非我在这里误解了您的基本前提,否则没有什么可以阻止您编写一个解析您自己的自定义语法的类,然后使用该自定义语法作为单参数字符串:

Allocation("Param 1=Check Up; Param 2=Mean Value Theorem;")

在本例中,分号充当名称-值-对分隔符,equals 代表名称-值分隔符。 此外,您可以轻松配置解析器以接受自定义分隔符作为对象构造函数的一部分。

如果编写解析器看起来太令人畏惧,请考虑(对于这样的语法)您可以通过简单地分割字符串

/\s*;\s*/

然后

/\s*=\s*/

快速获取名称-值对来获取值。 您还可以从已为 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:

Allocation("Param 1=Check Up; Param 2=Mean Value Theorem;")

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

/\s*;\s*/

and then on

/\s*=\s*/

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文