istr-python 1.1.4__tar.gz → 1.1.4.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.4
3
+ Version: 1.1.4.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
@@ -558,8 +558,30 @@ istr.digits('3-') ==> istr('34567879')
558
558
  istr.digits('X-') ==> istr('XYZ')
559
559
  ```
560
560
 
561
+ #### Decomposing to and composing from letter variables
562
+
563
+ When we have an istr, we can decompose the value into individual one letter (global) variables with the `decompose()` method.
564
+ E.g.
565
+
566
+ ```
567
+ istr(485).decompose("abc")
568
+ ```
569
+ will set the global variables `a`, `b` and `c` to be set to `istr(4)`. `istr(8)` and` istr(5)`.
570
+ Note that the length of the letters specifier must be the same as the length of the istr. Furthermore, multiple values for the same variables result in a ValueError.
571
+
572
+ With `istr.compose()`, an istr can be constructed from individual (global) variables.
573
+ E.g.
574
+
575
+ ```
576
+ x = 3
577
+ y = 9
578
+ z = 6
579
+ test = istr.compose("xyz")
580
+ ```
581
+ Now, `test` will be `istr(396)` .
561
582
 
562
583
  #### Subclassing istr
584
+
563
585
  When a class is derived from istr, all methods will return that newly derived class.
564
586
 
565
587
  E.g.
@@ -545,8 +545,30 @@ istr.digits('3-') ==> istr('34567879')
545
545
  istr.digits('X-') ==> istr('XYZ')
546
546
  ```
547
547
 
548
+ #### Decomposing to and composing from letter variables
549
+
550
+ When we have an istr, we can decompose the value into individual one letter (global) variables with the `decompose()` method.
551
+ E.g.
552
+
553
+ ```
554
+ istr(485).decompose("abc")
555
+ ```
556
+ will set the global variables `a`, `b` and `c` to be set to `istr(4)`. `istr(8)` and` istr(5)`.
557
+ Note that the length of the letters specifier must be the same as the length of the istr. Furthermore, multiple values for the same variables result in a ValueError.
558
+
559
+ With `istr.compose()`, an istr can be constructed from individual (global) variables.
560
+ E.g.
561
+
562
+ ```
563
+ x = 3
564
+ y = 9
565
+ z = 6
566
+ test = istr.compose("xyz")
567
+ ```
568
+ Now, `test` will be `istr(396)` .
548
569
 
549
570
  #### Subclassing istr
571
+
550
572
  When a class is derived from istr, all methods will return that newly derived class.
551
573
 
552
574
  E.g.
@@ -366,14 +366,28 @@ class istr(str):
366
366
  return n % 2 == 1
367
367
 
368
368
  def is_square(self):
369
+ return istr._is_power_of(self, 2)
370
+
371
+ def is_cube(self):
372
+ return istr._is_power_of(self, 3)
373
+
374
+ def is_power_of(self, power_of):
375
+ return istr._is_power_of(self, power_of)
376
+
377
+ @staticmethod
378
+ def _is_power_of(self, power_of):
369
379
  if isinstance(self, istr):
370
380
  if not self.is_int():
371
381
  raise TypeError(f"not interpretable as int: {self._frepr(self)}")
372
382
  n = self._as_int
373
383
  else:
374
384
  n = int(self)
385
+ if power_of < 1:
386
+ raise ValueError(f"power_of must be >=1; not {power_of}")
387
+ if not isinstance(power_of, int):
388
+ raise TypeError(f"power_of must be int; not {type(power_of)}")
375
389
 
376
- return n >= 0 and self == math.isqrt(n) ** 2
390
+ return n >= 0 and self == round(n ** (1 / power_of)) ** power_of
377
391
 
378
392
  def is_prime(self):
379
393
  if isinstance(self, istr):
@@ -411,13 +425,10 @@ class istr(str):
411
425
  raise ValueError(f"multiple values found for variable {letter}")
412
426
  if not letter.isidentifier():
413
427
  raise ValueError(f"{letter} cannot be used as a variable")
414
-
415
428
  lookup[letter] = ch
416
429
  if len(letters) != len(self):
417
430
  raise ValueError(f"incorrect number of variables {len(letters)}; should be {len(self)}")
418
-
419
- for letter, ch in zip(letters, self):
420
- namespace[letter] = ch
431
+ namespace.update(lookup)
421
432
 
422
433
  def __or__(self, other):
423
434
  try:
