Skip to content

Instantly share code, notes, and snippets.

View adosib's full-sized avatar

Abdulah Sibalo adosib

View GitHub Profile
@adosib
adosib / grey_pdf_bg.py
Last active February 18, 2025 01:42
Super simple snippet that replaces white background in a pdf with a grey one using pymupdf
# To make those white background PDFs a little easier on the eyes
import pymupdf
doc = pymupdf.open('path/to/pdf-in.pdf')
background_color = (211/255, 211/255, 211/255)
for page in doc:
page.draw_rect(page.rect, color=None, fill=background_color, overlay=False)
doc.ez_save('path/to/pdf-out.pdf')
@adosib
adosib / icd10_regex.md
Created October 22, 2024 18:09
Regular expressions for validating ICD-10 PCS and ICD-10 CM codes. There aren't enough codes to really need a regex (~80k codes for each code set), BUT if latency is more important than being 100% correct (false positives possible), these regexes will do better than a database or cache lookup.

ICD-10 PCS

\b[0-9BCDFGHX][0-9A-HJ-NP-Z]{6}\b

False positives exist! E.g. “G0BBLDGˮ would be identified as an ICD10 PCS code with the expression even though it isnʼt one.

But no false negatives (tested against all 2023 CMS-approved codes):

In [13]: import re
In [14]: with open("icd10pcs_codes_2023.txt") as pcs:
BEGIN;
/* UDF:
Purpose: Extract just the text from a field storing HTML
*/
CREATE OR REPLACE FUNCTION udf_extract_text_from_html(html_content VARCHAR(MAX))
RETURNS varchar(max)
STABLE
@adosib
adosib / duckdb_initcap.sql
Last active December 20, 2024 13:33
INITCAP function (macro) for DuckDB
/*
Not thoroughly tested. DuckDB version used: 1.0.0
Won't work on NULLs, so input should be coalesced first.
I make no guarantees that this function will produce output equivalent to Postgres' INITCAP function.
Examples:
select INITCAP('do androids dream of electronic sheep?') => 'Do Androids Dream Of Electronic Sheep?'
select INITCAP('dJ D-wayne megens') => 'Dj D-Wayne Megens'
select INITCAP('Sa''ar 5-class corvette') => 'Sa'Ar 5-Class Corvette
select INITCAP(NULL) => SQL Error: Parameter Not Allowed Error: Cannot perform list_reduce on an empty input list