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.
@@ -1,8 +1,15 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: istr-python
3
- Version: 0.0.6
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 string as if they were integers.
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 | e | n | d) + (m | o | r | e) == (m | o | n | e | y)):
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
- Them we can do
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
- So, how can we concatenate istrs? Just use the or operator (|):
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 multiply a string in the usual sense, you cannot use `3 * four`, as that will be `12`.
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 for integer value of an istr. So
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 a istr is treated as an ordinary string, although it is possible to use ints as well:
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
- Metadata-Version: 2.1
2
- Name: istr-python
3
- Version: 0.0.6
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
- Requires-Python: >=3.7
7
- Description-Content-Type: text/markdown
8
-
9
- # Introduction
10
-
11
- The istr module has exactly one class: istr.
12
-
13
- With this it is possible to interpret string as if they were integers.
14
-
15
- This can be very handy for solving puzzles, but also for other purposes. For instance the
16
- famous send more money puzzle
17
- ```
18
- S E N D
19
- M O R E
20
- --------- +
21
- M O N E Y
22
- ```
23
- can be nicely, albeit not very efficient, coded as:
24
- ```
25
- import itertools
26
- from istr import istr
27
-
28
- for s, e, n, d, m, o, r, y in istr(itertools.permutations(range(10), 8)):
29
- if m and ((s | e | n | d) + (m | o | r | e) == (m | o | n | e | y)):
30
- print(f" {s|e|n|d}")
31
- print(f" {m|o|r|e}")
32
- print("-----")
33
- print(f"{m|o|n|e|y}")
34
- ```
35
-
36
- And it is a nice demonstration of extending a class (str) with extra and changed functionality.
37
-
38
- # Installation
39
- Installing istr with pip is easy.
40
- ```
41
- $ pip install istr-python
42
- ```
43
- or when you want to upgrade,
44
- ```
45
- $ pip install istr-python --upgrade
46
- ```
47
- Alternatively, istr.py can be just copied into you current work directory from GitHub (https://github.com/salabim/istr).
48
-
49
- No dependencies!
50
-
51
- # Usage
52
- Just start with
53
-
54
- ```
55
- from istr import istr
56
- ```
57
-
58
- Now we can define some istrs:
59
- ```
60
- four = istr("4")
61
- five = istr("5")
62
- ```
63
- Them we can do
64
- ```
65
- x= four * five
66
- ```
67
- , after which x is `istr("20")`
68
-
69
- And now we can do
70
- ```
71
- print(x == 20)
72
- print(x == "20")
73
- ```
74
- resulting in two times `True`. That's because istr instances are treated as int, although they are strings.
75
-
76
- That means that we can also say
77
- ```
78
- print(x < 30)
79
- print(x >= "10")
80
- ```
81
- again resulting in two times `True`.
82
-
83
- In contrast to an ordinary string
84
- ```
85
- print(four + five)
86
- ```
87
- prints `9`, as istr are treated as ints.
88
-
89
- So, how can we concatenate istrs? Just use the or operator (|):
90
- ```
91
- print(four | five)
92
- ```
93
- will output `45`.
94
-
95
- And the result is again an istr.
96
-
97
- That means that
98
- ```
99
- (four | five) / 3
100
- ```
101
- is `istr("9")`.
102
-
103
- In order to multiply a string in the usual sense, you cannot use `3 * four`, as that will be `12`.
104
-
105
- We use the matrix multiplication operator (@) for this. So `3 @ four` is `444`.
106
-
107
- Also allowed are
108
- ```
109
- abs(four)
110
- -four
111
- ```
112
-
113
- The bool operator works for integer value of an istr. So
114
- `bool('0')` ==> `False`
115
- `bool('1')` ==> `True`
116
- ```
117
- if istr('0'):
118
- print("True")
119
- else:
120
- print("False")
121
- ```
122
- this will print `False`
123
-
124
- For the in operator a istr is treated as an ordinary string, although it is possible to use ints as well:
125
- ```
126
- "34" in istr(1234)
127
- 34 in istr(1234)
128
- ```
129
- On the left hand side an istr is always treated as a string:
130
- ```
131
- istr(1234) in "01234566890ABCDEF"
132
- ```
133
-
134
- 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.
135
- Also divisions are always floor divisions!
136
-
137
- 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)"
139
-
140
- Sorting a list of istrs is based on the integer value, not the string. So
141
-
142
- `' '.join(sorted('1 3 2 4 5 6 11 7 9 8 10 12 0'.split()))`
143
-
144
- is
145
-
146
- `'0 1 10 11 2 3 4 5 6 7 8 9'`
147
-
148
- ,whereas
149
-
150
- `' '.join(sorted(istr('1 3 2 4 5 6 11 7 9 8 10 12 0'.split())))`
151
-
152
- is
153
-
154
- `'0 1 2 3 4 5 6 7 8 9 10 11'`
155
-
156
- # Using other values for istr than int or str
157
- Apart from with simple int or str, istr can be initialized with
158
-
159
- -
160
-
161
- # Additional methods
162
- It is possible to test for even/odd with the
163
-
164
- `is_even` and `is_odd` method, e.g.
165
-
166
- ```
167
- print(istr(4).is_even())
168
- print(istr(5).is_odd())
169
- ```
170
- This will print `True` twice.
171
-
172
- The method `istr.reversed()` will return the an istr with the reversed content:
173
- ```
174
- print(repr(istr(456).reversed()))
175
- print(repr(istr("0456").reversed()))
176
- ```
177
- result:
178
- ```
179
- istr('654')
180
- istr('6540')
181
- ```
182
- Note that is impossible to reverse a negative istr.
183
-
184
- # Subclassing istr
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.6"
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
- # Introduction
2
-
3
- The istr module has exactly one class: istr.
4
-
5
- With this it is possible to interpret string 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
- Them 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
- So, how can we concatenate istrs? Just use the or operator (|):
82
- ```
83
- print(four | five)
84
- ```
85
- will output `45`.
86
-
87
- And the result is again an istr.
88
-
89
- That means that
90
- ```
91
- (four | five) / 3
92
- ```
93
- is `istr("9")`.
94
-
95
- In order to multiply a string in the usual sense, you cannot use `3 * four`, as that will be `12`.
96
-
97
- We use the matrix multiplication operator (@) for this. So `3 @ four` is `444`.
98
-
99
- Also allowed are
100
- ```
101
- abs(four)
102
- -four
103
- ```
104
-
105
- The bool operator works for integer value of an istr. So
106
- `bool('0')` ==> `False`
107
- `bool('1')` ==> `True`
108
- ```
109
- if istr('0'):
110
- print("True")
111
- else:
112
- print("False")
113
- ```
114
- this will print `False`
115
-
116
- For the in operator a istr is treated as an ordinary string, although it is possible to use ints as well:
117
- ```
118
- "34" in istr(1234)
119
- 34 in istr(1234)
120
- ```
121
- On the left hand side an istr is always treated as a string:
122
- ```
123
- istr(1234) in "01234566890ABCDEF"
124
- ```
125
-
126
- 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.
127
- Also divisions are always floor divisions!
128
-
129
- There's a special case for `istr("")`. This is a proper empty string, but also represents the value of 0.
130
- That is to allow for istr("").join(i for i in "01234)"
131
-
132
- Sorting a list of istrs is based on the integer value, not the string. So
133
-
134
- `' '.join(sorted('1 3 2 4 5 6 11 7 9 8 10 12 0'.split()))`
135
-
136
- is
137
-
138
- `'0 1 10 11 2 3 4 5 6 7 8 9'`
139
-
140
- ,whereas
141
-
142
- `' '.join(sorted(istr('1 3 2 4 5 6 11 7 9 8 10 12 0'.split())))`
143
-
144
- is
145
-
146
- `'0 1 2 3 4 5 6 7 8 9 10 11'`
147
-
148
- # Using other values for istr than int or str
149
- Apart from with simple int or str, istr can be initialized with
150
-
151
- -
152
-
153
- # Additional methods
154
- It is possible to test for even/odd with the
155
-
156
- `is_even` and `is_odd` method, e.g.
157
-
158
- ```
159
- print(istr(4).is_even())
160
- print(istr(5).is_odd())
161
- ```
162
- This will print `True` twice.
163
-
164
- The method `istr.reversed()` will return the an istr with the reversed content:
165
- ```
166
- print(repr(istr(456).reversed()))
167
- print(repr(istr("0456").reversed()))
168
- ```
169
- result:
170
- ```
171
- istr('654')
172
- istr('6540')
173
- ```
174
- Note that is impossible to reverse a negative istr.
175
-
176
- # Subclassing istr
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"
@@ -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