Advertisement
siverpro

Untitled

Jul 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import mysql.connector as mariadb
  2.  
  3. db = Database(db_hostname='mariamicro.cg7ucsq2kyal.eu-west-2.rds.amazonaws.com',
  4.     db_username='crap_db',
  5.     db_password='BrmDatabase3159',
  6.     db_dataname='CRAP')
  7.  
  8.  
  9. class DBError(Exception):
  10.     """
  11.     Fange exceptions for invalide kommandoer og andre responser som
  12.     ikke er en kode 200 respons.
  13.     """
  14.     def __init__(self, err):
  15.         pass
  16.  
  17. class Database(object):
  18.     def __init__(self, db_hostname = None, db_username = None, db_password = None, db_dataname = None)
  19.  
  20.         self.db_hostname = str(db_hostname) if db_hostname else raise DBError("No hostname given.")
  21.         self.db_username = str(db_username) if db_username else raise DBError("DB username is needed.")
  22.         self.db_password = str(db_password) if db_password else raise DBError("DB password is needed.")
  23.         self.db_dataname = str(db_dataname) if db_dataname else raise DBError("No database name is given.")
  24.  
  25.  
  26.     def connect(self):
  27.         try:
  28.             self.db_connection = mariadb.connect(host=self.db_hostname, user=self.db_username, password=self.db_password, database=self.db_dataname)
  29.         except self.db_connection.error as e:
  30.             raise DBError(str(e))
  31.  
  32.     def append_tx_log(self, line: dict) -> int:
  33.         out = 0
  34.  
  35.         if self.tx_exists(line) = True:
  36.             print("Line already exists, not appending")
  37.             return 0
  38.  
  39.         in_date = line['Date Acquired']
  40.         in_nok_amount = line['Cost Basis']
  41.         in_foreign_amount = line['Volume']
  42.         in_foreign_currency = line['Symbol']
  43.         cursor = self.db_connection.cursor()
  44.  
  45.         try:
  46.             cursor.execute("INSERT INTO Tx_Log
  47.                             (Date, NOK_Amount, Foreign_Amount, Foreign_Currency)
  48.                             VALUES (%s, %s, %s, %s)",
  49.                             (in_date, in_nok_amount, in_foreign_amount, in_foreign_currency))
  50.  
  51.         except self.db_connection.Error as error:
  52.             print("Error: {}".format(error))
  53.  
  54.         db_connection.commit()
  55.         out = db_connection.insert_id()
  56.         return out
  57.  
  58.  
  59.     def tx_exists(self, line: dict):
  60.         out = True
  61.        
  62.         in_date = line['Date Acquired']
  63.         in_nok_amount = line['Cost Basis']
  64.         in_foreign_amount = line['Volume']
  65.         in_foreign_currency = line['Symbol']
  66.  
  67.         cursor = self.db_connection.cursor()
  68.         match = cursor.execute("SELECT ID FROM Tx_Log WHERE
  69.                                 date=%s AND NOK_Amount=%s
  70.                                 AND Foreign_Amount=%s AND Foreign_Currency=%s",
  71.                                 (in_date, in_nok_amount, in_foreign_amount, in_foreign_currency))
  72.  
  73.         # Check log files for duplicates
  74.         if match.rowcount == 0
  75.             # return false if no match is found
  76.             out = False
  77.         return out
  78.  
  79.     def close_connection(self)
  80.         self.db_connection.close()
  81.  
  82.  
  83. # Check if the log file contains the transaction
  84. # by compare_line(dict, logfile). If it returns true,
  85. # add the line to log file append_log(dict, logfile)
  86. # and then push line to Fiken. If false, skip the line.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement