idstring 2.1.4__tar.gz → 2.1.5__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
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: idstring
3
- Version: 2.1.4
3
+ Version: 2.1.5
4
4
  Summary: A Python package to create unique, checksummed serial-number strings.
5
5
  Author: Vernon Cole
6
6
  Author-email: vernondcole@gmail.com
@@ -15,6 +15,7 @@ Classifier: Natural Language :: English
15
15
  Classifier: Operating System :: OS Independent
16
16
  Classifier: Topic :: Software Development
17
17
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Requires-Python: >=3.11
18
19
  Description-Content-Type: text/markdown
19
20
 
20
21
  idstring
@@ -51,6 +52,12 @@ class IDstring generates (and checks) alphanumeric serial number strings with a
51
52
  * The wonky 5-bit binary conversion functions, which only worked with the default alphabet, have been removed.
52
53
  * Version 2.1:
53
54
  * adds a .context dictionary to the IDstring object which a seedstore method can use to store its operating context.
55
+ * Version 2.1.5:
56
+ * Requires Python 3.11 or newer. (Users of older Pythons will continue to get version 2.1.4.)
57
+ * Bug fixes: the .value property now returns the string; IDstring(existing_id) copies the host of an id which has one;
58
+ my_id.sumcheck() checks the instance using its own hash, alphabet and case_shift;
59
+ each new IDstring factory gets its own .context dictionary (they had been sharing one).
60
+ * Tests now run automatically on GitHub Actions for every pull request.
54
61
 
55
62
  ### operation:
56
63
  IDstring extends the built-in str class, using an __ADD__ method which accepts the integer ONE.
@@ -63,7 +70,7 @@ So, in short, you can generate serial numbers like this:
63
70
  serial += 1 # Increments the string & generates a new checksum.
64
71
  print(serial)
65
72
 
66
- Gives a result series which starts with "1Y3R9CB" and loops 925,386 times, ending with:
73
+ Starting from "1Y3R9CB" (the seed plus its check digit), the loop runs 925,387 times, ending with:
67
74
  ```
68
75
  1YYYYVA
69
76
  1YYYYW8
@@ -79,7 +86,7 @@ The actual code for a new leading digit is: "carry_digit = '1' if self.alphabet[
79
86
  The "Luhn mod N" algorithm is used (unless disabled) to add another digit (from the same alphabet) as a check digit.
80
87
 
81
88
  The routine will automatically skip over patterns which contain substrings matching a list of "Dirty Words."
82
- The above example should have been expected to produce 95396 results, but skipped 10 objectionable values.
89
+ Without that filter, the above example would have produced 925,396 results; the filter skipped 9 objectionable values.
83
90
 
84
91
  You can override the supplied alphabet and objectionable word lists. Alternate alphabets overflow correctly.
85
92
 
@@ -32,6 +32,12 @@ class IDstring generates (and checks) alphanumeric serial number strings with a
32
32
  * The wonky 5-bit binary conversion functions, which only worked with the default alphabet, have been removed.
33
33
  * Version 2.1:
34
34
  * adds a .context dictionary to the IDstring object which a seedstore method can use to store its operating context.
35
+ * Version 2.1.5:
36
+ * Requires Python 3.11 or newer. (Users of older Pythons will continue to get version 2.1.4.)
37
+ * Bug fixes: the .value property now returns the string; IDstring(existing_id) copies the host of an id which has one;
38
+ my_id.sumcheck() checks the instance using its own hash, alphabet and case_shift;
39
+ each new IDstring factory gets its own .context dictionary (they had been sharing one).
40
+ * Tests now run automatically on GitHub Actions for every pull request.
35
41
 
36
42
  ### operation:
37
43
  IDstring extends the built-in str class, using an __ADD__ method which accepts the integer ONE.
@@ -44,7 +50,7 @@ So, in short, you can generate serial numbers like this:
44
50
  serial += 1 # Increments the string & generates a new checksum.
45
51
  print(serial)
46
52
 
47
- Gives a result series which starts with "1Y3R9CB" and loops 925,386 times, ending with:
53
+ Starting from "1Y3R9CB" (the seed plus its check digit), the loop runs 925,387 times, ending with:
48
54
  ```
49
55
  1YYYYVA
50
56
  1YYYYW8
@@ -60,7 +66,7 @@ The actual code for a new leading digit is: "carry_digit = '1' if self.alphabet[
60
66
  The "Luhn mod N" algorithm is used (unless disabled) to add another digit (from the same alphabet) as a check digit.
61
67
 
62
68
  The routine will automatically skip over patterns which contain substrings matching a list of "Dirty Words."
