Advertisement
Guest User

Untitled

a guest
Apr 12th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3.  
  4. Script de test para criar registros e recurar e calcular media
  5.  
  6. SQL de criação da tabela
  7. CREATE TABLE `tb_customer_account` (
  8. `id_customer` INT NOT NULL ,
  9. `cpf_cnpj` VARCHAR(20) NOT NULL ,
  10. `nm_customer` VARCHAR(200) NOT NULL ,
  11. `is_active` BOOLEAN NOT NULL ,
  12. `vl_total` DECIMAL(60) NOT NULL ,
  13. PRIMARY KEY (`id_customer`),
  14. UNIQUE `unique_cpf_cnpj` (`cpf_cnpj`)
  15. );
  16.  
  17. """
  18. from random import choice, randint, randrange
  19.  
  20. from mysql import connector
  21.  
  22.  
  23. def gera_cpf():
  24. def calcula_digito(digs):
  25. s = 0
  26. qtd = len(digs)
  27. for i in xrange(qtd):
  28. s += n[i] * (1 + qtd - i)
  29. res = 11 - s % 11
  30. if res >= 10:
  31. return 0
  32. return res
  33. n = [randrange(10) for i in xrange(9)]
  34. n.append(calcula_digito(n))
  35. n.append(calcula_digito(n))
  36. return "%d%d%d.%d%d%d.%d%d%d-%d%d" % tuple(n)
  37.  
  38.  
  39. DB_USER = 'root'
  40. DB_PASSWORD = 'root'
  41. DB_HOST = 'localhost'
  42. DB_DATABASE = 'camila_teste'
  43. cnx = connector.connect(user=DB_USER, password=DB_PASSWORD,
  44. host=DB_HOST, database=DB_DATABASE,)
  45. cursor = cnx.cursor()
  46.  
  47.  
  48. sql_add_customer = """ INSERT INTO tb_customer_account
  49. (id_customer, cpf_cnpj, nm_customer, is_active, vl_total) VALUE
  50. (%(id)s, %(cpf)s, %(nome)s, %(active)s, %(total)s);"""
  51.  
  52. # Criar os dados no Banco de Dados
  53. for n in xrange(1, 3000):
  54.  
  55. data = {
  56. 'id': n,
  57. 'cpf': gera_cpf(),
  58. 'nome': 'Nome Cliente %s' % n,
  59. 'active': choice(['1', '0']),
  60. 'total': float(randint(10000, 100000)) / 100,
  61. }
  62. cursor.execute(sql_add_customer, data)
  63.  
  64. cnx.commit()
  65. cursor.close()
  66.  
  67.  
  68. # Recuperar e calcular a media
  69.  
  70. sql_list_customer = """ SELECT nm_customer, vl_total FROM tb_customer_account WHERE id_customer >= 1500 AND id_customer <= 2700; """
  71. cursor = cnx.cursor()
  72. cursor.execute(sql_list_customer)
  73.  
  74. total = 0.0
  75. count = 0
  76. list_cusomers = []
  77. for nm_customer, vl_total in cursor:
  78.  
  79. if vl_total > 560.0:
  80. total += float(vl_total)
  81. count += 1
  82.  
  83. list_cusomers.append((nm_customer, total))
  84.  
  85. if count:
  86. print("Media Final: %s" % str(total / count))
  87.  
  88.  
  89. list_cusomers = sorted(list_cusomers, key=lambda x: x[1])
  90. for name in list_cusomers:
  91. print(name[0])
  92.  
  93. cursor.close()
  94. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement