def _sync(self) -> bool: try: str_date = DateTime(self.get_last_sync_date(), timezone_hours=0).to_str("%Y%m%d") market_data = self._get_market_data(str_date) if market_data is not None: with DBLock: try: # save to models self.save_market_data(market_data) # update sync date self.set_last_sync_date( DateTime(timezone_hours=0).date.timestamp) self._logger.log( "%s market data sync success. date: %s" % (self.__class__.__name__, DateTime(self.get_last_sync_date(), timezone_hours=0).to_str("%Y%m%d"))) commit() except Exception as e: rollback() raise e return True else: return False except Exception as e: self._logger.log_err(e) return False
def dbupdate(self): """Write a completed gamestate to the sqlite database.""" log.debug("Trying to add a row for the new game.") # Get the automatic game ID to use in the Events table in place of the # random engine-generated game ID. The game timestamp is that of the # first game event. autogen_game_id = db.Games.insert( Timestamp=self.gs.events[0]['timestamp'], PlayerName0=self.players[0].name, PlayerName1=self.players[1].name, PlayerName2=self.players[2].name, PlayerName3=self.players[3].name ) log.debug("Grabbed a game_id from the database.") # Write everything from Events to the database. log.info("Writing game data for local game %s, db game %s.", self.gs.game_id, autogen_game_id) for action in self.gs.events: db.Events.insert( game_id=autogen_game_id, HandNumber=action['hand_num'], Timestamp=action['timestamp'], EventString=action['output'] ) db.commit()
def update(filename): '''Change the cube list to reflect the contents of FILENAME. FILENAME: a list of cardnames, one per line. No quantity data - multiples must be listed separately for each instance. This matches CubeTutor's output as of August 2014. This function: 1. Iterates through the list and builds a {name: quantity} dictionary. 2. Iterates through the cards table, setting quantities to 0 on all cards not appearing in the dictionary. 3. Iterates through the dictionary, creating or updating rows as needed. This function does not add, update, or change card attributes (color etc.). ''' log.debug("Reading from %s", filename) with open(filename, 'r') as f: cardData = f.readlines() log.info("cardData indicates a %s-card cube.", len(cardData)) # Step 1 newCards = {} # empty dictionary to hold name:qty keypairs for i in range(len(cardData)): cardData[i] = cardData[i].strip() if cardData[i] in newCards: # Duplicate cardname found... newCards[cardData[i]] += 1 # so increment the quantity. else: newCards[cardData[i]] = 1 # Otherwise add a key with qty=1. log.warning("newCards indicates a %s-card cube.", sum(newCards.values())) # Step 2 cut_cards = added_cards = 0 # Count number of changed cards for logging. for row in db().select(db.Cards.Name, db.Cards.Quantity): if row.Name not in newCards and row.Quantity != 0: # Card has been cut db(db.Cards.Name == row.Name).update(Quantity = 0) cut_cards += row.Quantity else: added_cards -= row.Quantity # This subtracts out the existing records' quantities. The total # number of added cards is the sum of this (negative or 0) and the # update_or_insert quantity sum in step 3. log.warning("Cutting %s cards...", cut_cards) # Step 3 for cardname in newCards: db.Cards.update_or_insert(db.Cards.Name == cardname, Name = cardname, Quantity = newCards[cardname]) added_cards += newCards[cardname] log.warning("Adding %s cards...", added_cards) db.commit() return
def set(cls, key, value): value = JsonUtil.serialize(value) with cls._lock: if cls._has_key(key): sql = 'UPDATE state SET `value` = \'%s\' WHERE `key` = \'%s\';' % ( value, key) else: sql = 'INSERT INTO state (`key`, `value`) VALUES (\'%s\', \'%s\');' % ( key, value) with DBLock: execute(sql) commit()
def write_updated_ratings(transactions): '''Given a cardname:(mu, sigma) dictonary, write each entry to the database as a new transaction. Each entry in a given call to this function should have an identical timestamp. inputs outputs ''' timestamp = datetime.datetime.utcnow() for cardname in transactions: db.Transactions.insert(card_id = db.Cards(db.Cards.Name==cardname).id, mu = transactions[cardname][0], sigma = transactions[cardname][1], timestamp = timestamp) db.commit()
def _sync(self) -> bool: try: # str_date = DateTime(self.get_current_sync_date(), timezone_hours=0).to_str("%Y%m%d") dt = self.get_current_sync_date() date_table, _, _ = self.db_context() if self._exists(date_table, dt): self.set_last_sync_date(dt) self._logger.log_err('duplicate date: %s' % DateTime(dt, timezone_hours=0).to_str('%Y-%m-%d')) return True nr_data = self._req_queue.get(dt) # nr_data = self._get_daily_all_nr(str_date) if nr_data is not None: with DBLock: try: # save to models self._sort_nr_data(nr_data) self._save_nr_data(nr_data) # update sync date self.set_last_sync_date(dt) self._logger.log( "%s nr_data sync success. date: %s" % ( self.__class__.__name__, DateTime(self.get_last_sync_date(), timezone_hours=0).to_str("%Y%m%d") ) ) commit() self._req_queue.remove(dt) except Exception as e: rollback() raise e return True else: return False except Exception as e: self._logger.log_err(e) return False
elif sys.platform.startswith('linux'): sys.path.append(os.path.abspath(__file__).split('/resource/load_resource_main.py')[0]) from common.client import client from load_resource import load_resource from common.db import commit if __name__ == '__main__': arguments = sys.argv client.init_client(arguments[1], arguments[2], arguments[3]) load_resource.set_access_key(arguments[1]) print('[*] Resource Load Start') load_resource.load_iam_resource() load_resource.load_vpc_resource() load_resource.load_ec2_resource() load_resource.load_rds_resource() load_resource.load_ebs_resource() load_resource.load_s3_resource() load_resource.load_cloudtrail_resource() load_resource.load_cloudwtach_resource() load_resource.load_cloudfront_resource() load_resource.load_kms_resource() load_resource.load_lambda_resource() commit() print('[*] Resource Load Complete')