草庐IT

Hotel_Name

全部标签

python - 如何测试或模拟 "if __name__ == ' __main_ _'"内容

假设我有一个包含以下内容的模块:defmain():passif__name__=="__main__":main()我想为下半部分编写一个单元测试(我想实现100%的覆盖率)。我发现了执行import/__name__设置机制的runpy内置模块,但我不知道如何模拟或以其他方式检查main()函数被调用。这是我迄今为止尝试过的:importrunpyimportmock@mock.patch('foobar.main')deftest_main(self,main):runpy.run_module('foobar',run_name='__main__')main.assert_c

python - 导入错误 : cannot import name HTTPSHandler using PIP

使用pip安装python包时遇到HTTPSHandler错误,以下是堆栈跟踪,--------desktop:~$pipinstallDjango==1.3Traceback(mostrecentcalllast):File"/home/env/.genv/bin/pip",line9,inload_entry_point('pip==1.4.1','console_scripts','pip')()File"/home/env/.genv/lib/python2.7/site-packages/pkg_resources.py",line378,inload_entry_point

python - 导入错误 : cannot import name HTTPSHandler using PIP

使用pip安装python包时遇到HTTPSHandler错误,以下是堆栈跟踪,--------desktop:~$pipinstallDjango==1.3Traceback(mostrecentcalllast):File"/home/env/.genv/bin/pip",line9,inload_entry_point('pip==1.4.1','console_scripts','pip')()File"/home/env/.genv/lib/python2.7/site-packages/pkg_resources.py",line378,inload_entry_point

python - UnicodeEncodeError : 'ascii' codec can't encode character at special name

这个问题在这里已经有了答案:UnicodeEncodeError:'ascii'codeccan'tencodecharacteru'\xa0'inposition20:ordinalnotinrange(128)(34个回答)关闭6年前。我的python(ver2.7)脚本运行良好,可以从本地html文件中获取一些公司名称,但是当涉及到一些特定的国家名称时,它会给出这个错误“UnicodeEncodeError:'ascii'codeccan'tencodecharacter”当这个公司名称出现时特别出错公司名称:KühlfixKälteanlagenIng.GerhardDocze

python - UnicodeEncodeError : 'ascii' codec can't encode character at special name

这个问题在这里已经有了答案:UnicodeEncodeError:'ascii'codeccan'tencodecharacteru'\xa0'inposition20:ordinalnotinrange(128)(34个回答)关闭6年前。我的python(ver2.7)脚本运行良好,可以从本地html文件中获取一些公司名称,但是当涉及到一些特定的国家名称时,它会给出这个错误“UnicodeEncodeError:'ascii'codeccan'tencodecharacter”当这个公司名称出现时特别出错公司名称:KühlfixKälteanlagenIng.GerhardDocze

python - variable_scope 和 name_scope 有什么区别?

这个问题在这里已经有了答案:What'sthedifferenceofnamescopeandavariablescopeintensorflow?(8个回答)关闭3年前.variable_scope和name_scope有什么区别?variablescopetutorial谈论variable_scope隐式打开name_scope。我还注意到,在name_scope中创建变量会自动使用范围名称扩展其名称。那么,有什么区别呢? 最佳答案 我无法理解variable_scope之间的区别和name_scope(它们看起来几乎一样)在

python - variable_scope 和 name_scope 有什么区别?

这个问题在这里已经有了答案:What'sthedifferenceofnamescopeandavariablescopeintensorflow?(8个回答)关闭3年前.variable_scope和name_scope有什么区别?variablescopetutorial谈论variable_scope隐式打开name_scope。我还注意到,在name_scope中创建变量会自动使用范围名称扩展其名称。那么,有什么区别呢? 最佳答案 我无法理解variable_scope之间的区别和name_scope(它们看起来几乎一样)在

python - 使用 BeautifulSoup 根据 name 属性获取属性值

我想根据名字打印一个属性值,举个例子我想做这样的事情soup=BeautifulSoup(f)#fissomeHTMLcontainingtheabovemetatagformeta_taginsoup("meta"):ifmeta_tag["name"]=="City":print(meta_tag["content"])上面的代码给出了一个KeyError:'name',我相信这是因为name被BeatifulSoup使用了,所以它不能作为关键字参数。 最佳答案 很简单,使用如下:>>>frombs4importBeautifu

python - 使用 BeautifulSoup 根据 name 属性获取属性值

我想根据名字打印一个属性值,举个例子我想做这样的事情soup=BeautifulSoup(f)#fissomeHTMLcontainingtheabovemetatagformeta_taginsoup("meta"):ifmeta_tag["name"]=="City":print(meta_tag["content"])上面的代码给出了一个KeyError:'name',我相信这是因为name被BeatifulSoup使用了,所以它不能作为关键字参数。 最佳答案 很简单,使用如下:>>>frombs4importBeautifu

python - 获取 "global name ' foo' is not defined"with Python's timeit

我想知道执行一条Python语句需要多少时间,所以上网查了一下,发现标准库提供了一个模块,叫做timeit声称正是这样做的:importtimeitdeffoo():#...containscodeIwanttotime...defdotime():t=timeit.Timer("foo()")time=t.timeit(1)print"took%fs\n"%(time,)dotime()但是,这会产生错误:Traceback(mostrecentcalllast):File"",line1,inFile"",line3,indotimeFile"/usr/local/lib/pyth