Advertisement
Guest User

Untitled

a guest
Oct 11th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #! /usr/bin/env python
  2. import psycopg2
  3.  
  4. try:
  5. from StringIO import StringIO
  6. except ImportError:
  7. from io import StringIO
  8.  
  9. conn = psycopg2.connect(database='postgres', user='postgres', password='')
  10. cur = conn.cursor()
  11. rounds = range(14, 69)
  12.  
  13.  
  14. class PlanetRankTables:
  15. basename = 'planet_ranks_round_'
  16. for i in rounds:
  17. try:
  18. cur.execute(
  19. """CREATE TABLE IF NOT EXISTS round_%d_planet_rank (
  20. planet_rank integer, ruler_name text, planet_name text,
  21. nickname text, alliance_name text,
  22. planet_score integer DEFAULT 0, planet_value integer DEFAULT 0,
  23. planet_xp integer DEFAULT 0, planet_size integer DEFAULT 0,
  24. planet_race text, planet_coords text
  25. );"""
  26. % i)
  27. except IOError:
  28. print('ERROR: something went wrong creating the tables.')
  29. try:
  30. io = open(str('dumps/planet_ranks_round_%d.csv' % i), 'r')
  31. cur.copy_from(io, str('round_%d_planet_rank' % i), sep=',')
  32. except IOError:
  33. print('ERROR: something went wrong inserting the data.')
  34. conn.commit()
  35.  
  36.  
  37. class PlanetRanks:
  38. for i in rounds:
  39. try:
  40. cur.execute(
  41. """ALTER TABLE round_%d_planet_rank ADD COLUMN round_number integer;"""
  42. % i)
  43. cur.execute(
  44. """UPDATE round_{0}_planet_rank SET round_number = {1};"""
  45. .format(i, i))
  46. except IOError:
  47. print('ERROR: something went wrong altering the data.')
  48. try:
  49. cur.execute(
  50. """CREATE TABLE IF NOT EXISTS planetrankstmp (
  51. planet_rank integer, ruler_name text, planet_name text,
  52. nickname text, alliance_name text,
  53. planet_score integer DEFAULT 0, planet_value integer DEFAULT 0,
  54. planet_xp integer DEFAULT 0, planet_size integer DEFAULT 0,
  55. planet_race text, planet_coords text, round_number integer
  56. );""")
  57. cur.execute(
  58. """INSERT INTO planetrankstmp SELECT * FROM round_%d_planet_rank ORDER BY planet_rank ASC;"""
  59. % i)
  60. cur.execute(
  61. """INSERT INTO planetranks SELECT DISTINCT * FROM planetrankstmp ORDER BY round_number ASC, planet_rank ASC;"""
  62. )
  63. cur.execute("DROP TABLE planetrankstmp")
  64. except IOError:
  65. print('ERROR: something went wrong merging the data.')
  66. conn.commit()
  67. cur.close()
  68. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement