db set_if_new returns bool on if a new key was set or not and doesn't raise duplicate exception

This commit is contained in:
Kevin F 2022-05-05 00:05:53 -05:00
parent c065be0145
commit 91df7507f4

View File

@ -24,7 +24,7 @@ def _do_timeout(func, *args):
return res
def set_if_new(db_path, key, value):
def set_if_new(db_path, key, value) -> bool:
def _set(key, value):
with dbm.open(db_path, "c") as my_db:
try:
@ -33,7 +33,11 @@ def set_if_new(db_path, key, value):
my_db[key] = value
else:
raise DuplicateKey
_do_timeout(_set, key, value)
try:
_do_timeout(_set, key, value)
except DuplicateKey:
return False
return True
def set(db_path, key, value):