First time around Code Review so please be gentle (and I am happy for comments on which angles of this post are a good fit and which less so). I'm not pretty much used to software engineering in Python, and so I figured this might be a match:
import pickle
class Lexicon:
'a lexicon holding each term and providing some lexicon services like term frequency, and term ids'
def __init__(self):
self.dictt = {}
def size(self):
return len(self.dictt)
def add(self, token):
if token in self.dictt:
self.dictt[token] = self.dictt[token] + 1
else:
self.dictt[token] = 1
return token # for traceability by the caller
def remove_low_frequency(self, min_frequency):
'removes low frequency tokens'
self.dictt = dict(filter(lambda kv: kv[1] >= min_frequency, self.dictt.items()))
def pickle(self, output_file_name):
with open(output_file_name, 'wb') as handle:
pickle.dump(self.dictt, handle, protocol=pickle.HIGHEST_PROTOCOL)
@staticmethod
def unpickle(input_file_name):
with open(input_file_name, 'rb') as handle:
new_lexicon = Lexicon()
new_lexicon.dictt = pickle.load(handle)
return new_lexicon
def equals(self, other):
return self.dictt == other.dictt
- Am I being non-idiomatic above, or, just cumbersome anywhere?
- Does my
unpickle
method actually represent potential for a huge memory leak as I'm reusing the name but putting a different dictionary into it? - What might be the Pythonic idiom for a correct implementation in these cases?
- And what is the Pythonic way of making this class safe for concurrent usage? (thread-safe)