istr-python 1.0.11__tar.gz → 1.0.12__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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: istr-python
3
- Version: 1.0.11
3
+ Version: 1.0.12
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
@@ -349,6 +349,11 @@ It is possible to test for even/odd (provided the istr can be interpreted as an
349
349
  istr(4).is_even()) ==> True
350
350
  istr(5).is_odd()) ==> True
351
351
  ```
352
+ It is also possible to test for even/odd of an ordinary int:
353
+ ```
354
+ istr.is_even(4) ==> True
355
+ istr.is_odd(5) ==> True
356
+ ```
352
357
  #### test for square
353
358
 
354
359
  It is possible to test whether the value is a perfect square (provided the istr can be interpreted as an int) with the `is_square` method, e.g.
@@ -357,7 +362,11 @@ It is possible to test whether the value is a perfect square (provided the istr
357
362
  istr(4).is_square() ==> True
358
363
  istr(5).is_square()) ==> False
359
364
  ```
360
-
365
+ It is also possible to test for square of an ordinary int:
366
+ ```
367
+ istr.is_square(4) ==> True
368
+ istr.is_square(5) ==> False
369
+ ```
361
370
  #### test for prime
362
371
 
363
372
  It is possible to test whether the value is a prime number (provided the istr can be interpreted as an int) with the `is_prime` method, e.g.
@@ -366,18 +375,27 @@ It is possible to test whether the value is a prime number (provided the istr ca
366
375
  istr(4).is_prime() ==> False
367
376
  istr(5).is_prime()) ==> True
368
377
  ```
378
+ It is also possible to test for prime of an ordinary int:
369
379
 
380
+ ```
381
+ istr.is_prime(4) ==> False
382
+ istr.is_prime(5) ==> True
383
+ ```
370
384
  #### test for divisibility
371
385
 
372
386
  It is possible to test whether an istr is divisible by a given value with the `is_divisible_by method,` e.g.
373
387
 
374
388
  ```
375
- istr(18).is_divisible_by(3) ==> True
376
- istr(18).is_divisible_by(istr(3)) ==> True
377
- istr(19).is_divisible_by(3) ==> False
378
- istr(19).is_divisible_by(istr(3)) == False
389
+ istr(18).is_divisible_by(3) ==> True
390
+ istr(18).is_divisible_by(istr(3)) ==> True
391
+ istr(19).is_divisible_by(3) ==> False
392
+ istr(19).is_divisible_by(istr(3)) == False
393
+ ```
394
+ It is also possible to test for divisibility of an ordinary int:
395
+ ```
396
+ istr.is_divisible(18, 3) ==> True
397
+ istr.is_divisible(19, 3) ==> False
379
398
  ```
380
-
381
399
  #### test whether all characters are distinct
382
400
 
383
401
  With the `all_distinct` method, it is possible to test whether all characters are distinct (i.e. no character appears more than once).
@@ -336,6 +336,11 @@ It is possible to test for even/odd (provided the istr can be interpreted as an
336
336
  istr(4).is_even()) ==> True
337
337
  istr(5).is_odd()) ==> True
338
338
  ```
339
+ It is also possible to test for even/odd of an ordinary int:
340
+ ```
341
+ istr.is_even(4) ==> True
342
+ istr.is_odd(5) ==> True
343
+ ```
339
344
  #### test for square
340
345
 
341
346
  It is possible to test whether the value is a perfect square (provided the istr can be interpreted as an int) with the `is_square` method, e.g.
@@ -344,7 +349,11 @@ It is possible to test whether the value is a perfect square (provided the istr
344
349
  istr(4).is_square() ==> True
345
350
  istr(5).is_square()) ==> False
346
351
  ```
347
-
352
+ It is also possible to test for square of an ordinary int:
353
+ ```
354
+ istr.is_square(4) ==> True
355
+ istr.is_square(5) ==> False
356
+ ```
348
357
  #### test for prime
349
358
 
350
359
  It is possible to test whether the value is a prime number (provided the istr can be interpreted as an int) with the `is_prime` method, e.g.
@@ -353,18 +362,27 @@ It is possible to test whether the value is a prime number (provided the istr ca
353
362
  istr(4).is_prime() ==> False
354
363
  istr(5).is_prime()) ==> True
355
364
  ```
365
+ It is also possible to test for prime of an ordinary int:
356
366
 
367
+ ```
368
+ istr.is_prime(4) ==> False
369
+ istr.is_prime(5) ==> True
370
+ ```
357
371
  #### test for divisibility
358
372
 
359
373
  It is possible to test whether an istr is divisible by a given value with the `is_divisible_by method,` e.g.
360
374
 
