Пример #1
0
def suggest_errors(name, func):
    print_h2('Suggestions for: {}'.format(name))
    args, varargs, keywords, defaults = insp.getargspec(func)
    prnt('Args:', args)
    prnt('VarArgs:', varargs)
    prnt('Kwargs:', keywords)
    prnt('Defaults:', '')

    if defaults is not None:
        for val in defaults:
            if isinstance(val, int):
                print('Default val "{}" is an int; '
                      'have you tried testing a float?'.format(val))
            if isinstance(val, float):
                print('Default val "{}" is an float; '
                      'have you tried testing an int?'.format(val))
            if isinstance(val, str):
                print('Default val "{}" is a str; '
                      'have you tried testing a string? or None?'.format(val))
            if isinstance(val, list):
                print('Default val "{}" is a list; '
                      'have you tried testing empty? '
                      'full of values? with a dictionary?'.format(val))
            if isinstance(val, dict):
                print('Default val "{}" is a dict; '
                      'have you tried testing empty? '
                      'full of values? with a list? '
                      'with bad keys'.format(val))
            if isinstance(val, bool):
                print('Default val "{}" is a bool; have you tried the '
                      'opposite ({})?'.format(val, not val))
Пример #2
0
try:
    test_db = couch.create('test_db')
except couchdb.http.PreconditionFailed:
    couch.delete('test_db')
    test_db = couch.create('test_db')


def make_person():
    return {
        'name': faker.name(),
        'email': faker.email(),
        'address': faker.address(),
        'url': faker.url(),
        'created': str(dt.now())
    }


@test_speed
def insert_all(max_records):
    for n in range(max_records):
        test_db.save(make_person())


if DEBUG:
    with Section('CouchDB (via python-couchdb)'):
        run_trials(insert_all, trials=10)
        print_h2('Result from CouchDB execution: ')
        for res in test_db:
            divider()
            ppr(test_db[res])
Пример #3
0
def run_random_transducer(**kwargs):
    schema = generate_transducer(**kwargs)
    transducer = Transducer(states=schema, default='S0')
    transducer.run_all()


if DEBUG:
    with Section('Finite State Machine - Transducer'):
        # Recreation of http://www-01.sil.org/pckimmo/v2/doc/Figure-A3.gif
        transducer = Transducer(states={
            'S1': {'transitions': {'a': 'a:S2'}, 'val': 'Start'},
            'S2': {'transitions': {'b': 'b:S3', 'a': 'a:S4'}},
            'S3': {'transitions': {'a': 'a:S4', 'b': 'c:S2'}},
            'S4': {'transitions': None},
        }, default='S1', debug=True)
        print_h2('Running all edge transitions')
        transducer.run_all()
        # Made up
        transducer = Transducer(states={
            'S0': {'transitions': {'c': 'a:S1', 'd': 'e:S2'}, 'val': 'Start'},
            'S1': {'transitions': {'p': 'i:S3', 'h': 'o:S4'}},
            'S2': {'transitions': {'e': 'a:S3'}},
            'S3': {'transitions': {'d': 'e:S4', 'a': 'c:S0'}},
            'S4': {'transitions': None},
        }, default='S0', debug=True)
        print_h2('Running all edge transitions')
        transducer.run_all()
        # Fully automated, non-deterministic - which may lead to non-halting
        # or invalid states when testing.
        run_random_transducer()
Пример #4
0
def _print(*args):
    print('got: {}'.format(_fmt(*args)))


def print_operator(x):
    print('[operator]: {}'.format(x))


def print_len3(x):
    print('`{}` is at least 3 characters long.'.format(x))


if DEBUG:
    with Section('Reactive (sort of functional) programming via RxPY'):
        print_h2('Basic Observable')
        xs = Observable.from_iterable(range(10) + [None])
        d = xs.subscribe(MyObserver())

        observable_iterable = Observable.from_iterable(xrange(100))
        logwriter = observable_iterable.subscribe(LogWriterObserver())
        print(logwriter)

        print_h2('Observable from_')
        xs = Observable.from_(range(10))
        gobbledygook = Observable.from_(list(string.punctuation))
        letters = Observable.from_(list(string.ascii_uppercase))
        merged = xs.merge(letters, gobbledygook).subscribe(_print)

        print_h2('Subjects')
        stream = TestSubject()
