istr-python 1.1.34__tar.gz → 1.1.35__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.4
2
2
  Name: istr-python
3
- Version: 1.1.34
3
+ Version: 1.1.35
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
@@ -435,19 +435,19 @@ It is also possible to test for divisibility of an ordinary int:
435
435
  istr.is_divisible(18, 3) ==> True
436
436
  istr.is_divisible(19, 3) ==> False
437
437
  ```
438
- The method `divided_by` not only tests divisibility, but also returns the result of the division. If not possible, `istr('')` will be returned, unless the *fallback* (last argument) is given, in which case *fallback* will be returned.
438
+ The method `divided_by` not only tests divisibility, but also returns the result of the division. If not possible, `istr('0')` will be returned, unless the *fallback* (last argument) is given, in which case *fallback* will be returned.
439
439
 
440
440
  The result will be always istr-ed, except for None.
441
441
  ```
442
442
  istr(18).divided_by(3) ==> 6 (actually istr("6"))
443
443
  istr(18).divided_by(istr(3)) ==> 6
444
- istr(19).divided_by(3) ==> istr('')
445
- istr(19).divided_by(3, 0) ==> istr('0')
446
- istr(19).divided_by(3) ==> istr('')
447
- istr(19).divided_by(istr(3)) ==> istr('')
444
+ istr(19).divided_by(3) ==> istr('0')
445
+ istr(19).divided_by(3, 1) ==> istr('1')
446
+ istr(19).divided_by(3) ==> istr('0')
447
+ istr(19).divided_by(istr(3)) ==> istr('0')
448
448
  istr.divided_by(18, 3) ==> 6
449
- istr.divided_by(19, 3) ==> istr('')
450
- istr.divided_by(19, 3, 0) ==> istr('0')
449
+ istr.divided_by(19, 3) ==> istr('0')
450
+ istr.divided_by(19, 3, 1) ==> istr('1')
451
451
  ```
452
452
  #### test for square
453
453
 
@@ -490,6 +490,14 @@ istr.is_power_of(81, 4) ==> True
490
490
  istr.is_power_of(82, 4) ==> False
491
491
  ```
492
492
 
493
+ If called without an exponent, the test will be for any perfect power:
494
+
495
+ ```
496
+ istr(2**10).is_power_of() ==> True
497
+ istr(-3**3).is_power_of() ==> True
498
+ istr(34).is_power_of() ==> False
499
+ ```
500
+
493
501
  #### test for prime
494
502
 
495
503
  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.
@@ -504,6 +512,20 @@ It is also possible to test for prime of an ordinary int:
504
512
  istr.is_prime(4) ==> False
505
513
  istr.is_prime(5) ==> True
506
514
  ```
515
+ #### get divisors of a number
516
+ The method `divisors` will generate all divisors of a number.
517
+ Normally, the divisors are sorted, but if the sorted flag is False, the order is not necessarily maintained (this is slightly more efficient).
518
+ ```
519
+ istr(18).divisors() ==> [istr('1'), istr('2'), istr('3'), istr('6'), istr('9'), istr('18')]
520
+ istr(19).divisors() ==> [istr('1'), istr('19')]
521
+ istr(18).divisors(sorted=False) ==> [istr('1'), istr('18'), istr('2'), istr('9'), istr('3'), istr('6')]
522
+ ```
523
+ This method can also be used with an int. E.g.:
524
+ ```
525
+ istr.divisors(18) ==> [istr('1'), istr('2'), istr('3'), istr('6'), istr('9'), istr('18')]
526
+ istr.divisors(19) ==> [istr('1'), istr('19')]
527
+ istr.divisors(18, False) ==> [istr('1'), istr('18'), istr('2'), istr('9'), istr('3'), istr('6')]
528
+ ```
507
529
 
508
530
  #### test whether all characters are distinct
509
531
 
@@ -790,7 +812,7 @@ Examples:
790
812
  ```
791
813
  istr(1234).getitem(2) ==> 2
792
814
  istr(1234).getitem(-2) ==> 3
793
- istr(1234).getitem(5) ==> istr('2')
815
+ istr(1234).getitem(5) ==> istr('')
794
816
  istr(1234).getitem(5, '0') ==> istr('0')
795
817
  istr(1234).getitem(5, None) ==> None
796
818
  ```
@@ -798,7 +820,7 @@ This method can also be used with an str. E.g.:
798
820
  ```
799
821
  istr.getitem('1234', 2) ==> 2
800
822
  istr.getitem('1234', -2) ==> 3
