def populate_notices_data(): countries = session.query(Country).all()[4:] for country in countries: for entity in notices.get_notices_for_nationality(country.code): entity_id = entity.get("entity_id") entity_data = notices.get_entity_data(entity_id) date_of_birth = transform_date(entity_data.get('date_of_birth')) try: obj = Notice(entity_id=entity_id, date_of_birth=date_of_birth, country=country.code, sex=entity_data.get('sex'), place_of_birth=entity_data.get('place_of_birth'), charge=entity_data.get("charge")) nationalities = entity_data.get('nationalities') print(country, entity_id) # for nation in nationalities: # nation_obj = session.query(Country).filter(Country.code == nation).first() # obj.nationalities.append(nation_obj) session.add(obj) session.commit() except exc.IntegrityError: session.rollback() sleep(1)
def save_posts(posts): try: session.add_all(posts) session.commit() except IntegrityError: traceback.print_exc() session.rollback() return posts
async def latest_prices(): try: result = await session.query(CXPrice).all() data = [] for r in result: if isinstance(r, CXPrice): data.append(r.serialize()) print(data) return data except exc.SQLAlchemyError as err: print(err.args) session.rollback()
async def addMultiSigAddress(pub_addr: str, keys: list, uid: int): if pub_addr is not None and len(keys) > 0: for key in keys: trx_key = TrxKey(value=key, uid=uid) session.add(trx_key) session.flush() multi_sig_key = MKey(pub=pub_addr, uid=uid, kid=trx_key.id) session.add(multi_sig_key) try: session.commit() return multi_sig_key.id except exc.SQLAlchemyError as err: print(err.args) session.rollback()
def cx_after_flush(prev_session, context): for v in prev_session.dirty: if isinstance(v, CXPrice): last = session.query(CXPriceRevision).filter( CXPriceRevision.currency == v.currency).group_by( CXPriceRevision.id).order_by( desc(func.max(CXPriceRevision.rid))).one_or_none() rid = 1 if last is None else last.rid + 1 new_rev = CXPriceRevision(rid=rid, currency=v.currency, last=v.last, buy=v.buy, sell=v.sell, modified=v.modified) try: session.add(new_rev) session.commit() except exc.SQLAlchemyError as error: session.rollback()
async def latest_prices_async(currency='CAD'): if currency is None: currency = 'CAD' if currency_index[currency]: try: result = session.query(CXPriceRevision).filter( CXPriceRevision.currency == currency).order_by( CXPriceRevision.rid.desc()).limit(3).all() # result = session.query(CXPriceRevision).order_by(CXPriceRevision.rid.desc()).limit(16).all() data = [] for r in result: if isinstance(r, CXPriceRevision): data.append(r.serialize()) return data except exc.SQLAlchemyError as err: logger.debug('Error getting latest prices', err) session.rollback() else: # TODO: This needs proper error handling return False
def fetch(self, name): ''' ''' try: # Make sure the session is still valid: if not self.isauthenticated(): self.authenticate() start = time.time() # Query the TD API: tradable = session.query(Tradable).filter_by(name=name).first() response = self.tdclient.optionschain(tradable.name) self._parse(response, tradable) print 'Finished Fetching %s Options Data In %.2fs' % ( tradable, time.time() - start) except: print 'An Error Occurred On Fetching %s Options, Skipping...' % name print traceback.format_exc() session.rollback()
async def addSingleKey(key: str, uid: int): """ :param key: :param uid: :return: """ if key is not None and uid is not None: new_trx_key = TrxKey(value=key, uid=uid, multi=False, status=True) # TODO we should be checking if a key exists with the same hash # find_key = await find_key_for_uid(uid) session.add(new_trx_key) session.flush() single_key = SKey(value=key, uid=uid, kid=new_trx_key.id) session.add(single_key) try: session.commit() return single_key except exc.SQLAlchemyError as err: print(err.args) session.rollback() return err
def updatevols(cls): ''' Update the values of OptionsFetch volatility, oi, volume columns ''' # Get the IDs of the all options fetches from the past five days: now = datetime.datetime.now() days = 5 cutoff = now - datetime.timedelta(days=days) query = session.query( OptionsFetch.id).filter(OptionsFetch.time > cutoff).all() ids = [id for id, in query] print 'Updating %s Options Fetches From the last %s Days...' % ( len(ids), days) # Update Each Fetch: for id in ids: fetch = session.query(OptionsFetch).get(id) # Implied Volatility: if fetch.volatility is None: try: print 'Loading Implied Vol For %s...' % fetch.id fetch.volatility = VIXImplied.getiv(fetch) except: session.rollback() # Volume: if fetch.volume is None: print 'Loading Volume For %s...' % fetch.id fetch.volume = VIXImplied.volume(fetch) # Open Interest: if fetch.oi is None: print 'Loading Open Interest For %s...' % fetch.id fetch.oi = VIXImplied.openinterest(fetch) # Save all changes: session.commit()
def rollback_transaction(): try: session.rollback() return True except exc.SQLAlchemyError as error: return error