Last active
March 3, 2019 16:47
-
-
Save agrawalo/4b149de5b73cee1ed6c2dd01d2e506a0 to your computer and use it in GitHub Desktop.
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
python = <<EOF | |
from batchutil import bazzinga | |
def foo(): | |
return f"foo: I am {self.name}" | |
def bar(): | |
return f"bar: I am {self.name}" | |
def baz(): | |
return bazzinga(self) | |
EOF | |
<batch> | |
name p&l_calulator | |
exec_str echo %foo()% | |
</batch> |
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
def bazzinga(batch): | |
return f"baz: I am {batch.name}" |
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
from apacheconfig import * | |
class BatchJobSpec: | |
def __init__(self, pythoncode , batch): | |
self.pythoncode = pythoncode | |
self.name = batch.get("name") | |
self._exec_str = batch.get("exec_str") | |
self.exec() | |
@property | |
def exec_str(self): | |
_exec = self._exec_str.split("%") | |
for i in range(len(_exec)): | |
if _exec[i].endswith('()'): | |
f = self.__getattribute__(_exec[i][:-2]) | |
_exec[i] = f() | |
return ''.join(_exec) | |
def exec(self): | |
locals_ = {'self': self} | |
exec(self.pythoncode, locals_) | |
_exec = [] | |
for attrib in locals_.keys(): | |
if not attrib in ['__builtins__', 'self']: | |
print('__setattr__({})'.format(attrib)) | |
self.__setattr__(attrib, locals_[attrib]) | |
with make_loader() as loader: | |
config = loader.load('batch.spec') | |
batchjobspec = BatchJobSpec(config.get("python"), config.get("batch")) | |
print(batchjobspec.pythoncode) | |
print(batchjobspec.name) | |
print(batchjobspec.exec_str) | |
print(batchjobspec.baz()) |
I actually made it a staticmethod. Please review the code https://gist.github.com/agrawalo/407caf4dcd44687fae6989f46b54975a
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@agrawalo
I recommend the following, as it becomes a indirect member of the
class
and writingdef function_name(self)
are common.This needs to change the following line in
def exec(self)
:The drawback are, you can't call
batchjobspec.baz()
any more you have to usebatchjobspec.baz(batchjobspec)
.You have to decide, more logic in
batch.spec
or in Python code itself.And second, you have to use this with all
def ...
inbatch.spec
even if not needed.Yes, but this complicate finding function parameter in exec string