Пример #5
0
       [+]
       / \
     [2] [2]

    etc... `foo = 3 * 9`

        (ROOT)
         /  \
       [=]  [*]
       /    /  \
     [foo] [3] [9]

    """

if DEBUG:
    with Section('Abstract Syntax Tree (AST)'):
        print_h2('Reading functions and then building ast and executing.')
        src1 = inspect.getsource(foo)
        src2 = inspect.getsource(bar)
        tree = ast.parse(src1 + '\nfoo()\n' + src2 + '\nbar()')
        print_info(tree)
        exec(compile(tree, filename='<ast>', mode='exec'))

        drawing = ast.parse(inspect.getsource(ast_drawing), filename='<ast>')
        print_info('Printing AST docstring, a drawing of an AST (such meta)')
        for node in ast.walk(drawing):
            try:
                print(ast.get_docstring(node))
            except TypeError:
                continue
Пример #6
0
class Monad(pm.Monad):
    def bind(self, *args):
        pass

    def fmap(self, *args):
        pass

    def amap(self, *args):
        pass


if DEBUG:
    with Section('Functional programming with PyMonad'):
        # Partial Applications
        # Curried vs. non-curried
        print_h2('Partial Applications')
        assert add2(2, 3) == add2(2)(3)
        assert add3(2, 3, 4) == add3(2, 3)(4)
        add_to_5 = add3(2, 3)
        print(add_to_5(10))  # 15
        # Partially applied thrice
        assert add3(1)(2)(3) == add3(1, 2, 3)

        # Compositions
        print_h2('Compositions')
        composed = add * sub2
        # Add numbers (2), then subtract 2, as the function composition entails.
        assert composed(1, 1) == 0
        # Composed and partially applied.
        comp_partial = add2(2) * add3(2, 3)
        print(comp_partial(2))
Пример #7
0
                              '\n{}pass\n'.format(indent))
                entity_generated = True
            elif pos in ['VB', 'VBP', 'NNS', 'VBZ']:
                val = stemmer.stem(val.lower())
                self.data += ('\n    def {}(self, *args, **kwargs):\n'
                              '{}pass\n').format(val, indent)
        print('--------- Model ------------\n{}'.format(self.data))
        return self


if DEBUG:
    with Section('Reification - concrete examples'):
        # Most of the examples above can be demonstrated without instantiation,
        # since the idea is simply to show how one can take a concept
        # and convert it into a real assemblage of code.
        print_h2('Reification of processes: A baking recipe')
        # http://m.allrecipes.com/recipe/22850/chewy-sugar-cookies/
        cookies = Recipe([
            ('Preheat oven to 350 degrees F (175 degrees C). In a medium bowl, '
             'stir together the flour, baking soda, and salt; set aside.'),
            ('In a large bowl, cream together the margarine and 2 cups sugar '
             'until light and fluffy. Beat in the eggs one at a time, then the '
             'vanilla. Gradually stir in the dry ingredients until just '
             'blended. Roll the dough into walnut sized balls and roll the '
             'balls in remaining 1/4 cup of sugar. Place cookies 2 inches apart'
             ' onto ungreased cookie sheets and flatten slightly.'),
            ('Bake for 8 to 10 minutes in the preheated oven, until lightly '
             'browned at the edges. Allow cookies to cool on baking sheet for '
             '5 minutes before removing to a wire rack to cool completely.'),
        ])
        cookies.execute()
Пример #8
0
        linked_list['head'] = 'FOO'
        linked_list['node-3'] = 'BAR'
        linked_list['node-5'] = 'BIM'

        assert linked_list['head'].cargo == 'FOO'
        assert linked_list['node-3'].cargo == 'BAR'
        assert linked_list['node-5'].cargo == 'BIM'

        prnt('Testing value update', '', newlines=False)

        for node in linked_list:
            print(node)
            assert node.cargo

        print_h2('Parallel arrays')
        # "Parallel" arrays
        MAX_ITEMS = 10
        names = [faker.name() for _ in range(MAX_ITEMS)]
        emails = [faker.email() for _ in range(MAX_ITEMS)]
        parallel = [names, emails]
        for index in range(MAX_ITEMS):
            print('Name: {}, Email: {}'.format(names[index], emails[index]))

        print_h2('Matrix/Vector arrays')
        matrix = dm.random_matrix(rows=5, columns=5, choices=range(20))
        print_simple('Matrix', matrix)

        print_h2('Sorted array')
        # Props to John Neumann for "inventing" it
        orig = range(10)
Пример #9
0
def halloer_male():
    """Halloer func."""
    return functools.partial(
        greet, prefix='Herr', greeting='Hallo mein freuden')


def halloer_female():
    """Halloer func."""
    return functools.partial(
        greet, prefix='Frau', greeting='Hallo mein freuden')


if DEBUG:
    with Section('Functional programming with itertools'):
        print_h2('cmp_to_key')
        p1 = random_person_full()
        p2 = random_person_full()

        resfirst = sorted([p1, p2], cmp_people_fname)
        reslast = sorted([p1, p2], cmp_people_lname)

        ft_resfirst = sorted(
            [p1, p2], key=functools.cmp_to_key(cmp_people_fname))
        ft_reslast = sorted(
            [p1, p2], key=functools.cmp_to_key(cmp_people_lname))

        ppr([resfirst, reslast])
        ppr([ft_resfirst, ft_reslast])

        print_h2('total_ordering')
Пример #10
0
        return self.versions[key]


class ConfluentlyPersistentPathCopyingNode(ConfluentlyPersistentNode):
    """TODO"""


class ConfluentlyPersistentPathCopyingFatNode(
        ConfluentlyPersistentFatNode, ConfluentlyPersistentPathCopyingNode):
    """TODO"""


if DEBUG:
    with Section('Persistent data structures'):

        print_h2('Persistent partial node')
        pnode = PartiallyPersistentNode()
        print(pnode)
        pnode['foo'] = {'foo': 'bar', 'bim': 'baz'}
        print(pnode)
        pnode.view_all()
        # Should work, as it's the single item and current.
        pnode['foo'] = [2, 3, 4, 5]
        # Should work, since it's new
        pnode['bar'] = [1, 2, 3]
        try:
            del pnode['bar']
        except MutableAccessException:
            pass
        try:
            # Should not work, since it exists,
Пример #11
0
__author__ = """Chris Tabor ([email protected])"""

if __name__ == '__main__':
    from os import getcwd
    from os import sys
    sys.path.append(getcwd())

from MOAL.helpers.display import Section
from MOAL.helpers.display import print_simple
from MOAL.helpers.display import print_h2
import tiger
import hashlib
import hmac

DEBUG = True if __name__ == '__main__' else False

if DEBUG:
    with Section('Message digest cryptography functions'):
        sekrit = 'A very very secret thing'
        print_h2('Hashlib algorithms')
        for algo in hashlib.algorithms:
            hashed = hashlib.new(algo).hexdigest()
            print_simple(algo, hashed, newline=False)

        print_h2('HMAC')
        _hmac = hmac.new('*****@*****.**', sekrit)
        print(_hmac.hexdigest())

        _tiger = tiger.new(sekrit).hexdigest()
        print_simple('Tiger (TTH)', _tiger)
Пример #12
0
                              '\n{}pass\n'.format(indent))
                entity_generated = True
            elif pos in ['VB', 'VBP', 'NNS', 'VBZ']:
                val = stemmer.stem(val.lower())
                self.data += ('\n    def {}(self, *args, **kwargs):\n'
                              '{}pass\n').format(val, indent)
        print('--------- Model ------------\n{}'.format(self.data))
        return self


if DEBUG:
    with Section('Reification - concrete examples'):
        # Most of the examples above can be demonstrated without instantiation,
        # since the idea is simply to show how one can take a concept
        # and convert it into a real assemblage of code.
        print_h2('Reification of processes: A baking recipe')
        # http://m.allrecipes.com/recipe/22850/chewy-sugar-cookies/
        cookies = Recipe([
            ('Preheat oven to 350 degrees F (175 degrees C). In a medium bowl, '
             'stir together the flour, baking soda, and salt; set aside.'),
            ('In a large bowl, cream together the margarine and 2 cups sugar '
             'until light and fluffy. Beat in the eggs one at a time, then the '
             'vanilla. Gradually stir in the dry ingredients until just '
             'blended. Roll the dough into walnut sized balls and roll the '
             'balls in remaining 1/4 cup of sugar. Place cookies 2 inches apart'
             ' onto ungreased cookie sheets and flatten slightly.'),
            ('Bake for 8 to 10 minutes in the preheated oven, until lightly '
             'browned at the edges. Allow cookies to cool on baking sheet for '
             '5 minutes before removing to a wire rack to cool completely.'),
        ])
        cookies.execute()
Пример #13
0
class Monad(pm.Monad):
    def bind(self, *args):
        pass

    def fmap(self, *args):
        pass

    def amap(self, *args):
        pass


if DEBUG:
    with Section('Functional programming with PyMonad'):
        # Partial Applications
        # Curried vs. non-curried
        print_h2('Partial Applications')
        assert add2(2, 3) == add2(2)(3)
        assert add3(2, 3, 4) == add3(2, 3)(4)
        add_to_5 = add3(2, 3)
        print(add_to_5(10))  # 15
        # Partially applied thrice
        assert add3(1)(2)(3) == add3(1, 2, 3)

        # Compositions
        print_h2('Compositions')
        composed = add * sub2
        # Add numbers (2), then subtract 2, as the function composition entails.
        assert composed(1, 1) == 0
        # Composed and partially applied.
        comp_partial = add2(2) * add3(2, 3)
        print(comp_partial(2))
Пример #14
0
try:
    test_db = couch.create('test_db')
except couchdb.http.PreconditionFailed:
    couch.delete('test_db')
    test_db = couch.create('test_db')


def make_person():
    return {
        'name': faker.name(),
        'email': faker.email(),
        'address': faker.address(),
        'url': faker.url(),
        'created': str(dt.now())
    }


@test_speed
def insert_all(max_records):
    for n in range(max_records):
        test_db.save(make_person())


if DEBUG:
    with Section('CouchDB (via python-couchdb)'):
        run_trials(insert_all, trials=10)
        print_h2('Result from CouchDB execution: ')
        for res in test_db:
            divider()
            ppr(test_db[res])
Пример #15
0
    for i in range(0, len2):
        v1[i] = i

    for i in range(0, len1):
        v2[0] = i + 1
        for j in range(0, len2):
            cost = 0 if s1[i] == s2[j] else 1
            v2[j + 1] = min(v1[j] + 1, v1[j + 1] + 1, v1[j] + cost)
        for _j in range(0, lenv1):
            v1[_j] = v2[_j]

    dist = v2[len2]
    print('Levenshtein distance of "{}" and "{}" is {}'.format(s1, s2, dist))
    return dist


if __name__ == '__main__':
    with Section('Algorithms / coding theory - Levenshtein distance'):
        print_h2('Levenshtein distance', 'recursive')
        lev_recursive('mop', 'pop')

        print_h2('Levenshtein distance', 'iterative')
        pairs = [
            ('slaughter', 'laughter'), ('flower', 'power'),
            ('liliputian', 'brobdingnagian'),
            ('sitting', 'kitten'),
            ('gargantua', 'pentagruel'),
        ]
        for pair in pairs:
            lev_iterative(*pair)
Пример #16
0
        # The first time this is run will be quite slow -- running
        # the file again should be completely fine.
        # You can also run the command straight in your terminal.
        gen_largefile(size=1024000)  # 1GB
        reader = readfile(os.getcwd() + '/file.gz')
        print(reader)
        next(reader)
        next(reader)
        for _ in range(3):
            next(reader)

        prodgen = product()
        for _ in range(100):
            next(prodgen)

        print_h2('Generator expressions')
        # This won't work (MemoryError),
        # as range is still called and loaded into memory - *lame*.
        # genexpr = (f for f in range(1000000000000))
        # These work, but it is hard to argue their usefulness.
        genexpr = (f for f in get_nums())
        genexpr2 = (f for f in range(100))  # This still loads into memory.
        print(genexpr)
        for _ in range(10):
            next(genexpr)
        # Keep running if we want...
        next(genexpr)
        next(genexpr)
        # Even get the value
        assert next(genexpr) == 12
Пример #17
0
        if DEBUG:
            sleep(DELAY)
        bits = _shift_circular(bits)
    return bits


def rotate_through_carry(bits):
    """Like circular, but the end bits are stored as flags."""
    pass


if __name__ == '__main__':
    with Section('Computer organization - Bitwise operations'):
        assert bitwise_not('0111') == '1000'

        print_h2('Bitwise operations', desc='Wikipedia testing')
        # Testing examples from Wikipedia
        and_pairs = [('0110', '1101', '0100'), ('0101', '0011', '0001'),
                     ('0011', '0010', '0010'), ('0110', '0001', '0000')]
        or_pairs = [('0101', '0011', '0111'), ('0010', '1000', '1010')]
        xor_pairs = [('0101', '0011', '0110'), ('0010', '1010', '1000')]

        test_operator_pairs(and_pairs, bitwise_and)
        test_operator_pairs(or_pairs, bitwise_or)
        test_operator_pairs(xor_pairs, bitwise_xor)

        print_h2('Bitwise operations', desc='Randomly generated')

        for _ in range(5):
            pair = (rand_byte(), rand_byte())
            bitwise_not(pair[0])
Пример #18
0
if __name__ == '__main__':
    from os import getcwd
    from os import sys
    sys.path.append(getcwd())

from MOAL.helpers.display import Section
from MOAL.helpers.display import prnt
from MOAL.helpers.display import print_h2
from MOAL.helpers.datamaker import make_sparselist
from MOAL.helpers.datamaker import make_sparsematrix
from MOAL.helpers.text import gibberish2
from pprint import pprint as ppr
from random import randrange as rr

DEBUG = True if __name__ == '__main__' else False


if DEBUG:
    with Section('Sparse linear data structures'):
        max = 100
        density = 0.1
        items = {rr(0, max): gibberish2() for _ in range(int(max * density))}
        splist = make_sparselist(items, max)
        prnt('Sparse list', splist)

        sparse_data = {(x, x): gibberish2() for x in range(0, 10)}
        sparsematrix = make_sparsematrix(sparse_data, max, rows=10, cols=10)
        print_h2('Sparse matrix')
        ppr(sparsematrix)
Пример #19
0
        """See http://scikit-learn.org/stable/modules/tree.html for info.

        RULE OF THUMB:
        Classification = ClassificationTree
        Prediction = RegressionTreee
        """

        training = [
            [0, 0, 0],
            [1, 1, 1],
            [2, 2, 2],
            [4, 4, 4],
            [10, 10, 10]
        ]
        labels = ['thing0', 'thing1', 'thing2', 'thing4', 'thing10']
        print_h2('Fitting tree with training: {} and labels: {}'.format(
            training, labels))
        dec_tree = tree.DecisionTreeClassifier()
        dec_tree = dec_tree.fit(training, labels)
        run_predictions(tree)

        # ----------------------------------------------------------------------

        training = [
            [32905.00, 1230.00, 4, 4, 32.5, 4],
            [64230.00, 801.00, 4, 4, 29.3, 3],
            [18540.00, 230.00, 4, 4, 27.5, 3],
            [35540.00, 990.00, 2, 2, 22.5, 3],
            [45901.00, 2210.00, 4, 4, 30.0, 5],
        ]
        features = [range(0, 5) for _ in range(5)]
        labels = [
Пример #20
0
        # The first time this is run will be quite slow -- running
        # the file again should be completely fine.
        # You can also run the command straight in your terminal.
        gen_largefile(size=1024000)  # 1GB
        reader = readfile(os.getcwd() + '/file.gz')
        print(reader)
        next(reader)
        next(reader)
        for _ in range(3):
            next(reader)

        prodgen = product()
        for _ in range(100):
            next(prodgen)

        print_h2('Generator expressions')
        # This won't work (MemoryError),
        # as range is still called and loaded into memory - *lame*.
        # genexpr = (f for f in range(1000000000000))
        # These work, but it is hard to argue their usefulness.
        genexpr = (f for f in get_nums())
        genexpr2 = (f for f in range(100))  # This still loads into memory.
        print(genexpr)
        for _ in range(10):
            next(genexpr)
        # Keep running if we want...
        next(genexpr)
        next(genexpr)
        # Even get the value
        assert next(genexpr) == 12
Пример #21
0
                    'human_to_robot':
                    {dm.random_dna(): txt.random_binary(4)
                     for _ in range(4)},
                    'robot_to_human':
                    {txt.random_binary(4): dm.random_dna()
                     for _ in range(4)}
                }
            })

        prnt('List comprehension', [x**2 for x in range(10) if x % 2 == 0])
        prnt('List comprehension - nested',
             [[x**y for x in range(1, 4) if x % 2 == 0] for y in range(1, 8)])

        wtf = [[_nested(min=x), _nested(max=y)]
               for x, y in enumerate(range(1, 10))]
        print_h2('List comprehensions - triple nested')
        ppr(wtf)

        print_h2('Dictionary and list comprehensions, combined')
        dl_combined = {
            txt.gibberish2(): _nested(min=x, max=x * 2)
            for x in range(8)
        }
        ppr(dl_combined)

        print_h2('Set comprehension')
        set_comp = set([choice(range(100)) for _ in range(10)])
        ppr(set_comp)

        print_h2('Frozenset comprehension')
        frozen_set_comp = frozenset([choice(range(100)) for _ in range(10)])
Пример #22
0
    def rank(self, node):
        ranked = []
        for _node in self:
            ranked.append((_node, _node.size))
        # Sort by size, but leave node reference intact.
        for data in ranked:
            _node, size = data
            if _node.name == node:
                return size


if DEBUG:
    with Section('Order statistic - Binary Search Tree'):
        stats_bst = OrderStatisticBST()
        # Create a reasonable spread out tree
        for x in range(1, 6):
            stats_bst.put(x * x, {'num': x * x})
            stats_bst.put(x * 20, {'num': x * 20})
        bst.recurse_bst(stats_bst.root, None)

        print(stats_bst[1].data)
        print(stats_bst[100].data)

        print_h2('Testing select function')
        for n in range(10):
            print(stats_bst.select(n))

        print_h2('Testing rank function')
        for n in range(10):
            print(stats_bst.rank(n))
Пример #23
0
        self.registers[reg] = val


if __name__ == '__main__':
    with Section('Counter Machines'):
        classes = [
            SheperdsonSturgis, Minsky, Program, Abacus, Lambek,
            Successor, SuccessorRAM, ElgotRobinsonRASP,
        ]

        for klass in classes:
            prnt('Testing machine...', repr(klass))
            klass().run()

            cmd_title('New program')
            singleton = CounterMachine()
            singleton._generate_program()
            ppr(singleton.program)
            try:
                singleton.run()
            except TypeError:
                print('Inoperable program was generated :(')
            except NotImplementedError:
                print_error('Not implemented: {}'.format(klass))
            finally:
                singleton.halt()

        print_h2('Random Access Machine (multi-register counter machine)')
        ram = RandomAccessMachine()
        ram.run()
Пример #24
0
            print_error(''.join(chars), prefix='[NOT-FOUND]')
            return False


class Classifier(Acceptor):
    """From Wikipedia:
    "Classifier is a generalization that, similar to acceptor,
    produces single output when terminates but has more
    than two terminal states." """

    def classify(self, *args, **kwargs):
        return super(Classifier, self).test(*args, **kwargs)


