istr-python 1.1.35__tar.gz → 1.1.35.post0__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.35.post0
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,36 @@ 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
+ These methods 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
+
380
411
  #### test for even/odd
381
412
 
382
413
  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.
@@ -435,19 +466,6 @@ It is also possible to test for divisibility of an ordinary int:
435
466
  istr.is_divisible(18, 3) ==> True
436
467
  istr.is_divisible(19, 3) ==> False
437
468
  ```
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
469
  ```
452
470
  #### test for square
453
471
 
@@ -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,36 @@ 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
+ These methods 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
+
367
398
  #### test for even/odd
368
399
 
369
400
  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.
@@ -422,19 +453,6 @@ It is also possible to test for divisibility of an ordinary int:
422
453
  istr.is_divisible(18, 3) ==> True
423
454
  istr.is_divisible(19, 3) ==> False
424
455
  ```
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
456
  ```
439
457
  #### test for square
440
458
 
@@ -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.35.post0
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,36 @@ 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
+ These methods 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
+
380
411
  #### test for even/odd
381
412
 
382
413
  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.
@@ -435,19 +466,6 @@ It is also possible to test for divisibility of an ordinary int:
435
466
  istr.is_divisible(18, 3) ==> True
436
467
  istr.is_divisible(19, 3) ==> False
437
468
  ```
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
469
  ```
452
470
  #### test for square
453
471
 
@@ -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.35.post0"
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)