Inliner is a tool that inlines function calls from external libraries in a human-readable way. Inlining can be useful to:
The inliner has a Python API as well as an interactive GUI that you can use in Jupyter or JupyterLab notebooks.
As a simple example, consider a library that has this function:
# library.py
def foo(x, edge_case=False):
if edge_case:
return x + 1
else:
return x * 2
# client.py
from library import foo
x = 2
y = foo(x)
print(y) # 4
To understand how foo
works, you would normally have to read the documentation or source code. But this can be challenging since e.g. here, foo
has edge cases encoded as parameters with default values.
If you only care about the code paths used in your specific example, then these edge cases only hinder your understanding. The inliner can turn the client code above into a specialized version removing any code from library
, for example:
def client():
from library import foo
x = 2
y = foo(x)
print(y) # 4
from inliner import Inliner
i = Inliner(client, ['library'])
i.simplify()
print(i.make_program())
The Inliner
class takes the code snippet as input along with a list of modules to inline. Then simplify
performs a series of inlining and cleaning passes until reaching a fixpoint, then outputting this program:
x = 2
# foo(x)
foo_ret = x * 2
y = foo_ret
print(y)
pip3 install inliner
# Jupyter notebook extension (optional)
jupyter nbextension enable inliner/notebook
# JupyterLab extension (optional)
jupyter labextension install @wcrichto/inliner
git clone https://github.com/willcrichton/inliner
cd inliner
pip3 install -e .
pushd inliner_jupyter
npm install
npm run prepublishOnly
popd
jupyter nbextension install inliner_jupyter --user -s
jupyter nbextension enable inliner_jupyter/dist/notebook --user
jupyter labextension link inliner_jupyter
See the notebooks for example usage.