istr-python 0.0.8__tar.gz → 0.1.0__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-0.0.8/istr_python.egg-info → istr_python-0.1.0}/PKG-INFO +165 -7
- istr_python-0.0.8/PKG-INFO → istr_python-0.1.0/README.md +408 -263
- istr_python-0.1.0/istr/istr.py +509 -0
- istr_python-0.0.8/README.md → istr_python-0.1.0/istr_python.egg-info/PKG-INFO +421 -250
- {istr_python-0.0.8 → istr_python-0.1.0}/pyproject.toml +1 -1
- {istr_python-0.0.8 → istr_python-0.1.0}/tests/test_istr.py +69 -6
- istr_python-0.0.8/istr/istr.py +0 -397
- {istr_python-0.0.8 → istr_python-0.1.0}/istr/__init__.py +0 -0
- {istr_python-0.0.8 → istr_python-0.1.0}/istr/install istr.py +0 -0
- {istr_python-0.0.8 → istr_python-0.1.0}/istr_python.egg-info/SOURCES.txt +0 -0
- {istr_python-0.0.8 → istr_python-0.1.0}/istr_python.egg-info/dependency_links.txt +0 -0
- {istr_python-0.0.8 → istr_python-0.1.0}/istr_python.egg-info/top_level.txt +0 -0
- {istr_python-0.0.8 → istr_python-0.1.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: istr-python
|
|
3
|
-
Version: 0.0
|
|
3
|
+
Version: 0.1.0
|
|
4
4
|
Summary: istr is a module to use strings as if they were integers.
|
|
5
5
|
Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/salabim/istr
|
|
@@ -11,6 +11,8 @@ Classifier: Programming Language :: Python :: 3 :: Only
|
|
|
11
11
|
Requires-Python: >=3.7
|
|
12
12
|
Description-Content-Type: text/markdown
|
|
13
13
|
|
|
14
|
+
<img src="https://www.salabim.org/istr.png" width=500>
|
|
15
|
+
|
|
14
16
|
# Introduction
|
|
15
17
|
|
|
16
18
|
The istr module has exactly one class: istr.
|
|
@@ -76,7 +78,7 @@ And now we can do
|
|
|
76
78
|
print(x == 20)
|
|
77
79
|
print(x == "20")
|
|
78
80
|
```
|
|
79
|
-
resulting in two times `True`. That's because
|
|
81
|
+
resulting in two times `True`. That's because istrs instances are treated as int, although they are strings.
|
|
80
82
|
|
|
81
83
|
That means that we can also say
|
|
82
84
|
```
|
|
@@ -128,13 +130,14 @@ abs(four)
|
|
|
128
130
|
The bool operator works on the integer value of an istr. So
|
|
129
131
|
`bool("0")` ==> `False`
|
|
130
132
|
`bool("1")` ==> `True`
|
|
133
|
+
The code
|
|
131
134
|
```
|
|
132
135
|
if istr("0"):
|
|
133
136
|
print("True")
|
|
134
137
|
else:
|
|
135
138
|
print("False")
|
|
136
139
|
```
|
|
137
|
-
|
|
140
|
+
will print `False`
|
|
138
141
|
|
|
139
142
|
For the in operator, an istr is treated as an ordinary string, although it is possible to use ints as well:
|
|
140
143
|
```
|
|
@@ -168,17 +171,20 @@ is
|
|
|
168
171
|
|
|
169
172
|
`"0 1 2 3 4 5 6 7 8 9 10 11"`
|
|
170
173
|
|
|
171
|
-
# Using other values for istr than
|
|
172
|
-
Apart from with simple int or str, istr can be initialized with
|
|
174
|
+
# Using other values for istr than numeric value or str
|
|
175
|
+
Apart from with simple numeric (to be interpreted as an int) or str, istr can be initialized with
|
|
176
|
+
several other types:
|
|
173
177
|
|
|
174
|
-
- if a dict (or subtype of dict), the same type dict will be returned with all values istr
|
|
178
|
+
- if a dict (or subtype of dict), the same type dict will be returned with all values istr'ed
|
|
175
179
|
|
|
176
180
|
`istr({0: 0, 1: 1, 2: 4})` ==> `{0: istr("0"), 1: istr("1"), 2: istr("4")}`
|
|
181
|
+
|
|
177
182
|
- if an iterator, the iterator will be mapped with istr
|
|
178
183
|
|
|
179
184
|
`istr(i * i for i in range(3))` ==> `<map object>`
|
|
180
185
|
|
|
181
186
|
`list(istr(i * i for i in range(3)))` ==> `[istr("0"), istr("1"), istr("4")]`
|
|
187
|
+
|
|
182
188
|
- if an iterable, the same type will be returned with all elements istr'ed
|
|
183
189
|
|
|
184
190
|
`istr([0, 1, 4])` ==> `[istr("0"), istr("1"), istr("4")]`
|
|
@@ -186,6 +192,7 @@ Apart from with simple int or str, istr can be initialized with
|
|
|
186
192
|
`istr((0, 1, 4))` ==> `(istr("0"), istr("1"), istr("4"))`
|
|
187
193
|
|
|
188
194
|
`istr({0, 1, 4})` ==> `{istr("4"), istr("0"), istr("1")} # or similar`
|
|
195
|
+
|
|
189
196
|
- if a range, an istr.range instance will be returned
|
|
190
197
|
|
|
191
198
|
`istr(range(3))` ==> `istr.range(3)`
|
|
@@ -194,6 +201,8 @@ Apart from with simple int or str, istr can be initialized with
|
|
|
194
201
|
|
|
195
202
|
`len(istr(range(3)))` ==> `3`
|
|
196
203
|
|
|
204
|
+
- if an istr.range instance, the same istr.range will be returned
|
|
205
|
+
|
|
197
206
|
# More than one parameter for istr
|
|
198
207
|
It is possible to give more than one parameter, in which case a tuple
|
|
199
208
|
of the istrs of the parameters will be returned, which can be handy
|
|
@@ -245,6 +254,15 @@ istr('1') b
|
|
|
245
254
|
istr('2') c
|
|
246
255
|
```
|
|
247
256
|
|
|
257
|
+
# concatenate an iterable
|
|
258
|
+
|
|
259
|
+
The `istr.concat1 method can be useful to map all items of an iterable
|
|
260
|
+
to `istr` and then concatenate these.
|
|
261
|
+
|
|
262
|
+
`list(istr.concat(((1,2),(3,4)))` ==> `istr([12,34])`
|
|
263
|
+
|
|
264
|
+
`list(istr.concat(itertools.permutations(range(3),2)))` ==> `[istr('01'), istr('02'), istr('10'), istr('12'), istr('20'), istr('21')]`
|
|
265
|
+
|
|
248
266
|
# Subclassing istr
|
|
249
267
|
When a class is derived from istr, all methods will return that newly derived class.
|
|
250
268
|
|
|
@@ -257,7 +275,147 @@ print(repr(jstr(4) * jstr(5)))
|
|
|
257
275
|
```
|
|
258
276
|
will print `jstr('20')`
|
|
259
277
|
|
|
278
|
+
# Changing the way repr works
|
|
279
|
+
|
|
280
|
+
It is possible to control the way an `istr` instance will be repr'ed.
|
|
281
|
+
|
|
282
|
+
By default, the `istr('5')` is represented as `istr('5')`.
|
|
283
|
+
|
|
284
|
+
With the istr.repr_mode() context manager, that can be changed:
|
|
285
|
+
```
|
|
286
|
+
with istr.repr_mode("str"):
|
|
287
|
+
five = istr('5')
|
|
288
|
+
print(repr(five))
|
|
289
|
+
with istr.repr_mode("int"):
|
|
290
|
+
five = istr('5')
|
|
291
|
+
print(repr(five))
|
|
292
|
+
with istr.repr_mode("istr"):
|
|
293
|
+
five = istr('5')
|
|
294
|
+
print(repr(five))
|
|
295
|
+
```
|
|
296
|
+
This will print
|
|
297
|
+
```
|
|
298
|
+
'5'
|
|
299
|
+
5
|
|
300
|
+
istr('5')
|
|
301
|
+
```
|
|
302
|
+
Note that the way an `istr` is represented is determined at initialization.
|
|
303
|
+
|
|
304
|
+
It is also possible to set the repr mode without a context manager:
|
|
305
|
+
|
|
306
|
+
```
|
|
307
|
+
istr.repr_mode("str")
|
|
308
|
+
five = istr('5')
|
|
309
|
+
print(repr(five))
|
|
310
|
+
```
|
|
311
|
+
This will print
|
|
312
|
+
```
|
|
313
|
+
'5'
|
|
314
|
+
```
|
|
315
|
+
Finally, the current repr mode can be queried with `istr.repr_mode()`. So upon start:
|
|
316
|
+
```
|
|
317
|
+
print(repr(istr.repr_mode()))
|
|
318
|
+
```
|
|
319
|
+
will output `istr`.
|
|
320
|
+
|
|
321
|
+
# Changing the base system
|
|
322
|
+
|
|
323
|
+
By default, `istr` works in base 10. However it is possible to change the base system with the `istr.base()` context manager / method.
|
|
324
|
+
|
|
325
|
+
Any base between 2 and 36 may be used.
|
|
326
|
+
|
|
327
|
+
Note that the integer is always stored in base 10 mode, but the string
|
|
328
|
+
representation will reflect the chosen base (at time of initialization).
|
|
329
|
+
|
|
330
|
+
Some examples:
|
|
331
|
+
```
|
|
332
|
+
with istr.base(16):
|
|
333
|
+
a = istr("7fff")
|
|
334
|
+
print(int(a))
|
|
335
|
+
|
|
336
|
+
b = istr(15)
|
|
337
|
+
print(repr(b))
|
|
338
|
+
```
|
|
339
|
+
This will result in
|
|
340
|
+
```
|
|
341
|
+
32767
|
|
342
|
+
istr('F')
|
|
343
|
+
```
|
|
344
|
+
All calculations are done in the decimal 10 system.
|
|
345
|
+
|
|
346
|
+
Note that the way an `istr` is interpreted is determined at initialization.
|
|
347
|
+
|
|
348
|
+
It is also possible to set the repr mode without a context manager:
|
|
349
|
+
```
|
|
350
|
+
istr.base(16)
|
|
351
|
+
print(int(istr("7fff")))
|
|
352
|
+
```
|
|
353
|
+
This will print
|
|
354
|
+
```
|
|
355
|
+
32767
|
|
356
|
+
```
|
|
357
|
+
Finally, the current base can be queried with `istr.base()`, so upon start:
|
|
358
|
+
```
|
|
359
|
+
print(istr.base())
|
|
360
|
+
```
|
|
361
|
+
will result in `10`.
|
|
362
|
+
|
|
363
|
+
# Changing the format of the string
|
|
364
|
+
|
|
365
|
+
By default, `istr` does not change the way an istr is stored when a str is to initialize:
|
|
366
|
+
|
|
367
|
+
`repr('4'))` ==> `istr('4')`
|
|
368
|
+
|
|
369
|
+
`repr(' 4'))` ==> `istr(' 4')`
|
|
370
|
+
|
|
371
|
+
`repr('4 '))` ==> `istr('4 ')`
|
|
372
|
+
|
|
373
|
+
For initializing with an int (or other numeric) value, the string is simply the str representation
|
|
374
|
+
|
|
375
|
+
`repr(4))` ==> `istr('4')`
|
|
376
|
+
|
|
377
|
+
With the `istr.format()` context manager this behavior can be changed.
|
|
378
|
+
If the format specifier is a number, most likely a single digit, that
|
|
379
|
+
will be the minimum number of characters in the string:
|
|
380
|
+
```
|
|
381
|
+
with istr.format("3"):
|
|
382
|
+
print(repr(istr(1)))
|
|
383
|
+
print(repr(istr(12)))
|
|
384
|
+
print(repr(istr(123)))
|
|
385
|
+
print(repr(istr(1234)))
|
|
386
|
+
```
|
|
387
|
+
will print
|
|
388
|
+
```
|
|
389
|
+
istr(' 1')
|
|
390
|
+
istr(' 12')
|
|
391
|
+
istr('123')
|
|
392
|
+
istr('1234')
|
|
393
|
+
```
|
|
394
|
+
If the string starts with a `0`, the string will be zero filled:
|
|
395
|
+
```
|
|
396
|
+
with istr.format("03"):
|
|
397
|
+
print(repr(istr(1)))
|
|
398
|
+
print(repr(istr(12)))
|
|
399
|
+
print(repr(istr(123)))
|
|
400
|
+
print(repr(istr(1234)))
|
|
401
|
+
```
|
|
402
|
+
will print
|
|
403
|
+
```
|
|
404
|
+
istr('001')
|
|
405
|
+
istr('012')
|
|
406
|
+
istr('123')
|
|
407
|
+
istr('1234')
|
|
408
|
+
```
|
|
409
|
+
Note that if a format other than the default `''` is used, the string will reformatted even if the `istr` is specified with a string:
|
|
410
|
+
```
|
|
411
|
+
with istr.format("03"):
|
|
412
|
+
print(repr(istr(" 12 ")))
|
|
413
|
+
```
|
|
414
|
+
will result in `istr('0012')`
|
|
415
|
+
|
|
416
|
+
Remark: For bases other than 10, the string will never be reformatted!
|
|
417
|
+
|
|
260
418
|
# Test script
|
|
261
|
-
There's an extensive pytest script in the
|
|
419
|
+
There's an extensive pytest script in the `\tests` directory.
|
|
262
420
|
|
|
263
421
|
This script also shows clearly the ways istr can be used.
|