istr-python 1.0.10__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.10
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.10"
8
+ __version__ = "1.0.12"
9
9
  import functools
10
10
  import math
11
11
  import itertools
@@ -243,9 +243,9 @@ class istr(str):
243
243
  return value
244
244
  if isinstance(value, dict):
245
245
  return type(value)((k, cls(v)) for k, v in value.items())
246
- if not isinstance(value, (str, type)) and hasattr(value, "__iter__"):
246
+ if not isinstance(value, (str, type)) and hasattr(value, "__iter__"):
247
247
  if hasattr(value, "__next__"):
248
- return map(functools.partial(cls), value)
248
+ return map(functools.partial(cls), value)
249
249
  return type(value)(map(functools.partial(cls), value))
250
250
  as_int = cls._to_int(value)
251
251
  if isinstance(value, str):
@@ -343,36 +343,55 @@ 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 == 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
 
374
-
375
-
376
395
  def __or__(self, other):
377
396
  try:
378
397
  return self.__class__(str(self).__add__(other))
@@ -407,7 +426,13 @@ class istr(str):
407
426
  return self[::-1]
408
427
 
409
428
  def is_divisible_by(self, divisor):
410
- 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
411
436
 
412
437
  def _str_method(self, name, *args, **kwargs):
413
438
  return self.__class__(getattr(super(), name)(*args, **kwargs))
@@ -574,8 +599,9 @@ class istr(str):
574
599
 
575
600
 
576
601
  def main():
577
- for i in range(100):
578
- print(i, istr(i).is_prime(),istr(i).is_square())
602
+ print(istr(2).is_even())
603
+ print(istr.is_even("2"))
604
+ print(istr(3).is_prime())
579
605
 
580
606
 
581
607
  if __name__ == "__main__":
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: istr-python
3
- Version: 1.0.10
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.10"
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
 
@@ -304,13 +305,33 @@ def test_even_odd():
304
305
  assert not istr(1).is_even()
305
306
 
306
307
  assert istr(12345678).is_even()
307
-
308
+ with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
309
+ istr("a").is_odd()
310
+ with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
311
+ istr("a").is_even()
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
+
318
+
308
319
  def test_is_square():
320
+ assert not istr(-1).is_square()
321
+ assert istr(0).is_square()
309
322
  assert istr(1).is_square()
310
323
  assert not istr(2).is_square()
311
324
  assert istr(4).is_square()
312
325
  assert istr(16).is_square()
313
326
  assert not istr(99).is_square()
327
+ with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
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)
334
+
314
335
 
315
336
  def test_is_prime():
316
337
  assert not istr(0).is_prime()
@@ -320,10 +341,15 @@ def test_is_prime():
320
341
  assert not istr(4).is_prime()
321
342
  assert istr(97).is_prime()
322
343
  assert not istr(99).is_prime()
323
-
324
-
325
-
326
-
344
+ with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
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)
327
353
 
328
354
 
329
355
  def test_join():
@@ -574,12 +600,18 @@ def test_base():
574
600
  with istr.base(10):
575
601
  assert a * a == 225
576
602
 
577
- def test_is_divisible():
603
+
604
+ def test_is_divisible():
578
605
  assert istr(18).is_divisible_by(3)
579
606
  assert istr(18).is_divisible_by(istr(3))
580
607
  assert not istr(19).is_divisible_by(3)
581
608
  assert not istr(19).is_divisible_by(istr(3))
582
-
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)
613
+
614
+
583
615
  def test_digits():
584
616
  assert istr.digits().equals(istr("0123456789"))
585
617
  assert istr.digits("").equals(istr("0123456789"))
@@ -647,8 +679,7 @@ def test_all_distinct():
647
679
 
648
680
 
649
681
  def test_subclassing():
650
- class jstr(istr):
651
- ...
682
+ class jstr(istr): ...
652
683
 
653
684
  assert jstr(5).equals(jstr(5))
654
685
  assert repr(jstr(*range(3))) == "(jstr('0'), jstr('1'), jstr('2'))"
@@ -656,4 +687,3 @@ def test_subclassing():
656
687
 
657
688
  if __name__ == "__main__":
658
689
  pytest.main(["-vv", "-s", "-x", __file__])
659
-
File without changes