我想实现 Argparse 介绍中的示例:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args.accumulate(args.integers))
Run Code Online (Sandbox Code Playgroud)
但就我而言,我希望有更多可能的函数名称可供选择。
def a(): ...
def b(): ...
def c(): ...
parser.add_argument('-f', '--func',
choices=[a, b, c],
required=True,
help="""Choose one of the specified function to be run.""")
parser.func()
Run Code Online (Sandbox Code Playgroud)
像那样使用它不能按预期工作,我明白了
$ python program.py -f=a
program.py: error: argument -f/--func: invalid choice: 'a' (choose from
<function a at 0x7fa15f32f5f0>,
<function …Run Code Online (Sandbox Code Playgroud) 我用来ElementTree解析/构建一些稍微复杂但定义良好的 xml 文件,并用于mypy静态类型。我的.find陈述遍布各处,这导致了这样的事情:
from xml.etree.ElementTree import Element
...
root.find('tag_a').append(Element('tag_b'))
# run mypy..
-> type None from Optional[Element] has no attribute append
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为find根本找不到我给它的标签。但我知道它就在那里,并且不想添加类似try..exceptorassert语句之类的东西,本质上只是简单地保持沉默mypy,而不添加功能,同时使代码可读性较差。我也想避免# type: ignore到处发表评论。
我尝试了猴子修补Element.find.__annotations__,在我看来这将是一个很好的解决方案。但由于它是内置的,我不能这样做,而且子类化Element又感觉太多了。
有没有好的办法解决这个问题呢?
我有一个array_diff函数数组,如下所示:
Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
[5] => in
[6] => our
)
Run Code Online (Sandbox Code Playgroud)
如您所见,我们在键#3和#5之间存在间隙(即没有键#4).如何将该阵列分成两部分,如果有更多间隙,可能会更多?预期的产出是:
Array
(
[0] => Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
)
[1] => Array
(
[0] => in
[1] => our
)
)
Run Code Online (Sandbox Code Playgroud) 首先,手头的问题。我正在为一个scikit-learn类编写一个包装器,并且在使用正确的语法时遇到了问题。我想要实现的是fit_transform函数的覆盖,它只稍微改变输入,然后super用新参数调用它的-method:
from sklearn.feature_extraction.text import TfidfVectorizer
class TidfVectorizerWrapper(TfidfVectorizer):
def __init__(self):
TfidfVectorizer.__init__(self) # is this even necessary?
def fit_transform(self, x, y=None, **fit_params):
x = [content.split('\t')[0] for content in x] # filtering the input
return TfidfVectorizer.fit_transform(self, x, y, fit_params)
# this is the critical part, my IDE tells me for
# fit_params: 'unexpected arguments'
Run Code Online (Sandbox Code Playgroud)
程序到处崩溃,从 开始Multiprocessing exception,并没有真正告诉我任何有用的信息。我该如何正确地做到这一点?
附加信息:我需要以这种方式包装它的原因是因为我sklearn.pipeline.FeatureUnion在将它们放入sklearn.pipeline.Pipeline. 这样做的结果是,我只能在所有特征提取器中提供单个数据集——但不同的提取器需要不同的数据。我的解决方案是以易于分离的格式提供数据,并在不同的提取器中过滤不同的部分。如果这个问题有更好的解决方案,我也很高兴听到。
编辑 1:添加**解压 dict 似乎没有改变任何东西:

