From 7633c640572ae97257f13fe1b7129d6fc857eede Mon Sep 17 00:00:00 2001 From: simon987 Date: Sat, 6 Jun 2020 14:08:22 -0400 Subject: [PATCH] add pgsql cursor util & silent stdout --- hexlib/db.py | 17 +++++++++++++++++ hexlib/misc.py | 12 +++++++++++- setup.py | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/hexlib/db.py b/hexlib/db.py index 77bb672..0fb2745 100644 --- a/hexlib/db.py +++ b/hexlib/db.py @@ -129,3 +129,20 @@ def _deserialize(value, col_type): if col_type == "blob": return base64.b64decode(value) return value + + +def pg_fetch_cursor_all(cur, name, batch_size=1000): + + while True: + cur.execute("FETCH FORWARD %s FROM %s", (batch_size, name)) + cnt = 0 + + for row in cur: + cnt += 1 + yield row + + if cnt != batch_size: + cur.execute("FETCH ALL FROM %s", (batch_size, name)) + for row in cur: + yield row + break diff --git a/hexlib/misc.py b/hexlib/misc.py index 554b696..ff79309 100644 --- a/hexlib/misc.py +++ b/hexlib/misc.py @@ -1,4 +1,6 @@ import time +import os +import sys import atexit import siphash @@ -28,7 +30,6 @@ def rate_limit(per_second): def buffered(batch_size: int, flush_on_exit: bool = False): - def decorate(func): buffer = [] lock = Lock() @@ -58,3 +59,12 @@ def strhash(str): def signed64(i): return -(i & 0x8000000000000000) | (i & 0x7fffffffffffffff) + + +def silent_stdout(func, *args, **kwargs): + with open(os.devnull, 'w') as null: + stdout = sys.stdout + sys.stdout = null + res = func(*args, **kwargs) + sys.stdout = stdout + return res diff --git a/setup.py b/setup.py index 59e6f72..8908f2b 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name="hexlib", - version="1.8", + version="1.9", description="Misc utility methods", author="simon987", author_email="me@simon987.net",