istr-python 1.1.29__tar.gz → 1.1.31__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.29
3
+ Version: 1.1.31
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
@@ -548,6 +548,46 @@ istr('0456')[::-1] ==> istr('6540')
548
548
  > istr(-456).reversed() + 3 ==> TypeError
549
549
  > ```
550
550
 
551
+
552
+ #### ceil
553
+
554
+ The `istr.ceil` method can be used to find the smallest integer, divisible by a given number (divisible_by), greater than or equal to the value.
555
+ This can be useful to step through all multiples of n, >= m, like:
556
+
557
+ ```
558
+ for i in istr.range(istr(1000/3, 10000, 3)) ==> # 1002, 1005, ... 9999
559
+ for i in istr.count(int(istr,ceil(1000/3)), 3)) ==> 1002, 1005, ...
560
+ ```
561
+ Examples:
562
+ ```
563
+ istr(1000).ceil() ==> 1000 # divisible_by is 1 by default
564
+ istr(1000).ceil(2) ==> 1000
565
+ istr(1000).ceil(3) ==> 1002
566
+ ```
567
+ It is also possible to use the ceil method for floats or ints:
568
+ ```
569
+ istr.ceil(1000.2) ==> 1001
570
+ istr.ceil(1000.2, 2) ==> 1002
571
+ istr.ceil(1000.2, 3 ==> 1002
572
+ ```
573
+
574
+ #### floor
575
+
576
+ The `istr.floor` method can be used to find the largest integer, divisible by a given number (divisible_by), smaller than or equal to the value.
577
+
578
+ Examples:
579
+ ```
580
+ istr(1000).floor() ==> 1000 # divisible_by is 1 by default
581
+ istr(1000).floor(2) ==> 1000
582
+ istr(1000).floor(3) ==> 999
583
+ ```
584
+ It is also possible to use the ceil method for floats or ints:
585
+ ```
586
+ istr.floor(1000.2) ==> 1000
587
+ istr.floor(1000.2, 2) ==> 1000
588
+ istr.floor(1000.2, 3 ==> 999
589
+ ```
590
+
551
591
  #### enumerate with istrs
552
592
 
553
593
  The `istr.enumerate` class method can be used just as the built-in enumerate function.
@@ -535,6 +535,46 @@ istr('0456')[::-1] ==> istr('6540')
535
535
  > istr(-456).reversed() + 3 ==> TypeError
536
536
  > ```
537
537
 
538
+
539
+ #### ceil
540
+
541
+ The `istr.ceil` method can be used to find the smallest integer, divisible by a given number (divisible_by), greater than or equal to the value.
542
+ This can be useful to step through all multiples of n, >= m, like:
543
+
544
+ ```
545
+ for i in istr.range(istr(1000/3, 10000, 3)) ==> # 1002, 1005, ... 9999
546
+ for i in istr.count(int(istr,ceil(1000/3)), 3)) ==> 1002, 1005, ...
547
+ ```
548
+ Examples:
549
+ ```
550
+ istr(1000).ceil() ==> 1000 # divisible_by is 1 by default
551
+ istr(1000).ceil(2) ==> 1000
552
+ istr(1000).ceil(3) ==> 1002
553
+ ```
554
+ It is also possible to use the ceil method for floats or ints:
555
+ ```
556
+ istr.ceil(1000.2) ==> 1001
557
+ istr.ceil(1000.2, 2) ==> 1002
558
+ istr.ceil(1000.2, 3 ==> 1002
559
+ ```
560
+
561
+ #### floor
562
+
563
+ The `istr.floor` method can be used to find the largest integer, divisible by a given number (divisible_by), smaller than or equal to the value.
564
+
565
+ Examples:
566
+ ```
567
+ istr(1000).floor() ==> 1000 # divisible_by is 1 by default
568
+ istr(1000).floor(2) ==> 1000
569
+ istr(1000).floor(3) ==> 999
570
+ ```
571
+ It is also possible to use the ceil method for floats or ints:
572
+ ```
573
+ istr.floor(1000.2) ==> 1000
574
+ istr.floor(1000.2, 2) ==> 1000
575
+ istr.floor(1000.2, 3 ==> 999
576
+ ```
577
+
538
578
  #### enumerate with istrs
539
579
 
540
580
  The `istr.enumerate` class method can be used just as the built-in enumerate function.
@@ -5,7 +5,7 @@
5
5
  # |_||___/ \__||_|
6
6
  # strings you can count on
7
7
 
8
- __version__ = "1.1.29"
8
+ __version__ = "1.1.31"
9
9
  import functools
10
10
  import itertools
11
11
  import types
@@ -15,7 +15,7 @@ import math
15
15
  import operator
16
16
  import copy
17
17
  import bisect
18
-
18
+ import collections
19
19
  """
20
20
  Note: the changelog is in changelog.md
21
21
 
@@ -404,23 +404,23 @@ class istr(str):
404
404
  return not istr.is_divisible_by(self, 2)
405
405
 
406
406
  def is_palindrome(self):
407
- self_as_str = str(self)
407
+ self_as_str = istr.interpret_as_str(self)
408
408
  return self_as_str == self_as_str[::-1]
409
409
 
410
410
  def is_non_decreasing(self):
411
- self_as_str = str(self)
411
+ self_as_str = istr.interpret_as_str(self)
412
412
  return all(i0 <= i1 for i0, i1 in zip(self_as_str, self_as_str[1:]))
413
413
 
414
414
  def is_non_increasing(self):
415
- self_as_str = str(self)
415
+ self_as_str = istr.interpret_as_str(self)
416
416
  return all(i0 >= i1 for i0, i1 in zip(self_as_str, self_as_str[1:]))
417
417
 
418
418
  def is_increasing(self):
419
- self_as_str = str(self)
419
+ self_as_str = istr.interpret_as_str(self)
420
420
  return all(i0 < i1 for i0, i1 in zip(self_as_str, self_as_str[1:]))
421
421
 
422
422
  def is_decreasing(self):
423
- self_as_str = str(self)
423
+ self_as_str = istr.interpret_as_str(self)
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):
@@ -677,15 +677,50 @@ class istr(str):
677
677
 
678
678
  def reversed(self):
679
679
  return self[::-1]
680
+
681
+ def ceil(self, divisible_by=1):
682
+ if divisible_by<=0:
683
+ raise ValueError(f"step has to be >0, not {divisible_by}")
684
+ if divisible_by!=int(divisible_by):
685
+ raise ValueError(f"step has to be an integer value, not {divisible_by}")
686
+ n = istr.interpret_as_float(self)
687
+ return istr((math.ceil(n/divisible_by))*divisible_by)
688
+
689
+ def floor(self, divisible_by=1):
690
+ if divisible_by<=0:
691
+ raise ValueError(f"step has to be >0, not {divisible_by}")
692
+ if divisible_by!=int(divisible_by):
693
+ raise ValueError(f"step has to be an integer value, not {divisible_by}")
694
+ return istr((math.floor(self/divisible_by))*divisible_by)
695
+
680
696
 
681
697
  def interpret_as_int(self):
682
698
  if isinstance(self, istr):
683
699
  if not self.is_int():
684
700
  raise TypeError(f"not interpretable as int: {self._frepr(self)}")
685
- n = self._as_int
686
- else:
687
- n = int(self)
688
- return n
701
+ return self._as_int
702
+ if isinstance(self, collections.abc.Iterable) and not isinstance(self,str):
703
+ return int(istr.join(self))
704
+
705
+ return int(self)
706
+
707
+
708
+ def interpret_as_float(self):
709
+ if isinstance(self, istr):
710
+ if not self.is_int():
711
+ raise TypeError(f"not interpretable as float: {self._frepr(self)}")
712
+ return self._as_int
713
+ if isinstance(self, collections.abc.Iterable) and not isinstance(self,str):
714
+ return float(istr.join(self))
715
+
716
+ return float(self)
717
+
718
+ def interpret_as_str(self):
719
+
720
+ if isinstance(self, collections.abc.Iterable) and not isinstance(self,str):
721
+ return istr.join(self)
722
+
723
+ return str(self)
689
724
 
690
725
  def _str_method(self, name, *args, **kwargs):
691
726
  return self.__class__(getattr(super(), name)(*args, **kwargs))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: istr-python
3
- Version: 1.1.29
3
+ Version: 1.1.31
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
@@ -548,6 +548,46 @@ istr('0456')[::-1] ==> istr('6540')
548
548
  > istr(-456).reversed() + 3 ==> TypeError
549
549
  > ```
550
550
 
551
+
552
+ #### ceil
553
+
554
+ The `istr.ceil` method can be used to find the smallest integer, divisible by a given number (divisible_by), greater than or equal to the value.
555
+ This can be useful to step through all multiples of n, >= m, like:
556
+
557
+ ```
558
+ for i in istr.range(istr(1000/3, 10000, 3)) ==> # 1002, 1005, ... 9999
559
+ for i in istr.count(int(istr,ceil(1000/3)), 3)) ==> 1002, 1005, ...
560
+ ```
561
+ Examples:
562
+ ```
563
+ istr(1000).ceil() ==> 1000 # divisible_by is 1 by default
564
+ istr(1000).ceil(2) ==> 1000
565
+ istr(1000).ceil(3) ==> 1002
566
+ ```
567
+ It is also possible to use the ceil method for floats or ints:
568
+ ```
569
+ istr.ceil(1000.2) ==> 1001
570
+ istr.ceil(1000.2, 2) ==> 1002
571
+ istr.ceil(1000.2, 3 ==> 1002
572
+ ```
573
+
574
+ #### floor
575
+
576
+ The `istr.floor` method can be used to find the largest integer, divisible by a given number (divisible_by), smaller than or equal to the value.
577
+
578
+ Examples:
579
+ ```
580
+ istr(1000).floor() ==> 1000 # divisible_by is 1 by default
581
+ istr(1000).floor(2) ==> 1000
582
+ istr(1000).floor(3) ==> 999
583
+ ```
584
+ It is also possible to use the ceil method for floats or ints:
585
+ ```
586
+ istr.floor(1000.2) ==> 1000
587
+ istr.floor(1000.2, 2) ==> 1000
588
+ istr.floor(1000.2, 3 ==> 999
589
+ ```
590
+
551
591
  #### enumerate with istrs
552
592
 
553
593
  The `istr.enumerate` class method can be used just as the built-in enumerate function.
@@ -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.29"
13
+ version = "1.1.31"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.10"
16
16
  dependencies = []
@@ -988,6 +988,52 @@ def test_decompose():
988
988
  istr(123).decompose("xy1")
989
989
 
990
990
 
991
+ def test_ceil():
992
+ assert istr(1000).ceil() == 1000
993
+ assert istr(1000).ceil(1) == 1000
994
+ assert istr(1000).ceil(2) == 1000
995
+ assert istr(1000).ceil(3) == 1002
996
+ assert istr.ceil(1000) == 1000
997
+ assert istr.ceil(1000, 1) == 1000
998
+ assert istr.ceil(1000.2, 1).equals(istr(1001))
999
+
1000
+
1001
+ def test_floor():
1002
+ assert istr(1000).floor() == 1000
1003
+ assert istr(1000).floor(1) == 1000
1004
+ assert istr(1000).floor(2) == 1000
1005
+ assert istr(1000).floor(3) == 999
1006
+ assert istr.floor(1000) == 1000
1007
+ assert istr.floor(1000, 1) == 1000
1008
+ assert istr.floor(1000.2, 1).equals(istr(1000))
1009
+
1010
+
1011
+ def test_tuple_join():
1012
+ assert istr.is_prime(istr(1, 3))
1013
+ assert istr.is_square(istr(6, 4))
1014
+ assert istr.is_cube(istr(6, 4))
1015
+ assert istr.is_power_of(istr(3, 2), 5)
1016
+ assert istr.is_odd(istr(1, 3))
1017
+ assert istr.is_even(istr(1, 2))
1018
+ assert istr.is_triangular(istr(1, 0))
1019
+ assert istr.is_increasing(istr(1, 2))
1020
+ assert istr.is_non_decreasing(istr(1, 2, 2, 3))
1021
+ assert istr.is_decreasing(istr(2, 1))
1022
+ assert istr.is_non_increasing(istr(3, 2, 2, 1))
1023
+
1024
+ assert not istr.is_prime(istr(1, 4))
1025
+ assert not istr.is_square(istr(6, 5))
1026
+ assert not istr.is_cube(istr(6, 5))
1027
+ assert not istr.is_power_of(istr(3, 3), 5)
1028
+ assert not istr.is_odd(istr(1, 4))
1029
+ assert not istr.is_even(istr(1, 3))
1030
+ assert not istr.is_triangular(istr(1, 1))
1031
+ assert not istr.is_increasing(istr(1, 1))
1032
+ assert not istr.is_non_decreasing(istr(1, 2, 2, 1))
1033
+ assert not istr.is_decreasing(istr(2, 2))
1034
+ assert not istr.is_non_increasing(istr(3, 2, 2, 4))
1035
+
1036
+
991
1037
  def test_compose():
992
1038
  global x, y, z, _
993
1039
  x = 1
File without changes