2014-09-23 53 views
1

背景重复条目蟒蛇

登录时,我写了验证我的脚本中使用的参数的函数。我的脚本正常工作,现在我正试图使用​​logging库向其添加日志记录。

CODE

我已经建立了使用

import logging 

logging.basicConfig(filename='AutoStats.log', level=logging.INFO, format='%(asctime)s %(message)s') 

的日志我的日志记录在我的脚本工作正常使用的东西,如

logging.info('IT WORKED!!') 

然而,在此记录功能:

def valid(): 
    """ Check to see if the arguments used throughout the script are valid """ 
    false_count = 0 
    for a in argv[1:]: 
     if a == date: 
      if re.match('^\d{8}$', date): 
       logging.info('Date is Valid') 
       continue 
      else: 
       logging.warning('Date parameter was invalid') 
       false_count += 1 
     if a == raw_dir or a == today_dir or a == archive_dir: 
      if a.endswith('\\'): 
       logging.info(a + 'is a valid parameter') 
       continue 
      else: 
       logging.warning(a + 'is not a valid paremeter, check formatting') 
       false_count += 1 
    if false_count > 0: 
     logging.warning('One or more of your arguments in the overruling .bat file has been entered in an incorrect format') 
     return False 
    else: 
     logging.info('All parameters validated') 
     return True 

日志记录功能正常,但是重复。

不是看到

2014-09-23 13:52:42,717 Date is Valid 
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\RAW_OTQ\is a valid parameter 
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\today\is a valid parameter 
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\archive\is a valid parameter 
2014-09-23 13:52:42,717 All parameters validated 

这是我所期待的是什么,我居然看到:

2014-09-23 13:52:42,717 Date is Valid 
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\RAW_OTQ\is a valid parameter 
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\today\is a valid parameter 
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\archive\is a valid parameter 
2014-09-23 13:52:42,717 All parameters validated 
2014-09-23 13:52:42,717 Date is Valid 
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\RAW_OTQ\is a valid parameter 
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\today\is a valid parameter 
2014-09-23 13:52:42,717 C:\dev\OTQtxt\Correct_Ver\archive\is a valid parameter 
2014-09-23 13:52:42,717 All parameters validated 

我看过了我的脚本,不能看到它遍历这个函数两次,这会导致它做到这一点。

我把logging.info的调用放在错误的地方或错误的缩进级别吗?

编辑

这是valid()函数是如何调用:

def main(): 
    """ Run the defined functions above in a set order to check validation, clear the RAW directory and then concatenate the CSV files. All Functions are run in try/except blocks to catch and record errors""" 
    errors = 0 
    start = time.time() 
    if not valid(): 
     raise TypeError('One or more of your arguments in the overruling .bat file has been entered in an incorrect format') 
    if valid(): 
     do_stuff() 
+0

你在哪里调用'valid()'函数? – 2014-09-23 13:00:26

+4

你可以调用'valid()'2次。将'do_stuff()'放入'else'块中。 – sk11 2014-09-23 13:00:32

回答

2

您呼叫的valid()功能的两倍,而使用if-else块。

def main(): 
    """ Run the defined functions above in a set order to check validation, clear the RAW directory and then concatenate the CSV files. All Functions are run in try/except blocks to catch and record errors""" 
    errors = 0 
    start = time.time() 
    if not valid(): 
     raise TypeError('One or more of your arguments in the overruling .bat file has been entered in an incorrect format') 
    else: 
     do_stuff() 
0

有效的功能因此被称为两次错误。试试下面的代码:

def main(): 
    """ Run the defined functions above in a set order to check validation, clear the RAW directory and then concatenate the CSV files. All Functions are run in try/except blocks to  catch and record errors""" 
    errors = 0 
    start = time.time() 
    valid_output = valid() 
    if not valid_output: 
     raise TypeError('One or more of your arguments in the overruling .bat file has been entered in an incorrect format') 
    if valid_output: 
     do_stuff() 
+0

这会起作用,但sk11提出了一个更光滑的方法。另外,正确格式化答案将有所帮助。 – 2014-09-23 13:04:46