コード例 #1
0
def collect_chart(filters):
    """
    Helper for collecting data for charts.
    :return:
    """
    gr_attr, gr_rel, gr_model = get_stat_join_params(filters.pop('grouping'))
    title = f'Average per {gr_model.title()}'

    session = create_session()
    query = session.query(gr_model.name,
                          func.avg(Statistic.value)).join(gr_rel)

    filter_titles = []
    for k, v in filters.items():
        v = int(v)
        if not v:
            continue
        f_attr, f_rel, f_model = get_stat_join_params(k)
        if f_attr == gr_attr:
            query = query.filter(f_attr == v)
        else:
            query = query.join(f_model, f_attr == v)
        filter_name = session.query(f_model).get(v).name
        filter_titles.append(f'{f_model.title()}: {filter_name}')
    chart = query.group_by(gr_attr).all()
    if filter_titles:
        title += f" for {', '.join(filter_titles)}"

    return {'title': title, 'data': [list(x) for x in chart]}
コード例 #2
0
    def post_trades(self, trades, raw_json=None):

        with create_session() as session:
            account = session.query(AccountModel).filter_by(
                api_key=self.api_key).first()
            for trade_params in trades:
                trade_id = trade_params['id']
                trade_in_db = session.query(Trade).filter_by(
                    tradeId=trade_id).first()
                if not trade_in_db:
                    trade = Trade(
                        tradeId=trade_params['id'],
                        orderId=trade_params['orderId'],
                        symbol=trade_params['symbol'],
                        price=trade_params['price'],
                        qty=trade_params['qty'],
                        commission=trade_params['commission'],
                        commissionAsset=trade_params['commissionAsset'],
                        time=datetime.fromtimestamp(
                            float(trade_params['time']) / 1000),
                        isBuyer=trade_params['isBuyer'],
                        isMaker=trade_params['isMaker'],
                        isBestMatch=trade_params['isBestMatch'],
                        account=account,
                        raw_json=trade_params if not raw_json else raw_json)
                    session.add(trade)
            session.commit()
        return True
コード例 #3
0
ファイル: list_db_places.py プロジェクト: virdi/ptolemy
def main():
    session = create_session()
    for place in session.query(Place).order_by(Place.ptolemy_name):
        #point = wkb.loads(place.ptolemy_point)
        point = to_shape(place.ptolemy_point)
        print place.ptolemy_id, place.ptolemy_name, point.x, point.y
    session.close()
コード例 #4
0
def main():
    session = create_session()
    for place in session.query(Place).order_by(Place.ptolemy_name):
        #point = wkb.loads(place.ptolemy_point)
        point = to_shape(place.ptolemy_point)
        print place.ptolemy_id, place.ptolemy_name, point.x, point.y
    session.close()
コード例 #5
0
ファイル: trader.py プロジェクト: magnito2/clownbot
 def delete_trade_model(self, buy_order_id):
     with create_session() as session:
         trade = session.query(Trade).filter_by(
             buy_order_id=buy_order_id).first()
         session.delete(trade)
         session.commit()
     return True
コード例 #6
0
ファイル: trader.py プロジェクト: magnito2/clownbot
 def update_asset_balances(self, assets):
     with create_session() as session:
         account_model = session.query(ExchangeAccount).filter_by(
             id=self.account_model_id).first()
         if not type(assets) == list:
             logger.error(f"assets must be a list")
             return
         for asset in assets:
             asset_name = asset['name']
             asset_model = session.query(Asset).filter_by(
                 exchange=self._exchange).filter_by(
                     exchange_account_id=self.account_model_id).filter_by(
                         name=asset_name).first()
             if asset_model:
                 asset_model.name = asset_name
                 asset_model.free = asset['free']
                 asset_model.locked = asset['locked']
                 asset_model.timestamp = datetime.utcnow()
             else:
                 asset_model = Asset(exchange=self._exchange,
                                     name=asset['name'],
                                     free=asset['free'],
                                     locked=asset['locked'])
                 account_model.assets.append(asset_model)
         asset_names = [asset['name'] for asset in assets]
         for asset in account_model.assets:
             if asset.name not in asset_names:
                 logger.info(f'Removing {asset.name}')
                 session.delete(asset)
         session.commit()
コード例 #7
0
ファイル: controllers.py プロジェクト: coreyabshire/ptolemy
def show_places():
    try:
        session = models.create_session(models.DB_URL)
        places = session.query(models.Place).order_by(models.Place.ptolemy_id)
        return render_template('main/show_entries.html', places=places)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #8
0
ファイル: trader.py プロジェクト: magnito2/clownbot
    def get_account_signal_assoc(self, signal_name=None, signal_id=None):
        """
        returns the relationship model between exchange account and the telegram signal
        from the exchange_account_signal pivot table.
        :param signal_name:
        :return:
        """
        if not signal_name and not signal_id:
            return None

        with create_session() as session:
            account_model = session.query(ExchangeAccount).filter_by(
                id=self.account_model_id).first()
            sig_assocs = account_model.signal_assoc

            if signal_name:
                wanted_assocs = [
                    assoc for assoc in sig_assocs
                    if assoc.signal.name == signal_name
                ]
            else:
                wanted_assocs = [
                    assoc for assoc in sig_assocs
                    if assoc.signal.id == signal_id
                ]
            wanted_assoc = wanted_assocs[0] if len(wanted_assocs) else None
            return wanted_assoc
コード例 #9
0
ファイル: controllers.py プロジェクト: virdi/ptolemy
def save_place(ptolemy_id):
    try:
        if request.form['submit'] == 'Submit':
            session = models.create_session(models.DB_URL)
            place = session.query(models.Place).get(ptolemy_id)
            place.ptolemy_name = request.form['ptolemy_name']
            place.modern_name = request.form['modern_name']
            ptolemy_lat, ptolemy_lon = [
                float(s) for s in request.form['ptolemy_coords'].split(' ')
            ]
            place.ptolemy_point = from_shape(Point(ptolemy_lon, ptolemy_lat))
            if len(request.form['modern_coords'].split(' ')) > 1:
                modern_lat, modern_lon = [
                    float(s) for s in request.form['modern_coords'].split(' ')
                ]
                place.modern_point = from_shape(Point(modern_lon, modern_lat))
            else:
                place.modern_point = None
            place.disposition = request.form['disposition']
            session.add(place)
            session.commit()
            session.close()
        return redirect(url_for('main.show_place', ptolemy_id=ptolemy_id))
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #10
0
ファイル: controllers.py プロジェクト: coreyabshire/ptolemy
def show_projects():
    try:
        session = models.create_session(models.DB_URL)
        projects = session.query(models.Project).order_by(models.Project.id)
        return render_template('main/show_projects.html', projects=projects)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #11
0
    def post_assets(self, balances):

        with create_session() as session:
            account = session.query(AccountModel).filter_by(
                api_key=self.api_key).first()
            account_assets = account.my_assets
            account_assets_names = [asset.name for asset in account_assets]
            for asset_params in balances:
                if not asset_params[
                        'asset'] in account_assets_names:  # asset is not yet created
                    asset = Asset(name=asset_params['asset'],
                                  free=asset_params['free'],
                                  fixed=asset_params['locked'],
                                  account=account)
                    session.add(asset)
                    continue
                for account_asset in account_assets:
                    if asset_params['asset'] == account_asset.name:
                        account_asset.free = asset_params['free']
                        account_asset.fixed = asset_params['locked']
                        session.add(account_asset)
            #do the reverse, check if there is an asset whose balance is zero
            balances_assets_names = [asset['asset'] for asset in balances]
            for asset_name in account_assets_names:
                if asset_name not in balances_assets_names:
                    asset = [
                        asset for asset in account_assets
                        if asset.name == asset_name
                    ]
                    if asset:
                        asset = asset[0]
                        session.delete(asset)
                        logger.info(f"asset {asset_name} has been deleted")
            session.commit()
コード例 #12
0
ファイル: controllers.py プロジェクト: coreyabshire/ptolemy
def show_project(project_slug):
    try:
        session = models.create_session(models.DB_URL)
        project = session.query(models.Project).filter_by(slug=project_slug).first()
        return render_template('main/show_project.html', project=project)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #13
0
ファイル: controllers.py プロジェクト: virdi/ptolemy
def show_places():
    try:
        session = models.create_session(models.DB_URL)
        places = session.query(models.Place).order_by(models.Place.ptolemy_id)
        return render_template('main/show_entries.html', places=places)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #14
0
ファイル: trader.py プロジェクト: magnito2/clownbot
 def _create_order_model(self, **kwargs):
     with create_session() as session:
         account_model = session.query(ExchangeAccount).filter_by(
             id=self.account_model_id).first()
         order = Order(**kwargs)
         account_model.orders.append(order)
         session.commit()
コード例 #15
0
ファイル: controllers.py プロジェクト: coreyabshire/ptolemy
def show_region(region_slug):
    try:
        session = models.create_session(models.DB_URL)
        region = session.query(models.Region).filter_by(slug=region_slug).first()
        return render_template('main/show_region.html', region=region)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #16
0
ファイル: controllers.py プロジェクト: virdi/ptolemy
def download_region(region_slug):
    try:
        session = models.create_session(models.DB_URL)
        region = session.query(
            models.Region).filter_by(slug=region_slug).first()
        outfile = StringIO.StringIO()
        writer = unicodecsv.writer(outfile)
        header = ('ptolemy_id', 'ptolemy_name', 'modern_name', 'ptolemy_lat',
                  'ptolemy_lon', 'modern_lat', 'modern_lon', 'disposition')
        writer.writerow(header)
        for place in region.places:
            ptolemy_coords = to_shape(place.ptolemy_point)
            ptolemy_lat = ptolemy_coords.y
            ptolemy_lon = ptolemy_coords.x
            if place.modern_point is not None:
                modern_coords = to_shape(place.modern_point)
                modern_lat = modern_coords.y
                modern_lon = modern_coords.x
            else:
                modern_coords = None
                modern_lat = None
                modern_lon = None
            row = (place.ptolemy_id, place.ptolemy_name, place.modern_name,
                   ptolemy_lat, ptolemy_lon, modern_lat, modern_lon,
                   place.disposition)
            writer.writerow(row)
        response = make_response(outfile.getvalue())
        response.headers[
            'Content-Disposition'] = 'attachment; filename=%s.csv' % (
                region.slug)
        response.headers['Content-type'] = 'text/csv'
        session.close()
        return response
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #17
0
ファイル: controllers.py プロジェクト: virdi/ptolemy
def show_projects():
    try:
        session = models.create_session(models.DB_URL)
        projects = session.query(models.Project).order_by(models.Project.id)
        return render_template('main/show_projects.html', projects=projects)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #18
0
    def main(self):

        self.session = create_session(self.project_name)

        self.symid = 1  # Symbol.next_symid()

        from simpleparse import parses

        self.parses = {}
        for k, v in parses.items():
            self.parses[k] = v(self.project_name, self.project_path)

        self.pathname_to_obj = {}

        self.init_files(self.start_path)

        t0 = time.time()
        # ctags 符号
        self.symbols(self.start_path)
        t1 = time.time()

        # sym ref
        self.symref(self.start_path)
        t2 = time.time()
        self.track_info['t1'] = int(t1 - t0)
        self.track_info['t2'] = int(t2 - t1)
        return self.track_info
コード例 #19
0
ファイル: trader.py プロジェクト: magnito2/clownbot
    def signal_can_buy(self, signal_name, symbol):
        logger.info(f"{self.username} Check {signal_name} can buy {symbol}")
        signal_assoc = self.get_account_signal_assoc(signal_name)
        if not signal_assoc or not signal_assoc.percent_investment:
            return True

        symbol_info = self.get_symbol_info(symbol)
        asset = async_to_sync(
            self.get_asset_models)(asset=symbol_info.quote_asset)
        if not asset:
            logger.error("Quote asset not found")
            return False
        logger.info(
            f"Asset is {asset.name}, free {asset.free}, locked {asset.locked}")

        with create_session() as session:
            trades_models = session.query(Trade).filter_by(
                exchange=self._exchange).filter_by(
                    exchange_account_id=self.account_model_id).all()
            open_trades_models = [
                trade for trade in trades_models
                if trade.sell_status != "FILLED" and trade.health != "ERROR"
            ]
            quote_trades_models = [
                trade_model for trade_model in open_trades_models
                if trade_model.quote_asset == symbol_info.quote_asset
            ]
            signal_trade_models = [
                trade_model for trade_model in quote_trades_models if
                trade_model.signal and trade_model.signal.name == signal_name
            ]
            held_portfolio = 0
            total_portfolio = 0
            for trade in signal_trade_models:
                buy_quote = trade.buy_price * trade.buy_quantity
                sell_quote = trade.sell_price * trade.sell_quantity
                held_portfolio += buy_quote - sell_quote

            for trade in quote_trades_models:
                buy_quote = trade.buy_price * trade.buy_quantity
                sell_quote = trade.sell_price * trade.sell_quantity
                total_portfolio += buy_quote - sell_quote
                total_portfolio += float(asset.free) + float(asset.locked)

            if not total_portfolio:
                return True

            if signal_assoc.percent_investment and held_portfolio / total_portfolio > signal_assoc.percent_investment:
                logger.info(
                    f"{self.username}, {signal_name} cannot buy {symbol}")
                return False
            else:
                logger.info(
                    f"{self.username}Percent investment: {signal_assoc.percent_investment}"
                )
                logger.info(f"Held portfolio: {held_portfolio}")
                logger.info(f"total portfolio: {total_portfolio}")
                logger.info(f"{self.username}, {signal_name} can buy {symbol}")
                return True
コード例 #20
0
ファイル: trader.py プロジェクト: magnito2/clownbot
 def update_portfolio_model(self, portfolio):
     with create_session() as session:
         portfolio_model = Portfolio(btc_value=portfolio)
         account_model = session.query(ExchangeAccount).filter_by(
             id=self.account_model_id).first()
         account_model.portfolio.append(portfolio_model)
         session.add(account_model)
         session.commit()
コード例 #21
0
ファイル: controllers.py プロジェクト: virdi/ptolemy
def show_project(project_slug):
    try:
        session = models.create_session(models.DB_URL)
        project = session.query(
            models.Project).filter_by(slug=project_slug).first()
        return render_template('main/show_project.html', project=project)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #22
0
ファイル: trader.py プロジェクト: magnito2/clownbot
 def delete_order_model(self, order_id):
     with create_session() as session:
         order = session.query(Order).filter_by(
             exchange=self._exchange).filter_by(order_id=order_id).first()
         if order:
             session.delete(order)
             session.commit()
             return True
コード例 #23
0
ファイル: controllers.py プロジェクト: virdi/ptolemy
def show_region(region_slug):
    try:
        session = models.create_session(models.DB_URL)
        region = session.query(
            models.Region).filter_by(slug=region_slug).first()
        return render_template('main/show_region.html', region=region)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #24
0
ファイル: __init__.py プロジェクト: xgrg/pluricent
 def __init__(self, filepath, create_database=False):
     ''' filepath refers to the database file pluricent.db'''
     import os.path as osp
     if not osp.isfile(filepath) and not create_database:
         raise Exception('%s must be an existing file' % filepath)
     if create_database:
         models.create_database(filepath, from_existing_repository=True)
     self.filepath = osp.abspath(filepath)
     self.session = models.create_session(filepath)
コード例 #25
0
ファイル: trader.py プロジェクト: magnito2/clownbot
 def get_trade_models(self):
     with create_session() as session:
         trades = session.query(Trade).filter_by(
             exchange=self._exchange).filter_by(
                 exchange_account_id=self.account_model_id).all()
         if trades:
             return trades
         else:
             return []
コード例 #26
0
ファイル: trader.py プロジェクト: magnito2/clownbot
 def get_trade_model(self, buy_order_id=None, sell_order_id=None):
     with create_session() as session:
         trade = None
         if buy_order_id:
             trade = session.query(Trade).filter_by(
                 buy_order_id=buy_order_id).first()
         elif sell_order_id:
             trade = session.query(Trade).filter_by(
                 sell_order_id=sell_order_id).first()
         return trade
コード例 #27
0
 def save(self, solution):
     session = create_session()
     new = Solution(nqueen=self.size, solution=str(solution))
     session.add(new)
     try:
         session.commit()
     except Exception as e:
         print("Commiting the solution: {}".format(e))
         session.rollback()
     session.close()
コード例 #28
0
 def __init__(self, username, password):
     # must get the user's email and password for the account that will send
     # the email.
     # mus also get the email template.
     self.session = create_session()
     self.username = username
     self.password = password
     self.rules = []
     self.counters = {}
     pass
コード例 #29
0
ファイル: __init__.py プロジェクト: magnito2/clownbot
    async def my_event_handler(self, event):
        try:
            text = str(event.raw_text)
            chat = await event.get_input_chat()

            signal = None

            for channel in self.signal_channels:
                if isinstance(chat, types.InputPeerUser):
                    if channel.channel_id == chat.user_id:
                        logger.info(
                            f"[+] Channel {channel.name} has recived a signal {text}"
                        )
                        signal = channel.process(text)
                        break
                elif isinstance(chat, types.InputPeerChannel):
                    if channel.channel_id == chat.channel_id:
                        logger.info(
                            f"[+] Channel {channel.name} has recived a signal {text}"
                        )
                        signal = channel.process(text)
                        break

            if signal:
                if signal['side'] == "BUY":
                    signal['trade_signal_id'] = None
                    with create_session() as session:
                        signaller = session.query(Signal).filter_by(
                            name=signal['signal_name']).first()
                        ts = TradeSignal(signal_name=signaller.name,
                                         exchange=signal['exchange'],
                                         symbol=signal['symbol'],
                                         side=signal['side'],
                                         price=signal['price'])
                        signaller.trade_signals.append(ts)
                        session.commit()
                        signal['trade_signal_id'] = ts.id
                        signal['signal_id'] = signaller.id
                        signal['trade_signal_name'] = signaller.short_name
                    if signal['exchange'] == "BINANCE":
                        logger.info(f"********Putting {signal} into queues")
                        for queue in self.binance_queues:
                            await queue.put(signal)
                    elif signal['exchange'] == "BITTREX":
                        logger.info(f"********Putting {signal} into queues")
                        for queue in self.bittrex_queues:
                            await queue.put(signal)
                else:
                    logger.debug(f"Signal not captured, signal is {signal}")
            else:
                pass
                #logger.error(f"[!] {text} is not a signal")
        except Exception as e:
            logger.exception(e)
コード例 #30
0
ファイル: test.py プロジェクト: MindHatter/otus-web-python
def test_create_user():
    engine = setup_db("test")
    session = create_session(engine)

    user = User()
    user.name = "Alex"
    session.add(user)
    session.commit()
    users = session.query(User).all()
    assert len(users) == 1

    drop_db(engine)
コード例 #31
0
ファイル: trader.py プロジェクト: magnito2/clownbot
 def _get_asset_models(self, asset=None):
     with create_session() as session:
         if asset:
             asset_model = session.query(Asset).filter_by(
                 exchange=self._exchange).filter_by(
                     exchange_account_id=self.account_model_id).filter_by(
                         name=asset).first()
             return asset_model
         asset_models = session.query(Asset).filter_by(
             exchange=self._exchange).filter_by(
                 exchange_account_id=self.account_model_id).all()
         return asset_models
コード例 #32
0
ファイル: controllers.py プロジェクト: virdi/ptolemy
def show_place(ptolemy_id):
    try:
        session = models.create_session(models.DB_URL)
        place = session.query(models.Place).get(ptolemy_id)
        place.ptolemy_coords = to_shape(place.ptolemy_point)
        if place.modern_point is not None:
            place.modern_coords = to_shape(place.modern_point)
        else:
            place.modern_coords = None
        return render_template('main/show_place.html', place=place)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #33
0
ファイル: controllers.py プロジェクト: coreyabshire/ptolemy
def show_place(ptolemy_id):
    try:
        session = models.create_session(models.DB_URL)
        place = session.query(models.Place).get(ptolemy_id)
        place.ptolemy_coords = to_shape(place.ptolemy_point)
        if place.modern_point is not None:
            place.modern_coords = to_shape(place.modern_point)
        else:
            place.modern_coords = None
        return render_template('main/show_place.html', place=place)
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #34
0
ファイル: trader.py プロジェクト: magnito2/clownbot
    def update_trade(self, **kwargs):
        with create_session() as session:
            side = kwargs.get('side')
            exchange_account_id = kwargs.get('exchange_account_id')
            buy_order_id = kwargs.get('buy_order_id')
            if not side:
                logger.error('Must provide a side')
                return
            del kwargs['side']

            if side == 'BUY':
                if not buy_order_id:
                    logger.error('[!] Include buy order id')
                    return
                trade = session.query(Trade).filter_by(
                    buy_order_id=str(buy_order_id)).first()
                if trade:
                    session.query(Trade).filter_by(
                        buy_order_id=str(buy_order_id)).update(kwargs)
                else:
                    if not exchange_account_id:
                        logger.error('[!] Include Exchange account Id')
                        return
                    trade = Trade(**kwargs)
                    session.add(trade)

            elif side == 'SELL':
                if buy_order_id:
                    trade = session.query(Trade).filter_by(
                        buy_order_id=str(buy_order_id)).first()
                    if trade:
                        session.query(Trade).filter_by(
                            buy_order_id=str(buy_order_id)).update(kwargs)
                    else:
                        logger.error(
                            f'Buy order of id {buy_order_id} not found')
                else:
                    sell_order_id = kwargs.get('sell_order_id')
                    if not sell_order_id:
                        logger.error('Must include sell order id')
                        return
                    trade = session.query(Trade).filter_by(
                        sell_order_id=str(sell_order_id)).first()
                    if trade:
                        session.query(Trade).filter_by(
                            sell_order_id=str(sell_order_id)).update(kwargs)
                    else:
                        logger.error(
                            'If sell is new, include buy order id to link it to a buy, else, include sell order it.\n '
                            'The provided sell order id was not found')
            session.commit()
コード例 #35
0
ファイル: trader.py プロジェクト: magnito2/clownbot
 def sync_get_order_model(self, client_order_id=None, order_id=None):
     with create_session() as session:
         if client_order_id:
             order = session.query(Order).filter_by(
                 exchange=self._exchange).filter_by(
                     client_order_id=client_order_id).first()
         elif order_id:
             order = session.query(Order).filter_by(
                 exchange=self._exchange).filter_by(
                     order_id=order_id).first()
         else:
             return None
         if order:
             return order
コード例 #36
0
 def post_error(self, e):
     error_params = {
         'exception_name': e.__class__.__name__,
         'message': e.message if hasattr(e, 'message') else f"{e}"
     }
     logger.error(f"{self.name} {e}")
     with create_session() as session:
         account = session.query(AccountModel).filter_by(
             api_key=self.api_key).first()
         error = ErrorModel(message=error_params['message'],
                            exception_name=error_params['exception_name'],
                            account=account)
         session.add(error)
         session.commit()
コード例 #37
0
ファイル: load_results_to_db.py プロジェクト: virdi/ptolemy
def main(places):
    valid_dispositions = ('known', 'unknown', 'tentative')
    try:
        #connection = psycopg2.connect("dbname='ptolemy' user='******' host='localhost' password='******' port='5433'")
        session = models.create_session()
        print 'connected'
        #cursor = connection.cursor()
        #cursor.execute('''DELETE FROM places''')
        #query = '''INSERT INTO places (ptolemy_id, ptolemy_name, modern_name, ptolemy_point, modern_point, disposition) VALUES (%s, %s, %s, ST_GeogFromText(%s), ST_GeogFromText(%s), %s)'''
        for index, row in places.iterrows():
            print index
            try:
                place = session.query(models.Place).get(row.ptol_id)
                if place == None:
                    print 'inserting %s' % (row.ptol_id)
                    place = models.Place()
                    place.ptolemy_id = row.ptol_id
                else:
                    print 'updating %s' % (row.ptol_id)

                place.ptolemy_name = row.ptol_name
                if isinstance(row.modern_name, basestring):
                    place.modern_name = row.modern_name
                else:
                    place.modern_name = None
                place.ptolemy_point = from_shape(
                    Point(row.ptol_lon, row.ptol_lat))
                if np.isnan(row.modern_lat) or np.isnan(row.modern_lon):
                    place.modern_point = None
                else:
                    place.modern_point = from_shape(
                        Point(row.modern_lon, row.modern_lat))
                if row.disposition not in valid_dispositions:
                    place.disposition = None
                else:
                    place.disposition = row.disposition

                session.add(place)

                #cursor.execute(query, place_data)
            except Exception as e:
                print 'unable to insert %s: %s' % (row.ptol_id, e.message)
        #connection.commit()
        #cursor.close()
        #connection.close()
        session.commit()
        session.close()
    except Exception as e:
        print 'unable to connect: %s' % (e.message, )
コード例 #38
0
def main(places):
    valid_dispositions = ('known', 'unknown', 'tentative')
    try:
        #connection = psycopg2.connect("dbname='ptolemy' user='******' host='localhost' password='******' port='5433'")
        session = models.create_session()
        print 'connected'
        #cursor = connection.cursor()
        #cursor.execute('''DELETE FROM places''')
        #query = '''INSERT INTO places (ptolemy_id, ptolemy_name, modern_name, ptolemy_point, modern_point, disposition) VALUES (%s, %s, %s, ST_GeogFromText(%s), ST_GeogFromText(%s), %s)'''
        for index, row in places.iterrows():
            print index
            try:
                place = session.query(models.Place).get(row.ptol_id)
                if place == None:
                    print 'inserting %s' % (row.ptol_id)
                    place = models.Place()
                    place.ptolemy_id = row.ptol_id
                else:
                    print 'updating %s' % (row.ptol_id)

                place.ptolemy_name = row.ptol_name
                if isinstance(row.modern_name, basestring):
                    place.modern_name = row.modern_name
                else:
                    place.modern_name = None
                place.ptolemy_point = from_shape(Point(row.ptol_lon, row.ptol_lat))
                if np.isnan(row.modern_lat) or np.isnan(row.modern_lon):
                    place.modern_point = None
                else:
                    place.modern_point = from_shape(Point(row.modern_lon, row.modern_lat))
                if row.disposition not in valid_dispositions:
                    place.disposition = None
                else:
                    place.disposition = row.disposition

                session.add(place)

                #cursor.execute(query, place_data)
            except Exception as e:
                print 'unable to insert %s: %s' % (row.ptol_id, e.message)
        #connection.commit()
        #cursor.close()
        #connection.close()
        session.commit()
        session.close()
    except Exception as e:
        print 'unable to connect: %s' % (e.message, )
