euglug.org

[Think_python]

작성자

카테고리:



Bob MillerWed Dec 10 23:29:38 PST 2003

john fleming wrote:

> I would like to get a toe hold on this so I could start a command
>  or process from within a py program, retrieve output back into it
> to be processed. At this point I have no particular reason to do
> this other than to learn how to use python and linux better.
> Could someone with a better understanding of the practical use
> of python in unix give me some specific examples how this
> might be used?

Here's another example.  This is part of a larger program that I use
to process my mail.  Bogo.msg_is_spam() spawns a bogofilter process
and uses its exit status to learn whether bogofilter thinks a given
email message is spam.  Bogo.register_msg() keeps one or more copies
of bogofilter running, and passes email messages to them to train
bogofilter.

In both cases, msg is an instance of email.Message.Message from the
Python standard library.

I think the last line is clever. (-: It enforces that Bogo is a
singleton by removing the class from the module's namespace
immediately after defining it.  No code elsewhere in the file can
access the class definition.  It also ensures that the destructor will
run by creating an instance with module scope.  The instance is
destroyed when the module is destroyed.

class Bogo:

    """thin glue layer on bogofilter."""

    def __init__(self):
        self.registrar = {}

    def __del__(self):
        for bogo in self.registrar.itervalues():
            bogo.tochild.close()
            status = bogo.wait()
	    # # Commented out -- bogofilter exit status is unpredictable
            # assert os.WIFEXITED(status)
            # exit_status = os.WEXITSTATUS(status)
            # assert exit_status == 0

    def msg_is_spam(self, msg):
        """Return true if bogofilter thinks message is spam."""
        try:
            return msg['X-Bogosity'].strip().startswith('Yes')
        except (KeyError, AttributeError):
            pass
        bogo = popen2.Popen3('bogofilter -u')
        bogo.tochild.write(msg.as_string(unixfrom=True))
        bogo.tochild.close()
        status = bogo.wait()
        assert os.WIFEXITED(status)
        bogo_status = os.WEXITSTATUS(status)
        assert bogo_status in (0, 1), bogo_status
        if bogo_status is not 0:
            global exit_status
            exit_status = 1
        return bogo_status == 0

    def register_msg(self, msg, flags):
        """Register message in bogofilter's word list.
           flags are passed to bogofilter: s, n, sN, or nS.
        """
        if register_flag:
            if flags not in self.registrar:
                self.registrar[flags] = popen2.Popen3('bogofilter -' +
        flags)
            bogo = self.registrar[flags]
            bogo.tochild.write(msg.as_string(unixfrom=True))

Bogo = Bogo()                           # Singleton


-- 
Bob Miller                              K<bob>
kbobsoft software consulting
http://kbobsoft.com                     kbob at jogger-egg.com
_______________________________________________
Think_python mailing list
Think_python at efn.org
http://mailman.efn.org/mailman/listinfo/think_python