1 from string import digits 2 from time import strftime, gmtime 3 from random import randint, choice 4 5 LINES = 100000 6 7 # OUTPUT: 8 # ID_TRANSACCION, ID_USUARIO, FECHA, MONTO, NUMERO DE TRAJETA, DESCRIPCION 9 # 123412,1,05/11/2011 10:00:00,-10,4916288217067475, Compra supermercado 10 11 descs = [ "Compras supermercado", "Pago tarjeta", "Compras libreria", "Pago Mecanico", "Pago Dentista", "Pago Servicios online", "Compras Ferreteria", "Compras Accesorios Informatica", "Compras farmacia", "Ventas online", "Extraccion cajero" ] 12 13 def calculate_luhn(cc): 14 num = list(map(int, str(cc))) 15 check_digit = 10 - sum(num[-2::-2] + [sum(divmod(d * 2, 10)) for d in num[::-2]]) % 10 16 return 0 if check_digit == 10 else check_digit 17 18 def generate_card(type): 19 """ 20 Prefill some values based on the card type 21 """ 22 card_types = ["americanexpress","visa13", "visa16","mastercard","discover"] 23 24 def prefill(t): 25 # typical number of digits in credit card 26 def_length = 16 27 28 """ 29 Prefill with initial numbers and return it including the total number of digits 30 remaining to fill 31 """ 32 if t == card_types[0]: 33 # american express starts with 3 and is 15 digits long 34 # override the def lengths 35 return [3, randint(4,7)], 13 36 37 elif t == card_types[1] or t == card_types[2]: 38 # visa starts with 4 39 if t.endswith("16"): 40 return [4], def_length - 1 41 else: 42 return [4], 12 43 44 elif t == card_types[3]: 45 # master card start with 5 and is 16 digits long 46 return [5, randint(1,5)], def_length - 2 47 48 elif t == card_types[4]: 49 # discover card starts with 6011 and is 16 digits long 50 return [6, 0, 1, 1], def_length - 4 51 52 else: 53 # this section probably not even needed here 54 return [], def_length 55 56 def finalize(nums): 57 """ 58 Make the current generated list pass the Luhn check by checking and adding 59 the last digit appropriately bia calculating the check sum 60 """ 61 check_sum = 0 62 63 #is_even = True if (len(nums) + 1 % 2) == 0 else False 64 65 """ 66 Reason for this check offset is to figure out whether the final list is going 67 to be even or odd which will affect calculating the check_sum. 68 This is mainly also to avoid reversing the list back and forth which is specified 69 on the Luhn algorithm. 70 """ 71 check_offset = (len(nums) + 1) % 2 72 73 for i, n in enumerate(nums): 74 if (i + check_offset) % 2 == 0: 75 n_ = n*2 76 check_sum += n_ -9 if n_ > 9 else n_ 77 else: 78 check_sum += n 79 return nums + [10 - (check_sum % 10) ] 80 81 # main body 82 t = type.lower() 83 if t not in card_types: 84 print("Unknown type: '%s'" % type) 85 print("Please pick one of these supported types: %s" % card_types) 86 return 87 88 initial, rem = prefill(t) 89 so_far = initial + [randint(1,9) for x in range(rem - 1)] 90 return ("".join(map(str,finalize(so_far)))) 91 92 def generate_file(max_lines): 93 id_transaction_base = 123400 94 id_user_base = 1 95 id_user_max = 10000 96 amount_base = 1 97 amount_max = 100 98 for i in range(max_lines): 99 id_transaction = (i + id_transaction_base) 100 id_user = randint(id_user_base, id_user_max) 101 102 card_nr = generate_card("visa16") 103 while calculate_luhn(card_nr) != 0: 104 card_nr = generate_card("visa16") 105 106 date = strftime("%d.%m.%Y %H:%M:%S", gmtime(1320487200 + i)) 107 108 j = randint(1, len(descs) - 1) 109 desc = descs[j] 110 111 if j > 8: amount = randint(amount_base, amount_max) 112 else: amount = randint(-amount_max, -amount_base) 113 114 print(id_transaction,id_user,date,amount,card_nr,desc, sep=',') 115 116 generate_file(LINES)