编辑 2:我刚刚解决了剩下的问题——我需要删除构造函数重载。显然,通过尝试调用父构造函数,希望正确启动所有实例变量,我做了完全相反的事情。我的包装器不知道它可以期待什么样的参数。一旦我删除了多余的电话,一切都很顺利。
好的,所以我在包内创建了一个 python 脚本。这棵树看起来像这样:
\n\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 funcs\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 stuff.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 resources\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 haarcascade_frontalface_default.xml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 scripts\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 __init__.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 script.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 setup.py\nRun Code Online (Sandbox Code Playgroud)\n\n因此,在脚本文件中,我使用 openCV 的 cv2 来检测人脸,为此需要cv2.CascadeClassifier位于 /resources 下的 XML 文件的路径。现在因为这是一个脚本,我需要能够从任何地方运行它,所以遗憾的是资源文件的相对路径并不能解决问题。如何从 script.py 中获取 xml 文件的绝对路径?您可以假设脚本和 xml 文件分别相对于彼此定位,就像上面的示例一样。谢谢 :))
PS:如果该解决方案也适用于鸡蛋,那就更好了。非常感激
\n我注意到 Sphinx 呈现类描述的行为发生了变化。鉴于此代码
# my example happens to be a dataclass, but the behavior for
# regular classes is the same
@dataclass
class TestClass:
"""This is a test class for dataclasses.
This is the body of the docstring description.
"""
var_int: int
var_str: str
Run Code Online (Sandbox Code Playgroud)
加上一些通用的狮身人面像设置,我大约两年前就得到了这个
现在我得到了这个
有没有办法告诉 Sphinx 不要将类变量添加到类定义的底部?尤其令人烦恼的是,它假设它们的值为None,只是因为它们没有默认值。
这个问题是在这篇文章的讨论中出现的,其中还包含有关 Sphinx 配置等的评论中的更多上下文。
python python-sphinx autodoc sphinx-napoleon python-dataclasses
我有一张类型的地图
Map<Character, Integer> tmp;
Run Code Online (Sandbox Code Playgroud)
在我的代码中的某些时候,我执行以下操作:
if(tmp.containsKey(key))
tmp.put(key, tmp.get(key)+1)
Run Code Online (Sandbox Code Playgroud)
在第二行,我更愿意做类似的事情
tmp.get(key)++;
Run Code Online (Sandbox Code Playgroud)
我期望工作,因为我应该从get-call获得对Integer对象的引用.但事实并非如此,除了int语法之外,Integer似乎没有更新功能.围绕第一个构造没有办法吗?
我在 ubuntu 上使用 pycharm 3.4 版,并且有一个具有以下结构的项目:
~/project/src/python/utils/sub1/sub2/sub3/my_code.py
utils 文件夹还包含一个__init__.py提供许多实用功能的文件。我想包括其中一些,但它不会找到它:
from project.src.python.utils import read_utf8
Run Code Online (Sandbox Code Playgroud)
我关注了这篇文章,它似乎讨论了同样的问题: 如果我打开一个不是 Django 根目录的目录,PyCharm 找不到正确的路径
但是更改~/project为“源”文件夹并没有帮助。这不是我的错别字,因为它至少应该在尝试从中导入某些内容时找到“项目”,但是
import project
Run Code Online (Sandbox Code Playgroud)
也给了我一个“未解决的参考”错误。
编辑:我应该补充一点,我无法更改代码,因为它是一个共享项目。我需要那条确切的导入线才能在我的机器上工作。我的对手使用 eclipse,如果添加附加代码的路径似乎更容易。
其记录的属性是:
- [...]
- type:字段的类型。
对我来说,这似乎意味着该字段将包含类型本身,而不仅仅是字符串形式的名称。
但是,它似乎只是按原样复制类型注释,使其变得毫无用处。
例子:
@dataclasses.dataclass
class C:
c: 'C'
dataclasses.fields(C)[0].type # This returns the string 'C'
typing.get_type_hints(C)['c'] # This returns the class C, as expected
Run Code Online (Sandbox Code Playgroud)
该问题甚至在使用PEP563类型注释时系统性地发生。
这是数据类模块中的错误吗?这是预期的行为吗?如果是这样,我如何检索给定 Field 实例的类型对象?
我想在本地运行一个 dockerized Flask 服务器并访问我在其中定义的任何路由。设置重现我的问题:
应用程序
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello World!'
Run Code Online (Sandbox Code Playgroud)
文件
from python:3.7-alpine
COPY app.py app.py
RUN pip install flask
CMD [ "flask", "run" ]
Run Code Online (Sandbox Code Playgroud)
构建这个容器工作正常。按照文档和一些深入的示例,运行它docker run -p 5000:5000 flask应该可以解决问题。它启动容器并且看起来不错:
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Run Code Online (Sandbox Code Playgroud)
而docker ps让我发现,端口映射也似乎工作: …