CONTEXT
I have written a function that validates the arguments used in my script. My script works and I am now trying to add logging to it using logging library.
CODE
I have set up my logging using
import logging
logging.basicConfig(filename='AutoStats.log', level=logging.INFO, format='%(asctime)s %(message)s')
The logging in my script works fine using things such as
logging.info('IT WORKED!!')
However the logging in this function:
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
The logging does work, however it is repeated.
Instead of seeing
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
Which is what I'd expect, I actually see:
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
I've looked over my script and can't see where it loops over this function twice which would cause it to do this.
Have I placed the logging.info calls in the wrong place or wrong level of indentation or is it something else?
EDIT
This is how the valid() function is called:
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()
valid()function?valid()2 times. Put thedo_stuff()inside aelseblock.