Convert python function to PostgreSQL Pl/Python functions
Project description
py2plpy
pl2ply is a simple python package that makes it easy to write PostgreSQL PL/Python functions as native python code with full tooling support.
It consists of the following components:
- a
transformfunction that reads a python file and returns PL/Python code for all functions at the root of the file - a
sql_propertiesdecorator that adds SQL-specific information to a python function - predefined type aliases
SmallInt,BigInt,Real,Double,Out,In,InOut,SetOfandRecord - a dummy
plpyobject that implements the API of the corresponding Pl/Python object - the
py2plpycommand line tool
Usage
# fruit.py
from py2plpy import sql_properties, Out, SetOf, Record, plpy
@sql_properties(strict=True)
def find_fruit(string:str, fruit_no:Out[int], fruit:Out[str]) -> SetOf[Record]:
import re
plpy.info('Searching for fruit...')
for i, m in enumerate(re.findall(r'apple|pear|banana', string)):
yield i, m
py2plpy fruit.py fruit.sql
-- fruit.sql
CREATE OR REPLACE FUNCTION find_fruit (
string TEXT,
OUT fruit_no INTEGER,
OUT fruit TEXT)
RETURNS SETOF RECORD
STRICT
LANGUAGE PLPYTHON3U
AS $BODY$
import re
plpy.info('Searching for fruit...')
for i, m in enumerate(re.findall(r'apple|pear|banana', string)):
yield i, m
$BODY$;
Custom Types
Any unknown type is represented by its __name__ attribute in SQL. To use custom argument or return types, simply create a type of the appropriate name in Python. For full tooling support, it is usually most convienent to define it as an alias to type that will represent it in Python (typically str, dict for composite types):
# from Python 3.12
type custom_type = str
# Python 3.11 and below
from typing_extensions import TypeAliasType
custom_type = TypeAliasType('custom_type', str)
Missing Features
- Support for schema-qualified type names
- Predefined type aliases for PostgreSQL types with no native Python equivalent (e.g.
point)
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py2plpy-0.4.0.tar.gz.
File metadata
- Download URL: py2plpy-0.4.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
217fe999d43da2699b9cc755161aaba239631fa12fd3670f983cbc98a6ba9936
|
|
| MD5 |
0ba8bfee664fa2fdaba91cf16eeb960c
|
|
| BLAKE2b-256 |
3ee9c7ddbceb1c7303a74eb61c02e867631c436e250d5bd776196ca526744c7a
|
File details
Details for the file py2plpy-0.4.0-py3-none-any.whl.
File metadata
- Download URL: py2plpy-0.4.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b335799c88aa81d497507adc9eaa20f8115a287750bba780699999d5e60dba35
|
|
| MD5 |
5fb932abc07ea9b08d09fdcd98e7ccbe
|
|
| BLAKE2b-256 |
d5015d5bc1890b24bde06dbbb7c62b685a565e693d18f2ca95b4909c1bf16e22
|