mirror of
https://github.com/simon987/hexlib.git
synced 2025-04-04 02:12:59 +00:00
Add way to manually flush @buffered
This commit is contained in:
parent
43cb6c4a7b
commit
66d37e0be2
@ -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)
|
||||
|
2
setup.py
2
setup.py
@ -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
36
test/test_buffered.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user