istr-python 1.0.1__tar.gz → 1.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {istr_python-1.0.1 → istr_python-1.0.2}/PKG-INFO +51 -68
- {istr_python-1.0.1 → istr_python-1.0.2}/README.md +50 -67
- {istr_python-1.0.1 → istr_python-1.0.2}/istr/changelog.md +16 -0
- {istr_python-1.0.1 → istr_python-1.0.2}/istr/istr.py +22 -15
- {istr_python-1.0.1 → istr_python-1.0.2}/istr/readme.md +50 -67
- {istr_python-1.0.1 → istr_python-1.0.2}/istr_python.egg-info/PKG-INFO +51 -68
- {istr_python-1.0.1 → istr_python-1.0.2}/pyproject.toml +1 -1
- {istr_python-1.0.1 → istr_python-1.0.2}/istr/__init__.py +0 -0
- {istr_python-1.0.1 → istr_python-1.0.2}/istr_python.egg-info/SOURCES.txt +0 -0
- {istr_python-1.0.1 → istr_python-1.0.2}/istr_python.egg-info/dependency_links.txt +0 -0
- {istr_python-1.0.1 → istr_python-1.0.2}/istr_python.egg-info/top_level.txt +0 -0
- {istr_python-1.0.1 → istr_python-1.0.2}/setup.cfg +0 -0
- {istr_python-1.0.1 → istr_python-1.0.2}/tests/test_istr.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: istr-python
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.2
|
|
4
4
|
Summary: istr - strings you can count on
|
|
5
5
|
Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/salabim/istr
|
|
@@ -150,10 +150,10 @@ or even
|
|
|
150
150
|
four, five = istr(4, 5)
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
-
|
|
154
|
-
>
|
|
155
|
-
>
|
|
156
|
-
>
|
|
153
|
+
##### Important
|
|
154
|
+
>
|
|
155
|
+
> All calculations are strictly integer calculations. That means that if a float or decimal variable is ever produced it will be converted to an int.
|
|
156
|
+
> Also divisions are always floor divisions!
|
|
157
157
|
|
|
158
158
|
#### Use istrs as string
|
|
159
159
|
|
|
@@ -188,12 +188,13 @@ And
|
|
|
188
188
|
|
|
189
189
|
is also `istr('444')`
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
##### Note
|
|
192
|
+
|
|
192
193
|
>
|
|
193
194
|
> It is not allowed to use the `@` operator for two istrs. So, `four @ five` raises a TypeError.
|
|
194
|
-
|
|
195
195
|
#### istrs that can't be interpreted as an int
|
|
196
196
|
|
|
197
|
+
|
|
197
198
|
Although usualy, istrs are to be interpreted as an int, that's not a requirement.
|
|
198
199
|
|
|
199
200
|
So
|
|
@@ -234,7 +235,7 @@ is `False`.
|
|
|
234
235
|
|
|
235
236
|
|
|
236
237
|
|
|
237
|
-
The bool operator works normally on the integer value of an istr. So
|
|
238
|
+
The `bool` operator works normally on the integer value of an istr. So
|
|
238
239
|
|
|
239
240
|
`bool(istr('0'))` ==> `False`
|
|
240
241
|
`bool(istr('1'))` ==> `True`
|
|
@@ -251,6 +252,7 @@ For the `in` operator, an istr is treated as an ordinary string, although it is
|
|
|
251
252
|
```
|
|
252
253
|
'34' in istr(1234)
|
|
253
254
|
34 in istr(1234)
|
|
255
|
+
|
|
254
256
|
```
|
|
255
257
|
On the left hand side an istr is always treated as a string:
|
|
256
258
|
```
|
|
@@ -280,47 +282,39 @@ is
|
|
|
280
282
|
```
|
|
281
283
|
'0 1 2 3 4 5 6 7 8 9 10 11'
|
|
282
284
|
```
|
|
285
|
+
#### Using values that are neither string or numeric to initialize istr
|
|
283
286
|
|
|
284
|
-
|
|
285
|
-
Apart from with simple numeric (to be interpreted as an int) or str, istr can be initialized with
|
|
287
|
+
Apart from with numeric (to be interpreted as an int) or str, istr can be initialized with
|
|
286
288
|
several other types:
|
|
287
289
|
|
|
288
|
-
- if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
|
|
289
290
|
|
|
290
|
-
|
|
291
|
-
istr({0: 0, 1: 1, 2: 4}) ==> {0: istr('0'), 1: istr('1'), 2: istr('4')}
|
|
292
|
-
```
|
|
291
|
+
- if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
|
|
293
292
|
|
|
294
293
|
- if an iterator, the iterator will be mapped with istr
|
|
295
294
|
|
|
296
|
-
```
|
|
297
|
-
istr(i * i for i in range(3)) ==> <map object>
|
|
298
|
-
list(istr(i * i for i in range(3))) ==> [istr('0'), istr('1'), istr('4')]
|
|
299
|
-
```
|
|
300
|
-
|
|
301
295
|
- if an iterable, the same type will be returned with all elements istr'ed
|
|
302
296
|
|
|
303
|
-
|
|
297
|
+
```
|
|
304
298
|
istr([0, 1, 4]) ==> [istr('0'), istr('1'), istr('4')]
|
|
305
299
|
istr((0, 1, 4)) ==> (istr('0'), istr('1'), istr('4'))
|
|
306
|
-
istr({0, 1, 4}) ==> `{istr('4'), istr('0'), istr('1')} # or similar
|
|
307
|
-
|
|
300
|
+
istr({0, 1, 4}) ==> `{istr('4'), istr('0'), istr('1')} # or similar
|
|
301
|
+
```
|
|
308
302
|
|
|
309
303
|
- if a range, an istr.range instance will be returned
|
|
310
304
|
|
|
311
|
-
|
|
312
|
-
istr(range(3))
|
|
313
|
-
list(istr(range(3)))
|
|
314
|
-
len(istr(range(3)))
|
|
315
|
-
|
|
305
|
+
```
|
|
306
|
+
istr(range(3)) ==> istr.range(3)
|
|
307
|
+
list(istr(range(3))) ==> [istr('0'), istr('1'), istr('2')]
|
|
308
|
+
len(istr(range(3))) ==> 3
|
|
309
|
+
```
|
|
316
310
|
|
|
317
311
|
- if an istr.range instance, the same istr.range will be returned
|
|
318
312
|
|
|
319
313
|
- if an istr, the same istr will be used
|
|
320
314
|
|
|
321
|
-
|
|
315
|
+
```
|
|
322
316
|
istr(istr('4')) ==> istr ('4')
|
|
323
|
-
|
|
317
|
+
```
|
|
324
318
|
|
|
325
319
|
#### More than one parameter for istr
|
|
326
320
|
It is possible to give more than one parameter, in which case a tuple
|
|
@@ -355,7 +349,7 @@ istr(n100).all_distinct() ==> False
|
|
|
355
349
|
|
|
356
350
|
#### reverse an istr
|
|
357
351
|
|
|
358
|
-
The method `istr.reversed()` will return
|
|
352
|
+
The method `istr.reversed()` will return an istr with the reversed content:
|
|
359
353
|
```
|
|
360
354
|
istr(456).reversed() ==> istr('654')
|
|
361
355
|
istr('0456').reversed() ==> istr('6540')
|
|
@@ -365,13 +359,13 @@ The same can -of course- be achieved with
|
|
|
365
359
|
istr(456)[::-1] ==> istr('654')
|
|
366
360
|
istr('0456')[::-1] ==> istr('6540')
|
|
367
361
|
```
|
|
368
|
-
|
|
369
|
-
>
|
|
370
|
-
>
|
|
371
|
-
>
|
|
372
|
-
>
|
|
373
|
-
>
|
|
374
|
-
>
|
|
362
|
+
##### Note
|
|
363
|
+
>
|
|
364
|
+
> It is possible to reverse a negative istr, but the result can't be interpreted as an int anymore.
|
|
365
|
+
>
|
|
366
|
+
> ```
|
|
367
|
+
> istr(-456).reversed() ==> TypeError
|
|
368
|
+
> ```
|
|
375
369
|
|
|
376
370
|
#### enumerate with istrs
|
|
377
371
|
|
|
@@ -379,8 +373,8 @@ The `istr.enumerate` method can be used just as the builtin enumerate function.
|
|
|
379
373
|
The iteration counter however is an istr rather than an int. E.g.
|
|
380
374
|
|
|
381
375
|
```
|
|
382
|
-
|
|
383
|
-
|
|
376
|
+
for i, c in istr.enumerate('abc'):
|
|
377
|
+
print(f'{repr(i)} {c}')
|
|
384
378
|
```
|
|
385
379
|
prints
|
|
386
380
|
```
|
|
@@ -414,7 +408,8 @@ The given argument(s) result in a range of digits.
|
|
|
414
408
|
- `<n>` ==> n
|
|
415
409
|
- `<n-m>` ==> n, n+1, ..., m
|
|
416
410
|
- `-n>` ==> 0, 1, ... n
|
|
417
|
-
- `n->` ==> n, n+1, ..., 9
|
|
411
|
+
- `n->` ==> n, n+1, ..., 9 if n is numeric (0-9), n, n+1, ... Z if n is a letter
|
|
412
|
+
- `'-'` ==> 0, 1, ..., 9
|
|
418
413
|
- `''` ==> 0, 1, ..., 9
|
|
419
414
|
|
|
420
415
|
(n and m must be digits between 0 and 9 or letters letters between A and Z)
|
|
@@ -460,9 +455,9 @@ will print `jstr('20')`
|
|
|
460
455
|
|
|
461
456
|
It is possible to control the way an `istr` instance will be repr'ed.
|
|
462
457
|
|
|
463
|
-
By default,
|
|
458
|
+
By default, `istr(5)` is represented as `istr('5')`.
|
|
464
459
|
|
|
465
|
-
With the istr.repr_mode() context manager, that can be changed:
|
|
460
|
+
With the `istr.repr_mode()` context manager, that can be changed:
|
|
466
461
|
```
|
|
467
462
|
with istr.repr_mode('str'):
|
|
468
463
|
five = istr(5)
|
|
@@ -480,7 +475,7 @@ This will print
|
|
|
480
475
|
5
|
|
481
476
|
istr('5')
|
|
482
477
|
```
|
|
483
|
-
If the repr_mode is `'int'` and the istr can't be interpreted as an int the string
|
|
478
|
+
If the repr_mode is `'int'` and the istr can't be interpreted as an int the string `?` will be returned:
|
|
484
479
|
|
|
485
480
|
```
|
|
486
481
|
with istr.repr_mode('int'):
|
|
@@ -491,12 +486,12 @@ If the repr_mode is `'int'` and the istr can't be interpreted as an int the stri
|
|
|
491
486
|
This will print
|
|
492
487
|
|
|
493
488
|
```
|
|
494
|
-
|
|
489
|
+
?
|
|
495
490
|
```
|
|
496
491
|
|
|
497
|
-
|
|
498
|
-
>
|
|
499
|
-
>
|
|
492
|
+
##### Note
|
|
493
|
+
>
|
|
494
|
+
> The way an `istr` is represented is determined at initialization.
|
|
500
495
|
|
|
501
496
|
It is also possible to set the repr mode without a context manager:
|
|
502
497
|
|
|
@@ -530,13 +525,13 @@ with istr.base(16):
|
|
|
530
525
|
a = istr('7fff')
|
|
531
526
|
print(int(a))
|
|
532
527
|
|
|
533
|
-
b = istr(
|
|
528
|
+
b = istr(127)
|
|
534
529
|
print(repr(b))
|
|
535
530
|
```
|
|
536
531
|
This will result in
|
|
537
532
|
```
|
|
538
533
|
32767
|
|
539
|
-
istr('
|
|
534
|
+
istr('7F')
|
|
540
535
|
```
|
|
541
536
|
All calculations are done in the decimal 10 system.
|
|
542
537
|
|
|
@@ -559,7 +554,7 @@ will result in `10`.
|
|
|
559
554
|
|
|
560
555
|
#### Changing the format of the string
|
|
561
556
|
|
|
562
|
-
When an istr is initialized with a string the istr will be
|
|
557
|
+
When an istr is initialized with a string the istr will be always stored as such.
|
|
563
558
|
|
|
564
559
|
```
|
|
565
560
|
repr('4')) ==> istr('4')
|
|
@@ -573,7 +568,7 @@ For initializing with an int (or other numeric) value, the string is by default
|
|
|
573
568
|
repr(4)) ==> istr('4')
|
|
574
569
|
```
|
|
575
570
|
|
|
576
|
-
|
|
571
|
+
With the `istr.int_format()` context manager this behavior can be changed.
|
|
577
572
|
If the format specifier is a number, most likely a single digit, that
|
|
578
573
|
will be the minimum number of characters in the string:
|
|
579
574
|
|
|
@@ -606,24 +601,10 @@ istr('012')
|
|
|
606
601
|
istr('123')
|
|
607
602
|
istr('1234')
|
|
608
603
|
```
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
> > if a string is used to initialize an istr AND that string can be interpreted as an int. the string will reformatted:
|
|
612
|
-
> >
|
|
613
|
-
> > ```
|
|
614
|
-
> > with istr.int_format('03'):
|
|
615
|
-
> > print(repr(istr(12)))
|
|
616
|
-
> > ```
|
|
617
|
-
> >
|
|
618
|
-
> > will result in
|
|
619
|
-
> >
|
|
620
|
-
> > ```
|
|
621
|
-
> > istr('0012')
|
|
622
|
-
> > ```
|
|
604
|
+
|
|
605
|
+
##### Note
|
|
623
606
|
>
|
|
624
|
-
>
|
|
625
|
-
> >
|
|
626
|
-
> > For bases other than 10, the string will never be reformatted!
|
|
607
|
+
> For bases other than 10, the string will never be reformatted!
|
|
627
608
|
|
|
628
609
|
### Overview of operations
|
|
629
610
|
|
|
@@ -663,6 +644,8 @@ There's an extensive pytest script in the `\tests` directory.
|
|
|
663
644
|
|
|
664
645
|
This script also shows clearly the ways istr can be used, including several edge cases. Highly recommended to have a look at.
|
|
665
646
|
|
|
647
|
+
|
|
648
|
+
|
|
666
649
|
### Badges
|
|
667
650
|
  
