Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] make SourmashSignature printable and hashable #332

Merged
merged 3 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions sourmash_lib/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ def __init__(self, email, minhash, name='', filename=''):

self.minhash = minhash

def __hash__(self):
return hash(self.md5sum())

def __str__(self):
name = self.name()
md5pref = self.md5sum()[:8]
if name != md5pref:
return "SourmashSignature('{}', {})".format(name, md5pref)
return "SourmashSignature({})".format(md5pref)
__repr__ = __str__

def md5sum(self):
"Calculate md5 hash of the bottom sketch, specifically."
m = hashlib.md5()
Expand Down
27 changes: 27 additions & 0 deletions tests/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@
load_signatures, load_one_signature


def test_hashable(track_abundance):
# check: can we use signatures as keys in dictionaries and sets?
e = sourmash_lib.MinHash(n=1, ksize=20, track_abundance=track_abundance)
e.add("AT" * 10)

sig = SourmashSignature('', e)

x = set()
x.add(sig)


def test_str(track_abundance):
# signatures should be printable
e = sourmash_lib.MinHash(n=1, ksize=20, track_abundance=track_abundance)
e.add("AT" * 10)

sig = SourmashSignature('', e)

print(sig)
assert str(sig) == 'SourmashSignature(59502a74)'
assert repr(sig) == 'SourmashSignature(59502a74)'

sig.d['name'] = 'fizbar'
assert str(sig) == 'SourmashSignature(\'fizbar\', 59502a74)'
assert repr(sig) == 'SourmashSignature(\'fizbar\', 59502a74)'


def test_roundtrip(track_abundance):
e = sourmash_lib.MinHash(n=1, ksize=20, track_abundance=track_abundance)
e.add("AT" * 10)
Expand Down