Add way to manually flush @buffered

This commit is contained in:
simon987 2021-02-28 12:15:44 -05:00
parent 43cb6c4a7b
commit 66d37e0be2
3 changed files with 43 additions and 1 deletions

View File

@ -57,6 +57,12 @@ def buffered(batch_size: int, flush_on_exit: bool = False):
atexit.register(func, buffer)
def wrapper(items):
if items is None:
func(buffer)
buffer.clear()
return
with lock:
for item in items:
buffer.append(item)

View File

@ -2,7 +2,7 @@ from setuptools import setup
setup(
name="hexlib",
version="1.29",
version="1.30",
description="Misc utility methods",
author="simon987",
author_email="me@simon987.net",

36
test/test_buffered.py Normal file
View File

@ -0,0 +1,36 @@
from unittest import TestCase
from hexlib.misc import buffered
class TestBuffered(TestCase):
def test_simple(self):
my_list = []
@buffered(batch_size=2)
def put_item(items):
my_list.extend(items)
put_item([1, 2])
put_item([1])
put_item([1])
put_item([1])
self.assertEqual(len(my_list), 4)
def test_flush(self):
my_list = []
@buffered(batch_size=2)
def put_item(items):
my_list.extend(items)
put_item([1, 2])
put_item([1])
put_item([1])
put_item([1])
put_item(None)
self.assertEqual(len(my_list), 5)