3

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()
4

2 に答える 2

2

これらの関数をとで結合できます。たとえば、次のようになります。

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
于 2013-05-24T04:47:27.840 に答える