From 9d99ed3e29c53e92cb7601612af7966d714b8b68 Mon Sep 17 00:00:00 2001 From: Friedrich Lindenberg Date: Wed, 4 Apr 2018 09:34:39 +0200 Subject: [PATCH] Add helper class for chunked inserts. --- dataset/chunked.py | 27 +++++++++++++++++++++++++++ setup.py | 3 +-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 dataset/chunked.py diff --git a/dataset/chunked.py b/dataset/chunked.py new file mode 100644 index 0000000..094abdb --- /dev/null +++ b/dataset/chunked.py @@ -0,0 +1,27 @@ + + +class ChunkedInsert(object): + """Batch up insert operations + with ChunkedStorer(my_table) as storer: + table.insert(row) + + Rows will be inserted in groups of 1000 + """ + + def __init__(self, table, chunksize=1000): + self.queue = [] + self.table = table + self.chunksize = chunksize + + def insert(self, item): + self.queue.append(item) + if len(self.queue) > self.chunksize: + self.table.insert_many(self.queue) + self.queue = [] + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.table.insert_many(self.queue) + self.queue = [] diff --git a/setup.py b/setup.py index 7bca119..cce8dc8 100644 --- a/setup.py +++ b/setup.py @@ -3,9 +3,8 @@ from setuptools import setup, find_packages setup( name='dataset', - version='1.0.6', + version='1.0.7', description="Toolkit for Python-based database access.", - long_description="", classifiers=[ "Development Status :: 3 - Alpha", "Intended Audience :: Developers",