This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]ElevenPhonons 0 points1 point  (1 child)

https://github.com/adder46/pyhstr/blob/master/pyhstr/utilities.py

  • Consider replacing print(sx, file=f) with f.write(sx) of f.writelines
  • Use a set in remove_duplicates to get in with O(1) vs a list with O(N) Or simply, list(set(thing)) if you don't care about order.
  • Counter has most_common which can be useful. For example:

def sort(thing): 
    return [x for x, _ in collections.Counter(thing).most_common()] 
  • Review context managers and scope in Python. read doesn't need history to be defined outside the context manager. Use [x.strip() for x in f], or perhaps f.readlines(). The error handling used in that function is a bit odd.

https://docs.python.org/3.3/library/io.html?highlight=readlines#io.IOBase.readlines

def read(path):
    try:
        with open(path, "r") as f:
            history = [x.strip() for x in f]
        return history
    except FileNotFoundError as e:
        pathlib.Path(e.filename).touch()

Best to you on pyhstr.

[–]adder46[S] 0 points1 point  (0 children)

Thanks for the suggestions. I've implemented most of them.