Skip to content

Instantly share code, notes, and snippets.

/** Print the _known_ (2013) perfect numbers less than *limit* given on stdin to stdout.
It uses gmplib (-lgmp) to support arbitrary precision integer arithmetic.
To compile and print all perfect numbers that are less than 100000000:
$ gcc print-perfect-numbers.c -lgmp && echo 100000000 | ./a.out
*/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
@zed
zed / parse_date_locale.py
Created October 24, 2014 07:20
Parse date based on locale :python:
# Answer for http://stackoverflow.com/questions/26528007/parse-date-based-on-locale-php-python
from datetime import date
import parsedatetime as pdt # $ pip install parsedatetime
def unix(utc_date, epoch_ord=date(1970, 1, 1).toordinal()):
return (utc_date.toordinal() - epoch_ord) * 86400
for date_string, locale in [("30 juin 2009", "fr_FR"),
("30.06.2009", "de_DE"),
#!/usr/bin/env python3
"""Generate data at constant rate (float interval).
To try, run:
$ python3 constant-bit-rate-modulo.py | pv >/dev/null
"""
from time import sleep, monotonic as timer
/** Enumerate integer partitions using Algorithm P from Knuth's TOACP V4 7.2.1.4
$ cc -std=c99 *.c && echo 4 | ./a.out
# Output:
4
3 + 1
2 + 2
2 + 1 + 1
1 + 1 + 1 + 1
*/
@zed
zed / procinfo.py
Last active August 29, 2015 14:10
#!/usr/bin/env python
"""Print info in json format about OS process specified by its pid.
Usage: procinfo PID
To install:
$ sudo apt-get install python-psutil # install dependencies
# https://pypi.python.org/pypi?:action=display&name=psutil#downloads
@zed
zed / url2filename.py
Last active July 25, 2025 07:23
Extract filename from an url
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import posixpath
try:
from urlparse import urlsplit
from urllib import unquote
except ImportError: # Python 3
from urllib.parse import urlsplit, unquote
#!/usr/bin/env python
"""Play a media (audio/video) file using GStreamer.
To install Gstreamer and its Python bindings on Ubuntu, run:
$ sudo apt-get install python-gi python3-gi \
gstreamer1.0-tools \
gir1.2-gstreamer-1.0 \
gir1.2-gst-plugins-base-1.0 \
gstreamer1.0-plugins-good \
#!/usr/bin/env python
"""Save all Unicode characters as individual images.
Based on @JackNova's answer http://stackoverflow.com/a/22612295
To save as jpeg, install libjpeg before installing Pillow e.g.:
$ sudo apt-get install libjpeg-dev -y
$ pip install -U pillow # assuming a virtualenv is active
import os
import asyncio
import sys
from asyncio.streams import StreamWriter, FlowControlMixin
reader, writer = None, None
@asyncio.coroutine
def stdio(loop=None):
#!/usr/bin/env python
"""Test functions from
http://stackoverflow.com/questions/28680896/how-can-i-get-the-3rd-friday-of-a-month-in-python
"""
import calendar
from datetime import date, datetime, time, timedelta
from itertools import islice
DAY = timedelta(1)