Python-根据先前的函数退出状态调用函数的最佳方式?

标签 python function syntactic-sugar

在main()中,根据前面函数退出状态的条件调用一系列函数的最佳方式是什么?我可以做

if function foo(x, ssh) == True:
       if function bar(x.info, ssh) == True:
           if function foobar(x.info, ssh)

但我宁愿采用更有效的方式,比如将函数(及其参数)放在一个不可变列表中,并让它们在退出状态为 True 时循环迭代。或者 python 是否有一些更好的糖语法?

import pdb
import paramiko
import time
import sys
from collections import OrderedDict


class GetInfo():
    def __init__(self):
        self.info = OrderedDict([('count', None),
                     ('file_name_filename', ' '),
                     ('continute', ' '),
                     ('filename', None)])

        self.login_info = OrderedDict([('username', 'user'),
                                       ('hostname', '50.223.222.111'),
                           ('password', 'password')])

        epoch_birth = 15842
        epoch_birth = 15842
        count_start = 5555
        current_epoch = int(round(time.mktime(time.localtime())/86400))
        count = str((current_epoch - epoch_birth) + count_start)
        self.info["count"] = count

        fname = "/d2/file_name"+count
        self.info["filename"] = fname

    def login_name(self):
        name = raw_input("Enter Login Name: ")
        self.login_info["login_name"] = name
    def host_name(self): 
        name = raw_input("Enter Host Name: ")
        self.login_info["host_name"] = name
    def password(self):
        name = raw_input("Enter Password: ")
        self.login_info["password"] = name
    def fname(self):
        name = raw_input("Enter Filename and aboslute path: ")
        self.info["password"] = name

def login(object_dict):
        hostname = object_dict['hostname']
    username = object_dict['username']
    password = object_dict['password']
    port = 5777
    try: 
            ssh=paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect( hostname, port, username, password)
        return ssh
    except paramiko.AuthenticationException:
        print "Wrong username/password PUNK!"
        return False
    except: 
        print "Could not connect!"
        return False

def read_log(exit_status, stdin, stdout, stderr):
    if exit_status:
        output = stderr.readlines()
    else:
        output = stdout.readlines()
    return ''.join(output)

def get_file(object_dict, ssh):
    stdin, stdout, stderr=ssh.exec_command("bash -c 'ls -al $1' -- " + object_dict["filename"])
    for i in object_dict:
        stdin.write(object_dict[i]+"\n")
        stdin.flush()
    exit_status = stdout.channel.recv_exit_status()
    print exit_status 
    print read_log(exit_status, stdin, stdout, stderr)
    ssh.close()

def create(object_dict, ssh):
    stdin, stdout, stderr=ssh.exec_command("/home/one/file_nameslice.sh")
    for i in object_dict:
        stdin.write(object_dict[i]+"\n")
        stdin.flush()
    exit_status = stdout.channel.recv_exit_status()
    print read_log(exit_status, stdin, stdout, stderr)
    ssh.close()



def main():
    x = GetInfo()
    ssh = login(x.login_info)
    if ssh:
            get_file(x.info, ssh)

main()

最佳答案

你可以用加入这些函数,例如:

def foo(arg):
    if arg > 2:
        print True, arg
        return True
    else:
        print False, arg
        return False

foo(4) and foo(3) and foo(2) and foo(1)

只有当前一个函数返回true时,才会调用下一个函数。所以输出是:

True 4
True 3
False 2

关于Python-根据先前的函数退出状态调用函数的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16727675/

相关文章:

python - 解析 MediaWiki wiki 的 XML 转储

python - 在 TensorFlow 中,如何使用 python 从张量中获取非零值及其索引?

python - 缺少趋势线

java - Android 函数没有被调用

python - 哪个更可取使用 : lambda functions or nested functions ('def' )?

c - C 中的数组是指针的语法糖吗?

c++ - 如何将用户定义的文字放在 C++ 中相同类型的 constexpr 类中?

python - 使用 Python 在六边形网格中显示数据

Swift:约束泛型参数以用于更受约束的泛型函数

javascript - 如何优雅地检查 JavaScript 中两个对象之间是否有任何字段具有更长的值?