Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
fiveham authored Oct 24, 2019
1 parent 18f41d1 commit 19b889f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions canon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Canon:
"""Keeps canonical instances of hashable objects to save space when there
will be many equal copies made for whatever reason.
When webscraping, for example, there may be thousands of repetitions of
the same text that you get from pages that come from thousands of
separate requests. Space can be saved by simply filtering each likely-
duplicated instance through a collection of canonical copies of that
type of thing.
This filtering process maps each thing onto its canonical equivalent
(an object instance that is equal to it) from a cache. If and when the
thing in question is not already present in the cache, the thing itself
is added to the cache and then the thing is mapped onto itself."""

def __init__(self, things=None):
self.storage = {}
if things:
self.multi(things)

def single(self, thing):
""":returns: the canonical equivalent instance of `thing`"""
try:
return self.storage[thing]
except KeyError:
self.storage[thing] = thing
return thing

def multi(self, things):
""":returns: a collection of canonical instances of elements of `things`"""
return type(things)(self.single(thing) for thing in things)

0 comments on commit 19b889f

Please sign in to comment.