istr-python 1.1.33__tar.gz → 1.1.34__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.33
3
+ Version: 1.1.34
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,18 +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, None will be returned,
439
- 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('')` will be returned, unless the *fallback* (last argument) is given, in which case *fallback* will be returned.
439
+
440
+ The result will be always istr-ed, except for None.
440
441
  ```
441
442
  istr(18).divided_by(3) ==> 6 (actually istr("6"))
442
443
  istr(18).divided_by(istr(3)) ==> 6
443
- istr(19).divided_by(3) ==> None
444
- istr(19).divided_by(3, 0) ==> 0
445
- istr(19).divided_by(3) ==> None
446
- istr(19).divided_by(istr(3)) ==> None
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('')
447
448
  istr.divided_by(18, 3) ==> 6
448
- istr.divided_by(19, 3) ==> None
449
- istr.divided_by(19, 3, 0) ==> 0
449
+ istr.divided_by(19, 3) ==> istr('')
450
+ istr.divided_by(19, 3, 0) ==> istr('0')
450
451
  ```
451
452
  #### test for square
452
453
 
@@ -778,6 +779,31 @@ Alternatively, it is possible to get a list of all squares, cubes, power_ofs or
778
779
 
779
780
  Unless `cache=False` is specified, the query result is cached.
780
781
 
782
+ #### Safe indexing (getitem)
783
+
784
+ The `istr.getitem` method is essentially, a safe version of indexing an istr.
785
+ If the index is within the bounds of the istr, `getitem` just works like indexing. Otherwise, where normally an IndexError would be raised, the istr('') will be returned unless `fallback` (last argument) is specified.
786
+
787
+ Note that the result will always be istr-ed, except when the result is None.
788
+
789
+ Examples:
790
+ ```
791
+ istr(1234).getitem(2) ==> 2
792
+ istr(1234).getitem(-2) ==> 3
793
+ istr(1234).getitem(5) ==> istr('2')
794
+ istr(1234).getitem(5, '0') ==> istr('0')
795
+ istr(1234).getitem(5, None) ==> None
796
+ ```
797
+ This method can also be used with an str. E.g.:
798
+ ```
799
+ istr.getitem('1234', 2) ==> 2
800
+ istr.getitem('1234', -2) ==> 3
801
+ istr.getitem('1234', 5) ==> istr('2')
802
+ istr.getitem('1234',5, '0') ==> istr('0')
803
+ istr.getitem('1234',5, None) ==> None
804
+ ```
805
+ Note that this method has the advantage that it can accept an istr as index, in contrast to normal indexing.
806
+
781
807
  #### generate istr with digits
782
808
 
783
809
  The class method `digits` can be used to return an istr of digits according to a given specification.
@@ -422,18 +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, None will be returned,
426
- 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('')` will be returned, unless the *fallback* (last argument) is given, in which case *fallback* will be returned.
426
+
427
+ The result will be always istr-ed, except for None.
427
428
  ```
428
429
  istr(18).divided_by(3) ==> 6 (actually istr("6"))
429
430
  istr(18).divided_by(istr(3)) ==> 6
430
- istr(19).divided_by(3) ==> None
431
- istr(19).divided_by(3, 0) ==> 0
432
- istr(19).divided_by(3) ==> None
433
- istr(19).divided_by(istr(3)) ==> None
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('')
434
435
  istr.divided_by(18, 3) ==> 6
435
- istr.divided_by(19, 3) ==> None
436
- istr.divided_by(19, 3, 0) ==> 0
436
+ istr.divided_by(19, 3) ==> istr('')
437
+ istr.divided_by(19, 3, 0) ==> istr('0')
437
438
  ```
438
439
  #### test for square
439
440
 
@@ -765,6 +766,31 @@ Alternatively, it is possible to get a list of all squares, cubes, power_ofs or
765
766
 
766
767
  Unless `cache=False` is specified, the query result is cached.
767
768
 
769
+ #### Safe indexing (getitem)
770
+
771
+ The `istr.getitem` method is essentially, a safe version of indexing an istr.
772
+ If the index is within the bounds of the istr, `getitem` just works like indexing. Otherwise, where normally an IndexError would be raised, the istr('') will be returned unless `fallback` (last argument) is specified.
773
+
774
+ Note that the result will always be istr-ed, except when the result is None.
775
+
776
+ Examples:
777
+ ```
778
+ istr(1234).getitem(2) ==> 2
779
+ istr(1234).getitem(-2) ==> 3
780
+ istr(1234).getitem(5) ==> istr('2')
781
+ istr(1234).getitem(5, '0') ==> istr('0')
782
+ istr(1234).getitem(5, None) ==> None
783
+ ```
784
+ This method can also be used with an str. E.g.:
785
+ ```
786
+ istr.getitem('1234', 2) ==> 2
787
+ istr.getitem('1234', -2) ==> 3
788
+ istr.getitem('1234', 5) ==> istr('2')
789
+ istr.getitem('1234',5, '0') ==> istr('0')
790
+ istr.getitem('1234',5, None) ==> None
791
+ ```
792
+ Note that this method has the advantage that it can accept an istr as index, in contrast to normal indexing.
793
+
768
794
  #### generate istr with digits
769
795
 
770
796
  The class method `digits` can be used to return an istr of digits according to a given specification.
@@ -464,15 +464,15 @@ class istr(str):
464
464
  return result
465
465
 
466
466
  @classmethod
467
- def _primes(cls, lb, ub):
468
- sieve = bytearray(b"\x01") * (ub + 1)
467
+ def _primes(cls, start, stop):
468
+ sieve = bytearray(b"\x01") * (stop + 1)
469
469
  sieve[0:2] = b"\x00\x00"
470
470
 
471
- for i in range(2, int(ub**0.5) + 1):
471
+ for i in range(2, int(stop**0.5) + 1):
472
472
  if sieve[i]:
473
- sieve[i * i : ub + 1 : i] = b"\x00" * (((ub - i * i) // i) + 1)
473
+ sieve[i * i : stop + 1 : i] = b"\x00" * (((stop - i * i) // i) + 1)
474
474
 
475
- return list(map(cls, ([i for i, is_prime in enumerate(sieve) if is_prime and lb <= i < ub]))) # range check just to be sure
475
+ return list(map(cls, ([i for i, is_prime in enumerate(sieve) if is_prime and start <= i < stop]))) # range check just to be sure
476
476
 
477
477
  @classmethod
478
478
  @functools.lru_cache
@@ -555,25 +555,25 @@ class istr(str):
555
555
  return result
556
556
 
557
557
  @classmethod
558
- def _power_ofs(cls, exponent, lb, ub):
558
+ def _power_ofs(cls, exponent, start, stop):
559
559
  if exponent % 2 == 0:
560
- lb = max(0, lb)
560
+ start = max(0, start)
561
561
  match exponent:
562
562
  case 0:
563
- if lb <= 1 < ub:
563
+ if start <= 1 < stop:
564
564
  result = [1]
565
565
  else:
566
566
  result = []
567
567
  case 1:
568
- result = [*range(lb, ub)]
568
+ result = [*range(start, stop)]
569
569
  case _:
570
570
  result = []
571
- if lb < 0: # can't be the case for even n (because of above limiting)
572
- i = -int((-lb) ** (1 / exponent))
571
+ if start < 0: # can't be the case for even n (because of above limiting)
572
+ i = -int((-start) ** (1 / exponent))
573
573
  else:
574
- i = int(lb ** (1 / exponent))
575
- while (i_n := i**exponent) < ub:
576
- if i_n >= lb: # just to be sure
574
+ i = int(start ** (1 / exponent))
575
+ while (i_n := i**exponent) < stop:
576
+ if i_n >= start: # just to be sure
577
577
  result.append(i_n)
578
578
  i += 1
579
579
 
@@ -987,7 +987,7 @@ _cache = {}
987
987
 
988
988
  def in_range(lst, start, stop):
989
989
  """
990
- this function will give all values of lst in the range [lb, ub)
990
+ this function will give all values of lst in the range [start, stop)
991
991
 
992
992
  Parameters
993
993
  ----------