bidict/README.rst

280 lines
9.2 KiB
ReStructuredText
Raw Normal View History

.. Forward declarations for all the custom interpreted text roles that
Sphinx defines and that are used below. This helps Sphinx-unaware tools
(e.g. rst2html, PyPI's and GitHub's renderers, etc.).
.. role:: doc
2020-01-11 03:31:17 +00:00
.. Use :doc: rather than :ref: references below for better interop as well.
2020-01-11 03:31:17 +00:00
``bidict``
==========
The bidirectional mapping library for Python.
2018-10-17 15:16:43 +00:00
.. image:: https://raw.githubusercontent.com/jab/bidict/master/assets/logo-sm.png
:target: https://bidict.readthedocs.io/
:alt: bidict logo
Status
------
.. image:: https://img.shields.io/pypi/v/bidict.svg
:target: https://pypi.org/project/bidict
:alt: Latest release
.. image:: https://img.shields.io/readthedocs/bidict/master.svg
:target: https://bidict.readthedocs.io/en/master/
:alt: Documentation
2018-02-13 12:13:24 +00:00
.. image:: https://api.travis-ci.org/jab/bidict.svg?branch=master
:target: https://travis-ci.org/jab/bidict
:alt: Travis-CI build status
.. image:: https://codecov.io/gh/jab/bidict/branch/master/graph/badge.svg
:target: https://codecov.io/gh/jab/bidict
:alt: Test coverage
.. Hide to reduce clutter
Various API changes and other improvements. * Deprecate ``bidict.OVERWRITE`` and ``bidict.IGNORE``. A :class:`UserWarning` will now be emitted if these are used. :attr:`bidict.DROP_OLD` and :attr:`bidict.DROP_NEW` should be used instead. * Rename ``DuplicationPolicy`` to :class:`~bidict.OnDupAction` (and implement it via an :class:`~enum.Enum`). A :class:`~bidict.OnDupAction` may be one of :attr:`~bidict.RAISE`, :attr:`~bidict.DROP_OLD`, or :attr:`~bidict.DROP_NEW`. * Expose the new :class:`~bidict.OnDup` class, a named (*key*, *val*, *kv*) tuple of :class:`~bidict.OnDupAction`\s that should be taken upon encountering the 3 kinds of duplication that can occur. * Provide the :attr:`~bidict.ON_DUP_DEFAULT`, :attr:`~bidict.ON_DUP_RAISE`, and :attr:`~bidict.ON_DUP_DROP_OLD` :class:`~bidict.OnDup` convenience instances. * Deprecate the ``on_dup_key``, ``on_dup_val``, and ``on_dup_kv`` arguments of :meth:`~bidict.bidict.put` and :meth:`~bidict.bidict.putall`. A :class:`UserWarning` will now be emitted if these are used. They have been subsumed by the new *on_dup* argument, which takes an :class:`~bidict.OnDup` instance. Use it like this: ``bi.put(1, 2, OnDup(key=DROP_NEW))``. Or better yet, pass one of the ``ON_DUP_*`` convenience instances instead if possible. See the updated :ref:`basic-usage:Values Must Be Unique` docs for more info. * Deprecate the ``on_dup_key``, ``on_dup_val``, and ``on_dup_kv`` bidict class attributes. A :class:`UserWarning` will now be emitted if these are used. They have been subsumed by the new :attr:`~bidict.bidict.on_dup` class attribute, which takes an :class:`~bidict.OnDup` instance. See the updated :doc:`extending` docs for example usage. * Move :meth:`bidict.BidictBase.values` to :meth:`bidict.BidirectionalMapping.values`, since the implementation is generic. * No longer use ``__all__`` in ``bidict/__init__.py``. * Cap max_size rather than disabling health checks and deadline as a less heavyhanded way to improve hypothesis test reliability on Travis.
2020-01-07 22:20:20 +00:00
.. image:: https://img.shields.io/lgtm/alerts/github/jab/bidict.svg
:target: https://lgtm.com/projects/g/jab/bidict/
:alt: LGTM alerts
.. image:: https://api.codacy.com/project/badge/Grade/6628756a73254cd895656348236833b8
:target: https://www.codacy.com/app/jab/bidict
:alt: Codacy grade
.. image:: https://bestpractices.coreinfrastructure.org/projects/2354/badge
:target: https://bestpractices.coreinfrastructure.org/en/projects/2354
:alt: CII best practices badge
.. image:: https://img.shields.io/badge/tidelift-pro%20support-orange.svg
:target: https://tidelift.com/subscription/pkg/pypi-bidict?utm_source=pypi-bidict&utm_medium=referral&utm_campaign=docs
:alt: Paid support available via Tidelift
.. image:: https://ci.appveyor.com/api/projects/status/gk133415udncwto3/branch/master?svg=true
:target: https://ci.appveyor.com/project/jab/bidict
:alt: AppVeyor (Windows) build status
.. image:: https://img.shields.io/pypi/pyversions/bidict.svg
:target: https://pypi.org/project/bidict
:alt: Supported Python versions
.. image:: https://img.shields.io/pypi/implementation/bidict.svg
:target: https://pypi.org/project/bidict
:alt: Supported Python implementations
2015-04-27 22:20:03 +00:00
.. image:: https://img.shields.io/pypi/l/bidict.svg
:target: https://raw.githubusercontent.com/jab/bidict/master/LICENSE
:alt: License
.. image:: https://img.shields.io/badge/dynamic/json.svg?label=downloads&url=https%3A%2F%2Fpypistats.org%2Fapi%2Fpackages%2Fbidict%2Frecent%3Fperiod%3Dmonth&query=%24.data.last_month&colorB=blue&suffix=%2fmonth
:target: https://pypistats.org/packages/bidict
:alt: Downloads past month
2020-01-11 03:31:17 +00:00
``bidict``:
^^^^^^^^^^^
- has been used for many years by several teams at
Google, Venmo, CERN, Bank of America Merrill Lynch, Bloomberg, Two Sigma, and others
- has carefully designed APIs for
Various improvements. - Refactor proxied- (i.e. delegated-) to-``_fwdm`` logic for better composability and interoperability. Drop the ``_Proxied*`` mixin classes and instead move their methods into :class:`~bidict.BidictBase`, which now checks for an object defined by the :attr:`~bidict.BidictBase.__delegate__` attribute. The :attr:`~bidict.BidictBase.__delegate__` object will be delegated to if the method is available on it, otherwise a default implementation (e.g. inherited from :class:`~collections.abc.Mapping`) will be used otherwise. Subclasses may set ``__delegate__ = None`` to opt out. - Consolidate ``_MutableBidict`` into :class:`bidict.bidict` now that the dropped mixin classes make it unnecessary. - Change :attr:`~bidict.BidictBase.__repr_delegate__` to take simply a type like :class:`dict` or :class:`list`. - Upgrade to latest major `sortedcontainers <https://github.com/grantjenks/python-sortedcontainers>`__ version (from v1 to v2) for the :ref:`extending:Sorted Bidict Recipes`. - ``bidict.compat.{view,iter}{keys,values,items}`` on Python2 no longer assumes the target object implements these methods, as they're not actually part of the :class:`~collections.abc.Mapping` interface, and provides fallback implementations when the methods are unavailable. This allows the :ref:`extending:Sorted Bidict Recipes` to continue to work with sortedcontainers v2 on Python2. - Test code in docs via Sphinx rather than pytest. Enables running Python version-dependent tests conditionally rather than skipping them altogether, as well as hiding import statements (via `testsetup`) that otherwise just add noise. Run tests (viz. pytest and Sphinx doctest) via a new run-tests.py script.
2018-11-05 20:52:59 +00:00
safety, simplicity, flexibility, and ergonomics
- is fast, lightweight, and has no runtime dependencies other than Python's standard library
- integrates natively with Pythons collections interfaces
- is implemented in concise, well-factored, pure (PyPy-compatible) Python code
optimized both for reading and learning from [#fn-learning]_
as well as for running efficiently
- has extensive docs and test coverage
2018-10-17 15:16:43 +00:00
(including property-based tests and benchmarks)
Various improvements. - Refactor proxied- (i.e. delegated-) to-``_fwdm`` logic for better composability and interoperability. Drop the ``_Proxied*`` mixin classes and instead move their methods into :class:`~bidict.BidictBase`, which now checks for an object defined by the :attr:`~bidict.BidictBase.__delegate__` attribute. The :attr:`~bidict.BidictBase.__delegate__` object will be delegated to if the method is available on it, otherwise a default implementation (e.g. inherited from :class:`~collections.abc.Mapping`) will be used otherwise. Subclasses may set ``__delegate__ = None`` to opt out. - Consolidate ``_MutableBidict`` into :class:`bidict.bidict` now that the dropped mixin classes make it unnecessary. - Change :attr:`~bidict.BidictBase.__repr_delegate__` to take simply a type like :class:`dict` or :class:`list`. - Upgrade to latest major `sortedcontainers <https://github.com/grantjenks/python-sortedcontainers>`__ version (from v1 to v2) for the :ref:`extending:Sorted Bidict Recipes`. - ``bidict.compat.{view,iter}{keys,values,items}`` on Python2 no longer assumes the target object implements these methods, as they're not actually part of the :class:`~collections.abc.Mapping` interface, and provides fallback implementations when the methods are unavailable. This allows the :ref:`extending:Sorted Bidict Recipes` to continue to work with sortedcontainers v2 on Python2. - Test code in docs via Sphinx rather than pytest. Enables running Python version-dependent tests conditionally rather than skipping them altogether, as well as hiding import statements (via `testsetup`) that otherwise just add noise. Run tests (viz. pytest and Sphinx doctest) via a new run-tests.py script.
2018-11-05 20:52:59 +00:00
run continuously on all supported Python versions and OSes
2018-01-02 20:32:06 +00:00
Note: Python 3 Now Required
~~~~~~~~~~~~~~~~~~~~~~~~~~~
2017-03-15 20:43:23 +00:00
As promised in the 0.18.2 release (see :doc:`changelog` [#fn-changelog]_),
**Python 2 is no longer supported**.
Version 0.18.3
2020-01-11 03:31:17 +00:00
is the last release of ``bidict`` that supports Python 2.
This makes ``bidict`` more efficient on Python 3
and enables further improvement to bidict in the future.
See `python3statement.org <https://python3statement.org>`__
for more info.
2018-02-12 23:51:08 +00:00
Installation
------------
``pip install bidict``
2018-02-13 12:13:24 +00:00
Quick Start
2018-02-12 23:51:08 +00:00
-----------
.. code:: python
>>> from bidict import bidict
>>> element_by_symbol = bidict({'H': 'hydrogen'})
>>> element_by_symbol['H']
'hydrogen'
>>> element_by_symbol.inverse['hydrogen']
'H'
2018-02-12 23:51:08 +00:00
For more usage documentation,
head to the :doc:`intro` [#fn-intro]_
and proceed from there.
Community Support
-----------------
2017-11-18 04:54:53 +00:00
.. image:: https://img.shields.io/badge/chat-on%20gitter-5AB999.svg?logo=gitter-white
:target: https://gitter.im/jab/bidict
:alt: Chat
2017-11-18 04:54:53 +00:00
2020-01-11 03:31:17 +00:00
If you are thinking of using ``bidict`` in your work,
or if you have any questions, comments, or suggestions,
I'd love to know about your use case
and provide as much voluntary support for it as possible.
Please feel free to leave a message in the
`chatroom <https://gitter.im/jab/bidict>`__
or open a new issue on GitHub.
2018-02-24 09:14:42 +00:00
You can search through
`existing issues <https://github.com/jab/bidict/issues>`__
2018-02-24 09:14:42 +00:00
before creating a new one
in case your questions or concerns have been adressed there already.
2017-03-17 16:16:45 +00:00
2016-06-28 04:05:22 +00:00
2020-01-11 03:31:17 +00:00
Enterprise-Grade Support via Tidelift
-------------------------------------
2018-10-17 15:16:43 +00:00
.. image:: https://img.shields.io/badge/tidelift-pro%20support-orange.svg
:target: https://tidelift.com/subscription/pkg/pypi-bidict?utm_source=pypi-bidict&utm_medium=referral&utm_campaign=readme
:alt: Paid support available via Tidelift
2018-10-17 15:16:43 +00:00
If your use case requires a greater level of support,
2020-01-11 03:31:17 +00:00
enterprise-grade support for ``bidict`` can be obtained via the
`Tidelift subscription <https://tidelift.com/subscription/pkg/pypi-bidict?utm_source=pypi-bidict&utm_medium=referral&utm_campaign=readme>`__.
2018-10-17 15:16:43 +00:00
2017-11-18 04:54:53 +00:00
Notice of Usage
---------------
2020-01-11 03:31:17 +00:00
If you use ``bidict``,
2018-02-24 09:14:42 +00:00
and especially if your usage or your organization is significant in some way,
please let me know.
2018-01-16 03:02:08 +00:00
2018-02-24 09:14:42 +00:00
You can:
- `star bidict on GitHub <https://github.com/jab/bidict>`__
- `create an issue <https://github.com/jab/bidict/issues/new?title=Notice+of+Usage&body=I+am+using+bidict+for...>`__
- leave a message in the `chat room <https://gitter.im/jab/bidict>`__
- `email me <mailto:jab@math.brown.edu?subject=bidict&body=I%20am%20using%20bidict%20for...>`__
2017-11-18 04:54:53 +00:00
Changelog
---------
See the :doc:`changelog` [#fn-changelog]_
2020-01-11 03:31:17 +00:00
for a history of notable changes to ``bidict``.
Release Notifications
---------------------
.. duplicated in CHANGELOG.rst:
(would use `.. include::` but GitHub doesn't understand it)
.. image:: https://img.shields.io/badge/libraries.io-subscribe-5BC0DF.svg
:target: https://libraries.io/pypi/bidict
:alt: Follow on libraries.io
2020-01-11 03:31:17 +00:00
Subscribe to releases
`on GitHub <https://github.blog/changelog/2018-11-27-watch-releases/>`__ or
`libraries.io <https://libraries.io/pypi/bidict>`__
to be notified when new versions of ``bidict`` are released.
2016-06-28 04:05:22 +00:00
2020-01-11 03:31:17 +00:00
Learning from ``bidict``
------------------------
2018-01-02 20:32:06 +00:00
2020-01-11 03:31:17 +00:00
One of the best things about ``bidict``
is that it touches a surprising number of
interesting Python corners,
especially given its small size and scope.
2018-02-24 09:14:42 +00:00
Check out :doc:`learning-from-bidict` [#fn-learning]_
if you're interested in learning more.
2018-01-02 20:32:06 +00:00
2016-06-28 04:05:22 +00:00
Contributing
------------
2020-01-11 03:31:17 +00:00
``bidict`` is currently a one-person operation
maintained on a voluntary basis.
2018-02-24 09:14:42 +00:00
Your help would be most welcome!
Reviewers Wanted!
^^^^^^^^^^^^^^^^^
2020-01-11 03:31:17 +00:00
One of the most valuable ways to contribute to ``bidict``
and to explore some interesting Python corners [#fn-learning]_
while you're at it
is to review the relatively small codebase.
2018-02-24 09:14:42 +00:00
2018-03-04 03:16:04 +00:00
Please create an issue or pull request with any improvements you'd propose
or any other results you found.
2020-01-11 03:31:17 +00:00
Submitting a `draft PR <https://github.blog/2019-02-14-introducing-draft-pull-requests/>`__
with feedback in inline code comments, or a
`"Review results" issue <https://github.com/jab/bidict/issues/new?title=Review+results>`__,
would each work well.
2018-02-24 09:14:42 +00:00
2018-03-04 03:16:04 +00:00
You can also
+1 `this issue <https://github.com/jab/bidict/issues/63>`__
to sign up to give feedback on future proposed changes
that are in need of a reviewer.
2018-02-24 09:14:42 +00:00
2018-10-17 15:16:43 +00:00
Giving Back
^^^^^^^^^^^
.. duplicated in CONTRIBUTING.rst
(would use `.. include::` but GitHub doesn't understand it)
2017-03-17 16:16:45 +00:00
2020-01-11 03:31:17 +00:00
``bidict`` is the product of hundreds of hours of unpaid, voluntary work.
2018-10-17 15:16:43 +00:00
2020-01-11 03:31:17 +00:00
If ``bidict`` has helped you accomplish your work,
2017-03-17 16:16:45 +00:00
especially work you've been paid for,
please consider chipping in toward the costs
2020-01-11 03:31:17 +00:00
of its maintenance and development
and/or ask your organization to do the same.
.. image:: https://raw.githubusercontent.com/jab/bidict/master/assets/support-on-gumroad.png
:target: https://gumroad.com/l/bidict
:alt: Support bidict
2016-06-28 04:05:22 +00:00
Finding Documentation
---------------------
If you're viewing this on `<https://bidict.readthedocs.io>`__,
note that multiple versions of the documentation are available,
and you can choose a different version using the popup menu at the bottom-right.
Please make sure you're viewing the version of the documentation
2020-01-11 03:31:17 +00:00
that corresponds to the version of ``bidict`` you'd like to use.
If you're viewing this on GitHub, PyPI, or some other place
that can't render and link this documentation properly
and are seeing broken links,
try these alternate links instead:
.. [#fn-learning] `<docs/learning-from-bidict.rst>`__ | `<https://bidict.readthedocs.io/learning-from-bidict.html>`__
.. [#fn-changelog] `<CHANGELOG.rst>`__ | `<https://bidict.readthedocs.io/changelog.html>`__
.. [#fn-intro] `<docs/intro.rst>`__ | `<https://bidict.readthedocs.io/intro.html>`__
----
Next: :doc:`intro` [#fn-intro]_