Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Created December 19, 2019 07:18
Show Gist options
  • Save kurtbrose/1fa8cfd65dced3be655d381ced5125f9 to your computer and use it in GitHub Desktop.
Save kurtbrose/1fa8cfd65dced3be655d381ced5125f9 to your computer and use it in GitHub Desktop.
playing around with resumable glom execution model
from collections import ChainMap
def glom(target, spec):
return Glom(spec, target).glom(ChainMap())
class Glom(object):
"""
rather than state living inside glom() methods, the recursive
calls are all explicitly dumped out as Glom objects, which
can be run again and again to yield the same results (as long
as the targets and specs are not mutated)
if the scope includes the Glom objects, this means that halted
execution can be resumed
"""
def __init__(self, spec, target):
self.spec, self.target = spec, target
def glom(self, scope):
target = target.glom(scope.new_child())
return self.spec.glin([
glom.glom(scope.new_child()) if type(glom) is Glom else glom
for glom in self.spec.glout(self.target, scope)])
class Dict(object):
def __init__(self, spec):
self.spec = spec.items()
def glout(self, target, scope):
return [Glom(item[1], target) for item in self.spec]
def glin(self, results):
return {item[0]: result for item, result in zip(self.spec, results)}
class Tuple(object):
def __init__(self, spec):
self.spec = spec
def glout(self, target, scope):
sofar = target
for sub in self.spec:
# this doesn't quite work -- Glom(spec[1], Glom(spec[0], target))
# gets the target passing correct, but the scope nesting inside out
sofar = Glom(sub, sofar)
return [sofar]
def glin(self, results):
return results[0]
class List(object):
def __init__(self, spec):
self.spec = spec[0]
def glout(self, target, scope):
return [Glom(self.spec, target_item) for target_item in target]
def glin(self, results):
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment