Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@zed
zed / child.py
Last active February 20, 2016 17:11
#!/usr/bin/env python
"""Called from parent.py"""
import sys
import time
try:
while True:
time.sleep(1)
except KeyboardInterrupt: # handle SIGINT
sys.exit('child exits on KeyboardInterrupt')
@zed
zed / __main__.py
Last active November 3, 2016 04:04
tkinter loop
#!/usr/bin/env python3
"""Show how to implement a loop using tkinter."""
import itertools
import tkinter
def loop(widget, delay_ms=300, chars=itertools.cycle('\-/|')):
print(next(chars), end='\r', flush=True)
widget.after(delay_ms, loop, widget, delay_ms)
@zed
zed / inline.py
Last active July 5, 2016 23:10 — forked from dcoles/inline.py
#!/usr/bin/python
# from http://stackoverflow.com/questions/19326409/python-ctype-segmentation-fault
import ctypes
shellcode_data = (b"\x6a\x3b\x58\x99\x48\xbb\x2f\x62\x69\x6e\x2f\x73\x68\x00\x53"
b"\x48\x89\xe7\x68\x2d\x63\x00\x00\x48\x89\xe6\x52\xe8\x10\x00"
b"\x00\x00\x2f\x75\x73\x72\x2f\x62\x69\x6e\x2f\x77\x68\x6f\x61"
b"\x6d\x69\x00\x56\x57\x48\x89\xe6\x0f\x05")
shellcode = ctypes.create_string_buffer(shellcode_data)
function = ctypes.cast(shellcode, ctypes.CFUNCTYPE(None))
@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 / beep-evdev.py
Last active November 5, 2016 18:13
Beep PC Speaker using Linux evdev API.
#!/usr/bin/env python
"""Beep PC Speaker using Linux evdev API.
To make /dev/input/by-path/platform-pcspkr-event-spkr device available, run:
root# modprobe pcspkr
"""
import ctypes
import math
import os
@zed
zed / .gitignore
Last active January 31, 2018 18:23
/.cachedir/
/sites.json
@zed
zed / .gitignore
Last active March 28, 2017 09:30
shorten url using goo.gl
.cache*
@zed
zed / collatz-conjecture.py
Created December 20, 2016 20:37
Collatz conjecture
#!/usr/bin/env python3
"""Collatz conjecture."""
def collatz(n):
assert n > 0
while n != 1:
yield n
if n & 1: # odd
n = 3*n + 1
else:
#!/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,