让我通过我的确切代码:这是短模块
class SentenceSplitter:
def __init__(self, filename=None):
self._raw_text = self.raw_text(filename)
self._sentences = self.to_sentences()
def raw_text(self, filename):
text = ''
with open(filename, 'r') as file:
for line in file.readlines():
line = line.strip()
text += ''.join(line.replace(line, line+' '))
file.close()
text = text.strip() # Deal with the last whitespace
return text
def to_sentences(self):
""" Sentence boundaries occur at '.', '!', '?' except that,
there are some not-sentence boundaries that
may occur before/after the period.
"""
raw_text = self._raw_text
sentences = []
sentence = '' …Run Code Online (Sandbox Code Playgroud) 以下是我必须完成的代码.所以,我想要的结果是读取文件(每一行),如果我在文件中读取的行的第一个字母与我选择的字母匹配,则返回列表中的行匹配.
def lines_startswith(file,letter):
'''(file open for reading, str) -> list of str
Return the list of lines from file that begin with letter. The lines should have the newline removed.
Precondition:len(letter) == 1
'''
matches = []
(blank)
return matches
Run Code Online (Sandbox Code Playgroud)
我必须填写(空白)来完成代码.
这是我到目前为止,但我无法得到我需要的结果.
for line in file:
if line[0] == 'letter':
matches.append(line)
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?
我有一个类有几个类似的方法,每个方法都有相似的长文档字符串,但在几个短语/单词方面有所不同.我想构建一个docstring模板,然后将字符串格式应用于它.下面是一个笨拙的实现,其中__doc__s是在类方法之后定义的.
capture_doc = """
%(direc)s normal.
a %(sym)s b."""
class Cls():
def a(self):
pass
def b(self):
pass
a.__doc__ = capture_doc % {'direc' : 'below', 'sym' : '<'}
b.__doc__ = capture_doc % {'direc' : 'above', 'sym' : '>'}
c = Cls()
print(c.a.__doc__)
below normal.
a < b.
Run Code Online (Sandbox Code Playgroud)
问题:是否有Python文档或PEP规定的方法来执行此操作?我想保持基本的东西,我见过使用@Appender 装饰师,但认为这对我的需求有点花哨.
我有一个对象是字典的字典。我想告诉我的编辑这个对象是这样的,我该怎么做?目前我有:
def do_somthing(dict_of_dicts):
"""
:type dict_of_dicts: dict of dict
"""
for key, value in dict_of_dicts.items():
for key2, value2 in value.items():
print('Key')
Run Code Online (Sandbox Code Playgroud)
但是,我的编辑器(Pycharm)似乎没有注册这个。