istr-python 1.1.35__tar.gz → 1.1.36__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.35
3
+ Version: 1.1.36
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
@@ -312,6 +312,7 @@ is
312
312
  Apart from with numeric (to be interpreted as an int) or str, istr can be initialized with
313
313
  several other types:
314
314
 
315
+ - if `None`, `None` will be returned.
315
316
 
316
317
  - if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
317
318
  ```
@@ -377,6 +378,37 @@ In contrast to the builtin `range`, `istr.range` supports a non keyword paramete
377
378
 
378
379
  `list(istr.range(length=3))` is equivalent to `istr.range(100, 1000)`
379
380
 
381
+ #### square root, cubic root and nth root
382
+ The methods `sqrt`, `cbrt`, `nth_root`provide the square root, cubic root and nth root of an istr. If there is no integer root, the fallback value (default `istr('0')` will be returned.
383
+ ```
384
+ istr(64).sqrt() ==> istr('8')
385
+ istr(65).sqrt() ==> istr('0')
386
+ istr(1234**3).cbrt() ==> istr('1234')
387
+ istr(1234**3+1).cbrt() ==> istr('0')
388
+ istr(1234**5).nth_root(5) ==> istr('1234')
389
+ istr(1234**5+1).nth_root(5) ==> istr('0')
390
+ istr(1234**5+1).nth_root(5, 1) ==> istr('1')
391
+ istr(1234**5+1).nth_root(5, None) ==> None
392
+
393
+ istr.sqrt(64) ==> istr('8')
394
+ ```
395
+
396
+ #### integer division
397
+ 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.
398
+
399
+ The result will be always istr-ed, except for None.
400
+ ```
401
+ istr(18).divided_by(3) ==> 6 (actually istr("6"))
402
+ istr(18).divided_by(istr(3)) ==> 6
403
+ istr(19).divided_by(3) ==> istr('0')
404
+ istr(19).divided_by(3, 1) ==> istr('1')
405
+ istr(19).divided_by(3) ==> istr('0')
406
+ istr(19).divided_by(istr(3)) ==> istr('0')
407
+ istr.divided_by(18, 3) ==> 6
408
+ istr.divided_by(19, 3) ==> istr('0')
409
+ istr.divided_by(19, 3, 1) ==> istr('1')
410
+ ```
411
+
380
412
  #### test for even/odd
381
413
 
382
414
  It is possible to test for even/odd (provided the istr can be interpreted as an int) with the `is_even` and `is_odd` method, e.g.
@@ -409,16 +441,16 @@ istr.is_palindrome(min) ==> False
409
441
  #### test for increasing, decreasing, non-decreasing and non-increasing
410
442
 
411
443
  It is possible to test whether the characters of an istr are increasing, decreasing, non-decreasing or non-increasing, like
412
- ```
444
+ ```
413
445
  istr(1223).is_increasing() ==> False
414
446
  istr(1223).is_non_decreasing() ==> True
415
447
  istr(3221).is_decreasing() ==> False
416
448
  istr(3221).is_non_increasing ==> True
417
- ```
418
- It is also possible to test for 'increasingness' and friends for anything that can be converted to a str:
419
449
  ```
450
+ It is also possible to test for 'increasingness' and friends for anything that can be converted to a str:
451
+ ```
420
452
  istr.is_increasing(123) ==> True
421
- ```
453
+ ```
422
454
 
423
455
  #### test for divisibility
424
456
 
@@ -435,20 +467,6 @@ It is also possible to test for divisibility of an ordinary int:
435
467
  istr.is_divisible(18, 3) ==> True
436
468
  istr.is_divisible(19, 3) ==> False
437
469
  ```
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
-
440
- The result will be always istr-ed, except for None.
441
- ```
442
- istr(18).divided_by(3) ==> 6 (actually istr("6"))
443
- istr(18).divided_by(istr(3)) ==> 6
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
- istr.divided_by(18, 3) ==> 6
449
- istr.divided_by(19, 3) ==> istr('0')
450
- istr.divided_by(19, 3, 1) ==> istr('1')
451
- ```
452
470
  #### test for square
453
471
 
454
472
  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.
@@ -745,11 +763,11 @@ istr.count(10,3) ==> istr('10'), istr('13'), istr('16'), ...
745
763
  #### istr.zip
746
764
 
747
765
  The class method `istr.zip` is the equivalent of the zip builtin, but applies istr to each element. `istr.zip` also supports the join parameter:
748
- ```
766
+ ```
749
767
  print(list(istr.zip('12', '345')))
750
768
  print(list(istr.zip('12', '345', join=True)))
751
769
  print(list(istr.zip('12', '345', strict=True)))
752
- ```
770
+ ```
753
771
  results in
754
772
  ```
755
773
  [(istr('1'), istr('3')), (istr('2'), istr('4'))]
@@ -763,11 +781,11 @@ to `istr` and then concatenate these.
763
781
 
764
782
  `
765
783
 
766
- ```
784
+ ```
767
785
  list(istr.concat(((1,2),(3,4))) ==> istr([12,34])
768
786
  list(istr.concat(istr.permutations(range(3),2))) ==>
769
787
  [istr('01'), istr('02'), istr('10'), istr('12'), istr('20'), istr('21')]
770
- ```
788
+ ```
771
789
 
772
790
  #### prod to get product of an iterable
773
791
 
@@ -1097,29 +1115,29 @@ operator/function int str Example
1097
1115
  + x istr(20) + 3 ==> istr('23')
1098
1116
  _ x istr(20) - 3 ==> istr('17')
1099
1117
  * x istr(20) * 3 ==> istr('60')
1100
- / x istr(20) / 3 ==> istr('6')
1101
- // x istr(20) // 3 ==> istr('6')
1102
- % x istr(20) % 3 ==> istr('2')
1103
- divmod x divmod(istr(20), 3) ==> (istr('6'), istr('2'))
1104
- ** x istr(2) ** 3 ==> istr('8')
1105
- <=, <, >, >= x istr('100') > istr('2') ==> True
1106
- abs x abs(istr(-20)) ==> istr('20')
1107
- int x int(istr("20")) ==> 20
1108
- float x float(istr("20")) ==> 20.0
1109
- complex x complex(istr("20")) ==> (20+0j)
1110
- == x x istr(20) == 20 ==> True | istr(20) == '20' ==> True
1111
- bool x x *) bool(istr(' 0 ')) ==> False | bool(istr('')) ==> False
1112
- @ x istr(20) @ 3 ==> istr('202020')
1113
- | x istr(20) | '5' ==> istr('205')
1114
- slicing x istr(12345)[1:3] ==> istr('23')
1115
- iterate x [x for x in istr(20)] ==> [istr('2'), istr('0')]
1116
- len x len(istr(' 20 ')) ==> 4
1117
- count x istr(100).count(0) ==> 2
1118
+ / x istr(20) / 3 ==> istr('6')
1119
+ // x istr(20) // 3 ==> istr('6')
1120
+ % x istr(20) % 3 ==> istr('2')
1121
+ divmod x divmod(istr(20), 3) ==> (istr('6'), istr('2'))
1122
+ ** x istr(2) ** 3 ==> istr('8')
1123
+ <=, <, >, >= x istr('100') > istr('2') ==> True
1124
+ abs x abs(istr(-20)) ==> istr('20')
1125
+ int x int(istr("20")) ==> 20
1126
+ float x float(istr("20")) ==> 20.0
1127
+ complex x complex(istr("20")) ==> (20+0j)
1128
+ == x x istr(20) == 20 ==> True | istr(20) == '20' ==> True
1129
+ bool x x *) bool(istr(' 0 ')) ==> False | bool(istr('')) ==> False
1130
+ @ x istr(20) @ 3 ==> istr('202020')
1131
+ | x istr(20) | '5' ==> istr('205')
1132
+ slicing x istr(12345)[1:3] ==> istr('23')
1133
+ iterate x [x for x in istr(20)] ==> [istr('2'), istr('0')]
1134
+ len x len(istr(' 20 ')) ==> 4
1135
+ count x istr(100).count(0) ==> 2
1118
1136
  istr.count(100) ==> istr('100'), istr('101'). istr('102'), ...
1119
- index x istr(' 100 ').index('0') ==> 2
1120
- split x istr('1 2').split() ==> (istr('1'), istr('2'))
1121
- string format x f"|{istr(1234):6}|" ==> '|1234 |'
1122
- other string methods x istr('aAbBcC').lower() ==> istr('aabbcc')
1137
+ index x istr(' 100 ').index('0') ==> 2
1138
+ split x istr('1 2').split() ==> (istr('1'), istr('2'))
1139
+ string format x f"|{istr(1234):6}|" ==> '|1234 |'
1140
+ other string methods x istr('aAbBcC').lower() ==> istr('aabbcc')
1123
1141
  istr('aAbBcC').islower() ==> False
1124
1142
  istr(' abc ').strip() ==> istr('abc')
1125
1143
  ...
@@ -299,6 +299,7 @@ is
299
299
  Apart from with numeric (to be interpreted as an int) or str, istr can be initialized with
300
300
  several other types:
301
301
 
302
+ - if `None`, `None` will be returned.
302
303
 
303
304
  - if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
304
305
  ```
@@ -364,6 +365,37 @@ In contrast to the builtin `range`, `istr.range` supports a non keyword paramete
364
365
 
365
366
  `list(istr.range(length=3))` is equivalent to `istr.range(100, 1000)`
366
367
 
368
+ #### square root, cubic root and nth root
369
+ The methods `sqrt`, `cbrt`, `nth_root`provide the square root, cubic root and nth root of an istr. If there is no integer root, the fallback value (default `istr('0')` will be returned.
370
+ ```
371
+ istr(64).sqrt() ==> istr('8')
372
+ istr(65).sqrt() ==> istr('0')
373
+ istr(1234**3).cbrt() ==> istr('1234')
374
+ istr(1234**3+1).cbrt() ==> istr('0')
375
+ istr(1234**5).nth_root(5) ==> istr('1234')
376
+ istr(1234**5+1).nth_root(5) ==> istr('0')
377
+ istr(1234**5+1).nth_root(5, 1) ==> istr('1')
378
+ istr(1234**5+1).nth_root(5, None) ==> None
379
+
380
+ istr.sqrt(64) ==> istr('8')
381
+ ```
382
+
383
+ #### integer division
384
+ 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.
385
+
386
+ The result will be always istr-ed, except for None.
387
+ ```
388
+ istr(18).divided_by(3) ==> 6 (actually istr("6"))
389
+ istr(18).divided_by(istr(3)) ==> 6
390
+ istr(19).divided_by(3) ==> istr('0')
391
+ istr(19).divided_by(3, 1) ==> istr('1')
392
+ istr(19).divided_by(3) ==> istr('0')
393
+ istr(19).divided_by(istr(3)) ==> istr('0')
394
+ istr.divided_by(18, 3) ==> 6
395
+ istr.divided_by(19, 3) ==> istr('0')
396
+ istr.divided_by(19, 3, 1) ==> istr('1')
397
+ ```
398
+
367
399
  #### test for even/odd
368
400
 
369
401
  It is possible to test for even/odd (provided the istr can be interpreted as an int) with the `is_even` and `is_odd` method, e.g.
@@ -396,16 +428,16 @@ istr.is_palindrome(min) ==> False
396
428
  #### test for increasing, decreasing, non-decreasing and non-increasing
397
429
 
398
430
  It is possible to test whether the characters of an istr are increasing, decreasing, non-decreasing or non-increasing, like
399
- ```
431
+ ```
400
432
  istr(1223).is_increasing() ==> False
401
433
  istr(1223).is_non_decreasing() ==> True
402
434
  istr(3221).is_decreasing() ==> False
403
435
  istr(3221).is_non_increasing ==> True
404
- ```
405
- It is also possible to test for 'increasingness' and friends for anything that can be converted to a str:
406
436
  ```
437
+ It is also possible to test for 'increasingness' and friends for anything that can be converted to a str:
438
+ ```
407
439
  istr.is_increasing(123) ==> True
408
- ```
440
+ ```
409
441
 
410
442
  #### test for divisibility
411
443
 
@@ -422,20 +454,6 @@ It is also possible to test for divisibility of an ordinary int:
422
454
  istr.is_divisible(18, 3) ==> True
423
455
  istr.is_divisible(19, 3) ==> False
424
456
  ```
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
-
427
- The result will be always istr-ed, except for None.
428
- ```
429
- istr(18).divided_by(3) ==> 6 (actually istr("6"))
430
- istr(18).divided_by(istr(3)) ==> 6
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
- istr.divided_by(18, 3) ==> 6
436
- istr.divided_by(19, 3) ==> istr('0')
437
- istr.divided_by(19, 3, 1) ==> istr('1')
438
- ```
439
457
  #### test for square
440
458
 
441
459
  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.
@@ -732,11 +750,11 @@ istr.count(10,3) ==> istr('10'), istr('13'), istr('16'), ...
732
750
  #### istr.zip
733
751
 
734
752
  The class method `istr.zip` is the equivalent of the zip builtin, but applies istr to each element. `istr.zip` also supports the join parameter:
735
- ```
753
+ ```
736
754
  print(list(istr.zip('12', '345')))
737
755
  print(list(istr.zip('12', '345', join=True)))
738
756
  print(list(istr.zip('12', '345', strict=True)))
739
- ```
757
+ ```
740
758
  results in
741
759
  ```
742
760
  [(istr('1'), istr('3')), (istr('2'), istr('4'))]
@@ -750,11 +768,11 @@ to `istr` and then concatenate these.
750
768
 
751
769
  `
752
770
 
753
- ```
771
+ ```
754
772
  list(istr.concat(((1,2),(3,4))) ==> istr([12,34])
755
773
  list(istr.concat(istr.permutations(range(3),2))) ==>
756
774
  [istr('01'), istr('02'), istr('10'), istr('12'), istr('20'), istr('21')]
757
- ```
775
+ ```
758
776
 
759
777
  #### prod to get product of an iterable
760
778
 
@@ -1084,29 +1102,29 @@ operator/function int str Example
1084
1102
  + x istr(20) + 3 ==> istr('23')
1085
1103
  _ x istr(20) - 3 ==> istr('17')
1086
1104
  * x istr(20) * 3 ==> istr('60')
1087
- / x istr(20) / 3 ==> istr('6')
1088
- // x istr(20) // 3 ==> istr('6')
1089
- % x istr(20) % 3 ==> istr('2')
1090
- divmod x divmod(istr(20), 3) ==> (istr('6'), istr('2'))
1091
- ** x istr(2) ** 3 ==> istr('8')
1092
- <=, <, >, >= x istr('100') > istr('2') ==> True
1093
- abs x abs(istr(-20)) ==> istr('20')
1094
- int x int(istr("20")) ==> 20
1095
- float x float(istr("20")) ==> 20.0
1096
- complex x complex(istr("20")) ==> (20+0j)
1097
- == x x istr(20) == 20 ==> True | istr(20) == '20' ==> True
1098
- bool x x *) bool(istr(' 0 ')) ==> False | bool(istr('')) ==> False
1099
- @ x istr(20) @ 3 ==> istr('202020')
1100
- | x istr(20) | '5' ==> istr('205')
1101
- slicing x istr(12345)[1:3] ==> istr('23')
1102
- iterate x [x for x in istr(20)] ==> [istr('2'), istr('0')]
1103
- len x len(istr(' 20 ')) ==> 4
1104
- count x istr(100).count(0) ==> 2
1105
+ / x istr(20) / 3 ==> istr('6')
1106
+ // x istr(20) // 3 ==> istr('6')
1107
+ % x istr(20) % 3 ==> istr('2')
1108
+ divmod x divmod(istr(20), 3) ==> (istr('6'), istr('2'))
1109
+ ** x istr(2) ** 3 ==> istr('8')
1110
+ <=, <, >, >= x istr('100') > istr('2') ==> True
1111
+ abs x abs(istr(-20)) ==> istr('20')
1112
+ int x int(istr("20")) ==> 20
1113
+ float x float(istr("20")) ==> 20.0
1114
+ complex x complex(istr("20")) ==> (20+0j)
1115
+ == x x istr(20) == 20 ==> True | istr(20) == '20' ==> True
1116
+ bool x x *) bool(istr(' 0 ')) ==> False | bool(istr('')) ==> False
1117
+ @ x istr(20) @ 3 ==> istr('202020')
1118
+ | x istr(20) | '5' ==> istr('205')
1119
+ slicing x istr(12345)[1:3] ==> istr('23')
1120
+ iterate x [x for x in istr(20)] ==> [istr('2'), istr('0')]
1121
+ len x len(istr(' 20 ')) ==> 4
1122
+ count x istr(100).count(0) ==> 2
1105
1123
  istr.count(100) ==> istr('100'), istr('101'). istr('102'), ...
