Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.
/ Plpy-Man Public archive

This ill-fated foray into open source attempted to solve a simple problem in an overly complicated manor. The repository has been archived so others can laugh at it 😁

Notifications You must be signed in to change notification settings

snapperVibes/Plpy-Man

Repository files navigation

Plpy Man 🦸‍♂️

PlPython manager that synchronizes Python code between your web server and Postgres database

(Do not use: Deprecated)

Quickstart

Write database function in pure Python.

import plpy_man
import sqlalchemy as sa
from sqlalchemy.orm import Session

# Setup your database
engine = sa.create_engine("<Postgres connection string>")
db = sa.orm.Session(bind=engine)


# Write the database function. Decorate it with plpy_man.plpy_func.
@plpy_man.plpy_func
def py_add(x, y):
    return x + y


# Flush the function to the database!
plpy_man.flush(db)

value = db.execute(sa.text("SELECT py_add(3, 5)"))
assert value == 8

Synchronizes Python code between your web server and database functions

Ensure your PlPython functions match the web-server functions by copying them to PlPython's GD (Gloabl Dict)

import plpy_man
import sqlalchemy as sa
from plpy_man.mocks import plpy, GD
from sqlalchemy.orm import Session, declarative_base

# Setup your database
engine = sa.create_engine("<Postgres connection string>")
db = sa.orm.Session(bind=engine)
Base = declarative_base()
class Employees(Base):
    __tablename__ = "employees"
    id = sa.Column("id", sa.Integer, primary_key=True),
    name = sa.Column("name", sa.TEXT, nullable=False),
    pay = sa.Column("pay", sa.Integer)
db.add(Employees(id=1, name="Alice", pay=15))
db.add(Employees(id=2, name="Bob", pay=15))
db.add(Employees(id=3, name="Charlie", pay=200))
db.flush()


# Write a function.
# This function will be available in both your Python and PlPython scripts
def py_average(sequence):
    return sum(sequence) / len(sequence)


plpy_man.to_gd(py_average)


@plpy_man.plpy_func
def average_wage():
    rows = plpy.execute("SELECT pay FROM employees;")
    wages = [row["pay"] for row in rows]
    average = round(GD["py_average"](wages), 2)
    return f"Average wage is ${average}"


# Flush py_average to the Global Dict and average_wage to the database
plpy_man.flush()

value = db.execute(sa.text("SELECT living_wages()"))
assert value == "Average wage is $76.67"

About

This ill-fated foray into open source attempted to solve a simple problem in an overly complicated manor. The repository has been archived so others can laugh at it 😁

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published