def init(): def foo(): print('I am the foo func!') def foo2(): print('I am the foo2 func!') def bar(): print('I am the bar func!') foo() def bim(): print('I am the bim func!') foo2() tracer = StackTrace() for name, func in locals().iteritems(): if name is not 'tracer': func() tracer.call(func, name=name) with Section('Call Stack'): tracer.view()
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)
return num else: return fibo(num - 1) + fibo(num - 2) print(fibo(num)) @test_speed def o_log_n(max_amt=1000): foo = max_amt print('I am a O(log n) function') while foo > 0: foo = foo / 2 @test_speed def o_n_factorial(factor=10): # Basically a for loop nested `factor` times. pass if __name__ == '__main__': with Section('BIG O Notation'): o_1() o_n() o_n2() o_n_comprehension() run_trials(o_n3, trials=20) run_trials(o_log_n, trials=10) o_n_factorial() o_2n(10)
self.data = """class {}:\n{}\n""".format( self.entity, '{}# The generated value for {}'.format( ' ' * 4, self.entity)) self.data += ('\n def __init__(self, *args, **kwargs):' '\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 '
print('\n<{}>\n\nToken: {}'.format('=' * 80, token)) evaluation += self.evaluate_single(token, nonterminals) print('\nFinal value: "{}"\n'.format(evaluation)) return evaluation def cp(): return choice(punctuation) def cu(): return choice(ascii_uppercase) if DEBUG: with Section('Grammar parser - basic'): cfg = ContextFreeGrammar() # There are two rules for the same mapping "S"; thus, it's ambiguous. ambig_grammar = [ 'S => <{++>U<>', 'U => {}VV', 'B => \\{//U\\++}', 'V => *&&&*!$' ] ambiguous_cfg = ContextFreeGrammar() map(ambiguous_cfg.add_rule, ambig_grammar) tokens = [choice(['S', 'U', 'B', 'V']) for _ in range(10)] wiki_grammar = [ 'S => UV', 'U => aBc-bBac', 'B => caa$', 'V => ac B bca U' ] letters = ['S', 'U', 'B', 'V']
'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()
raise ValueError('"{}" is an invalid scenario'.format(scenario)) else: success, message = self.actions[scenario] message = '"{}" is {}: {} was "{}"'.format( scenario, 'not invalid' if not success else 'valid', 'error' if not success else 'message', message) print_error(message) if not success else print_success(message) def __setitem__(self, scenario, result): if DEBUG: print(scenario.split(',')) self.actions[scenario] = result if DEBUG: with Section('Decision Tables and Decision Table Testing'): # Example table: "Signup form" dtt = DecisionTable() # Add actions - degenerate case - success dtt['user::Y, email::Y, pass::Y, pass2::Y'] = (True, 'Login attempt') # All other actions - failure dtt['user::Y, email::Y, pass::Y, pass2::N'] = (False, 'Bad pass2') dtt['user::Y, email::Y, pass::N, pass2::N'] = (False, 'Bad pass/pass2') dtt['user::N, email::Y, pass::Y, pass2::Y'] = (False, 'Bad user') dtt['user::N, email::N, pass::Y, pass2::Y'] = (False, 'Bad user/email') dtt['user::N, email::N, pass::N, pass2::Y'] = (False, 'Bad user/email/pass') dtt['user::Y, email::N, pass::N, pass2::N'] = (False, 'Bad email/pass/pass2') dtt['user::N, email::N, pass::N, pass2::N'] = ( False, 'Bad usernmame/email/pass/pass2')
# -*- coding: utf-8 -*- __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.systems_engineering.message_queues.rabbitmq import start import pika import sys DEBUG = True if __name__ == '__main__' else False if DEBUG: with Section('Message Queues - RabbitMQ'): message = ' '.join(sys.argv[1:]) or '-- UNNAMED NEW TASK --' start.chan.basic_publish( exchange=start.EXCHANGE_NAME, routing_key=start.get_routing_key(sys.argv), body=message, properties=pika.BasicProperties(delivery_mode=2)) print('Sent new task: {}'.format(message)) start.connection.close()
def get_index(self): if self.index is None: self.re_index() return self.index def check_similary(self, doc, lsi): vec_bagofwords = self.dictionary.doc2bow(doc) # 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()
but copying it would defeat the purpose.""" dist = 0 last_longer = len(str1) < len(str2) first, last = ( str1, str2, ) if last_longer else ( str2, str1, ) for k, letter in enumerate(str1): try: if str1[k] != str2[k]: dist += 1 except IndexError: continue # Add remainder between offset of each (e.g. "cat", "cats" = range(2, 3)) for k in range(len(first), len(last)): dist += 1 print('Hamming dist for `{}` and `{}` = {}'.format(str1, str2, dist)) return dist if __name__ == '__main__': with Section('Algorithms / coding theory - Hamming distance'): hamming('Sanguine swine', 'Dandelion wine') hamming('Foo', 'Foobar') hamming('0123', '12345') hamming('Grape drank', 'Ape rank') hamming('011011101', '110110101')
logger = None def __init__(self, *args, **kwargs): if 'logger' not in kwargs: self.logger = Messenger() else: self.logger = kwargs.get('logger')() def log(self, message): msg = self.logger.message_action(message) print(msg) return msg if DEBUG: with Section('SOLID - Dependency Inversion Principle'): # It seems that DIP is a concept, and the mechanism to achieve it # is actually Dependency Injection (e.g. Class/Method/Prop injection). app = Application() assert app.log('Foobar') == '[Message]: Foobar' app_email = Application(logger=EMailMessenger) assert app_email.log('Foobar') == '[Email Message]: Foobar' app_sms = Application(logger=SMSMessenger) assert app_sms.log('Foobar') == '[SMS Message]: Foobar' app_log = Application(logger=LoggingMessenger) assert app_log.log('Foobar') == '[Logging Message]: Foobar'
def make(name, instantiate=False): newcls = None if name is 'car' or name is 'automobile': newcls = Automobile elif name is 'plane': newcls = Plane elif name is 'train': newcls = Train if newcls is None: raise ValueError('Invalid class.') if instantiate: return newcls(name) else: return newcls if DEBUG: with Section('GRASP Factory/Creator pattern'): clstypes = [('car', Automobile), ('plane', Plane), ('train', Train)] for clstype in clstypes: _cls, real_class = clstype output = ClassFactory.make(_cls) assert not isinstance(output, real_class) _cls, real_class = clstype output = ClassFactory.make(_cls, instantiate=True) assert isinstance(output, real_class) print('Created new `{}` from {}'.format(_cls, real_class)) print(_cls)
"""Calculate OR gate.""" return 1 if any([pulse1, pulse2]) else 0 def _not(pulse): """Calculate NOT gate.""" return 0 if pulse == 1 else 1 def _nor(pulse1, pulse2): """Calculate NOR gate.""" return 0 if pulse1 or pulse2 else 1 def _xor(pulse1, pulse2): """Calculate XOR gate.""" return 0 if pulse1 == pulse2 else 1 if DEBUG: with Section('Logic gates'): onegate = [('not', _not)] twogate = [('and', _and), ('or', _or), ('nor', _nor), ('xor', _xor)] for name, func in onegate: p1 = choice([0, 1]) print('{0}: {1} = {2}'.format(name.upper(), p1, func(p1))) for name, func in twogate: p1, p2 = choice([0, 1]), choice([0, 1]) print('{0}: {1}, {2} = {3}'.format(name.upper(), p1, p2, func(p1, p2)))
shifted by the given bias. See wikipedia.org/wiki/Excess-3 for more.""" bcd, binary, decimals = '', '', '' for digit in str(num): binval = encoders.dec_to_bin(int(digit)) binval = BaseDataType.add(str(binval), bias) binary += '{}{}'.format(binval, ' ' * (4 - len(binval) + 1)) if len(binval) < 4: binval = binval.zfill(4) bcd += '{} '.format(binval) decimals += digit + (' ' * 4) _show_bcd(num, decimals, binary, bcd) return bcd if DEBUG: with Section('Numerical encoding: Binary Coded Decimal (BCD)'): """More exist, but are not covered here. See books.google.com/books?id=0f-6diYBV0wC&lpg =PA48&ots=jG6NiHY3he&dq=bcd%207421&pg =PA51#v=onepage&q=bcd%207421&f=false For more examples.""" print('D = Decimal, B = Binary, C = Binary Coded Decimal') nums = [ 1, 2, 4, 16, 32, 64, 128, 256, 512, 1024, 2048, 1234, 12345, 123456, 1234567, 12345678, 123456789 ] print_h4('BCD', desc='8421 encoding') for num in nums: dec_to_bcd_8421(num) print_h4('BCD', desc='Excess-3 (bias) encoding')
DEBUG = True if __name__ == '__main__' else False def manhattan_distance(p1, p2): """Compute manhattan distance. Args: p1 (dict): The x and y coordinates of point 1 p2 (dict): The x and y coordinates of point 2 Returns: int: The calculated distance. """ # Equation from https://xlinux.nist.gov/dads//HTML/manhattanDistance.html # it is |x1 - x2| + |y1 - y2|. return abs(p1['x'] - p2['x']) + abs(p1['y'] - p2['y']) def rand_coords(max_width=100, max_height=100): """Generate random coordinates.""" return {'x': randrange(0, max_width), 'y': randrange(0, max_height)} if DEBUG: with Section('Manhattan Distance algorithm'): c1 = [rand_coords() for _ in range(10)] c2 = [rand_coords() for _ in range(10)] for p1, p2 in zip(c1, c2): dist = manhattan_distance(p1, p2) print('Distance for {} and {} = {}'.format(p1, p2, dist))
def add_message(self, message, priority=0): return super(WebLogger, self).add_message( '[WEBLOG] - ' + message, priority=priority) class NetworkLogger(Logger): def add_message(self, message, priority=0): return super(NetworkLogger, self).add_message( '[NETWORK] - ' + message, priority=priority) if DEBUG: STACK_COUNT = 10 with Section('Stacks'): stack = Stack() print('\n') print('First in push') print('\n') for _ in range(STACK_COUNT): stack.push('{} ... [ {} ]'.format(_, gibberish())) print(stack.head()) print('\n') print(stack.size(), stack.view()) print('\n') print('First out pop') print('\n') for _ in range(STACK_COUNT): print(stack.pop())
def frown(self): pass class ColonelMeow(InernetHouseCats): def attack(self): pass """ Class methods example """ if __name__ == '__main__': with Section('OOP Class types / examples'): print('\n') print(Cat.meow()) print(Cat.custom_classmethod()) print(Cat.custom_classmethod) print('\n') """ Examples of actual usage of class instances """ classes = ' <= '.join([c.__name__ for c in inspect.getmro(Species)]) print('Classes for most derived class {} are: {}'.format( Species, classes)) leopard = Cat('leopard')
so we don't need to re-define it here.""" def __init__(self, *args, **kwargs): super(AssociationList, self).__init__(*args, **kwargs) def __getitem__(self, key): """Get an existing node, returning only the value.""" node = self while node is not None: 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']
raise StopIteration 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', '')
if node.balance_factor < 0: # If the current nodes balanace factor is < 0, and its right # child's balance factor is > 0, rotate both sides, right and left. # Otherwise, just rotate the left side, if the right child is # not unbalanced. if node.right_child is not None: if node.right_child.balance_factor > 0: self._rotate_right(node.right_child) self._rotate_left(node) else: self._rotate_left(node) # If the balance factor of this node is > 0, then do the opposite # rotation for both child nodes. elif node.balance_factor > 0: if node.left_child is not None: if node.left_child.balance_factor < 0: self._rotate_left(node.left_child) self._rotate_right(node) else: self._rotate_right(node) print('New BF for node with value {} is {}'.format( node.key, node.balance_factor)) bst.recurse_bst(self.root, None) if __name__ == '__main__': with Section('AVL Trees'): avl = AVLTree() bst.populate_bst(avl, count=5)
# For each sub group, sort on a separate thread. for group in groups: self.sorting_queue.put(group) return self def run(self, items): # Make sure thread number is never greater than the number of items. num_items = len(items) while self.threads > num_items: self.threads -= 1 if num_items < 2: return items # Prevent passing in div by zero errors. if self.threads == 0: self.threads = 1 self._disperse()._enqueue(items) # Block until complete. self.sorting_queue.join() # Perform the second sort on already sorted sublists. return self.sorting_func(self.sorted_items) if __name__ == '__main__': with Section('Threaded Sorts'): threaded_quicksort = ThreadSort(quick_sort, threads=4) rand = random_number_set(max_range=20) res = threaded_quicksort.run(rand) print('Is valid? {}'.format(res == sorted(rand))) ppr(res)
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')
return self.hash_fnv1a(data) def compute_hashes(self, filedata, chunk_size): """Compute a list of hashes for each block of data given by `filedata`. Chunk size will affect performance since each chunk has to be hashed. This is largely dependent on the hashing algorithm used. """ current_offset = 0 while len(filedata) > 0: # Add hashed chunk to the list self.hash_list.append( self.hash(filedata[current_offset:])) current_offset += chunk_size # Continue with substring value filedata = filedata[current_offset:] class MockFile: def __init__(self): self.data = ''.join(map(gibberish3, range(10))) def __str__(self): return self.data if DEBUG: with Section('Hash list'): fakefile = MockFile() hashlist = HashList(fakefile.data, chunk_size=12) print(hashlist)
...In other words, it's another way to represent a graph, very compactly (and thus efficiently), by using a matrix that represents the connections -- by mapping the row and column to a specific node (similar technique as a Cayley or multiplication table). A B C D A 0 1 1 0 might correspond to: B 1 0 1 1 (row A, col A) = 0, C 1 1 0 0 (row B, col A) = 1, etc... D 1 0 0 0 generally, self referencing nodes are not represented, but if they are, their value is 2. """ if __name__ == '__main__': with Section('Adjacency Matrix'): amatrix = AdjacencyMatrix() amatrix['A'] = ['B'] amatrix['B'] = ['A'] print(amatrix) amatrix['B'] = ['A', 'C'] amatrix['C'] = ['B', 'A'] print(amatrix) amatrix['D'] = ['C', 'B', 'A'] print(amatrix) amatrix['E'] = ['D', 'C', 'B', 'A'] amatrix['F'] = ['A', 'B', 'C', 'D', 'E'] amatrix['E'] = ['A', 'B', 'C', 'D', 'F'] # Add more connections
DEBUG = True if __name__ == '__main__' else False def skew_to_dec(dec): res = 0 if dec == 0 or dec == 1: return dec for n, digit in enumerate(reversed(list(str(dec)))): n = n + 1 out = int(digit) * (2**n - 1) res += out return int(res) if DEBUG: with Section('Skew Binary system'): # Test numbers from https://uva.onlinejudge.org/external/5/575.html test_nums = [ (10120, 44), (200000000000000000000000000000, 2147483646), (10, 3), (1000000000000000000000000000000, 2147483647), (11, 4), (100, 7), (11111000001110000101101102000, 1041110737), (0, 0), ] for test in test_nums: dec, skewed = test print(skew_to_dec(dec))
def f(a): return a**a def g(b): return b * 2 def f_g(a, b, arg): return a(b(arg)) if DEBUG: with Section('Category Theory basics'): """Challenge ideas taken from bartoszmilewski.com/2014/11/04 /category-the-essence-of-composition/ "Challenges" section.""" print_h4('Identity function') for thing in ['cat', 1, 'dog', 2, range(0, 3), 0.03]: assert thing == id(thing) print(thing, id(thing)) print_h4('Function composition') res = compose_hybrid('composition is neat!', f=dictify, g=listify) print(res) print_h4('Random funcs') print(listify('foo')) print(dictify('foo')) print_h4('Higher order function composition') f2 = compose_hybrid_hof(f=listify, g=dictify)
super(AlephOne, self).__init__(1) class AlephOmega(Aleph): def __init__(self): super(AlephOmega, self).__init__('omega') class BethOne(Aleph): # For the curious... # http://en.wikipedia.org/wiki/Beth_number pass if __name__ == '__main__': with Section('Set theory'): set_series = Set([1, 2, 3, 4]) print(set_series) print('is series? {}'.format(set_series.is_series().next())) # True print('\n') set_nonseries = Set([1, 22, 103, 4]) for _ in range(50): set_nonseries.add(rr(1, 9999)) print(set_nonseries) print('\n') print('is series? {}'.format( set_nonseries.is_series().next())) # False with Section('Set theory - cardinality of the continuum examples'): all_squares = AllSquares() # Limit the count, without limiting
class ColoredSquare(Square): pass class Rectangle(Square): def set_height(self, h): pass def set_width(self, w): pass if DEBUG: with Section('SOLID - Liskov Substitution Principle'): # We can use the original structural ABC to access its subclasshook, # but it won't be structurally the same as our new inherited # classes, which is fine -- we just want to use the utility of # structural checking for all our own subclasses here. sqr = Square() # Expected to not be instance, since we modified it from the original. assert not isinstance(sqr, StructuralType) green_sqr = ColoredSquare() rect = Rectangle() green_sqr['color'] = 'green' rect['width'] = 100 rect['height'] = 100 sqr['size'] = 120
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)
rand_vec3 = random.rand(vector_length) print('\nStarting seed vector: {}'.format(rand_vec3)) for _ in xrange(steps): vec3, expected = choice(training_data) # Get the dot product of the randomized vector and the training vector result = dot(rand_vec3, vec3) # Get the offset of the expected and the unit step value offset = expected - unit_step(result) errors.append(offset) # Update the starting vector rand_vec3 += bias * offset * vec3 # Run it for visualization of the progress for vec3, expected in training_data: result = dot(vec3, rand_vec3) print("{}: {} = {} (expected {})".format(vec3[:2], result, unit_step(result), expected)) if DEBUG: with Section('Perceptron'): # Depending on the number of `steps` set, this may not return the right # answer each time. Since the starting vector is random, if the step # count is too low, it may be entirely wrong. You can play around with # this and see where things go. run_perceptron(training_data_or) run_perceptron(training_data_and) # This one trains much faster, as the number of cases is halved. run_perceptron(training_data_not)