istr-python 0.0.6__tar.gz → 0.0.7__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.6/istr_python.egg-info → istr_python-0.0.7}/PKG-INFO +71 -11
- istr_python-0.0.6/PKG-INFO → istr_python-0.0.7/README.md +230 -185
- {istr_python-0.0.6 → istr_python-0.0.7}/istr/istr.py +3 -2
- istr_python-0.0.6/README.md → istr_python-0.0.7/istr_python.egg-info/PKG-INFO +245 -177
- istr_python-0.0.7/pyproject.toml +30 -0
- istr_python-0.0.6/pyproject.toml +0 -15
- {istr_python-0.0.6 → istr_python-0.0.7}/istr/__init__.py +0 -0
- {istr_python-0.0.6 → istr_python-0.0.7}/istr/install istr.py +0 -0
- {istr_python-0.0.6 → istr_python-0.0.7}/istr_python.egg-info/SOURCES.txt +0 -0
- {istr_python-0.0.6 → istr_python-0.0.7}/istr_python.egg-info/dependency_links.txt +0 -0
- {istr_python-0.0.6 → istr_python-0.0.7}/istr_python.egg-info/top_level.txt +0 -0
- {istr_python-0.0.6 → istr_python-0.0.7}/setup.cfg +0 -0
- {istr_python-0.0.6 → istr_python-0.0.7}/tests/test_istr.py +0 -0
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: istr-python
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.7
|
|
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
|
+
Project-URL: Homepage, https://github.com/salabim/istr
|
|
7
|
+
Project-URL: Documentation, https://github.com/salabim/istr
|
|
8
|
+
Project-URL: Repository, https://github.com/salabim/istr
|
|
9
|
+
Project-URL: Issues, https://github.com/salabim/istr
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
6
13
|
Requires-Python: >=3.7
|
|
7
14
|
Description-Content-Type: text/markdown
|
|
8
15
|
|
|
@@ -10,7 +17,7 @@ Description-Content-Type: text/markdown
|
|
|
10
17
|
|
|
11
18
|
The istr module has exactly one class: istr.
|
|
12
19
|
|
|
13
|
-
With this it is possible to interpret
|
|
20
|
+
With this it is possible to interpret strings as if they were integers.
|
|
14
21
|
|
|
15
22
|
This can be very handy for solving puzzles, but also for other purposes. For instance the
|
|
16
23
|
famous send more money puzzle
|
|
@@ -26,7 +33,7 @@ import itertools
|
|
|
26
33
|
from istr import istr
|
|
27
34
|
|
|
28
35
|
for s, e, n, d, m, o, r, y in istr(itertools.permutations(range(10), 8)):
|
|
29
|
-
if m and ((s
|
|
36
|
+
if m and ((s|e|n|d) + (m|o|r|e) == (m|o|n|e|y)):
|
|
30
37
|
print(f" {s|e|n|d}")
|
|
31
38
|
print(f" {m|o|r|e}")
|
|
32
39
|
print("-----")
|
|
@@ -60,7 +67,7 @@ Now we can define some istrs:
|
|
|
60
67
|
four = istr("4")
|
|
61
68
|
five = istr("5")
|
|
62
69
|
```
|
|
63
|
-
|
|
70
|
+
Then we can do
|
|
64
71
|
```
|
|
65
72
|
x= four * five
|
|
66
73
|
```
|
|
@@ -86,7 +93,17 @@ print(four + five)
|
|
|
86
93
|
```
|
|
87
94
|
prints `9`, as istr are treated as ints.
|
|
88
95
|
|
|
89
|
-
|
|
96
|
+
Please note that `four` and `five` could have also be initialized with
|
|
97
|
+
```
|
|
98
|
+
four = istr(4)
|
|
99
|
+
five = istr(5)
|
|
100
|
+
```
|
|
101
|
+
or even
|
|
102
|
+
```
|
|
103
|
+
four, five = istr(4, 5)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
But how can we concatenate istrs? Just use the or operator (|):
|
|
90
107
|
```
|
|
91
108
|
print(four | five)
|
|
92
109
|
```
|
|
@@ -100,9 +117,9 @@ That means that
|
|
|
100
117
|
```
|
|
101
118
|
is `istr("9")`.
|
|
102
119
|
|
|
103
|
-
In order to
|
|
120
|
+
In order to repeat a string in the usual sense, you cannot use `3 * four`, as that woud be `12`.
|
|
104
121
|
|
|
105
|
-
We use the matrix multiplication operator (@) for this. So `3 @ four` is `444`.
|
|
122
|
+
We use the matrix multiplication operator (@) for this. So `3 @ four` is `444`. As is `four @ 3`.
|
|
106
123
|
|
|
107
124
|
Also allowed are
|
|
108
125
|
```
|
|
@@ -110,7 +127,7 @@ abs(four)
|
|
|
110
127
|
-four
|
|
111
128
|
```
|
|
112
129
|
|
|
113
|
-
The bool operator works
|
|
130
|
+
The bool operator works on the integer value of an istr. So
|
|
114
131
|
`bool('0')` ==> `False`
|
|
115
132
|
`bool('1')` ==> `True`
|
|
116
133
|
```
|
|
@@ -121,7 +138,7 @@ else:
|
|
|
121
138
|
```
|
|
122
139
|
this will print `False`
|
|
123
140
|
|
|
124
|
-
For the in operator
|
|
141
|
+
For the in operator, an istr is treated as an ordinary string, although it is possible to use ints as well:
|
|
125
142
|
```
|
|
126
143
|
"34" in istr(1234)
|
|
127
144
|
34 in istr(1234)
|
|
@@ -135,7 +152,7 @@ Note that all calculations are strictly integer calculations. That means that if
|
|
|
135
152
|
Also divisions are always floor divisions!
|
|
136
153
|
|
|
137
154
|
There's a special case for `istr("")`. This is a proper empty string, but also represents the value of 0.
|
|
138
|
-
That is to allow for istr("").join(i for i in "01234)"
|
|
155
|
+
That is to allow for `istr("").join(i for i in "01234)"`
|
|
139
156
|
|
|
140
157
|
Sorting a list of istrs is based on the integer value, not the string. So
|
|
141
158
|
|
|
@@ -156,7 +173,35 @@ is
|
|
|
156
173
|
# Using other values for istr than int or str
|
|
157
174
|
Apart from with simple int or str, istr can be initialized with
|
|
158
175
|
|
|
159
|
-
-
|
|
176
|
+
- if a dict (or subtype of dict), the same type dict will be returned with all values istr'ed
|
|
177
|
+
|
|
178
|
+
`istr({0: 0, 1: 1, 2: 4})` ==> `{0: istr('0'), 1: istr('1'), 2: istr('4')}`
|
|
179
|
+
- if an iterator, the iterator will be mapped with istr
|
|
180
|
+
|
|
181
|
+
`istr(i * i for i in range(3))` ==> `<map object>`
|
|
182
|
+
|
|
183
|
+
`list(istr(i * i for i in range(3)))` ==> `[istr('0'), istr('1'), istr('4')]`
|
|
184
|
+
- if an iterable, the same type will be returned with all elements istr'ed
|
|
185
|
+
|
|
186
|
+
`istr([0, 1, 4])` ==> `[istr('0'), istr('1'), istr('4')]`
|
|
187
|
+
|
|
188
|
+
`istr((0, 1, 4))` ==> `(istr('0'), istr('1'), istr('4'))`
|
|
189
|
+
|
|
190
|
+
`istr({0, 1, 4})` ==> `{istr('4'), istr('0'), istr('1')} # or similar`
|
|
191
|
+
- if a range, an istr.range instance will be returned
|
|
192
|
+
|
|
193
|
+
`istr(range(3))` ==> `istr.range(3)`
|
|
194
|
+
|
|
195
|
+
`list(istr(range(3)))` ==> `[istr('0'), istr('1'), istr('2')]`
|
|
196
|
+
|
|
197
|
+
`len(istr(range(3)))` ==> `3`
|
|
198
|
+
|
|
199
|
+
# More than one parameter for istr
|
|
200
|
+
It is possible to give more than one parameter, in which case a tuple
|
|
201
|
+
of the istrs of the parameters will be returned, which can be handy
|
|
202
|
+
to unpack multiple values, e.g.
|
|
203
|
+
|
|
204
|
+
`a, b, c = istr(5, 6, 7)` ==> `a=istr("5') , b=istr("6"), c=istr("7")`
|
|
160
205
|
|
|
161
206
|
# Additional methods
|
|
162
207
|
It is possible to test for even/odd with the
|
|
@@ -179,7 +224,22 @@ result:
|
|
|
179
224
|
istr('654')
|
|
180
225
|
istr('6540')
|
|
181
226
|
```
|
|
227
|
+
The same can -of course- be achieved with
|
|
228
|
+
```
|
|
229
|
+
print(repr(istr(456)[::-1]))
|
|
230
|
+
print(repr(istr("0456")[::-1]))
|
|
231
|
+
```
|
|
182
232
|
Note that is impossible to reverse a negative istr.
|
|
183
233
|
|
|
234
|
+
|
|
184
235
|
# Subclassing istr
|
|
236
|
+
When a class is derived from istr, all methods will return that newly derived class.
|
|
185
237
|
|
|
238
|
+
E.g.
|
|
239
|
+
```
|
|
240
|
+
class jstr(istr):
|
|
241
|
+
...
|
|
242
|
+
|
|
243
|
+
print(repr(jstr(4) * jstr(5)))
|
|
244
|
+
```
|
|
245
|
+
will print `jstr('20')`
|
|
@@ -1,185 +1,230 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
print(x
|
|
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
|
-
print("
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
```
|
|
131
|
-
istr
|
|
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
|
-
istr('
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
#
|
|
185
|
-
|
|
1
|
+
# Introduction
|
|
2
|
+
|
|
3
|
+
The istr module has exactly one class: istr.
|
|
4
|
+
|
|
5
|
+
With this it is possible to interpret strings as if they were integers.
|
|
6
|
+
|
|
7
|
+
This can be very handy for solving puzzles, but also for other purposes. For instance the
|
|
8
|
+
famous send more money puzzle
|
|
9
|
+
```
|
|
10
|
+
S E N D
|
|
11
|
+
M O R E
|
|
12
|
+
--------- +
|
|
13
|
+
M O N E Y
|
|
14
|
+
```
|
|
15
|
+
can be nicely, albeit not very efficient, coded as:
|
|
16
|
+
```
|
|
17
|
+
import itertools
|
|
18
|
+
from istr import istr
|
|
19
|
+
|
|
20
|
+
for s, e, n, d, m, o, r, y in istr(itertools.permutations(range(10), 8)):
|
|
21
|
+
if m and ((s|e|n|d) + (m|o|r|e) == (m|o|n|e|y)):
|
|
22
|
+
print(f" {s|e|n|d}")
|
|
23
|
+
print(f" {m|o|r|e}")
|
|
24
|
+
print("-----")
|
|
25
|
+
print(f"{m|o|n|e|y}")
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
And it is a nice demonstration of extending a class (str) with extra and changed functionality.
|
|
29
|
+
|
|
30
|
+
# Installation
|
|
31
|
+
Installing istr with pip is easy.
|
|
32
|
+
```
|
|
33
|
+
$ pip install istr-python
|
|
34
|
+
```
|
|
35
|
+
or when you want to upgrade,
|
|
36
|
+
```
|
|
37
|
+
$ pip install istr-python --upgrade
|
|
38
|
+
```
|
|
39
|
+
Alternatively, istr.py can be just copied into you current work directory from GitHub (https://github.com/salabim/istr).
|
|
40
|
+
|
|
41
|
+
No dependencies!
|
|
42
|
+
|
|
43
|
+
# Usage
|
|
44
|
+
Just start with
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
from istr import istr
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Now we can define some istrs:
|
|
51
|
+
```
|
|
52
|
+
four = istr("4")
|
|
53
|
+
five = istr("5")
|
|
54
|
+
```
|
|
55
|
+
Then we can do
|
|
56
|
+
```
|
|
57
|
+
x= four * five
|
|
58
|
+
```
|
|
59
|
+
, after which x is `istr("20")`
|
|
60
|
+
|
|
61
|
+
And now we can do
|
|
62
|
+
```
|
|
63
|
+
print(x == 20)
|
|
64
|
+
print(x == "20")
|
|
65
|
+
```
|
|
66
|
+
resulting in two times `True`. That's because istr instances are treated as int, although they are strings.
|
|
67
|
+
|
|
68
|
+
That means that we can also say
|
|
69
|
+
```
|
|
70
|
+
print(x < 30)
|
|
71
|
+
print(x >= "10")
|
|
72
|
+
```
|
|
73
|
+
again resulting in two times `True`.
|
|
74
|
+
|
|
75
|
+
In contrast to an ordinary string
|
|
76
|
+
```
|
|
77
|
+
print(four + five)
|
|
78
|
+
```
|
|
79
|
+
prints `9`, as istr are treated as ints.
|
|
80
|
+
|
|
81
|
+
Please note that `four` and `five` could have also be initialized with
|
|
82
|
+
```
|
|
83
|
+
four = istr(4)
|
|
84
|
+
five = istr(5)
|
|
85
|
+
```
|
|
86
|
+
or even
|
|
87
|
+
```
|
|
88
|
+
four, five = istr(4, 5)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
But how can we concatenate istrs? Just use the or operator (|):
|
|
92
|
+
```
|
|
93
|
+
print(four | five)
|
|
94
|
+
```
|
|
95
|
+
will output `45`.
|
|
96
|
+
|
|
97
|
+
And the result is again an istr.
|
|
98
|
+
|
|
99
|
+
That means that
|
|
100
|
+
```
|
|
101
|
+
(four | five) / 3
|
|
102
|
+
```
|
|
103
|
+
is `istr("9")`.
|
|
104
|
+
|
|
105
|
+
In order to repeat a string in the usual sense, you cannot use `3 * four`, as that woud be `12`.
|
|
106
|
+
|
|
107
|
+
We use the matrix multiplication operator (@) for this. So `3 @ four` is `444`. As is `four @ 3`.
|
|
108
|
+
|
|
109
|
+
Also allowed are
|
|
110
|
+
```
|
|
111
|
+
abs(four)
|
|
112
|
+
-four
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
The bool operator works on the integer value of an istr. So
|
|
116
|
+
`bool('0')` ==> `False`
|
|
117
|
+
`bool('1')` ==> `True`
|
|
118
|
+
```
|
|
119
|
+
if istr('0'):
|
|
120
|
+
print("True")
|
|
121
|
+
else:
|
|
122
|
+
print("False")
|
|
123
|
+
```
|
|
124
|
+
this will print `False`
|
|
125
|
+
|
|
126
|
+
For the in operator, an istr is treated as an ordinary string, although it is possible to use ints as well:
|
|
127
|
+
```
|
|
128
|
+
"34" in istr(1234)
|
|
129
|
+
34 in istr(1234)
|
|
130
|
+
```
|
|
131
|
+
On the left hand side an istr is always treated as a string:
|
|
132
|
+
```
|
|
133
|
+
istr(1234) in "01234566890ABCDEF"
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Note that all calculations are strictly integer calculations. That means that if a float variale is ever produced it will be converted to an int.
|
|
137
|
+
Also divisions are always floor divisions!
|
|
138
|
+
|
|
139
|
+
There's a special case for `istr("")`. This is a proper empty string, but also represents the value of 0.
|
|
140
|
+
That is to allow for `istr("").join(i for i in "01234)"`
|
|
141
|
+
|
|
142
|
+
Sorting a list of istrs is based on the integer value, not the string. So
|
|
143
|
+
|
|
144
|
+
`' '.join(sorted('1 3 2 4 5 6 11 7 9 8 10 12 0'.split()))`
|
|
145
|
+
|
|
146
|
+
is
|
|
147
|
+
|
|
148
|
+
`'0 1 10 11 2 3 4 5 6 7 8 9'`
|
|
149
|
+
|
|
150
|
+
,whereas
|
|
151
|
+
|
|
152
|
+
`' '.join(sorted(istr('1 3 2 4 5 6 11 7 9 8 10 12 0'.split())))`
|
|
153
|
+
|
|
154
|
+
is
|
|
155
|
+
|
|
156
|
+
`'0 1 2 3 4 5 6 7 8 9 10 11'`
|
|
157
|
+
|
|
158
|
+
# Using other values for istr than int or str
|
|
159
|
+
Apart from with simple int or str, istr can be initialized with
|
|
160
|
+
|
|
161
|
+
- if a dict (or subtype of dict), the same type dict will be returned with all values istr'ed
|
|
162
|
+
|
|
163
|
+
`istr({0: 0, 1: 1, 2: 4})` ==> `{0: istr('0'), 1: istr('1'), 2: istr('4')}`
|
|
164
|
+
- if an iterator, the iterator will be mapped with istr
|
|
165
|
+
|
|
166
|
+
`istr(i * i for i in range(3))` ==> `<map object>`
|
|
167
|
+
|
|
168
|
+
`list(istr(i * i for i in range(3)))` ==> `[istr('0'), istr('1'), istr('4')]`
|
|
169
|
+
- if an iterable, the same type will be returned with all elements istr'ed
|
|
170
|
+
|
|
171
|
+
`istr([0, 1, 4])` ==> `[istr('0'), istr('1'), istr('4')]`
|
|
172
|
+
|
|
173
|
+
`istr((0, 1, 4))` ==> `(istr('0'), istr('1'), istr('4'))`
|
|
174
|
+
|
|
175
|
+
`istr({0, 1, 4})` ==> `{istr('4'), istr('0'), istr('1')} # or similar`
|
|
176
|
+
- if a range, an istr.range instance will be returned
|
|
177
|
+
|
|
178
|
+
`istr(range(3))` ==> `istr.range(3)`
|
|
179
|
+
|
|
180
|
+
`list(istr(range(3)))` ==> `[istr('0'), istr('1'), istr('2')]`
|
|
181
|
+
|
|
182
|
+
`len(istr(range(3)))` ==> `3`
|
|
183
|
+
|
|
184
|
+
# More than one parameter for istr
|
|
185
|
+
It is possible to give more than one parameter, in which case a tuple
|
|
186
|
+
of the istrs of the parameters will be returned, which can be handy
|
|
187
|
+
to unpack multiple values, e.g.
|
|
188
|
+
|
|
189
|
+
`a, b, c = istr(5, 6, 7)` ==> `a=istr("5') , b=istr("6"), c=istr("7")`
|
|
190
|
+
|
|
191
|
+
# Additional methods
|
|
192
|
+
It is possible to test for even/odd with the
|
|
193
|
+
|
|
194
|
+
`is_even` and `is_odd` method, e.g.
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
print(istr(4).is_even())
|
|
198
|
+
print(istr(5).is_odd())
|
|
199
|
+
```
|
|
200
|
+
This will print `True` twice.
|
|
201
|
+
|
|
202
|
+
The method `istr.reversed()` will return the an istr with the reversed content:
|
|
203
|
+
```
|
|
204
|
+
print(repr(istr(456).reversed()))
|
|
205
|
+
print(repr(istr("0456").reversed()))
|
|
206
|
+
```
|
|
207
|
+
result:
|
|
208
|
+
```
|
|
209
|
+
istr('654')
|
|
210
|
+
istr('6540')
|
|
211
|
+
```
|
|
212
|
+
The same can -of course- be achieved with
|
|
213
|
+
```
|
|
214
|
+
print(repr(istr(456)[::-1]))
|
|
215
|
+
print(repr(istr("0456")[::-1]))
|
|
216
|
+
```
|
|
217
|
+
Note that is impossible to reverse a negative istr.
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
# Subclassing istr
|
|
221
|
+
When a class is derived from istr, all methods will return that newly derived class.
|
|
222
|
+
|
|
223
|
+
E.g.
|
|
224
|
+
```
|
|
225
|
+
class jstr(istr):
|
|
226
|
+
...
|
|
227
|
+
|
|
228
|
+
print(repr(jstr(4) * jstr(5)))
|
|
229
|
+
```
|
|
230
|
+
will print `jstr('20')`
|
|
@@ -8,7 +8,7 @@ import contextlib
|
|
|
8
8
|
# | |\__ \| |_ | |
|
|
9
9
|
# |_||___/ \__||_| use strings as integers
|
|
10
10
|
|
|
11
|
-
__version__ = "0.0.
|
|
11
|
+
__version__ = "0.0.7"
|
|
12
12
|
|
|
13
13
|
class istr(str):
|
|
14
14
|
"""
|
|
@@ -38,7 +38,8 @@ class istr(str):
|
|
|
38
38
|
it is possible to give more than one parameter, in which case a tuple
|
|
39
39
|
of the istrs of the parameters will be returned, which can be handy
|
|
40
40
|
to multiple assign, e.g.
|
|
41
|
-
a, b, c = istr(5, 6, 7) ==> a=istr("5') , b=istr("6"), c=istr("7")
|
|
41
|
+
a, b, c = istr(5, 6, 7) ==> a=istr("5') , b=istr("6"), c=istr("7")
|
|
42
|
+
"""
|
|
42
43
|
|
|
43
44
|
_format = ""
|
|
44
45
|
|
|
@@ -1,177 +1,245 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: istr-python
|
|
3
|
+
Version: 0.0.7
|
|
4
|
+
Summary: istr is a module to use strings as if they were integers.
|
|
5
|
+
Author-email: Ruud van der Ham <rt.van.der.ham@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/salabim/istr
|
|
7
|
+
Project-URL: Documentation, https://github.com/salabim/istr
|
|
8
|
+
Project-URL: Repository, https://github.com/salabim/istr
|
|
9
|
+
Project-URL: Issues, https://github.com/salabim/istr
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Requires-Python: >=3.7
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# Introduction
|
|
17
|
+
|
|
18
|
+
The istr module has exactly one class: istr.
|
|
19
|
+
|
|
20
|
+
With this it is possible to interpret strings as if they were integers.
|
|
21
|
+
|
|
22
|
+
This can be very handy for solving puzzles, but also for other purposes. For instance the
|
|
23
|
+
famous send more money puzzle
|
|
24
|
+
```
|
|
25
|
+
S E N D
|
|
26
|
+
M O R E
|
|
27
|
+
--------- +
|
|
28
|
+
M O N E Y
|
|
29
|
+
```
|
|
30
|
+
can be nicely, albeit not very efficient, coded as:
|
|
31
|
+
```
|
|
32
|
+
import itertools
|
|
33
|
+
from istr import istr
|
|
34
|
+
|
|
35
|
+
for s, e, n, d, m, o, r, y in istr(itertools.permutations(range(10), 8)):
|
|
36
|
+
if m and ((s|e|n|d) + (m|o|r|e) == (m|o|n|e|y)):
|
|
37
|
+
print(f" {s|e|n|d}")
|
|
38
|
+
print(f" {m|o|r|e}")
|
|
39
|
+
print("-----")
|
|
40
|
+
print(f"{m|o|n|e|y}")
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
And it is a nice demonstration of extending a class (str) with extra and changed functionality.
|
|
44
|
+
|
|
45
|
+
# Installation
|
|
46
|
+
Installing istr with pip is easy.
|
|
47
|
+
```
|
|
48
|
+
$ pip install istr-python
|
|
49
|
+
```
|
|
50
|
+
or when you want to upgrade,
|
|
51
|
+
```
|
|
52
|
+
$ pip install istr-python --upgrade
|
|
53
|
+
```
|
|
54
|
+
Alternatively, istr.py can be just copied into you current work directory from GitHub (https://github.com/salabim/istr).
|
|
55
|
+
|
|
56
|
+
No dependencies!
|
|
57
|
+
|
|
58
|
+
# Usage
|
|
59
|
+
Just start with
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
from istr import istr
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Now we can define some istrs:
|
|
66
|
+
```
|
|
67
|
+
four = istr("4")
|
|
68
|
+
five = istr("5")
|
|
69
|
+
```
|
|
70
|
+
Then we can do
|
|
71
|
+
```
|
|
72
|
+
x= four * five
|
|
73
|
+
```
|
|
74
|
+
, after which x is `istr("20")`
|
|
75
|
+
|
|
76
|
+
And now we can do
|
|
77
|
+
```
|
|
78
|
+
print(x == 20)
|
|
79
|
+
print(x == "20")
|
|
80
|
+
```
|
|
81
|
+
resulting in two times `True`. That's because istr instances are treated as int, although they are strings.
|
|
82
|
+
|
|
83
|
+
That means that we can also say
|
|
84
|
+
```
|
|
85
|
+
print(x < 30)
|
|
86
|
+
print(x >= "10")
|
|
87
|
+
```
|
|
88
|
+
again resulting in two times `True`.
|
|
89
|
+
|
|
90
|
+
In contrast to an ordinary string
|
|
91
|
+
```
|
|
92
|
+
print(four + five)
|
|
93
|
+
```
|
|
94
|
+
prints `9`, as istr are treated as ints.
|
|
95
|
+
|
|
96
|
+
Please note that `four` and `five` could have also be initialized with
|
|
97
|
+
```
|
|
98
|
+
four = istr(4)
|
|
99
|
+
five = istr(5)
|
|
100
|
+
```
|
|
101
|
+
or even
|
|
102
|
+
```
|
|
103
|
+
four, five = istr(4, 5)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
But how can we concatenate istrs? Just use the or operator (|):
|
|
107
|
+
```
|
|
108
|
+
print(four | five)
|
|
109
|
+
```
|
|
110
|
+
will output `45`.
|
|
111
|
+
|
|
112
|
+
And the result is again an istr.
|
|
113
|
+
|
|
114
|
+
That means that
|
|
115
|
+
```
|
|
116
|
+
(four | five) / 3
|
|
117
|
+
```
|
|
118
|
+
is `istr("9")`.
|
|
119
|
+
|
|
120
|
+
In order to repeat a string in the usual sense, you cannot use `3 * four`, as that woud be `12`.
|
|
121
|
+
|
|
122
|
+
We use the matrix multiplication operator (@) for this. So `3 @ four` is `444`. As is `four @ 3`.
|
|
123
|
+
|
|
124
|
+
Also allowed are
|
|
125
|
+
```
|
|
126
|
+
abs(four)
|
|
127
|
+
-four
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The bool operator works on the integer value of an istr. So
|
|
131
|
+
`bool('0')` ==> `False`
|
|
132
|
+
`bool('1')` ==> `True`
|
|
133
|
+
```
|
|
134
|
+
if istr('0'):
|
|
135
|
+
print("True")
|
|
136
|
+
else:
|
|
137
|
+
print("False")
|
|
138
|
+
```
|
|
139
|
+
this will print `False`
|
|
140
|
+
|
|
141
|
+
For the in operator, an istr is treated as an ordinary string, although it is possible to use ints as well:
|
|
142
|
+
```
|
|
143
|
+
"34" in istr(1234)
|
|
144
|
+
34 in istr(1234)
|
|
145
|
+
```
|
|
146
|
+
On the left hand side an istr is always treated as a string:
|
|
147
|
+
```
|
|
148
|
+
istr(1234) in "01234566890ABCDEF"
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Note that all calculations are strictly integer calculations. That means that if a float variale is ever produced it will be converted to an int.
|
|
152
|
+
Also divisions are always floor divisions!
|
|
153
|
+
|
|
154
|
+
There's a special case for `istr("")`. This is a proper empty string, but also represents the value of 0.
|
|
155
|
+
That is to allow for `istr("").join(i for i in "01234)"`
|
|
156
|
+
|
|
157
|
+
Sorting a list of istrs is based on the integer value, not the string. So
|
|
158
|
+
|
|
159
|
+
`' '.join(sorted('1 3 2 4 5 6 11 7 9 8 10 12 0'.split()))`
|
|
160
|
+
|
|
161
|
+
is
|
|
162
|
+
|
|
163
|
+
`'0 1 10 11 2 3 4 5 6 7 8 9'`
|
|
164
|
+
|
|
165
|
+
,whereas
|
|
166
|
+
|
|
167
|
+
`' '.join(sorted(istr('1 3 2 4 5 6 11 7 9 8 10 12 0'.split())))`
|
|
168
|
+
|
|
169
|
+
is
|
|
170
|
+
|
|
171
|
+
`'0 1 2 3 4 5 6 7 8 9 10 11'`
|
|
172
|
+
|
|
173
|
+
# Using other values for istr than int or str
|
|
174
|
+
Apart from with simple int or str, istr can be initialized with
|
|
175
|
+
|
|
176
|
+
- if a dict (or subtype of dict), the same type dict will be returned with all values istr'ed
|
|
177
|
+
|
|
178
|
+
`istr({0: 0, 1: 1, 2: 4})` ==> `{0: istr('0'), 1: istr('1'), 2: istr('4')}`
|
|
179
|
+
- if an iterator, the iterator will be mapped with istr
|
|
180
|
+
|
|
181
|
+
`istr(i * i for i in range(3))` ==> `<map object>`
|
|
182
|
+
|
|
183
|
+
`list(istr(i * i for i in range(3)))` ==> `[istr('0'), istr('1'), istr('4')]`
|
|
184
|
+
- if an iterable, the same type will be returned with all elements istr'ed
|
|
185
|
+
|
|
186
|
+
`istr([0, 1, 4])` ==> `[istr('0'), istr('1'), istr('4')]`
|
|
187
|
+
|
|
188
|
+
`istr((0, 1, 4))` ==> `(istr('0'), istr('1'), istr('4'))`
|
|
189
|
+
|
|
190
|
+
`istr({0, 1, 4})` ==> `{istr('4'), istr('0'), istr('1')} # or similar`
|
|
191
|
+
- if a range, an istr.range instance will be returned
|
|
192
|
+
|
|
193
|
+
`istr(range(3))` ==> `istr.range(3)`
|
|
194
|
+
|
|
195
|
+
`list(istr(range(3)))` ==> `[istr('0'), istr('1'), istr('2')]`
|
|
196
|
+
|
|
197
|
+
`len(istr(range(3)))` ==> `3`
|
|
198
|
+
|
|
199
|
+
# More than one parameter for istr
|
|
200
|
+
It is possible to give more than one parameter, in which case a tuple
|
|
201
|
+
of the istrs of the parameters will be returned, which can be handy
|
|
202
|
+
to unpack multiple values, e.g.
|
|
203
|
+
|
|
204
|
+
`a, b, c = istr(5, 6, 7)` ==> `a=istr("5') , b=istr("6"), c=istr("7")`
|
|
205
|
+
|
|
206
|
+
# Additional methods
|
|
207
|
+
It is possible to test for even/odd with the
|
|
208
|
+
|
|
209
|
+
`is_even` and `is_odd` method, e.g.
|
|
210
|
+
|
|
211
|
+
```
|
|
212
|
+
print(istr(4).is_even())
|
|
213
|
+
print(istr(5).is_odd())
|
|
214
|
+
```
|
|
215
|
+
This will print `True` twice.
|
|
216
|
+
|
|
217
|
+
The method `istr.reversed()` will return the an istr with the reversed content:
|
|
218
|
+
```
|
|
219
|
+
print(repr(istr(456).reversed()))
|
|
220
|
+
print(repr(istr("0456").reversed()))
|
|
221
|
+
```
|
|
222
|
+
result:
|
|
223
|
+
```
|
|
224
|
+
istr('654')
|
|
225
|
+
istr('6540')
|
|
226
|
+
```
|
|
227
|
+
The same can -of course- be achieved with
|
|
228
|
+
```
|
|
229
|
+
print(repr(istr(456)[::-1]))
|
|
230
|
+
print(repr(istr("0456")[::-1]))
|
|
231
|
+
```
|
|
232
|
+
Note that is impossible to reverse a negative istr.
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
# Subclassing istr
|
|
236
|
+
When a class is derived from istr, all methods will return that newly derived class.
|
|
237
|
+
|
|
238
|
+
E.g.
|
|
239
|
+
```
|
|
240
|
+
class jstr(istr):
|
|
241
|
+
...
|
|
242
|
+
|
|
243
|
+
print(repr(jstr(4) * jstr(5)))
|
|
244
|
+
```
|
|
245
|
+
will print `jstr('20')`
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "istr-python"
|
|
7
|
+
authors = [
|
|
8
|
+
{name = "Ruud van der Ham", email = "rt.van.der.ham@gmail.com"}
|
|
9
|
+
]
|
|
10
|
+
description = "istr is a module to use strings as if they were integers."
|
|
11
|
+
version = "0.0.7"
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.7"
|
|
14
|
+
dependencies = [
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
# How mature is this project? Common values are
|
|
18
|
+
# 3 - Alpha
|
|
19
|
+
# 4 - Beta
|
|
20
|
+
# 5 - Production/Stable
|
|
21
|
+
"Development Status :: 4 - Beta",
|
|
22
|
+
|
|
23
|
+
"License :: OSI Approved :: MIT License",
|
|
24
|
+
"Programming Language :: Python :: 3 :: Only"
|
|
25
|
+
]
|
|
26
|
+
[project.urls]
|
|
27
|
+
Homepage = "https://github.com/salabim/istr"
|
|
28
|
+
Documentation = "https://github.com/salabim/istr"
|
|
29
|
+
Repository = "https://github.com/salabim/istr"
|
|
30
|
+
Issues = "https://github.com/salabim/istr"
|
istr_python-0.0.6/pyproject.toml
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
[build-system]
|
|
2
|
-
requires = ["setuptools"]
|
|
3
|
-
build-backend = "setuptools.build_meta"
|
|
4
|
-
|
|
5
|
-
[project]
|
|
6
|
-
name = "istr-python"
|
|
7
|
-
authors = [
|
|
8
|
-
{name = "Ruud van der Ham", email = "rt.van.der.ham@gmail.com"}
|
|
9
|
-
]
|
|
10
|
-
description = "istr is a module to use strings as if they were integers."
|
|
11
|
-
version = "0.0.6"
|
|
12
|
-
readme = "README.md"
|
|
13
|
-
requires-python = ">=3.7"
|
|
14
|
-
dependencies = [
|
|
15
|
-
]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|