babar is a library for automatically generating Postgres extensions from absolutely any Python object.
Project description
Babar🐘❤️🐍
Turn any Python object into a Postgres extension
Why |
How |
Installation |
Usage |
Roadmap
Why
Postgres has supported Python as a procedural language for years via PL/Python.
Embedding non-trivial Python code in Postgres can get cumbersome and usually involves copy-pasting from existing code bases and/or creating thin wrappers around existing functions.
babar automates this process by allowing you to seamlessly package existing Python components (functions, classes etc.) int a postgres extension.
How
babar dynamically inspects the definition of Python objects
and generates semantically equivalent Postgres definitions
along with the necessary extension files (.control
, Makefile
, .sql
)
Installation
Usage
pip install pybabar
Let's create a Postgres extension called pystring
which adds two simple Python functions.
from babar import Extension
def pyconcat(x: str, y: str) -> str:
return x + y
def pyupper(x: str) -> str:
return x.upper()
if __name__ == "__main__":
Extension(
"pystring",
pyconcat, pyupper,
comment="this is the pystring extension",
default_version="0.1.0",
)
Then, from the command line, you can run:
python pystring.py
That will create the appropriate extension files, which you can then install in the usual Postgres way:
make
make install
Then you can CREATE
the extension and use it
psql -d postgres <<SQL
CREATE EXTENSION pystring
SELECT pyconcat('hello ', 'world');
SELECT pyupper('hello');
SQL
Roadmap
v0.1.0 (wip)
- simple functions (not classes, methods etc)
- Functions should be 100% self-contained (outer scope is not inspected)
- primitive types only: support only str/text as arg and return
- Probably no cli interface / just python main.py
Future
@pg_function
decorator for individual functions. Much likeclick
;- maybe subclass
Extension
should make a whole Python class an extension? - CLI like
babar -m pack.mod.func1 pack2.mod2.func2
- Handle upgrades
- pgxn integration
It is lightly inspired by Python-Fire which turns any Python object into a cli interface
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pybabar-0.1.0a1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2a873d861f2e4d0599e04620d58af9f4589c3d889d28efeb607b37cbaaa7c41 |
|
MD5 | 7ca7294f1c0cce003bd70c99b3a2541d |
|
BLAKE2b-256 | 953be3ccdd242340e14a03819f2fd3fd8fe18ef9c60bad5c699eff1f477e12fa |