how can I use a special keyword as my fabric function name?

▼魔方 西西 提交于 2020-01-25 08:07:48

问题


How can I make a fabric function with reserved keywords like this?

def not(*args):
    ......

This throws a "invalid syntax" error. Is there any way to override the special keyword and use it as a function name in classic method? I can do this with @task alias but all my other functions follow classic method. http://docs.fabfile.org/en/1.10/usage/tasks.html#task-decorator


回答1:


This looks fine to me:

fabfile.py

from fabric.api import task

@task(alias='not')
def _not():
    print 'not called'

@task(alias='in')
def _in():
    print 'in called'

@task(alias='while')
def _while():
    print 'while called'

old style:

from fabric import state

def _not():
    print 'not called'

def _in():
    print 'in called'

def _while():
    print 'while called'

state.commands['not'] = _not
state.commands['in'] = _in
state.commands['while'] = _while

and to run it.

$ fab not while in
not called
while called
in called

Done.

Also. Next time add python to the tags, this was almost impossible to get "finded" -- haha.



来源:https://stackoverflow.com/questions/33158137/how-can-i-use-a-special-keyword-as-my-fabric-function-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!