Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python3
"""Disable button on pressing it."""
import tkinter.ttk
root = tkinter.Tk()
button = tkinter.ttk.Button(text='Press Me Once',
command=lambda: button.state(['disabled']))
button.pack()
root.mainloop()
#!/usr/bin/env python
"""Emulate: grep -Ff <letters> [<words>]"""
import curses
import os
import sys
import re
if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()):
curses.setupterm()
@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;
@zed
zed / .gitignore
Last active August 27, 2017 20:01
/.cache/v/cache/lastfailed
#!/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
#!/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 / .gitignore
Last active September 10, 2022 17:42
setting EXTPROC on the master side works on Linux
/extproc
@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 / 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: