Python 元类和 ModGrammar

发布于 2024-11-29 18:21:22 字数 909 浏览 4 评论 0原文

我发现(在 StackOverflow 上的另一个问题之后)这个用 Python 编写的有趣的库,其目标是语法解析。

http://code.google.com/p/modgrammar/

我还找到了这个教程关于它:

http://packages.python.org/modgrammar/tutorial.html

所以,读完所有教程后,我明白了这是什么 我在找!我尝试编写教程中的第一个示例:

from modgrammar import *
class MyGrammar (Grammar):
  grammar = (LITERAL("Hello,"), LITERAL("world!"))

但遇到了此错误:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from modgrammar import *
  File "/Users/tesi/Desktop/Prova/modgrammar/__init__.py", line 503
    class Grammar(metaclass=GrammarClass):
                           ^
SyntaxError: invalid syntax

问题似乎出在元类声明中。当我调用 python 解释器时,可能需要添加一个“编译标志”?有什么好消息吗?! :) 谢谢

I found (after another question here on StackOverflow) this interesting library written in Python which goal is the grammar parsing.

http://code.google.com/p/modgrammar/

And I also found this tutorial regarding it:

http://packages.python.org/modgrammar/tutorial.html

So, after reading all the tutorial, I understood that this is what I'm looking for! I tried to write the first example in the tutorial:

from modgrammar import *
class MyGrammar (Grammar):
  grammar = (LITERAL("Hello,"), LITERAL("world!"))

but I ran into this error:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    from modgrammar import *
  File "/Users/tesi/Desktop/Prova/modgrammar/__init__.py", line 503
    class Grammar(metaclass=GrammarClass):
                           ^
SyntaxError: invalid syntax

The problem seems to be in the metaclass declaration. Might be I have to add a "compilation flag" when I call the python interpreter? Some good news?! :) Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

素染倾城色 2024-12-06 18:21:22
class Grammar(metaclass=GrammarClass)

使用Python3语法。等效的 Python2 语法是,

class Grammar(object):
    __metaclass__=GrammarClass

但由于可能存在许多其他 Python3 主义,因此您可能必须使用 Python3 才能使用 modgrammar。

class Grammar(metaclass=GrammarClass)

is using Python3 syntax. The equivalent Python2 syntax would be

class Grammar(object):
    __metaclass__=GrammarClass

but since there may be lots of other Python3-isms, you may have to use Python3 to use modgrammar.

往日情怀 2024-12-06 18:21:22

我已将 modgrammar 向后移植到 Python 2.6+。 Backport 也适用于 Python 3.x。您可以在这里找到它:https://github.com/don-ramon/modgrammar-py2。

I've backport modgrammar to Python 2.6+. Backport will also work with Python 3.x. You can find it here: https://github.com/don-ramon/modgrammar-py2.

明月夜 2024-12-06 18:21:22

是的,这是 Python3 库 http://code.google.com /p/modgrammar/source/browse/setup.py#32

Python2x 不支持 class Grammar(metaclass=GrammarClass)

Yeah, this is Python3 library http://code.google.com/p/modgrammar/source/browse/setup.py#32

Python2x doesn't support class Grammar(metaclass=GrammarClass)

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