Skip to content

Instantly share code, notes, and snippets.

Avatar

Eviatar Bach eviatarbach

View GitHub Profile
@eviatarbach
eviatarbach / fractional_powers.py
Last active Sep 15, 2016
A Python class for root-finding of sums of fractional powers
View fractional_powers.py
from fractions import gcd
from functools import reduce
import numpy
def lcm(a, b):
"""Return the least common denominator of two non-negative numbers."""
return a*b//gcd(a, b)
@eviatarbach
eviatarbach / closest_index_in_range.py
Last active Jan 9, 2016
A Python function that, given a lookup value, will find the index of the closest value in an equally-spaced list of numbers in constant time
View closest_index_in_range.py
def closest_index_in_range(lower, upper, step, value):
'''
Find the index of the closest value to `value` in the range
[lower, lower + step, ..., upper - step, upper] in constant time. `upper`
must be greater than `lower`. If `value` is outside the range, return the
corresponding boundary index (0 or the last index). When two values are
equally close, the index of the smaller is returned.
For example, `closest_index_in_range(-5, 5, 0.5, -4.8)` will return 0, since
-4.8 is closest to -5.
@eviatarbach
eviatarbach / matching_colours.py
Last active Aug 29, 2015
Get matching colours from a list
View matching_colours.py
#!/usr/bin/env python
# Copyright 2015 Eviatar Bach, eviatarbach@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
@eviatarbach
eviatarbach / takuzu.py
Last active Aug 29, 2015
A Takuzu solver implemented using Google's Constraint Programming solver, part of the or-tools project. A Takuzu board consists of a square grid of binary cells. There must be an equal number of 0s and 1s in every row and column, no duplicate rows or columns, and no more than two of the same bit consecutive in every row and column.
View takuzu.py
#!/usr/bin/env python
# Copyright 2013 Eviatar Bach, eviatarbach@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
@eviatarbach
eviatarbach / flickr_exif.py
Last active Mar 9, 2019
A Python script for downloading files from Flickr with EXIF metadata intact. This can already be done with images from Pro users who choose not to hide their original image; however, this will work for all others, downloading the largest image size possible. It has problems with photos that have numbered EXIF tags; this seems to be something Fli…
View flickr_exif.py
#!/usr/bin/env python
import urllib
import os
import argparse
import flickrapi
api_key = '367aed513876077c1cdcadb29d88ef02'
api_secret = '9b6e223653519900'
@eviatarbach
eviatarbach / tretyakov.py
Last active Aug 29, 2015
Tretyakov Gallery image download script. You must have ImageMagick installed, and run it in an empty directory since it creates files and will overwrite existing ones if they have the same name. You must call it with the image ID as the argument; for example, for http://www.tretyakovgallery.ru/en/collection/_show/image/_id/331, do python tretyak…
View tretyakov.py
#!/usr/bin/env python
import urllib
import sys
import re
import os
url = 'http://www.tretyakovgallery.ru/ajax/?action=fetch_image&id=%s' % sys.argv[1]
url_finder = re.compile("\['src'\] = '(.+?\.jpg)'")
images = urllib.urlopen(url).read()