MCPcopy Create free account
hub / github.com/Apress/python-for-matlab-development / populate_table

Function populate_table

code/IO/postgres_crud.py:43–62  ·  view source on GitHub ↗
(conn, cur)

Source from the content-addressed store, hash-verified

41 return (n, price, pet)
42
43def populate_table(conn, cur):
44 T_start = time.time()
45 cur.execute("""create table myvalues (id serial primary key,
46 n integer, price float, pet varchar)""")
47 batch_size = 3_0
48 n_records = 10_0
49 batch = []
50 insert = "insert into myvalues (n,price,pet) values(%s,%s,%s)"
51 for i in range(n_records):
52 n,price,pet = record()
53 batch.append((n, price, pet))
54 if len(batch) < batch_size:
55 continue
56 cur.executemany(insert, batch)
57 conn.commit()
58 batch = []
59 if batch:
60 cur.executemany(insert, batch)
61 conn.commit()
62 print(f'inserted {n_records} rows in {time.time()-T_start:.3} sec')
63
64def query_results(cursor):
65 query = "select sum(price), sum(n), pet from myvalues group by pet"

Callers 1

mainFunction · 0.85

Calls 1

recordFunction · 0.85

Tested by

no test coverage detected