|
|
668
651
|
|
|
@@ -137,10 +137,10 @@ or even
|
|
|
137
137
|
four, five = istr(4, 5)
|
|
138
138
|
```
|
|
139
139
|
|
|
140
|
-
|
|
141
|
-
>
|
|
142
|
-
>
|
|
143
|
-
>
|
|
140
|
+
##### Important
|
|
141
|
+
>
|
|
142
|
+
> All calculations are strictly integer calculations. That means that if a float or decimal variable is ever produced it will be converted to an int.
|
|
143
|
+
> Also divisions are always floor divisions!
|
|
144
144
|
|
|
145
145
|
#### Use istrs as string
|
|
146
146
|
|
|
@@ -175,12 +175,13 @@ And
|
|
|
175
175
|
|
|
176
176
|
is also `istr('444')`
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
##### Note
|
|
179
|
+
|
|
179
180
|
>
|
|
180
181
|
> It is not allowed to use the `@` operator for two istrs. So, `four @ five` raises a TypeError.
|
|
181
|
-
|
|
182
182
|
#### istrs that can't be interpreted as an int
|
|
183
183
|
|
|
184
|
+
|
|
184
185
|
Although usualy, istrs are to be interpreted as an int, that's not a requirement.
|
|
185
186
|
|
|
186
187
|
So
|
|
@@ -221,7 +222,7 @@ is `False`.
|
|
|
221
222
|
|
|
222
223
|
|
|
223
224
|
|
|
224
|
-
The bool operator works normally on the integer value of an istr. So
|
|
225
|
+
The `bool` operator works normally on the integer value of an istr. So
|
|
225
226
|
|
|
226
227
|
`bool(istr('0'))` ==> `False`
|
|
227
228
|
`bool(istr('1'))` ==> `True`
|
|
@@ -238,6 +239,7 @@ For the `in` operator, an istr is treated as an ordinary string, although it is
|
|
|
238
239
|
```
|
|
239
240
|
'34' in istr(1234)
|
|
240
241
|
34 in istr(1234)
|
|
242
|
+
|
|
241
243
|
```
|
|
242
244
|
On the left hand side an istr is always treated as a string:
|
|
243
245
|
```
|
|
@@ -267,47 +269,39 @@ is
|
|
|
267
269
|
```
|
|
268
270
|
'0 1 2 3 4 5 6 7 8 9 10 11'
|
|
269
271
|
```
|
|
272
|
+
#### Using values that are neither string or numeric to initialize istr
|
|
270
273
|
|
|
271
|
-
|
|
272
|
-
Apart from with simple numeric (to be interpreted as an int) or str, istr can be initialized with
|
|
274
|
+
Apart from with numeric (to be interpreted as an int) or str, istr can be initialized with
|
|
273
275
|
several other types:
|
|
274
276
|
|
|
275
|
-
- if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
|
|
276
277
|
|
|
277
|
-
|
|
278
|
-
istr({0: 0, 1: 1, 2: 4}) ==> {0: istr('0'), 1: istr('1'), 2: istr('4')}
|
|
279
|
-
```
|
|
278
|
+
- if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
|
|
280
279
|
|
|
281
280
|
- if an iterator, the iterator will be mapped with istr
|
|
282
281
|
|
|
283
|
-
```
|
|
284
|
-
istr(i * i for i in range(3)) ==> <map object>
|
|
285
|
-
list(istr(i * i for i in range(3))) ==> [istr('0'), istr('1'), istr('4')]
|
|
286
|
-
```
|
|
287
|
-
|
|
288
282
|
- if an iterable, the same type will be returned with all elements istr'ed
|
|
289
283
|
|
|
290
|
-
|
|
284
|
+
```
|
|
291
285
|
istr([0, 1, 4]) ==> [istr('0'), istr('1'), istr('4')]
|
|
292
286
|
istr((0, 1, 4)) ==> (istr('0'), istr('1'), istr('4'))
|
|
293
|
-
istr({0, 1, 4}) ==> `{istr('4'), istr('0'), istr('1')} # or similar
|
|
294
|
-
|
|
287
|
+
istr({0, 1, 4}) ==> `{istr('4'), istr('0'), istr('1')} # or similar
|
|
288
|
+
```
|
|
295
289
|
|
|
296
290
|
- if a range, an istr.range instance will be returned
|
|
297
291
|
|
|
298
|
-
|
|
299
|
-
istr(range(3))
|
|
300
|
-
list(istr(range(3)))
|
|
301
|
-
len(istr(range(3)))
|
|
302
|
-
|
|
292
|
+
```
|
|
293
|
+
istr(range(3)) ==> istr.range(3)
|
|
294
|
+
list(istr(range(3))) ==> [istr('0'), istr('1'), istr('2')]
|
|
295
|
+
len(istr(range(3))) ==> 3
|
|
296
|
+
```
|
|
303
297
|
|
|
304
298
|
- if an istr.range instance, the same istr.range will be returned
|
|
305
299
|
|
|
306
300
|
- if an istr, the same istr will be used
|
|
307
301
|
|
|
308
|
-
|
|
302
|
+
```
|
|
309
303
|
istr(istr('4')) ==> istr ('4')
|
|
310
|
-
|
|
304
|
+
```
|
|
311
305
|
|
|
312
306
|
#### More than one parameter for istr
|
|
313
307
|
It is possible to give more than one parameter, in which case a tuple
|
|
@@ -342,7 +336,7 @@ istr(n100).all_distinct() ==> False
|
|
|
342
336
|
|
|
343
337
|
#### reverse an istr
|
|
344
338
|
|
|
345
|
-
The method `istr.reversed()` will return
|
|
339
|
+
The method `istr.reversed()` will return an istr with the reversed content:
|
|
346
340
|
```
|
|
347
341
|
istr(456).reversed() ==> istr('654')
|
|
348
342
|
istr('0456').reversed() ==> istr('6540')
|
|
@@ -352,13 +346,13 @@ The same can -of course- be achieved with
|
|
|
352
346
|
istr(456)[::-1] ==> istr('654')
|
|
353
347
|
istr('0456')[::-1] ==> istr('6540')
|
|
354
348
|
```
|
|
355
|
-
|
|
356
|
-
>
|
|
357
|
-
>
|
|
358
|
-
>
|
|
359
|
-
>
|
|
360
|
-
>
|
|
361
|
-
>
|
|
349
|
+
##### Note
|
|
350
|
+
>
|
|
351
|
+
> It is possible to reverse a negative istr, but the result can't be interpreted as an int anymore.
|
|
352
|
+
>
|
|
353
|
+
> ```
|
|
354
|
+
> istr(-456).reversed() ==> TypeError
|
|
355
|
+
> ```
|
|
362
356
|
|
|
363
357
|
#### enumerate with istrs
|
|
364
358
|
|
|
@@ -366,8 +360,8 @@ The `istr.enumerate` method can be used just as the builtin enumerate function.
|
|
|
366
360
|
The iteration counter however is an istr rather than an int. E.g.
|
|
367
361
|
|
|
368
362
|
```
|
|
369
|
-
|
|
370
|
-
|
|
363
|
+
for i, c in istr.enumerate('abc'):
|
|
364
|
+
print(f'{repr(i)} {c}')
|
|
371
365
|
```
|
|
372
366
|
prints
|
|
373
367
|
```
|
|
@@ -401,7 +395,8 @@ The given argument(s) result in a range of digits.
|
|
|
401
395
|
- `<n>` ==> n
|
|
402
396
|
- `<n-m>` ==> n, n+1, ..., m
|
|
403
397
|
- `-n>` ==> 0, 1, ... n
|
|
404
|
-
- `n->` ==> n, n+1, ..., 9
|
|
398
|
+
- `n->` ==> n, n+1, ..., 9 if n is numeric (0-9), n, n+1, ... Z if n is a letter
|
|
399
|
+
- `'-'` ==> 0, 1, ..., 9
|
|
405
400
|
- `''` ==> 0, 1, ..., 9
|
|
406
401
|
|
|
407
402
|
(n and m must be digits between 0 and 9 or letters letters between A and Z)
|
|
@@ -447,9 +442,9 @@ will print `jstr('20')`
|
|
|
447
442
|
|
|
448
443
|
It is possible to control the way an `istr` instance will be repr'ed.
|
|
449
444
|
|
|
450
|
-
By default,
|
|
445
|
+
By default, `istr(5)` is represented as `istr('5')`.
|
|
451
446
|
|
|
452
|
-
With the istr.repr_mode() context manager, that can be changed:
|
|
447
|
+
With the `istr.repr_mode()` context manager, that can be changed:
|
|
453
448
|
```
|
|
454
449
|
with istr.repr_mode('str'):
|
|
455
450
|
five = istr(5)
|
|
@@ -467,7 +462,7 @@ This will print
|
|
|
467
462
|
5
|
|
468
463
|
istr('5')
|
|
469
464
|
```
|
|
470
|
-
If the repr_mode is `'int'` and the istr can't be interpreted as an int the string
|
|
465
|
+
If the repr_mode is `'int'` and the istr can't be interpreted as an int the string `?` will be returned:
|
|
471
466
|
|
|
472
467
|
```
|
|
473
468
|
with istr.repr_mode('int'):
|
|
@@ -478,12 +473,12 @@ If the repr_mode is `'int'` and the istr can't be interpreted as an int the stri
|
|
|
478
473
|
This will print
|
|
479
474
|
|
|
480
475
|
```
|
|
481
|
-
|
|
476
|
+
?
|
|
482
477
|
```
|
|
483
478
|
|
|
484
|
-
|
|
485
|
-
>
|
|
486
|
-
>
|
|
479
|
+
##### Note
|
|
480
|
+
>
|
|
481
|
+
> The way an `istr` is represented is determined at initialization.
|
|
487
482
|
|
|
488
483
|
It is also possible to set the repr mode without a context manager:
|
|
489
484
|
|
|
@@ -517,13 +512,13 @@ with istr.base(16):
|
|
|
517
512
|
a = istr('7fff')
|
|
518
513
|
print(int(a))
|
|
519
514
|
|
|
520
|
-
b = istr(
|
|
515
|
+
b = istr(127)
|
|
521
516
|
print(repr(b))
|
|
522
517
|
```
|
|
523
518
|
This will result in
|
|
524
519
|
```
|
|
525
520
|
32767
|
|
526
|
-
istr('
|
|
521
|
+
istr('7F')
|
|
527
522
|
```
|
|
528
523
|
All calculations are done in the decimal 10 system.
|
|
529
524
|
|
|
@@ -546,7 +541,7 @@ will result in `10`.
|
|
|
546
541
|
|
|
547
542
|
#### Changing the format of the string
|
|
548
543
|
|
|
549
|
-
When an istr is initialized with a string the istr will be
|
|
544
|
+
When an istr is initialized with a string the istr will be always stored as such.
|
|
550
545
|
|
|
551
546
|
```
|
|
552
547
|
repr('4')) ==> istr('4')
|
|
@@ -560,7 +555,7 @@ For initializing with an int (or other numeric) value, the string is by default
|
|
|
560
555
|
repr(4)) ==> istr('4')
|
|
561
556
|
```
|
|
562
557
|
|
|
563
|
-
|
|
558
|
+
With the `istr.int_format()` context manager this behavior can be changed.
|
|
564
559
|
If the format specifier is a number, most likely a single digit, that
|
|
565
560
|
will be the minimum number of characters in the string:
|
|
566
561
|
|
|
@@ -593,24 +588,10 @@ istr('012')
|
|
|
593
588
|
istr('123')
|
|
594
589
|
istr('1234')
|
|
595
590
|
```
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
> > if a string is used to initialize an istr AND that string can be interpreted as an int. the string will reformatted:
|
|
599
|
-
> >
|
|
600
|
-
> > ```
|
|
601
|
-
> > with istr.int_format('03'):
|
|
602
|
-
> > print(repr(istr(12)))
|
|
603
|
-
> > ```
|
|
604
|
-
> >
|
|
605
|
-
> > will result in
|
|
606
|
-
> >
|
|
607
|
-
> > ```
|
|
608
|
-
> > istr('0012')
|
|
609
|
-
> > ```
|
|
591
|
+
|
|
592
|
+
##### Note
|
|
610
593
|
>
|
|
611
|
-
>
|
|
612
|
-
> >
|
|
613
|
-
> > For bases other than 10, the string will never be reformatted!
|
|
594
|
+
> For bases other than 10, the string will never be reformatted!
|
|
614
595
|
|
|
615
596
|
### Overview of operations
|
|
616
597
|
|
|
@@ -650,6 +631,8 @@ There's an extensive pytest script in the `\tests` directory.
|
|
|
650
631
|
|
|
651
632
|
This script also shows clearly the ways istr can be used, including several edge cases. Highly recommended to have a look at.
|
|
652
633
|
|
|
634
|
+
|
|
635
|
+
|
|
653
636
|
### Badges
|
|
654
637
|
  
|
|
655
638
|
|
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
## changelog
|
|
2
2
|
|
|
3
|
+
### version 1.0.2 | 2024-05-08
|
|
4
|
+
|
|
5
|
+
When a string that can't be interpreted as an istr is created when istr.repr_mode is 'int', the repr of that file will be `?` (was: `nan`):
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
with istr.repr_mode('int'):
|
|
9
|
+
a = istr('abc')
|
|
10
|
+
print(a)
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
will print
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
?
|
|
17
|
+
```
|
|
18
|
+
|
|
3
19
|
### version 1.0.1 | 2024-05-07
|
|
4
20
|
|
|
5
21
|
From now on, the changelog is not anymore part of the istr.py file, but is in a separate `changelog.md` file.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
# |_||___/ \__||_|
|
|
6
6
|
# strings you can count on
|
|
7
7
|
|
|
8
|
-
__version__ = "1.0.
|
|
8
|
+
__version__ = "1.0.2"
|
|
9
9
|
import functools
|
|
10
10
|
import math
|
|
11
11
|
|
|
@@ -15,6 +15,7 @@ Note: the changelog is now in changelog.md
|
|
|
15
15
|
You can view the changelog on www.salabim/istr_changelog.html
|
|
16
16
|
"""
|
|
17
17
|
|
|
18
|
+
_0_to_Z = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
18
19
|
|
|
19
20
|
class _range:
|
|
20
21
|
"""
|
|
@@ -195,7 +196,7 @@ class istr(str):
|
|
|
195
196
|
_int_format = ""
|
|
196
197
|
_repr_mode = "istr"
|
|
197
198
|
_base = 10
|
|
198
|
-
_nan =
|
|
199
|
+
_nan = object()
|
|
199
200
|
_force_istr_repr = False
|
|
200
201
|
_digits_cache = {}
|
|
201
202
|
|
|
@@ -205,7 +206,7 @@ class istr(str):
|
|
|
205
206
|
raise ValueError(f"negative numbers are not allowed for base {base}")
|
|
206
207
|
result = ""
|
|
207
208
|
while number:
|
|
208
|
-
result +=
|
|
209
|
+
result += _0_to_Z[number % base]
|
|
209
210
|
number //= base
|
|
210
211
|
return result[::-1] or "0"
|
|
211
212
|
|
|
@@ -223,7 +224,7 @@ class istr(str):
|
|
|
223
224
|
operator = args[len(args) == 2]
|
|
224
225
|
if len(args) == 2:
|
|
225
226
|
other = args[0]
|
|
226
|
-
if not self.is_int() or self._to_int(other)
|
|
227
|
+
if not self.is_int() or self._to_int(other) is self._nan:
|
|
227
228
|
self.__class__._force_istr_repr = True
|
|
228
229
|
if right:
|
|
229
230
|
message = f"unsupported operand for {operator}: {repr(other)} and {repr(self)}"
|
|
@@ -263,7 +264,7 @@ class istr(str):
|
|
|
263
264
|
if isinstance(value, str):
|
|
264
265
|
as_str = value
|
|
265
266
|
else:
|
|
266
|
-
if as_int
|
|
267
|
+
if as_int is cls._nan:
|
|
267
268
|
raise TypeError(f"incorrect value for {cls.__name__}: {repr(value)}")
|
|
268
269
|
if cls._int_format == "" or cls._base != 10:
|
|
269
270
|
if cls._base == 10:
|
|
@@ -278,7 +279,7 @@ class istr(str):
|
|
|
278
279
|
if self._repr_mode == "istr":
|
|
279
280
|
self._as_repr = f"{cls.__name__}({repr(as_str)})"
|
|
280
281
|
elif self._repr_mode == "int":
|
|
281
|
-
self._as_repr = "
|
|
282
|
+
self._as_repr = "?" if as_int is self._nan else repr(as_int)
|
|
282
283
|
else:
|
|
283
284
|
self._as_repr = repr(as_str)
|
|
284
285
|
return self
|
|
@@ -453,7 +454,7 @@ class istr(str):
|
|
|
453
454
|
return len(self) == len(set(self))
|
|
454
455
|
|
|
455
456
|
def is_int(self):
|
|
456
|
-
return self._as_int
|
|
457
|
+
return self._as_int is not self._nan
|
|
457
458
|
|
|
458
459
|
def reversed(self):
|
|
459
460
|
return self[::-1]
|
|
@@ -569,7 +570,6 @@ class istr(str):
|
|
|
569
570
|
key = (args, cls._base, cls._int_format, cls._repr_mode)
|
|
570
571
|
if key in cls._digits_cache:
|
|
571
572
|
return cls.digits_cache[key]
|
|
572
|
-
sequence = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
573
573
|
result = []
|
|
574
574
|
if not args:
|
|
575
575
|
args = ["0-9"]
|
|
@@ -580,9 +580,9 @@ class istr(str):
|
|
|
580
580
|
if pre.strip() == "":
|
|
581
581
|
pre = "0"
|
|
582
582
|
pre = pre.upper()
|
|
583
|
-
if len(pre) > 1 or pre not in
|
|
583
|
+
if len(pre) > 1 or pre not in _0_to_Z:
|
|
584
584
|
raise ValueError(f"incorrect specifier: {repr(arg)}")
|
|
585
|
-
start =
|
|
585
|
+
start = _0_to_Z.index(pre)
|
|
586
586
|
|
|
587
587
|
if post:
|
|
588
588
|
post = post[0]
|
|
@@ -592,14 +592,14 @@ class istr(str):
|
|
|
592
592
|
else:
|
|
593
593
|
post = "Z"
|
|
594
594
|
post = post.upper()
|
|
595
|
-
if len(post) > 1 or post not in
|
|
595
|
+
if len(post) > 1 or post not in _0_to_Z:
|
|
596
596
|
raise ValueError(f"incorrect specifier: {repr(arg)}")
|
|
597
|
-
stop =
|
|
597
|
+
stop = _0_to_Z.index(post)
|
|
598
598
|
if start > stop:
|
|
599
599
|
raise ValueError(f"incorrect specifier: {repr(arg)}")
|
|
600
600
|
else:
|
|
601
601
|
stop = start
|
|
602
|
-
result.extend(
|
|
602
|
+
result.extend(_0_to_Z[i] for i in range(start, stop + 1))
|
|
603
603
|
|
|
604
604
|
result = istr("".join(result))
|
|
605
605
|
cls._digits_cache[key] = result
|
|
@@ -679,8 +679,15 @@ class istr(str):
|
|
|
679
679
|
|
|
680
680
|
|
|
681
681
|
def main():
|
|
682
|
-
|
|
683
|
-
|
|
682
|
+
with istr.repr_mode("int"):
|
|
683
|
+
print(repr(istr(3)))
|
|
684
|
+
print(repr(istr("a")))
|
|
685
|
+
with istr.int_format("4"):
|
|
686
|
+
print(repr(istr(3)))
|
|
687
|
+
print(repr(istr("a")))
|
|
688
|
+
with istr.int_format("04"):
|
|
689
|
+
print(repr(istr(3)))
|
|
690
|
+
print(repr(istr("a")))
|
|
684
691
|
|
|
685
692
|
if __name__ == "__main__":
|
|
686
693
|
main()
|
|
@@ -137,10 +137,10 @@ or even
|
|
|
137
137
|
four, five = istr(4, 5)
|
|
138
138
|
```
|
|
139
139
|
|
|
140
|
-
|
|
141
|
-
>
|
|
142
|
-
>
|
|
143
|
-
>
|
|
140
|
+
##### Important
|
|
141
|
+
>
|
|
142
|
+
> All calculations are strictly integer calculations. That means that if a float or decimal variable is ever produced it will be converted to an int.
|
|
143
|
+
> Also divisions are always floor divisions!
|
|
144
144
|
|
|
145
145
|
#### Use istrs as string
|
|
146
146
|
|
|
@@ -175,12 +175,13 @@ And
|
|
|
175
175
|
|
|
176
176
|
is also `istr('444')`
|
|
177
177
|
|
|
178
|
-
|
|
178
|
+
##### Note
|
|
179
|
+
|
|
179
180
|
>
|
|
180
181
|
> It is not allowed to use the `@` operator for two istrs. So, `four @ five` raises a TypeError.
|
|
181
|
-
|
|
182
182
|
#### istrs that can't be interpreted as an int
|
|
183
183
|
|
|
184
|
+
|
|
184
185
|
Although usualy, istrs are to be interpreted as an int, that's not a requirement.
|
|
185
186
|
|
|
186
187
|
So
|
|
@@ -221,7 +222,7 @@ is `False`.
|
|
|
221
222
|
|
|
222
223
|
|
|
223
224
|
|
|
224
|
-
The bool operator works normally on the integer value of an istr. So
|
|
225
|
+
The `bool` operator works normally on the integer value of an istr. So
|
|
225
226
|
|
|
226
227
|
`bool(istr('0'))` ==> `False`
|
|
227
228
|
`bool(istr('1'))` ==> `True`
|
|
@@ -238,6 +239,7 @@ For the `in` operator, an istr is treated as an ordinary string, although it is
|
|
|
238
239
|
```
|
|
239
240
|
'34' in istr(1234)
|
|
240
241
|
34 in istr(1234)
|
|
242
|
+
|
|
241
243
|
```
|
|
242
244
|
On the left hand side an istr is always treated as a string:
|
|
243
245
|
```
|
|
@@ -267,47 +269,39 @@ is
|
|
|
267
269
|
```
|
|
268
270
|
'0 1 2 3 4 5 6 7 8 9 10 11'
|
|
269
271
|
```
|
|
272
|
+
#### Using values that are neither string or numeric to initialize istr
|
|
270
273
|
|
|
271
|
-
|
|
272
|
-
Apart from with simple numeric (to be interpreted as an int) or str, istr can be initialized with
|
|
274
|
+
Apart from with numeric (to be interpreted as an int) or str, istr can be initialized with
|
|
273
275
|
several other types:
|
|
274
276
|
|
|
275
|
-
- if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
|
|
276
277
|
|
|
277
|
-
|
|
278
|
-
istr({0: 0, 1: 1, 2: 4}) ==> {0: istr('0'), 1: istr('1'), 2: istr('4')}
|
|
279
|
-
```
|
|
278
|
+
- if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
|
|
280
279
|
|
|
281
280
|
- if an iterator, the iterator will be mapped with istr
|
|
282
281
|
|
|
283
|
-
```
|
|
284
|
-
istr(i * i for i in range(3)) ==> <map object>
|
|
285
|
-
list(istr(i * i for i in range(3))) ==> [istr('0'), istr('1'), istr('4')]
|
|
286
|
-
```
|
|
287
|
-
|
|
288
282
|
- if an iterable, the same type will be returned with all elements istr'ed
|
|
289
283
|
|
|
290
|
-
|
|
284
|
+
```
|
|
291
285
|
istr([0, 1, 4]) ==> [istr('0'), istr('1'), istr('4')]
|
|
292
286
|
istr((0, 1, 4)) ==> (istr('0'), istr('1'), istr('4'))
|
|
293
|
-
istr({0, 1, 4}) ==> `{istr('4'), istr('0'), istr('1')} # or similar
|
|
294
|
-
|
|
287
|
+
istr({0, 1, 4}) ==> `{istr('4'), istr('0'), istr('1')} # or similar
|
|
288
|
+
```
|
|
295
289
|
|
|
296
290
|
- if a range, an istr.range instance will be returned
|
|
297
291
|
|
|
298
|
-
|
|
299
|
-
istr(range(3))
|
|
300
|
-
list(istr(range(3)))
|
|
301
|
-
len(istr(range(3)))
|
|
302
|
-
|
|
292
|
+
```
|
|
293
|
+
istr(range(3)) ==> istr.range(3)
|
|
294
|
+
list(istr(range(3))) ==> [istr('0'), istr('1'), istr('2')]
|
|
295
|
+
len(istr(range(3))) ==> 3
|
|
296
|
+
```
|
|
303
297
|
|
|
304
298
|
- if an istr.range instance, the same istr.range will be returned
|
|
305
299
|
|
|
306
300
|
- if an istr, the same istr will be used
|
|
307
301
|
|
|
308
|
-
|
|
302
|
+
```
|
|
309
303
|
istr(istr('4')) ==> istr ('4')
|
|
310
|
-
|
|
304
|
+
```
|
|
311
305
|
|
|
312
306
|
#### More than one parameter for istr
|
|
313
307
|
It is possible to give more than one parameter, in which case a tuple
|
|
@@ -342,7 +336,7 @@ istr(n100).all_distinct() ==> False
|
|
|
342
336
|
|
|
343
337
|
#### reverse an istr
|
|
344
338
|
|
|
345
|
-
The method `istr.reversed()` will return
|
|
339
|
+
The method `istr.reversed()` will return an istr with the reversed content:
|
|
346
340
|
```
|
|
347
341
|
istr(456).reversed() ==> istr('654')
|
|
348
342
|
istr('0456').reversed() ==> istr('6540')
|
|
@@ -352,13 +346,13 @@ The same can -of course- be achieved with
|
|
|
352
346
|
istr(456)[::-1] ==> istr('654')
|
|
353
347
|
istr('0456')[::-1] ==> istr('6540')
|
|
354
348
|
```
|
|
355
|
-
|
|
356
|
-
>
|
|
357
|
-
>
|
|
358
|
-
>
|
|
359
|
-
>
|
|
360
|
-
>
|
|
361
|
-
>
|
|
349
|
+
##### Note
|
|
350
|
+
>
|
|
351
|
+
> It is possible to reverse a negative istr, but the result can't be interpreted as an int anymore.
|
|
352
|
+
>
|
|
353
|
+
> ```
|
|
354
|
+
> istr(-456).reversed() ==> TypeError
|
|
355
|
+
> ```
|
|
362
356
|
|
|
363
357
|
#### enumerate with istrs
|
|
364
358
|
|
|
@@ -366,8 +360,8 @@ The `istr.enumerate` method can be used just as the builtin enumerate function.
|
|
|
366
360
|
The iteration counter however is an istr rather than an int. E.g.
|
|
367
361
|
|
|
368
362
|
```
|
|
369
|
-
|
|
370
|
-
|
|
363
|
+
for i, c in istr.enumerate('abc'):
|
|
364
|
+
print(f'{repr(i)} {c}')
|
|
371
365
|
```
|
|
372
366
|
prints
|
|
373
367
|
```
|
|
@@ -401,7 +395,8 @@ The given argument(s) result in a range of digits.
|
|
|
401
395
|
- `<n>` ==> n
|
|
402
396
|
- `<n-m>` ==> n, n+1, ..., m
|
|
403
397
|
- `-n>` ==> 0, 1, ... n
|
|
404
|
-
- `n->` ==> n, n+1, ..., 9
|
|
398
|
+
- `n->` ==> n, n+1, ..., 9 if n is numeric (0-9), n, n+1, ... Z if n is a letter
|
|
399
|
+
- `'-'` ==> 0, 1, ..., 9
|
|
405
400
|
- `''` ==> 0, 1, ..., 9
|
|
406
401
|
|
|
407
402
|
(n and m must be digits between 0 and 9 or letters letters between A and Z)
|
|
@@ -447,9 +442,9 @@ will print `jstr('20')`
|
|
|
447
442
|
|
|
448
443
|
It is possible to control the way an `istr` instance will be repr'ed.
|
|
449
444
|
|
|
450
|
-
By default,
|
|
445
|
+
By default, `istr(5)` is represented as `istr('5')`.
|
|
451
446
|
|
|
452
|
-
With the istr.repr_mode() context manager, that can be changed:
|
|
447
|
+
With the `istr.repr_mode()` context manager, that can be changed:
|
|
453
448
|
```
|
|
454
449
|
with istr.repr_mode('str'):
|
|
455
450
|
five = istr(5)
|
|
@@ -467,7 +462,7 @@ This will print
|
|
|
467
462
|
5
|
|
468
463
|
istr('5')
|
|
469
464
|
```
|
|
470
|
-
If the repr_mode is `'int'` and the istr can't be interpreted as an int the string
|
|
465
|
+
If the repr_mode is `'int'` and the istr can't be interpreted as an int the string `?` will be returned:
|
|
471
466
|
|
|
472
467
|
```
|
|
473
468
|
with istr.repr_mode('int'):
|
|
@@ -478,12 +473,12 @@ If the repr_mode is `'int'` and the istr can't be interpreted as an int the stri
|
|
|
478
473
|
This will print
|
|
479
474
|
|
|
480
475
|
```
|
|
481
|
-
|
|
476
|
+
?
|
|
482
477
|
```
|
|
483
478
|
|
|
484
|
-
|
|
485
|
-
>
|
|
486
|
-
>
|
|
479
|
+
##### Note
|
|
480
|
+
>
|
|
481
|
+
> The way an `istr` is represented is determined at initialization.
|
|
487
482
|
|
|
488
483
|
It is also possible to set the repr mode without a context manager:
|
|
489
484
|
|
|
@@ -517,13 +512,13 @@ with istr.base(16):
|
|
|
517
512
|
a = istr('7fff')
|
|
518
513
|
print(int(a))
|
|
519
514
|
|
|
520
|
-
b = istr(
|
|
515
|
+
b = istr(127)
|
|
521
516
|
print(repr(b))
|
|
522
517
|
```
|
|
523
518
|
This will result in
|
|
524
519
|
```
|
|
525
520
|
32767
|
|
526
|
-
istr('
|
|
521
|
+
istr('7F')
|
|
527
522
|
```
|
|
528
523
|
All calculations are done in the decimal 10 system.
|
|
529
524
|
|
|
@@ -546,7 +541,7 @@ will result in `10`.
|
|
|
546
541
|
|
|
547
542
|
#### Changing the format of the string
|
|
548
543
|
|
|
549
|
-
When an istr is initialized with a string the istr will be
|
|
544
|
+
When an istr is initialized with a string the istr will be always stored as such.
|
|
550
545
|
|
|
551
546
|
```
|
|
552
547
|
repr('4')) ==> istr('4')
|
|
@@ -560,7 +555,7 @@ For initializing with an int (or other numeric) value, the string is by default
|
|
|
560
555
|
repr(4)) ==> istr('4')
|
|
561
556
|
```
|
|
562
557
|
|
|
563
|
-
|
|
558
|
+
With the `istr.int_format()` context manager this behavior can be changed.
|
|
564
559
|
If the format specifier is a number, most likely a single digit, that
|
|
565
560
|
will be the minimum number of characters in the string:
|
|
566
561
|
|
|
@@ -593,24 +588,10 @@ istr('012')
|
|
|
593
588
|
istr('123')
|
|
594
589
|
istr('1234')
|
|
595
590
|
```
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
> > if a string is used to initialize an istr AND that string can be interpreted as an int. the string will reformatted:
|
|
599
|
-
> >
|
|
600
|
-
> > ```
|
|
601
|
-
> > with istr.int_format('03'):
|
|
602
|
-
> > print(repr(istr(12)))
|
|
603
|
-
> > ```
|
|
604
|
-
> >
|
|
605
|
-
> > will result in
|
|
606
|
-
> >
|
|
607
|
-
> > ```
|
|
608
|
-
> > istr('0012')
|
|
609
|
-
> > ```
|
|
591
|
+
|
|
592
|
+
##### Note
|
|
610
593
|
>
|
|
611
|
-
>
|
|
612
|
-
> >
|
|
613
|
-
> > For bases other than 10, the string will never be reformatted!
|
|
594
|
+
> For bases other than 10, the string will never be reformatted!
|
|
614
595
|
|
|
615
596
|
### Overview of operations
|
|
616
597
|
|
|
@@ -650,6 +631,8 @@ There's an extensive pytest script in the `\tests` directory.
|
|
|
650
631
|
|
|
651
632
|
This script also shows clearly the ways istr can be used, including several edge cases. Highly recommended to have a look at.
|
|
652
633
|
|
|
634
|
+
|
|
635
|
+
|
|
653
636
|
### Badges
|
|
654
637
|
  
|
|
655
638
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: istr-python
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.2
|
|
4
4
|
Summary: istr - strings you can count on
|
|
5
5
|
Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/salabim/istr
|
|
@@ -150,10 +150,10 @@ or even
|
|
|
150
150
|
four, five = istr(4, 5)
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
-
|
|
154
|
-
>
|
|
155
|
-
>
|
|
156
|
-
>
|
|
153
|
+
##### Important
|
|
154
|
+
>
|
|
155
|
+
> All calculations are strictly integer calculations. That means that if a float or decimal variable is ever produced it will be converted to an int.
|
|
156
|
+
> Also divisions are always floor divisions!
|
|
157
157
|
|
|
158
158
|
#### Use istrs as string
|
|
159
159
|
|
|
@@ -188,12 +188,13 @@ And
|
|
|
188
188
|
|
|
189
189
|
is also `istr('444')`
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
##### Note
|
|
192
|
+
|
|
192
193
|
>
|
|
193
194
|
> It is not allowed to use the `@` operator for two istrs. So, `four @ five` raises a TypeError.
|
|
194
|
-
|
|
195
195
|
#### istrs that can't be interpreted as an int
|
|
196
196
|
|
|
197
|
+
|
|
197
198
|
Although usualy, istrs are to be interpreted as an int, that's not a requirement.
|
|
198
199
|
|
|
199
200
|
So
|
|
@@ -234,7 +235,7 @@ is `False`.
|
|
|
234
235
|
|
|
235
236
|
|
|
236
237
|
|
|
237
|
-
The bool operator works normally on the integer value of an istr. So
|
|
238
|
+
The `bool` operator works normally on the integer value of an istr. So
|
|
238
239
|
|
|
239
240
|
`bool(istr('0'))` ==> `False`
|
|
240
241
|
`bool(istr('1'))` ==> `True`
|
|
@@ -251,6 +252,7 @@ For the `in` operator, an istr is treated as an ordinary string, although it is
|
|
|
251
252
|
```
|
|
252
253
|
'34' in istr(1234)
|
|
253
254
|
34 in istr(1234)
|
|
255
|
+
|
|
254
256
|
```
|
|
255
257
|
On the left hand side an istr is always treated as a string:
|
|
256
258
|
```
|
|
@@ -280,47 +282,39 @@ is
|
|
|
280
282
|
```
|
|
281
283
|
'0 1 2 3 4 5 6 7 8 9 10 11'
|
|
282
284
|
```
|
|
285
|
+
#### Using values that are neither string or numeric to initialize istr
|
|
283
286
|
|
|
284
|
-
|
|
285
|
-
Apart from with simple numeric (to be interpreted as an int) or str, istr can be initialized with
|
|
287
|
+
Apart from with numeric (to be interpreted as an int) or str, istr can be initialized with
|
|
286
288
|
several other types:
|
|
287
289
|
|
|
288
|
-
- if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
|
|
289
290
|
|
|
290
|
-
|
|
291
|
-
istr({0: 0, 1: 1, 2: 4}) ==> {0: istr('0'), 1: istr('1'), 2: istr('4')}
|
|
292
|
-
```
|
|
291
|
+
- if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
|
|
293
292
|
|
|
294
293
|
- if an iterator, the iterator will be mapped with istr
|
|
295
294
|
|
|
296
|
-
```
|
|
297
|
-
istr(i * i for i in range(3)) ==> <map object>
|
|
298
|
-
list(istr(i * i for i in range(3))) ==> [istr('0'), istr('1'), istr('4')]
|
|
299
|
-
```
|
|
300
|
-
|
|
301
295
|
- if an iterable, the same type will be returned with all elements istr'ed
|
|
302
296
|
|
|
303
|
-
|
|
297
|
+
```
|
|
304
298
|
istr([0, 1, 4]) ==> [istr('0'), istr('1'), istr('4')]
|
|
305
299
|
istr((0, 1, 4)) ==> (istr('0'), istr('1'), istr('4'))
|
|
306
|
-
istr({0, 1, 4}) ==> `{istr('4'), istr('0'), istr('1')} # or similar
|
|
307
|
-
|
|
300
|
+
istr({0, 1, 4}) ==> `{istr('4'), istr('0'), istr('1')} # or similar
|
|
301
|
+
```
|
|
308
302
|
|
|
309
303
|
- if a range, an istr.range instance will be returned
|
|
310
304
|
|
|
311
|
-
|
|
312
|
-
istr(range(3))
|
|
313
|
-
list(istr(range(3)))
|
|
314
|
-
len(istr(range(3)))
|
|
315
|
-
|
|
305
|
+
```
|
|
306
|
+
istr(range(3)) ==> istr.range(3)
|
|
307
|
+
list(istr(range(3))) ==> [istr('0'), istr('1'), istr('2')]
|
|
308
|
+
len(istr(range(3))) ==> 3
|
|
309
|
+
```
|
|
316
310
|
|
|
317
311
|
- if an istr.range instance, the same istr.range will be returned
|
|
318
312
|
|
|
319
313
|
- if an istr, the same istr will be used
|
|
320
314
|
|
|
321
|
-
|
|
315
|
+
```
|
|
322
316
|
istr(istr('4')) ==> istr ('4')
|
|
323
|
-
|
|
317
|
+
```
|
|
324
318
|
|
|
325
319
|
#### More than one parameter for istr
|
|
326
320
|
It is possible to give more than one parameter, in which case a tuple
|
|
@@ -355,7 +349,7 @@ istr(n100).all_distinct() ==> False
|
|
|
355
349
|
|
|
356
350
|
#### reverse an istr
|
|
357
351
|
|
|
358
|
-
The method `istr.reversed()` will return
|
|
352
|
+
The method `istr.reversed()` will return an istr with the reversed content:
|
|
359
353
|
```
|
|
360
354
|
istr(456).reversed() ==> istr('654')
|
|
361
355
|
istr('0456').reversed() ==> istr('6540')
|
|
@@ -365,13 +359,13 @@ The same can -of course- be achieved with
|
|
|
365
359
|
istr(456)[::-1] ==> istr('654')
|
|
366
360
|
istr('0456')[::-1] ==> istr('6540')
|
|
367
361
|
```
|
|
368
|
-
|
|
369
|
-
>
|
|
370
|
-
>
|
|
371
|
-
>
|
|
372
|
-
>
|
|
373
|
-
>
|
|
374
|
-
>
|
|
362
|
+
##### Note
|
|
363
|
+
>
|
|
364
|
+
> It is possible to reverse a negative istr, but the result can't be interpreted as an int anymore.
|
|
365
|
+
>
|
|
366
|
+
> ```
|
|
367
|
+
> istr(-456).reversed() ==> TypeError
|
|
368
|
+
> ```
|
|
375
369
|
|
|
376
370
|
#### enumerate with istrs
|
|
377
371
|
|
|
@@ -379,8 +373,8 @@ The `istr.enumerate` method can be used just as the builtin enumerate function.
|
|
|
379
373
|
The iteration counter however is an istr rather than an int. E.g.
|
|
380
374
|
|
|
381
375
|
```
|
|
382
|
-
|
|
383
|
-
|
|
376
|
+
for i, c in istr.enumerate('abc'):
|
|
377
|
+
print(f'{repr(i)} {c}')
|
|
384
378
|
```
|
|
385
379
|
prints
|
|
386
380
|
```
|
|
@@ -414,7 +408,8 @@ The given argument(s) result in a range of digits.
|
|
|
414
408
|
- `<n>` ==> n
|
|
415
409
|
- `<n-m>` ==> n, n+1, ..., m
|
|
416
410
|
- `-n>` ==> 0, 1, ... n
|
|
417
|
-
- `n->` ==> n, n+1, ..., 9
|
|
411
|
+
- `n->` ==> n, n+1, ..., 9 if n is numeric (0-9), n, n+1, ... Z if n is a letter
|
|
412
|
+
- `'-'` ==> 0, 1, ..., 9
|
|
418
413
|
- `''` ==> 0, 1, ..., 9
|
|
419
414
|
|
|
420
415
|
(n and m must be digits between 0 and 9 or letters letters between A and Z)
|
|
@@ -460,9 +455,9 @@ will print `jstr('20')`
|
|
|
460
455
|
|
|
461
456
|
It is possible to control the way an `istr` instance will be repr'ed.
|
|
462
457
|
|
|
463
|
-
By default,
|
|
458
|
+
By default, `istr(5)` is represented as `istr('5')`.
|
|
464
459
|
|
|
465
|
-
With the istr.repr_mode() context manager, that can be changed:
|
|
460
|
+
With the `istr.repr_mode()` context manager, that can be changed:
|
|
466
461
|
```
|
|
467
462
|
with istr.repr_mode('str'):
|
|
468
463
|
five = istr(5)
|
|
@@ -480,7 +475,7 @@ This will print
|
|
|
480
475
|
5
|
|
481
476
|
istr('5')
|
|
482
477
|
```
|
|
483
|
-
If the repr_mode is `'int'` and the istr can't be interpreted as an int the string
|
|
478
|
+
If the repr_mode is `'int'` and the istr can't be interpreted as an int the string `?` will be returned:
|
|
484
479
|
|
|
485
480
|
```
|
|
486
481
|
with istr.repr_mode('int'):
|
|
@@ -491,12 +486,12 @@ If the repr_mode is `'int'` and the istr can't be interpreted as an int the stri
|
|
|
491
486
|
This will print
|
|
492
487
|
|
|
493
488
|
```
|
|
494
|
-
|
|
489
|
+
?
|
|
495
490
|
```
|
|
496
491
|
|
|
497
|
-
|
|
498
|
-
>
|
|
499
|
-
>
|
|
492
|
+
##### Note
|
|
493
|
+
>
|
|
494
|
+
> The way an `istr` is represented is determined at initialization.
|
|
500
495
|
|
|
501
496
|
It is also possible to set the repr mode without a context manager:
|
|
502
497
|
|
|
@@ -530,13 +525,13 @@ with istr.base(16):
|
|
|
530
525
|
a = istr('7fff')
|
|
531
526
|
print(int(a))
|
|
532
527
|
|
|
533
|
-
b = istr(
|
|
528
|
+
b = istr(127)
|
|
534
529
|
print(repr(b))
|
|
535
530
|
```
|
|
536
531
|
This will result in
|
|
537
532
|
```
|
|
538
533
|
32767
|
|
539
|
-
istr('
|
|
534
|
+
istr('7F')
|
|
540
535
|
```
|
|
541
536
|
All calculations are done in the decimal 10 system.
|
|
542
537
|
|
|
@@ -559,7 +554,7 @@ will result in `10`.
|
|
|
559
554
|
|
|
560
555
|
#### Changing the format of the string
|
|
561
556
|
|
|
562
|
-
When an istr is initialized with a string the istr will be
|
|
557
|
+
When an istr is initialized with a string the istr will be always stored as such.
|
|
563
558
|
|
|
564
559
|
```
|
|
565
560
|
repr('4')) ==> istr('4')
|
|
@@ -573,7 +568,7 @@ For initializing with an int (or other numeric) value, the string is by default
|
|
|
573
568
|
repr(4)) ==> istr('4')
|
|
574
569
|
```
|
|
575
570
|
|
|
576
|
-
|
|
571
|
+
With the `istr.int_format()` context manager this behavior can be changed.
|
|
577
572
|
If the format specifier is a number, most likely a single digit, that
|
|
578
573
|
will be the minimum number of characters in the string:
|
|
579
574
|
|
|
@@ -606,24 +601,10 @@ istr('012')
|
|
|
606
601
|
istr('123')
|
|
607
602
|
istr('1234')
|
|
608
603
|
```
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
> > if a string is used to initialize an istr AND that string can be interpreted as an int. the string will reformatted:
|
|
612
|
-
> >
|
|
613
|
-
> > ```
|
|
614
|
-
> > with istr.int_format('03'):
|
|
615
|
-
> > print(repr(istr(12)))
|
|
616
|
-
> > ```
|
|
617
|
-
> >
|
|
618
|
-
> > will result in
|
|
619
|
-
> >
|
|
620
|
-
> > ```
|
|
621
|
-
> > istr('0012')
|
|
622
|
-
> > ```
|
|
604
|
+
|
|
605
|
+
##### Note
|
|
623
606
|
>
|
|
624
|
-
>
|
|
625
|
-
> >
|
|
626
|
-
> > For bases other than 10, the string will never be reformatted!
|
|
607
|
+
> For bases other than 10, the string will never be reformatted!
|
|
627
608
|
|
|
628
609
|
### Overview of operations
|
|
629
610
|
|
|
@@ -663,6 +644,8 @@ There's an extensive pytest script in the `\tests` directory.
|
|
|
663
644
|
|
|
664
645
|
This script also shows clearly the ways istr can be used, including several edge cases. Highly recommended to have a look at.
|
|
665
646
|
|
|
647
|
+
|
|
648
|
+
|
|
666
649
|
### Badges
|
|
667
650
|
  
|
|
668
651
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|