A micro library for generating Python source code.
pip install outsourcer
Outsourcer requires Python 3.6 or later.
Sometimes you have to generate some Python code, and sometimes it's a bit too complicated for using string templates. This is a small library you can use in those cases.
>>> from outsourcer import sym
>>> sym.print('hello, world')
print('hello, world')
This example uses sym
to create a code fragment.
Here's a longer example:
>>> from outsourcer import CodeBuilder, sym
>>> b = CodeBuilder()
>>> item = sym.item
>>> with b.FOR(item, in_=sym.some_collection):
... with b.IF(item % 2 == 0):
... b += sym.print('even')
... with b.ELSE():
... b += sym.skipped.append(item)
...
>>> print(b.source_code())
for item in some_collection:
if ((item % 2) == 0):
print('even')
else:
skipped.append(item)