RingSeq
Info
Listed here below are methods in the form RingSeq(Seq).method(...)
via the wrapper class RingSeq
.
The same exact methods exist in their original form method(Seq, ...)
and are available in the ring_seq.methods
module.
ring_seq.RingSeq.RingSeq
Wrapper class for circular methods.
Use this class to enable dot notation.
Attributes:
Name | Type | Description |
---|---|---|
underlying |
The wrapped sequence. |
Source code in src/ring_seq/RingSeq.py
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
|
__init__(underlying)
Initializes the instance with the sequence.
Source code in src/ring_seq/RingSeq.py
27 28 29 |
|
apply_o(i)
Gets the element at some circular index.
Examples:
>>> RingSeq('ABC').apply_o(-1)
'C'
>>> RingSeq('ABC').apply_o(3)
'A'
>>> 'ABC'[-1]
'C'
>>> 'ABC'[3]
IndexError: string index out of range
Notes
As shown in the examples, behaves differently from standard method [i]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
IndexO
|
circular index |
required |
Returns:
Type | Description |
---|---|
Any
|
The element at circular index |
Source code in src/ring_seq/RingSeq.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
index_from(i)
Normalizes a given circular index of a sequence.
Examples:
>>> RingSeq('ABC').index_from(-1)
2
>>> RingSeq('ABC').index_from(3)
0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
IndexO
|
circular index |
required |
Returns:
Type | Description |
---|---|
Index
|
A standard index |
Raises:
Type | Description |
---|---|
ArithmeticError
|
An error occurs if the sequence is empty. |
Source code in src/ring_seq/RingSeq.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
index_o(x, start=0, end=maxsize)
Gets the index of the first occurrence of a sub-sequence.
Examples:
>>> RingSeq('ABC').index_o('B', 2, 7)
1
>>> 'ABC'.index('B', 2, 7)
ValueError: substring not found
>>> RingSeq('ABC').index_o('BCAB', 2, 8)
1
Notes
Given the definition of circular sequence, the searched slice can contain more elements than the sequence itself.
As shown in the examples, behaves differently from standard method index(x[, i[, j]])
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Any
|
sub-sequence to be found, can be a |
required |
start |
IndexO
|
circular index where the search starts |
0
|
end |
IndexO
|
circular index where the search ends |
maxsize
|
Returns:
Type | Description |
---|---|
Index
|
A standard index |
Raises:
Type | Description |
---|---|
Value error
|
An error occurs if the sub-sequence is invalid or not found. |
Source code in src/ring_seq/RingSeq.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
is_reflection_of(that)
Tests whether this circular sequence is a reflection of a given sequence.
Examples:
>>> RingSeq('ABC').is_reflection_of('ACB')
True
>>> RingSeq('ABC').is_reflection_of('ABC')
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
that |
Seq
|
sequence to be compared |
required |
Returns:
Type | Description |
---|---|
bool
|
True if equal to any reflection of that |
Source code in src/ring_seq/RingSeq.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
is_reversion_of(that)
Tests whether this circular sequence is a reversion of a given sequence.
Examples:
>>> RingSeq('ABC').is_reversion_of('CBA')
True
>>> RingSeq('ABC').is_reversion_of('ABC')
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
that |
Seq
|
sequence to be compared |
required |
Returns:
Type | Description |
---|---|
bool
|
True if equal to any reversion of that |
Source code in src/ring_seq/RingSeq.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
is_rotation_of(that)
Tests whether this circular sequence is a rotation of a given sequence.
Examples:
>>> RingSeq('ABC').is_rotation_of('BCA')
True
>>> RingSeq('ABC').is_rotation_of('ABC')
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
that |
Seq
|
sequence to be compared |
required |
Returns:
Type | Description |
---|---|
bool
|
True if equal to any rotation of that |
Source code in src/ring_seq/RingSeq.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
|
is_rotation_or_reflection_of(that)
Tests whether this circular sequence is a rotation and/or reflection of a given sequence.
Examples:
>>> RingSeq('ABC').is_rotation_or_reflection_of('BAC')
True
>>> RingSeq('ABC').is_rotation_or_reflection_of('ABC')
True
Parameters:
Name | Type | Description | Default |
---|---|---|---|
that |
Seq
|
sequence to be compared |
required |
Returns:
Type | Description |
---|---|
bool
|
True if equal to any combination of rotation and reflection of that |
Source code in src/ring_seq/RingSeq.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
|
reflect_at(i=0)
Reflects the sequence to start at some circular index.
Examples:
>>> RingSeq('ABC').reflect_at()
'ACB'
>>> RingSeq('ABC').reflect_at(1)
'BAC'
Notes
reflect_at(-1)
is equivalent to reversed
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
IndexO
|
circular index where the reflected sequence starts |
0
|
Returns:
Type | Description |
---|---|
Seq
|
The reflected sequence |
Source code in src/ring_seq/RingSeq.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
|
reflections()
Computes all the reflections of this circular sequence
Examples:
>>> list(RingSeq('ABC').reflections())
['ABC', 'ACB']
>>> list(RingSeq('').reflections())
[]
Returns:
Type | Description |
---|---|
Iterator[Seq]
|
The sequence and its reflection |
Source code in src/ring_seq/RingSeq.py
215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
reversions()
Computes all the reversions of this circular sequence
Examples:
>>> list(RingSeq('ABC').reversions())
['ABC', 'CBA']
>>> list(RingSeq('').reversions())
[]
Returns:
Type | Description |
---|---|
Iterator[Seq]
|
The sequence and its reversion |
Source code in src/ring_seq/RingSeq.py
229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
rotate_left(step)
Rotates the sequence to the left by some steps.
Examples:
>>> RingSeq('ABC').rotate_left(1)
'BCA'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step |
int
|
number of rotation steps to the left |
required |
Returns:
Type | Description |
---|---|
Seq
|
The rotated sequence |
Source code in src/ring_seq/RingSeq.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
|
rotate_right(step)
Rotates the sequence to the right by some steps.
Examples:
>>> RingSeq('ABC').rotate_right(1)
'CAB'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
step |
int
|
number of rotation steps to the right |
required |
Returns:
Type | Description |
---|---|
Seq
|
The rotated sequence |
Source code in src/ring_seq/RingSeq.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
|
rotational_symmetry()
Computes the order of rotational symmetry possessed by this circular sequence.
Examples:
>>> RingSeq('-|--|--|--|-').rotational_symmetry()
4
>>> RingSeq('-|+-|+-|+-|+').rotational_symmetry()
4
Returns:
Type | Description |
---|---|
int
|
The rotational symmetry order, that is the number >= 1 of rotations |
int
|
in which a circular sequence looks exactly the same |
Source code in src/ring_seq/RingSeq.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
rotations()
Computes all the rotations of this circular sequence
Examples:
>>> list(RingSeq('ABC').rotations())
['ABC', 'BCA', 'CAB']
>>> list(RingSeq('').rotations())
[]
Returns:
Type | Description |
---|---|
Iterator[Seq]
|
The sequence and its rotations, 1 step at a time to the left |
Source code in src/ring_seq/RingSeq.py
173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
rotations_and_reflections()
Computes all the rotations and reflections of this circular sequence
Examples:
>>> list(RingSeq('ABC').rotations_and_reflections())
['ABC', 'BCA', 'CAB', 'ACB', 'CBA', 'BAC']
>>> list(RingSeq('').rotations_and_reflections())
[]
Returns:
Type | Description |
---|---|
Iterator[Seq]
|
The sequence and its rotations, and their reflections |
Source code in src/ring_seq/RingSeq.py
243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
slice_o(start, end, step=1)
Selects an interval of elements.
Examples:
>>> RingSeq('ABC').slice_o(-1, 5)
'CABCAB'
>>> 'ABC'[-1:5]
'C'
>>> RingSeq('ABC').slice_o(-1, 5, 2)
'CBA'
>>> 'ABC'[-1:5:2]
'C'
Notes
Given the definition of circular sequence, a slice can contain more elements than the sequence itself.
As shown in the examples, behaves differently from standard methods [i:j]
and [i:j:k]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start |
IndexO
|
circular index where the slice starts |
required |
end |
IndexO
|
circular index where the slice ends |
required |
step |
int
|
number of steps for filtering |
1
|
Returns:
Type | Description |
---|---|
Seq
|
The sliced sequence, with only the first element every each step |
Raises:
Type | Description |
---|---|
ValueError
|
An error occurs if slice step is zero. |
Source code in src/ring_seq/RingSeq.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
start_at(i)
Rotates the sequence to start at some circular index.
Examples:
>>> RingSeq('ABC').start_at(1)
'BCA'
Notes
Is equivalent to rotate_left
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
IndexO
|
circular index where the sequence starts |
required |
Returns:
Type | Description |
---|---|
Seq
|
The rotated sequence |
Source code in src/ring_seq/RingSeq.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
symmetry()
Computes the order of reflectional (mirror) symmetry possessed by this circular sequence.
Examples:
>>> RingSeq('-|--|--|--|-').symmetry()
4
>>> RingSeq('-|+-|+-|+-|+').symmetry()
0
Notes
Reflectional symmetry is always lower or equal than rotational symmetry.
Returns:
Type | Description |
---|---|
int
|
The reflectional (mirror) symmetry order, that is the number >= 0 of reflections |
int
|
in which a circular sequence looks exactly the same |
Source code in src/ring_seq/RingSeq.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
|
symmetry_indices()
Finds the indices of each element of this circular sequence close to an axis of reflectional symmetry.
Examples:
>>> RingSeq('-|--|--|--|-').symmetry_indices()
[1, 4, 7, 10]
>>> RingSeq('-|+-|+-|+-|+').symmetry_indices()
[]
Returns:
Type | Description |
---|---|
list[Index]
|
The indices of each element close to an axis of reflectional symmetry, |
list[Index]
|
that is a line of symmetry that splits the sequence in two identical halves |
Source code in src/ring_seq/RingSeq.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
|