Perl允许我使用 __DATA__
脚本中的标记,用于标记数据块的开头。我可以使用DATA文件句柄读取数据。在脚本中存储数据块的Pythonic方法是什么?
Perl允许我使用 __DATA__
脚本中的标记,用于标记数据块的开头。我可以使用DATA文件句柄读取数据。在脚本中存储数据块的Pythonic方法是什么?
这取决于你的数据,但dict文字和多行字符串都是非常好的方法。
state_abbr = {
'MA': 'Massachusetts',
'MI': 'Michigan',
'MS': 'Mississippi',
'MN': 'Minnesota',
'MO': 'Missouri',
}
gettysburg = """
Four score and seven years ago,
our fathers brought forth on this continent
a new nation,
conceived in liberty
and dedicated to the proposition
that all men are created equal.
"""
使用StringIO模块创建类似源文件的对象:
from StringIO import StringIO
textdata = """\
Now is the winter of our discontent,
Made glorious summer by this sun of York.
"""
# in place of __DATA__ = open('richard3.txt')
__DATA__ = StringIO(textdata)
for d in __DATA__:
print d
__DATA__.seek(0)
print __DATA__.readline()
打印:
Now is the winter of our discontent,
Made glorious summer by this sun of York.
Now is the winter of our discontent,
(我刚刚打电话给你 __DATA__
与原始问题保持一致。在实践中,这不是很好的Python命名风格 - 类似于 datafile
会更合适。)
不熟悉Perl的 __DATA__
变量谷歌告诉我它经常用于测试。假设您也在考虑测试代码,您可能需要考虑doctest(http://docs.python.org/library/doctest.html)。例如,而不是
import StringIO
__DATA__ = StringIO.StringIO("""lines
of data
from a file
""")
假设你想要 数据 成为一个文件对象,现在你已经拥有了,你可以像往常一样使用大多数其他文件对象。例如:
if __name__=="__main__":
# test myfunc with test data:
lines = __DATA__.readlines()
myfunc(lines)
但如果只使用 数据 用于测试你最好在PyUnit / Nose中创建doctest或编写测试用例。
例如:
import StringIO
def myfunc(lines):
r"""Do something to each line
Here's an example:
>>> data = StringIO.StringIO("line 1\nline 2\n")
>>> myfunc(data)
['1', '2']
"""
return [line[-2] for line in lines]
if __name__ == "__main__":
import doctest
doctest.testmod()
运行这样的测试:
$ python ~/doctest_example.py -v
Trying:
data = StringIO.StringIO("line 1\nline 2\n")
Expecting nothing
ok
Trying:
myfunc(data)
Expecting:
['1', '2']
ok
1 items had no tests:
__main__
1 items passed all tests:
2 tests in __main__.myfunc
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
Doctest做了很多不同的事情,包括在纯文本文件中查找python测试并运行它们。就个人而言,我不是一个大粉丝,更喜欢更有条理的测试方法(import unittest
但它确实是一种测试代码的pythonic方法。
IMO它在很大程度上取决于数据的类型:如果你只有文本,并且可以确定没有任何机会在里面的“'或”“”,你可以使用这个版本来存储文本。但是例如,如果您愿意,可以将某些文本存储在已知或可能存在“或”或“”的地方?那么它是值得建议的
示例:文本是
Python库中有许多和“”。
在这种情况下,通过三重报价可能很难做到这一点。所以你可以做到
__DATA__ = """There are many '''s and \"""s in Python libraries.""";
print __DATA__
但是在编辑或替换文本时你必须注意。 在这种情况下,它可能更有用
$ python -c 'import sys; print sys.stdin.read().encode("base64")'
There are many '''s and """s in Python libraries.<press Ctrl-D twice>
那你得到
VGhlcmUgYXJlIG1hbnkgJycncyBhbmQgIiIicyBpbiBQeXRob24gbGlicmFyaWVzLg==
作为输出。拿这个并把它放到你的脚本中,例如
__DATA__ = 'VGhlcmUgYXJlIG1hbnkgJycncyBhbmQgIiIicyBpbiBQeXRob24gbGlicmFyaWVzLg=='.decode('base64')
print __DATA__
并看到结果。