Skip to content

Instantly share code, notes, and snippets.

@mroig
Created July 10, 2017 11:41
Show Gist options
  • Save mroig/13753becf71a043c29b33db35c2897eb to your computer and use it in GitHub Desktop.
Save mroig/13753becf71a043c29b33db35c2897eb to your computer and use it in GitHub Desktop.
Generate Random CUPS
# coding: utf-8
import random
cups_checksum_table = 'TRWAGMYFPDXBNJZSQVHLCKE'
cups_name_length = 12
def gen_checksum(cupsname):
"""Calcula el checksum d'un CUPS."""
try:
rest0 = int(cupsname) % 529
except ValueError:
raise osv.except_osv("Error", "El CUPS no és vàlid.")
coficient = int(rest0 / 23)
rest1 = rest0 % 23
checksum = '%s%s' % (cups_checksum_table[coficient], cups_checksum_table[rest1])
return checksum
def gen_random_distri():
return str(random.randint(0, 9999)).zfill(4)
def gen_random_name():
return ''.join([str(random.randint(0, 9)) for i in range(cups_name_length)])
def gen_cups(distri=None, name=None, term='0F'):
if distri is None:
distri = gen_random_distri()
else:
distri.zfill(4)
if name is None:
name = gen_random_name()
else:
name.zfill(cups_name_length)
cupsname = distri + name
return 'ES' + cupsname + gen_checksum(cupsname) + term
# You can calculate a CUPS' checksum
print gen_checksum('1234000000000004')
# You can also generate a CUPS passing distri number and/or a CUPS number
print gen_cups('1234', '000000001234')
# Or generate a compleatly random CUPS
print gen_cups()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment