# HG changeset patch # User Paul Boddie # Date 1444485578 -7200 # Node ID 86d83f74ef9a7e053a38b59c6e50762dc2c0866f # Parent 3f96e51bfacb811ae5d749ad14130e8a9c79ea29 Moved the get_combinations function into the extension module. diff -r 3f96e51bfacb -r 86d83f74ef9a optimiser.py --- a/optimiser.py Sat Oct 10 15:36:48 2015 +0200 +++ b/optimiser.py Sat Oct 10 15:59:38 2015 +0200 @@ -24,7 +24,6 @@ from os.path import split, splitext import EXIF import PIL.Image -import itertools import sys # Image operations. @@ -50,22 +49,6 @@ d.sort(reverse=True) return d -def get_combinations(c, n): - - """ - Get combinations of colours from 'c' of size 'n' in decreasing order of - probability. - """ - - all = [] - for l in itertools.combinations(c, n): - total = 0 - for f, value in l: - total += f - all.append((total, l)) - all.sort(reverse=True) - return [l for total, l in all] - def test(): "Generate slices of the colour cube." diff -r 3f96e51bfacb -r 86d83f74ef9a optimiserlib.py --- a/optimiserlib.py Sat Oct 10 15:36:48 2015 +0200 +++ b/optimiserlib.py Sat Oct 10 15:59:38 2015 +0200 @@ -21,6 +21,7 @@ """ from random import random, randrange +import itertools corners = [ (0, 0, 0), (255, 0, 0), (0, 255, 0), (255, 255, 0), @@ -172,6 +173,24 @@ def amplify_value(x, exp): return int(pow(x / 255.0, exp) * 255.0) +# Image operations. + +def get_combinations(c, n): + + """ + Get combinations of colours from 'c' of size 'n' in decreasing order of + probability. + """ + + all = [] + for l in itertools.combinations(c, n): + total = 0 + for f, value in l: + total += f + all.append((total, l)) + all.sort(reverse=True) + return [l for total, l in all] + # Exercise functions for Shedskin. if __name__ == "__main__": @@ -182,5 +201,6 @@ get_value(rgb, [(255, 255, 255), (255, 0, 0), (255, 255, 0), (0, 0, 0)]) combine([(1.0, (255, 0, 0)), (0.0, (0, 0, 0))]) clip(200.0) + get_combinations([(0.5, (255, 0, 0)), (0.25, (255, 255, 0)), (0.25, (0, 0, 0))], 2) # vim: tabstop=4 expandtab shiftwidth=4