1

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()
2
  • Where are you calling the valid() function? Commented Sep 23, 2014 at 13:00
  • 4
    You call the valid() 2 times. Put the do_stuff() inside a else block. Commented Sep 23, 2014 at 13:00

2 Answers 2

2

You are calling the valid() function twice, instead use if-else block.

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()
Sign up to request clarification or add additional context in comments.

Comments

0

The valid function is called twice hence the error. Try the following code:

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

1 Comment

this would work, but sk11 has suggested a much sleeker way of doing it. Also, formatting answers properly will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.