Skip to main content

Wrapper around psycopg3 using easy functions to interact with postgres.

Project description

wrapg

Wrapper around psycopg 3.x meant to make easy use of postgres using simple python functions. Primary focus is processing python data structures into and out of postgres.

Project is in infancy, work in progress.

Table of Contents

Features

  • Simple python functions to run postgres sql via python
  • Pass/Retrieve various python data structutes via underlining sql functions
    • supports pandas dataframe out of box
  • Auto import default postgres connection parameters via .env
    • saves on repeating code to specify connection
    • overide default connection parameters with kwargs in each function
    • see examples for more details
  • Upsert, insert_ignore functions included
    • automatically creates index
    • pass sql functions on 'On Conflict' keys (see example)
    • option to upsert/insert_ignore without creating index (future)
  • Copy functions to follow postgres COPY protocol (today only csv is avail)

Installing Wrapg

Wrapg is available on PyPI:

pip install wrapg

Dependencies:

Usage

TODO: add more examples later

Connection

Before you get started is is recommended to create .env file with below connection parameters. Wrapg will auto-import default connectionn parameters and make all functions ready to be executed.
The .env file should contain below specific name variables

# Database config
PG_HOST = "localhost"
PG_USER = "postgres"
PG_PASSWORD = "mypass"
PG_PORT = 1234
PG_DBNAME = "sales"

Any connection variable can be overwritten in each function call if needed per postgres connection parameter key words.

Below will overide connection dbname for specific function call.

qry="SELECT * FROM customer" 
wrapg.query(raw_sql=qry, conn_kwargs: {'dbname': 'anotherdb'})

Upsert & Insert Ignore

Easily call sql upsert or insert_ignore.
The 'keys' parameter represents the on conflict target & will create unique index automatically.

record = {'name': 'Dave', 'email': 'dave@email.com'}
wrapg.upsert(data=record, table="customer", keys=["email"])

Todo

  • Handle other operators other than '='; >, <, <>, in, between, like?
  • Implement create_index(), drop_column(), distinct()
  • Handle JSON, ITERATOR?
  • Add more tests
  • Optimize code after it is all working

Acknowledgements

This project built on great work by psycopg 3 and was inspired by dataset

Contact

Created by jturnercode - hope package is helps.

License

  • MIT

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

wrapg-0.2.1.tar.gz (13.6 kB view hashes)

Uploaded Source

Built Distribution

wrapg-0.2.1-py3-none-any.whl (13.3 kB view hashes)

Uploaded Python 3

Supported by

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