acsresearch / interlab

MIT License
17 stars 3 forks source link

Allow using a context as parent while keeping it open #31

Open gavento opened 11 months ago

gavento commented 11 months ago

I want to repeatedly use a context as a parent context for others, but not close it after the first use.

I want something like this:

c = Context("root", store=store)
with c:
  Context("child1", inputs={"a":42}
[...]
with c:
  Context("child2", inputs={"a":42}
c.close()

This would be especially handy in jupyter notebooks.

One solution would be to have something like:

c = Context("root", store=store)
with c.kept_open:
  Context("child1", inputs={"a":42}
[...]
with c:  # not kept open here
  Context("child2", inputs={"a":42}
gavento commented 11 months ago

A related enhancement: With non-closing contexts, it could be helpful to add some automatic closing (with some non-critical error) when a context goes out of scope. This could be done in in __del__ except that Context is likely kept in the parent Context. (Could be del of a thin wrapper? Some other way?)