Python 从命令提示符调用函数

Python 从命令提示符调用函数,python,Python,下面是脚本的摘录(未测试) 这些函数在技术上(语法上)正确吗 调用python脚本时如何调用它们?我需要一次启动或停止集群,而不是同时启动或停止 1) 如果您在某个地方定义了conn并将其导入,那么它在语法上是正确的 (二) 用法: python2.7 test_syn.py fun 对于第二个问题,我将通过以下方式解析命令行: 我在脚本中添加了一个主函数,用于检查命令行参数,然后在未提供有效参数时提示您: import sys def start_custer(): try:

下面是脚本的摘录(未测试)

  • 这些函数在技术上(语法上)正确吗

  • 调用python脚本时如何调用它们?我需要一次启动或停止集群,而不是同时启动或停止

  • 1) 如果您在某个地方定义了
    conn
    并将其导入,那么它在语法上是正确的

    (二)

    用法:

       python2.7 test_syn.py fun
    

    对于第二个问题,我将通过以下方式解析命令行:


    我在脚本中添加了一个主函数,用于检查命令行参数,然后在未提供有效参数时提示您:

    import sys
    
    def start_custer():
        try:
            myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier']
        except IndexError:
            conn.restore_from_cluster_snapshot('vi-mar5-deliveryreport-new', mysnapidentifier, availability_zone='us-east-1a')
    
    def stop_cluster():
        try:    
            myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier']
            conn.delete_cluster(myidentifier, skip_final_cluster_snapshot=False, final_cluster_snapshot_identifier=myvar)
        except:
            print "error"
    
    def main():
        valid_args, proc = ['start','stop'], None
    
        # check if cmd line args were passed in (>1 as sys.argv[0] is name of program)
        if len(sys.argv) > 1:
            if sys.argv[1].lower() in valid_args:
                proc = sys.argv[1].lower()
    
        # if a valid arg was passed in this has been stored in proc, if not prompt user
        while not proc or proc not in valid_args:
            print "\nPlease state which procedure you want to call, valid options are:", valid_args
            proc = raw_input('>>> ').lower()
    
            # prompt user if invalid
            if proc not in valid_args:
                print proc, 'is not a valid selection.'
    
        if proc == 'start':
            start_custer()
        elif proc == 'stop':
            stop_cluster()
    
    # this makes the script automatically call main when starting up
    if __name__ == '__main__':
        main()
    
    您可以从命令行调用此命令,例如,如果您与文件位于同一目录(例如,名为cluster_ctrl.py),则只需执行以下操作:


    其他人已经向您展示了执行此操作的最佳方法,但作为记录,您也可以从命令行执行此操作:

    python -c "import cluster; cluster.start_cluster()"
    
    (假设您的模块文件名为
    cluster.py
    ——如果不是,则相应地调整
    import
    语句)


    这不像自己解析命令行那样方便用户,但在紧要关头就可以了。

    好吧,您需要同时捕获
    KeyError
    indexer
    。我可以建议您自己测试它吗?这取决于使用的变量。但是,是的,它在语法上似乎是正确的。如果我使用的不是“开始”或“停止”,我就会得到信息。但是为什么我会得到python提示符呢?与>>>类似,如果查看脚本,提示符实际上在
    原始输入调用中。这不是真正的Python提示符。@shantanuo正如kindall所说,这不是真正的Python提示符,它只是我碰巧在raw_输入函数中使用的文本。如果您对代码有任何其他问题,请随时询问。
    
    import argparse
    
    parser = argparse.ArgumentParser(description="Manage the cluster")
    parser.add_argument("action", choices=["stop", "start"],
                        help="Action to perform")
    
    args = parser.parse_args()
    if args.action == "start":
        start_cluster()
    if args.action == "stop":
        stop_cluster()
    
    import sys
    
    def start_custer():
        try:
            myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier']
        except IndexError:
            conn.restore_from_cluster_snapshot('vi-mar5-deliveryreport-new', mysnapidentifier, availability_zone='us-east-1a')
    
    def stop_cluster():
        try:    
            myidentifier=mydict['DescribeClustersResponse']['DescribeClustersResult']['Clusters'][0]['ClusterIdentifier']
            conn.delete_cluster(myidentifier, skip_final_cluster_snapshot=False, final_cluster_snapshot_identifier=myvar)
        except:
            print "error"
    
    def main():
        valid_args, proc = ['start','stop'], None
    
        # check if cmd line args were passed in (>1 as sys.argv[0] is name of program)
        if len(sys.argv) > 1:
            if sys.argv[1].lower() in valid_args:
                proc = sys.argv[1].lower()
    
        # if a valid arg was passed in this has been stored in proc, if not prompt user
        while not proc or proc not in valid_args:
            print "\nPlease state which procedure you want to call, valid options are:", valid_args
            proc = raw_input('>>> ').lower()
    
            # prompt user if invalid
            if proc not in valid_args:
                print proc, 'is not a valid selection.'
    
        if proc == 'start':
            start_custer()
        elif proc == 'stop':
            stop_cluster()
    
    # this makes the script automatically call main when starting up
    if __name__ == '__main__':
        main()
    
    python cluster_ctrl.py start
    
    python -c "import cluster; cluster.start_cluster()"