361
375
  ```
362
- istr(18).is_divisible_by(3) ==> True
363
- istr(18).is_divisible_by(istr(3)) ==> True
364
- istr(19).is_divisible_by(3) ==> False
365
- istr(19).is_divisible_by(istr(3)) == False
376
+ istr(18).is_divisible_by(3) ==> True
377
+ istr(18).is_divisible_by(istr(3)) ==> True
378
+ istr(19).is_divisible_by(3) ==> False
379
+ istr(19).is_divisible_by(istr(3)) == False
380
+ ```
381
+ It is also possible to test for divisibility of an ordinary int:
382
+ ```
383
+ istr.is_divisible(18, 3) ==> True
384
+ istr.is_divisible(19, 3) ==> False
366
385
  ```
367
-
368
386
  #### test whether all characters are distinct
369
387
 
370
388
  With the `all_distinct` method, it is possible to test whether all characters are distinct (i.e. no character appears more than once).
@@ -5,7 +5,7 @@
5
5
  # |_||___/ \__||_|
6
6
  # strings you can count on
7
7
 
8
- __version__ = "1.0.11"
8
+ __version__ = "1.0.12"
9
9
  import functools
10
10
  import math
11
11
  import itertools
@@ -343,31 +343,52 @@ class istr(str):
343
343
  return int(self._as_int)
344
344
 
345
345
  def is_even(self):
346
- if not self.is_int():
347
- raise TypeError(f"not interpretable as int: {self._frepr(self)}")
348
- return self._as_int % 2 == 0
346
+ if isinstance(self, istr):
347
+ if not self.is_int():
348
+ raise TypeError(f"not interpretable as int: {self._frepr(self)}")
349
+ n = self._as_int
350
+ else:
351
+ n = int(self)
352
+
353
+ return n % 2 == 0
349
354
 
350
355
  def is_odd(self):
351
- if not self.is_int():
352
- raise TypeError(f"not interpretable as int: {self._frepr(self)}")
353
- return self._as_int % 2 == 1
356
+ if isinstance(self, istr):
357
+ if not self.is_int():
358
+ raise TypeError(f"not interpretable as int: {self._frepr(self)}")
359
+ n = self._as_int
360
+ else:
361
+ n = int(self)
362
+
363
+ return n % 2 == 1
354
364
 
355
365
  def is_square(self):
356
- if not self.is_int():
357
- raise TypeError(f"not interpretable as int: {self._frepr(self)}")
358
- return self._as_int >= 0 and self == math.isqrt(self._as_int) ** 2
366
+ if isinstance(self, istr):
367
+ if not self.is_int():
368
+ raise TypeError(f"not interpretable as int: {self._frepr(self)}")
369
+ n = self._as_int
370
+ else:
371
+ n = int(self)
372
+
373
+ return n >= 0 and self == math.isqrt(n) ** 2
359
374
 
360
375
  def is_prime(self):
361
- if not self.is_int():
362
- raise TypeError(f"not interpretable as int: {self._frepr(self)}")
363
- if self._as_int < 2:
376
+ if isinstance(self, istr):
377
+ if not self.is_int():
378
+ raise TypeError(f"not interpretable as int: {self._frepr(self)}")
379
+ n = self._as_int
380
+ else:
381
+ n = int(self)
382
+
383
+ if n < 2:
364
384
  return False
365
- if self._as_int == 2:
385
+ if n == 2:
366
386
  return True
367
- if not self._as_int & 1:
387
+ if not n & 1:
368
388
  return False
369
- for x in range(3, int(self._as_int**0.5) + 1, 2):
370
- if self._as_int % x == 0:
389
+
390
+ for x in range(3, int(n**0.5) + 1, 2):
391
+ if n % x == 0:
371
392
  return False
372
393
  return True
373
394
 
@@ -405,7 +426,13 @@ class istr(str):
405
426
  return self[::-1]
406
427
 
407
428
  def is_divisible_by(self, divisor):
408
- return self._as_int % int(divisor) == 0
429
+ if isinstance(self, istr):
430
+ if not self.is_int():
431
+ raise TypeError(f"not interpretable as int: {self._frepr(self)}")
432
+ n = self._as_int
433
+ else:
434
+ n = int(self)
435
+ return n % int(divisor) == 0
409
436
 
410
437
  def _str_method(self, name, *args, **kwargs):
411
438
  return self.__class__(getattr(super(), name)(*args, **kwargs))
@@ -572,7 +599,9 @@ class istr(str):
572
599
 
573
600
 
574
601
  def main():
575
- ...
602
+ print(istr(2).is_even())
603
+ print(istr.is_even("2"))
604
+ print(istr(3).is_prime())
576
605
 
577
606
 
578
607
  if __name__ == "__main__":
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: istr-python
3
- Version: 1.0.11
3
+ Version: 1.0.12
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
@@ -349,6 +349,11 @@ It is possible to test for even/odd (provided the istr can be interpreted as an
349
349
  istr(4).is_even()) ==> True
350
350
  istr(5).is_odd()) ==> True
351
351
  ```
352
+ It is also possible to test for even/odd of an ordinary int:
353
+ ```
354
+ istr.is_even(4) ==> True
355
+ istr.is_odd(5) ==> True
356
+ ```
352
357
  #### test for square
353
358
 
354
359
  It is possible to test whether the value is a perfect square (provided the istr can be interpreted as an int) with the `is_square` method, e.g.
@@ -357,7 +362,11 @@ It is possible to test whether the value is a perfect square (provided the istr
357
362
  istr(4).is_square() ==> True
358
363
  istr(5).is_square()) ==> False
359
364
  ```
