Package: bitsets
This package provides a memory-efficient pure-pythonimmutable ordered set
data typefor working with large numbers of subsets from a predetermined pool
of objects.Given a name and a tuple of hashable objects, thebitset()-function returns
a custom integer subclass for creating ordered subsets from the object pool.
Each instance is a Python integer where the presence/absence of thenthpool
item is itsnthbit being set/unset (a.k.a. bit strings, powers of two, rank
incolexicographicalorder).Being just a regular (arbitrary precision) integer with some convenience
methods for translating between unique object collections and bit patterns
allows to perform certain tasks in a more natural way, e.g. sorting a
collection of sets lexicographically, or enumerating possible combinations in
an ordered fashion.LinksGitHub:https://github.com/xflr6/bitsetsPyPI:https://pypi.org/project/bitsets/Documentation:https://bitsets.readthedocs.ioChangelog:https://bitsets.readthedocs.io/en/latest/changelog.htmlIssue Tracker:https://github.com/xflr6/bitsets/issuesDownload:https://pypi.org/project/bitsets/#filesInstallationThis package runs under Python 3.7+ and has no required dependencies,
usepipto install:$pipinstallbitsetsQuickstartCreate a class for sets taken from your pool of objects:>>>frombitsetsimportbitset>>>PYTHONS=('Chapman','Cleese','Gilliam','Idle','Jones','Palin')>>>Pythons=bitset('Pythons',PYTHONS)Access its maximal and minimal instances. Retrieve instance members in
definition order:>>>Pythons.supremumPythons(['Chapman','Cleese','Gilliam','Idle','Jones','Palin'])>>>Pythons.infimumPythons()>>>Pythons(['Idle','Gilliam','Idle','Idle']).members()('Gilliam','Idle')Translate to/from bit string, boolean sequence, and int:>>>Pythons(['Chapman','Gilliam']).bits()'101000'>>>Pythons.frombits('101000')Pythons(['Chapman','Gilliam'])>>>Pythons(['Chapman','Gilliam']).bools()(True,False,True,False,False,False)>>>Pythons.frombools([True,None,1,False,0])Pythons(['Chapman','Gilliam'])>>>int(Pythons(['Chapman','Gilliam']))5>>>Pythons.fromint(5)Pythons(['Chapman','Gilliam'])Set operation and comparison methods (cf. build-infrozenset):>>>Pythons(['Jones','Cleese','Idle']).intersection(Pythons(['Idle']))Pythons(['Idle'])>>>Pythons(['Idle']).union(Pythons(['Jones','Cleese']))Pythons(['Cleese','Idle','Jones'])>>>Pythons.supremum.difference(Pythons(['Chapman','Cleese']))Pythons(['Gilliam','Idle','Jones','Palin'])>>>Pythons(['Palin','Jones']).symmetric_difference(Pythons(['Cleese','Jones']))Pythons(['Cleese','Palin'])>>>Pythons(['Gilliam']).issubset(Pythons(['Cleese','Palin']))False>>>Pythons(['Cleese','Palin']).issuperset(Pythons())TrueFurther readinghttps://wiki.python.org/moin/BitManipulationhttps://wiki.python.org/moin/BitArrayshttps://en.wikipedia.org/wiki/Bit_arrayhttps://en.wikipedia.org/wiki/Bit_manipulationhttps://en.wikipedia.org/wiki/Lexicographical_orderhttps://en.wikipedia.org/wiki/Colexicographical_orderSee alsobitarray– efficient boolean array implemented as C extensionbitstring– pure-Python bit string based onbytearrayBitVector– pure-Python bit array based on unsigned shortarrayBitsets– Cython interface to fast bitsets in Sagebitfield– Cython positive integer setsintbitset– integer bit sets as C extensiongmpy2– fast arbitrary precision integer arithmeticLicenseBitsets is distributed under theMIT license.