Skip to content

Instantly share code, notes, and snippets.

View mara004's full-sized avatar
💭

mara004

💭
View GitHub Profile
@mara004
mara004 / 01_win_opensource.md
Last active September 19, 2024 23:30
Interesting Windows(-only) software
@mara004
mara004 / labelling.py
Last active September 13, 2024 15:07
Enumerate data using letters rather than numbers
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: MPL-2.0
from string import ascii_uppercase as ALPHABET
N_CHARS, ORD_A = 26, 65 # len(ALPHABET), ord("A")
def idx_to_label(i):
count, remainder = divmod(i, N_CHARS)
char = ALPHABET[remainder] # chr(remainder + ORD_A)
@mara004
mara004 / deferred_imports.py
Last active September 4, 2024 00:04
Various attempts at deferred ("lazy") imports. None of these seems particularly satisfying, though. Missing PEP 690...
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause OR MPL-2.0
import sys
import importlib.util
def v1_deferred_import(modpath):
# FIXME If modpath points to a submodule (e.g. PIL.Image), the parent module will be loaded immediately when this function is called. What's more, non-deferred imports of the submodule will break. This seems to be a nasty limitation of the importlib APIs used here.
@mara004
mara004 / ghostscript_shell.py
Last active August 26, 2024 23:35
PDF rendering with Ghostscript (via subprocess)
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-FileCopyrightText: 2024 James R. Barlow <james@purplerock.ca>
# SPDX-License-Identifier: MPL-2.0
# Initial code derived from ocrmypdf/_exec/ghostscript.py
# Note that Ghostscript is AGPL-licensed. However, we are calling it via subprocess here, so not sure whether copyleft would actually apply.
# See also https://www.gnu.org/licenses/gpl-faq.en.html#MereAggregation
import io
import os
@mara004
mara004 / pymupdf.py
Created July 11, 2024 20:10
PDF rendering with pymupdf
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: MPL-2.0
# Note that (py)mupdf is AGPL-licensed, so this code is altogether affected by copyleft
import PIL.Image
import fitz as pymupdf
def invoke_pymupdf(filepath, index, scale=4, rotation=0, password=None):
@mara004
mara004 / poppler_qt5.py
Last active July 13, 2024 14:31
PDF rendering with poppler-qt5
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: MPL-2.0
# Note that Poppler is GPL-licensed, so this code is altogether affected by copyleft
import io
import PIL.Image
from popplerqt5 import Poppler
from PyQt5.QtCore import QByteArray, QBuffer
@mara004
mara004 / poppler.py
Last active July 11, 2024 20:35
PDF rendering with python-poppler
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: MPL-2.0
# Note that Poppler is GPL-licensed, so this code is altogether affected by copyleft
import PIL.Image
import poppler # python-poppler
from poppler.cpp.page_renderer import render_hint
def _translate_rotation(rotation):
@mara004
mara004 / poppler_gtk.py
Last active September 6, 2024 18:40
PDF rendering with poppler-gtk
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: MPL-2.0
# Note that Poppler is GPL-licensed, so this code is altogether affected by copyleft
import math
from pathlib import Path
import PIL.Image
import cairo
import gi
@mara004
mara004 / pnp.py
Last active September 11, 2024 00:33
Page number spec parser [Draft]
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: MPL-2.0
# Sophisticated parser for a page number spec mini-language
# Technically, this might be a use case for some parser generator like pyparsing or PLY, but this is a manual implementation based on common string operations.
__all__ = ["parse_pagenums"]
import re
import logging
@mara004
mara004 / argparse_compat.py
Last active July 21, 2024 22:34
Argparse compat extensions
# SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
# SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
import sys
import argparse
if sys.version_info >= (3, 9):
from argparse import BooleanOptionalAction
else: