Skip to content

Instantly share code, notes, and snippets.

View mila's full-sized avatar

Miloslav Pojman mila

View GitHub Profile
mila /
Created January 16, 2023 20:59
Python decorator with TypeVar and @overload
import datetime as dt
from dataclasses import dataclass
from typing import Callable, Generic, Optional, TypeVar, overload
INPUT_T = TypeVar("INPUT_T")
class pass_thru_none(Generic[INPUT_T, OUTPUT_T]):
def __init__(self, func: Callable[[INPUT_T], OUTPUT_T]) -> None:
mila /
Created January 7, 2020 18:13
Python decorator that converts a method into a lazy property.
class cached_property:
"""Decorator that converts a method into a lazy property."""
# Inspired by Django and Werkzeug implementations.
def __init__(self, func):
self.func = func = func.__name__
self.__doc__ = getattr(func, '__doc__')
mila /
Last active September 4, 2020 09:15
Gotchas of Pandas Hierarchical indexing (MultiIndex)
Fun with Pandas Hierarchical indexing (MultiIndex)
The examples belows shows various gotchas and corner cases when working with Pandas MultiIndex.
All examples use following sample data:
>>> import pandas as pd
mila /
Created March 29, 2018 13:08
Extracts CSV from the tables in a HTML page (using regular expressions).
#!/usr/bin/env python3
import argparse
import csv
import re
table_open_re = re.compile('<table[^>]*>', re.IGNORECASE)
table_close_re = re.compile('</table[^>]*>', re.IGNORECASE)
tr_open_re = re.compile('<tr[^>]*>', re.IGNORECASE)
mila /
Last active March 29, 2018 10:50
Extracts CSV from the largest table in a HTML page.
#!/usr/bin/env python3
import argparse
import csv
import bs4
def read_stream(stream, features=None):
soup = bs4.BeautifulSoup(stream, features=features)
mila / index.html
Last active January 17, 2020 14:42
index.html + style.css
<!doctype html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Example Page</title>
<link rel="stylesheet" href="style.css">
mila / .gitignore
Created January 18, 2017 17:49
Git keep directory but ignore its contents.
import datetime
start =, 11, 1)
stop =, 12, 1)
delta = datetime.timedelta(days=1)
tpl = "year=%(year)04d/month=%(month)02d/week=%(week)02d/day=%(day)02d"
date = start
while date < stop:
mila /
Created October 14, 2016 10:56
Installs generated Debian package to remote system
# Install generated Debian package to remote system
# Uploads packages directly to the target servers ignoring
# any repositories and installs them using `dpkg -i`.
# Restarts System V / Systemd services after successful installation.