Last active
March 31, 2020 13:12
-
-
Save nkpro2000sr/6bce95c4b58dfadd7ba76d6eb23c488a to your computer and use it in GitHub Desktop.
To call called function in another interpreter (or anyother) and return returned.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TeleCaller: | |
"""It is usefull for calling the called function in another interperter. | |
Args: | |
prefix (list): prefix to the call_string | |
modules (list): modules needed to import, added like "import module1, module2, ... ;" | |
before call_string. | |
encoder : passes through before calling | |
decoder : passed through before returning | |
Example: | |
>>>import shutil | |
>>>TeleCaller.caller = print # For understanding | |
>>>shutil_ = TeleCaller(['shutil']) | |
>>>shutil_.which('python') | |
shutil.which('python') | |
>>>TeleCaller.caller = lambda x:eval(x,globals()) | |
>>>shutil_.which('python') | |
'/usr/local/bin/python3.8' | |
""" | |
caller = print | |
encoder = lambda code : code | |
decoder = lambda code : code | |
def __init__(self,prefix=[],modules=[]): | |
self.prefix = prefix | |
self.modules = modules | |
self.caller = self.__class__.caller | |
self.encoder = self.__class__.encoder | |
self.decoder = self.__class__.decoder | |
def __getattr__(self,attr): | |
return TeleCaller(self.prefix+[attr],self.modules) | |
def __call__(self,*args,**kwargs): | |
importstr = 'import '+', '.join(self.modules)+'; ' if len(self.modules) else '' | |
argstr = [repr(arg) for arg in args]+[repr(key)+'= '+repr(val) for key,val in kwargs.items()] | |
call_string = importstr+ '.'.join(self.prefix)+'('+', '.join(argstr)+')' | |
if self.encoder: call_string = self.encoder(call_string) | |
Return = self.caller(call_string) | |
if self.decoder: Return = self.decoder(Return) | |
return Return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment