Python sqlalchemy.ext.declarative.DeclarativeMeta() Examples

The following are code examples for showing how to use sqlalchemy.ext.declarative.DeclarativeMeta(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: temporal-sqlalchemy   Author: CloverHealth   File: clock.py    BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def build_history_class(
        cls: declarative.DeclarativeMeta,
        prop: T_PROPS,
        schema: str = None) -> nine.Type[TemporalProperty]:
    """build a sqlalchemy model for given prop"""
    class_name = "%s%s_%s" % (cls.__name__, 'History', prop.key)
    table = build_history_table(cls, prop, schema)
    base_classes = (
        TemporalProperty,
        declarative.declarative_base(metadata=table.metadata),
    )
    class_attrs = {
        '__table__': table,
        'entity': orm.relationship(
            lambda: cls,
            backref=orm.backref('%s_history' % prop.key, lazy='dynamic'),
        ),
    }

    if isinstance(prop, orm.RelationshipProperty):
        class_attrs[prop.key] = orm.relationship(prop.argument, lazy='noload')

    model = type(class_name, base_classes, class_attrs)
    return model 
Example 2
Project: AppServer   Author: skytoup   File: alchemy_json_encoder.py    MIT License 6 votes vote down vote up
def decode(obj):
        if obj and isinstance(obj.__class__, DeclarativeMeta):
            fields = {}
            for field in [x for x in dir(obj) if not x.startswith('_') and not x.endswith('_') and x != 'metadata']:
                data = obj.__getattribute__(field)
                if isinstance(data, datetime.datetime):
                    fields[field] = data.timestamp()
                elif isinstance(data, datetime.date):
                    fields[field] = data.isoformat()
                elif isinstance(data, datetime.timedelta):
                    fields[field] = (datetime.datetime.min + data).time().isoformat()
                elif isinstance(data, int) or isinstance(data, float) or isinstance(data, str):
                    fields[field] = data
                elif isinstance(data, enum.Enum):
                    fields[field] = data.value
                elif isinstance(data.__class__, DeclarativeMeta):
                    fields[field] = AlchemyEncoder.decode(data)
                elif isinstance(data, list):
                    fields[field] = [AlchemyEncoder.decode(d) for d in data]
            return fields
        else:
            return obj 
Example 3
Project: eventsourcing   Author: johnbywater   File: datastore.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(
        self,
        settings: SQLAlchemySettings,
        base: DeclarativeMeta = Base,
        tables: Optional[Sequence] = None,
        connection_strategy: str = "plain",
        session: Optional[Union[Session, scoped_session]] = None,
    ):
        super(SQLAlchemyDatastore, self).__init__(settings=settings)
        self._was_session_created_here = False
        self._session = session
        if session:
            self._engine: Optional[Engine] = session.get_bind()
        else:
            self._engine = None
        self._base = base
        self._tables = tables
        self._connection_strategy = connection_strategy 
Example 4
Project: pulse-data   Author: Recidiviz   File: fl_aggregate_ingest.py    GNU General Public License v3.0 6 votes vote down vote up
def parse(location: str, filename: str) -> Dict[DeclarativeMeta, pd.DataFrame]:
    _setup()

    fl_county_table = _parse_county_table(location, filename)
    fl_county_table = fips.add_column_to_df(
        fl_county_table, fl_county_table.county_name, us.states.FL)

    # TODO(#689): Also set the facility_fips
    fl_facility_table = _parse_facility_table(location, filename)
    names = fl_facility_table.facility_name.apply(_pretend_facility_is_county)
    fl_facility_table = fips.add_column_to_df(
        fl_facility_table, names, us.states.FL)

    result = {
        FlCountyAggregate: fl_county_table,
        FlFacilityAggregate: fl_facility_table
    }

    date_scraped = _parse_date(filename)
    for table in result.values():
        table['report_date'] = date_scraped
        table['aggregation_window'] = enum_strings.monthly_granularity
        table['report_frequency'] = enum_strings.monthly_granularity

    return result 
Example 5
Project: pulse-data   Author: Recidiviz   File: fakes.py    GNU General Public License v3.0 6 votes vote down vote up
def use_on_disk_postgresql_database(declarative_base: DeclarativeMeta) -> None:
    """Creates a new SqlDatabase object used to communicate to an on-disk
    PostgreSQL database.

    This includes:
    1. Create all tables in the newly created sqlite database
    2. Bind the global SessionMaker to the new database engine

    Note, this assumes the following is true:
    - A local postgres server has been started
      (`pg_ctl -D /usr/local/var/postgres start`)
    - A DB named `recidiviz_test` exists (`createdb recidiviz_test`)
    - A User `usr` has been created with no password (`createdb usr`)
    """
    url = 'postgresql://usr:@localhost:5432/recidiviz_test'
    SQLAlchemyEngineManager.init_engine_for_db_instance(
        db_url=url, schema_base=declarative_base) 
Example 6
Project: zvdata   Author: zvtvz   File: contract.py    MIT License 6 votes vote down vote up
def get_schemas(provider: str) -> List[DeclarativeMeta]:
    """
    get domain schemas supported by the provider

    :param provider:
    :type provider:
    :return:
    :rtype:
    """
    schemas = []
    for provider1, dbs in _provider_map_dbnames.items():
        if provider == provider1:
            for dbname in dbs:
                schemas1 = _dbname_map_schemas.get(dbname)
                if schemas1:
                    schemas += schemas1
    return schemas 
Example 7
Project: xmusic-crawler   Author: rockers7414   File: serialize.py    Apache License 2.0 6 votes vote down vote up
def default(self, obj):
        fields = {}
        if isinstance(obj.__class__, DeclarativeMeta):
            # for sqlalchemy orm
            for key, value in obj.__dict__.items():
                if key.startswith('_'):
                    continue
                if(isinstance(value, list)):
                    inner_jsonObj = []
                    for _row in value:
                        inner_jsonObj.append(
                            self.default(_row))
                    fields[key] = inner_jsonObj
                else:
                    fields[key] = self.type_convert(value)
        else:
            # for raw sql
            for field in obj.keys():
                fields[field] = self.type_convert(
                    str(getattr(obj, field)))
        return fields 
Example 8
Project: k8s-redirectory   Author: kumina   File: database_actions.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def encode_model(model: DeclarativeMeta, parent_class: Any = None, expand: bool = False) -> dict:
    """
    Encodes a DB instance object of a given model into json

    Args:
        model: The DB model instance to serialize to json
        parent_class: A DB model might inherit from another DB model. Pass the parent class in order to be
                      serialized correctly
        expand: to include relationships or not

    Returns:
        a dictionary with basic data types that are all serializable
    """
    if parent_class is None:
        parent_class = (NoneType,)
    columns = model.__table__.columns.keys() + model.__mapper__.relationships.keys()
    return dict((c, _alchemy_encoder(getattr(model, c), parent_class + (model.__class__,))) for c in columns
                if not c.endswith('_id') and _check_instance(getattr(model, c), parent_class, expand)) 
Example 9
Project: k8s-redirectory   Author: kumina   File: database_actions.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _alchemy_encoder(obj: Any, obj_class: Any) -> Union[str, float, int, dict, list]:
    """
    JSON encoder function for SQLAlchemy special classes.

    Args:
        obj: Object to check
        obj_class: The class of the object passed

    Returns:
        Object formatted to acceptable json type
    """
    if isinstance(obj, list):
        return [_alchemy_encoder(i, obj_class) for i in obj] if not isinstance(obj[0], obj_class) else []
    elif isinstance(obj.__class__, DeclarativeMeta):
        return encode_model(obj, obj_class, expand=True)
    elif isinstance(obj, enum.Enum):
        return getattr(obj, 'name')
    elif isinstance(obj, date):
        return obj.isoformat()
    elif isinstance(obj, decimal.Decimal):
        return float(obj)
    else:
        return obj 
Example 10
Project: heron   Author: Eastwu5788   File: json_encoder.py    MIT License 6 votes vote down vote up
def default(self, obj):

        # 处理SQLAlchemy模型
        if isinstance(obj.__class__, DeclarativeMeta):
            fields = {}
            for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
                data = obj.__getattribute__(field)
                try:
                    json.dumps(data)
                    fields[field] = data
                except TypeError:
                    fields[field] = None

        # 处理datetime
        elif isinstance(obj, datetime.datetime):
            return obj.strftime("%Y-%m-%d %H:%M:%S")

        # 其它默认处理
        return json.JSONEncoder.default(self, obj) 
Example 11
Project: bio2bel   Author: bio2bel   File: abstract_manager.py    MIT License 6 votes vote down vote up
def _base(self) -> DeclarativeMeta:
        """Return the declarative base.

        It is usually sufficient to return an instance that is module-level.

        How to build an instance of :class:`sqlalchemy.ext.declarative.api.DeclarativeMeta` by using
        :func:`sqlalchemy.ext.declarative.declarative_base`:

        >>> from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
        >>> Base: DeclarativeMeta = declarative_base()

        Then just override this abstract property like:

        >>> @property
        >>> def _base(self) -> DeclarativeMeta:
        >>>     return Base

        Note that this property could effectively also be a static method.
        """ 
Example 12
Project: serialchemy   Author: ESSS   File: model_serializer.py    MIT License 6 votes vote down vote up
def dump(self, model):
        """
        Create a serialized dict from a Declarative model

        :param DeclarativeMeta model: the model to be serialized

        :rtype: dict
        """
        serial = {}
        for attr, field in self._fields.items():
            if field.load_only:
                continue
            value = getattr(model, attr, None)
            if field:
                self._assign_default_serializer(field, attr)
                serialized = field.dump(value)
            else:
                serialized = value
            serial[attr] = serialized
        return serial 
Example 13
Project: pythonlib   Author: RudolfCardinal   File: dump.py    Apache License 2.0 6 votes vote down vote up
def quick_mapper(table: Table) -> Type[DeclarativeMeta]:
    """
    Makes a new SQLAlchemy mapper for an existing table.
    See
    http://www.tylerlesmann.com/2009/apr/27/copying-databases-across-platforms-sqlalchemy/
    
    Args:
        table: SQLAlchemy :class:`Table` object

    Returns:
        a :class:`DeclarativeMeta` class

    """  # noqa
    # noinspection PyPep8Naming
    Base = declarative_base()

    class GenericMapper(Base):
        __table__ = table

    # noinspection PyTypeChecker
    return GenericMapper 
Example 14
Project: python-tools   Author: voidpp   File: alchemy_encoder.py    MIT License 5 votes vote down vote up
def default(self, obj):
        if isinstance(obj.__class__, DeclarativeMeta):
            # an SQLAlchemy class
            fields = {}
            for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
                data = obj.__getattribute__(field)
                try:
                    json.dumps(data) # this will fail on non-encodable values, like other classes
                    fields[field] = data
                except TypeError:
                    fields[field] = None
            # a json-encodable dict
            return fields

        return json.JSONEncoder.default(self, obj) 
Example 15
Project: backend   Author: bitex-coin   File: models.py    GNU General Public License v2.0 5 votes vote down vote up
def default(self, obj):
    if isinstance(obj.__class__, DeclarativeMeta):
      # an SQLAlchemy class
      fields = {}
      for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
        data = obj.__getattribute__(field)
        try:
          json.dumps(data) # this will fail on non-encodable values, like other classes
          fields[field] = data
        except TypeError:
          fields[field] = None
          # a json-encodable dict
      return fields

    return json.JSONEncoder.default(self, obj) 
Example 16
Project: recipe   Author: juiceinc   File: shelf.py    MIT License 5 votes vote down vote up
def find_column(selectable, name):
    """
    Find a column named `name` in selectable

    :param selectable:
    :param name:
    :return: A column object
    """
    from recipe import Recipe

    if isinstance(selectable, Recipe):
        selectable = selectable.subquery()

    # Selectable is a table
    if isinstance(selectable, DeclarativeMeta):
        col = getattr(selectable, name, None)
        if col is not None:
            return col

        col = _find_in_columncollection(selectable.__table__.columns, name)
        if col is not None:
            return col

    # Selectable is a sqlalchemy subquery
    elif hasattr(selectable, "c") and isinstance(
        selectable.c, ImmutableColumnCollection
    ):
        col = getattr(selectable.c, name, None)
        if col is not None:
            return col

        col = _find_in_columncollection(selectable.c, name)
        if col is not None:
            return col

    raise BadIngredient("Can not find {} in {}".format(name, selectable)) 
Example 17
Project: safrs   Author: thomaxxl   File: json_encoder.py    MIT License 5 votes vote down vote up
def default(self, obj, **kwargs):
        """
            override the default json encoding
            :param obj: object to be encoded
            :return: encoded/serizlaized object
        """
        if isinstance(obj, SAFRSBase):
            result = obj._s_jsonapi_encode()
            return result
        if isinstance(obj, datetime.datetime):
            return obj.isoformat(" ")
        if isinstance(obj, datetime.date):
            return obj.isoformat()
        if isinstance(obj, SAFRSDummy):
            return {}
        if isinstance(obj, set):
            return list(obj)
        if isinstance(obj, DeclarativeMeta):
            return self.sqla_encode(obj)
        if isinstance(obj, SAFRSFormattedResponse):
            return obj.to_dict()
        if isinstance(obj, decimal.Decimal):
            return str(obj)
        if isinstance(obj, bytes):
            safrs.log.warning("bytes obj, TODO")

        # We shouldn't get here in a normal setup
        # getting here means we already abused safrs... and we're no longer jsonapi compliant
        if not is_debug():
            # only continue if in debug mode
            safrs.log.warning('JSON Encoding Error: Unknown object type "{}" for {}'.format(type(obj), obj))
            return {"error": "invalid object"}

        return self.ghetto_encode(obj) 
Example 18
Project: IBATS_HuobiTrader_old   Author: mmmaaaggg   File: db_utils.py    GNU General Public License v3.0 5 votes vote down vote up
def default(self, obj):
        # print("obj.__class__", obj.__class__, "isinstance(obj.__class__, DeclarativeMeta)", isinstance(obj.__class__, DeclarativeMeta))
        if isinstance(obj.__class__, DeclarativeMeta):
            # an SQLAlchemy class
            fields = {}
            for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
                data = obj.__getattribute__(field)
                try:
                    json.dumps(data)     # this will fail on non-encodable values, like other classes
                    fields[field] = data
                except TypeError:    # 添加了对datetime的处理
                    print(data)
                    if isinstance(data, datetime):
                        fields[field] = data.isoformat()
                    elif isinstance(data, date):
                        fields[field] = data.isoformat()
                    elif isinstance(data, timedelta):
                        fields[field] = (datetime.min + data).time().isoformat()
                    else:
                        fields[field] = None
            # a json-encodable dict
            return fields
        elif isinstance(obj, date):
            return json.dumps(date_2_str(obj))

        return json.JSONEncoder.default(self, obj) 
Example 19
Project: pulse-data   Author: Recidiviz   File: session_factory.py    GNU General Public License v3.0 5 votes vote down vote up
def for_schema_base(cls, schema_base: DeclarativeMeta) -> Session:
        engine = SQLAlchemyEngineManager.get_engine_for_schema_base(schema_base)
        if engine is None:
            raise ValueError(f"No engine set for base [{schema_base.__name__}]")

        return Session(bind=engine) 
Example 20
Project: pulse-data   Author: Recidiviz   File: schema_utils.py    GNU General Public License v3.0 5 votes vote down vote up
def schema_base_for_system_level(system_level: SystemLevel) -> DeclarativeMeta:
    if system_level == SystemLevel.STATE:
        return StateBase
    if system_level == SystemLevel.COUNTY:
        return JailsBase

    raise ValueError(f"Unsupported SystemLevel type: {system_level}") 
Example 21
Project: pulse-data   Author: Recidiviz   File: schema_utils.py    GNU General Public License v3.0 5 votes vote down vote up
def schema_base_for_schema_module(module: ModuleType) -> DeclarativeMeta:
    if module in (aggregate_schema, county_schema):
        return JailsBase
    if module == state_schema:
        return StateBase

    raise ValueError(f"Unsupported module: {module}") 
Example 22
Project: pulse-data   Author: Recidiviz   File: sqlalchemy_engine_manager.py    GNU General Public License v3.0 5 votes vote down vote up
def init_engine_for_db_instance(
            cls, db_url: str, schema_base: DeclarativeMeta) -> None:
        engine = sqlalchemy.create_engine(db_url)
        schema_base.metadata.create_all(engine)
        cls._engine_for_schema[schema_base] = engine 
Example 23
Project: pulse-data   Author: Recidiviz   File: sqlalchemy_engine_manager.py    GNU General Public License v3.0 5 votes vote down vote up
def get_engine_for_schema_base(
            cls, schema_base: DeclarativeMeta) -> Optional[Engine]:
        return cls._engine_for_schema.get(schema_base, None) 
Example 24
Project: pulse-data   Author: Recidiviz   File: dao.py    GNU General Public License v3.0 5 votes vote down vote up
def _write_df_only_successful_rows(
        table: DeclarativeMeta, df: pd.DataFrame) -> None:
    """If the dataframe can't be written all at once (eg. some rows already
    exist in the database) then we write only the rows that we can."""
    for i in range(len(df)):
        row = df.iloc[i:i + 1]
        try:
            row.to_sql(table.__tablename__,
                       SQLAlchemyEngineManager.get_engine_for_schema_base(
                           JailsBase),
                       if_exists='append',
                       index=False)
        except IntegrityError:
            # Skip rows that can't be written
            logging.info("Skipping write_df to %s table: %s.", table, row) 
Example 25
Project: pulse-data   Author: Recidiviz   File: ny_aggregate_ingest.py    GNU General Public License v3.0 5 votes vote down vote up
def parse(location: str, filename: str) -> Dict[DeclarativeMeta, pd.DataFrame]:
    _setup()

    table = _parse_table(location, filename)

    # Fuzzy match each facility_name to a county fips
    county_names = table.facility_name.map(_pretend_facility_is_county)
    table = fips.add_column_to_df(table, county_names, us.states.NY)

    table['aggregation_window'] = enum_strings.monthly_granularity
    table['report_frequency'] = enum_strings.monthly_granularity

    return {
        NyFacilityAggregate: table
    } 
Example 26
Project: pulse-data   Author: Recidiviz   File: tx_aggregate_ingest.py    GNU General Public License v3.0 5 votes vote down vote up
def parse(location: str, filename: str) -> Dict[DeclarativeMeta, pd.DataFrame]:
    report_date = _parse_date(filename)
    table = _parse_table(location, filename, report_date)

    county_names = table.facility_name.map(_pretend_facility_is_county)
    table = fips.add_column_to_df(table, county_names, us.states.TX)

    table['report_date'] = report_date
    table['aggregation_window'] = enum_strings.daily_granularity
    table['report_frequency'] = enum_strings.monthly_granularity

    return {
        TxCountyAggregate: table
    } 
Example 27
Project: pulse-data   Author: Recidiviz   File: ga_aggregate_ingest.py    GNU General Public License v3.0 5 votes vote down vote up
def parse(location: str, filename: str) -> Dict[DeclarativeMeta, pd.DataFrame]:
    table = _parse_table(location, filename)

    # Fuzzy match each facility_name to a county fips
    county_names = table.county_name.map(_sanitize_county_name)
    table = fips.add_column_to_df(table, county_names, us.states.GA)

    table['report_date'] = _parse_date(filename)
    table['aggregation_window'] = enum_strings.daily_granularity
    table['report_frequency'] = enum_strings.monthly_granularity

    return {
        GaCountyAggregate: table
    } 
Example 28
Project: pulse-data   Author: Recidiviz   File: pa_aggregate_ingest.py    GNU General Public License v3.0 5 votes vote down vote up
def parse(_, filename: str) -> Dict[DeclarativeMeta, pd.DataFrame]:
    table_1 = _parse_tab_1(filename)
    table_2 = _parse_tab_2(filename)

    return {
        PaFacilityPopAggregate: table_1,
        PaCountyPreSentencedAggregate: table_2
    } 
Example 29
Project: pulse-data   Author: Recidiviz   File: hi_aggregate_ingest.py    GNU General Public License v3.0 5 votes vote down vote up
def parse(location: str, filename: str) -> Dict[DeclarativeMeta, pd.DataFrame]:
    table = _parse_table(location, filename)

    table['report_date'] = parse_date(filename)
    table['aggregation_window'] = enum_strings.daily_granularity
    table['report_frequency'] = enum_strings.monthly_granularity

    return {
        HiFacilityAggregate: table
    } 
Example 30
Project: pulse-data   Author: Recidiviz   File: ky_aggregate_ingest.py    GNU General Public License v3.0 5 votes vote down vote up
def parse(location: str, filename: str) -> Dict[DeclarativeMeta, pd.DataFrame]:
    table = _parse_table(location, filename)

    # Fuzzy match each facility_name to a county fips
    county_names = table.facility_name.map(_pretend_facility_is_county)
    table = fips.add_column_to_df(table, county_names, us.states.KY)

    table['report_date'] = parse_date(filename)
    table['aggregation_window'] = enum_strings.daily_granularity
    table['report_frequency'] = enum_strings.weekly_granularity

    return {
        KyFacilityAggregate: table
    } 
Example 31
Project: pulse-data   Author: Recidiviz   File: ca_aggregate_ingest.py    GNU General Public License v3.0 5 votes vote down vote up
def parse(_, filename: str) -> Dict[DeclarativeMeta, pd.DataFrame]:
    table = _parse_table(filename)

    county_names = table.jurisdiction_name.map(_pretend_jurisdiction_is_county)
    table = fips.add_column_to_df(table, county_names, us.states.CA)

    table['aggregation_window'] = enum_strings.monthly_granularity
    table['report_frequency'] = enum_strings.monthly_granularity

    return {
        CaFacilityAggregate: table
    } 
Example 32
Project: pulse-data   Author: Recidiviz   File: fakes.py    GNU General Public License v3.0 5 votes vote down vote up
def use_in_memory_sqlite_database(declarative_base: DeclarativeMeta) -> None:
    """Creates a new SqlDatabase object used to communicate to a fake in-memory
    sqlite database.

    This includes:
    1. Creates a new in memory sqlite database engine
    2. Create all tables in the newly created sqlite database
    3. Bind the global SessionMaker to the new fake database engine
    """

    SQLAlchemyEngineManager.init_engine_for_db_instance(
        db_url='sqlite:///:memory:',
        schema_base=declarative_base) 
Example 33
Project: pulse-data   Author: Recidiviz   File: base_state_direct_ingest_controller_tests.py    GNU General Public License v3.0 5 votes vote down vote up
def schema_base(cls) -> DeclarativeMeta:
        return StateBase 
Example 34
Project: pulse-data   Author: Recidiviz   File: base_direct_ingest_controller_tests.py    GNU General Public License v3.0 5 votes vote down vote up
def schema_base(cls) -> DeclarativeMeta:
        pass 
Example 35
Project: pulse-data   Author: Recidiviz   File: us_tx_brazos_controller_test.py    GNU General Public License v3.0 5 votes vote down vote up
def schema_base(cls) -> DeclarativeMeta:
        return JailsBase 
Example 36
Project: zvdata   Author: zvtvz   File: contract.py    MIT License 5 votes vote down vote up
def table_name_to_domain_name(table_name: str) -> DeclarativeMeta:
    """
    the rules for table_name -> domain_class

    :param table_name:
    :type table_name:
    :return:
    :rtype:
    """
    parts = table_name.split('_')
    domain_name = ''
    for part in parts:
        domain_name = domain_name + part.capitalize()
    return domain_name 
Example 37
Project: zvdata   Author: zvtvz   File: contract.py    MIT License 5 votes vote down vote up
def get_db_name(data_schema: DeclarativeMeta) -> str:
    """
    get db name of the domain schema

    :param data_schema:
    :type data_schema:
    :return:
    :rtype:
    """
    for db_name, base in _dbname_map_base.items():
        if issubclass(data_schema, base):
            return db_name 
Example 38
Project: zvdata   Author: zvtvz   File: contract.py    MIT License 5 votes vote down vote up
def get_schema_by_name(name: str) -> DeclarativeMeta:
    """
    get domain schema by the name

    :param name:
    :type name:
    :return:
    :rtype:
    """
    for schema in global_schemas:
        if schema.__name__ == name:
            return schema 
Example 39
Project: zvdata   Author: zvtvz   File: contract.py    MIT License 5 votes vote down vote up
def get_schema_columns(schema: DeclarativeMeta) -> object:
    """
    get all columns of the domain schema

    :param schema:
    :type schema:
    :return:
    :rtype:
    """
    return schema.__table__.columns.keys() 
Example 40
Project: planespotter   Author: yfauser   File: __init__.py    MIT License 5 votes vote down vote up
def make_declarative_base(self, model, metadata=None):
        """Creates the declarative base that all models will inherit from.

        :param model: base model class (or a tuple of base classes) to pass
            to :func:`~sqlalchemy.ext.declarative.declarative_base`. Or a class
            returned from ``declarative_base``, in which case a new base class
            is not created.
        :param: metadata: :class:`~sqlalchemy.MetaData` instance to use, or
            none to use SQLAlchemy's default.

        .. versionchanged 2.3.0::
            ``model`` can be an existing declarative base in order to support
            complex customization such as changing the metaclass.
        """
        if not isinstance(model, DeclarativeMeta):
            model = declarative_base(
                cls=model,
                name='Model',
                metadata=metadata,
                metaclass=DefaultMeta
            )

        # if user passed in a declarative base and a metaclass for some reason,
        # make sure the base uses the metaclass
        if metadata is not None and model.metadata is not metadata:
            model.metadata = metadata

        if not getattr(model, 'query_class', None):
            model.query_class = self.Query

        model.query = _QueryProperty(self)
        return model 
Example 41
Project: planespotter   Author: yfauser   File: model.py    MIT License 5 votes vote down vote up
def should_set_tablename(cls):
    """Determine whether ``__tablename__`` should be automatically generated
    for a model.

    * If no class in the MRO sets a name, one should be generated.
    * If a declared attr is found, it should be used instead.
    * If a name is found, it should be used if the class is a mixin, otherwise
      one should be generated.
    * Abstract models should not have one generated.

    Later, :meth:`._BoundDeclarativeMeta.__table_cls__` will determine if the
    model looks like single or joined-table inheritance. If no primary key is
    found, the name will be unset.
    """
    if (
        cls.__dict__.get('__abstract__', False)
        or not any(isinstance(b, DeclarativeMeta) for b in cls.__mro__[1:])
    ):
        return False

    for base in cls.__mro__:
        if '__tablename__' not in base.__dict__:
            continue

        if isinstance(base.__dict__['__tablename__'], declared_attr):
            return False

        return not (
            base is cls
            or base.__dict__.get('__abstract__', False)
            or not isinstance(base, DeclarativeMeta)
        )

    return True 
Example 42
Project: k8s-redirectory   Author: kumina   File: database_actions.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _check_instance(model, parent, expand):
    if isinstance(model.__class__, DeclarativeMeta) and not expand:
        return False
    if isinstance(model, list):
        if len(model) == 0:
            return False
        if isinstance(model[0].__class__, DeclarativeMeta) and not expand:
            return False
        return not isinstance(model[0], parent)
    return not isinstance(model, parent) 
Example 43
Project: plataforma-livre-dados-abertos   Author: pbaesse   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def make_declarative_base(self, model, metadata=None):
        """Creates the declarative base that all models will inherit from.

        :param model: base model class (or a tuple of base classes) to pass
            to :func:`~sqlalchemy.ext.declarative.declarative_base`. Or a class
            returned from ``declarative_base``, in which case a new base class
            is not created.
        :param metadata: :class:`~sqlalchemy.MetaData` instance to use, or
            none to use SQLAlchemy's default.

        .. versionchanged 2.3.0::
            ``model`` can be an existing declarative base in order to support
            complex customization such as changing the metaclass.
        """
        if not isinstance(model, DeclarativeMeta):
            model = declarative_base(
                cls=model,
                name='Model',
                metadata=metadata,
                metaclass=DefaultMeta
            )

        # if user passed in a declarative base and a metaclass for some reason,
        # make sure the base uses the metaclass
        if metadata is not None and model.metadata is not metadata:
            model.metadata = metadata

        if not getattr(model, 'query_class', None):
            model.query_class = self.Query

        model.query = _QueryProperty(self)
        return model 
Example 44
Project: plataforma-livre-dados-abertos   Author: pbaesse   File: model.py    GNU General Public License v3.0 5 votes vote down vote up
def should_set_tablename(cls):
    """Determine whether ``__tablename__`` should be automatically generated
    for a model.

    * If no class in the MRO sets a name, one should be generated.
    * If a declared attr is found, it should be used instead.
    * If a name is found, it should be used if the class is a mixin, otherwise
      one should be generated.
    * Abstract models should not have one generated.

    Later, :meth:`._BoundDeclarativeMeta.__table_cls__` will determine if the
    model looks like single or joined-table inheritance. If no primary key is
    found, the name will be unset.
    """
    if (
        cls.__dict__.get('__abstract__', False)
        or not any(isinstance(b, DeclarativeMeta) for b in cls.__mro__[1:])
    ):
        return False

    for base in cls.__mro__:
        if '__tablename__' not in base.__dict__:
            continue

        if isinstance(base.__dict__['__tablename__'], declared_attr):
            return False

        return not (
            base is cls
            or base.__dict__.get('__abstract__', False)
            or not isinstance(base, DeclarativeMeta)
        )

    return True 
Example 45
Project: python_entity_framework   Author: Enforcer   File: test_nested_value_objects.py    MIT License 5 votes vote down vote up
def sa_repo(sa_base: DeclarativeMeta) -> Type[Union[SqlAlchemyRepo, BoardRepo]]:
    class SaBoardRepo(SqlAlchemyRepo, BoardRepo):
        base = sa_base
        registry = SaRegistry()

    return SaBoardRepo 
Example 46
Project: python_entity_framework   Author: Enforcer   File: conftest.py    MIT License 5 votes vote down vote up
def sa_base() -> DeclarativeMeta:
    return declarative_base() 
Example 47
Project: python_entity_framework   Author: Enforcer   File: conftest.py    MIT License 5 votes vote down vote up
def session(sa_base: DeclarativeMeta, engine: Engine) -> Generator[Session, None, None]:
    sa_base.metadata.drop_all(engine)
    sa_base.metadata.create_all(engine)
    session_factory = sessionmaker(engine)
    yield session_factory()
    session_factory.close_all()
    sa_base.metadata.drop_all(engine) 
Example 48
Project: python_entity_framework   Author: Enforcer   File: test_one_level_nested_entities.py    MIT License 5 votes vote down vote up
def sa_repo(sa_base: DeclarativeMeta) -> Type[Union[SqlAlchemyRepo, SubscriberRepo]]:
    class SqlSubscriberRepo(SqlAlchemyRepo, SubscriberRepo):
        base = sa_base
        registry = SaRegistry()

    return SqlSubscriberRepo 
Example 49
Project: python_entity_framework   Author: Enforcer   File: __init__.py    MIT License 5 votes vote down vote up
def prepare(cls, entity_cls: Type[EntityType]) -> None:
        assert cls.base, "Must set cls base to an instance of DeclarativeMeta!"
        if not getattr(cls, "entity", None):
            cls.entity = entity_cls
            aet = cls.registry.entities_to_aets[entity_cls]
            ModelConstructingVisitor(cls.base, cls.registry).traverse_from(aet.root) 
Example 50
Project: serialchemy   Author: ESSS   File: model_serializer.py    MIT License 5 votes vote down vote up
def __init__(self, model_class, nest_foreign_keys=False):
        """
        :param Type[DeclarativeMeta] model_class: the SQLAlchemy mapping class to be serialized
        """
        self._model_class = model_class
        self._fields = self._get_declared_fields()
        self._initialize_fields(nest_foreign_keys) 
Example 51
Project: serialchemy   Author: ESSS   File: model_serializer.py    MIT License 5 votes vote down vote up
def load(self, serialized, existing_model=None, session=None):
        """
        Initialize a Declarative model from a serialized dict

        :param dict serialized: the serialized object.

        :param None|DeclarativeMeta existing_model: If given, the model will be updated with the serialized data.

        :param None|Session session: a SQLAlchemy session. Used only to load nested models
        """
        from .nested_fields import SessionBasedField

        if existing_model:
            model = existing_model
        else:
            model = self._create_model(serialized)
            assert model is not None, "ModelSerializer._create_model cannot return None"
        for field_name, value in serialized.items():
            if field_name not in self._fields:
                warnings.warn(f"Field '{field_name}' not defined for {self._model_class.__name__}")
                continue
            field = self._fields[field_name]
            if field.dump_only:
                continue
            if field.creation_only and existing_model:
                continue
            self._assign_default_serializer(field, field_name)
            if isinstance(field, SessionBasedField):
                deserialized = field.load(value, session=session)
            else:
                deserialized = field.load(value)
            setattr(model, field_name, deserialized)
        return model 
Example 52
Project: serialchemy   Author: ESSS   File: model_serializer.py    MIT License 5 votes vote down vote up
def _create_model(self, serialized):
        """
        Can be overridden in a derived class to customize model initialization.

        :param dict serialized: the serialized object

        :rtype: DeclarativeMeta
        """
        return self.model_class() 
Example 53
Project: sqlalchemy-repr   Author: manicmaniac   File: sqlalchemy_repr.py    MIT License 5 votes vote down vote up
def repr(self, obj):
        if isinstance(obj.__class__, DeclarativeMeta):
            return self.repr_Base(obj, self.maxlevel)
        if sys.version_info < (3,):
            return _Repr.repr(self, obj)
        else:
            return super(Repr, self).repr(obj) 
Example 54
Project: sidr   Author: oiime   File: model.py    GNU General Public License v2.0 5 votes vote down vote up
def json_encoder(export_fields):
    class AlchemyEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj.__class__, DeclarativeMeta):
                fields = {}
                for field in [x for x in dir(obj) if not x.startswith('_') and x in export_fields]:
                    data = obj.__getattribute__(field)
                    if type(data).__name__ in json_encoders:
                        data = json_encoders[type(data).__name__](data)
                    fields[field] = data
                return fields

            return json.JSONEncoder.default(self, obj)
    return AlchemyEncoder 
Example 55
Project: wsgicli   Author: kobinpy   File: wsgicli.py    MIT License 5 votes vote down vote up
def _sqlalchemy_model():
    from sqlalchemy.ext.declarative import DeclarativeMeta
    from sqlalchemy.orm import sessionmaker
    return [sessionmaker, DeclarativeMeta] 
Example 56
Project: fakeit   Author: RCheese   File: utils.py    MIT License 5 votes vote down vote up
def _tableconvert(func):
    @wraps(func)
    def wrapped(cls_or_rbl, *args, **kwargs):
        if isinstance(cls_or_rbl, Table):
            table = cls_or_rbl
        elif isinstance(cls_or_rbl, DeclarativeMeta):
            table = cls_or_rbl.__table__
        else:
            raise TypeError
        return func(table, *args, **kwargs)

    return wrapped 
Example 57
Project: Full-Stack-Application-Development   Author: Bolton-and-Menk-GIS   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def make_declarative_base(self, model, metadata=None):
        """Creates the declarative base that all models will inherit from.

        :param model: base model class (or a tuple of base classes) to pass
            to :func:`~sqlalchemy.ext.declarative.declarative_base`. Or a class
            returned from ``declarative_base``, in which case a new base class
            is not created.
        :param: metadata: :class:`~sqlalchemy.MetaData` instance to use, or
            none to use SQLAlchemy's default.

        .. versionchanged 2.3.0::
            ``model`` can be an existing declarative base in order to support
            complex customization such as changing the metaclass.
        """
        if not isinstance(model, DeclarativeMeta):
            model = declarative_base(
                cls=model,
                name='Model',
                metadata=metadata,
                metaclass=DefaultMeta
            )

        # if user passed in a declarative base and a metaclass for some reason,
        # make sure the base uses the metaclass
        if metadata is not None and model.metadata is not metadata:
            model.metadata = metadata

        if not getattr(model, 'query_class', None):
            model.query_class = self.Query

        model.query = _QueryProperty(self)
        return model 
Example 58
Project: Full-Stack-Application-Development   Author: Bolton-and-Menk-GIS   File: model.py    GNU General Public License v3.0 5 votes vote down vote up
def should_set_tablename(cls):
    """Determine whether ``__tablename__`` should be automatically generated
    for a model.

    * If no class in the MRO sets a name, one should be generated.
    * If a declared attr is found, it should be used instead.
    * If a name is found, it should be used if the class is a mixin, otherwise
      one should be generated.
    * Abstract models should not have one generated.

    Later, :meth:`._BoundDeclarativeMeta.__table_cls__` will determine if the
    model looks like single or joined-table inheritance. If no primary key is
    found, the name will be unset.
    """
    if (
        cls.__dict__.get('__abstract__', False)
        or not any(isinstance(b, DeclarativeMeta) for b in cls.__mro__[1:])
    ):
        return False

    for base in cls.__mro__:
        if '__tablename__' not in base.__dict__:
            continue

        if isinstance(base.__dict__['__tablename__'], declared_attr):
            return False

        return not (
            base is cls
            or base.__dict__.get('__abstract__', False)
            or not isinstance(base, DeclarativeMeta)
        )

    return True 
Example 59
Project: temporal-sqlalchemy   Author: CloverHealth   File: clock.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def build_history_table(
        cls: declarative.DeclarativeMeta,
        prop: T_PROPS,
        schema: str = None) -> sa.Table:
    """build a sqlalchemy history table for given prop"""
    if isinstance(prop, orm.RelationshipProperty):
        columns = [util.copy_column(column) for column in prop.local_columns]
    else:
        columns = [util.copy_column(column) for column in prop.columns]

    local_table = cls.__table__
    history_table_name = _generate_history_table_name(local_table, columns)
    table_name = util.truncate_identifier(history_table_name)
    # Build the foreign key(s), specifically adding an index since we may use
    # a casted foreign key in our constraints. See _exclusion_in_uuid
    entity_foreign_keys = list(util.foreign_key_to(local_table, index=True))
    entity_constraints = [
        _exclusion_in(fk.type, fk.key)
        for fk in entity_foreign_keys
    ]

    constraints = [
        sa.Index(
            util.truncate_identifier('%s_effective_idx' % table_name),
            'effective',
            postgresql_using='gist',
        ),
        sap.ExcludeConstraint(
            *itertools.chain(entity_constraints, [('vclock', '&&')]),
            name=util.truncate_identifier('%s_excl_vclock' % table_name),
        ),
        sap.ExcludeConstraint(
            *itertools.chain(entity_constraints, [('effective', '&&')]),
            name=util.truncate_identifier('%s_excl_effective' % table_name),
        ),
    ]

    return sa.Table(
        table_name,
        local_table.metadata,
        sa.Column('id',
                  sap.UUID(as_uuid=True),
                  default=uuid.uuid4,
                  primary_key=True),
        sa.Column('effective',
                  sap.TSTZRANGE,
                  default=util.effective_now,
                  nullable=False),
        sa.Column('vclock', sap.INT4RANGE, nullable=False),
        *itertools.chain(entity_foreign_keys, columns, constraints),
        schema=schema or local_table.schema,
        keep_existing=True,
    )  # memoization ftw 
Example 60
Project: k8s-redirectory   Author: kumina   File: hs_actions.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def get_expressions_ids_flags(db_model: DeclarativeMeta,
                              expression_path: str,
                              expression_regex_path: str,
                              id_path: str,
                              combine_expr_with: str = None) -> Tuple[List[bytes], List[int], List[int]]:
    """
    Gets the expression in the correct format from the database.
    Depending on the arguments the expression can be combined with another piece
    of data. The expression will also be regex escaped if it is a literal.
    If the expression is a regex then a second check will be conducted which checks if the expression
    matches an empty string. If so a different flag than the default is applied.

    Args:
        db_model: The model/table of the current database
        expression_path: The attribute where the expression can be found in the model
        expression_regex_path: The attribute holding the value if an expression is regex or not
        id_path: The attribute where the id can be found
        combine_expr_with: The attribute of extra piece of data that can be appended before the expression

    Returns:
        a tuple containing the expressions, the ids and the flags. tuple(expressions, ids, flags)
    """
    expressions = []
    ids = []
    flags = []

    db_session = DatabaseManager().get_session()

    for instance in db_session.query(db_model):
        expression: str = multi_getattr(instance, expression_path)
        expression_regex: bool = multi_getattr(instance, expression_regex_path)
        expression_id: int = multi_getattr(instance, id_path)

        if combine_expr_with:
            combine_with = multi_getattr(instance, combine_expr_with)
            expression = str(combine_with) + expression

        if expression_regex:
            compiled_ex = re.compile(expression)
            if compiled_ex.search(""):
                flags.append(hs.HS_FLAG_ALLOWEMPTY)
            else:
                flags.append(hs.HS_FLAG_SOM_LEFTMOST)
            del compiled_ex
        else:
            expression = re.escape(expression)
            flags.append(hs.HS_FLAG_SOM_LEFTMOST)

        ids.append(expression_id)
        expressions.append(expression.encode("UTF-8"))

    # Release db session
    DatabaseManager().return_session(db_session)

    return expressions, ids, flags