Grid


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 generated Grid and number of items to generate
    if func_or_value is a value, the Grid will collapse to one element.
  • func_or_value
    if func_or_value is callable it is called as func(i) otherwise the value is 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 0
  • step
    Step size
  • size
    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 = 0
  • ratio
    Multiplication factor
  • size
    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 = 0
  • end
    Value at index = size
  • size
    Number of total elements to generate including start and end.
    size == 1 will return Grid({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 generate
  • low
    Lowest possible value
  • high
    Highest possible value
  • integer
    • True: only generate random Integers
    • False (default): generate random Floats
.fib(size, a, b) Creates a Fibonacci-like Grid

Parameters

  • size
    Number of elements to generate including a and b
  • a (default = 0)
    Value of index 0 which will be added to index 1 for the next value
  • b (default = 1)
    Value of index 1 which will be added to the result of a+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
Python
from GreasyPidgin.Grid import Grid
g = 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 = 77
print("quantised value for", testVal, "in f3 is",f3.quantise(77))