mirror of
https://github.com/simon987/hexlib.git
synced 2025-04-19 17:56:43 +00:00
add pgsql cursor util & silent stdout
This commit is contained in:
parent
f92776e646
commit
7633c64057
17
hexlib/db.py
17
hexlib/db.py
@ -129,3 +129,20 @@ def _deserialize(value, col_type):
|
|||||||
if col_type == "blob":
|
if col_type == "blob":
|
||||||
return base64.b64decode(value)
|
return base64.b64decode(value)
|
||||||
return 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
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import time
|
import time
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
import atexit
|
import atexit
|
||||||
import siphash
|
import siphash
|
||||||
@ -28,7 +30,6 @@ def rate_limit(per_second):
|
|||||||
|
|
||||||
|
|
||||||
def buffered(batch_size: int, flush_on_exit: bool = False):
|
def buffered(batch_size: int, flush_on_exit: bool = False):
|
||||||
|
|
||||||
def decorate(func):
|
def decorate(func):
|
||||||
buffer = []
|
buffer = []
|
||||||
lock = Lock()
|
lock = Lock()
|
||||||
@ -58,3 +59,12 @@ def strhash(str):
|
|||||||
|
|
||||||
def signed64(i):
|
def signed64(i):
|
||||||
return -(i & 0x8000000000000000) | (i & 0x7fffffffffffffff)
|
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
|
||||||
|
2
setup.py
2
setup.py
@ -2,7 +2,7 @@ from setuptools import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="hexlib",
|
name="hexlib",
|
||||||
version="1.8",
|
version="1.9",
|
||||||
description="Misc utility methods",
|
description="Misc utility methods",
|
||||||
author="simon987",
|
author="simon987",
|
||||||
author_email="me@simon987.net",
|
author_email="me@simon987.net",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user