idstring 2.0.3__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.
- idstring-2.1.5/PKG-INFO +117 -0
- {idstring-2.0.3 → idstring-2.1.5}/README.md +35 -10
- idstring-2.1.5/idstring/__init__.py +3 -0
- {idstring-2.0.3 → idstring-2.1.5/idstring}/idstring.py +73 -44
- idstring-2.1.5/idstring.egg-info/PKG-INFO +117 -0
- {idstring-2.0.3 → idstring-2.1.5}/idstring.egg-info/SOURCES.txt +5 -4
- idstring-2.1.5/pyproject.toml +30 -0
- idstring-2.1.5/setup.cfg +4 -0
- idstring-2.1.5/setup.py +4 -0
- idstring-2.1.5/test/test_idstring.py +317 -0
- idstring-2.0.3/MANIFEST.in +0 -2
- idstring-2.0.3/PKG-INFO +0 -32
- idstring-2.0.3/idstring.egg-info/PKG-INFO +0 -32
- idstring-2.0.3/setup.cfg +0 -10
- idstring-2.0.3/setup.py +0 -54
- {idstring-2.0.3 → idstring-2.1.5}/idstring.egg-info/dependency_links.txt +0 -0
- {idstring-2.0.3 → idstring-2.1.5}/idstring.egg-info/top_level.txt +0 -0
idstring-2.1.5/PKG-INFO
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: idstring
|
|
3
|
+
Version: 2.1.5
|
|
4
|
+
Summary: A Python package to create unique, checksummed serial-number strings.
|
|
5
|
+
Author: Vernon Cole
|
|
6
|
+
Author-email: vernondcole@gmail.com
|
|
7
|
+
Project-URL: repository, https://github.com/vernondcole/idstring
|
|
8
|
+
Project-URL: documentation, https://github.com/vernondcole/idstring/wiki
|
|
9
|
+
Keywords: serial number,string,checksum,Luhn,increment
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: GNU General Public License (GPL)
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Natural Language :: English
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Topic :: Software Development
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Requires-Python: >=3.11
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
idstring
|
|
22
|
+
========
|
|
23
|
+
|
|
24
|
+
Python module to create alpha-numeric serial number strings with a check digit, with smart incrementing.
|
|
25
|
+
|
|
26
|
+
an IDstring is a string-like object which can be incremented, to produce
|
|
27
|
+
a series of serial-number-like strings, consisting of Arabic digits and upper case letters,
|
|
28
|
+
skipping those which can be easily misinterpreted ("oh", "I", "Q", and Zed) and which has
|
|
29
|
+
(by defaut) an optional Luhn-N check digit, capable of detecting any single-letter error and most other errors.
|
|
30
|
+
|
|
31
|
+
Programmers may define alternate alphabets, alternate lists of "dirty" words (which are skipped),
|
|
32
|
+
a fixed subfield for generating serial number from multiple sources, and a hash code to create unique check
|
|
33
|
+
digits algorithms for multiple projects. Passing a hash code of None will surpress check digit operation.
|
|
34
|
+
|
|
35
|
+
For more explanation, please refer to [the wiki on github](https://github.com/vernondcole/idstring/wiki)
|
|
36
|
+
|
|
37
|
+
class IDstring generates (and checks) alphanumeric serial number strings with a checksum
|
|
38
|
+
|
|
39
|
+
### changes:
|
|
40
|
+
|
|
41
|
+
* 2018 Update: Still works fine in Python 3.6. No issues have been filed, no bugs reported.
|
|
42
|
+
|
|
43
|
+
* 2022 Update: works with Python 3.10. Python 2.7 dropped. Version 2.0.1
|
|
44
|
+
|
|
45
|
+
* Version 2.0:
|
|
46
|
+
* Added ability to pass hash=None to turn off checksums.
|
|
47
|
+
* Support for lower-case letters in alphabets.
|
|
48
|
+
* Assigning alternate alphabets (etc.) during instance creation.
|
|
49
|
+
* Choice of automatic case shift upper, lower, or None.
|
|
50
|
+
* Some small breaking changes. Especially . . .
|
|
51
|
+
* The use of IdString(seed='sss') syntax is required in usual cases when manually creating a new instance.
|
|
52
|
+
* The wonky 5-bit binary conversion functions, which only worked with the default alphabet, have been removed.
|
|
53
|
+
* Version 2.1:
|
|
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.
|
|
61
|
+
|
|
62
|
+
### operation:
|
|
63
|
+
IDstring extends the built-in str class, using an __ADD__ method which accepts the integer ONE.
|
|
64
|
+
Adding 1 to an instance of the class will generate a new instance with an incremented value (and a new checksum).
|
|
65
|
+
|
|
66
|
+
So, in short, you can generate serial numbers like this:
|
|
67
|
+
|
|
68
|
+
serial = idstring.IDstring(seed='1Y3R9C')
|
|
69
|
+
while serial.startswith('1'): # notice that it is like a string, so .startswith works.
|
|
70
|
+
serial += 1 # Increments the string & generates a new checksum.
|
|
71
|
+
print(serial)
|
|
72
|
+
|
|
73
|
+
Starting from "1Y3R9CB" (the seed plus its check digit), the loop runs 925,387 times, ending with:
|
|
74
|
+
```
|
|
75
|
+
1YYYYVA
|
|
76
|
+
1YYYYW8
|
|
77
|
+
1YYYYX6
|
|
78
|
+
1YYYYY4
|
|
79
|
+
200000X
|
|
80
|
+
```
|
|
81
|
+
Incrementation flows smoothly through the alphabet -- "9" increments to "A". Incrementing "Y" gives "0" and
|
|
82
|
+
causes a carry to increment the next digit. If you run out of digits, a "1" is prepended to the string.
|
|
83
|
+
(By default. Alternate alphabets increment in their implied order.
|
|
84
|
+
The actual code for a new leading digit is: "carry_digit = '1' if self.alphabet[0] == '0' else self.alphabet[0]")
|
|
85
|
+
|
|
86
|
+
The "Luhn mod N" algorithm is used (unless disabled) to add another digit (from the same alphabet) as a check digit.
|
|
87
|
+
|
|
88
|
+
The routine will automatically skip over patterns which contain substrings matching a list of "Dirty Words."
|
|
89
|
+
Without that filter, the above example would have produced 925,396 results; the filter skipped 9 objectionable values.
|
|
90
|
+
|
|
91
|
+
You can override the supplied alphabet and objectionable word lists. Alternate alphabets overflow correctly.
|
|
92
|
+
|
|
93
|
+
You may supply an optional call-back function to assist in maintaining the persistence of the serial number
|
|
94
|
+
sequence of multiple program runs.
|
|
95
|
+
|
|
96
|
+
The Luhn-N check digit will detect any single character substitution, any inversion of adjacent characters
|
|
97
|
+
(except the first and last character in the alphabet: i.e. 0<->Y for the supplied default) and most other errors.
|
|
98
|
+
|
|
99
|
+
The default alphabet consists of [0-9] and [A-Y] dropping "I", "O", and "Q".
|
|
100
|
+
|
|
101
|
+
Provision is made for a fixed subfield (of user defined length) between the check digit and the incrementing
|
|
102
|
+
field, so that multiple sites can produce non-overlapping series of numbers. This subfield and the check digit
|
|
103
|
+
are included in the "dirty word" checking.
|
|
104
|
+
|
|
105
|
+
### still don't understand?
|
|
106
|
+
Read the source, Luke.
|
|
107
|
+
https://pypi.org/project/idstring/#files
|
|
108
|
+
|
|
109
|
+
Read (and then test run) the program sample_seedstore.py to see how it works.
|
|
110
|
+
|
|
111
|
+
Run "python sample_seedstore.py" from multiple terminals to observe how it uses
|
|
112
|
+
a one-row SQL database to maintain the seed value and gives unique values.
|
|
113
|
+
|
|
114
|
+
### installation:
|
|
115
|
+
Installation uses the usual Python methods:
|
|
116
|
+
|
|
117
|
+
pip install idstring
|
|
@@ -3,10 +3,21 @@ idstring
|
|
|
3
3
|
|
|
4
4
|
Python module to create alpha-numeric serial number strings with a check digit, with smart incrementing.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
an IDstring is a string-like object which can be incremented, to produce
|
|
7
|
+
a series of serial-number-like strings, consisting of Arabic digits and upper case letters,
|
|
8
|
+
skipping those which can be easily misinterpreted ("oh", "I", "Q", and Zed) and which has
|
|
9
|
+
(by defaut) an optional Luhn-N check digit, capable of detecting any single-letter error and most other errors.
|
|
10
|
+
|
|
11
|
+
Programmers may define alternate alphabets, alternate lists of "dirty" words (which are skipped),
|
|
12
|
+
a fixed subfield for generating serial number from multiple sources, and a hash code to create unique check
|
|
13
|
+
digits algorithms for multiple projects. Passing a hash code of None will surpress check digit operation.
|
|
14
|
+
|
|
15
|
+
For more explanation, please refer to [the wiki on github](https://github.com/vernondcole/idstring/wiki)
|
|
7
16
|
|
|
8
17
|
class IDstring generates (and checks) alphanumeric serial number strings with a checksum
|
|
9
18
|
|
|
19
|
+
### changes:
|
|
20
|
+
|
|
10
21
|
* 2018 Update: Still works fine in Python 3.6. No issues have been filed, no bugs reported.
|
|
11
22
|
|
|
12
23
|
* 2022 Update: works with Python 3.10. Python 2.7 dropped. Version 2.0.1
|
|
@@ -19,8 +30,17 @@ class IDstring generates (and checks) alphanumeric serial number strings with a
|
|
|
19
30
|
* Some small breaking changes. Especially . . .
|
|
20
31
|
* The use of IdString(seed='sss') syntax is required in usual cases when manually creating a new instance.
|
|
21
32
|
* The wonky 5-bit binary conversion functions, which only worked with the default alphabet, have been removed.
|
|
22
|
-
|
|
23
|
-
|
|
33
|
+
* Version 2.1:
|
|
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.
|
|
41
|
+
|
|
42
|
+
### operation:
|
|
43
|
+
IDstring extends the built-in str class, using an __ADD__ method which accepts the integer ONE.
|
|
24
44
|
Adding 1 to an instance of the class will generate a new instance with an incremented value (and a new checksum).
|
|
25
45
|
|
|
26
46
|
So, in short, you can generate serial numbers like this:
|
|
@@ -30,7 +50,7 @@ So, in short, you can generate serial numbers like this:
|
|
|
30
50
|
serial += 1 # Increments the string & generates a new checksum.
|
|
31
51
|
print(serial)
|
|
32
52
|
|
|
33
|
-
|
|
53
|
+
Starting from "1Y3R9CB" (the seed plus its check digit), the loop runs 925,387 times, ending with:
|
|
34
54
|
```
|
|
35
55
|
1YYYYVA
|
|
36
56
|
1YYYYW8
|
|
@@ -46,7 +66,7 @@ The actual code for a new leading digit is: "carry_digit = '1' if self.alphabet[
|
|
|
46
66
|
The "Luhn mod N" algorithm is used (unless disabled) to add another digit (from the same alphabet) as a check digit.
|
|
47
67
|
|
|
48
68
|
The routine will automatically skip over patterns which contain substrings matching a list of "Dirty Words."
|
|
49
|
-
|
|
69
|
+
Without that filter, the above example would have produced 925,396 results; the filter skipped 9 objectionable values.
|
|
50
70
|
|
|
51
71
|
You can override the supplied alphabet and objectionable word lists. Alternate alphabets overflow correctly.
|
|
52
72
|
|
|
@@ -62,11 +82,16 @@ Provision is made for a fixed subfield (of user defined length) between the chec
|
|
|
62
82
|
field, so that multiple sites can produce non-overlapping series of numbers. This subfield and the check digit
|
|
63
83
|
are included in the "dirty word" checking.
|
|
64
84
|
|
|
85
|
+
### still don't understand?
|
|
86
|
+
Read the source, Luke.
|
|
87
|
+
https://pypi.org/project/idstring/#files
|
|
88
|
+
|
|
89
|
+
Read (and then test run) the program sample_seedstore.py to see how it works.
|
|
90
|
+
|
|
91
|
+
Run "python sample_seedstore.py" from multiple terminals to observe how it uses
|
|
92
|
+
a one-row SQL database to maintain the seed value and gives unique values.
|
|
93
|
+
|
|
94
|
+
### installation:
|
|
65
95
|
Installation uses the usual Python methods:
|
|
66
96
|
|
|
67
|
-
cd your-unzipped-directory
|
|
68
|
-
python setup.py install
|
|
69
|
-
|
|
70
|
-
or
|
|
71
|
-
|
|
72
97
|
pip install idstring
|
|
@@ -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.
|
|
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.0.3"
|
|
|
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.
|
|
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()
|
|
@@ -61,10 +62,6 @@ __version__ = "2.0.3"
|
|
|
61
62
|
#-
|
|
62
63
|
#-----------------------------------------------------------------------
|
|
63
64
|
|
|
64
|
-
#Define exceptions
|
|
65
|
-
import idstring
|
|
66
|
-
|
|
67
|
-
|
|
68
65
|
class IdStringError(ValueError):
|
|
69
66
|
"""Base class for all errors in the IdString package"""
|
|
70
67
|
pass
|
|
@@ -79,12 +76,26 @@ def noshift(s):
|
|
|
79
76
|
|
|
80
77
|
DEFAULT_CASE_SHIFT = str.upper
|
|
81
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
|
+
|
|
82
93
|
# define the characters which may legally appear in an idString
|
|
83
94
|
DEFAULT_ALPHABET = "0123456789ABCDEFGHJKLMNPRSTUVWXY"
|
|
84
95
|
|
|
85
96
|
# create a list of George Carlan's "the seven words you can't say on T.V."
|
|
86
97
|
# plus a few extras, but ignoring those with an "I" in them, since the default ALPHABET has no "I"
|
|
87
|
-
DEFAULT_DIRTY_WORDS = [c[::-1] for c in ['KCUF', 'TNUC', 'TRAF', 'DRUT', 'TAWT', 'SLLAB']]
|
|
98
|
+
DEFAULT_DIRTY_WORDS = [c[::-1] for c in ['KCUF', 'TNUC', 'TRAF', 'DRUT', 'TAWT', 'SLLAB', 'SSA']]
|
|
88
99
|
DIRTY_I_WORDS = [c[::-1] for c in ['TIHS', 'TIT', 'SSIP']] # if your alternate alphabet contains an "I"
|
|
89
100
|
# alter your list like--> IDstring.DIRTY_WORDS = idstring.DEFAULT_DIRTY_WORDS + idstring.DIRTY_I_WORDS
|
|
90
101
|
# the words are stored backwards in the source, because 'kcuf' is not objectionable.
|
|
@@ -112,28 +123,17 @@ class IDstring(str):
|
|
|
112
123
|
raise NotImplementedError('This method has been deprecated.')
|
|
113
124
|
|
|
114
125
|
|
|
115
|
-
def _check_host(self, host):
|
|
116
|
-
try:
|
|
117
|
-
h = self.host
|
|
118
|
-
if len(host) != len(h):
|
|
119
|
-
raise InvalidIdError(f'New host examples must be same length as defined host field "{h}"')
|
|
120
|
-
case_shift = self.case_shift
|
|
121
|
-
d = case_shift(str(host).strip())
|
|
122
|
-
for c in d:
|
|
123
|
-
if not c in self.alphabet:
|
|
124
|
-
raise InvalidIdError('Incorrect character "%s" in "%s"' % (c, d))
|
|
125
|
-
return d
|
|
126
|
-
except AttributeError:
|
|
127
|
-
return host
|
|
128
|
-
|
|
129
126
|
@staticmethod
|
|
130
127
|
def __new__(cls, idstr=None, seed=None, host='', seedstore=None, hash='', alphabet=None,
|
|
131
|
-
case_shift=DEFAULT_CASE_SHIFT, no_check=False):
|
|
128
|
+
case_shift=DEFAULT_CASE_SHIFT, no_check=False, context=None):
|
|
132
129
|
"""
|
|
133
130
|
:S - an existing legal idString, or None
|
|
134
131
|
:seed - the seed string for a new factory [ignored unless S is None]
|
|
135
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)
|
|
136
134
|
:seedstore - a seed preservation function for the new factory
|
|
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)
|
|
137
137
|
:hash - an additional string to alter the calculation of the check digit for diverse projects
|
|
138
138
|
pass hash=None to turn off checksum testing and creation. Makes this module dumb.
|
|
139
139
|
:case_shift - function to apply to input strings. one of str.upper str.lower or None
|
|
@@ -146,18 +146,22 @@ class IDstring(str):
|
|
|
146
146
|
if host:
|
|
147
147
|
host = case_shift(host)
|
|
148
148
|
except TypeError:
|
|
149
|
-
|
|
149
|
+
raise InvalidIdError(f'Invalid seed "{seed}" or host "{host}" argument') from None
|
|
150
150
|
alphabet = alphabet or IDstring.ALPHABET
|
|
151
151
|
value = None
|
|
152
152
|
if isinstance(idstr, cls):
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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}")')
|
|
157
|
+
if seed is None: # making a clone
|
|
158
|
+
value = str(idstr)
|
|
159
|
+
seed = idstr._seed
|
|
157
160
|
hash = idstr.hash
|
|
158
161
|
alphabet = idstr.alphabet
|
|
159
162
|
seedstore = idstr.seedstore
|
|
160
163
|
case_shift = idstr.case_shift
|
|
164
|
+
context = idstr.context
|
|
161
165
|
elif isinstance(idstr, str):
|
|
162
166
|
us = case_shift(idstr) # if passing a string as on IDstring, it must already have a checksum
|
|
163
167
|
if no_check:
|
|
@@ -175,7 +179,7 @@ class IDstring(str):
|
|
|
175
179
|
raise InvalidIdError(f'No valid ID in id="{idstr}" or seed="{seed}"')
|
|
176
180
|
value = case_shift(value)
|
|
177
181
|
new = super().__new__(cls, value) #create the new instance
|
|
178
|
-
new.
|
|
182
|
+
new._seed = seed # fill in the new instances attributes (as if we were __init__)
|
|
179
183
|
new.host = host
|
|
180
184
|
new.hash = hash
|
|
181
185
|
new.alphabet = alphabet
|
|
@@ -183,24 +187,32 @@ class IDstring(str):
|
|
|
183
187
|
if not isinstance(seedstore, Callable):
|
|
184
188
|
raise IdStringError ('seedstore "%s" is not Callable' % repr(seedstore))
|
|
185
189
|
new.seedstore = seedstore
|
|
190
|
+
new.context = {} if context is None else context
|
|
186
191
|
new.case_shift = case_shift
|
|
187
192
|
return new
|
|
188
193
|
|
|
189
194
|
|
|
195
|
+
@property
|
|
196
|
+
def seed(self):
|
|
197
|
+
return self.get_seed()
|
|
198
|
+
|
|
199
|
+
|
|
190
200
|
def get_seed(self):
|
|
191
201
|
"""extracts the incrementable part (without the host and checksum digits) of the IDstring"""
|
|
192
202
|
try:
|
|
193
|
-
|
|
203
|
+
stored_seed = self._seed
|
|
194
204
|
except AttributeError:
|
|
195
|
-
|
|
196
|
-
if
|
|
205
|
+
stored_seed = None
|
|
206
|
+
if stored_seed is None:
|
|
197
207
|
inc = 0 if self.hash is None else 1
|
|
198
208
|
try:
|
|
199
209
|
n = len(self.host) + inc
|
|
200
210
|
except TypeError:
|
|
201
211
|
n = inc
|
|
202
212
|
ret = str(self)[:-n] if n > 0 else str(self)
|
|
203
|
-
|
|
213
|
+
self._seed = ret # cache for next time
|
|
214
|
+
return ret
|
|
215
|
+
return stored_seed
|
|
204
216
|
|
|
205
217
|
|
|
206
218
|
def _next_value(self):
|
|
@@ -223,7 +235,7 @@ class IDstring(str):
|
|
|
223
235
|
cat = carry_digit + cat # add a new place
|
|
224
236
|
keep_looping = False
|
|
225
237
|
seed = seed[:n] + cat
|
|
226
|
-
self.
|
|
238
|
+
self._seed = seed
|
|
227
239
|
return _checksum(seed + self.host, self.hash, self.alphabet)
|
|
228
240
|
|
|
229
241
|
|
|
@@ -247,7 +259,7 @@ class IDstring(str):
|
|
|
247
259
|
next_value = _checksum(next_root, self.hash, self.alphabet)
|
|
248
260
|
else:
|
|
249
261
|
next_value = self._run_factory() # will this ever happen?
|
|
250
|
-
del self.
|
|
262
|
+
del self._seed # invalidate cached seed
|
|
251
263
|
return next_value
|
|
252
264
|
|
|
253
265
|
|
|
@@ -255,21 +267,24 @@ class IDstring(str):
|
|
|
255
267
|
"""supports 'IDstring + 1' to generate the next serial number. (all other addends just do str concat)
|
|
256
268
|
|
|
257
269
|
if idstring.host was defined, skip that many characters before incrementing
|
|
258
|
-
|
|
270
|
+
if seedstore() returns a value (other than None) that will become the new incremented value
|
|
259
271
|
"""
|
|
260
272
|
if other == 1:
|
|
261
273
|
next_thing = self._run_factory()
|
|
274
|
+
# Python strings are immutable, so we must create a new instance
|
|
262
275
|
ret = IDstring(next_thing, host=self.host, seedstore=self.seedstore, hash=self.hash,
|
|
263
|
-
case_shift=self.case_shift, alphabet=self.alphabet, no_check=True)
|
|
276
|
+
case_shift=self.case_shift, alphabet=self.alphabet, no_check=True, context=self.context)
|
|
264
277
|
if ret.seedstore: # call the seedstore function supplied by the program, with "self" as an argument
|
|
265
|
-
ret.seedstore(ret)
|
|
278
|
+
correction = ret.seedstore(ret)
|
|
279
|
+
if correction: # user can return a new, improved ID value
|
|
280
|
+
return correction
|
|
266
281
|
return ret
|
|
267
282
|
else:
|
|
268
283
|
return str(self) + other
|
|
269
284
|
|
|
270
285
|
@property
|
|
271
286
|
def value(self):
|
|
272
|
-
str(self)
|
|
287
|
+
return str(self)
|
|
273
288
|
|
|
274
289
|
def __repr__(self):
|
|
275
290
|
return "idString('%s')" % str(self) # reveal what's inside
|
|
@@ -280,10 +295,24 @@ class IDstring(str):
|
|
|
280
295
|
return _checksum(s, hash, alphabet)
|
|
281
296
|
|
|
282
297
|
|
|
283
|
-
@
|
|
284
|
-
def sumcheck(cls, s=None, hash=
|
|
285
|
-
if
|
|
286
|
-
|
|
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
|
|
287
316
|
return _sumcheck(s, hash, alphabet, case_shift)
|
|
288
317
|
|
|
289
318
|
|
|
@@ -346,8 +375,8 @@ def _sumcheck(s, hash='', alphabet=None, case_shift=None):
|
|
|
346
375
|
alphabet = IDstring.ALPHABET
|
|
347
376
|
if case_shift is None:
|
|
348
377
|
case_shift = DEFAULT_CASE_SHIFT
|
|
349
|
-
if hash is None: # checksums are not in use.
|
|
350
|
-
for c in case_shift(s):
|
|
378
|
+
if hash is None: # if checksums are not in use. . .
|
|
379
|
+
for c in case_shift(s): # just check for valid characters
|
|
351
380
|
if c not in alphabet:
|
|
352
381
|
return False
|
|
353
382
|
return True
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: idstring
|
|
3
|
+
Version: 2.1.5
|
|
4
|
+
Summary: A Python package to create unique, checksummed serial-number strings.
|
|
5
|
+
Author: Vernon Cole
|
|
6
|
+
Author-email: vernondcole@gmail.com
|
|
7
|
+
Project-URL: repository, https://github.com/vernondcole/idstring
|
|
8
|
+
Project-URL: documentation, https://github.com/vernondcole/idstring/wiki
|
|
9
|
+
Keywords: serial number,string,checksum,Luhn,increment
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: GNU General Public License (GPL)
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Natural Language :: English
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Topic :: Software Development
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Requires-Python: >=3.11
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
idstring
|
|
22
|
+
========
|
|
23
|
+
|
|
24
|
+
Python module to create alpha-numeric serial number strings with a check digit, with smart incrementing.
|
|
25
|
+
|
|
26
|
+
an IDstring is a string-like object which can be incremented, to produce
|
|
27
|
+
a series of serial-number-like strings, consisting of Arabic digits and upper case letters,
|
|
28
|
+
skipping those which can be easily misinterpreted ("oh", "I", "Q", and Zed) and which has
|
|
29
|
+
(by defaut) an optional Luhn-N check digit, capable of detecting any single-letter error and most other errors.
|
|
30
|
+
|
|
31
|
+
Programmers may define alternate alphabets, alternate lists of "dirty" words (which are skipped),
|
|
32
|
+
a fixed subfield for generating serial number from multiple sources, and a hash code to create unique check
|
|
33
|
+
digits algorithms for multiple projects. Passing a hash code of None will surpress check digit operation.
|
|
34
|
+
|
|
35
|
+
For more explanation, please refer to [the wiki on github](https://github.com/vernondcole/idstring/wiki)
|
|
36
|
+
|
|
37
|
+
class IDstring generates (and checks) alphanumeric serial number strings with a checksum
|
|
38
|
+
|
|
39
|
+
### changes:
|
|
40
|
+
|
|
41
|
+
* 2018 Update: Still works fine in Python 3.6. No issues have been filed, no bugs reported.
|
|
42
|
+
|
|
43
|
+
* 2022 Update: works with Python 3.10. Python 2.7 dropped. Version 2.0.1
|
|
44
|
+
|
|
45
|
+
* Version 2.0:
|
|
46
|
+
* Added ability to pass hash=None to turn off checksums.
|
|
47
|
+
* Support for lower-case letters in alphabets.
|
|
48
|
+
* Assigning alternate alphabets (etc.) during instance creation.
|
|
49
|
+
* Choice of automatic case shift upper, lower, or None.
|
|
50
|
+
* Some small breaking changes. Especially . . .
|
|
51
|
+
* The use of IdString(seed='sss') syntax is required in usual cases when manually creating a new instance.
|
|
52
|
+
* The wonky 5-bit binary conversion functions, which only worked with the default alphabet, have been removed.
|
|
53
|
+
* Version 2.1:
|
|
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.
|
|
61
|
+
|
|
62
|
+
### operation:
|
|
63
|
+
IDstring extends the built-in str class, using an __ADD__ method which accepts the integer ONE.
|
|
64
|
+
Adding 1 to an instance of the class will generate a new instance with an incremented value (and a new checksum).
|
|
65
|
+
|
|
66
|
+
So, in short, you can generate serial numbers like this:
|
|
67
|
+
|
|
68
|
+
serial = idstring.IDstring(seed='1Y3R9C')
|
|
69
|
+
while serial.startswith('1'): # notice that it is like a string, so .startswith works.
|
|
70
|
+
serial += 1 # Increments the string & generates a new checksum.
|
|
71
|
+
print(serial)
|
|
72
|
+
|
|
73
|
+
Starting from "1Y3R9CB" (the seed plus its check digit), the loop runs 925,387 times, ending with:
|
|
74
|
+
```
|
|
75
|
+
1YYYYVA
|
|
76
|
+
1YYYYW8
|
|
77
|
+
1YYYYX6
|
|
78
|
+
1YYYYY4
|
|
79
|
+
200000X
|
|
80
|
+
```
|
|
81
|
+
Incrementation flows smoothly through the alphabet -- "9" increments to "A". Incrementing "Y" gives "0" and
|
|
82
|
+
causes a carry to increment the next digit. If you run out of digits, a "1" is prepended to the string.
|
|
83
|
+
(By default. Alternate alphabets increment in their implied order.
|
|
84
|
+
The actual code for a new leading digit is: "carry_digit = '1' if self.alphabet[0] == '0' else self.alphabet[0]")
|
|
85
|
+
|
|
86
|
+
The "Luhn mod N" algorithm is used (unless disabled) to add another digit (from the same alphabet) as a check digit.
|
|
87
|
+
|
|
88
|
+
The routine will automatically skip over patterns which contain substrings matching a list of "Dirty Words."
|
|
89
|
+
Without that filter, the above example would have produced 925,396 results; the filter skipped 9 objectionable values.
|
|
90
|
+
|
|
91
|
+
You can override the supplied alphabet and objectionable word lists. Alternate alphabets overflow correctly.
|
|
92
|
+
|
|
93
|
+
You may supply an optional call-back function to assist in maintaining the persistence of the serial number
|
|
94
|
+
sequence of multiple program runs.
|
|
95
|
+
|
|
96
|
+
The Luhn-N check digit will detect any single character substitution, any inversion of adjacent characters
|
|
97
|
+
(except the first and last character in the alphabet: i.e. 0<->Y for the supplied default) and most other errors.
|
|
98
|
+
|
|
99
|
+
The default alphabet consists of [0-9] and [A-Y] dropping "I", "O", and "Q".
|
|
100
|
+
|
|
101
|
+
Provision is made for a fixed subfield (of user defined length) between the check digit and the incrementing
|
|
102
|
+
field, so that multiple sites can produce non-overlapping series of numbers. This subfield and the check digit
|
|
103
|
+
are included in the "dirty word" checking.
|
|
104
|
+
|
|
105
|
+
### still don't understand?
|
|
106
|
+
Read the source, Luke.
|
|
107
|
+
https://pypi.org/project/idstring/#files
|
|
108
|
+
|
|
109
|
+
Read (and then test run) the program sample_seedstore.py to see how it works.
|
|
110
|
+
|
|
111
|
+
Run "python sample_seedstore.py" from multiple terminals to observe how it uses
|
|
112
|
+
a one-row SQL database to maintain the seed value and gives unique values.
|
|
113
|
+
|
|
114
|
+
### installation:
|
|
115
|
+
Installation uses the usual Python methods:
|
|
116
|
+
|
|
117
|
+
pip install idstring
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
MANIFEST.in
|
|
2
1
|
README.md
|
|
3
|
-
|
|
4
|
-
setup.cfg
|
|
2
|
+
pyproject.toml
|
|
5
3
|
setup.py
|
|
4
|
+
idstring/__init__.py
|
|
5
|
+
idstring/idstring.py
|
|
6
6
|
idstring.egg-info/PKG-INFO
|
|
7
7
|
idstring.egg-info/SOURCES.txt
|
|
8
8
|
idstring.egg-info/dependency_links.txt
|
|
9
|
-
idstring.egg-info/top_level.txt
|
|
9
|
+
idstring.egg-info/top_level.txt
|
|
10
|
+
test/test_idstring.py
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = 'idstring'
|
|
3
|
+
description = 'A Python package to create unique, checksummed serial-number strings.'
|
|
4
|
+
authors = [{name = 'Vernon Cole'},{email = "vernondcole@gmail.com"}]
|
|
5
|
+
dependencies = []
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
dynamic = ['version'] # set from __version__ in idstring.py
|
|
8
|
+
keywords = ["serial number", "string", "checksum", "Luhn", "increment"]
|
|
9
|
+
classifiers = [
|
|
10
|
+
'Development Status :: 5 - Production/Stable',
|
|
11
|
+
'Intended Audience :: Developers',
|
|
12
|
+
'License :: OSI Approved :: GNU General Public License (GPL)',
|
|
13
|
+
'Programming Language :: Python',
|
|
14
|
+
'Natural Language :: English',
|
|
15
|
+
'Operating System :: OS Independent',
|
|
16
|
+
'Topic :: Software Development',
|
|
17
|
+
'Topic :: Software Development :: Libraries :: Python Modules'
|
|
18
|
+
]
|
|
19
|
+
readme = "README.md"
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
repository = "https://github.com/vernondcole/idstring"
|
|
23
|
+
documentation = "https://github.com/vernondcole/idstring/wiki"
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["setuptools", "wheel", "build"]
|
|
27
|
+
build-backend = "setuptools.build_meta"
|
|
28
|
+
|
|
29
|
+
[tool.setuptools.dynamic]
|
|
30
|
+
version = {attr = "idstring.__version__"}
|
idstring-2.1.5/setup.cfg
ADDED
idstring-2.1.5/setup.py
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Test code for the IDstring package
|
|
4
|
+
"""
|
|
5
|
+
__author__ = 'vernon'
|
|
6
|
+
|
|
7
|
+
import sys, os
|
|
8
|
+
mommy = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
|
|
9
|
+
sys.path.insert(1, mommy) # use the local copy, not some system version
|
|
10
|
+
|
|
11
|
+
import idstring
|
|
12
|
+
from idstring.idstring import IDstring, InvalidIdError
|
|
13
|
+
import unittest
|
|
14
|
+
import random
|
|
15
|
+
from contextlib import contextmanager
|
|
16
|
+
|
|
17
|
+
dummy_seed = None # Glabal variable used for ephemeral seed storage
|
|
18
|
+
|
|
19
|
+
def assertion(seen, expected, seed=None):
|
|
20
|
+
assert str(seen) == expected, 'Value is "%s". expected "%s"' % (seen, expected)
|
|
21
|
+
if seed:
|
|
22
|
+
assert dummy_seed == seed, 'seed returned was "%s" expected "%s"' % (seen, dummy_seed)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# the seedstore function will be called back with an IDstring object as its only argument
|
|
26
|
+
def dummy(idstr): # nonfunctional seedstore function
|
|
27
|
+
global dummy_seed
|
|
28
|
+
d = idstr.get_seed() # the seed we need to preserve
|
|
29
|
+
dummy_seed = d # just store it in a global variable for test purposes
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Test1(unittest.TestCase):
|
|
33
|
+
def test1(self):
|
|
34
|
+
#test building a factory
|
|
35
|
+
fact = IDstring(seed='90a', host='1234', seedstore=dummy)
|
|
36
|
+
#value comes back unchanged
|
|
37
|
+
assertion(fact, '90A1234A', None)
|
|
38
|
+
#try the next value
|
|
39
|
+
x = fact + 1
|
|
40
|
+
assertion(x, '90B12348', '90B')
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Test2(unittest.TestCase):
|
|
44
|
+
def test2(self):
|
|
45
|
+
#test the carry
|
|
46
|
+
fact = IDstring(seed='000Y', host='ppp', seedstore=dummy)
|
|
47
|
+
x = fact + 1
|
|
48
|
+
assertion(x, '0010PPP9', '0010')
|
|
49
|
+
def test2a(self):
|
|
50
|
+
#test carry out of top digit
|
|
51
|
+
fact = IDstring(seed='YYYY')
|
|
52
|
+
x = fact + 1
|
|
53
|
+
assertion(x, '10000X')
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class Test3(unittest.TestCase):
|
|
57
|
+
def test3(self):
|
|
58
|
+
# test the checksum interface
|
|
59
|
+
assert IDstring.sumcheck('TESTME2K'), 'valid checksum shown as invalid'
|
|
60
|
+
try:
|
|
61
|
+
assert IDstring('TESTME3K'), 'invalid checksum should not pass'
|
|
62
|
+
except InvalidIdError:
|
|
63
|
+
pass
|
|
64
|
+
else:
|
|
65
|
+
self.fail('Did not see OutOfRangeError')
|
|
66
|
+
|
|
67
|
+
def test3b(self):
|
|
68
|
+
assert not IDstring.sumcheck('TSETME2K'), 'invalid checksum should not pass'
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class Test4(unittest.TestCase):
|
|
72
|
+
# test the dirty word eliminator
|
|
73
|
+
|
|
74
|
+
def test4(self): #test that we skip bad words on the right
|
|
75
|
+
f = IDstring(seed='dcasr')
|
|
76
|
+
assertion(f, 'DCASRV')
|
|
77
|
+
x = f + 1
|
|
78
|
+
assertion(x, 'DCASTR') #skips the bad word 'ass'
|
|
79
|
+
|
|
80
|
+
def test4b(self):
|
|
81
|
+
#test that we skip bad words on the left
|
|
82
|
+
f = IDstring(seed='fucjyyy', seedstore=dummy)
|
|
83
|
+
x = f + 1 #skip you, too, buddy
|
|
84
|
+
assertion(x, 'FUCL000U', 'FUCL000')
|
|
85
|
+
|
|
86
|
+
def test4c(self): # test the case where a checksum causes a bad word
|
|
87
|
+
f = IDstring(seed='000vfub', seedstore=dummy)
|
|
88
|
+
x = f + 1 # '000VFUC' has a checksum of 'K'
|
|
89
|
+
assertion(x, '000VFUDH', '000VFUD')
|
|
90
|
+
|
|
91
|
+
def test4d(self): # test the case where a host field causes a bad word
|
|
92
|
+
f = IDstring(seed='0ct', host='nt', seedstore=dummy)
|
|
93
|
+
x = f + 1 #
|
|
94
|
+
assertion(x,'0CVNTG', '0CV')
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class Test5(unittest.TestCase):
|
|
98
|
+
# test the checksum algorithm as a whole
|
|
99
|
+
def test5(self):
|
|
100
|
+
alphabet = IDstring.ALPHABET
|
|
101
|
+
def ranlets(size,allowed=alphabet): # a function to generate a random string of our alphabet
|
|
102
|
+
randomstring = ''.join([allowed[random.randint(0, len(allowed) - 1)] for x in range(size)])
|
|
103
|
+
return randomstring
|
|
104
|
+
|
|
105
|
+
# run the test using many different random strings
|
|
106
|
+
for N in range(1000):
|
|
107
|
+
jumble = IDstring(seed=ranlets(random.randint(2, 20))) # make a random length random string
|
|
108
|
+
for i,should_be in enumerate(jumble): # for each character in that string
|
|
109
|
+
others = list(alphabet) # replace it with every other possible character
|
|
110
|
+
others.remove(should_be)
|
|
111
|
+
for c in others: #try every possible other value for this character
|
|
112
|
+
mumble = ''.join([jumble[:i], c, jumble[i+1:]])
|
|
113
|
+
assert not IDstring.sumcheck(mumble), 'IDstring "%s" should fail checksum'
|
|
114
|
+
# now test digit flips
|
|
115
|
+
# -- swap a character with its next door neighbor
|
|
116
|
+
# (this is the most common transcription error when humans are involved)
|
|
117
|
+
# Note: this algorithm will not detect flips of the first and last characters of its alphabet
|
|
118
|
+
# (called a 0-9 flip when only decimal digits are used)
|
|
119
|
+
#
|
|
120
|
+
for i in range(len(jumble)-1):
|
|
121
|
+
j = i+1 # skip test of letters are the same -- or if letters are first and last
|
|
122
|
+
if jumble[i] != jumble[j] and {jumble[i],jumble[j]} != {alphabet[0],alphabet[-1]}:
|
|
123
|
+
s = list(jumble)
|
|
124
|
+
s[j], s[i] = s[i], s[j] # swap two characters
|
|
125
|
+
mumble = ''.join(s)
|
|
126
|
+
assert not IDstring.sumcheck(mumble), 'checksum error in "%s" when changed to "%s"'%(jumble,mumble)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class Test6(unittest.TestCase):
|
|
130
|
+
# test weird extra functions
|
|
131
|
+
def test6(self):
|
|
132
|
+
# seed in binary is deprecated
|
|
133
|
+
self.assertRaises(idstring.InvalidIdError, IDstring, [], **{'seed': 0})
|
|
134
|
+
|
|
135
|
+
f = IDstring(seed='0')
|
|
136
|
+
assertion(f, '00')
|
|
137
|
+
x = f + 1
|
|
138
|
+
assertion(x, u'1X')
|
|
139
|
+
x = f + 1 # !! do not use this terrible side effect !!!
|
|
140
|
+
self.assertNotEqual(x, '2V')
|
|
141
|
+
|
|
142
|
+
x += 1
|
|
143
|
+
assertion(x, '2V')
|
|
144
|
+
self.assertRaises(ValueError, int, *[x], **{}) # deprecated function int(IDstring)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@contextmanager
|
|
148
|
+
def weird_alphabet(self, weird):
|
|
149
|
+
keep = IDstring.ALPHABET
|
|
150
|
+
IDstring.ALPHABET = weird
|
|
151
|
+
try:
|
|
152
|
+
yield None
|
|
153
|
+
finally:
|
|
154
|
+
IDstring.ALPHABET = keep
|
|
155
|
+
|
|
156
|
+
def test6b(self):
|
|
157
|
+
# change the class alphabet to hexadecimal
|
|
158
|
+
with self.weird_alphabet('0123456789ABCDEF'):
|
|
159
|
+
# put a checksum on a hexadecimal number
|
|
160
|
+
f = IDstring(seed='7FFF')
|
|
161
|
+
assertion(f, '7FFFC')
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class Test7(unittest.TestCase):
|
|
165
|
+
# Testing hash
|
|
166
|
+
def test7a(self):
|
|
167
|
+
#test building a factory with a variant hash
|
|
168
|
+
fact = IDstring(None, '90a', '1234', dummy, hash='0')
|
|
169
|
+
#value comes back unchanged
|
|
170
|
+
assertion(fact, '90A1234Y', None)
|
|
171
|
+
IDstring.sumcheck(fact, hash='0')
|
|
172
|
+
#try the next value
|
|
173
|
+
x = fact + 1
|
|
174
|
+
assertion(x, '90B1234X', '90B')
|
|
175
|
+
|
|
176
|
+
def test7b(self):
|
|
177
|
+
#test the carry
|
|
178
|
+
fact = IDstring(None, '000Y', 'ppp', dummy, '0')
|
|
179
|
+
x = fact + 1
|
|
180
|
+
assertion(x, '0010PPP2', '0010')
|
|
181
|
+
|
|
182
|
+
def test7c(self):
|
|
183
|
+
# test the checksum interface
|
|
184
|
+
assert IDstring.sumcheck('TESTME29', hash='0'), 'valid checksum shown as invalid'
|
|
185
|
+
try:
|
|
186
|
+
assert IDstring('TESTME39', hash='0'), 'invalid checksum should not pass'
|
|
187
|
+
except InvalidIdError:
|
|
188
|
+
pass
|
|
189
|
+
else:
|
|
190
|
+
self.fail('Did not see OutOfRangeError')
|
|
191
|
+
|
|
192
|
+
def test7d(self):
|
|
193
|
+
assert not IDstring.sumcheck('TESTME29'), 'invalid checksum should not pass (default hash)'
|
|
194
|
+
|
|
195
|
+
def test7e(self):
|
|
196
|
+
assert not IDstring.sumcheck('TESTME29', hash='XX'), 'invalid checksum should not pass (different hash)'
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class Test8(unittest.TestCase):
|
|
200
|
+
# Testing with no checksum (hash=None)
|
|
201
|
+
def test8a(self):
|
|
202
|
+
#test building a factory with a null hash
|
|
203
|
+
fact = IDstring(None, '5GA', '1234', dummy, None)
|
|
204
|
+
#value comes back unchanged
|
|
205
|
+
assertion(fact, '5GA1234', None)
|
|
206
|
+
IDstring.sumcheck(fact, hash=None)
|
|
207
|
+
#try the next value
|
|
208
|
+
x = fact + 1
|
|
209
|
+
assertion(x, '5GB1234', '5GB')
|
|
210
|
+
|
|
211
|
+
def test8b(self):
|
|
212
|
+
x = IDstring('ADD', seedstore=dummy, hash=None, alphabet='abcd', case_shift=str.lower)
|
|
213
|
+
x += 1
|
|
214
|
+
assertion(x, 'baa', 'baa')
|
|
215
|
+
|
|
216
|
+
x = IDstring(seed='DDDD', seedstore=dummy, hash=None, alphabet='ABCD')
|
|
217
|
+
x += 1
|
|
218
|
+
assertion(x, 'AAAAA')
|
|
219
|
+
|
|
220
|
+
self.assertFalse(IDstring.sumcheck('ABAD1', hash=None, alphabet='ABCD'))
|
|
221
|
+
|
|
222
|
+
class Test9(unittest.TestCase):
|
|
223
|
+
# testing that case_shift=None will not shift case
|
|
224
|
+
def test9(self):
|
|
225
|
+
x = IDstring(seed='AbC', seedstore=dummy, hash=None, alphabet='ABCabc', case_shift=None)
|
|
226
|
+
assertion(x, 'AbC')
|
|
227
|
+
x += 1
|
|
228
|
+
assertion(x, 'Aba')
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
class Test10(unittest.TestCase):
|
|
232
|
+
# Test the dirty word filter in more depth
|
|
233
|
+
@contextmanager
|
|
234
|
+
def weird_words(self, weird):
|
|
235
|
+
keep = IDstring.DIRTY_WORDS
|
|
236
|
+
IDstring.DIRTY_WORDS = weird
|
|
237
|
+
try:
|
|
238
|
+
yield None
|
|
239
|
+
finally:
|
|
240
|
+
IDstring.DIRTY_WORDS = keep
|
|
241
|
+
|
|
242
|
+
def test10(self):
|
|
243
|
+
# try changing the dirty word list
|
|
244
|
+
more_words = idstring.DEFAULT_DIRTY_WORDS + idstring.DIRTY_I_WORDS
|
|
245
|
+
with self.weird_words(more_words):
|
|
246
|
+
f = IDstring(seed='tBtist', alphabet='aBcit', case_shift=None, hash=None) # a short mixed case alphabet
|
|
247
|
+
f += 1 # carry should spell a three letter objectionabe word
|
|
248
|
+
assertion(f, 'tBtiaa') # The second "t" should be incremented to "a"
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def test11(self):
|
|
252
|
+
# test carry out of range with alternate alphabet
|
|
253
|
+
f = IDstring(seed='zzz', alphabet='abcz', case_shift=None)
|
|
254
|
+
f += 1
|
|
255
|
+
assertion(f, 'aaaaa')
|
|
256
|
+
|
|
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
|
+
|
|
316
|
+
if __name__ == "__main__":
|
|
317
|
+
unittest.main()
|
idstring-2.0.3/MANIFEST.in
DELETED
idstring-2.0.3/PKG-INFO
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: idstring
|
|
3
|
-
Version: 2.0.3
|
|
4
|
-
Summary: a class for compact alpha-numeric serial numbers with (an optional) check digit
|
|
5
|
-
Home-page: https://github.com/vernondcole/idstring
|
|
6
|
-
Author: Vernon Cole
|
|
7
|
-
Author-email: vernondcole@gmail.com
|
|
8
|
-
License: GPL
|
|
9
|
-
Keywords: serial number string checksum
|
|
10
|
-
Platform: UNKNOWN
|
|
11
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
-
Classifier: Intended Audience :: Developers
|
|
13
|
-
Classifier: License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
|
|
14
|
-
Classifier: Programming Language :: Python
|
|
15
|
-
Classifier: Programming Language :: Python :: 3
|
|
16
|
-
Classifier: Natural Language :: English
|
|
17
|
-
Classifier: Operating System :: OS Independent
|
|
18
|
-
Classifier: Topic :: Software Development
|
|
19
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
-
|
|
21
|
-
an IDstring is a string-like object which can be incremented, to produce
|
|
22
|
-
a series of serial-number-like strings, consisting of Arabic digits and upper case letters,
|
|
23
|
-
skipping those which can be easily misinterpreted ("oh", "I", "Q", and Zed) and which has
|
|
24
|
-
an optional Luhn-N check digit, capable of detecting any single-letter error and most other errors.
|
|
25
|
-
|
|
26
|
-
Programmers may define alternate alphabets, alternate lists of "dirty" words (which are skipped),
|
|
27
|
-
a fixed subfield for generating serial number from multiple sources, and a hash code to create unique check
|
|
28
|
-
digits algorithms for multiple projects. Passing a hash code of None will surpress check digit operation.
|
|
29
|
-
|
|
30
|
-
The class will also perform generic binary to alpha conversions in your alphabet (base-32 for the default).
|
|
31
|
-
|
|
32
|
-
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: idstring
|
|
3
|
-
Version: 2.0.3
|
|
4
|
-
Summary: a class for compact alpha-numeric serial numbers with (an optional) check digit
|
|
5
|
-
Home-page: https://github.com/vernondcole/idstring
|
|
6
|
-
Author: Vernon Cole
|
|
7
|
-
Author-email: vernondcole@gmail.com
|
|
8
|
-
License: GPL
|
|
9
|
-
Keywords: serial number string checksum
|
|
10
|
-
Platform: UNKNOWN
|
|
11
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
-
Classifier: Intended Audience :: Developers
|
|
13
|
-
Classifier: License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)
|
|
14
|
-
Classifier: Programming Language :: Python
|
|
15
|
-
Classifier: Programming Language :: Python :: 3
|
|
16
|
-
Classifier: Natural Language :: English
|
|
17
|
-
Classifier: Operating System :: OS Independent
|
|
18
|
-
Classifier: Topic :: Software Development
|
|
19
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
-
|
|
21
|
-
an IDstring is a string-like object which can be incremented, to produce
|
|
22
|
-
a series of serial-number-like strings, consisting of Arabic digits and upper case letters,
|
|
23
|
-
skipping those which can be easily misinterpreted ("oh", "I", "Q", and Zed) and which has
|
|
24
|
-
an optional Luhn-N check digit, capable of detecting any single-letter error and most other errors.
|
|
25
|
-
|
|
26
|
-
Programmers may define alternate alphabets, alternate lists of "dirty" words (which are skipped),
|
|
27
|
-
a fixed subfield for generating serial number from multiple sources, and a hash code to create unique check
|
|
28
|
-
digits algorithms for multiple projects. Passing a hash code of None will surpress check digit operation.
|
|
29
|
-
|
|
30
|
-
The class will also perform generic binary to alpha conversions in your alphabet (base-32 for the default).
|
|
31
|
-
|
|
32
|
-
|
idstring-2.0.3/setup.cfg
DELETED
idstring-2.0.3/setup.py
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
|
|
3
|
-
ld = """an IDstring is a string-like object which can be incremented, to produce
|
|
4
|
-
a series of serial-number-like strings, consisting of Arabic digits and upper case letters,
|
|
5
|
-
skipping those which can be easily misinterpreted ("oh", "I", "Q", and Zed) and which has
|
|
6
|
-
an optional Luhn-N check digit, capable of detecting any single-letter error and most other errors.
|
|
7
|
-
|
|
8
|
-
Programmers may define alternate alphabets, alternate lists of "dirty" words (which are skipped),
|
|
9
|
-
a fixed subfield for generating serial number from multiple sources, and a hash code to create unique check
|
|
10
|
-
digits algorithms for multiple projects. Passing a hash code of None will surpress check digit operation.
|
|
11
|
-
|
|
12
|
-
The class will also perform generic binary to alpha conversions in your alphabet (base-32 for the default).
|
|
13
|
-
"""
|
|
14
|
-
|
|
15
|
-
VERSION = None # in case searching for version fails
|
|
16
|
-
a = open("idstring.py") # find the version string in the source code
|
|
17
|
-
for line in a:
|
|
18
|
-
if "__version__" in line:
|
|
19
|
-
VERSION = line.split('"')[1]
|
|
20
|
-
print('idstring version="%s"' % VERSION)
|
|
21
|
-
break
|
|
22
|
-
a.close()
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def setup_package():
|
|
26
|
-
from setuptools import setup
|
|
27
|
-
from setuptools.command.build_py import build_py
|
|
28
|
-
|
|
29
|
-
setup(
|
|
30
|
-
cmdclass={"build_py": build_py},
|
|
31
|
-
name = 'idstring',
|
|
32
|
-
version = VERSION,
|
|
33
|
-
author = "Vernon Cole",
|
|
34
|
-
author_email = "vernondcole@gmail.com",
|
|
35
|
-
description = "a class for compact alpha-numeric serial numbers with (an optional) check digit",
|
|
36
|
-
long_description = ld,
|
|
37
|
-
license = "GPL",
|
|
38
|
-
keywords = "serial number string checksum",
|
|
39
|
-
classifiers = [
|
|
40
|
-
'Development Status :: 5 - Production/Stable',
|
|
41
|
-
'Intended Audience :: Developers',
|
|
42
|
-
'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
|
|
43
|
-
'Programming Language :: Python',
|
|
44
|
-
'Programming Language :: Python :: 3',
|
|
45
|
-
'Natural Language :: English',
|
|
46
|
-
'Operating System :: OS Independent',
|
|
47
|
-
'Topic :: Software Development',
|
|
48
|
-
'Topic :: Software Development :: Libraries :: Python Modules'],
|
|
49
|
-
url = 'https://github.com/vernondcole/idstring',
|
|
50
|
-
py_modules=["idstring"]
|
|
51
|
-
)
|
|
52
|
-
|
|
53
|
-
if __name__ == '__main__':
|
|
54
|
-
setup_package()
|
|
File without changes
|
|
File without changes
|