View gist:d30dfc0679def54df81e3ed0d4aedd85
from __future__ import print_function, division
import time, sys
from gi.repository import Vips
time0 = time.time()
im = Vips.Image.new_from_file(sys.argv[1])
orig_width, orig_height = im.width, im.height
im = im.crop(0, 0, orig_height, orig_height)
View gist:8d69954e619a5961a37369275d6239c0
class Glob(object):
def change(glob):
glob.a = 2
print glob.a
View gist:5b5a08faaeced731d98bfe9c55059033
>>> import numpy
>>> D = numpy.array([[1, 0, 1, 0], [1, 0, 0, 0], [0, 1, 1, 1], [1, 1, 1, 1], [0, 0, 0, 0]], dtype=numpy.int8)
>>> D
array([[1, 0, 1, 0],
[1, 0, 0, 0],
[0, 1, 1, 1],
[1, 1, 1, 1],
[0, 0, 0, 0]], dtype=int8)
>>> D.size
"""Efficient sliding-window sorting of time-series data in CSV file.
Demo for
Tested on Python 3.5.
import collections
import csv
import datetime
"""Test how many threads we can run at once."""
import itertools
import threading
import time
import sys
import requests
"""Calculate the average number of moves in a snakes and ladders game.
Because as a parent one gets roped into these board (boring?) games
every so often, and I wanted to calculate the average duration of a
snakes and ladders game. Turns out it's about 36 moves (though
admittedly that's for a single-player game). :-)
> python
Played 10000 rounds, averaged 36.0559 moves, max 324 moves, took 0.508s
"""Calculate the probability of generating a duplicate random number after
generating "n" random numbers in the range "d".
Usage: python n [d=365]
Each value can either be an integer directly, or in the format "2**x", where
x is the number of bits in the value.
For example, to calculate the probability that two people will have the same
birthday in a room with 23 people:
"""An atomic, thread-safe incrementing counter."""
import threading
class AtomicCounter:
"""An atomic, thread-safe incrementing counter.
>>> counter = AtomicCounter()
>>> counter.increment()
"""Function to generate a random string (key) of length chars."""
import binascii
import os
def generate_key(length=40, get_bytes=os.urandom):
"""Return a randomly-generated key of length chars.
>>> len(generate_key())
"""Print most frequent N-grams in given file.
Usage: python filename
Problem description: Build a tool which receives a corpus of text,
analyses it and reports the top 10 most frequent bigrams, trigrams,
four-grams (i.e. most frequently occurring two, three and four word
consecutive combinations).