A simpler way to create ad hoc store types #23
thorwhalen
started this conversation in
Enhancement
Replies: 1 comment
-
Pushed proposals as: KvReaderShell and StoreShell. On the problem I was working on, I realized that it would be better (necessary?) to have recursive capabilities. That is, be able to use the wrapper to wrap the (non-leaf) values of the mapping. The situation was (approx) this: ## Assuming:
# from extrude import mk_web_app
# from extrude import mk_api, run_api
# def foo(x): return x
# app = mk_api([foo])
# run_api(app, server='wsgiref') # in a separate process
# app = mk_web_app([foo], api_url='http://localhost:3030/openapi') And I could do this: from dol.scrap.store_factories import KvReaderShell
s = KvReaderShell(
app,
getter=lambda o, k : o.children[k],
lister=lambda o: iter(range(len(o.children))),
sizer=lambda o: len(o.children),
)
s[0].children[1]
# ExecSection(obj=<function mk_request_function.<locals>.request_func at 0x14df6cdc0>, name='Execution') But wanted to express it as Did it like this: from dol.scrap.store_factories import KvReaderShell
S = partial(
KvReaderShell,
getter=lambda o, k : S(o.children[k]),
lister=lambda o: iter(range(len(o.children))),
sizer=lambda o: len(o.children),
)
s = S(app)
s[0][1]
# KvReaderShell(src=ExecSection(obj=<function mk_request_function.<locals>.request_func at 0x14df6cdc0>, name='Execution'), getter=<function <lambda> at 0x14df256c0>, lister=<function <lambda> at 0x14dbe9990>, sizer=<function <lambda> at 0x14df6d510>, is_contained=<function check_by_iteration at 0x14d80d900>) (No leaf termination is not handled here!) This recursive gets pattern is a common one. We should make it a clear recipe for starters, and make a tool eventually. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
To use the tooling around mappings we need to get the problem into a mapping problem in the first place.
Often, the concept is easy but the implementation has too much boilerplate.
It doesn't have to be so with the right tool.
For example, consider a tree structure -- a ubiquitous structure in programming. If explicitly implemented as a tree, you'll get something like node types that have a method/property providing the contents of a node and a method/property that lists the child nodes (sometimes there are more than one choice for these defining methods).
Transforming that interface to a mapping one is simple, in principle: The
__getitem__
should return the content and the__iter__
should list the child nodes. So all you need to do is define a class, subclassingKvReader
(to get containment and length methods for free), and write the__init__
,__getitem__
and__iter__
classes. But that seems like more boilerplate than is needed.Beta Was this translation helpful? Give feedback.
All reactions