Skip to main content

PythonUtils

This package contains common Python utility classes and functions.

Classes

  • Pushing records to Kinesis
  • Setting and retrieving a resource in S3
  • Decrypting values with KMS
  • Encoding and decoding records using a given Avro schema
  • Retrieving secrets from AWS Secrets Manager
  • Downloading files from a remote SSH SFTP server
  • Connecting to and querying an Azure SQL database
  • Connecting to and querying a MySQL database
  • Connecting to and querying a PostgreSQL database
  • Connecting to and querying Redshift
  • Connecting to and querying Snowflake
  • Making requests to the Oauth2 authenticated APIs such as NYPL Platform API and Sierra
  • Interacting with vendor APIs such as cloudLibrary

Functions

  • Reading a YAML config file and putting the contents in os.environ -- see config/sample.yaml for an example of how the config file should be formatted
  • Creating a logger in the appropriate format
  • Obfuscating a value using bcrypt
  • Parsing/building Research Catalog identifiers
  • Mapping between barcodes and Sierra patron ids plus getting patron data from Sierra and Redshift using those ids or record_nums

Usage

# test_file.py
from nypl_py_utils.classes.kinesis_client import KinesisClient
from nypl_py_utils.functions.config_helper import load_env_file

load_env_file(...)
kinesis_client = KinesisClient(...)
# requirements.txt

# Do not use any version below 1.0.0
# All available optional dependencies can be found in pyproject.toml.
# See the "Managing dependencies" section below for more details.
nypl-py-utils[kinesis-client,config-helper]==1.x.y

Developing locally

In order to use the local version of the package instead of the global version, use a virtual environment. To set up a virtual environment and install all the necessary dependencies, run:

python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install .
pip install '.[development]'
deactivate && source .venv/bin/activate

Managing dependencies

In order to prevent dependency bloat, this package has no required dependencies. Instead, each class and helper file has its own optional dependency set. For instance, if an app needs to use the KMS client and the obfuscation helper, it should add nypl-py-utils[kms-client, obfuscation-helper] to the app's requirements. This way, only the required dependencies are installed.

When a new client or helper file is created, a new optional dependency set should be added to pyproject.toml. The development dependency set, which includes all the dependencies required by all of the classes and tests, should also be updated.

The optional dependency sets also give the developer the option to manually list out the dependencies of the clients rather than relying upon what the package thinks is required, which can be beneficial in certain circumstances. For instance, AWS lambda functions come with boto3 and botocore pre-installed, so it's not necessary to include these (rather hefty) dependencies in the lambda deployment package.

Troubleshooting

Using PostgreSQLClient in an AWS Lambda

Because psycopg requires a statically linked version of the libpq library, the PostgreSQLClient cannot be installed as-is in an AWS Lambda function. Instead, it must be packaged as follows:

pip install --target ./package nypl-py-utils[postgresql-client]==1.x.y

pip install \
    --platform manylinux2014_x86_64 \
    --target=./package \
    --implementation cp \
    --python 3.9 \
    --only-binary=:all: --upgrade \
    'psycopg[binary]'

Using PostgreSQLClient locally

If using the PostgreSQLClient produces the following error locally:

ImportError: no pq wrapper available.
Attempts made:
- couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
- couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary'
- couldn't import psycopg 'python' implementation: dlsym(0x7f8620446f40, PQsslInUse): symbol not found

then try running:

pip uninstall psycopg
pip install "psycopg[c]"

Git workflow

This repo uses the Main-QA-Production git workflow.

main has the latest and greatest commits, qa has what's in our QA environment, and production has what's in our production environment.

Ideal Workflow

  • Cut a feature branch off of main
  • Commit changes to your feature branch
  • File a pull request against main and assign a reviewer (who must be an owner)
    • Include relevant updates to pyproject.toml and README
      • If you're planning to cut a release, remember to update project version in pyproject.toml!
    • In order for the PR to be accepted, it must pass all unit tests, have no lint issues, and update the CHANGELOG (or contain the Skip-Changelog label in GitHub)
  • After the PR is accepted, merge into main
  • Merge main > qa
  • Deploy app to QA on GitHub and confirm it works
  • Merge qa > production
  • Deploy app to production on GitHub and confirm it works

Deployment

The utils repo is deployed as a PyPI package here and as a Test PyPI package for QA purposes here. In order to be deployed, the version listed in pyproject.toml must be updated. To deploy to Test PyPI, create a new release in GitHub and tag it qa-vX.X.X. The GitHub Actions deploy-qa workflow will then build and publish the package. To deploy to production PyPI, create a release and tag it production-vX.X.X.

Metadata

Release files for nypl-py-utils 1.12.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nypl-py-utils 1.12.3
File Size Uploaded
nypl_py_utils-1.12.3.tar.gz 40.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nypl-py-utils 1.12.3
File Interpreter ABI Platform
nypl_py_utils-1.12.3-py3-none-any.whl Python 3 none any Details

Total release size: 71.7 kB

Release files / nypl_py_utils-1.12.3.tar.gz

Download URL nypl_py_utils-1.12.3.tar.gz
Size 40.2 kB
Tags Source
SHA-256 checksum
How to use checksums
067f3aea59e2ee081d9404daf8a84cbb71aaaa7bded503d7066a5967ed10cfe2
BLAKE2b-256 checksum
How to use checksums
69288abd63af44430114ff72784505f6efed711f723ea1174e2962160e41b649
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release files / nypl_py_utils-1.12.3-py3-none-any.whl

Download URL nypl_py_utils-1.12.3-py3-none-any.whl
Size 31.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c39de986958bdbc94b583f9c5d2ce6bcac6bce6b9f9d71b3149dfbdb420f818
BLAKE2b-256 checksum
How to use checksums
5ca1e917bd31520cb69fe8b82872cc2a57880c75ae5129cb08740ce3515c7779
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 21, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.12.3 This release

2 release files

1.12.2

2 release files

1.12.1

2 release files

1.12.0

2 release files

1.11.1

2 release files

1.10.2

2 release files

1.10.1

2 release files

1.10.0

2 release files

1.9.1

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.5

2 release files

1.6.4

2 release files

1.6.2

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.2

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page