def evaluate(self, tokens, evaluation=''): """A basic parser for a custom attribute grammar. One thing to note is that ambiguous grammars need to be iterated over, since the duplicate rules can't be mapped via dictionary key. Unambiguous grammars are therefore more performant, because the lookup is O(1) vs. O(N). """ if self.DEBUG: prnt('Ruleset', self.rules) print('\nEvaluating tokens: {}'.format(tokens)) for token in tokens: for rule in self.rules: # Expressions are already parsed when adding: (L => R = [L, R]) left, right = rule print('Current rule = {} => {}'.format(left, right)) if token == left: is_attr = self.attr_token in right is_terminal = right.endswith(self.terminus) if is_attr and is_terminal: print('\nEvaluating terminal: `{}`'.format(token)) evaluation += self._parse_terminal( self._attr(right), token) else: non_terms = list(right) print('\nEvaluating non-terminal {}'.format(non_terms)) evaluation += self.evaluate(non_terms, evaluation=evaluation) return evaluation
def evaluate(self, tokens=None, evaluation=''): """A basic parser for a custom attribute grammar. One thing to note is that ambiguous grammars need to be iterated over, since the duplicate rules can't be mapped via dictionary key. Unambiguous grammars are therefore more performant, because the lookup is O(1) vs. O(N). """ if tokens is None: if hasattr(self, 'tokens'): tokens = self.tokens else: raise ContextFreeGrammar.InvalidTokenSet expressions = [r[0] for r in self.rules] tokens = [r[1] for r in self.rules] groups = [[expressions[k], tokens[k].split(' ')] for k, _ in enumerate(tokens)] prnt('Groups', groups) evaluation = self._evaluate(groups, evaluation='') new_tokens = list(evaluation) for token in new_tokens: for expression in expressions: if token in list(expression): token = self._evaluate(groups, evaluation=evaluation) if ContextSensitiveGrammar.DEBUG: print('Final evaluation in `evaluate`: {}'.format( evaluation, ''.join(new_tokens))) return evaluation
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))
def evaluate(self, tokens, evaluation=''): """A basic parser for a custom attribute grammar. One thing to note is that ambiguous grammars need to be iterated over, since the duplicate rules can't be mapped via dictionary key. Unambiguous grammars are therefore more performant, because the lookup is O(1) vs. O(N). """ if self.DEBUG: prnt('Ruleset', self.rules) print('\nEvaluating tokens: {}'.format(tokens)) for token in tokens: for rule in self.rules: # Expressions are already parsed when adding: (L => R = [L, R]) left, right = rule print('Current rule = {} => {}'.format(left, right)) if token == left: is_attr = self.attr_token in right is_terminal = right.endswith(self.terminus) if is_attr and is_terminal: print('\nEvaluating terminal: `{}`'.format(token)) evaluation += self._parse_terminal( self._attr(right), token) else: non_terms = list(right) print('\nEvaluating non-terminal {}'.format(non_terms)) evaluation += self.evaluate( non_terms, evaluation=evaluation) return evaluation
def evaluate(self, tokens=None, evaluation=''): """A basic parser for a custom attribute grammar. One thing to note is that ambiguous grammars need to be iterated over, since the duplicate rules can't be mapped via dictionary key. Unambiguous grammars are therefore more performant, because the lookup is O(1) vs. O(N). """ if tokens is None: if hasattr(self, 'tokens'): tokens = self.tokens else: raise ContextFreeGrammar.InvalidTokenSet expressions = [r[0] for r in self.rules] tokens = [r[1] for r in self.rules] groups = [[ expressions[k], tokens[k].split(' ')] for k, _ in enumerate(tokens) ] prnt('Groups', groups) evaluation = self._evaluate(groups, evaluation='') new_tokens = list(evaluation) for token in new_tokens: for expression in expressions: if token in list(expression): token = self._evaluate(groups, evaluation=evaluation) if ContextSensitiveGrammar.DEBUG: print('Final evaluation in `evaluate`: {}'.format( evaluation, ''.join(new_tokens))) return evaluation
def _view(self, suffixes): prnt(suffixes[0][:-1], '') for k, substr in enumerate(suffixes): max_width = (len(suffixes[:-1]) + 30) spaces = '~' * max_width print(spaces) print(' {}{} {}... k={}'.format( substr, ' ' * (max_width - len(substr) - 12), len(substr), k))
def readfile(filename): """Read a file, without loading into memory.""" with open(filename, 'r+') as tempfile: while True: data = tempfile.read(1024) if not data: raise StopIteration prnt('Read line ================ ', data) yield data
def encode(self, seq=None): """Encode the objects' sequence data as a normal graph representative dictionary.""" super(CartesianTree, self).__init__() prnt('Encoding with new sequence...', seq) self.sequence = self.subdivide( seq if seq is not None else self.sequence) if DEBUG: print(cartesian_tree)
def traverse_word(self): prnt('Graph nodes beginning', self.graph.vertices) start, end = 0, len(self.word) while start < end: # Print must come before next step, # otterwise it will print the letters shifted. print('Counter: {}, Output: {}, Curr val: {}, Curr key: {}'.format( start, self.output, self.curr_val, self.curr_key)) self._run_step() start += 1
def _run_step(self): if self.DEBUG: prnt('curr node (before):', self.curr_node) time.sleep(self.DELAY) # Visually update if self.DEBUG: prnt('curr node (after):', self.curr_node) self._update_state() if self.curr_node is not None: # Add to the string as the program runs. self.output += self.curr_val
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))
def print_nodes(node): def _fmt(node): return '[<{}.{}>] {divider} '.format( node.title, node.cargo, divider='....' if node.next is not None else '') \ if node is not None else '' path, count = '', 0 cmd_title('Printing Nodes') # Follows a node along it's next target, until next is None. while node is not None: path += _fmt(node) count += 1 node = node.next prnt('Linked List\n', path, newlines=False)
def simple_rule(rule, string=''): _rule = list(rule) _string = list(string) if ContextSensitiveGrammar.DEBUG: print('simple rule: {} and string: {}'.format(_rule, _string)) # We only replace tokens that match a rule. # The rest remain unchanged. for k, char in enumerate(string): if char in _rule: _string[k] = ''.join(string).replace(' ', '') # Replace the token with the rules' string _string[k] = ContextSensitiveGrammar.simple_rule( rule, string=_string) ret = ''.join(_string) if ContextSensitiveGrammar.DEBUG: prnt('simple rule retval: ', ret) return ret
def cfg2(): prnt('Ambiguous CFG result', ambiguous_cfg.evaluate(tokens=tokens))
DEBUG = True if __name__ == '__main__' else False faker = Factory.create() redis_conn = Redis(host='localhost', port=6379, db=0) pipe = redis_conn.pipeline() testing = {} def insert_name(*args, **kwargs): key = faker.name() val = faker.email() testing[key] = val pipe.set(key, val) pipe.get(key) @test_speed def insert_all(max_records): for n in range(max_records): insert_name() if DEBUG: with Section('Redis (via Redis-py)'): run_trials(insert_all, trials=10) print_success('This is **too** easy!') # Use single pipeline for speed. res = pipe.execute() prnt('Result from Redis store pipeline execution: ', res)
'created': dt.now() } @test_speed def insert_all(max_records): global _count global _testdata for n in range(max_records): _count += 1 data = make_person() _testdata.append(data['name']) collection.insert(data) if DEBUG: with Section('MongoDB (via pymongo)'): # Clean up stale collection beforehand, if it exists. collection.remove() print(collection.count()) run_trials(insert_all, trials=10) res = collection.find({}) # Assert all names are correct mongo_res = [] for k, item in enumerate(res): mongo_res.append(item['name']) prnt('Result from MongoDB execution, names only: ', mongo_res) # Check database count vs. local increment assert sorted(mongo_res) == sorted(_testdata) assert _count == collection.count()
if length < segments: length = segments return [[_ for _ in combochars(n, segments)] for n in range(2, length)] def cartesian(max_chars, max_nums, unique=False): res = [''.join(prod[0]) for prod in product( combochars(max_chars, 2), range(0, max_nums))] if unique: return list(set(res)) else: return res if __name__ == '__main__': with Section('Combinatorics'): prnt('Unique chars', uniqchars(10)) fact = factorial(12) prnt('Final factorial amount:', fact[0]) ppr(fact[1]) combos = combochars(4, 2) prnt('Permutations of random letters', ', '.join( [''.join(combo) for combo in combos])) prnt( 'Combinations of multiple permutations of random letters', group_combochars(6, segments=2)) prnt('Cartesian product of two sets', cartesian(4, 4)) prnt( 'Cartesian product of two sets (unique)', cartesian(4, 4, unique=True))
if DEBUG: with Section('PostgreSQL (via psycopg2)'): # Starting postgresql on mac: # `postgres -D /usr/local/var/postgres/data` try: conn = psycopg2.connect( dbname='ctabor', user='******', host='localhost', password='') print_success('Successfully connected!') except: print_error('Could not connect to PostgreSql :(') raise Exception cur = conn.cursor() # Always clean up DB for this demo. cur.execute("""DROP TABLE people""") cur.execute("""CREATE TABLE people(id serial PRIMARY KEY, name varchar, email varchar, address varchar)""") print_h2('Adding a bunch of records...') run_trials(insert_all, trials=10) conn.commit() print_h2('Reading all records...') cur.execute('SELECT * FROM people;') records = cur.fetchall() prnt('SQL Records', records) # Close up shop to prevent zombie processes, etc. cur.close()
def test_number(*args): func, args = args[0], args[1:] try: prnt(func.func_name, [_ for _ in func(*args)]) except TypeError: prnt(func.func_name, func(*args))
self.__str__() if __name__ == "__main__": with Section("Container Abstract Data Type"): val_cont = ContainerADT() for n in range(4): val_cont[n * n] = randchars(n) val_cont.insert(n, randchars(n)) print(val_cont.size()) print(val_cont.new("fooo")) for el in val_cont: print("Iterator: {}, membership check ({}) => {}".format(val_cont[el], el, el in val_cont)) prnt("Value Container", val_cont.elements) print(val_cont.clear()) prnt("Empty Container", val_cont.elements) assert len(val_cont) == 0 ref_cont = ReferenceContainer() prnt("Reference Container...", ref_cont) meta = ReferenceContainer() ref_cont.insert("meta", meta) ppr(ref_cont.elements) # Change original instance meta["foo"] = "bar"
def sort(self): for k, suffix_group in enumerate(self.strings): for j, suffixes in enumerate(suffix_group): self.strings[k][j] = sorted(self.strings[k][j]) if __name__ == '__main__': with Section('Suffix Array'): DEBUG = False word_gen = words_unix_dict() iarray = InfixArray() iarray.make_superstring(strings=[word_gen.next() for _ in range(2)]) prnt('Weird infix style substring', iarray) ssarray = SuperSuffixArray() ssarray.make_superstring(strings=[word_gen.next() for _ in range(10)]) prnt('Superstring suffix array', ssarray, func=repr) ssarray.sort() prnt('SORTED Superstring suffix array', ssarray, func=repr) if DEBUG: ssarray2 = SuperSuffixArray() ssarray2.make_superstring( strings=['pneumonoultramicroscopicsilicovolcanoconiosis'])
DEBUG = True if __name__ == '__main__' else False faker = Factory.create() es = Elasticsearch() def get_name(): return { 'name': faker.name(), 'email': faker.email(), 'address': faker.address(), 'timestamp': dt.now(), } @test_speed def insert_all(max_records): for n in range(max_records): res = es.index(index='testing_index', doc_type='test', id=n, body=get_name()) print(res) if DEBUG: with Section('ElasticSearch (via ElasticSearch-py)'): insert_all(10) res = es.get(index='testing_index', doc_type='test', id=1) prnt('ES Results:', res)
def add_program(self, *args): name, program = args if self.DEBUG: prnt('Adding program', program) self.programs[name] = program
res['heads'][1] = '~{}%'.format(res['heads'][0] // 10.0) res['tails'][1] = '~{}%'.format(res['tails'][0] // 10.0) print(res) return res def _test(*args): values = [rr(1, 999) for d in range(10)] ppr(get_results(values)) if __name__ == '__main__': with Section('Probability'): values_rand = [rr(1, 999) for d in range(rr(1, 20))] prnt( ('Probability of a given value:', values_rand), Probability.of_single(values_rand)) run_trials(_test) prnt( ('Probability of multiple given values:', values_rand, values_rand[:2]), Probability.of_group(values_rand, values_rand[:2], are_equal=True)) prnt( ('Probability of multiple given values:', values_rand, values_rand[:4]), Probability.of_group(values_rand, values_rand[:4], are_equal=True)) run_trials(test_coin_flip)
print('{} ==> {}'.format(self.program, code)) self.code = code def decode(self): """Re-assemble the string sequence into a real data structure -- not part of the typical UTM behavior, but signifies the need to decode the values into something usable.""" print('Decoding active program') code = { int(n.split(';')[0][1]): { 'value': int(n.split(';')[1][1]), 'next': int(n.split(';')[2][1]) } for n in self.code.split(':')} print(code) if __name__ == '__main__': with Section('Universal Turing Machine'): program = DummyProgramGenerator.make(max_states=5) prnt('UTM program', program) utm = UniversalTuringMachine(program=program) # Add some seed "programs" for n in range(4): utm.add_program( 'program-{}'.format(n), DummyProgramGenerator.make(max_states=4)) for name, _ in utm.programs.iteritems(): utm.run(name)
Optional parameters can be added to the dictionary, but of course they are not referenced here.""" graph = UndirectedGraph({ 0: {'edges': [1, 2], 'val': 'A'}, 1: {'edges': [2, 0], 'val': 'B'}, 2: {'edges': [0], 'val': 'C'}, }) assert len(graph.walk(0, 3)) == 0 # non-existent node assert len(graph.walk(2, 1)) == 3 # unreachable node assert graph.is_closed(1, 2) # Cycle assert graph.is_closed(0, 0) # Trivial cycle assert graph.is_closed(2, 1) # Cycle graph[3] = {'edges': [], 'val': 'D'} graph.all_vertices() prnt('Generated graph', graph.vertices) deg, vertex = randrange(0, MAX_EDGES), choice(all_vertices) print('Has degree {} ... {}? {}'.format( deg, vertex, graph.has_degree(deg, vertex))) for vertex in graph: print('Vertex {} has degree {}'.format( vertex, graph.degree(vertex))) prnt('graph', graph) prnt('Has multiple degrees?', graph.has_multiple_degrees(1)) with Section('Directed Graph'): digraph = DirectedGraph() digraph.DEBUG = True for n in range(MAX_VERTICES): digraph[n] = {'edges': _rand_edges(MAX_EDGES), 'val': n}
The graph is keyed by the node name, which can be any valid dictionary key format (e.g. string, number, list, etc...) Optional parameters can be added to the dictionary, but of course they are not referenced here.""" graph = UndirectedGraph( {0: {"edges": [1, 2], "val": "A"}, 1: {"edges": [2, 0], "val": "B"}, 2: {"edges": [0], "val": "C"}} ) assert len(graph.walk(0, 3)) == 0 # non-existent node assert len(graph.walk(2, 1)) == 3 # unreachable node assert graph.is_closed(1, 2) # Cycle assert graph.is_closed(0, 0) # Trivial cycle assert graph.is_closed(2, 1) # Cycle graph[3] = {"edges": [], "val": "D"} graph.all_vertices() prnt("Generated graph", graph.vertices) deg, vertex = randrange(0, MAX_EDGES), choice(all_vertices) print("Has degree {} ... {}? {}".format(deg, vertex, graph.has_degree(deg, vertex))) for vertex in graph: print("Vertex {} has degree {}".format(vertex, graph.degree(vertex))) prnt("graph", graph) prnt("Has multiple degrees?", graph.has_multiple_degrees(1)) with Section("Directed Graph"): digraph = DirectedGraph() digraph.DEBUG = True for n in range(MAX_VERTICES): digraph[n] = {"edges": _rand_edges(MAX_EDGES), "val": n} prnt("Generated directed-graph", digraph.vertices) prnt("Digraph", digraph)
__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)
2 | integer ---------------------------- """ def format_table(code_string): s_table = symtable(code_string, 'string', 'exec') table = [] for child in s_table.get_children(): row = { 'name': child.get_name(), 'scope': 'global', 'children': [subchild.get_name() for subchild in child.get_children()] } table.append(row) return s_table, table if DEBUG: with Section('Data structure - symbol table'): """Messing around with built-in symbol table parser.""" prnt('Some example code...', test_func) symbols, formatted = format_table(test_func) assert len(symbols.lookup('Maths').get_namespaces()) == 1 assert len(symbols.lookup('Words').get_namespaces()) == 1 print_simple('Output using stdlib symtable module:', [symbols, formatted]) prnt('Which might be translated to...', example_output)
def recalculate(self): """Draws/redraws the graph, taking the current value and assigning the nodes / paths accordingly. Assigns the edge values for each node, and updates the value for each symbol in the word as well. In the example {0, 1} symbol set machine, each node can have a 0, 1, or both 0 and 1 edges, which allow for the instructions to be dictated by traversing the graph. In a traditional directed graph, nodes are either inbound, or outbound. In the SMM, the edges define the direction, but they also encode the state symbols that can be used to determine behavior in a way that operates like tape, cells, and state in a Turing machine. An example graph looks like this: graph = { 0: ..., 1: {'val': 'A', 'edges': {0: {'to': 0}, 1: {'to': 2},}, 2: .... } The key is the node, and is used as a pointer to other nodes. The edges attribute is a dictionary, keyed by each edge that corresponds to all symbol states (e.g. {0, 1} in the example). Each key points to a dictionary, representing the node to move to. """ end = len(self.word) # Special case for single nodes. if end == 1: self.graph[0] = { 'key': 0, 'val': self.word, # guaranteed to only be one character 'edges': { 0: { 'to': 0 }, } } else: for key, val in enumerate(self.word): self.graph[key] = {'key': key, 'val': val, 'edges': {}} # The first nodes 0-edge points to itself, # and the 1-edge points to the next node. if key == 0: self.graph[key]['edges'] = { 0: { 'to': 0 }, 1: { 'to': key + 1 } } # The last nodes 0 and 1 edges # BOTH point to the previous node. elif key == end - 1: self.graph[key]['edges'] = { 0: { 'to': key - 1 }, 1: { 'to': key - 1 } } # All others; 0-edge points to the previous, # 1-edge points to the next. else: self.graph[key]['edges'] = { 0: { 'to': key - 1 }, 1: { 'to': key + 1 } } if self.DEBUG: prnt('Graph', self.graph.vertices)
def recalculate(self): """Draws/redraws the graph, taking the current value and assigning the nodes / paths accordingly. Assigns the edge values for each node, and updates the value for each symbol in the word as well. In the example {0, 1} symbol set machine, each node can have a 0, 1, or both 0 and 1 edges, which allow for the instructions to be dictated by traversing the graph. In a traditional directed graph, nodes are either inbound, or outbound. In the SMM, the edges define the direction, but they also encode the state symbols that can be used to determine behavior in a way that operates like tape, cells, and state in a Turing machine. An example graph looks like this: graph = { 0: ..., 1: {'val': 'A', 'edges': {0: {'to': 0}, 1: {'to': 2},}, 2: .... } The key is the node, and is used as a pointer to other nodes. The edges attribute is a dictionary, keyed by each edge that corresponds to all symbol states (e.g. {0, 1} in the example). Each key points to a dictionary, representing the node to move to. """ end = len(self.word) # Special case for single nodes. if end == 1: self.graph[0] = { 'key': 0, 'val': self.word, # guaranteed to only be one character 'edges': {0: {'to': 0}, } } else: for key, val in enumerate(self.word): self.graph[key] = { 'key': key, 'val': val, 'edges': {} } # The first nodes 0-edge points to itself, # and the 1-edge points to the next node. if key == 0: self.graph[key]['edges'] = { 0: {'to': 0}, 1: {'to': key + 1}} # The last nodes 0 and 1 edges # BOTH point to the previous node. elif key == end - 1: self.graph[key]['edges'] = { 0: {'to': key - 1}, 1: {'to': key - 1}} # All others; 0-edge points to the previous, # 1-edge points to the next. else: self.graph[key]['edges'] = { 0: {'to': key - 1}, 1: {'to': key + 1}} if self.DEBUG: prnt('Graph', self.graph.vertices)
test_number(lg, 10) [test_number(lg_custom, 10, n) for n in range(10)] test_number(square, 10) test_number(fibo, 10) test_number(cube, 10) test_number(rand, 10) test_number(triangle, 10) [test_number(gcd, rr(999, 9999), rr(999, 9999)) for _ in range(4)] funcs = [ lambda x: x**2, lambda x: x * 2, lambda x: x // 2, lambda x: x * x - x // x + x ] [ prnt('Series of series with: {}'.format(getsource(f)), [map(f, (n for n in range(1, _))) for _ in fibo(8)]) for f in funcs ] [ prnt('Factor {}'.format(n), [f for f in factor(rr(100, 9999))][0]) for n in range(4) ] prnt('Factorial', [f for f in factorials(10)]) [f for f in factor_factorials(10)] # Misc. exercises test_number(exercise_1_2, 20)
print('{} ==> {}'.format(self.program, code)) self.code = code def decode(self): """Re-assemble the string sequence into a real data structure -- not part of the typical UTM behavior, but signifies the need to decode the values into something usable.""" print('Decoding active program') code = { int(n.split(';')[0][1]): { 'value': int(n.split(';')[1][1]), 'next': int(n.split(';')[2][1]) } for n in self.code.split(':') } print(code) if __name__ == '__main__': with Section('Universal Turing Machine'): program = DummyProgramGenerator.make(max_states=5) prnt('UTM program', program) utm = UniversalTuringMachine(program=program) # Add some seed "programs" for n in range(4): utm.add_program('program-{}'.format(n), DummyProgramGenerator.make(max_states=4)) for name, _ in utm.programs.iteritems(): utm.run(name)
if node.title == key: return node node = node.next return None if DEBUG: with Section('Arrays & Linked Lists'): MAX_NODES = 10 single = SingleNode('head') single['next'] = SingleNode('next-1') print(single) linked_list = build_list(MAX_NODES) print_nodes(linked_list) prnt('Length of linked list:', len(linked_list), newlines=False) prnt('Testing iteration', '', newlines=False) for node in linked_list: print(node) del linked_list['node-1'] del linked_list['node-4'] del linked_list['node-2'] del linked_list['node-6'] del linked_list['tail'] assert not linked_list['tail'] prnt('Testing iteration - post delete:', '', newlines=False)
def matrix_fill(w, h, fill=0): """Generates a two-dimensional matrix of W x H, filled with zeroes or a custom `fill` value. e.g. [[0, 0, 0], [0, 0, 0], [0, 0, 0]] """ return [[fill for _ in range(w)] for _ in range(h)] if __name__ == '__main__': with Section('Testing abstract data type extensions'): # Testing il = intlist([2, 3, 4]) prnt('ADT - int', (il, il.mul(100), il.add(10).div(4))) strs = strlist(['a', 'b', 'c']) prnt('ADT - str up, copy, add', ( strs, strs.up(), strs.copy(2).add('!!'))) strs2 = strlist(['cat', 'dog', 'monkey']) prnt('ADT - str up, shuffle, copy, reverse', ( strs2, strs2.up(), strs2.copy(3).reverse().shuffle())) prnt('ADT - str add, copy, up, add, shuffle', ( strs2, strs2.add(' ').copy(3).up().add(':D').shuffle())) print('Testing strictness')
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)
'url': faker.url(), 'created': dt.now() } @test_speed def insert_all(max_records): global _count global _testdata for n in range(max_records): _count += 1 data = make_person() _testdata.append(data['name']) collection.insert(data) if DEBUG: with Section('MongoDB (via pymongo)'): # Clean up stale collection beforehand, if it exists. collection.remove() print(collection.count()) run_trials(insert_all, trials=10) res = collection.find({}) # Assert all names are correct mongo_res = [] for k, item in enumerate(res): mongo_res.append(item['name']) prnt('Result from MongoDB execution, names only: ', mongo_res) # Check database count vs. local increment assert sorted(mongo_res) == sorted(_testdata) assert _count == collection.count()
def reals(max_nums): def _range(generator): for n in generator: print(n) yield (_range(wholes(max_nums)), _range(naturals(max_nums)), _range(integers(max_nums)), _range(rationals(max_nums)), _range(irrationals(max_nums))) yield '\n' if __name__ == '__main__': with Section('Basic number sets'): MAX_NUMS_PER_EXAMPLE = 10 prnt('Naturals', '') for n in naturals(MAX_NUMS_PER_EXAMPLE): print(n) prnt('Integers', '') for n in integers(MAX_NUMS_PER_EXAMPLE): print(n) prnt('Rationals', '') for n in rationals(MAX_NUMS_PER_EXAMPLE): print(n) prnt('Irrationals', '') for n in irrationals(MAX_NUMS_PER_EXAMPLE): print(n)
def cfg1(): prnt('CFG result', '') cfg.evaluate(tokens=['B', 'B', 'B', 'V', 'U']) cfg.evaluate(tokens=['U', 'C', 'B', 'V', 'U', 'S']) cfg.evaluate(tokens=[choice(letters) for _ in range(10)])
2: {'edges': [5], 'val': 'i am greater than 1!', 'parent': 0}, 3: {'edges': [6, 7], 'parent': 1}, 4: {'edges': [], 'parent': 1}, 5: {'edges': [8, 9], 'parent': 2}, 6: {'edges': [10], 'parent': 3}, 7: {'edges': [], 'val': 'lucky number 7', 'parent': 3}, 8: {'edges': [], 'parent': 5}, 9: {'edges': [], 'parent': 5}, 10: {'edges': [11, 12], 'parent': 6}, 11: {'edges': [], 'parent': 10}, 12: {'edges': [], 'parent': 10}, } tree = Tree(graph) tree[9] = {'edges': [], 'parent': 5} prnt('Tree, subclassed from graph', tree) tree.render_tree('tree-example.png') divider(newline=False) for n in range(len(graph)): print('Testing get: {} ... {}'.format(n, graph[n])) cmd_title('Testing: children_count', newlines=False) assert tree.children_count(1) == 2 assert tree.children_count(2) == 1 assert tree.children_count(3) == 2 assert tree.children_count(9) == 0 # Correctness testing cmd_title('Testing: get_root', newlines=False)
print(kwarg) return args, kwargs return inner_func(*args, **kwargs) def nums_and_squares(max_nums): return range(0, max_nums), [n * n for n in range(max_nums)] def div_exp(*nums): return [n ** n // n for n in nums] if __name__ == '__main__': with Section('Python - unpacking'): prnt('Arg/kwarg unpacking', '') f = func(1, 2, 3, cats=True, dogs=True) prnt('Args/kwargs passing', f) orig, squares = nums_and_squares(10) prnt('Return val (tuple) unpacking', '{}, {}'.format(orig, squares)) mysandwich = { 'sandwich': 'Ham & Cheese', 'slices_cheese': 2, 'slices_ham': 3 } sand_type, cheeses, hams = mysandwich prnt('Unpacking keys (default)', '{}, {}, {}'.format( sand_type, cheeses, hams))
def insert_all(max_records): peeps = [make_person() for n in range(max_records)] prnt('Records to create', peeps) cur.executemany("""INSERT INTO people(name, email, address) VALUES (%(name)s, %(email)s, %(address)s)""", peeps)
is_attr = self.attr_token in right is_terminal = right.endswith(self.terminus) if is_attr and is_terminal: print('\nEvaluating terminal: `{}`'.format(token)) evaluation += self._parse_terminal( self._attr(right), token) else: non_terms = list(right) print('\nEvaluating non-terminal {}'.format(non_terms)) evaluation += self.evaluate( non_terms, evaluation=evaluation) return evaluation if __name__ == '__main__': with Section('Grammar parser - basic'): attribute_cfg = AttributeContextFreeGrammar() rule_templates = [ 'COMB => ST', 'S => S.value $', 'T => T.prop $', 'X => X.len $', 'Z => Z.chars $', ] map(attribute_cfg.add_rule, rule_templates) prnt( 'Attribute CFG evaluation result', attribute_cfg.evaluate(['COMB', 'COMB', 'Z', 'X', 'S', 'T']))
with Section('PostgreSQL (via psycopg2)'): # Starting postgresql on mac: # `postgres -D /usr/local/var/postgres/data` try: conn = psycopg2.connect(dbname='ctabor', user='******', host='localhost', password='') print_success('Successfully connected!') except: print_error('Could not connect to PostgreSql :(') raise Exception cur = conn.cursor() # Always clean up DB for this demo. cur.execute("""DROP TABLE people""") cur.execute("""CREATE TABLE people(id serial PRIMARY KEY, name varchar, email varchar, address varchar)""") print_h2('Adding a bunch of records...') run_trials(insert_all, trials=10) conn.commit() print_h2('Reading all records...') cur.execute('SELECT * FROM people;') records = cur.fetchall() prnt('SQL Records', records) # Close up shop to prevent zombie processes, etc. cur.close()
def insert_all(max_records): peeps = [make_person() for n in range(max_records)] prnt('Records to create', peeps) cur.executemany( """INSERT INTO people(name, email, address) VALUES (%(name)s, %(email)s, %(address)s)""", peeps)
See programmerspatch.blogspot.com.au /2013/02/ukkonens-suffix-tree-algorithm.html for more information.""" def __init__(self): raise NotImplementedError if __name__ == '__main__': with Section('Suffix Tree'): words = ['peanut', 'butter', 'banana', 'hotdog'] shallow = ShallowSuffixTree(words[0]) deep = DeepSuffixTree(words[0]) forest = GeneralizedSuffixTree(words) prnt('Shallow suffix tree', shallow) prnt('Deep suffix tree', deep) prnt('Generalized suffix tree aka "suffix forest"', forest) DEBUG = True if DEBUG: for _ in range(100): keys = range(0, len(words[0])) def _rc(choices): return choice(choices) try: subset = deep[_rc(keys)][_rc(keys)] if len(subset) > 1 and isinstance(subset, list): prnt('Random choice...', subset) except IndexError: