Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderedSet.update #17

Open
jayvdb opened this issue Jan 30, 2021 · 1 comment
Open

OrderedSet.update #17

jayvdb opened this issue Jan 30, 2021 · 1 comment

Comments

@jayvdb
Copy link

jayvdb commented Jan 30, 2021

Would it be acceptable to add OrderedSet.update to act similar to OrderedDict.update ?

@grantjenks
Copy link
Owner

Maybe. If update() were added, would it mean adding intersection() and intersection_update() too (and maybe others)? For some reason the MutableSet ABC in https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes prefers the binary operators rather than the method names. For example:

$ ipython
Python 3.8.4 (v3.8.4:dfa645a65e, Jul 13 2020, 10:45:06) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from sortedcollections import OrderedSet                                         

In [2]: s = OrderedSet()                                                                 

In [3]: s.update([1, 2, 3, 4])                                                           
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-a5458f946adc> in <module>
----> 1 s.update([1, 2, 3, 4])

AttributeError: 'OrderedSet' object has no attribute 'update'

In [4]: s |= [1, 2, 3, 4]                                                                

In [5]: s                                                                                
Out[5]: OrderedSet([1, 2, 3, 4])

I once knew why but have since forgotten. It's probably as simple as adding update = MutableSet.__ior__ to the class definition (which I did not do ... again I can't recall why).

I would also add that OrderedSet has only the advantage of random-access indexing. Now that Python's dictionary maintains insertion order, a much faster version of OrderedSet could simply be implemented atop the dictionary data type.

from collections.abc import MutableSet

class OrderedSet(MutableSet):
    def __init__(self, iterable=()):
        self._values = dict()
        for element in iterable:
            self.add(element)

    def __contains__(self, key):
        return key in self._values

    def __iter__(self):
        return iter(self._values)

    def __len__(self):
        return len(self._values)

    def add(self, value):
        self._values[value] = None

    def discard(self, value):
        self._values.pop(value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants