本文整理汇总了Python中pytrie.StringTrie.get方法的典型用法代码示例。如果您正苦于以下问题:Python StringTrie.get方法的具体用法?Python StringTrie.get怎么用?Python StringTrie.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pytrie.StringTrie
的用法示例。
在下文中一共展示了StringTrie.get方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: UriObservationMap
# 需要导入模块: from pytrie import StringTrie [as 别名]
# 或者: from pytrie.StringTrie import get [as 别名]
#.........这里部分代码省略.........
elif match == u"wildcard":
# if the wildcard-matching URI isn't in our map, create a new observation
#
if uri not in self._observations_wildcard:
self._observations_wildcard[uri] = WildcardUriObservation(uri, ordered=self._ordered, extra=extra)
is_first_observer = True
else:
is_first_observer = False
# get the observation
#
observation = self._observations_wildcard[uri]
else:
raise Exception("invalid match strategy '{}'".format(match))
# note observation in observation ID map
#
if is_first_observer:
self._observation_id_to_observation[observation.id] = observation
# add observer if not already in observation
#
if observer not in observation.observers:
observation.observers.add(observer)
was_already_observed = False
else:
was_already_observed = True
return observation, was_already_observed, is_first_observer
def get_observation(self, uri, match=u"exact"):
"""
Get a observation (if any) for given URI and match policy.
:param uri: The URI (or URI pattern) to get the observation for.
:type uri: unicode
:param match: The matching policy for observation to retrieve, one of ``u"exact"``, ``u"prefix"`` or ``u"wildcard"``.
:type match: unicode
:returns: The observation (instance of one 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__))
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.
示例2: UriObservationMap
# 需要导入模块: from pytrie import StringTrie [as 别名]
# 或者: from pytrie.StringTrie import get [as 别名]
#.........这里部分代码省略.........
self._observations_wildcard_patterns[observation.pattern_len] = {}
# setup map: (pattern length, pattern) -> pattern count
if observation.pattern not in self._observations_wildcard_patterns[observation.pattern_len]:
self._observations_wildcard_patterns[observation.pattern_len][observation.pattern] = 1
else:
self._observations_wildcard_patterns[observation.pattern_len][observation.pattern] += 1
else:
is_first_observer = False
# get the observation
#
observation = self._observations_wildcard[uri]
else:
raise Exception("invalid match strategy '{}'".format(match))
# note observation in observation ID map
#
if is_first_observer:
self._observation_id_to_observation[observation.id] = observation
# add observer if not already in observation
#
if observer not in observation.observers:
observation.observers.add(observer)
was_already_observed = False
else:
was_already_observed = True
return observation, was_already_observed, is_first_observer
def get_observation(self, uri, match=u"exact"):
"""
Get a observation (if any) for given URI and match policy.
:param uri: The URI (or URI pattern) to get the observation for.
:type uri: unicode
:param match: The matching policy for observation to retrieve, one of ``u"exact"``, ``u"prefix"`` or ``u"wildcard"``.
:type match: unicode
:returns: The observation (instance of one of ``ExactUriObservation``, ``PrefixUriObservation`` or ``WildcardUriObservation``)
or ``None``.
:rtype: obj or None
"""
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.
示例3: RouterRoleStaticAuth
# 需要导入模块: from pytrie import StringTrie [as 别名]
# 或者: from pytrie.StringTrie import get [as 别名]
class RouterRoleStaticAuth(RouterRole):
"""
A role on a router realm that is authorized using a static configuration.
"""
def __init__(self, router, uri, permissions=None, default_permissions=None):
"""
:param router: The router this role is defined on.
:type router: obj
:param uri: The URI of the role.
:type uri: unicode
:param permissions: A permissions configuration, e.g. a list
of permission dicts like `{'uri': 'com.example.*', 'call': True}`
:type permissions: list of dict
:param default_permissions: The default permissions to apply when no other
configured permission matches. The default permissions when not explicitly
set is to deny all actions on all URIs!
:type default_permissions: dict
"""
RouterRole.__init__(self, router, uri)
assert(permissions is None or isinstance(permissions, list))
if permissions:
for p in permissions:
assert(isinstance(p, dict))
assert(default_permissions is None or isinstance(default_permissions, dict))
# default permissions (used when nothing else is matching)
# note: default permissions have their matching URI and match policy set to None!
if default_permissions:
self._default = RouterPermissions.from_dict(default_permissions)
else:
self._default = RouterPermissions(None, None,
call=False,
register=False,
publish=False,
subscribe=False,
disclose_caller=False,
disclose_publisher=False,
cache=True)
# Trie of explicitly configured permissions
self._permissions = StringTrie()
self._wild_permissions = StringTrie()
# for "wildcard" URIs, there will be a ".." in them somewhere,
# and so we want to match on the biggest prefix
# (i.e. everything to the left of the first "..")
for obj in permissions or []:
perms = RouterPermissions.from_dict(obj)
if '..' in perms.uri:
trunc = perms.uri[:perms.uri.index('..')]
self._wild_permissions[trunc] = perms
else:
self._permissions[perms.uri] = perms
def authorize(self, session, uri, action, options):
"""
Authorize a session connected under this role to perform the given
action on the given URI.
:param session: The WAMP session that requests the action.
:type session: Instance of :class:`autobahn.wamp.protocol.ApplicationSession`
:param uri: The URI on which to perform the action.
:type uri: str
:param action: The action to be performed.
:type action: str
:return: bool -- Flag indicating whether session is authorized or not.
"""
self.log.debug(
"CrossbarRouterRoleStaticAuth.authorize {myuri} {uri} {action}",
myuri=self.uri, uri=uri, action=action)
try:
# longest prefix match of the URI to be authorized against our Trie
# of configured URIs for permissions
permissions = self._permissions.longest_prefix_value(uri)
# if there is a _prefix_ matching URI, check that this is actually the
# match policy on the permission (otherwise, apply default permissions)!
if permissions.match != u'prefix' and uri != permissions.uri:
permissions = self._default
except KeyError:
# workaround because of https://bitbucket.org/gsakkis/pytrie/issues/4/string-keys-of-zero-length-are-not
permissions = self._permissions.get(u'', self._default)
# if we found a non-"exact" match, there might be a better one in the wildcards
if permissions.match != u'exact':
try:
wildperm = self._wild_permissions.longest_prefix_value(uri)
Pattern(wildperm.uri, Pattern.URI_TARGET_ENDPOINT).match(uri)
except (KeyError, Exception):
# match() raises Exception on no match
wildperm = None
if wildperm is not None:
permissions = wildperm
#.........这里部分代码省略.........