Hello.
I'm new to Python; for a project to get to know the language I wrote up an api for representing finite topologies, and for subsets of topologies for which the orbit (the number of ways to use closure, interior, and complement operators to get separate sets) is fourteen.
It can be used like this:
some_set = [[1, 2, 3], [1, 2], [2, 3], [1, 3], [1], [2], [3], []]a = FiniteTopology(some_set, [1, 2, 3])
...
With all attendant methods. The constructor will take a list of lists or a set of sets.
orbits_of_fourteen(8)
will generate all topologies on subsets of [1, 2, 3, 4, 5, 6, 7, 8], and look for subsets of those topologies whose orbits are of length fourteen. (It will also never end. I wouldn't recommend trying that; I put it in as an exercise.)
One thing I'm not sure of: if the user tries to create a FiniteTopology with a collection of sets that's not a topology, I throw a NotATopologyError in the constructor. But it looks like the FiniteTopology instance is still created. Is there any way to keep the instance from being created at all?
I'm also interested in tips about documentation and style.
Thanks a lot!
Best,James
#!/usr/bin/env python
class NotATopologyError(Exception):
passclass FiniteTopology(object):
"""This represents a finite topology on a set of elements that can be represented by Python's builtin set class. Its representation is of a set
of frozensets, but its constructor will take a list of lists; let it be
stressed that the elements on which the topology is defined must be immutable."""
def __init__(self, topology, main_set): """Creates an instance of FiniteTopology, checking first to make sure
that it *is* a topology. If it isn't, we raise a NotATopology exception."""
self.topology = set([frozenset(element) for element in topology])
self.main_set = set(main_set)
if not self._is_a_topology(): raise NotATopologyError()
def get_topology(self): return self.topology
def _is_a_topology(self): """Checks if self.topology
is a topology.""" check_one = self._check_contains_empty_main()
check_two = self._check_unions() check_three = self._check_intersections()
return check_one and check_two and check_three
def _check_contains_empty_main(self):
"""A topology contains its set and the empty set."""
return self.main_set in self.topology and set([]) in self.topology
def _check_unions(self): """A topology contains all unions of open sets."""
for i in xrange(1, len(self.topology) + 1): for subset in combinations(self.as_list(), i):
current_union = set() for one_set in subset:
current_union = current_union.union(one_set) if not current_union in self.topology:
return False return True
def _check_intersections(self): """A topology contains all pairwise intersections of open sets."""
for item in self.topology: for next_item in self.topology
: intersect = set(item).intersection(next_item) if not intersect in
self.topology: return False return True
def as_list(self): """Returns a list representation of the topology."""
return [[i for i in j] for j in self.topology]
def orbit(self, subset): """Calculates the maximum number of ways we can use take complements,
interiors, and closures of a set to make unique sets. There are at most 14 ways to do this, and usually less."""
orbit = set()
complement = self.complement closure = self.closure interior =
self.interior orbit.add(frozenset(subset))
orbit.add(frozenset(complement(subset))) orbit.add(frozenset(complement(interior(subset
orbit.add(frozenset(complement(interior(complement(subset)
orbit.add(frozenset(complement(interior(complement(closure(subset)) orbit.add(frozenset(interior(subset)))
orbit.add(frozenset(interior(closure(subset orbit.add(frozenset(interior(complement(subset
orbit.add(frozenset(interior(complement(interior(subset) orbit.add(frozenset(interior(closure(interior(subset)
orbit.add(frozenset(interior(closure(interior(complement(subset)) orbit.add(frozenset(closure(subset)))
orbit.add(frozenset(closure(interior(subset orbit.add(frozenset(closure(interior(complement(subset)
return orbit
def closure(self, subset): """A limit point p of a set X is such that all open sets containing p
contain another point of X not equal to p. The closure of a set is the set plus all of its limit points.
This finds all of the limit points of the subset and adds them."""
subset = set(subset) set_of_lps = set()
for maybe_lp in self.m