• Source
    1. import csv
    2. import names
    3. from rstr import rstr
    4. from string import digits
    5. import datetime
    6. import re
    7. from random import randint
    8. from time import ctime
    9.  
    10.  
    11. ''' Gera um arquivo csv contendo os campos
    12. "cpf, matricula, sobrenome, nome, email, data de ingresso"
    13. '''
    14.  
    15. def get_matricula(data_ingresso):
    16. try:
    17. return '{}{}'.format(datetime.datetime.strptime(
    18. data_ingresso, "%a %b %d %H:%M:%S %Y").strftime("%Y"), rstr(digits, 5))
    19. except:
    20. return '{}{}'.format(datetime.datetime.now().strftime("%Y"), rstr(digits, 5))
    21.  
    22. def get_cpf():
    23. ''' retorna string de CPF não verificado'''
    24. while True:
    25. return '{}.{}.{}-{}'.format(
    26. rstr(digits, 3),
    27. rstr(digits, 3),
    28. rstr(digits, 3),
    29. rstr(digits, 2),
    30. )
    31.  
    32. def gen_nome_completo():
    33. '''retorna string com nome, sobrenome'''
    34. while True:
    35. yield (names.get_full_name())
    36.  
    37. def gen_email(nomecompleto, dominio='incolume.com.br'):
    38. '''recebe nome completo e dominio, retorna email'''
    39. try:
    40. return '{}.{}@{}'.format(*(nomecompleto.lower().split()), dominio)
    41. except:
    42. return None
    43.  
    44. def gen_data_ingresso():
    45. ''':return data'''
    46. seconds = int('{}{}'.format(
    47. randint(12, 14),
    48. rstr(digits, 8)
    49. ))
    50. return ctime(seconds)
    51.  
    52.  
    53. def gen_massa(qlinhas, cvsname):
    54. '''cria cvsname com a quantidade de linhas informadas em qlinhas '''
    55. try:
    56. header = "cpf, matricula, sobrenome, nome, email, data de ingresso"
    57. with open(cvsname, 'w') as file:
    58. csvhandler = csv.writer(file)
    59. csvhandler.writerow(header.split(', '))
    60. for i in range(qlinhas):
    61. nome = gen_nome_completo()
    62. person = next(nome)
    63. date = gen_data_ingresso()
    64. linha = '{}, {}, {c[1]}, {c[0]}, {}, {}'.format(
    65. get_cpf(),
    66. get_matricula(date),
    67. gen_email(nomecompleto=person),
    68. date,
    69. c = person.split(),
    70. )
    71. csvhandler.writerow(linha.split(','))
    72. return True
    73. except:
    74. raise
    75.  
    76.  
    77. if __name__ == '__main__':
    78. print(gen_massa(30, 'file.csv'))
    79.