How to call one function from within another function in python
这似乎是一个非常基本的问题,但我很难理解它,我会感谢任何帮助。
我想打电话给EDOCX1
这是代码:当我运行它时,会得到错误:
1 2 3 4 5 6 7 8 | jira ticket regex matched! printing m.group(1) QA-65 my_args ... QA-65 transaction abort! rollback completed abort: pretxncommit.jira hook exited with status 1 |
意思是,那个支票存款记录(我的账户)只是没有接到电话。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #!/usr/bin/env python import re, os, sys, jira, subprocess def verify_commit_text(tags): for line in tags: if re.match('^NO-TIK',line): return True elif re.match('^NO-REVIEW', line): return True elif re.match(r'[a-zA-Z]+-\d+', line): # Validate the JIRA ID print 'jira ticket regex matched!' m = re.search("([a-zA-Z]+-\d+)",line) print 'printing m.group(1)' print m.group(1) my_args = m.group(1) print 'my_args ...' print my_args result = CheckForJiraIssueRecord(my_args) print 'printing result....' print result if result == False: #util.warn("%s does not exist"%my_args) print 'result = False.......' else: print 'if result == False return True' return True return True else: return False def CheckForJiraIssueRecord(object): sys.stdout = os.devnull sys.stderr = os.devnull try: com = jira.Commands() logger = jira.setupLogging() jira_env = {'home':os.environ['HOME']} command_cat="cat" command_logout="logout" #my_args = ["QA-656"] server ="http://jira.myserver.com:8080/rpc/soap/jirasoapservice-v2?wsdl" except Exception, e: sys.exit('config error') if __name__ == '__main__': commit_text_verified = verify_commit_text(os.popen('hg tip --template"{desc}"')) #commit_text_verified = verify_commit_text(os.popen('hg log -r $1 --template"{desc}"')) if (commit_text_verified): sys.exit(0) else: print >> sys.stderr, ('[obey the rules!]') sys.exit(1); class Options: pass options = Options() options.user = 'username' options.password = 'password' try: jira.soap = jira.Client(server) jira.start_login(options, jira_env, command_cat, com, logger) issue = com.run(command_cat, logger, jira_env, my_args) except Exception, e: print sys.exit('data error') |
违规行:
1 2 | sys.stdout = os.devnull sys.stderr = os.devnull |
必须将sys.stdout赋给变量,在函数完成它应该做的操作后,将其改回。即
1 2 3 4 5 6 | out = sys.stdout err = sys.stderr .... Your code #Just befor function exits sys.stdout = out sys.stderr = err |
正在调用函数,但由于以下行,它引发了异常:
1 2 | 32 sys.stdout = os.devnull 33 sys.stderr = os.devnull |
您正在将一个字符串(os.devnull)分配给一个应该是文件句柄的对象,因此当任何人写入stdout或stder时,它都会由于类型错误而引发异常。你应该尝试:
1 2 | 32 sys.stdout = open(os.devnull) 33 sys.stderr = open(os.devnull) |
看看这对你有什么作用。