[classifyTools] rename 'sorted' -> 'sort' to avoid confusions with the built-in name

This commit is contained in:
Cosimo Lupo 2016-04-08 22:08:21 +01:00
parent 0be1d722dc
commit 09d2983fbb

View File

@ -10,13 +10,13 @@ class Classifier(object):
Main Classifier object, used to classify things into similar sets.
"""
def __init__(self, sorted=True):
def __init__(self, sort=True):
self._things = set() # set of all things known so far
self._sets = [] # list of class sets produced so far
self._mapping = {} # map from things to their class set
self._dirty = False
self._sorted = sorted
self._sort = sort
def add(self, set_of_things):
"""
@ -75,7 +75,7 @@ class Classifier(object):
sets = self._sets
self._sets = [s for s in sets if s]
if self._sorted:
if self._sort:
self._sets = sorted(self._sets, key=lambda s: (-len(s), s))
self._dirty = False
@ -110,7 +110,7 @@ class Classifier(object):
return self._sets
def classify(list_of_sets, sorted=True):
def classify(list_of_sets, sort=True):
"""
Takes a iterable of iterables (list of sets from here on; but any
iterable works.), and returns the smallest list of sets such that
@ -121,7 +121,7 @@ def classify(list_of_sets, sorted=True):
any of the input sets, into similar classes, based on which sets
things are a member of.
If sorted=True, return class sets are sorted by decreasing size and
If sort=True, return class sets are sorted by decreasing size and
their natural sort order within each class size. Otherwise, class
sets are returned in the order that they were identified, which is
generally not significant.
@ -145,25 +145,25 @@ def classify(list_of_sets, sorted=True):
>>> classify([[1,2],[2,4,5]]) == (
... [{4, 5}, {1}, {2}], {1: {1}, 2: {2}, 4: {4, 5}, 5: {4, 5}})
True
>>> classify([[1,2],[2,4,5]], sorted=False) == (
>>> classify([[1,2],[2,4,5]], sort=False) == (
... [{1}, {4, 5}, {2}], {1: {1}, 2: {2}, 4: {4, 5}, 5: {4, 5}})
True
>>> classify([[1,2,9],[2,4,5]], sorted=False) == (
>>> classify([[1,2,9],[2,4,5]], sort=False) == (
... [{1, 9}, {4, 5}, {2}], {1: {1, 9}, 2: {2}, 4: {4, 5}, 5: {4, 5},
... 9: {1, 9}})
True
>>> classify([[1,2,9,15],[2,4,5]], sorted=False) == (
>>> classify([[1,2,9,15],[2,4,5]], sort=False) == (
... [{1, 9, 15}, {4, 5}, {2}], {1: {1, 9, 15}, 2: {2}, 4: {4, 5},
... 5: {4, 5}, 9: {1, 9, 15}, 15: {1, 9, 15}})
True
>>> classes, mapping = classify([[1,2,9,15],[2,4,5],[15,5]], sorted=False)
>>> classes, mapping = classify([[1,2,9,15],[2,4,5],[15,5]], sort=False)
>>> set([frozenset(c) for c in classes]) == set(
... [frozenset(s) for s in ({1, 9}, {4}, {2}, {5}, {15})])
True
>>> mapping == {1: {1, 9}, 2: {2}, 4: {4}, 5: {5}, 9: {1, 9}, 15: {15}}
True
"""
classifier = Classifier(sorted=sorted)
classifier = Classifier(sort=sort)
classifier.update(list_of_sets)
return classifier.getClasses(), classifier.getMapping()