Skip to content

Instantly share code, notes, and snippets.

@zed
zed / time33.py
Last active April 15, 2019 09:04
time.process_time() and time.perf_counter() for Python 2 on Ubuntu.
"""time.process_time() and time.perf_counter() for Python 2 on Ubuntu."""
import ctypes
import errno
from ctypes.util import find_library
from functools import partial
CLOCK_PROCESS_CPUTIME_ID = 2 # time.h
CLOCK_MONOTONIC_RAW = 4
clockid_t = ctypes.c_int
@ju-popov
ju-popov / timestamp.py
Last active April 7, 2021 16:09
Python DateTime / Timestamp Convertion
######################################################################
# CURRENT AWARE LOCAL DATETIME
######################################################################
from datetime import datetime
from tzlocal import get_localzone
local_tz = get_localzone()
local_dt = datetime.now(local_tz)

Create a Postgis timezone DB

Ubuntu's analog of robcowie / postgis_timezone_db.markdown

Install postgis

sudo apt-get install python-software-properties
sudo apt-add-repository ppa:ubuntugis/ppa
sudo apt-get update

sudo apt-get install postgresql-9.1-postgis

#!/usr/bin/env python
"""Merge sort a singly linked linear list."""
import random
from itertools import product
# Linked list is either empty or a value and a link to the next list
empty = None # empty list
class LL(object):
__slots__ = "value", "next"
@audreyfeldroy
audreyfeldroy / pypi-release-checklist.md
Last active May 20, 2025 07:59
My PyPI Release Checklist
  • Update HISTORY.md
  • Commit the changes:
git add HISTORY.md
git commit -m "Changelog for upcoming release 0.1.1."
  • Update version number (can also be minor or major)
bumpversion patch
@zed
zed / try_named_pipe.py
Last active September 24, 2016 09:42
#!/usr/bin/env python3
"""Emulate bash:
$ cat /tmp/fifo.tub &
$ gunzip -c /tmp/filedata.dat.gz > /tmp/fifo.tub
http://stackoverflow.com/questions/19859283/python-subprocess-hangs-with-named-pipes
"""
import os
from contextlib import contextmanager
/* pipe/fork/dup2/execve exercise.
The results are similar to: `sort -u --parallel 2 < input_file`
http://stackoverflow.com/questions/19876980/create-2-child-processes-to-sort-words-of-a-file-using-pipe
Usage:
$ gcc -o sort-parallel-uniq *.c && ./sort-parallel-uniq < input_file
*/
#!/usr/bin/env python
"""Demo: use a socket as subprocesses stdin/stdout.
http://stackoverflow.com/a/19961290
"""
import socket
from contextlib import closing
from subprocess import Popen, PIPE
host = "stackoverflow.com"
/** Pipeline between three processes.
http://stackoverflow.com/q/20056084/
See @chill's answer: http://stackoverflow.com/a/8092270
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#!/usr/bin/env python
"""Print words containing the same letter at least three times in a row.
http://stackoverflow.com/questions/20170022/elongated-word-check-in-sentence
"""
import re
words = open('/usr/share/dict/words')
#NOTE: support only ascii letters
elongated = re.compile(r'(?i)([a-z])\1{2,}').search