字符串处理和百分比运算符 [英] String handling and the percent operator
问题描述
我有一些代码可以自动生成一些样板代码,因此当我想创建一个新模块时,我不需要b $ b需要执行繁琐的设置。
>
因此,我的脚本会提示用户输入模块名称,然后打开两个
文件,这些文件各自获取其中一个功能的内容:
def GetPyContents(模块):
样板= \
""
class%s:
通过
if __name__ ==''__ main__'':
import unittest
unittest.main (''%s_t'')
"""
返回样板%((模块,)* 2)
def GetTestContents(模块):
样板= \
"""%s import *
import unittest
class Test%s(unittest.TestCase):
def testConstruction(self):
self。 failUnless(%s())
def testWriteMoreTests(se lf):
self.fail(''此测试应该失败。'')
if __name__ ==''__ main __'':
unittest.main()
"""
返回样板%((模块,)* 3)
我的问题是,我不喜欢硬编码
模块名称应该在两个返回函数中重复的次数。有没有
直接(内联适当)的方式来计算''样板'字符串中
''%s''的数量? ...或者可能是另一种更多
Pythonic的方法来做到这一点? (也许我可以用某种方式使用发电机?)
thx。
-tom!
Tom Plunket写道:
我有一些代码可以自动生成一些样板代码,这样我就不需要
了当我想创建一个新模块时,做一些繁琐的设置。
所以,我的脚本提示用户输入模块名称,然后打开两个
文件和那些文件各自获得以下功能之一的内容:
def GetPyContents(模块):
样板= \
""
class%s:
pass
if __name__ ==''__ main__'' :
import unittest
unittest.main(''%s_t'')
"""
返回样板%((模块)* 2)
def GetTestContents(模块):
样板= \
"""%s import *
import unittest
class Test%s(unittest.TestCase):
def testConstruction(self):
self.failUnless(%s())
def testWriteMoreTests(self):
self.fail(''此测试应该失败。''')
if __name__ ==''__ main__'':
unittest.main()
" "
返回样板%((模块,)* 3)
我的问题是,我不喜欢硬编码
模块名称应在两个返回函数中重复的次数。有没有
直接(内联适当)的方式来计算''样板'字符串中
''%s''的数量? ...或者可能是另一种更多
Pythonic的方法来做到这一点? (也许我可以用某种方式使用发电机?)
thx。
-tom!
字符串有一个count()方法。
因为你知道你不会有像''这样的东西在你的
样板中%% s'',使用它是完全合理的:
返回样板%((模块)*样板文件(''%s''))
代码。
和平,
~Simon
返回样板%((模块)* 3)
Simon Forman写道:
< blockquote class =post_quotes>
字符串有一个count()方法。
谢谢!
为了丰富目的,有没有办法用
$来做这类事情b $ ba发电机?例如。类似于:
def SentenceGenerator():
words = [''我',''有'',''已'',' 'to'','''',''fair'']
for w in words:
yield w
message ="%s%s%s%s"
打印消息%SentenceGenerator()
(我问因为以上都不行?)
-tom!
Tom Plunket写道:
为了浓缩目的,有没有办法用
a发电机做这种事情?例如。类似于:
def SentenceGenerator():
words = [''我',''有'',''已'',' 'to'','''',''fair'']
for w in words:
yield w
message ="%s%s%s%s"
打印消息%SentenceGenerator()
(我问因为上面不起作用)?
使用元组(SentenceGenerator())。生成器只是另一个对象,因此使用%运算符的
会尝试将其替换为一个值。 (即使
这个修复,你的消息也没有足够的格式化程序。)
-
Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/
美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM erikmaxfrancis
人类的救赎掌握在创造性失调的手中。
- 小马丁·路德金博士
>
I have some code to autogenerate some boilerplate code so that I don''t
need to do the tedious setup stuff when I want to create a new module.
So, my script prompts the user for the module name, then opens two
files and those files each get the contents of one of these functions:
def GetPyContents(module):
boilerplate = \
"""
class %s:
pass
if __name__ == ''__main__'':
import unittest
unittest.main(''%s_t'')
"""
return boilerplate % ((module,) * 2)
def GetTestContents(module):
boilerplate = \
"""from %s import *
import unittest
class Test%s(unittest.TestCase):
def testConstruction(self):
self.failUnless(%s())
def testWriteMoreTests(self):
self.fail(''This test should fail.'')
if __name__ == ''__main__'':
unittest.main()
"""
return boilerplate % ((module,) * 3)
My question is, I don''t like hardcoding the number of times that the
module name should be repeated in the two return functions. Is there
an straight forward (inline-appropriate) way to count the number of
''%s''es in the ''boilerplate'' strings? ...or maybe a different and more
Pythonic way to do this? (Maybe I could somehow use generators?)
thx.
-tom!
Tom Plunket wrote:I have some code to autogenerate some boilerplate code so that I don''t
need to do the tedious setup stuff when I want to create a new module.
So, my script prompts the user for the module name, then opens two
files and those files each get the contents of one of these functions:
def GetPyContents(module):
boilerplate = \
"""
class %s:
pass
if __name__ == ''__main__'':
import unittest
unittest.main(''%s_t'')
"""
return boilerplate % ((module,) * 2)
def GetTestContents(module):
boilerplate = \
"""from %s import *
import unittest
class Test%s(unittest.TestCase):
def testConstruction(self):
self.failUnless(%s())
def testWriteMoreTests(self):
self.fail(''This test should fail.'')
if __name__ == ''__main__'':
unittest.main()
"""
return boilerplate % ((module,) * 3)
My question is, I don''t like hardcoding the number of times that the
module name should be repeated in the two return functions. Is there
an straight forward (inline-appropriate) way to count the number of
''%s''es in the ''boilerplate'' strings? ...or maybe a different and more
Pythonic way to do this? (Maybe I could somehow use generators?)
thx.
-tom!
strings have a count() method.
Since you know that you won''t have things like ''%%s'' in your
boilerplate, it''s perfectly reasonable to use:
return boilerplate % ((module,) * boilerplate.count(''%s''))
in your code.
Peace,
~Simon
return boilerplate % ((module,) * 3)
Simon Forman wrote:
strings have a count() method.thanks!
For enrichment purposes, is there a way to do this sort of thing with
a generator? E.g. something like:
def SentenceGenerator():
words = [''I'', ''have'', ''been'', ''to'', ''the'', ''fair'']
for w in words:
yield w
message = "%s %s %s %s"
print message % SentenceGenerator()
(I ask because the above doesn''t work)?
-tom!
Tom Plunket wrote:
For enrichment purposes, is there a way to do this sort of thing with
a generator? E.g. something like:
def SentenceGenerator():
words = [''I'', ''have'', ''been'', ''to'', ''the'', ''fair'']
for w in words:
yield w
message = "%s %s %s %s"
print message % SentenceGenerator()
(I ask because the above doesn''t work)?Use tuple(SentenceGenerator()). A generator is just another object, so
using it with the % operator tries to substitute it at one value. (Even
with this fix, though, your message didn''t have enough formatters.)
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Human salvation lies in the hands of the creatively maladjusted.
-- Dr. Martin Luther King, Jr.
这篇关于字符串处理和百分比运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!