wheerd / multiset Goto Github PK
View Code? Open in Web Editor NEWA multiset implementation for python
License: MIT License
A multiset implementation for python
License: MIT License
Firstly, thanks for the module. There's 2 minor issues, sorry to cram them into one heap.
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.
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)
.
__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.
just started using multiset in my project and got these mypy messages:
Mypy found 1 error, 1 note in 1 file
verify_pgx_json_call.py : 1 error, 1 note
Skipping analyzing "multiset": module is installed, but missing library stubs or py.typed marker [import] (5:0)
See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports (5:0)
Any plan to add full type support to the library?
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)
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.
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.
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 ???
Dependabot couldn't authenticate with https://pypi.python.org/simple/.
You can provide authentication details in your Dependabot dashboard by clicking into the account menu (in the top right) and selecting 'Config variables'.
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.