if DEBUG:
    with Section('Finite State Machine'):
        # Example creation of
        # en.wikipedia.org/wiki/Finite-state_machine#/media/File:DFAexample.svg
        print_h2('FSM - Acceptor')
        acceptor = Acceptor(
            states={
                'S1': {'transitions': {0: 'S2', 1: 'S1'}, 'val': 'End node'},
                'S2': {'transitions': {0: 'S1', 1: 'S2'}, 'val': 'Start node'}},
            default='S1')
        assert acceptor.test('01100101011')
        assert acceptor.test('11')
        assert not acceptor.test('113')
        assert acceptor.test('1110001010')
        assert acceptor.test('0')
        assert acceptor.test('1')
Пример #25
0
if __name__ == '__main__':
    from os import getcwd
    from os import sys
    sys.path.append(getcwd())

from MOAL.helpers.display import Section
from MOAL.helpers.display import print_simple
from MOAL.helpers.display import print_h2
import tiger
import hashlib
import hmac

DEBUG = True if __name__ == '__main__' else False


if DEBUG:
    with Section('Message digest cryptography functions'):
        sekrit = 'A very very secret thing'
        print_h2('Hashlib algorithms')
        for algo in hashlib.algorithms:
            hashed = hashlib.new(algo).hexdigest()
            print_simple(algo, hashed, newline=False)

        print_h2('HMAC')
        _hmac = hmac.new('*****@*****.**', sekrit)
        print(_hmac.hexdigest())

        _tiger = tiger.new(sekrit).hexdigest()
        print_simple('Tiger (TTH)', _tiger)
Пример #26
0
                result += '/'
                fives = 0
            else:
                result += '|'
            fives += 1
    print_h4('Base 1', desc='aka "tally"')
    print('{} base 1 = {}'.format(num, result))


if DEBUG:
    with Section('Numerical encoding: "N-ary" (positional) - extended'):
        """I thought I had the extent of the positional number systems,
        but much more exist, and many can be found at:
        http://www.calculand.com/unit-converter/zahlen.php"""

        print_h2('N to Decimal',
                 desc='Conversion to decimal from a given base')

        for n in range(4):
            print_h4('Convert base {}'.format(n))
            n_to_dec(999, n)

        print_h2(
            'Decimal to N',
            desc='Conversion of numbers to any base with divide-by technique.')

        for base_info in BASES:
            base, title = base_info['base'], base_info['name']
            print_h4('Base {}'.format(base), desc='aka "{}"'.format(title))
            test_base(base_info)

        for num in [4, 256, 512]:
Пример #27
0
    def rank(self, node):
        ranked = []
        for _node in self:
            ranked.append((_node, _node.size))
        # Sort by size, but leave node reference intact.
        for data in ranked:
            _node, size = data
            if _node.name == node:
                return size


if DEBUG:
    with Section('Order statistic - Binary Search Tree'):
        stats_bst = OrderStatisticBST()
        # Create a reasonable spread out tree
        for x in range(1, 6):
            stats_bst.put(x * x, {'num': x * x})
            stats_bst.put(x * 20, {'num': x * 20})
        bst.recurse_bst(stats_bst.root, None)

        print(stats_bst[1].data)
        print(stats_bst[100].data)

        print_h2('Testing select function')
        for n in range(10):
            print(stats_bst.select(n))

        print_h2('Testing rank function')
        for n in range(10):
            print(stats_bst.rank(n))
