【问题标题】:dsl in python example needed like in rubypython 示例中的 dsl 需要像 ruby​​ 中一样
【发布时间】:2013-01-21 07:01:26
【问题描述】:

我基本上是一个 ruby​​ 人,遇到了一种情况,我需要在 py 中制作一个小的 dsl,如下所示,我知道在 ruby​​ 中以下是可行的,我在 py 中寻找完全相同的

from_a_dsl_file = "
   take_this 'abc'
   and_process_it
   and_give_me_the_output
"

class A
   def take_this abc
   end

   def and_process_it
   end

   def and_give_me_the_output
     'desired'
   end
end

A.new.instance_eval from_a_dsl_file
# => 'desired'

任何提示,或者很高兴有一个工作示例

提前致谢

【问题讨论】:

    标签: python metaprogramming eval dsl


    【解决方案1】:

    据我了解,在 Ruby 中,您可以使用不需要括号的函数调用来做一些棘手的事情:

    x y
    

    在 Ruby 中,这可能是函数调用,其中函数 xy 为参数调用。

    好吧,在 Python 中,我们没有这些技巧。如果你正在调用一个函数,你需要在函数名后面加上括号。所以,我不认为你会因为这个尝试使用eval() 玩游戏。

    更好的方法是编写一个解析器来为您解析语言并弄清楚该语言试图做什么。为此,Python 有一个特别好的库:pyparsing

    http://pyparsing.wikispaces.com/

    附:只需在 Google 上搜索“Python 领域特定语言”,就会发现一些很好的链接,其中很多都在 StackOverflow 中。这是我找到的最好的:

    Mini-languages in Python

    编辑:好的,你问了一个例子,你去。这是我在 PyParsing 中的第一个程序,非常简单。我什至没有阅读文档;我刚刚从网上找到的演示文稿中的示例开始工作。

    这是演示文稿的网址:http://www.ptmcg.com/geo/python/confs/TxUnconf2008Pyparsing.html

    from pyparsing import *
    
    def give_desired_output(s):
        return "desired"
    
    TAKE_THIS = Suppress("take_this") + Suppress(Word(printables))
    AND_PROC = Suppress("and_process_it")
    AND_GIVE = Keyword("and_give_me_the_output")
    AND_GIVE.setParseAction(give_desired_output)
    
    LANGUAGE_LINE = TAKE_THIS | AND_PROC | AND_GIVE
    
    LANGUAGE = ZeroOrMore(LANGUAGE_LINE)
    
    
    def parse_language(text):
        lst = LANGUAGE.parseString(text)
        assert len(lst) == 1  # trivial language returns a single value
        return lst[0]
    
    if __name__ == "__main__":
        from_a_dsl_file = \
    """
       take_this 'abc'
       and_process_it
       and_give_me_the_output
    """
    
        print(parse_language(from_a_dsl_file))  # prints the word: desired
    

    【讨论】:

    【解决方案2】:

    听起来您可能想查看exec() 和/或execfile()(特别是诸如指定可用全局变量和局部变量的能力)。

    (还有eval(),但它只允许单个表达式,而不是一系列命令。)

    【讨论】:

      猜你喜欢
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 2010-10-26
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多