[Python-Dev] I think my set module is ready for prime time; comments?

Eric S. Raymond esr@thyrsus.com
Mon, 22 Jan 2001 12:41:59 -0500

\section{\module{set} ---
         Basic set algebra for Python}

\modulesynopsis{Basic set algebra operations on sequences.}
\moduleauthor{Eric S. Raymond}{esr@thyrsus.com}
\sectionauthor{Eric S. Raymond}{esr@thyrsus.com}

The \module{set} module defines functions for treating lists and other
sequences as mathematical sets, and defines a set class that uses
these operations natively and overloads Python's standard operator set.

The \module{set} functions work on any sequence type and return lists.
The set methods can take a set or any sequence type as an argument.
Set or sequence elements may be of any type and may be mutable.
Comparisons and membership tests of elements against sequence objects
are done using \keyword{in}, and so can be customized by supplying a 
suitable \method{__getattr__} method for the sequence type.

The running time of these functions is O(n**2) in the worst case
unless otherwise noted.  For cases that can be short-circuited by 
cardinality comparisons, this has been done.

Returns a list of the argument sequence's elements with duplicates removed.

\begin{funcdesc}{union}{list1, list2}
Set union.  All elements of both sets or sequences are returned.

\begin{funcdesc}{intersection}{list1, list2}
Set intersection.  All elements common to both sets or sequences are returned.

\begin{funcdesc}{difference}{list1, list2}
Set difference.  All elements of the first set or sequence not present
in the second are returned.

\begin{funcdesc}{symmetric_difference}{list1, list2}
Set symmetric difference.  All elements present in one sequence or the other
but not in both are returned.

\begin{funcdesc}{cartesian}{list1, list2}
Returns a list of tuples consisting of all possible pairs of elements
from the first and second sequences or sets.

\begin{funcdesc}{equality}{list1, list2}
Set comparison.  Return 1 if the two sets or sequences contain exactly
the same elements, 0 or otherwise.

\begin{funcdesc}{subset}{list1, list2}
Set subset test.  Return 1 if all elements of the fiorst set or
sequence are members of the second, 0 otherwise.

\begin{funcdesc}{proper_subset}{list1, list2}
Set subset test, excluding equality.  Return 1 if the arguments fail a
set equality test, and all elements of the fiorst set or sequence are
members of the second, 0 otherwise.

Return the set of all subsets of the argument set or
sequence. Warning: this produces huge results from small arguments and
is O(2**n) in both running time and space requirements; you can
readily run yourself out of memory using it.

\subsection{set Objects \label{set-objects}}

A \class{set} instance uses the \module{set} module functions to
implement set semantics on the list it contains, and to support 
a full set of Python list methods and operaors.  Thus, the set
methods can take a set or any sequence type as an argument.  

A set object contains a single data member:

List containing the elements of the set.  

Set objects can be treated as mutable sequences; they support the
special methods 
and \method{__delattr__}.  
\method{__getattr__}, they support the memebership test via
\keyword{in}. All the standard mutable-sequence methods
\method{insert} (the index argument is ignored), 
and \method{sort}
are also supported.  After method calls that add elements
\method{append}, \method{extend}, \method{insert}), the
elements of the data member are re-setified, so it is not possible to
introduce duplicates.

Calling \function{repr()} on a set returns the result of calling
\function{repr} on its element list.  Calling \function{str()} returns
a representation resembling mathematical notation for the set; an
open set bracket, followed by a comma-separated list of \function{str()}
representations of the elements, followed by a close set brackets.

Set objects support the following Python operators:

\begin {tableiii}{l|l|l}{code}{Operator}{Function}{Description}
\lineiii{^}{symmetric_difference}{Symmetric differe}
\lineiii{*}{cartesian}{Cartesian product}
\lineiii{==}{equality}{Equality test}
\lineiii{!=,<>}{}{Inequality test}
\lineiii{<}{proper_subset}{Proper-subset test}
\lineiii{<=}{subset}{Subset test}
\lineiii{>}{}{Proper superset test}
\lineiii{>=}{}{Superset test}
\end {tableiii}

		<a href="http://www.tuxedo.org/~esr/">Eric S. Raymond</a>

Government is actually the worst failure of civilized man. There has
never been a really good one, and even those that are most tolerable
are arbitrary, cruel, grasping and unintelligent.
	-- H. L. Mencken