Пример #28
0
    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    email = Column(String(255))
    url = Column(String(255))

    def __repr__(self):
        return '<Person(name={}, email={}, url={})>'.format(
            self.name, self.email, self.url)


@test_speed
def insert_all(max_records):
    people = [random_person() for n in range(max_records)]
    prnt('Records to create:', people)
    for person in people:
        # Don't need this prop for our example schema.
        del person['address']
        db_session.add(Person(**person))

if DEBUG:
    with Section('MySQL - SQL Alchemy'):
        Base.metadata.create_all(engine)

        print_h2('Adding a bunch of records...')
        run_trials(insert_all, trials=10)

        print_h2('Reading all records...')
        recs = db_session.query(Person).all()
        ppr(recs)
Пример #29
0
    def _hash(self, data):
        return tiger.new(data).hexdigest()


if DEBUG:
    with Section('Hashed array tree'):
        data = {
            0: {'edges': [1, 2, 3], 'is_root': True},
            1: {'edges': [], 'parent': 0},
            2: {'edges': [4], 'parent': 0},
            3: {'edges': [5], 'parent': 0},
            4: {'edges': [], 'parent': 0},
            5: {'edges': [], 'parent': 0},
        }
        for k, node in data.iteritems():
            node['val'] = gibberish2()

        print_h2('Merkle Tree')
        merkle = MerkleTree(data)
        for k, node in merkle.vertices.iteritems():
            print('Node edges: {}, Child hash: {}'.format(
                node['edges'], node['val']))

        print_h2('Tiger Tree Hash')
        data[0]['edges'].remove(3)
        tth = TigerTreeHash(data)
        for k, node in tth.vertices.iteritems():
            print('Node edges: {}, Child hash: {}'.format(
                node['edges'], node['val']))
Пример #30
0
        for version in self.versions[key]:
            print('- (Partially) retroactively updating item')
            version = self._handlenode(self.versions[key][version])


class FullyRetroactiveNode(RetroactiveNode,
                           persistent.ConfluentlyPersistentFatNode):
    def _update_retroactive(self, key):
        for version in self.versions[key]['versions']:
            print('- Retroactively updating item')
            version = self._handlenode(version['data'])


if DEBUG:
    with Section('Retroactive data structures'):
        print_h2('Partially retroactive node')
        partial = PartiallyRetroactiveNode()
        _example = {'foo': 'bam'}
        partial['foo'] = _example
        partial['bar'] = {'foo': 'bam'}
        # Do some updates to test.
        for x in range(4):
            partial['foo'] = {
                '{}{}'.format(x, k): v
                for k, v in _example.iteritems()
            }
        # Test plain values
        partial['foo'] = 133332
        print_simple('Partially retroactive data:', partial.versions)
        print_simple('All data for `foo`:', partial['foo'])
Пример #31
0
if __name__ == '__main__':
    from os import getcwd
    from os import sys
    sys.path.append(getcwd())

from MOAL.helpers.display import Section
from MOAL.helpers.display import print_h2
from MOAL.helpers.display import cmd_title
from parsimonious.grammar import Grammar

DEBUG = True if __name__ == '__main__' else False

