Grid is a lightweight extension of Python’s built-in set. It represents a collection of unique elements with additional helper methods for:
- structured generation (series, interpolation, random, Fibonacci, etc.)
- sorting
- type checking
- comparability testing
- quantisation (nearest value lookup)
Because Grid inherits from set, all standard set behavior applies: elements are unique, order is not guaranteed, membership testing is fast.
ConstrUction
Grid(List or Set or Tuple)
Grid can be initialized directly using a List, Set or Tuple. Be aware that it will remove duplicates from Lists and Tuples!
The following class methods are inspired by SuperCollider’s Array functions
.fill(size, func_or_value) Creates a Grid of size elements and iterates through the function or repeats the value
Parameters
size
Length of the generatedGridand number of items to generate
iffunc_or_valueis a value, theGridwill collapse to one element.func_or_value
iffunc_or_valueis callable it is called asfunc(i)otherwise thevalueis repeated
.series(start, step, size) Creates a Grid using an arithmetic progression
The arithmetic progression follows the formula start + step*i(size)
Automatically checks for number of decimal positions to avoid floating point errors
Parameters
start
Value at index 0step
Step sizesize
Number of elements to be generated including index 0
.geom(start, ratio, size) Creates a Grid using a geometric progression
The geometric progression follows the formula start * ratio^i(size)
Parameters
start
Value for index = 0ratio
Multiplication factorsize
Number of elements to generate
.interpolation(start, end, size) Creates a Grid that interpolates between start and end, including both values
start
Value at index = 0end
Value at index =sizesize
Number of total elements to generate including start and end.size == 1will returnGrid({start})
.rand(size, low, high, integer) Creates a Grid with random values
Especially in the case of a Grid filled with random Integers, the Grid will collapse in size if duplicates are generated!
Parameters
size
Number of elements to generatelow
Lowest possible valuehigh
Highest possible valueintegerTrue: only generate randomIntegersFalse(default): generate randomFloats
.fib(size, a, b) Creates a Fibonacci-like Grid
Parameters
size
Number of elements to generate including a and ba(default = 0)
Value of index 0 which will be added to index 1 for the next valueb(default = 1)
Value of index 1 which will be added to the result ofa+b
Methods
.sorted() Return the Grid as a sorted List.
Return: List
.quantise(value) Quantise a value to the Grid finding the closest element.
Value must be comparable to the elements of the Grid (e.g. a Number, not a String or Iterable)
Parameters
value
Value against which the Grid is compared
Return: Element from the Grid that is closest to the value (Integer or Float)
Example
from GreasyPidgin.Grid import Gridg = Grid([1, 3, 5, 7])p = Grid({2,4,6,8})q = Grid((1,2,3))t = Grid.fill(10, lambda i: 2**i)s1 = Grid.series(0, 1, 10)s2 = Grid.series(0.01, 0.1, 10)g = Grid.geom(442, 2**(1/12), 13)i = Grid.interpolation(117, 200.1, 5)r1 = Grid.rand(5,0,12.77)r2 = Grid.rand(5,0,13,integer=True)r3 = Grid.rand(5,0,12.77,integer=True)f1 = Grid.fib(10, 3,4)f2 = Grid.fib(10, 4,3)f3 = Grid.fib(12, 0.1, 7**(1/12))print("from List:\n", q)print("from Set:\n", p)print("from Tuplet:\n", q)print("from Function:\n", t)print("from arithmetic series:\n", s1)print("from arithmetic series(floats):\n", s2)print("from geometric series:\n", g.sorted())print("from interpolation:\n", i.sorted())print("from random(float):\n", r1)print("from random(integer):\n", r2)print("from random(forced integer):\n", r3)print("from Fibonacci-like Seq:\n", f1.sorted())print("from Fibonacci-like Seq (reverse order):\n", f2.sorted())print("from Fibonacci-like Seq (not limited to Integers):\n", f3.sorted())testVal = 77print("quantised value for", testVal, "in f3 is",f3.quantise(77))