win_magnification.tools

@MaxBQb

Aug 11, 2023

Additional tools available for programmer
Author: MaxBQb
get_matrix_side(elements_count: int, dimension_count=2)[source]
Gets size of square matrix side
Example: linear matrix 9x9:
elements_count = 81
dimension_count = 2
Parameters
  • elements_count – Total count of elements in matrix

  • dimension_count – Array = 1, matrix = 2, cube = 3, etc.

Returns

Matrix rows/columns count

pos_for_matrix(array_len: int, *coords: int) int[source]
Get index of element in linear structure, defined by it’s coordinates.
Example:
>>> array = (
...   0, 0, 0,
...   0, 1, 2,
...   0, 0, 0,
... )
>>> array[pos_for_matrix(len(array), 1, 2)]
2
Parameters
  • array_len – Size fo linear structure

  • coords – Element coordinates

Returns

Index in array

get_extraction_pattern(matrix_size: int, *positions: Iterable[int])[source]
Generates sequence of linear positions from matrix coords
Example:
>>> list(get_extraction_pattern(9, (1, 1), (2, 2), (1, 2)))
[4, 8, 5]
Parameters
  • matrix_size – Amount of elements in matrix

  • positions – Sequence of coords

Returns

Sequence of linear coords

extract_from_matrix(matrix: Tuple[float, ...], *linear_positions: int)[source]
Get values from matrix at positions specified
Example:
>>> array = (
...   0, 0, 0,
...   0, 1, 2,
...   0, 3, 0,
... )
>>> pattern = get_extraction_pattern(len(array), (2, 1), (1, 1), (1, 2))
>>> extract_from_matrix(array, *pattern)
(3, 1, 2)
Parameters
  • matrix – Linear matrix

  • linear_positions – Indexes in matrix

Returns

Values from positions specified

matrix_to_str(matrix: Tuple[float, ...])[source]
Converts matrix to pretty string format
Example:
>>> matrix_to_str((2.0,))
'2.0'
>>> print(matrix_to_str((1.0,2.0,-3.9,4.0)))
 1.0  2.0
-3.9  4.0
Parameters

matrix – Array which sqrt(size) is natural number

print_matrix(matrix: Tuple[float, ...])[source]
Prints matrix in pretty format
Example:
>>> print_matrix((2.0,))
2.0
>>> print_matrix((1.0,2.0,-3.9,4.0))
 1.0  2.0
-3.9  4.0
Parameters

matrix – Array which sqrt(size) is natural number

get_transform_matrix(x=1.0, y=1.0, offset_x=0.0, offset_y=0.0) types.TransformationMatrix[source]
Creates screen transformation matrix
Example:
>>> print_matrix(get_transform_matrix(2.0, 8.0))
2.0  0.0  0.0
0.0  8.0  0.0
0.0  0.0  1.0
>>> print_matrix(get_transform_matrix(3.0, 4.0, 5.0, 6.0))
3.0  0.0 -5.0
0.0  4.0 -6.0
0.0  0.0  1.0
Parameters
  • x – Horizontal magnification

  • y – Vertical magnification

  • offset_x – Horizontal offset from left upper corner of window

  • offset_y – Vertical offset from left upper corner of window

Returns

Screen transformation matrix

get_filled_matrix(value=0.0, size=0) Tuple[float, ...][source]
get_simple_color_matrix(mul_red=1.0, mul_green=1.0, mul_blue=1.0, mul_alpha=1.0, add_red=0.0, add_green=0.0, add_blue=0.0, add_alpha=0.0) types.ColorMatrix[source]
Creates simple color transformation matrix
Example:
>>> print_matrix(get_simple_color_matrix(
...     1.1, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
... ))
1.1  0.0  0.0  0.0  0.0
0.0  2.0  0.0  0.0  0.0
0.0  0.0  3.0  0.0  0.0
0.0  0.0  0.0  4.0  0.0
5.0  6.0  7.0  8.0  1.0
Parameters
  • mul_red – Red color multiplier

  • mul_green – Green color multiplier

  • mul_blue – Blue color multiplier

  • mul_alpha – Colors transparency multiplier

  • add_red – Red color addendum

  • add_green – Green color addendum

  • add_blue – Blue color addendum

  • add_alpha – Colors transparency addendum

Returns

Color transformation matrix

Transition

Function with predefined transition matrix makes predefined start moved towards predefined end, with scale of value, which normally stays between 0 (start) and 1 (end) to get transition effect

alias of Callable[[Union[float, int]], Matrix.Linear]

get_transition(start: Tuple[float, ...], end: Tuple[float, ...]) Transition[source]
Make function which returns start moved towards end
with scale of value (param of that function):
value = 0 |=> start
value = 1 |=> end
0 <= value <= 1 |=> start moved towards end
value > 1 |=> start moved towards end out of end bound
value < 0 |=> end moved towards start out of start bound
Example:
>>> move = get_transition((0, 0, 0), (10, 10, 10))
>>> move(0)
(0.0, 0.0, 0.0)
>>> move(1)
(10.0, 10.0, 10.0)
>>> move(0.4)
(4.0, 4.0, 4.0)
>>> move(-0.4)
(-4.0, -4.0, -4.0)
>>> move(-1.4)
(-14.0, -14.0, -14.0)
Parameters
  • startInitial state

  • endFinal state

Returns

Transition function from start to end matrix

combine_matrices(first: Tuple[float, ...], second: Tuple[float, ...])[source]
Multiplies matrices, can be used to combine color transformations
Example:
>>> A = (1, 2, 3, 4)
>>> B = (5, 6, 7, 8)
>>> combine_matrices(A, B)
(19, 22, 43, 50)
>>> combine_matrices(A, (1,))
Traceback (most recent call last):
...
ValueError: Matrices must be the same size!
Parameters
  • first – Matrix A

  • second – Matrix B

Returns

A*B

Raises

ValueError – On matrices sizes mismatch

replace(func: Callable) Callable[source]
Decorator, replaces one function with another (func)
Example:
>>> def call():
...     print("call")
...
>>> @replace(call)
... def talk():
...     '''talk docs'''
...     print("talk")
>>> talk()
call
>>> talk.__doc__
'talk docs'
Parameters

func – Function which will replace any other

Returns

Wrapper, that acts like func

class Matrix[source]

Bases: object

Matrix wrapper

__str__()[source]
Converts matrix to string
Example:
>>> print(Matrix.from_linear((1.0,2.0,3.0,4.0)))
1.0  2.0
3.0  4.0
__matmul__(other: Union[Any, int, float])[source]
Multipies matrices
Example:
>>> print(Matrix.from_linear((1,2,3,4)) * Matrix.from_linear((5,6,7,8)))
19.0  22.0
43.0  50.0
>>> Matrix.from_linear((1.0,2.0,3.0,4.0)) @ "hello world"
Traceback (most recent call last):
...
TypeError: Can't multiply matrix and 'str'
Raises

TypeError – When unable to convert operand

__mul__(other: Union[Any, int, float])[source]
Multiply matrix and number/matrix
Example:
>>> print(Matrix.from_linear((1.0,2.0,3.0,4.0)) * 1)
1.0  2.0
3.0  4.0
>>> test_matrix = Matrix.from_linear((1.0,2.0,3.0,4.0))
>>> test_matrix *= -2
>>> print(test_matrix)
-2.0 -4.0
-6.0 -8.0
>>> print(-Matrix.from_linear((1.0,2.0,3.0,4.0)) * 2)
-2.0 -4.0
-6.0 -8.0
>>> print(Matrix.from_linear((1.0,2.0,3.0,4.0)) * Matrix.from_linear((5.0,6.0,7.0,8.0)))
19.0  22.0
43.0  50.0
__add__(other: Union[Any, int, float])[source]
Add matrix/number to matrix
Example:
>>> print(Matrix.from_linear((1.0,2.0,3.0,4.0)) + Matrix.from_linear((4.1,3.2,2.3,1.4)))
5.1  5.2
5.3  5.4
>>> print(Matrix.from_linear((1.0,2.0,3.0,4.0)) + 4)
5.0  6.0
7.0  8.0
>>> Matrix.from_linear((1.0,2.0,3.0,4.0)) + "hello world"
Traceback (most recent call last):
...
TypeError: Can't add 'str' to matrix
Raises

TypeError – When unable to convert operand

__sub__(other: Union[Any, int, float])[source]
Subtract matrix/number from matrix
Example:
>>> print(Matrix.from_linear((1.1,2.2,3.3,4.4)) - Matrix.from_linear((1.0,2.0,3.0,4.0)))
0.1  0.2
0.3  0.4
>>> print(Matrix.from_linear((1.0,2.0,3.0,4.0)) - 1)
0.0  1.0
2.0  3.0
>>> Matrix.from_linear((1.0,2.0,3.0,4.0)) - "hello world"
Traceback (most recent call last):
...
TypeError: Can't subtract 'str' from matrix
Raises

TypeError – When unable to convert operand

__neg__()[source]
Multiply matrix with -1
Example:
>>> print(-Matrix.from_linear((1.0,2.0,3.0,4.0)))
-1.0 -2.0
-3.0 -4.0
Linear

Matrix represented as flat tuple of floats

alias of Tuple[float, …]

Square

Matrix represented as tuple of tuples of floats

alias of Tuple[Tuple[float, …], …]

LinearLike

Matrix represented as collection of numbers

alias of Collection[Union[float, int]]

SquareLike

Matrix represented as collection of collections of numbers

alias of Collection[Collection[Union[float, int]]]

Any

Any matrix: SquareLike, LinearLike or Matrix itself

alias of Union[Collection[Union[float, int]], Collection[Collection[Union[float, int]]], Matrix]

property linear: Linear
Get/set linear matrix from/to raw value
Example:
>>> matrix = Matrix.from_linear((1,2,3,4))
>>> matrix.linear = [4, 5, 6.9, 7]
>>> matrix.linear
(4.0, 5.0, 6.9, 7.0)
>>> matrix.linear = [0]*5
Traceback (most recent call last):
...
ValueError: Linear matrix size mismatch
Expected 4, got 5
Accessors: Get Set
Raises

ValueError – On size of new linear matrix musmatch the old one

property square: Square
Get/set square matrix from/to raw value
Example:
>>> matrix = Matrix.from_linear((1,2,3,4))
>>> matrix.square = [
...     [4, 5],
...     [6.9, 7]
... ]
>>> matrix.linear
(4.0, 5.0, 6.9, 7.0)
>>> matrix.square
((4.0, 5.0), (6.9, 7.0))
>>> matrix.square = [0]*4
Traceback (most recent call last):
...
TypeError: 'int' object is not iterable
Accessors: Get Set
classmethod from_any(value: Any) Optional[Matrix][source]
Convert Linear/Square matrix to Matrix
Example:
>>> Matrix.from_any((1,2,3.5))
>>> print(Matrix.from_any((1,2,3.5,4)))
1.0  2.0
3.5  4.0
>>> Matrix.from_any(((1,2),(3,)))
>>> print(Matrix.from_any(((1,2),(3,4))))
1.0  2.0
3.0  4.0
>>> print(Matrix.from_any(Matrix.from_any(((1,2),(3,4)))))
1.0  2.0
3.0  4.0
Parameters

value (Matrix.Any) – Source of raw data

Returns

Filled matrix, or None on conversion fails

classmethod from_linear(value: LinearLike) Optional[Matrix][source]
Convert Linear matrix to Matrix
Example:
>>> print(Matrix.from_linear((1,2,3.5,4)))
1.0  2.0
3.5  4.0
>>> Matrix.from_linear(((1,2),(3,4)))
>>> Matrix.from_linear((1, 2))
Parameters

value – Source of raw data

Returns

Filled matrix, or None on conversion fails

classmethod from_square(value: SquareLike) Optional[Matrix][source]
Convert Square matrix to Matrix
Example:
>>> print(Matrix.from_square(((1,2),(3,4))))
1.0  2.0
3.0  4.0
>>> Matrix.from_square(((1,2),(3,)))
Parameters

value – Source of raw data

Returns

Filled matrix, or None on conversion fails