Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Backoff by blocks when batch inserting data somewhere, and a few records tend to fail
from collections import Iterable
def flatten(l):
if not isinstance(l, Iterable):
yield l
for el in l:
if isinstance(el, Iterable) and not isinstance(el, (str, bytes)):
yield from flatten(el)
yield el
def recursive_backoff(fn, data, **args):
res = []
res = fn(data, **args)
except Exception as e:
if len(data) == 1: return [None]
l = len(data)
res.append(recursive_backoff(fn, data[ :l//2], **args))
res.append(recursive_backoff(fn, data[l//2: ], **args))
return list(flatten(res))
def f(d, args=None):
return (len(d) if (len(d) % 2 == 0) else 1/0)
recursive_backoff(f, [1,2,3,4,5,6,7,8,9,10,11], )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment