I'm writing a library for creating smaller python modules. I am not sure if I should be using compile() instead of exec(), it currently execute the files(any basic code in the global scope is executed) but not stored as methods in the Function where it can be accessed later by a different file ETC... . It loads the python files as strings
from bagel import webImport
MyFile = WebImport("https://WhereFileIsLoaded.py")
# OR Feature Not Added Yet
MyModule = WebImport("https://WhereModuleIsLoaded.zip")
Here is the Code
import requests
from functools import wraps
# Thank you stack overflow
def withself(f):
@wraps(f)
def wrapper(*args, **kwds):
return f(f, *args, **kwds)
return wrapper
def webImport( URL: str):
data = requests.get(URL).text
@withself
def returnFunc(self):
funcs = {}
exec(data, {'__builtins__':__builtins__}, funcs)
print("Executed Data")
setattr(self, "funcs", funcs)
return self
# Function Object
FUNC = returnFunc()
for func in FUNC.funcs.items():
# Can go through multipe because self.funcs
if not hasattr(FUNC.funcs, str(func[0])):
print(func[0])
# Setting the Atribute of this class -> The FUNCTION with the body of the functiion
setattr(FUNC, str(func[0]), FUNC.funcs[func])
# exec(f"FUNC.{func}")
return FUNC
And here is the Console Output, I am using Python IDLE To test this.
from webImport import webImport
web = webImport("https://pastebin.com/raw/xDJxwMri")
Instead of this line,
setattr(FUNC, str(func[0]), FUNC.funcs[func])
Use This
setattr(FUNC, str(func[0]), FUNC.funcs.get(func[0]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.