Skip to main content

babar is a library for automatically generating Postgres extensions from absolutely any Python object.

Project description

Logo

Babar🐘❤️🐍

Turn any Python object into a Postgres extension

Status | Why | How | Installation | Usage | Roadmap

Status

WARNING: This is a work in progress and is far from doing what it promises. But not that far... 😉

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 a few 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 like click;
  • 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybabar-0.1.0a3.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybabar-0.1.0a3-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file pybabar-0.1.0a3.tar.gz.

File metadata

  • Download URL: pybabar-0.1.0a3.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for pybabar-0.1.0a3.tar.gz
Algorithm Hash digest
SHA256 8b24c975645e3c3a821406be22d5d685b3083851ed20115b415018899acbded7
MD5 45c64483ea45598aa7045b45ff4a96db
BLAKE2b-256 495224d2d1794258a51596477865f78438bd1011928db7c7669c7fb1d98fbdc9

See more details on using hashes here.

File details

Details for the file pybabar-0.1.0a3-py3-none-any.whl.

File metadata

  • Download URL: pybabar-0.1.0a3-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for pybabar-0.1.0a3-py3-none-any.whl
Algorithm Hash digest
SHA256 c24d0add7ad7e38f5221fe6ded4fd6266eee2e1b07bd9d07e669b88cf0b3a1aa
MD5 ccb688728bcccf56062531ad36660f94
BLAKE2b-256 14f8215a652683fe460d17ff840de67c7a57303d05b446991c3e01025800a17c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page