63
- The above example should have been expected to produce 95396 results, but skipped 10 objectionable values.
69
+ Without that filter, the above example would have produced 925,396 results; the filter skipped 9 objectionable values.
64
70
 
65
71
  You can override the supplied alphabet and objectionable word lists. Alternate alphabets overflow correctly.
66
72
 
@@ -26,9 +26,10 @@ The string is initialized using a five-argument call like; idstring.IDstring(IDs
26
26
  # http://www.gnu.org/copyleft/lgpl.html
27
27
  #
28
28
  from collections.abc import Callable
29
+ import functools
29
30
 
30
31
  __author__ = "Vernon Cole <vernondcole@gmail.com>"
31
- __version__ = "2.1.4"
32
+ __version__ = "2.1.5"
32
33
 
33
34
  # -- a short calling sample -- real code would use a better storage method ---------
34
35
  #- import pickle, idstring
@@ -51,7 +52,7 @@ __version__ = "2.1.4"
51
52
  #- raise Warning('Caution: IDstring storage not found -- your system may be broken')
52
53
  #-
53
54
  #- # initialize the idString factory with the saved values
54
- #- present_id = idstring.idString(seed=my_seed_memory['seed'], host=my_seed_memory['host'], my_seedstore)
55
+ #- present_id = idstring.IDstring(seed=my_seed_memory['seed'], host=my_seed_memory['host'], seedstore=my_seedstore)
55
56
  #-
56
57
  #- while I_am_still_working:
57
58
  #- some_patient_data = some_kind_of_input()
@@ -75,6 +76,20 @@ def noshift(s):
75
76
 
76
77
  DEFAULT_CASE_SHIFT = str.upper
77
78
 
79
+ _UNSET = object() # marks an argument which the caller did not supply
80
+
81
+
82
+ class _class_or_instance_method:
83
+ """like @classmethod, but the decorated function also receives the instance (or None if called on the class)"""
84
+ def __init__(self, func):
85
+ self.func = func
86
+
87
+ def __get__(self, instance, owner=None):
88
+ @functools.wraps(self.func)
89
+ def bound(*args, **kwargs):
90
+ return self.func(owner, instance, *args, **kwargs)
91
+ return bound
92
+
78
93
  # define the characters which may legally appear in an idString
79
94
  DEFAULT_ALPHABET = "0123456789ABCDEFGHJKLMNPRSTUVWXY"
80
95
 
@@ -108,29 +123,17 @@ class IDstring(str):
108
123
  raise NotImplementedError('This method has been deprecated.')
109
124
 
110
125
 
111
- def _check_host(self, host):
112
- try:
113
- h = self.host
114
- if len(host) != len(h):
115
- raise InvalidIdError(f'New host examples must be same length as defined host field "{h}"')
116
- case_shift = self.case_shift
117
- d = case_shift(str(host).strip())
118
- for c in d:
119
- if not c in self.alphabet:
120
- raise InvalidIdError('Incorrect character "%s" in "%s"' % (c, d))
121
- return d
122
- except AttributeError:
123
- return host
124
-
125
126
  @staticmethod
126
127
  def __new__(cls, idstr=None, seed=None, host='', seedstore=None, hash='', alphabet=None,
127
- case_shift=DEFAULT_CASE_SHIFT, no_check=False, context={}):
128
+ case_shift=DEFAULT_CASE_SHIFT, no_check=False, context=None):
128
129
  """
129
130
  :S - an existing legal idString, or None
130
131
  :seed - the seed string for a new factory [ignored unless S is None]
131
132
  :host - the host portion for a new idString factory, len(host) will set host length for factory
133
+ when cloning an existing IDstring, the host may be omitted (it is copied from the original)
132
134
  :seedstore - a seed preservation function for the new factory
133
135
  :context - a dictionary of things reserved for seedstore to use, internal to seedstore
136
+ (a new empty dictionary is created for each new factory if none is supplied)
134
137
  :hash - an additional string to alter the calculation of the check digit for diverse projects
135
138
  pass hash=None to turn off checksum testing and creation. Makes this module dumb.
136
139
  :case_shift - function to apply to input strings. one of str.upper str.lower or None
@@ -143,12 +146,14 @@ class IDstring(str):
143
146
  if host:
144
147
  host = case_shift(host)
145
148
  except TypeError:
146
- IdStringError(f'Invalid seed "{seed}" or host "{host}" argument')
149
+ raise InvalidIdError(f'Invalid seed "{seed}" or host "{host}" argument') from None
147
150
  alphabet = alphabet or IDstring.ALPHABET
148
151
  value = None
149
152
  if isinstance(idstr, cls):
150
- if host != idstr.host:
151
- raise InvalidIdError(f'Cannot use host "{host}" with id "{value}"')
153
+ if not host: # host was not supplied, so keep the original's host
154
+ host = idstr.host
155
+ elif host != idstr.host:
156
+ raise InvalidIdError(f'Cannot use host "{host}" with id "{idstr}" (its host is "{idstr.host}")')
152
157
  if seed is None: # making a clone
153
158
  value = str(idstr)
154
159
  seed = idstr._seed
@@ -182,7 +187,7 @@ class IDstring(str):
182
187
  if not isinstance(seedstore, Callable):
183
188
  raise IdStringError ('seedstore "%s" is not Callable' % repr(seedstore))
184
189
  new.seedstore = seedstore
185
- new.context = context
190
+ new.context = {} if context is None else context
186
191
  new.case_shift = case_shift
187
192
  return new
188
193
 
@@ -279,7 +284,7 @@ class IDstring(str):
279
284
 
280
285
  @property
281
286
  def value(self):
282
- str(self)
287
+ return str(self)
283
288
 
284
289
  def __repr__(self):
285
290
  return "idString('%s')" % str(self) # reveal what's inside
@@ -290,10 +295,24 @@ class IDstring(str):
290
295
  return _checksum(s, hash, alphabet)
291
296
 
292
297
 
293
- @classmethod
294
- def sumcheck(cls, s=None, hash='', alphabet=None, case_shift=None):
295
- if s is None: # called as an instance method
296
- s = str(cls)
298
+ @_class_or_instance_method
299
+ def sumcheck(cls, self, s=None, hash=_UNSET, alphabet=_UNSET, case_shift=_UNSET):
300
+ """True if the checksum of s is valid.
301
+
302
+ IDstring.sumcheck('TESTME2K') checks a string using the default (or supplied) hash, alphabet and case_shift.
303
+ my_id.sumcheck() checks my_id itself, using its own hash, alphabet and case_shift
304
+ (unless they are supplied as arguments).
305
+ """
306
+ if s is None and self is not None: # called as an instance method: check this instance
307
+ s = str(self)
308
+ hash = self.hash if hash is _UNSET else hash
309
+ alphabet = self.alphabet if alphabet is _UNSET else alphabet
310
+ case_shift = self.case_shift if case_shift is _UNSET else case_shift
311
+ if s is None:
312
+ raise TypeError('sumcheck() needs a string to check when called on the class')
313
+ hash = '' if hash is _UNSET else hash
314
+ alphabet = None if alphabet is _UNSET else alphabet
315
+ case_shift = None if case_shift is _UNSET else case_shift
297
316
  return _sumcheck(s, hash, alphabet, case_shift)
298
317
 
299
318
 
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: idstring
3
- Version: 2.1.4
3
+ Version: 2.1.5
4
4
  Summary: A Python package to create unique, checksummed serial-number strings.
5
5
  Author: Vernon Cole
6
6
  Author-email: vernondcole@gmail.com
@@ -15,6 +15,7 @@ Classifier: Natural Language :: English
15
15
  Classifier: Operating System :: OS Independent
16
16
  Classifier: Topic :: Software Development
17
17
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Requires-Python: >=3.11
18
19
  Description-Content-Type: text/markdown
19
20
 
20
21
  idstring
@@ -51,6 +52,12 @@ class IDstring generates (and checks) alphanumeric serial number strings with a
51
52
  * The wonky 5-bit binary conversion functions, which only worked with the default alphabet, have been removed.
52
53
  * Version 2.1:
53
54
  * adds a .context dictionary to the IDstring object which a seedstore method can use to store its operating context.
55
+ * Version 2.1.5:
56
+ * Requires Python 3.11 or newer. (Users of older Pythons will continue to get version 2.1.4.)
57
+ * Bug fixes: the .value property now returns the string; IDstring(existing_id) copies the host of an id which has one;
58
+ my_id.sumcheck() checks the instance using its own hash, alphabet and case_shift;
59
+ each new IDstring factory gets its own .context dictionary (they had been sharing one).
60
+ * Tests now run automatically on GitHub Actions for every pull request.
54
61
 
55
62
  ### operation:
56
63
  IDstring extends the built-in str class, using an __ADD__ method which accepts the integer ONE.
@@ -63,7 +70,7 @@ So, in short, you can generate serial numbers like this:
63
70
  serial += 1 # Increments the string & generates a new checksum.
64
71
  print(serial)
65
72
 
66
- Gives a result series which starts with "1Y3R9CB" and loops 925,386 times, ending with:
73
+ Starting from "1Y3R9CB" (the seed plus its check digit), the loop runs 925,387 times, ending with:
67
74
  ```
68
75
  1YYYYVA
69
76
  1YYYYW8
@@ -79,7 +86,7 @@ The actual code for a new leading digit is: "carry_digit = '1' if self.alphabet[
79
86
  The "Luhn mod N" algorithm is used (unless disabled) to add another digit (from the same alphabet) as a check digit.
80
87
 
81
88
  The routine will automatically skip over patterns which contain substrings matching a list of "Dirty Words."
82
- The above example should have been expected to produce 95396 results, but skipped 10 objectionable values.
89
+ Without that filter, the above example would have produced 925,396 results; the filter skipped 9 objectionable values.
83
90
 
84
91
  You can override the supplied alphabet and objectionable word lists. Alternate alphabets overflow correctly.
85
92
 
@@ -3,6 +3,7 @@ name = 'idstring'
3
3
  description = 'A Python package to create unique, checksummed serial-number strings.'
4
4
  authors = [{name = 'Vernon Cole'},{email = "vernondcole@gmail.com"}]
5
5
  dependencies = []
6
+ requires-python = ">=3.11"
6
7
  dynamic = ['version'] # set from __version__ in idstring.py
7
8
  keywords = ["serial number", "string", "checksum", "Luhn", "increment"]
8
9
  classifiers = [
@@ -255,5 +255,63 @@ class Test10(unittest.TestCase):
255
255
  assertion(f, 'aaaaa')
256
256
 
257
257
 
258
+ class Test12(unittest.TestCase):
259
+ # regression tests for version 2.1.5 fixes
260
+
261
+ def test12a_value(self):
262
+ # the .value property returns the string value
263
+ f = IDstring(seed='90A', host='1234')
264
+ self.assertEqual(f.value, '90A1234A')
265
+ self.assertIs(type(f.value), str)
266
+
267
+ def test12b_clone_inherits_host(self):
268
+ # cloning an IDstring which has a host should not require repeating the host
269
+ f = IDstring(seed='90A', host='1234')
270
+ g = IDstring(f)
271
+ assertion(g, '90A1234A')
272
+ self.assertEqual(g.host, '1234')
273
+ self.assertEqual(g.get_seed(), '90A')
274
+ assertion(g + 1, '90B12348')
275
+ # supplying the same host is still fine
276
+ assertion(IDstring(f, host='1234'), '90A1234A')
277
+
278
+ def test12c_clone_wrong_host(self):
279
+ f = IDstring(seed='90A', host='1234')
280
+ with self.assertRaises(InvalidIdError) as cm:
281
+ IDstring(f, host='5678')
282
+ self.assertIn('90A1234A', str(cm.exception)) # message names the id (not "None")
283
+
284
+ def test12d_instance_sumcheck(self):
285
+ # sumcheck() called on an instance checks that instance, using its own settings
286
+ self.assertTrue(IDstring(seed='90A', host='1234').sumcheck())
287
+ self.assertTrue(IDstring(seed='90A', host='1234', hash='0').sumcheck())
288
+ self.assertTrue(IDstring(seed='abc', alphabet='abcd', case_shift=str.lower).sumcheck())
289
+ self.assertTrue(IDstring(seed='5GA', hash=None).sumcheck())
290
+ # an explicitly supplied argument still overrides the instance's setting
291
+ self.assertFalse(IDstring(seed='90A', hash='0').sumcheck(hash='XX'))
292
+ # an instance can still be used to check some other string
293
+ self.assertTrue(IDstring(seed='90A').sumcheck('TESTME2K'))
294
+ # calling on the class without a string is an error
295
+ self.assertRaises(TypeError, IDstring.sumcheck)
296
+
297
+ def test12e_context_not_shared(self):
298
+ # each new factory gets its own context dictionary
299
+ a = IDstring(seed='AAA')
300
+ b = IDstring(seed='BBB')
301
+ a.context['x'] = 1
302
+ self.assertEqual(b.context, {})
303
+ # but the context follows a factory as it is incremented
304
+ a2 = a + 1
305
+ self.assertIs(a2.context, a.context)
306
+ # and a supplied context is used as given
307
+ mine = {'conn': None}
308
+ self.assertIs(IDstring(seed='CCC', context=mine).context, mine)
309
+
310
+ def test12f_bad_seed_type(self):
311
+ with self.assertRaises(InvalidIdError) as cm:
312
+ IDstring(seed=123)
313
+ self.assertIn('Invalid seed', str(cm.exception))
314
+
315
+
258
316
  if __name__ == "__main__":
259
317
  unittest.main()
File without changes
File without changes
File without changes