本文整理汇总了Python中pytrie.StringTrie.iter_prefix_values方法的典型用法代码示例。如果您正苦于以下问题:Python StringTrie.iter_prefix_values方法的具体用法?Python StringTrie.iter_prefix_values怎么用?Python StringTrie.iter_prefix_values使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pytrie.StringTrie
的用法示例。
在下文中一共展示了StringTrie.iter_prefix_values方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: UriObservationMap
# 需要导入模块: from pytrie import StringTrie [as 别名]
# 或者: from pytrie.StringTrie import iter_prefix_values [as 别名]
#.........这里部分代码省略.........
"""
if match == u"exact":
return self._observations_exact.get(uri, None)
elif match == u"prefix":
return self._observations_prefix.get(uri, None)
elif match == u"wildcard":
return self._observations_wildcard.get(uri, None)
else:
raise Exception("invalid match strategy '{}'".format(match))
def match_observations(self, uri):
"""
Returns the observations matching the given URI. This is the core method called
by a broker to actually dispatch events.
:param uri: The URI to match.
:type uri: unicode
:returns: A list of observations matching the URI. This is a list of instance of
one of ``ExactUriObservation``, ``PrefixUriObservation`` or ``WildcardUriObservation``.
:rtype: list
"""
observations = []
if uri in self._observations_exact:
observations.append(self._observations_exact[uri])
for observation in self._observations_prefix.iter_prefix_values(uri):
observations.append(observation)
uri_parts = tuple(uri.split('.'))
uri_parts_len = len(uri_parts)
if uri_parts_len in self._observations_wildcard_patterns:
for pattern in self._observations_wildcard_patterns[uri_parts_len]:
patterned_uri = '.'.join(['' if pattern[i] else uri_parts[i] for i in range(uri_parts_len)])
if patterned_uri in self._observations_wildcard:
observations.append(self._observations_wildcard[patterned_uri])
return observations
def best_matching_observation(self, uri):
"""
Returns the observation that best matches the given URI. This is the core method called
by a dealer to actually forward calls.
:param uri: The URI to match.
:type uri: unicode
:returns: The observation best matching the URI. This is an instance of
``ExactUriObservation``, ``PrefixUriObservation`` or ``WildcardUriObservation`` or ``None``.
:rtype: obj or None
"""
# a exact matching observation is always "best", if any
#
if uri in self._observations_exact:
return self._observations_exact[uri]
# "second best" is the longest prefix-matching observation, if any
# FIXME: do we want this to take precedence over _any_ wildcard (see below)?
#
示例2: UriObservationMap
# 需要导入模块: from pytrie import StringTrie [as 别名]
# 或者: from pytrie.StringTrie import iter_prefix_values [as 别名]
#.........这里部分代码省略.........
if match == u"exact":
return self._observations_exact.get(uri, None)
elif match == u"prefix":
return self._observations_prefix.get(uri, None)
elif match == u"wildcard":
return self._observations_wildcard.get(uri, None)
else:
raise Exception("invalid match strategy '{}'".format(match))
def match_observations(self, uri):
"""
Returns the observations matching the given URI. This is the core method called
by a broker to actually dispatch events.
:param uri: The URI to match.
:type uri: unicode
:returns: A list of observations matching the URI. This is a list of instance of
one of ``ExactUriObservation``, ``PrefixUriObservation`` or ``WildcardUriObservation``.
:rtype: list
"""
observations = []
if not isinstance(uri, six.text_type):
raise Exception("'uri' should be unicode, not {}".format(type(uri).__name__))
if uri in self._observations_exact:
observations.append(self._observations_exact[uri])
for observation in self._observations_prefix.iter_prefix_values(uri):
observations.append(observation)
for observation in self._observations_wildcard.iter_matches(uri):
observations.append(observation)
return observations
def best_matching_observation(self, uri):
"""
Returns the observation that best matches the given URI. This is the core method called
by a dealer to actually forward calls.
:param uri: The URI to match.
:type uri: unicode
:returns: The observation best matching the URI. This is an instance of
``ExactUriObservation``, ``PrefixUriObservation`` or ``WildcardUriObservation`` or ``None``.
:rtype: obj or None
"""
if not isinstance(uri, six.text_type):
raise Exception("'uri' should be unicode, not {}".format(type(uri).__name__))
# a exact matching observation is always "best", if any
#
if uri in self._observations_exact:
return self._observations_exact[uri]
# "second best" is the longest prefix-matching observation, if any
# FIXME: do we want this to take precedence over _any_ wildcard (see below)?
#
try:
return self._observations_prefix.longest_prefix_value(uri)