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

QS: Output Schema #972

Open
wants to merge 7 commits into
base: quantumstrand
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions floss/qs/db/expert.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,29 @@ class ExpertStringDatabase:
def __len__(self) -> int:
return len(self.string_rules) + len(self.substring_rules) + len(self.regex_rules)

def query(self, s: str) -> Set[str]:
ret = set()
def query(self, s: str) -> Tuple[Set[str], List[ExpertRule]]:
ret_set = set()
ret_list = list()

if s in self.string_rules:
ret.add(self.string_rules[s].tag)
ret_set.add(self.string_rules[s].tag)
ret_list.append(self.string_rules[s])

# note that this is O(m * n)
# #strings * #rules
for rule in self.substring_rules:
if rule.value in s:
ret.add(rule.tag)
ret_set.add(rule.tag)
ret_list.append(rule)

# note that this is O(m * n)
# #strings * #rules
for rule, regex in self.regex_rules:
if regex.search(s):
ret.add(rule.tag)
ret_set.add(rule.tag)
ret_list.append(rule)

return ret
return ret_set, ret_list

@classmethod
def from_file(cls, path: pathlib.Path) -> "ExpertStringDatabase":
Expand Down
10 changes: 7 additions & 3 deletions floss/qs/db/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,16 @@ def __contains__(self, other: bytes | str) -> bool:
if isinstance(other, bytes):
return other in self.string_hashes
elif isinstance(other, str):
m = hashlib.md5()
m.update(other.encode("utf-8"))
return m.digest()[:8] in self.string_hashes
return self.get_hash(other) in self.string_hashes
else:
raise ValueError("other must be bytes or str")

@staticmethod
def get_hash(string: str) -> bytes:
m = hashlib.md5()
m.update(string.encode("utf-8"))
return m.digest()[:8]

@classmethod
def from_file(cls, path: pathlib.Path) -> "StringHashDatabase":
string_hashes: Set[bytes] = set()
Expand Down
Loading
Loading