我想在我当前的Python 3.5项目中使用类型提示.我的函数应该接收一个函数作为参数.
如何在类型提示中指定类型函数?
import typing
def my_function(name:typing.AnyStr, func: typing.Function) -> None:
# However, typing.Function does not exist.
# How can I specify the type function for the parameter `func`?
# do some processing
pass
Run Code Online (Sandbox Code Playgroud)
我检查了PEP 483,但在那里找不到函数类型提示.
我们希望在Django项目中了解印度的当前时间.在settings.py中,默认情况下是UTC.我们如何将其更改为IST?
我发现关于字典推导以下堆栈溢出后Python2.7
和Python 3+
:创建与Python列表理解的字典,说明我能够把字典解析如下:
d = {key: value for (key, value) in sequence}
Run Code Online (Sandbox Code Playgroud)
我在Python 3中尝试过它.但是,它引发了一个异常.
d = {'a':1, 'b':2, 'c':3, 'd':4}
{key : value for (key, value) in d}
{key : value for key, value in d}
Run Code Online (Sandbox Code Playgroud)
这两个版本都提出了一个ValueError
说法ValueError: need more than 1 value to unpack
.
在Python3中进行字典理解的最简单/最直接的方法是什么?
我想在我的Python程序中使用Type Hints.如何为复杂的数据结构创建类型提示
例
def names() -> list:
# I would like to specify that the list contains strings?
return ['Amelie', 'John', 'Carmen']
def numbers():
# Which type should I specify for `numbers()`?
for num in range(100):
yield num
Run Code Online (Sandbox Code Playgroud) 我尝试使用sphinx及其autodoc插件为包含数千个类的项目创建一个html文档.sphinx-apidoc创建了精彩的rst文件,但缺少自动创建autoclasses和automethods.
sphinx中有一个方法/命令/实用程序可以自动包含文档中的所有变量,函数,类和装饰器吗?
一个rst文件的一些示例代码:
tagger Package
=================
:mod:`tagger` Package
------------------------
.. automodule:: project.tagger
:members:
:mod:`client` Module
--------------------
.. automodule:: project.analyzers.tagger.client
:members:
Run Code Online (Sandbox Code Playgroud) 我想组织我的Scala包,并喜欢Python如何解决这个问题pip
.
你能推荐一个类似的工具来管理Scala包吗?
编辑: 我正在寻找一个简单的安装新包与所有它的依赖关系,如
>>> pip install <a_package> # installs a_package with all dependencies.
Run Code Online (Sandbox Code Playgroud) 我创建了一个Django应用程序,并且非常依赖于pytest
发现和组织我的单元和功能测试.但是,我希望将Behavior Driven with behave
Development应用于将来的测试.遗憾的是,behave
测试功能未被自动检测到pytest
.
我如何将behave
其测试集成到pytest
发现,执行和报告中?
我试图boilerpipe
用Python 运行multiprocessing
.这样做是为了解析来自多个来源的RSS源.问题是它在处理一些链接后挂在其中一个线程中.如果我删除池并在循环中运行它,整个流程都有效.
这是我的多处理代码:
proc_pool = Pool(processes=4)
for each_link in data:
proc_pool.apply_async(process_link_for_feeds, args=(each_link, ), callback=store_results_to_db)
proc_pool.close()
proc_pool.join()
Run Code Online (Sandbox Code Playgroud)
这是我boilerpipe
在里面调用的代码process_link_for_feeds()
:
def parse_using_bp(in_url):
extracted_html = ""
if ContentParser.url_skip_p.match(in_url):
return extracted_html
try:
extractor = Extractor(extractor='ArticleExtractor', url=in_url)
extracted_html = extractor.getHTML()
del extractor
except BaseException as e:
print "Something's wrong at Boilerpipe -->", in_url, "-->", e
extracted_html = ""
finally:
return extracted_html
Run Code Online (Sandbox Code Playgroud)
我对它悬挂的原因一无所知.proc_pool
代码中有什么问题吗?
我想使用Python prompt-toolkit
(https://github.com/jonathanslenders/python-prompt-toolkit)为我的命令行界面构建创建单元测试.
示例代码:
from os import path
from prompt_toolkit import prompt
def csv():
csv_path = prompt('\nselect csv> ')
full_path = path.abspath(csv_path)
return full_path
Run Code Online (Sandbox Code Playgroud) 我尝试用py.test收集我的测试,但它没有这样做.
我是否必须在命令行中提供其他选项?
Py.test在我的.py文件目录中执行.还有其他要求吗?
我的测试名称是否正确?在我的代码中,我使用'Test-'表示类,'test_'表示方法.
> py.test
=============================== in 0.00 seconds ============================
user@host:~/workspace/my_project/src/html_extractor$ py.test
============================= test session starts ===========================
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
plugins: xdist
collected 0 items
Run Code Online (Sandbox Code Playgroud)
class Factories(object):
TEST_LINKS = ['http://www.burgtheater.at',
'http://www.musikverein.at/']
def links(self):
for link in self.TEST_LINKS:
yield link
class TestHTMLExtractor(unittest.TestCase):
def setUp(self):
self.factory = Factories()
self.extractor = HTMLExtractor()
def test_extract_site_return(self):
for link in self.factory.links():
raw_html_page = self.extractor.run(link)
def test_whatever():
pass
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud) python ×9
python-3.x ×3
pytest ×2
python-3.5 ×2
type-hinting ×2
bdd ×1
boilerpipe ×1
django ×1
easy-install ×1
mypy ×1
pip ×1
python-2.7 ×1
scala ×1