From 6e4a65579c3ade76feea0890072099f0d0caf416 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Mon, 28 Aug 2023 21:05:25 -0400 Subject: Prototype Mynion This implements a prototype Mynion, my chatbot which will eventually help me write code here. In fact he's already helping me, and works pretty well over xmpp. The prompt is currently not checked in because I'm experimenting with it a lot, and it should probably be a runtime parameter anyways. In the course of writing this I added some helper libraries to get me going, configured black (didn't even know that was possible), and added 'outlines' and its dependencies even though I didn't end up using it. I'll keep outlines around for now, but I'm not sure how useful it really is because afaict its just pre-defining some stop conditions. But it took a while to get it working so I'll just keep it in for now. --- Biz/Repl.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Biz/Repl.py (limited to 'Biz/Repl.py') diff --git a/Biz/Repl.py b/Biz/Repl.py new file mode 100644 index 0000000..0732fae --- /dev/null +++ b/Biz/Repl.py @@ -0,0 +1,36 @@ +""" +This module attempts to emulate the workflow of ghci or lisp repls. It uses +importlib to load a namespace from the given path. It then binds 'r()' to a +function that reloads the same namespace. +""" + +import importlib +import sys + + +def use(ns: str, path: str) -> None: + """ + Load or reload the module named 'ns' from 'path'. Like `use` in the Guile + Scheme repl. + """ + spec = importlib.util.spec_from_file_location(ns, path) + module = importlib.util.module_from_spec(spec) + # delete module and its imported names if its already loaded + if ns in sys.modules: + del sys.modules[ns] + for name in module.__dict__: + if name in globals(): + del globals()[name] + sys.modules[ns] = module + spec.loader.exec_module(module) + names = [x for x in module.__dict__] + globals().update({k: getattr(module, k) for k in names}) + + +if __name__ == "__main__": + NS = sys.argv[1] + PATH = sys.argv[2] + use(NS, PATH) + + def r(): + return use(NS, PATH) -- cgit v1.2.3