Skip to content

Instantly share code, notes, and snippets.

View birkin's full-sized avatar

Birkin James Diana birkin

View GitHub Profile
@birkin
birkin / uv_workshop.md
Last active July 8, 2025 16:41
`uv` unconference workshop description

Introducing uv

Python is a friendly language, used by a wide-variety of folk in the Library -- from back-end developers to folk exploring digital-humanities text-mining and web-scraping to others exploring AI to others doing data-cleanup.

But too often there's friction in the process of getting code running because of the need to install:

  • various versions of python
  • a virtual-environment (venv)
  • various packages.

This workshop -- no programming experience required -- will introduce the python package-manager "uv", which has become incredibly popular because it removes lots of that friction, allowing people to focus more on their work/exploration/research, rather than "setup". It also helps collaboration, by making it easier for others using uv to use your scripts.

@birkin
birkin / load_gsheet_data.py
Created July 4, 2025 16:15
access public google spreadsheet
# /// script
# requires-python = "==3.12.*"
# dependencies = [
# "polars",
# "httpx"
# ]
# ///
"""
Example of accessing a public google-sheet ("Anyone with the link can view")
@birkin
birkin / secure_id_maker.py
Created May 11, 2025 02:25
unique strings
# /// script
# requires-python = "==3.12.*"
# ///
"""
Generates a secure unique ID
Usage:
uv run ./unique_id.py # default length is 10
uv run ./unique_id.py --length 20
@birkin
birkin / lxml_install_test.py
Created May 7, 2025 16:41
lxml old-server install test
# /// script
# requires-python = "==3.8.*"
# dependencies = ["lxml==4.9.1"]
# ///
"""
Script to test lxml installation on redhat-7 server.
Uses python PEP-723 inline-script-metadata to allow uv to run without venv installs.
@birkin
birkin / exempt_path_suggestion.md
Created April 22, 2025 14:50
thought about handling exempt-paths for django turnstile middleware.

exempt-paths refactor suggestion

idea

Based on thinking and research, below is a suggestion for handling exempt-paths in turnstile_middleware.py.

Some nice things about this...

  • the regex-compilation happens just once, when the webapp first loads, improving speed.
  • it allows us to change a .env setting easily, without deploying new code.
  • it allows the pattern-match code in turnstile_middleware.py to be simple, becase regex is handling the "starts-with" or "anywhere-within" check.
@birkin
birkin / pydantic_example.py
Last active March 14, 2025 18:07
pydantic example
# /// script
# requires-python = "==3.12.*"
# dependencies = ["pydantic==2.10.*"]
# ///
"""
Pydantic example
- Pydantic is a data validation package for Python. Using rust, it's fast.
- <https://docs.pydantic.dev/latest/>
@birkin
birkin / script_hack_django_sqlite.sh
Last active December 12, 2024 22:55
hack to get newish version of django to work with old version of sqlite.
#!/bin/bash
## Purpose:
## This script hacks Django's sqlite package to enable it to work with an old version
## of sqlite3 on an old version of RedHat.
##
## Flow:
## It checks if django and pysqlite3 and pysqlite3-binary are installed in the virtual environment.
## If the required packages are not found, the script alerts the user and exits.
## If the packages are found, it proceeds to update Django's `sqlite3/base.py` file.
@birkin
birkin / render_xml_via_template.py
Created November 15, 2024 19:23
example of rendering xml via django template
"""
Generates XML output using Django's template system.
Accepts command-line arguments to specify a person's name and role.
Usage:
$ uv run "./render_xml_via_template.py" --person "Birkin" --role "cheerleader"
Output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
@birkin
birkin / pdf_conformance_suggestions.md
Created October 21, 2024 19:54
pdf conformance ChatGPT suggestions

My October-2024 question

(continuation from irrelevant code-thread)...

If I had a PDF that conformed to this standard, and wanted to indicate that in metadata, what would be the a common digital-repository metadata xml spec where I'd list this conformance? And please provide an example or two of what the entry might look like.


ChatGPT response...

@birkin
birkin / pdf_edit_via_pikepdf.py
Created October 21, 2024 18:35
edit PDF DecodeParms dict
import pikepdf
def edit_decodeparms(pdf_path, output_path):
with pikepdf.open(pdf_path) as pdf:
for page_num, page in enumerate(pdf.pages, start=1):
resources = page.get('/Resources', {})
xobjects = resources.get('/XObject', {})
for xobj_name, xobj_ref in xobjects.items():
xobj = xobj_ref # Use the object directly
filters = xobj.get('/Filter', [])