360
-
365
+ It is also possible to test for square of an ordinary int:
366
+ ```
367
+ istr.is_square(4) ==> True
368
+ istr.is_square(5) ==> False
369
+ ```
361
370
  #### test for prime
362
371
 
363
372
  It is possible to test whether the value is a prime number (provided the istr can be interpreted as an int) with the `is_prime` method, e.g.
@@ -366,18 +375,27 @@ It is possible to test whether the value is a prime number (provided the istr ca
366
375
  istr(4).is_prime() ==> False
367
376
  istr(5).is_prime()) ==> True
368
377
  ```
378
+ It is also possible to test for prime of an ordinary int:
369
379
 
380
+ ```
381
+ istr.is_prime(4) ==> False
382
+ istr.is_prime(5) ==> True
383
+ ```
370
384
  #### test for divisibility
371
385
 
372
386
  It is possible to test whether an istr is divisible by a given value with the `is_divisible_by method,` e.g.
373
387
 
374
388
  ```
375
- istr(18).is_divisible_by(3) ==> True
376
- istr(18).is_divisible_by(istr(3)) ==> True
377
- istr(19).is_divisible_by(3) ==> False
378
- istr(19).is_divisible_by(istr(3)) == False
389
+ istr(18).is_divisible_by(3) ==> True
390
+ istr(18).is_divisible_by(istr(3)) ==> True
391
+ istr(19).is_divisible_by(3) ==> False
392
+ istr(19).is_divisible_by(istr(3)) == False
393
+ ```
394
+ It is also possible to test for divisibility of an ordinary int:
395
+ ```
396
+ istr.is_divisible(18, 3) ==> True
397
+ istr.is_divisible(19, 3) ==> False
379
398
  ```
380
-
381
399
  #### test whether all characters are distinct
382
400
 
383
401
  With the `all_distinct` method, it is possible to test whether all characters are distinct (i.e. no character appears more than once).
@@ -8,7 +8,7 @@ authors = [
8
8
  {name = "Ruud van der Ham", email = "rt.van.der.ham@gmail.com"}
9
9
  ]
10
10
  description = "istr - strings you can count on"
11
- version = "1.0.11"
11
+ version = "1.0.12"
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.7"
14
14
  dependencies = [
@@ -7,9 +7,10 @@ from pathlib import Path
7
7
 
8
8
  if __name__ == "__main__": # to make the tests run without the pytest cli
9
9
  file_folder = Path(__file__).parent
10
- top_folder = (file_folder / "..").resolve()
10
+ top_folder = (file_folder / ".." / "istr").resolve()
11
11
  sys.path.insert(0, str(top_folder))
12
12
  os.chdir(file_folder)
13
+ print(f"{file_folder=} {top_folder=}")
13
14
 
14
15
  import pytest
15
16
 
@@ -309,6 +310,11 @@ def test_even_odd():
309
310
  with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
310
311
  istr("a").is_even()
311
312
 
313
+ assert istr.is_odd(1)
314
+ assert not istr.is_even(1)
315
+ assert istr.is_even(12345678)
316
+ assert istr.is_odd(11111111)
317
+
312
318
 
313
319
  def test_is_square():
314
320
  assert not istr(-1).is_square()
@@ -320,6 +326,11 @@ def test_is_square():
320
326
  assert not istr(99).is_square()
321
327
  with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
322
328
  istr("a").is_square()
329
+ assert istr.is_square(0)
330
+ assert istr.is_square(1)
331
+ assert not istr.is_square(2)
332
+ assert istr.is_square(4)
333
+ assert istr.is_square(16)
323
334
 
324
335
 
325
336
  def test_is_prime():
@@ -332,6 +343,13 @@ def test_is_prime():
332
343
  assert not istr(99).is_prime()
333
344
  with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
334
345
  istr("a").is_prime()
346
+ assert not istr.is_prime(0)
347
+ assert not istr.is_prime(1)
348
+ assert istr.is_prime(2)
349
+ assert istr.is_prime(3)
350
+ assert not istr.is_prime(4)
351
+ assert istr.is_prime(97)
352
+ assert not istr.is_prime(99)
335
353
 
336
354
 
337
355
  def test_join():
@@ -588,6 +606,10 @@ def test_is_divisible():
588
606
  assert istr(18).is_divisible_by(istr(3))
589
607
  assert not istr(19).is_divisible_by(3)
590
608
  assert not istr(19).is_divisible_by(istr(3))
609
+ with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
610
+ istr("a").is_divisible_by(3)
611
+ assert istr.is_divisible_by(18, 3)
612
+ assert not istr.is_divisible_by(19, 3)
591
613
 
592
614
 
593
615
  def test_digits():
File without changes