if DEBUG:
    with Section('Embedded Domain Specific Language (EDSL)'):
        print_h2('Parsing a grammar using the "parsimonious" library')
        button_grammar = Grammar(r"""
            btn = btn1 / btn2
            btn2 = "[" text "]"
            btn1 = "((" text "))"
            text = ~"[A-Z 0-9]*"i
            """)

        cmd_title('Printing [mybutton] and ((mybutton)) &', newlines=False)
        print(button_grammar.parse('[ mybutton ]'))
        print(button_grammar.parse('(( mybutton ))'))

        # Order matters - e.g. `tag` must come first, as it builds from the
        # previous tokens. This is obviously extremely naive, as it doesn't
        # check valid HTML-matching elements, nesting, single + wrapped
        # combos, attributes, etc....
Пример #32
0
        for version in self.versions[key]:
            print('- (Partially) retroactively updating item')
            version = self._handlenode(self.versions[key][version])


class FullyRetroactiveNode(
        RetroactiveNode, persistent.ConfluentlyPersistentFatNode):

    def _update_retroactive(self, key):
        for version in self.versions[key]['versions']:
            print('- Retroactively updating item')
            version = self._handlenode(version['data'])

if DEBUG:
    with Section('Retroactive data structures'):
        print_h2('Partially retroactive node')
        partial = PartiallyRetroactiveNode()
        _example = {'foo': 'bam'}
        partial['foo'] = _example
        partial['bar'] = {'foo': 'bam'}
        # Do some updates to test.
        for x in range(4):
            partial['foo'] = {
                '{}{}'.format(x, k): v for k, v in _example.iteritems()}
        # Test plain values
        partial['foo'] = 133332
        print_simple('Partially retroactive data:', partial.versions)
        print_simple('All data for `foo`:', partial['foo'])

        print_h2('Fully retroactive node')
        full = FullyRetroactiveNode()
Пример #33
0
__author__ = """Chris Tabor ([email protected])"""

if __name__ == '__main__':
    from os import getcwd
    from os import sys
    sys.path.append(getcwd())

from MOAL.helpers.display import Section
from MOAL.helpers.display import prnt
from MOAL.helpers.display import print_h2
from MOAL.helpers.datamaker import make_sparselist
from MOAL.helpers.datamaker import make_sparsematrix
from MOAL.helpers.text import gibberish2
from pprint import pprint as ppr
from random import randrange as rr

DEBUG = True if __name__ == '__main__' else False

if DEBUG:
    with Section('Sparse linear data structures'):
        max = 100
        density = 0.1
        items = {rr(0, max): gibberish2() for _ in range(int(max * density))}
        splist = make_sparselist(items, max)
        prnt('Sparse list', splist)

        sparse_data = {(x, x): gibberish2() for x in range(0, 10)}
        sparsematrix = make_sparsematrix(sparse_data, max, rows=10, cols=10)
        print_h2('Sparse matrix')
        ppr(sparsematrix)
Пример #34
0
        # convert the query to LSI space
        vec_lsi = lsi[vec_bagofwords]
        # print(vec_lsi)
        index = similarities.MatrixSimilarity(lsi[corpus])
        return index[vec_lsi]


if DEBUG:
    with Section('Topic Modeling'):

        doc_tokens = [get_filetokens('topic{}.txt'.format(
                      filenum)) for filenum in range(1, 5)]

        tm = TopicModeler(doc_tokens)

        print_h2('Token frequency')
        ppr(tm.frequency())

        # Compact tokens, scoring and remove empty values
        tm.compactify()

        print_h2('Token ID and bag-of-word as vectors')
        # Convert the document to a vector of ID and bag-of-words
        vectors = tm.get_vectors()
        print(vectors)

        # print_h2('Show (lockstep), the vectors and corresponding docs')
        # for k, doc in enumerate(doc_tokens):
        #     print('{} {}'.format(vectors[k], doc))

        print_h2('Token IDs (via `token2id`)')
Пример #35
0
    def __init__(self, content):
        self.content = content

    def _print(self, data):
        print('Saved: {}'.format(data))

    def __enter__(self):
        print('Saving data: {}'.format(self.content))

    def __exit__(self, exception_type, exception_value, traceback):
        map(self._print, self.content)


if DEBUG:
    with Section('Context managers (aka "with")'):
        print_h2('[decorator] with 1 - database save wrapper, simplified')
        with orm_save(FakeDatabase, data={'my': 'fake-data'}) as saved:
            db, data = saved
            for k, data in data.iteritems():
                print('Yielded results: {}'.format(data))

        print_h2('[decorator] with 2 - database save wrapper, simplified')
        with orm_save2(foo='bar', bim='bam') as stuff:
            for data in stuff:
                print('Yielded results: {}'.format(data))

        print_h2('[decorator] nested context managers - save to db + show html')
        stuff = {
            'dna': dm.random_dna(max=20), 'faketrix': dm.random_binary_matrix()}
        with orm_save2(**stuff) as data, to_list(**data) as html:
            print('Saved all data, and converted to html: {}'.format(html))
Пример #36
0
def _print(*args):
    print('got: {}'.format(_fmt(*args)))


def print_operator(x):
    print('[operator]: {}'.format(x))


def print_len3(x):
    print('`{}` is at least 3 characters long.'.format(x))


