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
/scheme.png
/** Convert Roman numerals given on the command line to decimals.
$ ragel -F0 -o roman_numerals.c roman_numerals.rl
$ gcc -finput-charset=utf-8 -fexec-charset=utf-8 -o roman_numerals roman_numerals.c
$ ./roman_numerals IX Ⅻ CCCC
9
12
400
$ ./roman_numerals IIX && exit 1 || echo invalid
invalid
@zed
zed / __main__.py
Last active June 3, 2017 10:59
Play Youtube video on remote VLC.
#!/usr/bin/env python
"""Play Youtube video on remote VLC.
Usage: play-youtube-video-on-remote-vlc <webpage-url>
Send media url to remote (desktop) VLC using (Lua) http interface
https://wiki.videolan.org/VLC_HTTP_requests/
"""
import base64
import os
#!/usr/bin/env python3
"""Print a random number of lines in response to each input line.
Terminate each response with 'END'.
"""
import sys
import random
def log(msg):
@zed
zed / README.md
Created June 11, 2017 19:59
Passwordless ssh from iPhone using StaSh for Pythonista for iOS

To generate ssh keys for a passwordless login, type on the phone in StaSh launched in Pythonista for iOS:

$ mkdir $STASH_ROOT/.ssh
$ cd $STASH_ROOT/.ssh
$ ssh-keygen
$ mail -s 'public ssh key' -f id_rsa.pub your_email@example.com

Then on the server, add id_rsa.pub from the e-mail, to be able to ssh from the phone without a password:

@zed
zed / README.md
Created June 15, 2017 23:32
namedtuple vs. dict micro-benchmark

namedtuple vs. dict micro-benchmark

$ python -m perf timeit --rigorous --duplicate 10 --hist --stats -s 'import collections; Point = collections.namedtuple("Point", "x y")' 'Point(10.5, 11.5)'
$ python -m perf timeit --rigorous --duplicate 10 --hist --stats '{"x": 10.5, "y": 11}'

Point(10.5, 11.5) is 6 times slower than {'x': 10.5, 'y':11.5}. The absolute times are 635 ns +- 26 ns vs. 105 ns +- 4 ns. Don't create classes at the function level unless you know why you need it. If your API requires dict than use dict -- it has nothing to do with performance.

@zed
zed / .gitignore
Last active September 10, 2022 17:42
setting EXTPROC on the master side works on Linux
/extproc
#!/usr/bin/env python3
"""Draw text in the center of the screen."""
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QLabel
app = QApplication(sys.argv)
w = QLabel()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Write Unicode text to a pdf file embedding font."""
from fpdf import FPDF # $ pip install fpdf
output_filename = 'unicode.pdf'
# http://unifoundry.com/pub/unifont-10.0.05/font-builds/unifont-10.0.05.ttf
font_path = 'unifont-10.0.05.ttf'
# https://helloworldcollection.github.io/#Human