def search(self, targets, partial=True, fuzzy=False):
allInstances = self.instances()
matchedInstances = set()
for host in targets:
for instance in allInstances:
names = [instance.name]
if instance.aliases != None:
names += list(instance.aliases)
for name in names:
if host.lower() == name.lower():
matchedInstances.add((100, instance))
elif partial and host.lower() in name.lower():
matchedInstances.add((99, instance))
if fuzzy:
score = fuzz.partial_ratio(host.lower(), name.lower())
if score > 85 or host.lower() in name.lower():
matchedInstances.add((score, instance))
# it is possible for the same instance to be matched, if so, it should only
# appear on the return list once (still ordered by the most probable match)
return list(collections.OrderedDict([(v, None) for k, v in sorted(list(matchedInstances))]).keys())
评论列表
文章目录