<commit_before># Copyright 2015 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from abc import ABCMeta
from functools import partial
from numbers import Integral
from operator import getitem, itemgetter
import warnings

from logbook import Logger
import numpy as np
import pandas as pd
from pandas.tseries.tools import normalize_date
from six import with_metaclass, string_types
import sqlalchemy as sa
from toolz import compose

from zipline.errors import (
    MultipleSymbolsFound,
    RootSymbolNotFound,
    SidNotFound,
    SymbolNotFound,
    MapAssetIdentifierIndexError,
)
from zipline.assets import (
    Asset, Equity, Future,
)
from zipline.assets.asset_writer import (
    FUTURE_TABLE_FIELDS,
    EQUITY_TABLE_FIELDS,
)

log = Logger('assets.py')


class AssetFinder(object):

    def __init__(self, engine, allow_sid_assignment=True, fuzzy_char=None):

        self.fuzzy_char = fuzzy_char
        self.allow_sid_assignment = allow_sid_assignment

        self.engine = engine
        metadata = sa.MetaData(bind=engine)
        self.equities = equities = sa.Table(
            'equities',
            metadata,
            autoload_with=engine,
        )
        self.futures_exchanges = sa.Table(
            'futures_exchanges',
            metadata,
            autoload_with=engine,
        )
        self.futures_root_symbols = sa.Table(
            'futures_root_symbols',
            metadata,
            autoload_with=engine,
        )
        self.futures_contracts = futures_contracts = sa.Table(
            'futures_contracts',
            metadata,
            autoload_with=engine,
        )
        self.asset_router = sa.Table(
            'asset_router',
            metadata,
            autoload_with=engine,
        )

        # Create the equity and future queries once.
        _equity_sid = equities.c.sid
        _equity_by_sid = sa.select(
            tuple(map(partial(getitem, equities.c), EQUITY_TABLE_FIELDS)),
        )

        def select_equity_by_sid(sid):
            return _equity_by_sid.where(_equity_sid == int(sid))

        self.select_equity_by_sid = select_equity_by_sid

        _future_sid = futures_contracts.c.sid
        _future_by_sid = sa.select(
            tuple(map(
                partial(getitem, futures_contracts.c),
                FUTURE_TABLE_FIELDS,
            )),<commit_msg>ENH: Add autoload=True for compatibility with SQLAlchemy < 1.0.0<commit_after># Copyright 2015 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from abc import ABCMeta
from functools import partial
from numbers import Integral
from operator import getitem, itemgetter
import warnings

from logbook import Logger
import numpy as np
import pandas as pd
from pandas.tseries.tools import normalize_date
from six import with_metaclass, string_types
import sqlalchemy as sa
from toolz import compose

from zipline.errors import (
    MultipleSymbolsFound,
    RootSymbolNotFound,
    SidNotFound,
    SymbolNotFound,
    MapAssetIdentifierIndexError,
)
from zipline.assets import (
    Asset, Equity, Future,
)
from zipline.assets.asset_writer import (
    FUTURE_TABLE_FIELDS,
    EQUITY_TABLE_FIELDS,
)

log = Logger('assets.py')


class AssetFinder(object):

    def __init__(self, engine, allow_sid_assignment=True, fuzzy_char=None):

        self.fuzzy_char = fuzzy_char
        self.allow_sid_assignment = allow_sid_assignment

        self.engine = engine
        metadata = sa.MetaData(bind=engine)
        self.equities = equities = sa.Table(
            'equities',
            metadata,
            autoload=True,
            autoload_with=engine,
        )
        self.futures_exchanges = sa.Table(
            'futures_exchanges',
            metadata,
            autoload=True,
            autoload_with=engine,
        )
        self.futures_root_symbols = sa.Table(
            'futures_root_symbols',
            metadata,
            autoload=True,
            autoload_with=engine,
        )
        self.futures_contracts = futures_contracts = sa.Table(
            'futures_contracts',
            metadata,
            autoload=True,
            autoload_with=engine,
        )
        self.asset_router = sa.Table(
            'asset_router',
            metadata,
            autoload=True,
            autoload_with=engine,
        )

        # Create the equity and future queries once.
        _equity_sid = equities.c.sid
        _equity_by_sid = sa.select(
            tuple(map(partial(getitem, equities.c), EQUITY_TABLE_FIELDS)),
        )

        def select_equity_by_sid(sid):
            return _equity_by_sid.where(_equity_sid == int(sid))

        self.select_equity_by_sid = select_equity_by_sid

        _future_sid = futures_contracts.c.sid
        _future_by_sid = sa.select(
            tuple(map(
                partial(getitem, futures_contracts.c),
                FUTURE_TABLE_FIELDS,
            )),
{'fraction_non_alphanumeric': 0.06964586846543001, 'fraction_numerical': 0.0032040472175379428, 'mean_word_length': 2.538782816229117, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3678369', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1710, 'n_words': 458}