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

Add optional argument in_memory to FastSS __init__ and open to store index in dictionary #1

Closed
wants to merge 1 commit into from
Closed
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: 8 additions & 3 deletions fastss.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def bytes2set(b):
# FastSS class

class FastSS:
def __init__(self, path, flag='c', max_dist=2):
def __init__(self, path, flag='c', max_dist=2, in_memory=False):
"""Open an FastSS index file on <path>.

flag: the mode in which the index is opened. Use "r" for read-only,
Expand All @@ -158,6 +158,11 @@ def __init__(self, path, flag='c', max_dist=2):
self.max_dist = max_dist
self.db[MAXDIST_KEY] = int2byte(max_dist)

if in_memory:
d = dict(self.db)
self.db.close()
self.db = d

def __enter__(self):
return self

Expand All @@ -171,9 +176,9 @@ def __contains__(self, word):
return False

@classmethod
def open(cls, path, flag='c', max_dist=2):
def open(cls, path, flag='c', max_dist=2, in_memory=False):
"""Conventional interface for opening FastSS index file"""
return cls(path, flag, max_dist)
return cls(path, flag, max_dist, in_memory=in_memory)

def close(self):
self.db.close()
Expand Down