Skip to content

Instantly share code, notes, and snippets.

@lw
Created February 17, 2013 13:54
Show Gist options
  • Save lw/4971583 to your computer and use it in GitHub Desktop.
Save lw/4971583 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import random
def genera_codice():
result = ""
c1 = random.randint(1, 3);
for i in range(c1):
result += random.choice('BCDFGHJKLMNPQRSTVWXYZ')
for i in range(3 - c1):
result += random.choice('AEIOU')
c2 = random.randint(1, 3);
for i in range(c2):
result += random.choice('BCDFGHJKLMNPQRSTVWXYZ')
for i in range(3 - c2):
result += random.choice('AEIOU')
result += '%02d' % random.randint(0, 99)
result += random.choice('ABCDEHLMPRST')
if result[-1] == 'B':
if (int(result[-3:-1]) % 4 != 0):
day = random.randint(1, 28)
else:
day = random.randint(1, 29)
elif result[-1] in 'DHPS':
day = random.randint(1, 30)
else:
day = random.randint(1, 31)
if random.randint(0, 1) == 0:
day += 40;
result += "%02d" % day
result += random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
result += "%03d" % random.randint(1, 999)
somma = 0
codes = [1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23]
for i in range(len(result)):
code = (ord(result[i]) - ord('A')) if 'A' <= result[i] <= 'Z' else int(result[i])
if i % 2 == 0:
somma += codes[code]
else:
somma += code
result += chr(somma % 26 + ord('A'))
return result
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Genera codici fiscali.')
parser.add_argument('-n', '--number', default=1, type=int, metavar='N',
help='il numero di codici che verranno generati')
args = parser.parse_args()
for i in range(args.number):
print(genera_codice())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment