Skip to main content

Coupon codes algorithm

Project description

couponcodes

Implements the Coupon Codes Algorithm from Dan Mclean:

CouponCodes are designed to be distributed in printed form and typed into a web form. Features of the codes that make them well suited to manual transcription:

  • The codes are not case sensitive.
  • Not all letters and numbers are used, so if a person enters the letter 'O' we can automatically correct it to the digit '0' (similarly for I ⇨ 1, S ⇨ 5, Z ⇨ 2).
  • The 4th character of each part is a checkdigit, so client-side scripting can be used to highlight parts which have been mis-typed, before the code is even submitted to the application's back-end validation.
  • The checkdigit algorithm takes into account the position of the part being keyed. So for example '1K7Q' might be valid in the first part but not in the second so if a user typed the parts in the wrong boxes then their error could be highlighted.
  • The code generation algorithm avoids 'undesirable' codes. For example any code in which transposed characters happen to result in a valid checkdigit will be skipped. Any generated part which happens to spell an 'inappropriate' 4-letter word (e.g.: 'P00P') will also be skipped.
  • Codes can be generated and validated on the server

Meta

This python implementation is not written or endorsed by Dan Mclean.

PyPi page: https://pypi.org/project/cccodes/ Githup page: https://github.com/birlorg/couponcodes

Known bugs/shortcomings compared to upstream:

  • Too lazy to rot13 the bad words, you have to live with them in the repo.
  • You can not select your own plaintext, it will only generate new random codes.
  • This code has been barely tested and not audited, security and fit for purpose is your problem.

All other shortcomings are bugs and you should file issues. Patches to fix the known shortcomings welcome.

Usage

pip install cccodes

# Generate a code:
python3 -c "import cccodes;print(cccodes.generate())"
66WUX-FJ5XA-6WUBU

# validate a code:
python3 -c "import cccodes;print(cccodes.validate('66wUX-FJ5XA-6WUBU'))"
True

# invalid check digit in 1st part from above code.
python3 -c "import cccodes;print(cccodes.validate('66WU7-FJ5XA-6WUBU'))"
False

# if you are storing the codes that are generated, so you can prevent re-use or whatever:
python3 -c "import cccodes;print(cccodes.normalize('tI3P-SLMP-E0B5'))"
T13P-5LMP-E0B5
#FYI: don't worry, validate will call normalize() for you, so you don't have to.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cccodes-0.1.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

cccodes-0.1.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file cccodes-0.1.2.tar.gz.

File metadata

  • Download URL: cccodes-0.1.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for cccodes-0.1.2.tar.gz
Algorithm Hash digest
SHA256 011c625e0d1256941c662f29079afc2d5f968ffc1884cee66f6661763cafbbaa
MD5 5f7b5a9de7e9dbaf41d801289c38865e
BLAKE2b-256 c4c3488cb943f235280eb55bd4776c0ccd6c7bb5311b01fa6a008133c54fbebf

See more details on using hashes here.

File details

Details for the file cccodes-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: cccodes-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

File hashes

Hashes for cccodes-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ec9c8e688f4247058fe13979239b139d7c25a805a0b6109dd8e63bca3a2bbdc1
MD5 a7df940524a31799c21221f78b9773a6
BLAKE2b-256 f8cfe58094c4a19785b0ab8b364cb4f629202cf834b39dd8dd3a1f8d02229c5a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page