'Construct a new Session.
See also the :class:`.sessionmaker` function which is used to
generate a :class:`.Session`-producing callable with a given
set of arguments.
:param autocommit:
.. warning::
The autocommit flag is **not for general use**, and if it is used,
queries should only be invoked within the span of a
:meth:`.Session.begin` / :meth:`.Session.commit` pair.   Executing
queries outside of a demarcated transaction is a legacy mode
of usage, and can in some cases lead to concurrent connection
checkouts.
Defaults to ``False``. When ``True``, the
:class:`.Session` does not keep a persistent transaction running, and
will acquire connections from the engine on an as-needed basis,
returning them immediately after their use. Flushes will begin and
commit (or possibly rollback) their own transaction if no
transaction is present. When using this mode, the
:meth:`.Session.begin` method is used to explicitly start
transactions.
.. seealso::
:ref:`session_autocommit`
:param autoflush: When ``True``, all query operations will issue a
``flush()`` call to this ``Session`` before proceeding. This is a
convenience feature so that ``flush()`` need not be called
repeatedly in order for database queries to retrieve results. It\'s
typical that ``autoflush`` is used in conjunction with
``autocommit=False``. In this scenario, explicit calls to
``flush()`` are rarely needed; you usually only need to call
``commit()`` (which flushes) to finalize changes.
:param bind: An optional ``Engine`` or ``Connection`` to which this
``Session`` should be bound. When specified, all SQL operations
performed by this session will execute via this connectable.
:param binds: An optional dictionary which contains more granular
"bind" information than the ``bind`` parameter provides. This
dictionary can map individual ``Table`` instances as well as
``Mapper`` instances to individual ``Engine`` or ``Connection``
objects. Operations which proceed relative to a particular
``Mapper`` will consult this dictionary for the direct ``Mapper``
instance as well as the mapper\'s ``mapped_table`` attribute in
order to locate an connectable to use. The full resolution is
described in the ``get_bind()`` method of ``Session``.
Usage looks like::
Session = sessionmaker(binds={
SomeMappedClass: create_engine(\'postgresql://engine1\'),
somemapper: create_engine(\'postgresql://engine2\'),
some_table: create_engine(\'postgresql://engine3\'),
Also see the :meth:`.Session.bind_mapper`
and :meth:`.Session.bind_table` methods.
:param \class_: Specify an alternate class other than
``sqlalchemy.orm.session.Session`` which should be used by the
returned class. This is the only argument that is local to the
``sessionmaker()`` function, and is not sent directly to the
constructor for ``Session``.
:param _enable_transaction_accounting:  Defaults to ``True``.  A
legacy-only flag which when ``False`` disables *all* 0.5-style
object accounting on transaction boundaries, including auto-expiry
of instances on rollback and commit, maintenance of the "new" and
"deleted" lists upon rollback, and autoflush of pending changes upon
begin(), all of which are interdependent.
:param expire_on_commit:  Defaults to ``True``. When ``True``, all
instances will be fully expired after each ``commit()``, so that
all attribute/object access subsequent to a completed transaction
will load from the most recent database state.
:param extension: An optional
:class:`~.SessionExtension` instance, or a list
of such instances, which will receive pre- and post- commit and
flush events, as well as a post-rollback event. **Deprecated.**
Please see :class:`.SessionEvents`.
:param info: optional dictionary of arbitrary data to be associated
with this :class:`.Session`.  Is available via the :attr:`.Session.info`
attribute.  Note the dictionary is copied at construction time so
that modifications to the per-:class:`.Session` dictionary will be local
to that :class:`.Session`.
.. versionadded:: 0.9.0
:param query_cls:  Class which should be used to create new Query
objects, as returned by the ``query()`` method. Defaults to
:class:`~sqlalchemy.orm.query.Query`.
:param twophase:  When ``True``, all transactions will be started as
a "two phase" transaction, i.e. using the "two phase" semantics
of the database in use along with an XID.  During a ``commit()``,
after ``flush()`` has been issued for all attached databases, the
``prepare()`` method on each database\'s ``TwoPhaseTransaction``
will be called. This allows each database to roll back the entire
transaction, before each transaction is committed.
:param weak_identity_map:  Defaults to ``True`` - when set to
``False``, objects placed in the :class:`.Session` will be
strongly referenced until explicitly removed or the
:class:`.Session` is closed.  **Deprecated** - this option
is obsolete.'

def __init__(self, bind=None, autoflush=True, expire_on_commit=True, _enable_transaction_accounting=True, autocommit=False, twophase=False, weak_identity_map=True, binds=None, extension=None, info=None, query_cls=query.Query):

    if weak_identity_map:
        self._identity_cls = identity.WeakInstanceDict
    else:
        util.warn_deprecated('weak_identity_map=False    is    deprecated.        This    feature    is    not    needed.')
        self._identity_cls = identity.StrongInstanceDict
    self.identity_map = self._identity_cls()
    self._new = {}
    self._deleted = {}
    self.bind = bind
    self.__binds = {}
    self._flushing = False
    self._warn_on_events = False
    self.transaction = None
    self.hash_key = _new_sessionid()
    self.autoflush = autoflush
    self.autocommit = autocommit
    self.expire_on_commit = expire_on_commit
    self._enable_transaction_accounting = _enable_transaction_accounting
    self.twophase = twophase
    self._query_cls = query_cls
    if info:
        self.info.update(info)
    if extension:
        for ext in util.to_list(extension):
            SessionExtension._adapt_listener(self, ext)
    if (binds is not None):
        for (mapperortable, bind) in binds.items():
            insp = inspect(mapperortable)
            if insp.is_selectable:
                self.bind_table(mapperortable, bind)
            elif insp.is_mapper:
                self.bind_mapper(mapperortable, bind)
            else:
                assert False
    if (not self.autocommit):
        self.begin()
    _sessions[self.hash_key] = self
