[Python-modules-commits] [python-ordered-set] 01/03: New upstream version 2.0.2

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu Apr 20 21:35:41 UTC 2017


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch master
in repository python-ordered-set.

commit bb4d74485a49b0c734a0d2bb49364c597656a6b0
Author: Ghislain Antony Vaillant <ghisvail at gmail.com>
Date:   Wed Apr 19 21:40:28 2017 +0100

    New upstream version 2.0.2
---
 MANIFEST.in                               |   2 +
 MIT-LICENSE                               |  19 +++
 PKG-INFO                                  |  17 +++
 README                                    |  20 +++
 ordered_set.egg-info/PKG-INFO             |  17 +++
 ordered_set.egg-info/SOURCES.txt          |  10 ++
 ordered_set.egg-info/dependency_links.txt |   1 +
 ordered_set.egg-info/top_level.txt        |   1 +
 ordered_set.py                            | 198 ++++++++++++++++++++++++++++++
 setup.cfg                                 |   5 +
 setup.py                                  |  26 ++++
 test.py                                   | 126 +++++++++++++++++++
 12 files changed, 442 insertions(+)

diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..4bedc7c
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1,2 @@
+include MIT-LICENSE
+include test.py
diff --git a/MIT-LICENSE b/MIT-LICENSE
new file mode 100644
index 0000000..256dc09
--- /dev/null
+++ b/MIT-LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2013 Luminoso Technologies, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the "Software"),
+to deal in the Software without restriction, including without limitation
+the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/PKG-INFO b/PKG-INFO
new file mode 100644
index 0000000..4512731
--- /dev/null
+++ b/PKG-INFO
@@ -0,0 +1,17 @@
+Metadata-Version: 1.1
+Name: ordered-set
+Version: 2.0.2
+Summary: A MutableSet that remembers its order, so that every entry has an index.
+Home-page: http://github.com/LuminosoInsight/ordered-set
+Author: Luminoso Technologies, Inc.
+Author-email: rob at luminoso.com
+License: MIT-LICENSE
+Description: UNKNOWN
+Platform: any
+Classifier: Development Status :: 5 - Production/Stable
+Classifier: Intended Audience :: Developers
+Classifier: License :: OSI Approved :: MIT License
+Classifier: Programming Language :: Python :: 2
+Classifier: Programming Language :: Python :: 3
+Classifier: Programming Language :: Python :: Implementation :: CPython
+Classifier: Programming Language :: Python :: Implementation :: PyPy
diff --git a/README b/README
new file mode 100644
index 0000000..00417f1
--- /dev/null
+++ b/README
@@ -0,0 +1,20 @@
+An OrderedSet is a custom MutableSet that remembers its order, so that every
+entry has an index that can be looked up.
+
+Based on a recipe originally posted to ActiveState Recipes by Raymond Hettiger,
+and released under the MIT license:
+
+    http://code.activestate.com/recipes/576694-orderedset/
+
+Rob Speer's changes are as follows:
+
+    - changed the content from a doubly-linked list to a regular Python list.
+      Seriously, who wants O(1) deletes but O(N) lookups by index?
+    - add() returns the index of the added item
+    - index() just returns the index of an item
+    - added a __getstate__ and __setstate__ so it can be pickled
+    - added __getitem__
+    - __getitem__ and index() can be passed lists or arrays, looking up all
+      the elements in them to perform NumPy-like "fancy indexing"
+
+Tested on Python 2.6, 2.7, 3.3, 3.4, 3.5, PyPy, and PyPy3.
diff --git a/ordered_set.egg-info/PKG-INFO b/ordered_set.egg-info/PKG-INFO
new file mode 100644
index 0000000..4512731
--- /dev/null
+++ b/ordered_set.egg-info/PKG-INFO
@@ -0,0 +1,17 @@
+Metadata-Version: 1.1
+Name: ordered-set
+Version: 2.0.2
+Summary: A MutableSet that remembers its order, so that every entry has an index.
+Home-page: http://github.com/LuminosoInsight/ordered-set
+Author: Luminoso Technologies, Inc.
+Author-email: rob at luminoso.com
+License: MIT-LICENSE
+Description: UNKNOWN
+Platform: any
+Classifier: Development Status :: 5 - Production/Stable
+Classifier: Intended Audience :: Developers
+Classifier: License :: OSI Approved :: MIT License
+Classifier: Programming Language :: Python :: 2
+Classifier: Programming Language :: Python :: 3
+Classifier: Programming Language :: Python :: Implementation :: CPython
+Classifier: Programming Language :: Python :: Implementation :: PyPy
diff --git a/ordered_set.egg-info/SOURCES.txt b/ordered_set.egg-info/SOURCES.txt
new file mode 100644
index 0000000..956f605
--- /dev/null
+++ b/ordered_set.egg-info/SOURCES.txt
@@ -0,0 +1,10 @@
+MANIFEST.in
+MIT-LICENSE
+README
+ordered_set.py
+setup.py
+test.py
+ordered_set.egg-info/PKG-INFO
+ordered_set.egg-info/SOURCES.txt
+ordered_set.egg-info/dependency_links.txt
+ordered_set.egg-info/top_level.txt
\ No newline at end of file
diff --git a/ordered_set.egg-info/dependency_links.txt b/ordered_set.egg-info/dependency_links.txt
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/ordered_set.egg-info/dependency_links.txt
@@ -0,0 +1 @@
+
diff --git a/ordered_set.egg-info/top_level.txt b/ordered_set.egg-info/top_level.txt
new file mode 100644
index 0000000..1c191ee
--- /dev/null
+++ b/ordered_set.egg-info/top_level.txt
@@ -0,0 +1 @@
+ordered_set
diff --git a/ordered_set.py b/ordered_set.py
new file mode 100644
index 0000000..726e120
--- /dev/null
+++ b/ordered_set.py
@@ -0,0 +1,198 @@
+"""
+An OrderedSet is a custom MutableSet that remembers its order, so that every
+entry has an index that can be looked up.
+
+Based on a recipe originally posted to ActiveState Recipes by Raymond Hettiger,
+and released under the MIT license.
+
+Rob Speer's changes are as follows:
+
+    - changed the content from a doubly-linked list to a regular Python list.
+      Seriously, who wants O(1) deletes but O(N) lookups by index?
+    - add() returns the index of the added item
+    - index() just returns the index of an item
+    - added a __getstate__ and __setstate__ so it can be pickled
+    - added __getitem__
+"""
+import collections
+
+SLICE_ALL = slice(None)
+__version__ = '2.0.1'
+
+
+def is_iterable(obj):
+    """
+    Are we being asked to look up a list of things, instead of a single thing?
+    We check for the `__iter__` attribute so that this can cover types that
+    don't have to be known by this module, such as NumPy arrays.
+
+    Strings, however, should be considered as atomic values to look up, not
+    iterables. The same goes for tuples, since they are immutable and therefore
+    valid entries.
+
+    We don't need to check for the Python 2 `unicode` type, because it doesn't
+    have an `__iter__` attribute anyway.
+    """
+    return hasattr(obj, '__iter__') and not isinstance(obj, str) and not isinstance(obj, tuple)
+
+
+class OrderedSet(collections.MutableSet):
+    """
+    An OrderedSet is a custom MutableSet that remembers its order, so that
+    every entry has an index that can be looked up.
+    """
+    def __init__(self, iterable=None):
+        self.items = []
+        self.map = {}
+        if iterable is not None:
+            self |= iterable
+
+    def __len__(self):
+        return len(self.items)
+
+    def __getitem__(self, index):
+        """
+        Get the item at a given index.
+
+        If `index` is a slice, you will get back that slice of items. If it's
+        the slice [:], exactly the same object is returned. (If you want an
+        independent copy of an OrderedSet, use `OrderedSet.copy()`.)
+
+        If `index` is an iterable, you'll get the OrderedSet of items
+        corresponding to those indices. This is similar to NumPy's
+        "fancy indexing".
+        """
+        if index == SLICE_ALL:
+            return self
+        elif hasattr(index, '__index__') or isinstance(index, slice):
+            result = self.items[index]
+            if isinstance(result, list):
+                return OrderedSet(result)
+            else:
+                return result
+        elif is_iterable(index):
+            return OrderedSet([self.items[i] for i in index])
+        else:
+            raise TypeError("Don't know how to index an OrderedSet by %r" %
+                    index)
+
+    def copy(self):
+        return OrderedSet(self)
+
+    def __getstate__(self):
+        if len(self) == 0:
+            # The state can't be an empty list.
+            # We need to return a truthy value, or else __setstate__ won't be run.
+            #
+            # This could have been done more gracefully by always putting the state
+            # in a tuple, but this way is backwards- and forwards- compatible with
+            # previous versions of OrderedSet.
+            return (None,)
+        else:
+            return list(self)
+
+    def __setstate__(self, state):
+        if state == (None,):
+            self.__init__([])
+        else:
+            self.__init__(state)
+
+    def __contains__(self, key):
+        return key in self.map
+
+    def add(self, key):
+        """
+        Add `key` as an item to this OrderedSet, then return its index.
+
+        If `key` is already in the OrderedSet, return the index it already
+        had.
+        """
+        if key not in self.map:
+            self.map[key] = len(self.items)
+            self.items.append(key)
+        return self.map[key]
+    append = add
+
+    def update(self, sequence):
+        """
+        Update the set with the given iterable sequence, then return the index
+        of the last element inserted.
+        """
+        item_index = None
+        try:
+            for item in sequence:
+                item_index = self.add(item)
+        except TypeError:
+            raise ValueError('Argument needs to be an iterable, got %s' % type(sequence))
+        return item_index
+
+    def index(self, key):
+        """
+        Get the index of a given entry, raising an IndexError if it's not
+        present.
+
+        `key` can be an iterable of entries that is not a string, in which case
+        this returns a list of indices.
+        """
+        if is_iterable(key):
+            return [self.index(subkey) for subkey in key]
+        return self.map[key]
+
+    def pop(self):
+        """
+        Remove and return the last element from the set.
+
+        Raises KeyError if the set is empty.
+        """
+        if not self.items:
+            raise KeyError('Set is empty')
+
+        elem = self.items[-1]
+        del self.items[-1]
+        del self.map[elem]
+        return elem
+
+    def discard(self, key):
+        """
+        Remove an element.  Do not raise an exception if absent.
+
+        The MutableSet mixin uses this to implement the .remove() method, which
+        *does* raise an error when asked to remove a non-existent item.
+        """
+        if key in self:
+            i = self.map[key]
+            del self.items[i]
+            del self.map[key]
+            for k, v in self.map.items():
+                if v >= i:
+                    self.map[k] = v - 1
+
+    def clear(self):
+        """
+        Remove all items from this OrderedSet.
+        """
+        del self.items[:]
+        self.map.clear()
+
+    def __iter__(self):
+        return iter(self.items)
+
+    def __reversed__(self):
+        return reversed(self.items)
+
+    def __repr__(self):
+        if not self:
+            return '%s()' % (self.__class__.__name__,)
+        return '%s(%r)' % (self.__class__.__name__, list(self))
+
+    def __eq__(self, other):
+        if isinstance(other, OrderedSet):
+            return len(self) == len(other) and self.items == other.items
+        try:
+            other_as_set = set(other)
+        except TypeError:
+            # If `other` can't be converted into a set, it's not equal.
+            return False
+        else:
+            return set(self) == other_as_set
+
diff --git a/setup.cfg b/setup.cfg
new file mode 100644
index 0000000..861a9f5
--- /dev/null
+++ b/setup.cfg
@@ -0,0 +1,5 @@
+[egg_info]
+tag_build = 
+tag_date = 0
+tag_svn_revision = 0
+
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..4696ffd
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,26 @@
+from setuptools import setup
+
+setup(
+    name="ordered-set",
+    version = '2.0.2',
+    maintainer='Luminoso Technologies, Inc.',
+    maintainer_email='rob at luminoso.com',
+    license = "MIT-LICENSE",
+    url = 'http://github.com/LuminosoInsight/ordered-set',
+    platforms = ["any"],
+    description = "A MutableSet that remembers its order, so that every entry has an index.",
+    py_modules=['ordered_set'],
+    package_data={'': ['MIT-LICENSE']},
+    include_package_data=True,
+    test_suite='nose.collector',
+    tests_require=['nose'],
+    classifiers=[
+        'Development Status :: 5 - Production/Stable',
+        'Intended Audience :: Developers',
+        'License :: OSI Approved :: MIT License',
+        'Programming Language :: Python :: 2',
+        'Programming Language :: Python :: 3',
+        'Programming Language :: Python :: Implementation :: CPython',
+        'Programming Language :: Python :: Implementation :: PyPy',
+    ]
+)
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..db84db3
--- /dev/null
+++ b/test.py
@@ -0,0 +1,126 @@
+from nose.tools import eq_, raises, assert_raises
+from ordered_set import OrderedSet
+import pickle
+
+
+def test_pickle():
+    set1 = OrderedSet('abracadabra')
+    roundtrip = pickle.loads(pickle.dumps(set1))
+    assert roundtrip == set1
+
+
+def test_empty_pickle():
+    empty_oset = OrderedSet()
+    empty_roundtrip = pickle.loads(pickle.dumps(empty_oset))
+    assert empty_roundtrip == empty_oset
+
+
+def test_order():
+    set1 = OrderedSet('abracadabra')
+    eq_(len(set1), 5)
+    eq_(set1, OrderedSet(['a', 'b', 'r', 'c', 'd']))
+    eq_(list(reversed(set1)), ['d', 'c', 'r', 'b', 'a'])
+
+
+def test_binary_operations():
+    set1 = OrderedSet('abracadabra')
+    set2 = OrderedSet('simsalabim')
+    assert set1 != set2
+
+    eq_(set1 & set2, OrderedSet(['a', 'b']))
+    eq_(set1 | set2, OrderedSet(['a', 'b', 'r', 'c', 'd', 's', 'i', 'm', 'l']))
+    eq_(set1 - set2, OrderedSet(['r', 'c', 'd']))
+
+
+def test_indexing():
+    set1 = OrderedSet('abracadabra')
+    eq_(set1[:], set1)
+    eq_(set1.copy(), set1)
+    assert set1[:] is set1
+    assert set1.copy() is not set1
+
+    eq_(set1[[1, 2]], OrderedSet(['b', 'r']))
+    eq_(set1[1:3], OrderedSet(['b', 'r']))
+    eq_(set1.index('b'), 1)
+    eq_(set1.index(['b', 'r']), [1, 2])
+    try:
+        set1.index('br')
+        assert False, "Looking up a nonexistent key should be a KeyError"
+    except KeyError:
+        pass
+
+
+def test_tuples():
+    set1 = OrderedSet()
+    tup = ('tuple', 1)
+    set1.add(tup)
+    eq_(set1.index(tup), 0)
+    eq_(set1[0], tup)
+
+
+def test_remove():
+    set1 = OrderedSet('abracadabra')
+
+    set1.remove('a')
+    set1.remove('b')
+
+    assert set1 == OrderedSet('rcd')
+    assert set1[0] == 'r'
+    assert set1[1] == 'c'
+    assert set1[2] == 'd'
+
+    assert set1.index('r') == 0
+    assert set1.index('c') == 1
+    assert set1.index('d') == 2
+
+    assert 'a' not in set1
+    assert 'b' not in set1
+    assert 'r' in set1
+
+    # Make sure we can .discard() something that's already gone, plus
+    # something that was never there
+    set1.discard('a')
+    set1.discard('a')
+
+
+ at raises(KeyError)
+def test_remove_error():
+    # If we .remove() an element that's not there, we get a KeyError
+    set1 = OrderedSet('abracadabra')
+    set1.remove('z')
+
+
+def test_clear():
+    set1 = OrderedSet('abracadabra')
+    set1.clear()
+
+    assert len(set1) == 0
+    assert set1 == OrderedSet()
+
+
+def test_update():
+    set1 = OrderedSet('abcd')
+    result = set1.update('efgh')
+
+    assert result == 7
+    assert len(set1) == 8
+    assert ''.join(set1) == 'abcdefgh'
+
+    set2 = OrderedSet('abcd')
+    result = set2.update('cdef')
+    assert result == 5
+    assert len(set2) == 6
+    assert ''.join(set2) == 'abcdef'
+
+
+def test_pop():
+    set1 = OrderedSet('ab')
+    elem = set1.pop()
+
+    assert elem == 'b'
+    elem = set1.pop()
+
+    assert elem == 'a'
+
+    assert_raises(KeyError, set1.pop)
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-ordered-set.git



More information about the Python-modules-commits mailing list