# -*- 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.text import gibberish from MOAL.helpers.trials import test_speed from MOAL.helpers.trials import run_trials from MOAL.helpers.display import Section dict_test = {gibberish(length=10): x for x in range(1000)} dict_test['foo'] = 'bar' @test_speed def o_1(): print('I am a O(1) function') print(dict_test['foo']) @test_speed def o_n(): print('I am a O(n) function') for x in range(10, 1000): print(x * x)
def __init__(self, title, cargo=None, next=None): self.DEBUG = True self.title = title self.next = next self.cargo = cargo or gibberish(length=5)
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()) print('\n') print('{} {}'.format(stack.size(), stack.view())) with Section('Loggers'): logger = Logger()
def make(self): return gibberish()
def fill_to(self, x): for _ in range(x): self.__setitem__(gibberish(length=3), gibberish(length=6))
class PrinterQueue(Queue): def add_job(self, name, doc): self.push({'name': name, 'doc': doc}) print('Adding {} to queue for printing...'.format(name)) def print_job(self): print('Printing... {}'.format(self.head()['name'])) if __name__ == '__main__': with Section('Queues'): q = Queue() for _ in range(5): print('en-queuing new item...') q.enqueue(gibberish()) with Section('Double ended queue'): dq = Dequeue('backwards') for _ in range(5): print('en-queuing (dequeue) new item...') dq.enqueue(gibberish()) with Section('Queue rotation example'): hps = HotPotatoSimulator( ['Tuvok', 'Neelix', 'Kim', 'Paris', 'Seven', 'Chakotay'], 20) for _ in range(7): hps.move() with Section('Printer queue example'):
def populate_bst(bst, count=1): for _ in range(count): bst.put(randrange(1, 100), gibberish())
self.head.next.append(None) def insert(self, elem): # Create a new node with random height node = SkipNode(height=self.random_height(), elem=elem) print('Adding {} node to the list:'.format(elem)) # Balance length before inserting to avoid issues with indices. self._balance_length(node) updated = self.update_list(elem) # Add the new element if it doesn't already exist. if self.find(elem, updated=updated) is None: for i, _ in enumerate(node.next): node.next[i] = updated[i].next[i] updated[i].next[i] = node if __name__ == '__main__': with Section('Skip Lists'): gibs = [{ 'name': gibberish(), 'value': rr(10, 1000) } for _ in range(10)] sl = SkipList() for gib in gibs: sl.insert(elem=gib) for gib in gibs: sl.find(gib) sl.view()
def insert(self, elem): # Create a new node with random height node = SkipNode(height=self.random_height(), elem=elem) print('Adding {} node to the list:'.format(elem)) # Balance length before inserting to avoid issues with indices. self._balance_length(node) updated = self.update_list(elem) # Add the new element if it doesn't already exist. if self.find(elem, updated=updated) is None: for i, _ in enumerate(node.next): node.next[i] = updated[i].next[i] updated[i].next[i] = node if __name__ == '__main__': with Section('Skip Lists'): gibs = [{ 'name': gibberish(), 'value': rr(10, 1000) } for _ in range(10)] sl = SkipList() for gib in gibs: sl.insert(elem=gib) for gib in gibs: sl.find(gib) sl.view()