Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择做一个";“迷你dsl”;用Python?_Python_Testing_Dsl - Fatal编程技术网

选择做一个";“迷你dsl”;用Python?

选择做一个";“迷你dsl”;用Python?,python,testing,dsl,Python,Testing,Dsl,我目前正在开发一个框架,用于在虚拟机中自动配置我们的软件产品,以便在python中进行测试。到目前为止,一切都是面向对象的。框架将创建“目标”对象,然后根据目标的类型,框架将“操作”添加到列表中;最后,“执行”这些操作 现在,创建这些操作列表如下所示: def build_actions(): cmds = list() cmds.append(UploadSshKeys(target.get_ip(), user.get_name()) cmds.append(RemoteActi

我目前正在开发一个框架,用于在虚拟机中自动配置我们的软件产品,以便在python中进行测试。到目前为止,一切都是面向对象的。框架将创建“目标”对象,然后根据目标的类型,框架将“操作”添加到列表中;最后,“执行”这些操作

现在,创建这些操作列表如下所示:

def build_actions():
  cmds = list()
  cmds.append(UploadSshKeys(target.get_ip(), user.get_name())
  cmds.append(RemoteAction("some command"))
...
  return cmds
最终拥有这些物品是件好事;因为他们允许我做各种有趣的事情;但另一方面,;这种“符号”增加了很多“样板”。拥有某种“迷你dsl”感觉会好得多;看起来像:

upload_keys(target.get_ip() ...
remote("some command
def upload_keys(ip, ...):
  check if there is a list, if not create one
  create the UploadSsh object
  add the object to the list
以某种方式神奇地构建底层对象;把它们放在一个列表中;并在最后提供了这个列表。一个简单的实现可以提供以下方法,可能看起来像:

upload_keys(target.get_ip() ...
remote("some command
def upload_keys(ip, ...):
  check if there is a list, if not create one
  create the UploadSsh object
  add the object to the list
但是说真的,对于许多不同的命令,这样做听起来既麻烦又无聊。多年前,我确实用Perl创建了一个类似的DSL,使用它的“默认”自动加载方法;以及使用BEGIN、CHECK等轻松进入各个编译阶段


长话短说:我是Python的新手;并寻找有趣(但仍然强大!)的选项来实现我的“迷你dsl”。

如果我正确理解您的问题,您可以像这样概括您的功能:

def add_action(klass, **kwargs):
    check if there is a list, if not create one
    create the klass object with arguments **kwargs
    add the object to the list    
那么实际的方法就变成了:

def upload_keys(**kwargs):
    add_action(UploadSsh, kwargs)

def remote_action(**kwargs):
    add_action(RemoteAction, kwargs)
然后你可以这样称呼他们:

upload_keys(target.get_ip(), user.get_name())
remote_action("Some command")