summaryrefslogtreecommitdiff
path: root/Biz/Repl.py
diff options
context:
space:
mode:
authorBen Sima <ben@bsima.me>2023-08-28 21:05:25 -0400
committerBen Sima <ben@bsima.me>2023-09-20 17:56:12 -0400
commit6e4a65579c3ade76feea0890072099f0d0caf416 (patch)
tree95671321c951134753323978854cece5f7d5435b /Biz/Repl.py
parent13added53bbf996ec25a19b734326a6834918279 (diff)
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.
Diffstat (limited to 'Biz/Repl.py')
-rw-r--r--Biz/Repl.py36
1 files changed, 36 insertions, 0 deletions
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)