psycopg2 utils
This package contains helper classes for psycopg2.
Installation
pip install psycopg2-utils
ConnectionPool
This connection pool is thread safe and does not raise an exception when the pool is empty and a thread tried to get a connection from it. Instead the thread will wait for a connection to become available in the pool. Connections will also not be closed when returned to the pool until they have been idle for some specified time.
from psycopg2_utils import ConnectionPool
config = {
"minconn": os.environ["DB_MINCONN"],
"maxconn": os.environ["DB_MAXCONN"],
"idle_time": os.environ["IDLE_TIME"],
"dbname": os.environ["DB_NAME"],
"host": os.environ["DB_HOST"],
"port": os.environ["DB_PORT"],
"user": os.environ["DB_USER"],
"password": os.environ["DB_PASS"],
"options": "-c search_path={}".format(os.environ["DB_SCHEMA"]),
}
pool = ConnectionPool(**config)
# Get connection from the pool
con = pool.getconn()
# Return connection to the pool
pool.putconn(con)
Cursor
This cursor extends the dict curser and enables logging the sql queries when the log level is set to DEBUG and also logs how long each query takes to execute if log level is INFO and there is an environment variable named METRIC_LOGGING that has the value "TRUE".
from psycopg2 import connect
from psycopg2_utils import Cursor
con = connect("")
cur = con.cursor(cursor_factory=Cursor)
pooled_cursor
This allows a method of a class to be decorated so that it will automatically supply
the method with a cursor to use.
It requires that the class has a property named pool containing a connection pool that
implements the AbstractConnectionPool.
from psycopg2_utils import pooled_cursor
class MyClass:
def __init__(self, config):
self.pool = ConnectionPool(**config)
@pooled_cursor
def get_count(self, cursor):
cursor.execute("SELECT count(*) FROM table;")
return cursor.fetchone()[0]
my_class = MyClass(config)
#The method can then be used like this:
count = my_class.get_count()
# If you already hold a cursor and don't want to get a new one
# from the pool then it can be passed into the method.
cursor = some_method_that_gets_cursor()
count = my_class.get_count(cursor=cursor)
The wrapper will take care of getting a connection from the pool and returning it to the pool at the end. It also does a commit when there is no exception.
Metadata
Release files for psycopg2-utils 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| psycopg2-utils-1.0.3.tar.gz | 5.7 kB | Without provenance | Details for psycopg2-utils-1.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| psycopg2_utils-1.0.3-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for psycopg2_utils-1.0.3-py2.py3-none-any.whl |
Total release size: 12.3 kB
Release files / psycopg2-utils-1.0.3.tar.gz
| Download URL | psycopg2-utils-1.0.3.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4fd51bd00c927823691aecaa492270a2925324e9ec4b1fb19e9007a9734ee4f4
|
|
BLAKE2b-256 checksum How to use checksums |
b28a6e88e0885f4174056b2dfff06d99c9b209d08f0b29b5d9c6a72d94a4d2ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6
|
Release files / psycopg2_utils-1.0.3-py2.py3-none-any.whl
| Download URL | psycopg2_utils-1.0.3-py2.py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
6a71ebbe1e934c1076f92fb0b5b0b6d0b22a5886740694d2d3e11c1c9ec12581
|
|
BLAKE2b-256 checksum How to use checksums |
2cae96d5ab1e3068a319c62d56fce2da06803c804baceb7567004d6b9f42eff9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6
|
Security for psycopg2-utils 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.