Open bitprophet opened 10 years ago
Another option besides using locals()
+ a dict constructor, would be simply reusing the module constructor with sys.modules[__name__]
. Probably nice.
Still have to solve the actual API for this for a few cases:
Collection.__init__()
- with no additional tweaks or rebindings"
ns = Collection.from_here()
ns = Collection(here(), imported_module_as_is, differentname=another_imported_module)
but that falls afoul of the previously-noted "pulls local tasks in as a subcollection, instead of using it as the root itself"ns = Collection.from_here(imported_module_as_is, differentname=another_imported_module)
is confusing/uglyns = Collection(root=here(), imported_module_as_is, differentname=another_imported_module)
(or base=
instead of root=
)Collection( imported_module_as_is, differentname=another_imported_module, root=here())
doesn't read/write very well because the fact that it's rooted in the local module is obscured.add_collections()
plural-adder which acts just like __init__
, so:
ns = Collection.from_here() ns.add_collections(imported_module_as_is, differentname=another_imported_module)
ns = Collection.from_here().with(imported_module_as_is, differentname=another_imported_module)
but I'm not a big fan of that, really.
Use case
One's local tasks.py marries some imported task modules (e.g. from Invocations) and some custom local-to-project tasks. It has to be done like this right now:
This isn't awful on the face of it, but it means that one has to remember to edit that
ns
line every time one changes the contents of the local module. This is extremely error-prone (I say this via experience...) and thus frustrating.Solutions
Make it easier to add "self" to a
Collection
so one may state something like:Or more realistically:
Alternately/additionally:
locals()
(orglobals()
?) and does abovementioned scraping ofTask
values. Again, same logic asfrom_module
just w/ a dict instead of a module object.from_dict
,from_collection
and/or some new classmethod (from_here
?) so they can take additional args like__init__
does.add_collection
line for every imported module, let me do something likeCollection.from_dict(locals(), docs, test)
.add_collections
that takes >1 arg...__init__
be told to call e.g.from_dict
on dict input, just like how it handles modules, collections & tasks already. E.g.ns = Collection(locals(), docs, test)
.locals()
- we don't wantlocals()
to end up in another sub-namespace. It needs to be the root namespace.__init__
always just being sugar for calling other public methods by hand.locals()
and/orglobals()
may include the imported sub-modules from Invocations. Make sure any syntactic sugar implementation does the least surprising thing here, and that working around it / avoiding using it is not a big slog.Related to #59.