本文整理汇总了Python中dictionary.Dictionary.find方法的典型用法代码示例。如果您正苦于以下问题:Python Dictionary.find方法的具体用法?Python Dictionary.find怎么用?Python Dictionary.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类dictionary.Dictionary
的用法示例。
在下文中一共展示了Dictionary.find方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Translator
# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import find [as 别名]
class Translator():
""" Class to encapsulate morse translation logic. """
def __init__(self):
self.dictionary = Dictionary()
def to_morse(self, input_str):
"""
Evaluates each character of the input string by looking it up in the morse dictionary
If the char exists in the dictionary, append its corresponding morse code value to a character list.
If the char does not exist, raise a TranslationException.
Returns the morse code character list joined together into a string, separated by spaces.
"""
char_list = []
for letter in input_str:
entry = self.dictionary.find(letter)
if entry:
char_list.append(entry)
else:
raise TranslationException(input_str, letter, "Key not found.")
return " ".join(char_list)