if DEBUG:
    with Section('Reactive (sort of functional) programming via RxPY'):
        print_h2('Basic Observable')
        xs = Observable.from_iterable(range(10) + [None])
        d = xs.subscribe(MyObserver())

        observable_iterable = Observable.from_iterable(xrange(100))
        logwriter = observable_iterable.subscribe(LogWriterObserver())
        print(logwriter)

        print_h2('Observable from_')
        xs = Observable.from_(range(10))
        gobbledygook = Observable.from_(list(string.punctuation))
        letters = Observable.from_(list(string.ascii_uppercase))
        merged = xs.merge(letters, gobbledygook).subscribe(_print)

        print_h2('Subjects')
        stream = TestSubject()
Пример #37
0
if DEBUG:
    with Section('Map Abstract Data Type'):
        map_adt = MapADT(title='map_head')

        for n in range(6):
            key = 'map_item-{}'.format(''.join(randchars(8)))
            map_adt[key] = ''.join(randchars(4))

        print(map_adt)

    with Section('Multi-Map Abstract Data Type'):
        faker = Factory.create()
        multimap = MultiMap(title='multi-map_head')

        print_h2('Multimap', desc='General abstract usage.')

        for n in range(4):
            key = 'map_item-{}'.format(n)
            multimap[key] = ''.join(randchars(4))

        print(multimap)
        print(multimap['map_item-1'])

        print_h2('Multimap', desc='Used contextually for school enrollment.')

        classes_map = Enrollment(
            classes=['English', 'Comp Sci', 'History', 'Math', 'Art'],
            students=[faker.name() for _ in range(3)] + ['Ms. Ella Tabor'])

        map(classes_map.enroll_all, classes_map.schedules())
Пример #38
0
                fives = 0
            else:
                result += '|'
            fives += 1
    print_h4('Base 1', desc='aka "tally"')
    print('{} base 1 = {}'.format(num, result))


if DEBUG:
    with Section('Numerical encoding: "N-ary" (positional) - extended'):

        """I thought I had the extent of the positional number systems,
        but much more exist, and many can be found at:
        http://www.calculand.com/unit-converter/zahlen.php"""

        print_h2('N to Decimal', desc='Conversion to decimal from a given base')

        for n in range(4):
            print_h4('Convert base {}'.format(n))
            n_to_dec(999, n)

        print_h2(
            'Decimal to N',
            desc='Conversion of numbers to any base with divide-by technique.')

        for base_info in BASES:
            base, title = base_info['base'], base_info['name']
            print_h4('Base {}'.format(base), desc='aka "{}"'.format(title))
            test_base(base_info)

        for num in [4, 256, 512]:
Пример #39
0
        return self.versions[key]


class ConfluentlyPersistentPathCopyingNode(ConfluentlyPersistentNode):
    """TODO"""


class ConfluentlyPersistentPathCopyingFatNode(
        ConfluentlyPersistentFatNode, ConfluentlyPersistentPathCopyingNode):
    """TODO"""


if DEBUG:
    with Section('Persistent data structures'):

        print_h2('Persistent partial node')
        pnode = PartiallyPersistentNode()
        print(pnode)
        pnode['foo'] = {'foo': 'bar', 'bim': 'baz'}
        print(pnode)
        pnode.view_all()
        # Should work, as it's the single item and current.
        pnode['foo'] = [2, 3, 4, 5]
        # Should work, since it's new
        pnode['bar'] = [1, 2, 3]
        try:
            del pnode['bar']
        except MutableAccessException:
            pass
        try:
            # Should not work, since it exists,
Пример #40
0
    id = Column(Integer, primary_key=True)
    name = Column(String(255))
    email = Column(String(255))
    url = Column(String(255))

    def __repr__(self):
        return '<Person(name={}, email={}, url={})>'.format(
            self.name, self.email, self.url)


@test_speed
def insert_all(max_records):
    people = [random_person() for n in range(max_records)]
    prnt('Records to create:', people)
    for person in people:
        # Don't need this prop for our example schema.
        del person['address']
        db_session.add(Person(**person))


if DEBUG:
    with Section('MySQL - SQL Alchemy'):
        Base.metadata.create_all(engine)

        print_h2('Adding a bunch of records...')
        run_trials(insert_all, trials=10)

        print_h2('Reading all records...')
        recs = db_session.query(Person).all()
        ppr(recs)
Пример #41
0
if DEBUG:
    with Section('Map Abstract Data Type'):
        map_adt = MapADT(title='map_head')

        for n in range(6):
            key = 'map_item-{}'.format(''.join(randchars(8)))
            map_adt[key] = ''.join(randchars(4))

        print(map_adt)

    with Section('Multi-Map Abstract Data Type'):
        faker = Factory.create()
        multimap = MultiMap(title='multi-map_head')

        print_h2('Multimap', desc='General abstract usage.')

        for n in range(4):
            key = 'map_item-{}'.format(n)
            multimap[key] = ''.join(randchars(4))

        print(multimap)
        print(multimap['map_item-1'])

        print_h2('Multimap', desc='Used contextually for school enrollment.')

        classes_map = Enrollment(
            classes=['English', 'Comp Sci', 'History', 'Math', 'Art'],
            students=[faker.name() for _ in range(3)] + ['Ms. Ella Tabor'])

        map(classes_map.enroll_all, classes_map.schedules())