コード例 #39
0
ファイル: controllers.py プロジェクト: coreyabshire/ptolemy
def save_place(ptolemy_id):
    try:
        if request.form['submit'] == 'Submit':
            session = models.create_session(models.DB_URL)
            place = session.query(models.Place).get(ptolemy_id)
            place.ptolemy_name = request.form['ptolemy_name']
            place.modern_name = request.form['modern_name']
            ptolemy_lat, ptolemy_lon = [float(s) for s in request.form['ptolemy_coords'].split(' ')]
            place.ptolemy_point = from_shape(Point(ptolemy_lon, ptolemy_lat))
            if len(request.form['modern_coords'].split(' ')) > 1:
                modern_lat, modern_lon = [float(s) for s in request.form['modern_coords'].split(' ')]
                place.modern_point = from_shape(Point(modern_lon, modern_lat))
            else:
                place.modern_point = None
            place.disposition = request.form['disposition']
            session.add(place)
            session.commit()
            session.close()
        return redirect(url_for('main.show_place', ptolemy_id=ptolemy_id))
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #40
0
ファイル: controllers.py プロジェクト: coreyabshire/ptolemy
def download_region(region_slug):
    try:
        session = models.create_session(models.DB_URL)
        region = session.query(models.Region).filter_by(slug=region_slug).first()
        outfile = StringIO.StringIO()
        writer = unicodecsv.writer(outfile)
        header = ('ptolemy_id', 'ptolemy_name', 'modern_name', 'ptolemy_lat', 'ptolemy_lon', 'modern_lat', 'modern_lon', 'disposition')
        writer.writerow(header)
        for place in region.places:
            ptolemy_coords = to_shape(place.ptolemy_point)
            ptolemy_lat = ptolemy_coords.y
            ptolemy_lon = ptolemy_coords.x
            if place.modern_point is not None:
                modern_coords = to_shape(place.modern_point)
                modern_lat = modern_coords.y
                modern_lon = modern_coords.x
            else:
                modern_coords = None
                modern_lat = None
                modern_lon = None
            row = (place.ptolemy_id,
                   place.ptolemy_name,
                   place.modern_name,
                   ptolemy_lat,
                   ptolemy_lon,
                   modern_lat,
                   modern_lon,
                   place.disposition)
            writer.writerow(row)
        response = make_response(outfile.getvalue())
        response.headers['Content-Disposition'] = 'attachment; filename=%s.csv' % (region.slug)
        response.headers['Content-type'] = 'text/csv'
        session.close()
        return response
    except Exception as e:
        current_app.logger.error(e.message, exc_info=e)
コード例 #41
0
ファイル: pipelines.py プロジェクト: BruceDone/scrapy_demo
 def __init__(self):
     self.session = models.create_session()
コード例 #42
0
from models import User

from models import create_session

from werkzeug.security import check_password_hash, generate_password_hash

session = create_session()

def set_password(password):
    return generate_password_hash(password)

def check_password(password1, password2):
    return check_password_hash(password1, password2)

def addUser(username, password):
    users = session.query(User).all()
    for user in users:
        if user.username == username:
            print('User already in DB')
            return
    pwd = set_password(password)
    user = User(username=username, password=pwd)
    session.begin()
    session.add(user)
    try:
        session.commit()
    except IntegrityError:
        session.rollback()
        print('Unable to add user to DB...')

    print('User successfully added to DB!')
コード例 #43
0
ファイル: firewall.py プロジェクト: GustavoFRGomes/heimdall
 def __init__(self):
     self.session = create_session()
     self.table = iptc.Table(iptc.Table.FILTER)
     self.chain = iptc.Chain(self.table, "INPUT")
     self.rules = None
     pass