Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #airport-codes.alaska/us
  2.  
  3. from numpy import genfromtxt
  4. from time import time
  5. from datetime import datetime
  6. from sqlalchemy import Column, Integer, Float, Date, String
  7. from sqlalchemy.ext.declarative import declarative_base
  8. from sqlalchemy import create_engine
  9. from sqlalchemy.orm import sessionmaker
  10.  
  11. def Load_Data(file_name):
  12. f = lambda s: str(s)
  13. data = genfromtxt(file_name, delimiter=',', skiprows=1, converters={0: f, 1:f,
  14. 2:f, 6:f, 7:f, 8:f, 9:f, 10:f})
  15. return data.tolist()
  16.  
  17. Base = declarative_base()
  18.  
  19. class AirportCode(Base):
  20.  
  21. __tablename__ = 'AirportCode'
  22. __table_args__ = {'sqlite_autoincrement': True}
  23. #tell SQLAlchemy the name of column and its attributes:
  24. id = Column(Integer, primary_key=True, nullable=False)
  25. ident = Column(String)
  26. type = Column(String)
  27. name = Column(String)
  28. latitude_deg = Column(String)
  29. longitude_deg = Column(String)
  30. elevation_ft = Column(String)
  31. continent = Column(String)
  32. iso_country = Column(String)
  33. iso_region = Column(String)
  34. municipality = Column(String)
  35. gps_code = Column(String)
  36.  
  37.  
  38. def __repr__(self):
  39. #return "<AirportCode(name='%s', municipality='%s')>n" % (self.name, self.municipality)
  40. return "name:{} municipality:{}n".format(self.name, self.municipality)
  41.  
  42. if __name__ == "__main__":
  43. t = time()
  44.  
  45. #Create the database
  46. engine = create_engine('sqlite:///airport-codes.db')
  47. Base.metadata.create_all(engine)
  48.  
  49. #Create the session
  50. session = sessionmaker()
  51. session.configure(bind=engine)
  52. s = session()
  53.  
  54. try:
  55. records_to_commit = 0
  56. file_name = "airport-codes.us" #23,000 records fails at next line
  57. #file_name = "airport-codes.alaska 250 records works fine"
  58. print file_name #for debugging
  59. data = Load_Data(file_name) # fails here on large files and triggers the except: below
  60. print 'file loaded' #for debugging
  61.  
  62. for i in data:
  63. records_to_commit += 1
  64. print i
  65.  
  66. record = AirportCode(**{
  67.  
  68. 'ident' : i[0].lower(),
  69. 'type' : i[1].lower(),
  70. 'name' : i[2].lower(),
  71. 'latitude_deg' : i[3],
  72. 'longitude_deg' : i[4],
  73. 'elevation_ft' : i[5],
  74. 'continent' : i[6],
  75. 'iso_country' : i[7],
  76. 'iso_region' : i[8],
  77. 'municipality' : i[9].lower(),
  78. 'gps_code' : i[10].lower()
  79.  
  80.  
  81. })
  82.  
  83. s.add(record) #Add all the records
  84. #print 'hi'
  85. if records_to_commit == 1000:
  86. s.flush() #Attempt to commit batch of 1000 records
  87. records_to_commit = 0
  88. s.commit() # flushes everything remaining + commits
  89. print 'commited'
  90. except:
  91.  
  92. print 'except'
  93.  
  94. #s.rollback() #Rollback the changes on error
  95. finally:
  96. s.close() #Close the connection
  97. print "Time elapsed: " + str(time() - t) + " s."
  98.  
  99. data = Load_Data(file_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement