Skip to content

Instantly share code, notes, and snippets.

@zed
zed / ping_pong.c
Last active January 30, 2023 07:05
Native messaging "ping_pong" example in C
/*** Native messaging "ping_pong" example in C.
*
* It is reimplementation of the corresponding Python example from
* https://github.com/mdn/webextensions-examples/tree/master/native-messaging
*
* # Python 3.x version
* # Read a message from stdin and decode it.
* def getMessage():
* rawLength = sys.stdin.buffer.read(4)
* if len(rawLength) == 0:
/*.json
/*.xml
@zed
zed / __main__.py
Last active January 4, 2021 11:46
#!/usr/bin/env python3
"""
- read subprocess output without threads using a socket pair
- show the output in a tkinter GUI (while the process is still running)
- stop subprocess on a button press
"""
import logging
import os
import socket
import sys
@zed
zed / ytcallback-server.py
Last active April 7, 2024 21:26
Send telegrams about new videos on subscribed YouTube channels.
#!/usr/bin/env python3
"""Send telegrams about new videos on subscribed YouTube channels.
To try it, install dependencies:
$ python3 -mpip install aiohttp feedparser werkzeug
run the callback server:
$ env TELEBOT_TOKEN=<token> TELEBOT_CHAT_ID=<chat_id> python3 ytcallback-server.py
@zed
zed / test_josephus3.py
Last active September 23, 2017 18:26
#!/usr/bin/env python3
"""Closed formula (safe_position3(n)) for Josephus3 problem for 1<=n<2**31"""
from decimal import Decimal, getcontext, localcontext
def safe_position3(n, *, precision=getcontext().prec):
# [jos]: http://user.math.uzh.ch/halbeisen/publications/pdf/jos.pdf
# j(n, k, n - l) = (n - c_m) * k + d_m;
# c_m <= n < c_m_plus_1
# return j(n, 3, n) + 1;
#!/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()
@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
/** 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
/scheme.png
@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