def ustr(excl_list=None, max_tries=50, max_iter=2): doc_gen = DocumentGenerator() num_words = random.randint(1, max_iter) removelist = "-" excl_list = [] if excl_list is None else excl_list try_cnt = 1 while True: str_t = '' for i in range(num_words): str_t = str_t + '_' + re.sub(r'[^\w' + removelist + ']', '', doc_gen.slug()).replace('-', '_') # endfor if str_t not in excl_list: break # endif if try_cnt > max_tries: raise ValueError( 'max tries {} exceeded in ustr()'.format(max_tries)) # endif try_cnt += 1 # endwhile return str_t.strip('_')
def test_all(self): gen = DocumentGenerator() def gen_contact(): return {'email': gen.email(), 'phone': gen.phone()} def gen_rating(): return random.uniform(1, 5) template = { 'id': 'guid', 'status': ['online', 'offline', 'dnd', 'anonymous'], 'age': 'small_int', 'homepage': 'url', 'name': 'name', 'headline': 'sentence', 'about': 'paragraph', 'email': { 'typemap': 'email', 'unique': True }, 'rating': gen_rating, 'contact': gen_contact, 'favpost': [gen.slug() for n in range(1000)] } gen.set_template(template) documents = gen.documents(10) print(documents[0]) self.assertEqual(len(documents), 10)
def main(): gen = DocumentGenerator() conn = None while conn is None: try: conn = psycopg2.connect(database=os.getenv('DB_NAME'), user=os.getenv('DB_USER'), password=os.getenv('DB_PASSWORD'), host=os.getenv('DB_HOST')) except Exception as e: print(f'PostgreSQL does not seem to be ready yet: {e}') time.sleep(1) print('Connected to PostgreSQL') while True: time.sleep(random.random() / 5) key = gen.slug() value = gen.sentence() pk = uuid.uuid1() cursor = conn.cursor() cursor.execute('INSERT INTO events(id, name, info) VALUES(%s, %s, %s)', (str(pk), key, value)) conn.commit() print('New event:') print(' pk:', pk) print(' key:', key) print(' value:', value) return 0