prototype

This commit is contained in:
2021-02-06 12:40:18 +00:00
commit 13ab316144
6 changed files with 114 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
import sys
import importlib.util
def load_module(module_name, file_path):
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
Executable
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
from pathlib import Path
from time import sleep
from concurrent.futures import ThreadPoolExecutor
from importing import load_module
def load_files(folder):
folder = Path(folder)
fnames = folder.glob("*.py")
fnames = sorted(fnames)
res = {}
for fn in fnames:
print(fn, fn.stat().st_mtime)
name = fn.stem
mod = load_module(name, fn)
res[name] = mod.run
return res
folder = "scripts"
mods = load_files(folder)
while True:
with ThreadPoolExecutor() as ex:
for f in mods.values():
fut = ex.submit(f)
print(fut)
sleep(1)
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env python
from time import sleep
def run():
print("Test Sleep: start")
sleep(1)
print("Test Sleep: done")
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env python
N = 0
def run():
global N
print("Test Global Variable:", N)
N += 1
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env python
class Counter:
def __init__(self):
self.n = 0
def inc(self):
self.n += 1
def __str__(self):
return str(self.n)
counter = Counter()
def run():
print("Test Global Class:", counter)
counter.inc()
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env python
class Run:
def __init__(self):
self.n = 0
def inc(self):
self.n += 1
def __str__(self):
return f"Test Callable Class: {self.n}"
def __call__(self):
print(self)
self.inc()
run = Run()