Skip to content

Commit

Permalink
fix various issues
Browse files Browse the repository at this point in the history
- replace CollapseException with ValueExistsException, which trumps it
- configure py.test to run doctests in docs dir as well as tests dir
  • Loading branch information
jab committed Nov 28, 2015
1 parent 01c8d08 commit 490b7f4
Show file tree
Hide file tree
Showing 27 changed files with 49 additions and 152 deletions.
3 changes: 1 addition & 2 deletions bidict/__init__.py
Expand Up @@ -9,7 +9,7 @@
"""

from ._common import BidirectionalMapping, BidictException, CollapseException, ValueExistsException
from ._common import BidirectionalMapping, BidictException, ValueExistsException
from ._bidict import bidict
from ._loose import loosebidict
from ._frozen import frozenbidict
Expand All @@ -19,7 +19,6 @@
__all__ = (
'BidirectionalMapping',
'BidictException',
'CollapseException',
'ValueExistsException',
'bidict',
'loosebidict',
Expand Down
4 changes: 1 addition & 3 deletions bidict/_bidict.py
Expand Up @@ -31,9 +31,7 @@ def put(self, key, val):
def forceput(self, key, val):
"""
Like :attr:`bidict.bidict.put` but silently removes any existing
mapping that would otherwise cause
:class:`bidict.ValueExistsException` or
:class:`bidict.CollapseException`
mapping that would otherwise cause :class:`bidict.ValueExistsException`
before inserting the given mapping.
"""
oldval = self._fwd.get(key, _missing)
Expand Down
9 changes: 0 additions & 9 deletions bidict/_common.py
Expand Up @@ -50,8 +50,6 @@ def _put(self, key, val):
oldval = self._fwd.get(key, _missing)
if key == oldkey and val == oldval:
return
if oldkey is not _missing and oldval is not _missing:
raise CollapseException((key, oldval), (oldkey, val))
if oldkey is not _missing:
raise ValueExistsException((oldkey, val))
if oldval is not _missing:
Expand Down Expand Up @@ -110,11 +108,4 @@ class ValueExistsException(BidictException):
key maps to the value of an existing mapping, violating one-to-one-ness.
"""

class CollapseException(BidictException):
"""
Raised when an attempt is made to insert a new mapping into a bidict that
would collapse two existing mappings.
"""


_missing = object()
3 changes: 1 addition & 2 deletions bidict/_loose.py
Expand Up @@ -3,8 +3,7 @@
class loosebidict(bidict):
"""
A mutable bidict which always uses forcing put operations
so that it never raises :class:`bidict.CollapseException` or
:class:`bidict.CollapseException`.
so that it never raises :class:`bidict.ValueExistsException`.
"""
def _put(self, key, val):
return self.forceput(key, val)
File renamed without changes.
55 changes: 0 additions & 55 deletions docs/caveat-collapsing.rst.inc

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,7 +1,7 @@
.. _caveat-unique-values:

Uniqueness of Values
--------------------
Values Must Be Unique
---------------------

Consider the following::

Expand Down
8 changes: 3 additions & 5 deletions docs/caveats.rst → docs/caveats.doctest.rst
Expand Up @@ -7,11 +7,9 @@ Bidirectional maps are a simple concept on the surface,
but have some subtle properties worth mentioning.
Please bear the following in mind while using bidict.

.. include:: caveat-unique-values.rst.inc
.. include:: caveat-unique-values.doctest.rst.inc

.. include:: caveat-hashable-values.rst.inc
.. include:: caveat-hashable-values.doctest.rst.inc

.. include:: caveat-collapsing.rst.inc

With those out of the way,
With those caveats out of the way,
let's turn now to considering bidict's :ref:`performance`.
12 changes: 7 additions & 5 deletions docs/changelog.rst
Expand Up @@ -8,8 +8,8 @@ Changelog

- Removed several features in favor of keeping the API simpler
and the code more maintainable
- Stricter one-to-one checking checking by default
- New :attr:`bidict.bidict.forceupdate`` method for bulk forceput
- Raise on non-unique value rather than overwriting by default
- New :attr:`bidict.bidict.forceupdate` method for bulk forceput

Breaking API Changes
^^^^^^^^^^^^^^^^^^^^
Expand All @@ -23,10 +23,12 @@ Breaking API Changes
- Removed ``bidict.invert``.
Use ``b.inv`` rather than inverting a bidict in place.
`#20 <https://github.com/jab/bidict/issues/20>`_
- Raise :class:`bidict.ValueExistsException`` when attempting to insert a new
- Raise :class:`bidict.ValueExistsException` when attempting to insert a new
key associated with an existing value
- Rename ``collapsingbidict`` to :class:`bidict.loosebidict`` now that it's
loose in the case of :class:`bidict.ValueExistsException`` too
`#21 <https://github.com/jab/bidict/issues/21>`_
- Rename ``collapsingbidict`` to :class:`bidict.loosebidict` now that
:class:`bidict.ValueExistsException` has replaced ``CollapseException``
`#21 <https://github.com/jab/bidict/issues/21>`_


0.9.0.post1 (2015-06-06)
Expand Down
4 changes: 2 additions & 2 deletions docs/cut.rst
Expand Up @@ -36,9 +36,9 @@ where bidicts could be used for fun and profit
More Caveats
------------

.. include:: caveat-frozenbidict-hash.rst.inc
.. include:: caveat-frozenbidict-hash.doctest.rst.inc

.. include:: caveat-mutation.rst.inc
.. include:: caveat-mutation.doctest.rst.inc

Other Verbiage, Esoterica, Navel Gazing, &c.
--------------------------------------------
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions docs/index.rst
Expand Up @@ -12,12 +12,12 @@ and related functionality.
:maxdepth: 2
:hidden:

intro
basic-usage
caveats
performance
other-bidict-types
inverted
intro.doctest
basic-usage.doctest
caveats.doctest
performance.doctest
other-bidict-types.doctest
inverted.doctest
api
changelog
credits
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion docs/inverted.rst → docs/inverted.doctest.rst
Expand Up @@ -10,13 +10,14 @@ of any kind of object that can be inverted.

Pass in a mapping to get the inverse mapping::

>>> from bidict import bidict, inverted
>>> dict(inverted({1: 'one'}))
{'one': 1}

an iterable of pairs to get the pairs' inverses::

>>> list(inverted([(1, 'one'), (2, 'two')]))
[('one': 1), ('two', 2)]
[('one', 1), ('two', 2)]
>>> list(inverted((i*i, i) for i in range(2, 5)))
[(2, 4), (3, 9), (4, 16)]

Expand Down
File renamed without changes.
Expand Up @@ -45,9 +45,9 @@ that a bidict is dict-like::
>>> isinstance(b, Mapping)
True

.. include:: frozenbidict.rst.inc
.. include:: frozenbidict.doctest.rst.inc

.. include:: namedbidict.rst.inc
.. include:: namedbidict.doctest.rst.inc

There's one last useful bit of functionality to mention:
:ref:`the "inverted" iterator <inverted>`.
File renamed without changes.
5 changes: 4 additions & 1 deletion pytest.ini
@@ -1,4 +1,7 @@
[pytest]
testpaths = bidict tests docs
addopts = --ignore=docs/conf.py --ignore=setup.py --doctest-modules --collect-only
; need to collect doctest from files ending in .rst, .rst.inc, and .txt,
; but doctest-glob option not multi-allowed. resort to including "doctest" in
; filenames of all doctest files we want collected and matching on that.
addopts = --doctest-modules --doctest-glob=*doctest* --ignore=docs/conf.py --ignore=setup.py --ignore=docs/_build

This comment has been minimized.

Copy link
@jab

jab Nov 28, 2015

Author Owner

Hi @pfctdayelise, @tomviner (in case either of you have a moment for a py.test question), am I missing something here? Or is there a better workaround?

This comment has been minimized.

Copy link
@tomviner

tomviner Nov 30, 2015

Contributor

Can't see a workaround myself.

doctest_optionflags = IGNORE_EXCEPTION_DETAIL ELLIPSIS
29 changes: 0 additions & 29 deletions tests/test_bidict.bidict.txt → tests/doctest_bidict.bidict.txt
Expand Up @@ -152,35 +152,6 @@ Adding a new key associated with an existing value fails::
>>> b
bidict({1: 1})

Initializing with collapsing mappings fails::

>>> bidict([(1, 1), (2, 2), (1, 2)])
Traceback (most recent call last):
...
CollapseException: ((1, 1), (2, 2))

Collapsing updates fail::

>>> b = bidict({1: 1, 2: 2})
>>> b[1] = 2
Traceback (most recent call last):
...
CollapseException: ((1, 1), (2, 2))
>>> b.inv[2] = 1
Traceback (most recent call last):
...
CollapseException: ((1, 1), (2, 2))
>>> b.update({1: 2})
Traceback (most recent call last):
...
CollapseException: ((1, 1), (2, 2))

``forceput`` can be used instead::

>>> b.forceput(1, 2)
>>> b
bidict({1: 2})

Trying to insert an existing mapping does not raise, and is a no-op::

>>> b = bidict({1: 'one'})
Expand Down
18 changes: 18 additions & 0 deletions tests/doctest_bidict.loosebidict.txt
@@ -0,0 +1,18 @@
Test script for bidict.loosebidict::

>>> from bidict import loosebidict

Initializing with different keys mapping to the same value succeeds::

>>> loosebidict([(1, 1), (2, 1)])
loosebidict({2: 1})

Adding a new key associated with an existing value succeeds::

>>> b = loosebidict({1: 1})
>>> b[2] = 1
>>> b
loosebidict({2: 1})
>>> b.update({3: 1})
>>> b
loosebidict({3: 1})
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 0 additions & 28 deletions tests/test_bidict.loosebidict.txt

This file was deleted.

0 comments on commit 490b7f4

Please sign in to comment.