Skip to content

Instantly share code, notes, and snippets.

View tomschr's full-sized avatar

Tom Schraitle tomschr

View GitHub Profile
@tomschr
tomschr / atom.py
Created November 14, 2024 10:05
Atom definitions
from collections import namedtuple
Element = namedtuple('Element', 'symbol, z, weight')
ATOMS = {
'H': Element('H', 1, 1.008), # Wasserstoff
'D': Element('D', 1, 2.0141), # Deuterium
'He': Element('He', 2, 4.002602), # Helium
'Li': Element('Li', 3, 6.94), # Lithium
@tomschr
tomschr / watchdog_with_timer.py
Created March 20, 2024 07:42
Python Watchdog: trigger a function only when there are no more changes for a certain period
#!/usr/bin/python3
#
# Purpose:
# Use watchdog to monitor a directory. After a certain amount of time without any change
# the processing is triggered.
#
import os.path
import time
import threading
@tomschr
tomschr / example.json
Created July 26, 2023 08:52
Example of JSON-LD
{
"@context": "http://schema.org/",
"@type": "TechArticle",
"headline": "Working with systemd timers",
"abstract": "A complete overview of systemd timers that covers creating, maintaining, testing, troubleshooting and migrating from cron.",
"datePublished": "2023-02-28",
"dateModified": "2023-07-25",
@tomschr
tomschr / errors.log
Last active January 19, 2023 15:26
Error from transforming a big book into HTML
No titlepage template for: quote
No localization for keycap/keycap in en, using "MISSING"
No localization for keycap/keycap in en, using "MISSING"
Type error at char 11 in expression in xsl:variable/@select on line 94 column 57 of objects.xsl:
XPTY0004 An empty sequence is not allowed as the first argument of array:size()
invoked by xsl:iterate at file:/home/tom/repos/GH/tomschr/docbook-xslt3-fo/docbook-xslTNG-2.0.2/xslt/modules/objects.xsl#62
In template rule with match="element(Q{http://docbook.org/ns/docbook}mediaobject)" on line 23 of objects.xsl
invoked by xsl:apply-templates at file:/home/tom/repos/GH/tomschr/docbook-xslt3-fo/docbook-xslTNG-2.0.2/xslt/modules/blocks.xsl#20
In template rule with match="element(Q{http://docbook.org/ns/docbook}informalfigure)" on line 17 of blocks.xsl
invoked by xsl:apply-templates at file:/home/tom/repos/GH/tomschr/docbook-xslt3-fo/docbook-xslTNG-2.0.2/xslt/modules/sections.xsl#20
@tomschr
tomschr / xinclude.xsl
Last active August 20, 2021 15:41
XSLT stylesheet to process <xi:include/> elements and adds xml:base attribute on included root element
<?xml version="1.0" encoding="UTF-8"?>
<!--
Purpose:
Process xi:include elements and add xml:base into included root
element, pointing to the original file
Reason:
Neither xmllint nor lxml API provides something to get the origin
of an included file. For lxml, see
https://mailman-mail5.webfaction.com/pipermail/lxml/20130831/014852.html
#
# Example of a child loggers
#
import logging
from logging.config import dictConfig
LOGGING_CFG = {
"version": 1,
@tomschr
tomschr / docrate.py
Last active July 21, 2023 12:15
Proof-of-concept to retrieve (GET) and store (POST) ratings from documentation URLs similar to doc.suse.com
#!/usr/bin/env python3
"""
Proof-of-concept to retrieve (GET) and store (POST) ratings from
documentation URLs similar to doc.suse.com.
Requirements
------------
* aiohttp
* Python >=3.6, preferably a more recent version
@tomschr
tomschr / typehints.py
Created May 1, 2021 21:35
Typical type hints
# https://learning.oreilly.com/library/view/mastering-object-oriented-python/9781789531367/beafb66d-a1c5-444c-b22d-9ecf0d75e3f5.xhtml
from typing import Any, Callable, TypeVar, cast
FuncType = Callable[..., Any]
F = TypeVar('F', bound=FuncType)
def my_decorator(func: F) -> F:
...
@tomschr
tomschr / pathlib_and_json.py
Created May 1, 2021 15:11
Read and write JSON files with pathlib.Path
# Source
# Mastering Object-Oriented Python - Second Edition by Steven F. Lott Published by Packt Publishing, 2019
# https://learning.oreilly.com/library/view/mastering-object-oriented-python/9781789531367/c34be237-5ccd-4775-a0b0-ec1f7652f7bc.xhtml
#
from pathlib import Path
# write JSON files:
with Path("temp.json").open("w", encoding="UTF-8") as target:
json.dump(travel3, target, default=blog_j2_encode)
@tomschr
tomschr / nested_dict.py
Last active March 5, 2021 15:19
Nested dictionaries
"""
The "ddict" is able to create nested structures. With the help of "defaultdict",
it creates arbitrary levels of "dict"-like objects.
Run the doctests with:
$ python3 -m doctest nested_dict.py
>>> d = ddict(a=ddict(b=ddict(c=10)) )