Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
"""Leave Start button pressed until Stop button is pressed. (ttk version)
http://stackoverflow.com/q/23353746/
"""
try:
import Tkinter as tk
import ttk
except ImportError: # Python 3
import tkinter as tk
@zed
zed / shuffle-sentences-in-a-question.py
Created May 5, 2014 12:39
Shuffle sentences in a StackOverflow question
#!/usr/bin/env python
"""Shuffle sentences in a StackOverflow question.
http://stackoverflow.com/q/19471967
"""
import random
import sys
import urllib
import requests # $ pip install requests
@zed
zed / countchars-compile-errors.ipynb
Created May 12, 2014 07:43
Demonstrate that L = [0] * n is a compile error if L is a C array
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@zed
zed / cat.py
Last active July 6, 2017 00:13
cat(1) in Python 3
#!/usr/bin/env python3
"""`cat` for Python 3.
- if a file has no newline at the end then the last line is merged
with the first line of the next file like `cat` does
- os.fsencode() is not used to encode error message therefore there
could be surrogates in the output (encoded using the default for
errors 'backslashreplace' error handler): it is always possible to
restore the original filename whatever (ascii-based) locale
@zed
zed / demo_normalize_replace.py
Created August 18, 2014 21:22
Demonstrate why pytz.timezone.normalize() may be useful after datetime.replace() in Python
"""Demonstrate why tz.normalize() may be useful after dt.replace()."""
from datetime import datetime
import pytz
# see Localized times and date arithmetic section in pytz docs
# http://pytz.sourceforge.net/#localized-times-and-date-arithmetic
eastern = pytz.timezone('US/Eastern')
dt = eastern.localize(datetime(2002, 10, 27, 7, 0, 0), is_dst=None)
unnormalized_dt = dt.replace(hour=0, minute=0, second=0)
print(unnormalized_dt.utcoffset())
@zed
zed / Makefile
Last active August 29, 2015 14:05
Fastest way to sum integers in text file
NAME := sum1Gints
CC := gcc -std=c89
INPUTFILE := /tmp/1G-int-per-line
.PHONY: clean run
run: $(NAME)
/usr/bin/time ./$< $(INPUTFILE)
# Recommended gcc warning options for C, from:
@zed
zed / csum_block.c
Last active October 3, 2020 08:11
Fastest way to sum integers in a file. Python `ctypes` multithreaded version.
/** To make shared library, run:
$ gcc -shared -O3 -o libcsum_block.so -fPIC csum_block.c
Algorithm from:
http://stackoverflow.com/questions/25606833/fastest-way-to-sum-integers-in-text-file/25607155#comment40046670_25607155
*/
#include <stdint.h>
#!/usr/bin/python3
"""Demonstrate that PySide can display Unicode characters.
To install on Ubuntu:
$ sudo apt-get install python3-pyside
"""
import PySide
print(PySide.__version__) # 1.2.1
import PySide.QtCore
// show that c99 isspace returns the same result for char and unsigned char
#include <algorithm>
#include <cctype>
#include <climits>
#include <iostream>
int main() {
for (char c = CHAR_MIN; ; ++c) { // iterate over all char values
unsigned char u = c;
if (::isspace(c) != ::isspace(u)) { // error
@zed
zed / .gitignore
Last active August 29, 2015 14:07
Measure time performance of slurp.{pl,py} scripts.
/1/
/2/