Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
import json
import sys
import typing
from pathlib import Path
from types import NoneType
@jacksmith15
jacksmith15 / README.md
Last active August 22, 2023 15:45
express-knex-transaction-middleware.ts

Middleware factory for using a single transaction per request. Transactions are only started if a downstream handler requests one. If a transaction is started by the handler, it is automatically committed or rolled back once the handler completes.

Usage:

Enable the middleware:

app.use(makeDatabaseTransactionMiddleware(knex));

Python Dependency Management

Below is a very rough-and-ready summary of Python dependency management tools I have used.

An (incomplete) list of important factors of dependency management tooling:

  • Reproducibility - will committed files allow us to exactly reproduce an environment across dev machines, CI and production environments
  • Speed of resolution - how fast can a complete environment be resolved given a set of primary dependencies
  • Ease of management - how easy is it to upgrade one/all packages? how easy is it to add a new package? how easy is it to inspect the dependency graph?
  • Availability of packages - can I get the packages I need?
import asyncio
from typing import TYPE_CHECKING
from aiohttp.client import ClientSession
from elasticsearch import AsyncElasticsearch
from elasticsearch.connection_pool import DummyConnectionPool, EmptyConnectionPool
if TYPE_CHECKING:
from aiohttp.client_reqrep import ConnectionKey
@jacksmith15
jacksmith15 / README.md
Last active December 21, 2021 10:51
Command-line log streaming from Elastic.

Logs

Stream logs from Elasticsearch to stdout.

Requirements

Python 3.8 or higher

pip install python-dateutil==2.8.2 fire==0.4.0 requests==2.26.0
@jacksmith15
jacksmith15 / README.md
Last active April 11, 2022 16:14
k3s for local testing

K3s for local testing/CI

The following is a docker-compose k3s configuration, with a script to pre-populate images in the cluster, allowing complete tear-down and rebuild very quickly.

This could be useful for spinning up a small cluster during CI jobs, as it allows the images to be cached rather than pulled into the newly built cluster for each and every job.

Results from local testing:

| | No preloaded images | Preloaded images |

OpenAPI Combinator

Web app which combines OpenAPI specifications into a single specification.

Requirements:

fastapi ~= 0.63.0
httpx ~= 0.16.1
aiocache ~= 0.11.1
@jacksmith15
jacksmith15 / searchable_dict.py
Last active January 18, 2021 15:47
Searchable dictionary, allowing fast substring matching on keys.
"""Searchable dictionary, allowing fast substring matching on keys.
Requires:
- Python 3.8 or greater
- pytrie 0.4.0 or greater
Example:
>>> dictionary = SearchableDict({
... "foo bar": 1,
"""This module implements a parser for query strings encoded with Ruby RestClient:
https://www.rubydoc.info/gems/rest-client/RestClient%2FUtils.encode_query_string
"""
import re
from urllib.parse import unquote
class ParameterTypeError(Exception):
"""Exception for mixed parameter types."""
"""A set of helpers for reversing urls, similar to Django ``reverse``.
Usage:
.. code:: python
@router.get("/class/{class_id}")
async def get_class(request: Request, class_id: int = Path(...)):
student_route = get_route(request.app, "list_students")
class_students_url = URLFactory(student_route).get_path(class_id=class_id)