Guest User

Untitled

a guest
Apr 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. import datetime
  2. import random
  3. import sqlalchemy
  4. from sqlalchemy import text
  5.  
  6. from config import config
  7.  
  8.  
  9. def random_date(start, end):
  10. return start + datetime.timedelta(
  11. seconds=random.randint(0, int((end - start).total_seconds())),
  12. )
  13.  
  14.  
  15. def clear_tables(connect):
  16. sql = [
  17. "DELETE FROM employment",
  18. "DELETE FROM operation",
  19. "DELETE FROM position",
  20. "DELETE FROM employee",
  21. "DELETE FROM project"
  22. ]
  23. for query in sql:
  24. connect.execute(query)
  25.  
  26.  
  27. def fill_position(connect, count):
  28. sql = text("INSERT INTO position (name, salary) VALUES (:name, :salary)")
  29. values = []
  30. for i in range(count):
  31. values.append(dict(
  32. name=str(i),
  33. salary=random.randint(100, 10000)
  34. ))
  35. connect.execute(sql, values)
  36.  
  37.  
  38. def fill_project(connect, count):
  39. sql = text("INSERT INTO project (name, budget) VALUES (:name, :budget)")
  40. values = []
  41. for i in range(count):
  42. values.append(dict(
  43. name=str(i),
  44. budget=random.randint(10 ** 3, 10 ** 6)
  45. ))
  46. connect.execute(sql, values)
  47.  
  48.  
  49. def fill_employee(connect, count):
  50. sql = text("INSERT INTO employee (fio, sex, bday, start_date) VALUES (:fio, :sex, :bday, :start_date)")
  51. values = []
  52. for i in range(count):
  53. values.append(dict(
  54. fio=str(i),
  55. sex=random.choice(['m', 'f']),
  56. bday=random_date(datetime.datetime(1985, 1, 1), datetime.datetime(1995, 1, 1)),
  57. start_date=random_date(datetime.datetime(2010, 1, 1), datetime.datetime(2018, 1, 1))
  58. ))
  59. connect.execute(sql, values)
  60.  
  61.  
  62. def get_table_ids(connect, table):
  63. res = connect.execute("Select id from {}".format(table)).fetchall()
  64. return tuple(pid[0] for pid in res)
  65.  
  66.  
  67. def fill_operation(connect, count):
  68. pids = get_table_ids(connect, "project")
  69. sql = text("INSERT INTO operation (project_id, name, description, sum) VALUES (:pid, :name, :description, :sum)")
  70. values = []
  71. for i in range(count):
  72. values.append(dict(
  73. pid=random.choice(pids),
  74. name=str(i),
  75. description=str(i ** 2),
  76. sum=random.randint(1, 100)
  77. ))
  78. connect.execute(sql, values)
  79.  
  80.  
  81. def fill_employment(connect, count):
  82. pids = get_table_ids(connect, "project")
  83. eids = get_table_ids(connect, "employee")
  84. posids = get_table_ids(connect, "position")
  85. sql = text("INSERT INTO employment (project_id, employee_id, position_id, start_date, rate) "
  86. "VALUES (:pid, :eid, :posid, :start_date, :rate)")
  87. values = []
  88. for i in range(count):
  89. values.append(dict(
  90. pid=random.choice(pids),
  91. eid=random.choice(eids),
  92. posid=random.choice(posids),
  93. start_date=random_date(datetime.datetime(2018, 1, 1), datetime.datetime(2020, 1, 1)),
  94. rate=(random.random() + 0.1) * 2.5
  95. ))
  96. connect.execute(sql, values)
  97.  
  98.  
  99. def create_connect():
  100. engine = sqlalchemy.create_engine('postgresql://{user}:{password}@{host}:{port}/{db}'.format(**config))
  101. connect = engine.connect()
  102. return connect
  103.  
  104.  
  105. def _main():
  106. connect = create_connect()
  107. fill_employment(connect, 100)
  108. # clear_tables(connect)
  109.  
  110.  
  111. if __name__ == '__main__':
  112. _main()
Add Comment
Please, Sign In to add comment