Usage examples
This page shows examples of how the library can be used.
Ring
class
Tip
The example Ring
class can be built in few lines of code, thanks to the methods in the original form method(Seq, ...)
available in the ring_seq.methods
module.
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 sequence. |
|
head_index |
The state of rotation, a circular index of where the sequence currently starts, default = 0 |
|
is_reflected |
The state of reflection |
Source code in src/ring_seq/examples/Ring.py
13 14 15 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 93 94 95 96 97 98 99 100 101 |
|
__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
21 22 23 24 25 |
|
current()
Gets the sequence at the current rotation and reflection state.
Examples:
>>> r = Ring('ABC')
>>> r.rotate_r(1)
>>> r.current()
'CAB'
Returns:
Type | Description |
---|---|
Seq
|
The current sequence |
Source code in src/ring_seq/examples/Ring.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
current_head()
Gets the start of the sequence at the current rotation state.
Examples:
>>> r = Ring('ABC')
>>> r.rotate_r(1)
>>> r.current_head()
'C'
Returns:
Type | Description |
---|---|
Any
|
The current head element of the sequence |
Source code in src/ring_seq/examples/Ring.py
72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
reflect()
Inverts the reflection state.
Examples:
>>> r = Ring('ABC')
>>> r.reflect()
>>> r.current()
'ACB'
Source code in src/ring_seq/examples/Ring.py
61 62 63 64 65 66 67 68 69 70 |
|
rotate_l(step=1)
Updates the rotation state by some steps to the left.
Examples:
>>> r = Ring('ABC')
>>> r.rotate_l(1)
>>> r.current()
'BCA'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step |
int
|
number of rotation steps to the left |
1
|
Source code in src/ring_seq/examples/Ring.py
47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
rotate_r(step=1)
Updates the rotation state by some steps to the right.
Examples:
>>> r = Ring('ABC')
>>> r.rotate_r(1)
>>> r.current()
'CAB'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step |
int
|
number of rotation steps to the right |
1
|
Source code in src/ring_seq/examples/Ring.py
33 34 35 36 37 38 39 40 41 42 43 44 45 |
|