py library overview

Grig Gheorghiu

What is the py library?

Resources:
py library homepage: http://codespeak.net/py/current/doc/index.html

py.test: general operation

Resources:
py.test home page: http://codespeak.net/py/current/doc/test.html
py.test SVN repository: http://codespeak.net/svn/py/dist
py.test download page (alpha release): http://codespeak.net/download/py/

py.test: assertions and exception handling

py.test: test fixture management

py.test: maintaining state


def setup_module(module):
    module.TestState.classcount = 0

class TestState:

    def setup_class(cls):
        cls.classcount += 1

    def setup_method(self, method):
        self.methodcount = self.classcount + 1

    def test_state1(self):
        self.methodcount += 1
        assert self.classcount == 1
        assert self.methodcount == 3

    def test_state2(self):
        self.methodcount += 1
        assert self.classcount == 1
        assert self.methodcount == 3

py.test: command-line options

py.test: customizing the test collection

Greenlets: implementing coroutines in Python

Resources:
Greenlet documentation: http://codespeak.net/py/current/doc/greenlet.html
Implementing generators with greenlets: http://codespeak.net/svn/user/arigo/greenlet/test_generator.py

Greenlet example: XML parsing

from py.magic import greenlet
import xml.parsers.expat

def send(arg):
    greenlet.getcurrent().parent.switch(arg)

def start_element(name, attrs):
    send(('START', name, attrs))
def end_element(name):
    send(('END', name))
def char_data(data):
    data = data.strip()
    if data: send(('DATA', data))

def greenparse(xmldata):
    p = xml.parsers.expat.ParserCreate()
    p.StartElementHandler = start_element
    p.EndElementHandler = end_element
    p.CharacterDataHandler = char_data
    p.Parse(xmldata, 1)
def iterxml(xmldata):
    g = greenlet(greenparse)
    data = g.switch(xmldata)
    while data is not None:
        yield data
        data = g.switch()

if __name__ == "__main__":
    for data in iterxml("somexmldata"):
        # do something with data

py.xml: easy XML generation

Resources:
py.xml documentation: http://codespeak.net/py/current/doc/xml.html

py.xml: easy HTML generation

py.log: keyword-based logging

More details on py.log: http://agiletesting.blogspot.com/2005/06/keyword-based-logging-with-py-library.html

py.log: configuring logging on the fly

py.log: defining severity levels

log = py.log.Producer("")
log.debug = py.log.Producer("logfile debug")
log.info = py.log.Producer("console info")
log.error = py.log.Producer("console logfile error")

logfile = "/tmp/myapp.out"
py.log.setconsumer("logfile", py.log.Path(logfile))
py.log.setconsumer("console", py.log.STDOUT)

l = open(logfile, 'a', 1)
def console_logfile_logger(msg):
    print >>sys.stdout, str(msg)
    print >>l, str(msg)
py.log.setconsumer("console logfile", console_logfile_logger)

log.debug("DEBUG MESSAGE") # prints "[logfile:debug] DEBUG MESSAGE" to logfile
log.info("INFO MESSAGE") # prints "[console:info] INFO MESSAGE" to stdout
log.error("ERROR MESSAGE") # prints "[console:logfile:error] ERROR MESSAGE" to stdout and logfile
log.warn("WARNING MESSAGE") # prints "[warn] WARNING MESSAGE" to stdout (default consumer)
# to disable debug messages: py.log.setconsumer("logfile debug", None)