Skip to content

Commit

Permalink
Merge pull request #137 from vladak/type_hints_matcher
Browse files Browse the repository at this point in the history
add type hints to matcher.py
  • Loading branch information
tekktrik authored Jan 16, 2023
2 parents 729c77a + 414debb commit b47e7b1
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions adafruit_minimqtt/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
* Author(s): Yoch (https://github.com/yoch)
"""

try:
from typing import Dict
except ImportError:
pass


class MQTTMatcher:
"""Intended to manage topic filters including wildcards.
Expand All @@ -27,22 +32,22 @@ class Node:

__slots__ = "children", "content"

def __init__(self):
self.children = {}
def __init__(self) -> None:
self.children: Dict[str, MQTTMatcher.Node] = {}
self.content = None

def __init__(self):
def __init__(self) -> None:
self._root = self.Node()

def __setitem__(self, key, value):
def __setitem__(self, key: str, value) -> None:
"""Add a topic filter :key to the prefix tree
and associate it to :value"""
node = self._root
for sym in key.split("/"):
node = node.children.setdefault(sym, self.Node())
node.content = value

def __getitem__(self, key):
def __getitem__(self, key: str):
"""Retrieve the value associated with some topic filter :key"""
try:
node = self._root
Expand All @@ -54,7 +59,7 @@ def __getitem__(self, key):
except KeyError:
raise KeyError(key) from None

def __delitem__(self, key):
def __delitem__(self, key: str) -> None:
"""Delete the value associated with some topic filter :key"""
lst = []
try:
Expand All @@ -71,13 +76,13 @@ def __delitem__(self, key):
break
del parent.children[k]

def iter_match(self, topic):
def iter_match(self, topic: str):
"""Return an iterator on all values associated with filters
that match the :topic"""
lst = topic.split("/")
normal = not topic.startswith("$")

def rec(node, i=0):
def rec(node: MQTTMatcher.Node, i: int = 0):
if i == len(lst):
if node.content is not None:
yield node.content
Expand Down

0 comments on commit b47e7b1

Please sign in to comment.