Generate codes
This commit is contained in:
parent
701ef6db2e
commit
4309a79a5d
|
@ -0,0 +1,56 @@
|
||||||
|
import logging
|
||||||
|
import random
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from .database import get_db
|
||||||
|
|
||||||
|
|
||||||
|
ALPHABET = '0123456789bcdefghjkmnpqrstuvwxyz'
|
||||||
|
assert len(ALPHABET) == 32
|
||||||
|
|
||||||
|
LENGTH = 4
|
||||||
|
|
||||||
|
|
||||||
|
def list_errors(code):
|
||||||
|
for place in range(LENGTH):
|
||||||
|
for replacement in ALPHABET:
|
||||||
|
if replacement == code[place]:
|
||||||
|
continue
|
||||||
|
new_code = code[:place] + replacement + code[place + 1:]
|
||||||
|
yield new_code
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
if len(sys.argv) <= 1:
|
||||||
|
number = 1
|
||||||
|
elif len(sys.argv) == 2:
|
||||||
|
number = int(sys.argv[1], 10)
|
||||||
|
else:
|
||||||
|
raise ValueError
|
||||||
|
|
||||||
|
# Get the current codes
|
||||||
|
with get_db() as db:
|
||||||
|
correct_codes = set(row[0] for row in db.execute(
|
||||||
|
'''\
|
||||||
|
SELECT code FROM families;
|
||||||
|
''',
|
||||||
|
))
|
||||||
|
|
||||||
|
# Augment them with possible errored codes
|
||||||
|
errored_codes = set()
|
||||||
|
for code in correct_codes:
|
||||||
|
errored_codes.update(list_errors(code))
|
||||||
|
assert len(errored_codes) == LENGTH * (len(ALPHABET) - 1) * len(correct_codes)
|
||||||
|
|
||||||
|
# Generate new codes
|
||||||
|
generated = 0
|
||||||
|
while generated < number:
|
||||||
|
code = ''.join(random.choice(ALPHABET) for _ in range(LENGTH))
|
||||||
|
if code in correct_codes or code in errored_codes:
|
||||||
|
continue
|
||||||
|
correct_codes.add(code)
|
||||||
|
errored_codes.update(list_errors(code))
|
||||||
|
print(code)
|
||||||
|
generated += 1
|
|
@ -15,6 +15,9 @@ uwsgi = ["uWSGI"]
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
|
|
||||||
|
[tool.poetry.scripts]
|
||||||
|
generate-codes = "france_rsvp.codes:main"
|
||||||
|
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["poetry-core>=1.0.0"]
|
requires = ["poetry-core>=1.0.0"]
|
||||||
build-backend = "poetry.core.masonry.api"
|
build-backend = "poetry.core.masonry.api"
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
INSERT INTO families(code, name) VALUES
|
INSERT INTO families(code, name) VALUES
|
||||||
('aaaa', 'one'),
|
('bbbb', 'one'),
|
||||||
('bbbb', 'two'),
|
('cccc', 'two'),
|
||||||
('cccc', 'three');
|
('dddd', 'three');
|
||||||
|
|
||||||
INSERT INTO replies(family_code, date, adults, children) VALUES
|
INSERT INTO replies(family_code, date, adults, children) VALUES
|
||||||
('aaaa', '2022-05-10 20:37:39', 2, 0),
|
('bbbb', '2022-05-10 20:37:39', 2, 0),
|
||||||
('aaaa', '2022-05-10 23:55:55', 0, 0),
|
('bbbb', '2022-05-10 23:55:55', 0, 0),
|
||||||
('bbbb', '2022-05-10 20:22:11', 2, 1);
|
('cccc', '2022-05-10 20:22:11', 2, 1);
|
||||||
|
|
Loading…
Reference in New Issue