801
- istr.getitem('1234', 5) ==> istr('2')
823
+ istr.getitem('1234', 5) ==> istr('')
802
824
  istr.getitem('1234',5, '0') ==> istr('0')
803
825
  istr.getitem('1234',5, None) ==> None
804
826
  ```
@@ -422,19 +422,19 @@ It is also possible to test for divisibility of an ordinary int:
422
422
  istr.is_divisible(18, 3) ==> True
423
423
  istr.is_divisible(19, 3) ==> False
424
424
  ```
425
- The method `divided_by` not only tests divisibility, but also returns the result of the division. If not possible, `istr('')` will be returned, unless the *fallback* (last argument) is given, in which case *fallback* will be returned.
425
+ The method `divided_by` not only tests divisibility, but also returns the result of the division. If not possible, `istr('0')` will be returned, unless the *fallback* (last argument) is given, in which case *fallback* will be returned.
426
426
 
427
427
  The result will be always istr-ed, except for None.
428
428
  ```
429
429
  istr(18).divided_by(3) ==> 6 (actually istr("6"))
430
430
  istr(18).divided_by(istr(3)) ==> 6
431
- istr(19).divided_by(3) ==> istr('')
432
- istr(19).divided_by(3, 0) ==> istr('0')
433
- istr(19).divided_by(3) ==> istr('')
434
- istr(19).divided_by(istr(3)) ==> istr('')
431
+ istr(19).divided_by(3) ==> istr('0')
432
+ istr(19).divided_by(3, 1) ==> istr('1')
433
+ istr(19).divided_by(3) ==> istr('0')
434
+ istr(19).divided_by(istr(3)) ==> istr('0')
435
435
  istr.divided_by(18, 3) ==> 6
436
- istr.divided_by(19, 3) ==> istr('')
437
- istr.divided_by(19, 3, 0) ==> istr('0')
436
+ istr.divided_by(19, 3) ==> istr('0')
437
+ istr.divided_by(19, 3, 1) ==> istr('1')
438
438
  ```
439
439
  #### test for square
440
440
 
@@ -477,6 +477,14 @@ istr.is_power_of(81, 4) ==> True
477
477
  istr.is_power_of(82, 4) ==> False
478
478
  ```
479
479
 
480
+ If called without an exponent, the test will be for any perfect power:
481
+
482
+ ```
483
+ istr(2**10).is_power_of() ==> True
484
+ istr(-3**3).is_power_of() ==> True
485
+ istr(34).is_power_of() ==> False
486
+ ```
487
+
480
488
  #### test for prime
481
489
 
482
490
  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.
@@ -491,6 +499,20 @@ It is also possible to test for prime of an ordinary int:
491
499
  istr.is_prime(4) ==> False
492
500
  istr.is_prime(5) ==> True
493
501
  ```
502
+ #### get divisors of a number
503
+ The method `divisors` will generate all divisors of a number.
504
+ Normally, the divisors are sorted, but if the sorted flag is False, the order is not necessarily maintained (this is slightly more efficient).
505
+ ```
506
+ istr(18).divisors() ==> [istr('1'), istr('2'), istr('3'), istr('6'), istr('9'), istr('18')]
507
+ istr(19).divisors() ==> [istr('1'), istr('19')]
508
+ istr(18).divisors(sorted=False) ==> [istr('1'), istr('18'), istr('2'), istr('9'), istr('3'), istr('6')]
509
+ ```
510
+ This method can also be used with an int. E.g.:
511
+ ```
512
+ istr.divisors(18) ==> [istr('1'), istr('2'), istr('3'), istr('6'), istr('9'), istr('18')]
513
+ istr.divisors(19) ==> [istr('1'), istr('19')]
514
+ istr.divisors(18, False) ==> [istr('1'), istr('18'), istr('2'), istr('9'), istr('3'), istr('6')]
515
+ ```
494
516
 
495
517
  #### test whether all characters are distinct
496
518
 
@@ -777,7 +799,7 @@ Examples:
777
799
  ```
778
800
  istr(1234).getitem(2) ==> 2
779
801
  istr(1234).getitem(-2) ==> 3
780
- istr(1234).getitem(5) ==> istr('2')
802
+ istr(1234).getitem(5) ==> istr('')
781
803
  istr(1234).getitem(5, '0') ==> istr('0')
782
804
  istr(1234).getitem(5, None) ==> None
783
805
  ```
@@ -785,7 +807,7 @@ This method can also be used with an str. E.g.:
785
807
  ```
786
808
  istr.getitem('1234', 2) ==> 2
787
809
  istr.getitem('1234', -2) ==> 3
788
- istr.getitem('1234', 5) ==> istr('2')
810
+ istr.getitem('1234', 5) ==> istr('')
789
811
  istr.getitem('1234',5, '0') ==> istr('0')
790
812
  istr.getitem('1234',5, None) ==> None
791
813
  ```
@@ -5,7 +5,7 @@
5
5
  # |_||___/ \__||_|
6
6
  # strings you can count on
7
7
 
8
- __version__ = "1.1.34"
8
+ __version__ = "1.1.35"
9
9
  import functools
10
10
  import itertools
11
11
  import types
@@ -424,9 +424,9 @@ class istr(str):
424
424
  return all(i0 > i1 for i0, i1 in zip(self_as_str, self_as_str[1:]))
425
425
 
426
426
  def is_divisible_by(self, divisor):
427
- return istr.divided_by(self, divisor) != ""
427
+ return istr.divided_by(self, divisor, None) is not None
428
428
 
429
- def divided_by(self, divisor, fallback=""):
429
+ def divided_by(self, divisor, fallback="0"):
430
430
  if fallback is not None:
431
431
  fallback = istr(fallback)
432
432
  if divisor == 0:
@@ -443,6 +443,23 @@ class istr(str):
443
443
  else:
444
444
  return istr(fallback)
445
445
 
446
+ def divisors(self, sorted=True):
447
+ n = istr.interpret_as_int(self)
448
+ if n<=0:
449
+ raise ValueError(f'must be >=0, not {n}')
450
+ tail = []
451
+ for d in range(1, math.isqrt(n) + 1):
452
+ q, r = divmod(n, d)
453
+ if r == 0:
454
+ yield istr(d)
455
+ if q != d:
456
+ if sorted:
457
+ tail.append(istr(q))
458
+ else:
459
+ yield istr(q)
460
+ if sorted:
461
+ yield from reversed(tail)
462
+
446
463
  def is_prime(self):
447
464
  n = istr.interpret_as_int(self)
448
465
  if n < 1_000_000:
@@ -501,8 +518,10 @@ class istr(str):
501
518
  def is_cube(self):
502
519
  return istr.is_power_of(self, 3)
503
520
 
504
- def is_power_of(self, exponent):
521
+ def is_power_of(self, exponent=None):
505
522
  n = istr.interpret_as_int(self)
523
+ if exponent is None:
524
+ return (n == 1) or any(istr(n).is_power_of(exponent) for exponent in range(2, int(math.log2(abs(n)) + 1)))
506
525
  exponent = check_integer(exponent, "exponent")
507
526
  if n < 0:
508
527
  if exponent % 2 == 0:
@@ -848,7 +867,8 @@ class istr(str):
848
867
 
849
868
  cls._int_format = int_format
850
869
 
851
- def __enter__(self): ...
870
+ def __enter__(self):
871
+ ...
852
872
 
853
873
  def __exit__(self, exc_type, exc_value, exc_tb):
854
874
  self.saved_cls._int_format = self.saved_int_format
@@ -869,7 +889,8 @@ class istr(str):
869
889
  self.saved_cls = cls
870
890
  cls._repr_mode = mode
871
891
 
872
- def __enter__(self): ...
892
+ def __enter__(self):
893
+ ...
873
894
 
874
895
  def __exit__(self, exc_type, exc_value, exc_tb):
875
896
  self.saved_cls._repr_mode = self.saved_repr_mode
@@ -888,7 +909,8 @@ class istr(str):
888
909
  self.saved_cls = cls
889
910
  cls._base = base
890
911
 
891
- def __enter__(self): ...
912
+ def __enter__(self):
913
+ ...
892
914
 
893
915
  def __exit__(self, exc_type, exc_value, exc_tb):
894
916
  self.saved_cls._base = self.saved_base
@@ -1101,3 +1123,4 @@ class istrModule(types.ModuleType):
1101
1123
 
1102
1124
  if __name__ != "__main__":
1103
1125
  sys.modules["istr"].__class__ = istrModule
1126
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: istr-python
3
- Version: 1.1.34
3
+ Version: 1.1.35
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
@@ -435,19 +435,19 @@ It is also possible to test for divisibility of an ordinary int:
435
435
  istr.is_divisible(18, 3) ==> True
436
436
  istr.is_divisible(19, 3) ==> False
437
437
  ```
438
- The method `divided_by` not only tests divisibility, but also returns the result of the division. If not possible, `istr('')` will be returned, unless the *fallback* (last argument) is given, in which case *fallback* will be returned.
438
+ The method `divided_by` not only tests divisibility, but also returns the result of the division. If not possible, `istr('0')` will be returned, unless the *fallback* (last argument) is given, in which case *fallback* will be returned.
439
439
 
440
440
  The result will be always istr-ed, except for None.
441
441
  ```
442
442
  istr(18).divided_by(3) ==> 6 (actually istr("6"))
443
443
  istr(18).divided_by(istr(3)) ==> 6
444
- istr(19).divided_by(3) ==> istr('')
445
- istr(19).divided_by(3, 0) ==> istr('0')
446
- istr(19).divided_by(3) ==> istr('')
447
- istr(19).divided_by(istr(3)) ==> istr('')
444
+ istr(19).divided_by(3) ==> istr('0')
445
+ istr(19).divided_by(3, 1) ==> istr('1')
446
+ istr(19).divided_by(3) ==> istr('0')
447
+ istr(19).divided_by(istr(3)) ==> istr('0')
448
448
  istr.divided_by(18, 3) ==> 6
449
- istr.divided_by(19, 3) ==> istr('')
450
- istr.divided_by(19, 3, 0) ==> istr('0')
449
+ istr.divided_by(19, 3) ==> istr('0')
450
+ istr.divided_by(19, 3, 1) ==> istr('1')
451
451
  ```
452
452
  #### test for square
453
453
 
@@ -490,6 +490,14 @@ istr.is_power_of(81, 4) ==> True
490
490
  istr.is_power_of(82, 4) ==> False
491
491
  ```
492
492
 
493
+ If called without an exponent, the test will be for any perfect power:
494
+
495
+ ```
496
+ istr(2**10).is_power_of() ==> True
497
+ istr(-3**3).is_power_of() ==> True
498
+ istr(34).is_power_of() ==> False
499
+ ```
500
+
493
501
  #### test for prime
494
502
 
495
503
  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.
@@ -504,6 +512,20 @@ It is also possible to test for prime of an ordinary int:
504
512
  istr.is_prime(4) ==> False
505
513
  istr.is_prime(5) ==> True
506
514
  ```
515
+ #### get divisors of a number
516
+ The method `divisors` will generate all divisors of a number.
517
+ Normally, the divisors are sorted, but if the sorted flag is False, the order is not necessarily maintained (this is slightly more efficient).
518
+ ```
519
+ istr(18).divisors() ==> [istr('1'), istr('2'), istr('3'), istr('6'), istr('9'), istr('18')]
520
+ istr(19).divisors() ==> [istr('1'), istr('19')]
521
+ istr(18).divisors(sorted=False) ==> [istr('1'), istr('18'), istr('2'), istr('9'), istr('3'), istr('6')]
522
+ ```
523
+ This method can also be used with an int. E.g.:
524
+ ```
525
+ istr.divisors(18) ==> [istr('1'), istr('2'), istr('3'), istr('6'), istr('9'), istr('18')]
526
+ istr.divisors(19) ==> [istr('1'), istr('19')]
527
+ istr.divisors(18, False) ==> [istr('1'), istr('18'), istr('2'), istr('9'), istr('3'), istr('6')]
528
+ ```
507
529
 
508
530
  #### test whether all characters are distinct
509
531
 
@@ -790,7 +812,7 @@ Examples:
790
812
  ```
791
813
  istr(1234).getitem(2) ==> 2
792
814
  istr(1234).getitem(-2) ==> 3
793
- istr(1234).getitem(5) ==> istr('2')
815
+ istr(1234).getitem(5) ==> istr('')
794
816
  istr(1234).getitem(5, '0') ==> istr('0')
795
817
  istr(1234).getitem(5, None) ==> None
796
818
  ```
@@ -798,7 +820,7 @@ This method can also be used with an str. E.g.:
798
820
  ```
799
821
  istr.getitem('1234', 2) ==> 2
800
822
  istr.getitem('1234', -2) ==> 3
801
- istr.getitem('1234', 5) ==> istr('2')
823
+ istr.getitem('1234', 5) ==> istr('')
802
824
  istr.getitem('1234',5, '0') ==> istr('0')
803
825
  istr.getitem('1234',5, None) ==> None
804
826
  ```
@@ -10,7 +10,7 @@ authors = [
10
10
  { name = "Ruud van der Ham", email = "rt.van.der.ham@gmail.com" },
11
11
  ]
12
12
  description = "istr - strings you can count on"
13
- version = "1.1.34"
13
+ version = "1.1.35"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.10"
16
16
  dependencies = []
@@ -424,18 +424,18 @@ def test_is_divisible_by():
424
424
  def test_divided_by():
425
425
  assert istr(18).divided_by(3).equals(istr(6))
426
426
  assert istr(18).divided_by(istr(3)).equals(istr(6))
427
- assert istr(19).divided_by(3).equals(istr(""))
428
- assert istr(19).divided_by(istr(3)).equals(istr(""))
427
+ assert istr(19).divided_by(3).equals(istr("0"))
428
+ assert istr(19).divided_by(istr(3)).equals(istr("0"))
429
429
  with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
430
430
  istr("a").divided_by(3)
431
431
  assert istr.divided_by(18, 3).equals(istr(6))
432
- assert istr.divided_by(19, 3).equals(istr(""))
433
- assert istr.divided_by(18, 3, 0).equals(istr(6))
434
- assert istr.divided_by(19, 3, 0).equals(istr("0"))
432
+ assert istr.divided_by(19, 3).equals(istr("0"))
433
+ assert istr.divided_by(18, 3, 1).equals(istr(6))
434
+ assert istr.divided_by(19, 3, 1).equals(istr("1"))
435
435
  assert istr.divided_by(18, 3, None).equals(istr(6))
436
436
  assert istr.divided_by(19, 3, None) is None
437
- assert istr.divided_by(4, 0).equals(istr(""))
438
- assert istr.divided_by(4, 0, 0).equals(istr("0"))
437
+ assert istr.divided_by(4, 0).equals(istr("0"))
438
+ assert istr.divided_by(4, 0, 1).equals(istr("1"))
439
439
 
440
440
 
441
441
  def test_is_square():
@@ -506,7 +506,12 @@ def test_is_power_of():
506
506
  assert istr(10).is_power_of(1)
507
507
  assert istr(-8).is_power_of(3)
508
508
 
509
-
509
+ def test_is_power_of_no_arg():
510
+ assert istr(2**10).is_power_of()
511
+ assert not istr(2**10+1).is_power_of()
512
+ assert istr(1).is_power_of()
513
+ assert istr((-5)**3).is_power_of()
514
+
510
515
  def test_is_prime():
511
516
  assert not istr(0).is_prime()
512
517
  assert not istr(1).is_prime()
@@ -577,7 +582,19 @@ def test_power_ofs():
577
582
  assert id(istr.power_ofs(3, 2000)) != id(istr.cubes(3, 2000)) # test caching
578
583
  assert id(istr.power_ofs(3, 1000, cache=False)) != id(istr.cubes(3, 1000, cache=False)) # test caching
579
584
 
580
-
585
+ def test_divisors():
586
+ assert list(istr(36).divisors())==istr([1,2,3,4,6,9,12,18,36])
587
+ assert list(istr(36).divisors(sorted=False))==istr([1,36,2,18,3,12,4,9,6])
588
+ assert list(istr(37).divisors())==istr([1,37])
589
+ assert list(istr(1).divisors())==istr([1])
590
+ with pytest.raises(ValueError,match=re.escape("must be >=0, not 0")):
591
+ list(istr(0).divisors())
592
+ with pytest.raises(ValueError,match=re.escape("must be >=0, not -1")):
593
+ list(istr(-1).divisors())
594
+ assert list(istr.divisors(36))==istr([1,2,3,4,6,9,12,18,36])
595
+ assert list(istr.divisors(36,False))==istr([1,36,2,18,3,12,4,9,6])
596
+ assert list(istr.divisors(37))==istr([1,37])
597
+
581
598
  def test_in_range():
582
599
  primes1000 = istr.primes(1000)
583
600
  n = len(primes1000)
@@ -1014,7 +1031,8 @@ def test_sumprod():
1014
1031
 
1015
1032
 
1016
1033
  def test_subclassing():
1017
- class jstr(istr.type): ...
1034
+ class jstr(istr.type):
1035
+ ...
1018
1036
 
1019
1037
  assert jstr(5).equals(jstr(5))
1020
1038
  assert repr(jstr(*range(3))) == "(jstr('0'), jstr('1'), jstr('2'))"
@@ -1139,3 +1157,4 @@ def test_compose():
1139
1157
 
1140
1158
  if __name__ == "__main__":
1141
1159
  pytest.main(["-vv", "-s", "-x", __file__])
1160
+
File without changes