1111* Author(s): Yoch (https://github.com/yoch)
1212"""
1313
14+ try :
15+ from typing import Dict
16+ except ImportError :
17+ pass
18+
1419
1520class MQTTMatcher :
1621 """Intended to manage topic filters including wildcards.
@@ -27,22 +32,22 @@ class Node:
2732
2833 __slots__ = "children" , "content"
2934
30- def __init__ (self ):
31- self .children = {}
35+ def __init__ (self ) -> None :
36+ self .children : Dict [ str , MQTTMatcher . Node ] = {}
3237 self .content = None
3338
34- def __init__ (self ):
39+ def __init__ (self ) -> None :
3540 self ._root = self .Node ()
3641
37- def __setitem__ (self , key , value ):
42+ def __setitem__ (self , key : str , value ) -> None :
3843 """Add a topic filter :key to the prefix tree
3944 and associate it to :value"""
4045 node = self ._root
4146 for sym in key .split ("/" ):
4247 node = node .children .setdefault (sym , self .Node ())
4348 node .content = value
4449
45- def __getitem__ (self , key ):
50+ def __getitem__ (self , key : str ):
4651 """Retrieve the value associated with some topic filter :key"""
4752 try :
4853 node = self ._root
@@ -54,7 +59,7 @@ def __getitem__(self, key):
5459 except KeyError :
5560 raise KeyError (key ) from None
5661
57- def __delitem__ (self , key ) :
62+ def __delitem__ (self , key : str ) -> None :
5863 """Delete the value associated with some topic filter :key"""
5964 lst = []
6065 try :
@@ -71,13 +76,13 @@ def __delitem__(self, key):
7176 break
7277 del parent .children [k ]
7378
74- def iter_match (self , topic ):
79+ def iter_match (self , topic : str ):
7580 """Return an iterator on all values associated with filters
7681 that match the :topic"""
7782 lst = topic .split ("/" )
7883 normal = not topic .startswith ("$" )
7984
80- def rec (node , i = 0 ):
85+ def rec (node : MQTTMatcher . Node , i : int = 0 ):
8186 if i == len (lst ):
8287 if node .content is not None :
8388 yield node .content
0 commit comments