Esempio n. 1
0
def test_sliceTxt():
    test(sliceTxt, ["a","b"], "   a   ### ### b   ")
    test(sliceTxt, ["a","b"], "   a   ;;; ;;; b   ", ";;;")
    test(sliceTxt, ["##"], "#####")
    test(sliceTxt, ["a", "##"], "a#####")
    return True
Esempio n. 2
0
from display import reportyFunc

def _popen(cmdLst): #(!:2)
    try:
        return Popen(cmdLst, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    except OSError:
        stderr.write("(EB) Subprocess failed: %s\n"%(" ".join(cmdLst)))
    return None

def _finalizeReport(obs, retGet, stdout, stderr, finishTime):
    return merge(obs, artificeBreed='observation',
                 finished=finishTime, stdout=stdout,
                 stderr=stderr, retCode=retGet(),
                 duration=finishTime-obs.get('started',0))
test(_finalizeReport, 
     {'started':10,'retCode':0,'stdout':'O','stderr':'E', 'a':'A',
      'finished':12, 'duration':2, 'artificeBreed': 'observation'}, 
     {'started':10, 'a':'A'}, lambda: 0, 'O', 'E', 12)

def _prepFinish(obs, retGet, stdout, stderr, *ignored): #(!:2)
    return _finalizeReport(obs, retGet, stdout, stderr, time())

def _execute(obs, p): #(!:2)
    if p is None:
        return {}
    return _prepFinish(obs, p.wait, *p.communicate(obs.get("instream")))

def _evoke(cmdLst, notes): #(!:2)
    return _execute(merge(notes, started=time()), _popen(cmdLst))

def _runner(preprocessor=lambda x,**y: split(x), **iNotes):
    return lambda cmd,**notes: _evoke(preprocessor(cmd,**iNotes),
def buildAssessorsFromText(rawText):
    return buildAssessors(*[asplt(x) for x in rawText.splitlines() if ":::" in x])

def loadAssessorsFrom(filePath):
    return buildAssessorsFromText(readBytes(filePath))

def loadAssessorFiles(*files):
    return map(loadAssessorsFrom, files)

def second(one=None, two=None, *rest):
    return two

def scnd(sequence):
    return second(*sequence) or tuple()
test(scnd, tuple(), ('foo',) )
test(scnd, ('bar', "BAR"), {'foo':"FOO", 'bar':"BAR"}.items() )

def assessAgainst(assessors, command):
    def qal(pair):
        return bool(pair[0].findall(command))
    return scnd(zip(*filter(qal, assessors.items())))
test(assessAgainst, (), {}, "foo")
test(assessAgainst, (), {re.compile("bar.*"): "bar"}, "foo")
test(assessAgainst, ("bar",), {re.compile("foo.*"): "bar"}, "foo")
test(assessAgainst, ("bar",), {re.compile("foo.*"): "bar"}, "foo yo")

def create_assessor_functions(assessorList):
    return partial(assessAgainst, assessorList)

def generateAssessors(*sources):
Esempio n. 4
0
from os import path
import time, socket

interruptSignalReceived=False

def handleInt(sig, frm): #rig:3
    global interruptSignalReceived
    stderr.write('''Received SIGINT.  Quitting after current test.\n''')
    interruptSignalReceived=True

def _stripit(txt):
    return str(txt).strip()

def sliceTxt(text, splitter="###"):
    return text and filter(None, map(_stripit, text.split(splitter))) or []
test(sliceTxt, ["a"], "   a ")
test(sliceTxt, ["a"], "a")
test(sliceTxt, ["a","b"], "a###b")

def newCatalogId(): #rig:3
    return "%s_%s"%(socket.gethostname(), time.time())

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

@reportyFunc
def test_sliceTxt():
    test(sliceTxt, ["a","b"], "   a   ### ### b   ")
    test(sliceTxt, ["a","b"], "   a   ;;; ;;; b   ", ";;;")
    test(sliceTxt, ["##"], "#####")
    test(sliceTxt, ["a", "##"], "a#####")
    return True
def _chk(wAttribs, k,v):
    return k and v and not wAttribs or k.strip() in wAttribs

def qualified(wantedAttrs, obs):
    return dict([x for x in obs.items() if _chk(wantedAttrs, *x)])

def alignKeys(obs):
    def buildIndentor(width):
        def indentor(i):
            return (i[0].rjust(width), i[1])
        return indentor
    def indent(indentor):
        return dict(map(indentor, obs.items()))
    return indent(buildIndentor(max(map(len, obs))))
test(alignKeys, {'  a':'once', 'aaa':'thrice'}, {'a':'once', 'aaa':'thrice'})

def tablify(obs):
    return ["%s: %s\n"%x for x in obs.items()]
test(tablify, ['  a: a\n','aaa: a\n'], {'  a':'a', 'aaa':'a'})

def extractObs(raw):
    def makeObs(rawObs):
        try:
            return [eval(rawObs)]
        except SyntaxError:
            return []
    def evaluate(these):
        return filter(lambda x: hasattr(x, "update"), sum(map(makeObs, these), []))
    return evaluate([x.strip() for x in raw.splitlines()])
test(extractObs, [{'A':'A'}], "{'A':'A'}")
Esempio n. 6
0
#!/usr/bin/python
from sys import argv, exit
from time import time, ctime
from pymongo import Connection
from difflib import SequenceMatcher
from diagnostics import test
from artifice import pairsOf
from display import reportyFunc


def _seq(*things):
    return sum([list(x) if hasattr(x, "count") else [x] for x in things], [])


test(_seq, [1, "a", "b"], 1, "ab")
test(_seq, [0], 0)
test(_seq, [0, 1], range(2))


def _lcase(x):
    return hasattr(x, "lower") and x.lower() or x


test(_lcase, "a", "A")
test(_lcase, 0, 0)


def _simlRatio(*things):
    return SequenceMatcher(None, *[_seq(_lcase(x)) for x in things]).ratio()

Esempio n. 7
0
#!/usr/bin/python
from diagnostics import test, testl
from sys import exit
import re

def pairsOf(first=None, second=None, *things):
    return first and second and [(first,second)]+pairsOf(*things) or []
test(pairsOf, [('a','aA'), ('b','bB')], 'a', 'aA', 'b', 'bB')
test(pairsOf, [('b','bB') ,('a','aA')], 'b', 'bB', 'a', 'aA')
test(pairsOf, [('b','bB') ,('a','aA')], 'b', 'bB', 'a', 'aA', 'c')

def merge(*dicts, **extra):
    return dict(sum([x.items() for x in dicts+(extra,)], []))
test(merge, {'a':'a','b':'b'}, {'a':'a'}, {'b':'b'})
test(merge, {'a':'a','b':'B'}, {'a':'a','b':'b'}, {'b':'B'})

def contribute(obs, *elements):
    return dict(obs.items()+pairsOf(*elements))
test(contribute, {'a':'aA', 'b':'bB'}, {'a':'aA'}, 'b', 'bB')
test(contribute, {'b':'bB', 'a':'aA'}, {'b':'bB'}, 'a', 'aA')
test(contribute, {'b':'bB', 'a':'aA'}, {'b':'bB'}, 'a', 'aA', 'c')
test(contribute, {'b':'bB', 'a':'aA'}, {'a':'aA', 'b':'NOT'}, 'b', 'bB')

def validate(artificeData, **artReq):
    def check(vv, av):
        if hasattr(vv, 'findall'):
            return bool(vv.findall(av))
        return vv==av or callable(vv) and vv(av)
    def _do(k): return check(artReq.get(k), artificeData.get(k))
    return all(map(_do, artReq))
testl(validate, True, artificeData={'foo':'bar'}, foo="bar")
Esempio n. 8
0
    first function given.  Take returned value of
    each function in pipeline and pass in as value
    for next function in pipeline.  Return final
    function's return value.  None parameter breaks
    the pipeline.  Exceptions print to stderr and
    return None.'''
    def do(val, fn):
        if val is not None and callable(fn):
            try:
                return fn(val) 
            except Exception as e:
                sys.stderr.write('''Exception raised in pipeline on %s(%s):
%s\n'''%(fn.func_name, val, e))
        return None
    return reduce(do, functions, starting)
test(pipeline, 10, 0, lambda x: x+3, lambda x: x+2, lambda x: x+5)
test(pipeline, None, 0, lambda x: x+2, lambda x: None, lambda x: x+3)

def trough(f):
    '''Support decorator for cascades.'''
    def dostep(**params):
        return merge(params, f(**params))
    return dostep

def cascade(*functions, **arguments):
    '''Sloppy state container, higher order function.
    Receives a series of functions and some starting
    kwargs that will be handed off to each function
    as arguments in the order they are given.

    Each function in the cascade must receive kwargs
Esempio n. 9
0
def linesOf(stream): # rig:3 
    '''Could not read lines out of stream.'''
    return firstOrNone(*mchain(stream, "readlines", "close"))

@attempt
def goodLines(linesrc): # rig:3 
    '''Bad line list.'''
    return hasattr(linesrc, "__iter__") or None

@attempt
def _cleanLines(cleanWith, lines):
    '''Can not clean the lines list with method specified.'''
    if goodLines(lines) and callable(cleanWith):
        return map(cleanWith, lines)
    return None
test(_cleanLines, ["",""], lambda x: "", ["aoeu", "ntehu"])
test(_cleanLines, ["aoeu","ueoa"], lambda x: x.strip(), ["  aoeu ", "\nueoa\t"])
test(_cleanLines, [3,4], lambda x: x+2, [1,2])

@attempt
def bytesOf(stream):
    '''Could not read data from stream.'''
    return firstOrNone(*mchain(stream, "read", "close"))

@attempt
def readFile(filePath, mode, *etc): # rig:3
    '''Failed to load file'''
    return pipeline(filePath,
                      fpth,
                      partial(loadf, mode),
                      *etc)