@@ -626,18 +637,18 @@ class istr(str):
626
637
  cls._digits_cache[key] = result
627
638
  return result
628
639
 
640
+ @classmethod
641
+ def compose(cls, letters, namespace=None):
642
+ """
643
+ compose an istr from individual letter variables
644
+ """
645
+ if namespace is None:
646
+ namespace = inspect.currentframe().f_back.f_globals
647
+ for letter in letters:
648
+ if letter not in namespace:
649
+ raise ValueError(f"variable {letter} not defined")
629
650
 
630
- def compose(letters, namespace=None):
631
- """
632
- compose an istr from individual letter variables
633
- """
634
- if namespace is None:
635
- namespace = inspect.currentframe().f_back.f_globals
636
- for letter in letters:
637
- if letter not in namespace:
638
- raise ValueError(f"variable {letter} not defined")
639
-
640
- return istr("").join(istr(namespace[letter]) for letter in letters)
651
+ return istr("").join(istr(namespace[letter]) for letter in letters)
641
652
 
642
653
 
643
654
  istr.type = type(istr(0))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: istr-python
3
- Version: 1.1.4
3
+ Version: 1.1.4.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
@@ -558,8 +558,30 @@ istr.digits('3-') ==> istr('34567879')
558
558
  istr.digits('X-') ==> istr('XYZ')
559
559
  ```
560
560
 
561
+ #### Decomposing to and composing from letter variables
562
+
563
+ When we have an istr, we can decompose the value into individual one letter (global) variables with the `decompose()` method.
564
+ E.g.
565
+
566
+ ```
567
+ istr(485).decompose("abc")
568
+ ```
569
+ will set the global variables `a`, `b` and `c` to be set to `istr(4)`. `istr(8)` and` istr(5)`.
570
+ Note that the length of the letters specifier must be the same as the length of the istr. Furthermore, multiple values for the same variables result in a ValueError.
571
+
572
+ With `istr.compose()`, an istr can be constructed from individual (global) variables.
573
+ E.g.
574
+
575
+ ```
576
+ x = 3
577
+ y = 9
578
+ z = 6
579
+ test = istr.compose("xyz")
580
+ ```
581
+ Now, `test` will be `istr(396)` .
561
582
 
562
583
  #### Subclassing istr
584
+
563
585
  When a class is derived from istr, all methods will return that newly derived class.
564
586
 
565
587
  E.g.
@@ -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.4"
13
+ version = "1.1.4.post0"
14
14
  readme = "README.md"
15
15
  requires-python = ">=3.7"
16
16
  dependencies = []
@@ -330,7 +330,43 @@ def test_is_square():
330
330
  assert istr.is_square(4)
331
331
  assert istr.is_square(16)
332
332
 
333
-
333
+ def test_is_cube():
334
+ assert not istr(-1).is_cube()
335
+ assert istr(0).is_cube()
336
+ assert istr(1).is_cube()
337
+ assert not istr(2).is_cube()
338
+ assert istr(8).is_cube()
339
+ assert istr(27).is_cube()
340
+ assert not istr(99).is_cube()
341
+ with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
342
+ istr("a").is_cube()
343
+ assert istr.is_cube(0)
344
+ assert istr.is_cube(1)
345
+ assert not istr.is_cube(2)
346
+ assert istr.is_cube(8)
347
+ assert istr.is_cube(27)
348
+
349
+
350
+ def test_is_power_of():
351
+ assert not istr(-1).is_power_of(3)
352
+ assert istr(0).is_power_of(3)
353
+ assert istr(1).is_power_of(3)
354
+ assert not istr(2).is_power_of(3)
355
+ assert istr(8).is_power_of(3)
356
+ assert istr(27).is_power_of(3)
357
+ assert not istr(99).is_power_of(3)
358
+ with pytest.raises(TypeError, match=re.escape(f"not interpretable as int")):
359
+ istr("a").is_power_of(3)
360
+ assert istr.is_power_of(0,3)
361
+ assert istr.is_power_of(1,3)
362
+ assert not istr.is_power_of(2,3)
363
+ assert istr.is_power_of(8,3)
364
+ assert istr.is_power_of(27,3)
365
+ with pytest.raises(TypeError):
366
+ istr(1).is_power_of(3.1)
367
+ with pytest.raises(ValueError):
368
+ istr(1).is_power_of(-1)
369
+
334
370
  def test_is_prime():
335
371
  assert not istr(0).is_prime()
336
372
  assert not istr(1).is_prime()
File without changes