Skip to content

Instantly share code, notes, and snippets.

@agrawalo
Created March 3, 2019 16:47
Show Gist options
  • Save agrawalo/407caf4dcd44687fae6989f46b54975a to your computer and use it in GitHub Desktop.
Save agrawalo/407caf4dcd44687fae6989f46b54975a to your computer and use it in GitHub Desktop.
python = <<EOF
from batchutil import bazzinga
def foo(batch):
return f"foo: I am {batch.name}"
def bar(batch):
return f"bar: I am {batch.name}"
def baz(batch):
return bazzinga(batch)
EOF
<batch>
name p&l_calulator
exec_str echo %foo(self)%
</batch>
def bazzinga(batch):
return f"baz: I am {batch.name}"
from apacheconfig import *
with make_loader() as loader:
config = loader.load('cleanbatch.spec')
class BatchJobSpec:
def __init__(self, pythoncode , batch):
self.pythoncode = pythoncode
self.name = batch.get("name")
self._exec_str = batch.get("exec_str")
self.exec()
def exec(self):
locals_ = {'self': self}
exec(self.pythoncode, locals_)
_exec = []
for attrib in locals_.keys():
if not attrib in ['__builtins__', 'self']:
setattr(BatchJobSpec, attrib, staticmethod(locals_[attrib]))
@property
def exec_str(self):
_exec = self._exec_str.split("%")
for i in range(len(_exec)):
if _exec[i].endswith('(self)'):
f = self.__getattribute__(_exec[i][:-6])
_exec[i] = f(self)
return ''.join(_exec)
batchjobspec = BatchJobSpec(config.get("python"), config.get("batch"))
print(batchjobspec.name)
print(batchjobspec.exec_str)
print(batchjobspec.baz(batchjobspec))
@WK-GiHu
Copy link

WK-GiHu commented Mar 3, 2019

@agrawalo

Ahh, using

setattr(BatchJobSpec, attrib, locals_[attrib])

make it a full class method, now you can use

print(batchjobspec.baz())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment