Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.32 KB | None | 0 0
  1. from pprint import pprint
  2. import psycopg2
  3. import random, datetime
  4. from dateutil.relativedelta import relativedelta
  5.  
  6. # from sshtunnel import SSHTunnelForwarder
  7. #
  8. # tunnel = SSHTunnelForwarder(
  9. #     'pascal.fis.agh.edu.pl',
  10. #     ssh_username="6marks",
  11. #     ssh_password="lol",
  12. #     remote_bind_address=('127.0.0.1', 22)
  13. #
  14. # )
  15. #
  16. # tunnel.start()
  17. # print(tunnel.local_bind_host)
  18. conn = psycopg2.connect(
  19.     # database='db',
  20.     user='postgres',
  21.     password='lol'
  22. )
  23.  
  24. cur = conn.cursor()
  25.  
  26. cur.execute("""-- Tworzenie tabel --
  27. create table Typ_pojazdu ( typ_pojazdu_id int, nazwa varchar(30)) ;
  28. create table Linia ( numer_linii int, typ_pojazdu int) ;
  29. create table Przystanek ( przystanek_id int, nazwa varchar(30), autobusy boolean, tramwaje boolean ) ;
  30. create table Linia_Przystanek ( przystanek_id int, numer_linii int, godzina time without time zone ) ;
  31.  
  32. alter table Typ_pojazdu add primary key (typ_pojazdu_id) ;
  33. alter table Linia add primary key (numer_linii) ;
  34. alter table Przystanek add primary key (przystanek_id) ;
  35. alter table Linia_Przystanek add primary key (przystanek_id, numer_linii, godzina) ;
  36.  
  37. alter table Linia add foreign key (typ_pojazdu) references Typ_pojazdu ( typ_pojazdu_id) ;
  38. alter table Linia_Przystanek add foreign key (przystanek_id) references Przystanek ( przystanek_id) ;
  39. alter table Linia_Przystanek add foreign key (numer_linii) references Linia ( numer_linii) ;
  40.  
  41. insert into Typ_pojazdu  values
  42. ( 0, 'Autobus'),
  43. ( 1, 'Tramwaj');
  44.  
  45. """)
  46.  
  47. polecenie = "insert into Linia  values"
  48. for i in range(1, 25):
  49.     polecenie += "\n(" + str(i) + ", " + str(random.randrange(0, 2)) + "), "
  50. polecenie = polecenie[:-2] + ";"
  51. cur.execute(polecenie)
  52.  
  53. polecenie = "insert into Przystanek  values"
  54. for i in range(1, 45):
  55.     polecenie += "\n(" + str(i) + ", 'Przystanek nr. " + str(i) + "', True, True), "
  56. polecenie = polecenie[:-2] + ";"
  57. cur.execute(polecenie)
  58.  
  59. for linia in range(1, 25):
  60.     liczba_przystankow = random.randrange(10, 21)
  61.     linieprzystankow = set()
  62.     while (len(linieprzystankow) < liczba_przystankow):
  63.         linieprzystankow.add(random.randrange(1, 45))
  64.     czas_odjazdu = datetime.datetime(year=2000, day=1, month=1, hour=random.randrange(0, 24),
  65.                                      minute=random.randrange(0, 60))
  66.     new_time = czas_odjazdu + relativedelta(hours=8)
  67.     new_time2 = czas_odjazdu + relativedelta(hours=16)
  68.     for przystanek in linieprzystankow:
  69.         czas_odjazdu += relativedelta(minutes=2)
  70.         new_time += relativedelta(minutes=2)
  71.         new_time2 += relativedelta(minutes=2)
  72.         cur.execute(
  73.             "insert into Linia_Przystanek  values (" + str(przystanek) + ", " + str(linia) + ", " + "'{0:02d}".format(
  74.                 czas_odjazdu.hour) + ":" + "{0:02d}'".format(czas_odjazdu.minute) + ");")
  75.         cur.execute(
  76.             "insert into Linia_Przystanek  values (" + str(przystanek) + ", " + str(linia) + ", " + "'{0:02d}".format(
  77.                 new_time.hour) + ":" + "{0:02d}'".format(new_time.minute) + ");")
  78.         cur.execute(
  79.             "insert into Linia_Przystanek  values (" + str(przystanek) + ", " + str(linia) + ", " + "'{0:02d}".format(
  80.                 new_time2.hour) + ":" + "{0:02d}'".format(new_time2.minute) + ");")
  81.  
  82. cur.execute("SELECT * FROM Linia")
  83. pprint(cur.fetchall())
  84.  
  85. cur.execute("SELECT * FROM Przystanek")
  86. pprint(cur.fetchall())
  87.  
  88. cur.execute("SELECT * FROM Linia_Przystanek")
  89. pprint(cur.fetchall())
  90.  
  91. cur.execute(
  92.     "SELECT Linia.numer_linii, Linia_Przystanek.przystanek_id FROM Linia, Linia_Przystanek WHERE Linia.numer_linii = Linia_Przystanek.numer_linii AND Linia_Przystanek.przystanek_id = 1 group by Linia.numer_linii, Linia_Przystanek.przystanek_id")
  93. pprint(cur.fetchall())
  94. print("\n\n")
  95.  
  96. cur.execute(
  97.     "SELECT Linia.numer_linii, Przystanek.nazwa, Linia_Przystanek.godzina  FROM Linia, Linia_Przystanek, Przystanek WHERE Linia.numer_linii = 5 AND Linia_Przystanek.numer_linii = 5 AND Linia_Przystanek.przystanek_id = 1 AND Przystanek.przystanek_id = 1" )
  98. pprint(cur.fetchall())
  99. print("\n\n")
  100.  
  101.  
  102. cur.execute(
  103.     "SELECT Linia.numer_linii, Przystanek.nazwa, Linia_Przystanek.godzina  FROM Linia, Linia_Przystanek, Przystanek WHERE Linia.numer_linii = 5 AND Linia_Przystanek.numer_linii = 5 AND Linia_Przystanek.przystanek_id = Przystanek.przystanek_id " )
  104. pprint(cur.fetchall())
  105. # conn.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement