Queer European MD passionate about IT

Mission251Solutions.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import csv
  2. from datetime import datetime
  3. import io
  4. import psycopg2
  5. from urllib import request
  6. conn = psycopg2.connect(dbname='postgres', user='postgres')
  7. cur = conn.cursor()
  8. # Autocommit instead of commiting every transaction.
  9. conn.autocommit = True
  10. # Create database and users.
  11. cur.execute('CREATE DATABASE ihw')
  12. cur.execute("CREATE USER production WITH PASSWORD 'abc123'")
  13. cur.execute("CREATE USER analyst WITH PASSWORD 'def456'")
  14. # Reconnect to ihw database.
  15. conn = psycopg2.connect(dbname='ihw', user='postgres')
  16. conn.autocommit = True
  17. cur = conn.cursor()
  18. # Create the table.
  19. cur.execute(
  20. """
  21. CREATE TABLE hurricanes (
  22. fid INTEGER PRIMARY KEY,
  23. recorded_at TIMESTAMP,
  24. btid INTEGER,
  25. name VARCHAR(10),
  26. lat DECIMAL(4, 1),
  27. long DECIMAL(4, 1),
  28. wind_kts SMALLINT,
  29. pressure INTEGER,
  30. category VARCHAR(2),
  31. basin VARCHAR(16),
  32. shape_length DECIMAL(8, 6)
  33. )
  34. """
  35. )
  36. # Manage privileges.
  37. cur.execute("REVOKE ALL ON hurricanes FROM production")
  38. cur.execute("REVOKE ALL ON hurricanes FROM analyst")
  39. cur.execute("GRANT SELECT, INSERT, UPDATE ON hurricanes TO production")
  40. cur.execute("GRANT SELECT ON hurricanes TO analyst")
  41. conn.close()
  42. # Reconnect with production user.
  43. conn = psycopg2.connect(dbname='ihw', user='production', password='abc123')
  44. cur = conn.cursor()
  45. conn.autocommit = True
  46. # Insert the data.
  47. response = request.urlopen('https://dq-content.s3.amazonaws.com/251/storm_data.csv')
  48. reader = csv.reader(io.TextIOWrapper(response))
  49. # Skip the header.
  50. _ = next(reader)
  51. rows = []
  52. for line in reader:
  53. recorded_at = datetime(int(line[1]), int(line[2]), int(line[3]), hour=int(line[4][:2]), minute=int(line[4][2:-1]))
  54. new_line = [line[0], recorded_at] + line[5:]
  55. rows.append(
  56. cur.mogrify(
  57. "(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
  58. new_line
  59. ).decode('utf-8')
  60. )
  61. cur.execute('INSERT INTO hurricanes VALUES ' + ",".join(rows))