1106
- index x istr(' 100 ').index('0') ==> 2
1107
- split x istr('1 2').split() ==> (istr('1'), istr('2'))
1108
- string format x f"|{istr(1234):6}|" ==> '|1234 |'
1109
- other string methods x istr('aAbBcC').lower() ==> istr('aabbcc')
1124
+ index x istr(' 100 ').index('0') ==> 2
1125
+ split x istr('1 2').split() ==> (istr('1'), istr('2'))
1126
+ string format x f"|{istr(1234):6}|" ==> '|1234 |'
1127
+ other string methods x istr('aAbBcC').lower() ==> istr('aabbcc')
1110
1128
  istr('aAbBcC').islower() ==> False
1111
1129
  istr(' abc ').strip() ==> istr('abc')
1112
1130
  ...
@@ -5,7 +5,7 @@
5
5
  # |_||___/ \__||_|
6
6
  # strings you can count on
7
7
 
8
- __version__ = "1.1.35"
8
+ __version__ = "1.1.36"
9
9
  import functools
10
10
  import itertools
11
11
  import types
@@ -261,6 +261,8 @@ class istr(str):
261
261
  raise TypeError("no parameter given")
262
262
 
263
263
  match value:
264
+ case None:
265
+ return None
264
266
  case range():
265
267
  return cls.range(value.start, value.stop, value.step, base=base, int_format=int_format, repr_mode=repr_mode)
266
268
  case _range():
@@ -430,23 +432,17 @@ class istr(str):
430
432
  if fallback is not None:
431
433
  fallback = istr(fallback)
432
434
  if divisor == 0:
433
- if fallback is None:
434
- return None
435
- else:
436
- return istr(fallback)
435
+ return istr(fallback)
437
436
  quotient, remainder = divmod(istr.interpret_as_int(self), int(divisor))
438
437
  if remainder == 0:
439
438
  return istr(quotient)
440
439
  else:
441
- if fallback is None:
442
- return None
443
- else:
444
- return istr(fallback)
440
+ return istr(fallback)
445
441
 
446
442
  def divisors(self, sorted=True):
447
443
  n = istr.interpret_as_int(self)
448
- if n<=0:
449
- raise ValueError(f'must be >=0, not {n}')
444
+ if n <= 0:
445
+ raise ValueError(f"must be >=0, not {n}")
450
446
  tail = []
451
447
  for d in range(1, math.isqrt(n) + 1):
452
448
  q, r = divmod(n, d)
@@ -460,6 +456,23 @@ class istr(str):
460
456
  if sorted:
461
457
  yield from reversed(tail)
462
458
 
459
+ def sqrt(self, fallback=0):
460
+ return istr.nth_root(self, 2, fallback)
461
+
462
+ def cbrt(self, fallback=0):
463
+ return istr.nth_root(self, 3, fallback)
464
+
465
+ def nth_root(self, nth, fallback=0):
466
+ n = istr.interpret_as_int(self)
467
+ if nth < 1 or int(nth) != nth:
468
+ raise ValueError(f"nth should be integer >=1, not {nth}")
469
+ if n < 0:
470
+ raise ValueError(f"value should be integer >=0, not {n}")
471
+ result = round(n ** (1 / nth))
472
+ if result**nth != n:
473
+ result = fallback
474
+ return istr(result)
475
+
463
476
  def is_prime(self):
464
477
  n = istr.interpret_as_int(self)
465
478
  if n < 1_000_000:
@@ -623,10 +636,7 @@ class istr(str):
623
636
  try:
624
637
  return istr(istr.interpret_as_str(self)[int(index)])
625
638
  except IndexError:
626
- if fallback is None:
627
- return fallback
628
- else:
629
- return istr(fallback)
639
+ return istr(fallback)
630
640
 
631
641
  def decompose(self, letters, namespace=None):
632
642
  """
@@ -1097,7 +1107,7 @@ def get_namespace(namespace):
1097
1107
 
1098
1108
 
1099
1109
  def real_caller_frame():
1100
- # this will return the frame of the first frame on the stack that does not belong to this module,
1110
+ # this will return the first frame on the stack that does not belong to this module,
1101
1111
  # so in the 'user' space
1102
1112
  frame = inspect.currentframe()
1103
1113
  frame_name = frame.f_globals.get("__name__")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: istr-python
3
- Version: 1.1.35
3
+ Version: 1.1.36
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
@@ -312,6 +312,7 @@ is
312
312
  Apart from with numeric (to be interpreted as an int) or str, istr can be initialized with
313
313
  several other types:
314
314
 
315
+ - if `None`, `None` will be returned.
315
316
 
316
317
  - if a dict (or subtype of dict), the same type dict will be returned with all *values* istr'ed
317
318
  ```
@@ -377,6 +378,37 @@ In contrast to the builtin `range`, `istr.range` supports a non keyword paramete
377
378
 
378
379
  `list(istr.range(length=3))` is equivalent to `istr.range(100, 1000)`
379
380
 
381
+ #### square root, cubic root and nth root
382
+ The methods `sqrt`, `cbrt`, `nth_root`provide the square root, cubic root and nth root of an istr. If there is no integer root, the fallback value (default `istr('0')` will be returned.
383
+ ```
384
+ istr(64).sqrt() ==> istr('8')
385
+ istr(65).sqrt() ==> istr('0')
386
+ istr(1234**3).cbrt() ==> istr('1234')
387
+ istr(1234**3+1).cbrt() ==> istr('0')
388
+ istr(1234**5).nth_root(5) ==> istr('1234')
389
+ istr(1234**5+1).nth_root(5) ==> istr('0')
390
+ istr(1234**5+1).nth_root(5, 1) ==> istr('1')
391
+ istr(1234**5+1).nth_root(5, None) ==> None
392
+
393
+ istr.sqrt(64) ==> istr('8')
394
+ ```
395
+
396
+ #### integer division
397
+ 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.
398
+
399
+ The result will be always istr-ed, except for None.
400
+ ```
401
+ istr(18).divided_by(3) ==> 6 (actually istr("6"))
402
+ istr(18).divided_by(istr(3)) ==> 6
403
+ istr(19).divided_by(3) ==> istr('0')
404
+ istr(19).divided_by(3, 1) ==> istr('1')
405
+ istr(19).divided_by(3) ==> istr('0')
406
+ istr(19).divided_by(istr(3)) ==> istr('0')
407
+ istr.divided_by(18, 3) ==> 6
408
+ istr.divided_by(19, 3) ==> istr('0')
409
+ istr.divided_by(19, 3, 1) ==> istr('1')
410
+ ```
411
+
380
412
  #### test for even/odd
381
413
 
382
414
  It is possible to test for even/odd (provided the istr can be interpreted as an int) with the `is_even` and `is_odd` method, e.g.
@@ -409,16 +441,16 @@ istr.is_palindrome(min) ==> False
409
441
  #### test for increasing, decreasing, non-decreasing and non-increasing
410
442
 
411
443
  It is possible to test whether the characters of an istr are increasing, decreasing, non-decreasing or non-increasing, like
412
- ```
444
+ ```
413
445
  istr(1223).is_increasing() ==> False
414
446
  istr(1223).is_non_decreasing() ==> True
415
447
  istr(3221).is_decreasing() ==> False
416
448
  istr(3221).is_non_increasing ==> True
417
- ```
418
- It is also possible to test for 'increasingness' and friends for anything that can be converted to a str:
419
449
  ```
450
+ It is also possible to test for 'increasingness' and friends for anything that can be converted to a str:
451
+ ```
420
452
  istr.is_increasing(123) ==> True
421
- ```
453
+ ```
422
454
 
423
455
  #### test for divisibility
424
456
 
@@ -435,20 +467,6 @@ It is also possible to test for divisibility of an ordinary int:
435
467
  istr.is_divisible(18, 3) ==> True
436
468
  istr.is_divisible(19, 3) ==> False
437
469
  ```
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
-
440
- The result will be always istr-ed, except for None.
441
- ```
442
- istr(18).divided_by(3) ==> 6 (actually istr("6"))
443
- istr(18).divided_by(istr(3)) ==> 6
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
- istr.divided_by(18, 3) ==> 6
449
- istr.divided_by(19, 3) ==> istr('0')
450
- istr.divided_by(19, 3, 1) ==> istr('1')
451
- ```
452
470
  #### test for square
453
471
 
454
472
  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.
@@ -745,11 +763,11 @@ istr.count(10,3) ==> istr('10'), istr('13'), istr('16'), ...
745
763
  #### istr.zip
746
764
 
747
765
  The class method `istr.zip` is the equivalent of the zip builtin, but applies istr to each element. `istr.zip` also supports the join parameter:
748
- ```
766
+ ```
749
767
  print(list(istr.zip('12', '345')))
750
768
  print(list(istr.zip('12', '345', join=True)))
751
769
  print(list(istr.zip('12', '345', strict=True)))
752
- ```
770
+ ```
753
771
  results in
754
772
  ```
755
773
  [(istr('1'), istr('3')), (istr('2'), istr('4'))]
@@ -763,11 +781,11 @@ to `istr` and then concatenate these.
763
781
 
764
782
  `
765
783
 
766
- ```
784
+ ```
767
785
  list(istr.concat(((1,2),(3,4))) ==> istr([12,34])
768
786
  list(istr.concat(istr.permutations(range(3),2))) ==>
769
787
  [istr('01'), istr('02'), istr('10'), istr('12'), istr('20'), istr('21')]
770
- ```
788
+ ```
771
789
 
772
790
  #### prod to get product of an iterable
773
791
 
@@ -1097,29 +1115,29 @@ operator/function int str Example
1097
1115
  + x istr(20) + 3 ==> istr('23')
1098
1116
  _ x istr(20) - 3 ==> istr('17')
1099
1117
  * x istr(20) * 3 ==> istr('60')
1100
- / x istr(20) / 3 ==> istr('6')
1101
- // x istr(20) // 3 ==> istr('6')
1102
- % x istr(20) % 3 ==> istr('2')
1103
- divmod x divmod(istr(20), 3) ==> (istr('6'), istr('2'))
1104
- ** x istr(2) ** 3 ==> istr('8')
1105
- <=, <, >, >= x istr('100') > istr('2') ==> True
1106
- abs x abs(istr(-20)) ==> istr('20')
1107
- int x int(istr("20")) ==> 20
1108
- float x float(istr("20")) ==> 20.0
1109
- complex x complex(istr("20")) ==> (20+0j)
1110
- == x x istr(20) == 20 ==> True | istr(20) == '20' ==> True
1111
- bool x x *) bool(istr(' 0 ')) ==> False | bool(istr('')) ==> False
1112
- @ x istr(20) @ 3 ==> istr('202020')
1113
- | x istr(20) | '5' ==> istr('205')
1114
- slicing x istr(12345)[1:3] ==> istr('23')
1115
- iterate x [x for x in istr(20)] ==> [istr('2'), istr('0')]
1116
- len x len(istr(' 20 ')) ==> 4
1117
- count x istr(100).count(0) ==> 2
1118
+ / x istr(20) / 3 ==> istr('6')
1119
+ // x istr(20) // 3 ==> istr('6')
1120
+ % x istr(20) % 3 ==> istr('2')
1121
+ divmod x divmod(istr(20), 3) ==> (istr('6'), istr('2'))
1122
+ ** x istr(2) ** 3 ==> istr('8')
1123
+ <=, <, >, >= x istr('100') > istr('2') ==> True
1124
+ abs x abs(istr(-20)) ==> istr('20')
1125
+ int x int(istr("20")) ==> 20
1126
+ float x float(istr("20")) ==> 20.0
1127
+ complex x complex(istr("20")) ==> (20+0j)
1128
+ == x x istr(20) == 20 ==> True | istr(20) == '20' ==> True
1129
+ bool x x *) bool(istr(' 0 ')) ==> False | bool(istr('')) ==> False
1130
+ @ x istr(20) @ 3 ==> istr('202020')
1131
+ | x istr(20) | '5' ==> istr('205')
1132
+ slicing x istr(12345)[1:3] ==> istr('23')
1133
+ iterate x [x for x in istr(20)] ==> [istr('2'), istr('0')]
1134
+ len x len(istr(' 20 ')) ==> 4
1135
+ count x istr(100).count(0) ==> 2
1118
1136
  istr.count(100) ==> istr('100'), istr('101'). istr('102'), ...
1119
- index x istr(' 100 ').index('0') ==> 2
1120
- split x istr('1 2').split() ==> (istr('1'), istr('2'))
1121
- string format x f"|{istr(1234):6}|" ==> '|1234 |'
1122
- other string methods x istr('aAbBcC').lower() ==> istr('aabbcc')
1137
+ index x istr(' 100 ').index('0') ==> 2
1138
+ split x istr('1 2').split() ==> (istr('1'), istr('2'))
1139
+ string format x f"|{istr(1234):6}|" ==> '|1234 |'
1140
+ other string methods x istr('aAbBcC').lower() ==> istr('aabbcc')
1123
1141
  istr('aAbBcC').islower() ==> False
1124
1142
  istr(' abc ').strip() ==> istr('abc')
1125
1143
  ...
@@ -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.35"
13
+ version = "1.1.36"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.10"
16
16
  dependencies = []
@@ -218,6 +218,9 @@ def test_divmod():
218
218
  def test_iter():
219
219
  assert [x for x in istr.range(3)] == [istr(0), istr(1), istr(2)]
220
220
 
221
+ def test_none():
222
+ assert istr(None) is None
223
+
221
224
 
222
225
  def test_reversed():
223
226
  assert [x for x in reversed(istr.range(3))] == [istr(2), istr(1), istr(0)]
@@ -410,6 +413,56 @@ def test_getitem():
410
413
  assert istr.getitem(b, 6).equals(istr(""))
411
414
 
412
415
 
416
+ def test_nth_power():
417
+ assert istr(1234).nth_root(1).equals(istr(1234))
418
+ assert istr(1234**5).nth_root(5).equals(istr(1234))
419
+ assert istr(1234**5 + 1).nth_root(5).equals(istr(0))
420
+ assert istr(1234**5 + 1).nth_root(5, 1).equals(istr(1))
421
+ assert istr(1234**5 + 1).nth_root(5, None) is None
422
+
423
+ assert istr.nth_root(1234**5, 5).equals(istr(1234))
424
+ assert istr.nth_root(1234**5 + 1, 5).equals(istr(0))
425
+ assert istr.nth_root(1234**5 + 1, 5, 1).equals(istr(1))
426
+ assert istr.nth_root(1234**5 + 1, 5, None) is None
427
+
428
+ with pytest.raises(ValueError):
429
+ istr(1234).nth_root(0)
430
+ with pytest.raises(ValueError):
431
+ istr(1234).nth_root(1.5)
432
+ with pytest.raises(ValueError):
433
+ istr(-(1234**5)).nth_root(5)
434
+
435
+
436
+ def test_sqrt():
437
+ assert istr(1234**2).sqrt().equals(istr(1234))
438
+ assert istr(1234**2 + 1).sqrt().equals(istr(0))
439
+ assert istr(1234**2 + 1).sqrt(1).equals(istr(1))
440
+ assert istr(1234**2 + 1).sqrt(None) is None
441
+
442
+ assert istr.sqrt(1234**2).equals(istr(1234))
443
+ assert istr.sqrt(1234**2 + 1).equals(istr(0))
444
+ assert istr.sqrt(1234**2 + 1, 1).equals(istr(1))
445
+ assert istr.sqrt(1232**2 + 1, None) is None
446
+
447
+ with pytest.raises(ValueError):
448
+ istr(-125).sqrt()
449
+
450
+
451
+ def test_cbrt():
452
+ assert istr(1234**3).cbrt().equals(istr(1234))
453
+ assert istr(1234**3 + 1).cbrt().equals(istr(0))
454
+ assert istr(1234**3 + 1).cbrt(1).equals(istr(1))
455
+ assert istr(1234**3 + 1).cbrt(None) is None
456
+
457
+ assert istr.cbrt(1234**3).equals(istr(1234))
458
+ assert istr.cbrt(1234**3 + 1).equals(istr(0))
459
+ assert istr.cbrt(1234**3 + 1, 1).equals(istr(1))
460
+ assert istr.cbrt(1232**3 + 1, None) is None
461
+
462
+ with pytest.raises(ValueError):
463
+ istr(-125).cbrt()
464
+
465
+
413
466
  def test_is_divisible_by():
414
467
  assert istr(18).is_divisible_by(3)
415
468
  assert istr(18).is_divisible_by(istr(3))
@@ -506,12 +559,14 @@ def test_is_power_of():
506
559
  assert istr(10).is_power_of(1)
507
560
  assert istr(-8).is_power_of(3)
508
561
 
562
+
509
563
  def test_is_power_of_no_arg():
510
564
  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
-
565
+ assert not istr(2**10 + 1).is_power_of()
566
+ assert istr(1).is_power_of()
567
+ assert istr((-5) ** 3).is_power_of()
568
+
569
+
515
570
  def test_is_prime():
516
571
  assert not istr(0).is_prime()
517
572
  assert not istr(1).is_prime()
@@ -582,19 +637,21 @@ def test_power_ofs():
582
637
  assert id(istr.power_ofs(3, 2000)) != id(istr.cubes(3, 2000)) # test caching
583
638
  assert id(istr.power_ofs(3, 1000, cache=False)) != id(istr.cubes(3, 1000, cache=False)) # test caching
584
639
 
640
+
585
641
  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")):
642
+ assert list(istr(36).divisors()) == istr([1, 2, 3, 4, 6, 9, 12, 18, 36])
643
+ assert list(istr(36).divisors(sorted=False)) == istr([1, 36, 2, 18, 3, 12, 4, 9, 6])
644
+ assert list(istr(37).divisors()) == istr([1, 37])
645
+ assert list(istr(1).divisors()) == istr([1])
646
+ with pytest.raises(ValueError, match=re.escape("must be >=0, not 0")):
591
647
  list(istr(0).divisors())
592
- with pytest.raises(ValueError,match=re.escape("must be >=0, not -1")):
648
+ with pytest.raises(ValueError, match=re.escape("must be >=0, not -1")):
593
649
  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
-
650
+ assert list(istr.divisors(36)) == istr([1, 2, 3, 4, 6, 9, 12, 18, 36])
651
+ assert list(istr.divisors(36, False)) == istr([1, 36, 2, 18, 3, 12, 4, 9, 6])
652
+ assert list(istr.divisors(37)) == istr([1, 37])
653
+
654
+
598
655
  def test_in_range():
599
656
  primes1000 = istr.primes(1000)
600
657
  n = len(primes1000)
File without changes