Giter Club home page Giter Club logo

multiset's People

Contributors

bakert avatar dependabot-preview[bot] avatar dependabot[bot] avatar ldeluigi avatar lonnen avatar mikefhay avatar razi96 avatar tjni avatar wheerd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

multiset's Issues

.values() not implemented, __str__() is unweildy

Firstly, thanks for the module. There's 2 minor issues, sorry to cram them into one heap.

  1. The documentation mentions .values() as a synonym for .multiplicities(), however this does not work. I would prefer .values() to work as documented (rather than say, deleting it from the docs). This would be consistent with the multiset working with other standard dict methods.

  2. When dealing with large multisets, I am forced to do print(mset.__repr__()) because to the default .__str__() produces unwieldy output

>>> mset = Multiset({'Pumpkin': 1, 'Eggs': 3, 'Milk': 4, 'Sugar': 30})      
>>> print(mset.__repr__())
Multiset({'Pumpkin': 1, 'Eggs': 3, 'Milk': 4, 'Sugar': 30})
>>> print(mset)          
{Pumpkin, Eggs, Eggs, Eggs, Milk, Milk, Milk, Milk, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar, Sugar}

Multisets are often notated in a concise form, as referenced here

the multiset {a, a, a, a, a, b, b, b, c, d, d} ,which may be written in compact form as the map {a: 5, b: 3, c: 1, d: 2}, where for each distinct member we specify the multiplicity.

So I think the .__repr__() is perfectly acceptable stringification under all circumstances. If I need the entire list, I can always list(mset).

Unnecessary copying and sorting in FrozenMultiset __hash__

__hash__ in FrozenMultiset is implemented as return hash(tuple(sorted(self._elements.items()))). This is simple and correctly results in a hash value that does not depend on the order of items, but it is presumably quite slow and will fail if the elements are not orderable.
I would have to test to be sure, but I believe that a solution of simply summing the hashes, like Java's AbstractMap, would be faster and work for all hashable entries.

Can't load from pickle

I have an AttributeError when I try to load a FrozenMultiset with pickle.load.
Namely, this code raises an AttributeError on line 118 of multiset.py when trying to access the _elements attribute.

import pickle as pk
import multiset as ms

fms = ms.FrozenMultiset("aabcd")
    
with open("my_file","w") as my_file:
    pk.dump(fms, my_file)

with open("my_file", "r") as my_file:
    fms_copy = pk.load(my_file)

What happened to popitem?

I tried versions 2.1.1 and 2.0.3: both are missing the popitem() and keys() methods (but they are described in the doc). And apparently nothing replaced it.
Is this a feature or a bug?

Furthermore, the button "docs" links to doc version 2.0.4, for all the versions available.

Several issues noticed while skimming the code...

  • Multiset.__delitem__ is untested and appears to be broken: it doesn't update self._total. Maybe it should be implemented as self[element] = 0 to reuse the __setitem__ code?

  • The docstring for BaseMultiset.union says "For a variant of the operation which modifies the multiset in place see :meth:`union`.". But... that's what I'm looking at :-)

  • Your package metadata says you support both py2 and py3, but the wheel on pypi is marked as being py3-only. Maybe you need to set the universal=1 option in setup.cfg?

  • Not really a bug, but I found the semantics of remove and discard surprising: I would have expected them to be the inverse of add, rather than removing/discarding all copies by default. Maybe a less surprising design would be to have remove remove 1 item, discard discard 0 or 1 items, and then have remove_all and discard_all methods for when you want to do that? I don't know if it's worth breaking backwards compatibility over though.

Why sometimes the Multiset is not disordered???

Look at these samples:

Input:
{
FrozenMultiset([frozenset({'b'}) , frozenset({'a'})])
} == {
FrozenMultiset([frozenset({'a'}) , frozenset({'b'})])
}
Output: False

Input:
{
frozenset([frozenset('b') , frozenset('a')])
}=={
frozenset([frozenset('a') , frozenset('b')])
}
Output: True

It seems Multiset can not perfectly inherit attributes of set ???

TypeError: type 'FrozenMultiset' is not subscriptable

I'd like to be able to write type hints with generic typing in the form FrozenMultiset[T] but the interpreter complaints, Could you extend Generic[T] from the typing package and then write typeing for the methods?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.