Skip to content

Commit

Permalink
fix: str and repr for objects
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarsic committed Apr 21, 2023
1 parent 1612506 commit 08ed07d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fmt:
pdm fmt

test: fmt
pdm test

build: test
pdm build
5 changes: 4 additions & 1 deletion tinysearch/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def __init__(self, original: str):
self.tokens = self.analyze()

def __str__(self):
return f'"{self.original}"'
return f"Document['{self.original}']"

def __repr__(self):
return self.__str__()

def analyze(self) -> Counter:
"""Parses the original text into a list of tokens."""
Expand Down
6 changes: 6 additions & 0 deletions tinysearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class Index:
def __init__(self, docs: List[str]) -> None:
self.docs = self.process_docs(docs)

def __str__(self):
return f"Index[docs={len(self.docs)}]"

def __repr__(self):
return self.__str__()

def process_docs(self, docs: List[str]) -> List[Document]:
processed = []
for doc in docs:
Expand Down
9 changes: 9 additions & 0 deletions tinysearch/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def __init__(self):
def __str__(self):
return str(self.results)

def __repr__(self):
return self.__str__()

def append(self, result: Result) -> None:
self.results.append(result)
if result.score > 0.0:
Expand All @@ -78,6 +81,12 @@ def __init__(self, docs: List[str], query: str) -> None:
self.results = Results()
self.search()

def __str__(self):
return f"Search[query='{self.query}', matches={self.results.count}]"

def __repr__(self):
return self.__str__()

def search(self) -> Results:
self.score_docs(self.query)

Expand Down

0 comments on commit 08ed07d

Please sign in to comment.