You are not logged in.

#1 2010-05-08 18:13:53

munkyeetr
Member
From: Merritt, BC
Registered: 2008-08-07
Posts: 83

[SOLVED] Python: problems with function return value

I've been plugging away at this and am still scratching my head...

I am writing a python script to continually train bogofilter to recognize new spam in Sylpheed email client.
Sylpheed stores each email message as a file named with a number: eg. /path/to/mailbox/1 /path/to/mailbox/2 ...

So I have a config file that holds values of the last mail message used to train bogofilter in this format:

spam=115
ham=218

bogotrain.py (I've left out the code to filter good mail, but it has the same problem with return value)

#!/usr/bin/python
import os;

spam_directory = "/home/jon/Mail/Junk/";
ham_directory = "/home/jon/Mail/Archives/2010/";
config = "/home/jon/.bogo"

def ReadBogo(config):
    f = open(config, 'r');
    values = f.readlines();
    f.close();

    for value in values:
        (name, value) = value.split("=");
        if name == "spam":    sc = value;
        if name == "ham":    hc = value;
    
    return (int(sc), int(hc));

def FilterSpam(dir, count):
    if os.path.exists("%s%d" % (dir, count)):
    # start debug
        print "DEBUG %s%d" % (dir, count);
        #os.system("bogofilter -s < %s%d" % (dir, count));
        count += 1;
        FilterSpam(dir, count);
    else:
    # start debug
        print "DEBUG Inside else statement"
        print "DEBUG returning count %d" % count;
        print "DEBUG returning type %s " % type(count);
    # end debug
        return count;

# read count of last filtered message
(spam_count, ham_count) = ReadBogo(config);

spam_count = FilterSpam(spam_directory, spam_count);

print "DEBUG return value from FilterSpam %d" % (spam_count);

But when I run the script I get this output:

DEBUG /home/jon/Mail/Junk/115
DEBUG /home/jon/Mail/Junk/116
DEBUG /home/jon/Mail/Junk/117
DEBUG /home/jon/Mail/Junk/118
DEBUG /home/jon/Mail/Junk/119
DEBUG /home/jon/Mail/Junk/120
DEBUG Inside else statement
DEBUG returning count 121
DEBUG returning type <type 'int'>

Traceback (most recent call last):
  File "bogotrain.py", line 61, in <module>
    print "DEBUG return from FilterSpam %d" % (spam_count);
TypeError: %d format: a number is required, not NoneType

The function is performing as it should right up until the return. And clearly before the function returns the count value is an integer, but it returns it as <NoneType>

Any ideas of what I am overlooking?

Last edited by munkyeetr (2010-05-08 18:29:52)


If the advice you're given in this forum solves your issue, please mark the post as [SOLVED] in consideration to others.

"More than any time in history mankind faces a crossroads. One path leads to despair and utter hopelessness, the other to total extinction.
Let us pray that we have the wisdom to choose correctly." -- Woody Allen

Offline

#2 2010-05-08 18:26:32

livibetter
Member
From: Taipei
Registered: 2008-05-14
Posts: 95
Website

Re: [SOLVED] Python: problems with function return value

It should be

        count += 1
        return FilterSpam(dir, count)

And you don't need the tailing ';'
Also you might want to check out logging module for `logging.debug('debugging information')`

Last edited by livibetter (2010-05-08 18:27:16)

Offline

#3 2010-05-08 18:29:11

munkyeetr
Member
From: Merritt, BC
Registered: 2008-08-07
Posts: 83

Re: [SOLVED] Python: problems with function return value

livibetter wrote:

It should be

        count += 1
        return FilterSpam(dir, count)

And you don't need the tailing ';'
Also you might want to check out logging module for `logging.debug('debugging information')`

Thank you livibetter...that was it.


If the advice you're given in this forum solves your issue, please mark the post as [SOLVED] in consideration to others.

"More than any time in history mankind faces a crossroads. One path leads to despair and utter hopelessness, the other to total extinction.
Let us pray that we have the wisdom to choose correctly." -- Woody Allen

Offline

#4 2010-05-08 19:02:06

munkyeetr
Member
From: Merritt, BC
Registered: 2008-08-07
Posts: 83

Re: [SOLVED] Python: problems with function return value

I have a quick followup question to this...

I am curious why adding return to the initial if branch fixed the issue? In the else branch the return value was <TypeInt> right before the return, shouldn't it have returned a <TypeInt>?


If the advice you're given in this forum solves your issue, please mark the post as [SOLVED] in consideration to others.

"More than any time in history mankind faces a crossroads. One path leads to despair and utter hopelessness, the other to total extinction.
Let us pray that we have the wisdom to choose correctly." -- Woody Allen

Offline

#5 2010-05-08 22:35:00

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: [SOLVED] Python: problems with function return value

It did, in the "inner" calls where the else-branch was executed.  But your "outer" call to FilterSpam didn't do anything with it, because that was in the then-branch (the part immediately after "if ... :").

That sounded like it didn't make any sense.  Let me try again.  When you call FilterSpam, you either pick the "if" block or the "else" block.  One of them returns something, with the explicit return statement.  One of them returns nothing, because there's no return in that branch.  Unlike Perl, Python never returns anything "by default" -- you have to use an explicit "return" or "yield" in a function if you want to get something out of it.  So anytime the condition os.path.exists("%s%d" % (dir, count)) is True, the result of the recursive call to FilterSpam is discarded and the function returns None.

Offline

#6 2010-05-08 22:51:29

munkyeetr
Member
From: Merritt, BC
Registered: 2008-08-07
Posts: 83

Re: [SOLVED] Python: problems with function return value

Thanks Trent

It took me a few times reading your post before I finally got to "Ahhh, I get it". And thanks for rewording the explanation. I don't think I would have understood what you were saying in the first attempt.


If the advice you're given in this forum solves your issue, please mark the post as [SOLVED] in consideration to others.

"More than any time in history mankind faces a crossroads. One path leads to despair and utter hopelessness, the other to total extinction.
Let us pray that we have the wisdom to choose correctly." -- Woody Allen

Offline

#7 2010-05-09 07:15:33

livibetter
Member
From: Taipei
Registered: 2008-05-14
Posts: 95
Website

Re: [SOLVED] Python: problems with function return value

munkyeetr wrote:

Thanks Trent

It took me a few times reading your post before I finally got to "Ahhh, I get it". And thanks for rewording the explanation. I don't think I would have understood what you were saying in the first attempt.

Let me quote something from documentation, Defining Functions:

Coming from other languages, you might object that fib is not a function but a procedure since it doesn't return a value. In fact, even functions without a return statement do return a value, albeit a rather boring one. This value is called None (it's a built-in name). Writing the value None is normally suppressed by the interpreter if it would be the only value written.

And you might also want to read return statement.

Offline

Board footer

Powered by FluxBB