view tests/objects.py @ 98:d59f0425d705 default tip

version bump
author Jeff Hammel <k0scist@gmail.com>
date Mon, 27 May 2024 16:20:52 -0700
parents 2449fb4a89c1
children
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, **values):
        self.app = app
        self.values = dict([(int(j), i) for i, j in values.items()]) # TODO: should be automagically converted ideally via pyloader
    def __call__(self, *args, **kwargs):
        retval = self.app(*args, **kwargs)
        values = {1: 'one',
                  2: 'two',
                  3: 'three'} # etc
        values.update(self.values)
        if retval in values:
            return values[retval]
        return retval

class ArgsPrinter(object):
    def __init__(self, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
    def __call__(self):
        for arg in self.args:
            print arg
        for kwarg in sorted(self.kwargs):
            print kwarg + ": " + self.kwargs[kwarg]

def wrap(app, **values):
    return Wrapper(app, **values)

def fib(n):
    """return the nth fibonacci term"""
    n = int(n) # TODO: should be automagically converted ideally via pyloader
    sequence = [1,1]
    while len(sequence) - 1 < n:
        sequence.append(sequence[len(sequence)-1] + sequence[len(sequence)-2])
    return sequence[n]