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)
'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()
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)
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)
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)