Skip to content

Instantly share code, notes, and snippets.

@zed
zed / KV.ebnf
Last active March 6, 2017 10:01
# $ grako -o kv_parser.py KV.ebnf
@@grammar :: KV
# whitespace inside a string excluding the start/end of the line
@@whitespace :: /((?!^)(?![\r\n])\s)+/
@@eol_comments :: /#[^\r\n]*/
@@left_recursion :: False
# input is zero or more assignments
#!/usr/bin/env python3
r"""Parse space-separated key=value pairs.
Format from http://ru.stackoverflow.com/a/635295/23044
>>> from pprint import pprint
>>> pprint(parse(r'a=1 b="2" c=3.123 d=[1, 2, 3] e={"Ke\n":["h=4"],"Key2":{" ":1}}'))
{'a': 1,
'b': '2',
'c': 3.123,
@zed
zed / .gitignore
Last active March 28, 2017 09:30
shorten url using goo.gl
.cache*
@zed
zed / .gitignore
Last active January 31, 2018 18:23
/.cachedir/
/sites.json
@zed
zed / beep.py
Last active November 3, 2016 03:54
beep
#!/usr/bin/env python
"""beep the pc speaker in the console.
Enable pc speaker:
$ sudo modprobe pcspkr
From https://github.com/hilbix/kdmktone
See also http://www.tldp.org/LDP/lpg/node83.html
"""
@zed
zed / leapseconds.py
Last active September 23, 2025 11:05
leap seconds
#!/usr/bin/env python
"""Get TAI-UTC difference in seconds for a given time using tzdata.
i.e., find the number of seconds that must be added to UTC to compute
TAI for any timestamp at or after the given time[1].
>>> from datetime import datetime
>>> import leapseconds
>>> leapseconds.dTAI_UTC_from_utc(datetime(2005, 1, 1))
datetime.timedelta(seconds=32)
@zed
zed / .gitignore
Last active September 4, 2019 16:38
next leap second IERS-OP web service client
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
@zed
zed / binary16.py
Last active July 30, 2022 00:19
IEEE 754 floating-point binary16
#!/usr/bin/env python
# -*- coding: utf-8 -*-
r"""Support for IEEE 754 half-precision binary floating-point format: binary16.
>>> [binary16(f) for f in [0.006534, -.1232]] == [b'\xb0\x1e', b'\xe2\xaf']
True
"""
from __future__ import division
import struct
from math import copysign, frexp, isinf, isnan, trunc
#!/usr/bin/env python
"""Play a media (audio/video) file using GStreamer.
To install Gstreamer and its Python bindings on Ubuntu, run:
$ sudo apt-get install python-gi python3-gi \
gstreamer1.0-tools \
gir1.2-gstreamer-1.0 \
gir1.2-gst-plugins-base-1.0 \
gstreamer1.0-plugins-good \
@zed
zed / url2filename.py
Last active July 25, 2025 07:23
Extract filename from an url
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import posixpath
try:
from urlparse import urlsplit
from urllib import unquote
except ImportError: # Python 3
from urllib.parse import urlsplit, unquote