view tests/objects.py @ 42:0eebe05f8984

introduce a section with a wrapper; not yet used
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 08 Jun 2011 17:52:41 -0700
parents f6616a5f468f
children 49f88cde6219
line wrap: on
line source

"""
test objects
"""

def stringsort(*strings):
    return ''.join(sorted(''.join(strings)))

class StringMunge(object):
    def __init__(self, prefix, delimeter=':', callback=None):
        self.delimeter = delimeter
        self.prefix = prefix
        self.callback = callback
    def __call__(self, string):
        if self.callback:
            string = self.callback(string)
        return self.prefix + self.delimeter + string

class Foo(object):
    def __init__(self, a, b):
        self.factor = a*100 + b
    def __call__(self, number):
        return number * self.factor

class Wrapper(object):
    def __init__(self, app):
        self.app = app
    def __call__(self, *args, **kwargs):
        retval = self.app(*args, **kwargs)
        values = {1: 'one',
                  2: 'two',
                  3: 'three'} # etc
        if retval in values:
            return values[retval]
        return retval

def wrap(app):
    return Wrapper(app)

def fib(n):
    """return the nth fibonacci term"""
    sequence = [1,1]
    while len(sequence) - 1 < n:
        sequence.append(sequence[len(sequence)-1] + sequence[len(sequence)-2])
    return sequence[n]