users.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import random
  2. import re
  3. import csv
  4. from os import path
  5. from pprint import pprint
  6. def generate_record(i, row, addresses):
  7. address = random.choice(addresses)
  8. return {
  9. 'id': 1000 + i,
  10. 'name': row[0] + ' ' + row[1],
  11. 'username': row[0] + row[1],
  12. 'password': row[0] + row[1],
  13. 'email': row[0].lower() + '.' + row[1].lower() + '@mail.com',
  14. 'street': address['street'],
  15. 'housenumber': re.search(r'\d+', address['housenumber']).group(),
  16. 'plz': address['plz'],
  17. 'city': 'Heidelberg',
  18. 'country': 'Deutschland',
  19. 'geocode': address['geocode']
  20. }
  21. def generate_data():
  22. file = open(path.join(path.dirname(path.dirname(__file__)), 'resources', 'names.csv'), newline='', encoding='utf-8')
  23. data = csv.reader(file, delimiter=',')
  24. names = list(data)[1:]
  25. file = open(path.join(path.dirname(path.dirname(__file__)), 'resources', 'heidelberg-buildings.csv'), newline='', encoding='utf-8')
  26. data = csv.reader(file, delimiter='\t')
  27. addresses = [
  28. {
  29. 'plz': row[0],
  30. 'street': row[1],
  31. 'housenumber': row[2],
  32. 'geocode': row[3]
  33. }
  34. for i, row in enumerate(data)
  35. if i >= 1
  36. ]
  37. return [
  38. generate_record(i, row, addresses)
  39. for (i, row) in enumerate(names)
  40. ]
  41. if __name__ == '__main__':
  42. pprint(generate_data())