Usage examples
This page shows examples of how the library can be used.
Ring class
Tip
The example Ring class demonstrates how a mutable ring view can be built on top of the
immutable RingSeq class — keeping separate rotation and reflection states, then combining
them to reproduce the current orientation.
ring_seq.examples.Ring.Ring
An example class wrapping a sequence and keeping mutable states of rotation and reflection.
Attributes:
| Name | Type | Description |
|---|---|---|
underlying |
The wrapped |
|
head_index |
The state of rotation, a circular index of where the sequence currently starts |
|
is_reflected |
The state of reflection |
Source code in src/ring_seq/examples/Ring.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
__init__(underlying, head_index=0, is_reflected=False)
Initializes the instance with the sequence and the states.
Source code in src/ring_seq/examples/Ring.py
25 26 27 28 29 30 | |
current()
Gets the sequence at the current rotation and reflection state, in the same concrete type as the original input.
Source code in src/ring_seq/examples/Ring.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
current_head()
Gets the start of the sequence at the current rotation state.
Examples:
>>> r = Ring([1, 2, 3])
>>> r.rotate_r(1)
>>> r.current_head()
3
Source code in src/ring_seq/examples/Ring.py
68 69 70 71 72 73 74 75 76 77 | |
reflect()
Inverts the reflection state.
Examples:
>>> r = Ring([1, 2, 3])
>>> r.reflect()
>>> r.current()
[1, 3, 2]
Source code in src/ring_seq/examples/Ring.py
57 58 59 60 61 62 63 64 65 66 | |
rotate_l(step=1)
Updates the rotation state by some steps to the left.
Examples:
>>> r = Ring([1, 2, 3])
>>> r.rotate_l(1)
>>> r.current()
[2, 3, 1]
Source code in src/ring_seq/examples/Ring.py
46 47 48 49 50 51 52 53 54 55 | |
rotate_r(step=1)
Updates the rotation state by some steps to the right.
Examples:
>>> r = Ring([1, 2, 3])
>>> r.rotate_r(1)
>>> r.current()
[3, 1, 2]
Source code in src/ring_seq/examples/Ring.py
35 36 37 38 39 40 41 42 43 44 | |