Skip to main content

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.

Metadata

Release files for cccodes 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cccodes 0.1.2
File Size Uploaded
cccodes-0.1.2.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cccodes 0.1.2
File Interpreter ABI Platform
cccodes-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.6 kB

Release files / cccodes-0.1.2.tar.gz

Download URL cccodes-0.1.2.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
011c625e0d1256941c662f29079afc2d5f968ffc1884cee66f6661763cafbbaa
BLAKE2b-256 checksum
How to use checksums
c4c3488cb943f235280eb55bd4776c0ccd6c7bb5311b01fa6a008133c54fbebf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

Release files / cccodes-0.1.2-py3-none-any.whl

Download URL cccodes-0.1.2-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ec9c8e688f4247058fe13979239b139d7c25a805a0b6109dd8e63bca3a2bbdc1
BLAKE2b-256 checksum
How to use checksums
f8cfe58094c4a19785b0ab8b364cb4f629202cf834b39dd8dd3a1f8d02229c5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/23.4.0

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page