<commit_before># -*- coding: utf-8 -*-
"""
chanjo.store.core
~~~~~~~~~~~~~~~~~~
"""
from __future__ import absolute_import, division, unicode_literals

from sqlalchemy import create_engine, func
from sqlalchemy.orm import sessionmaker

from .models import (
  Base,
  Superblock,
  Block,
  Interval,
  Interval_Block,
  SuperblockData,
  BlockData,
  IntervalData,
  Sample
)


class Store(object):

  """SQLAlchemy-based database object.

  Bundles functionality required to setup and interact with various
  related genomic interval elements.

  .. versionchanged:: 2.1.0
    Lazy-loadable, all "init" arguments optional.

  Examples:
    >>> chanjo_db = Store('data/elements.sqlite')
    >>> chanjo_db.set_up()

  .. note::

    For testing pourposes use ``:memory:`` as the ``path`` argument to
    set up in-memory (temporary) database.

  Args:
    uri (str, optional): path/URI to the database to connect to
    dialect (str, optional): connector + type of database:
      'sqlite'/'mysql'
    debug (bool, optional): whether to output logging information

  Attributes:
    uri (str): path/URI to the database to connect to
    engine (class): SQLAlchemy engine, defines what database to use
    session (class): SQLAlchemy ORM session, manages persistance
    query (method): SQLAlchemy ORM query builder method
    classes (dict): bound ORM classes
  """

  def __init__(self, uri=None, dialect='sqlite', debug=False):
    super(Store, self).__init__()
    self.uri = uri
    if uri:
      self.connect(uri, dialect=dialect, debug=debug)

    # ORM class shortcuts to enable fetching models dynamically
    self.classes = {
      'superblock': Superblock,
      'block': Block,
      'interval': Interval,
      'interval_block': Interval_Block,
      'superblock_data': SuperblockData,
      'block_data': BlockData,
      'interval_data': IntervalData,
      'sample': Sample
    }

  def connect(self, uri, dialect='sqlite', debug=False):
    """Configure connection to a SQL database.

    .. versionadded:: 2.1.0

    Args:
      uri (str): path/URI to the database to connect to
      dialect (str, optional): connector + type of database:
        'sqlite'/'mysql'
      debug (bool, optional): whether to output logging information
    """
    # connect to the SQL database
    if dialect == 'sqlite':
      self.engine = create_engine("sqlite:///%s" % uri, echo=debug)

    else:
      # build URI for MySQL containing:
      # <connector>+<sql_type>://<username>:<password>@<server>/<database>
      auth_path = "%(type)s://%(uri)s" % dict(type=dialect, uri=uri)
      self.engine = create_engine(auth_path, pool_recycle=3600, echo=debug)

    # make sure the same engine is propagated to the Base classes
    Base.metadata.bind = self.engine

    # start a session
    self.session = sessionmaker(bind=self.engine)()

    # shortcut to query method
    self.query = self.session.query

    return self

  @property
  def dialect(self):
    """Return database dialect name used for the current connection.

    Dynamic attribute.

    Returns:
      str: name of dialect used for database connection
    """
    return self.engine.dialect.name

  def set_up(self):
    """Initialize a new database with the default tables and columns.

    Returns:
      Store: self
    """
    # create the tables
    Base.metadata.create_all(self.engine)

    return self

  def tare_down(self):<commit_msg>Add some SQLAlchemy best practises<commit_after># -*- coding: utf-8 -*-
"""
chanjo.store.core
~~~~~~~~~~~~~~~~~~
"""
from __future__ import absolute_import, division, unicode_literals

from sqlalchemy import create_engine, func
from sqlalchemy.orm import scoped_session, sessionmaker

from .models import (
  Base,
  Superblock,
  Block,
  Interval,
  Interval_Block,
  SuperblockData,
  BlockData,
  IntervalData,
  Sample
)


class Store(object):

  """SQLAlchemy-based database object.

  Bundles functionality required to setup and interact with various
  related genomic interval elements.

  .. versionchanged:: 2.1.0
    Lazy-loadable, all "init" arguments optional.

  Examples:
    >>> chanjo_db = Store('data/elements.sqlite')
    >>> chanjo_db.set_up()

  .. note::

    For testing pourposes use ``:memory:`` as the ``path`` argument to
    set up in-memory (temporary) database.

  Args:
    uri (str, optional): path/URI to the database to connect to
    dialect (str, optional): connector + type of database:
      'sqlite'/'mysql'
    debug (bool, optional): whether to output logging information

  Attributes:
    uri (str): path/URI to the database to connect to
    engine (class): SQLAlchemy engine, defines what database to use
    session (class): SQLAlchemy ORM session, manages persistance
    query (method): SQLAlchemy ORM query builder method
    classes (dict): bound ORM classes
  """

  def __init__(self, uri=None, dialect='sqlite', debug=False):
    super(Store, self).__init__()
    self.uri = uri
    if uri:
      self.connect(uri, dialect=dialect, debug=debug)

    # ORM class shortcuts to enable fetching models dynamically
    self.classes = {
      'superblock': Superblock,
      'block': Block,
      'interval': Interval,
      'interval_block': Interval_Block,
      'superblock_data': SuperblockData,
      'block_data': BlockData,
      'interval_data': IntervalData,
      'sample': Sample
    }

  def connect(self, uri, dialect='sqlite', debug=False):
    """Configure connection to a SQL database.

    .. versionadded:: 2.1.0

    Args:
      uri (str): path/URI to the database to connect to
      dialect (str, optional): connector + type of database:
        'sqlite'/'mysql'
      debug (bool, optional): whether to output logging information
    """
    kwargs = {'echo': debug, 'convert_unicode': True}

    # connect to the SQL database
    if dialect == 'sqlite':
      # conform to the slightly awkward sqlite adapter syntax (///)
      auth_path = "sqlite:///%s" % uri

    elif 'mysql' in dialect:
      # build URI for MySQL containing:
      # <connector>+<sql_type>://<username>:<password>@<server>/<database>
      auth_path = "%(type)s://%(uri)s" % dict(type=dialect, uri=uri)

      kwargs['pool_recycle'] = 3600

    else:
      raise NotImplementedError(
        'Only "sqlite" and "mysql" are supported database dialects.')

    self.engine = create_engine(auth_path, **kwargs)

    # make sure the same engine is propagated to the Base classes
    Base.metadata.bind = self.engine

    # start a session
    self.session = scoped_session(
      sessionmaker(autoflush=True, bind=self.engine))

    # shortcut to query method
    self.query = self.session.query

    return self

  @property
  def dialect(self):
    """Return database dialect name used for the current connection.

    Dynamic attribute.

    Returns:
      str: name of dialect used for database connection
    """
    return self.engine.dialect.name

  def set_up(self):
    """Initialize a new database with the default tables and columns.

    Returns:
      Store: self
    """
    # create the tables
    Base.metadata.create_all(self.engine)

    return self

  def tare_down(self):
{'fraction_non_alphanumeric': 0.11145337440492858, 'fraction_numerical': 0.003080369644357323, 'mean_word_length': 3.2116745283018866, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7016887', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2045, 'n_words': 673}