小编Run*_*ith的帖子

解决新的 pip 回溯运行时问题

随 20.3 版一起发布的新 pip 依赖项解析器安装包的时间过长。昨天在我们的 CI 管道中,在 1 小时的 pip 安装消息之后,过去需要约 10 分钟的 docker 构建超时(几乎对于任何依赖项安装的每个库,都有类似的日志输出):

INFO: pip is looking at multiple versions of setuptools to determine which version is compatible with other requirements. This could take a while.
  Downloading setuptools-50.0.0-py3-none-any.whl (783 kB)
  Downloading setuptools-49.6.0-py3-none-any.whl (803 kB)
  Downloading setuptools-49.5.0-py3-none-any.whl (803 kB)
  Downloading setuptools-49.4.0-py3-none-any.whl (803 kB)
  Downloading setuptools-49.3.2-py3-none-any.whl (790 kB)
INFO: This is taking longer than usual. You might need to provide the dependency resolver with stricter constraints to reduce …
Run Code Online (Sandbox Code Playgroud)

python pip

74
推荐指数
3
解决办法
2万
查看次数

为什么 super() 没有实现 __call__?

想象一下python 3中的以下情况(我使用的是3.6):

class T(object):
    def __call__(self):
        return 5

class U(T):
    def __call__(self):
        return 10 + super()()

U()()
Run Code Online (Sandbox Code Playgroud)

这导致TypeError: 'super' object is not callable.

使其工作需要U定义如下:

class U(T):
    def __call__(self):
        return 10 + super().__call__()
Run Code Online (Sandbox Code Playgroud)

为什么第一个版本不起作用?我发现了这个相关的问题,但它没有回答这个问题。其实这个问题也在评论里问的有答案。

进一步阅读super() 实际返回的内容给我的印象是,返回的代理super()必须实现__call__并转发到正确的__call__实现。从这个答案中,我得到返回的代理对象super()只有一个__getattribute__方法可以返回任何父对象的正确函数。

如果我理解正确,这意味着代理只需要实现以下内容即可super()()工作:

def __call__(self, *args, **kwargs):
    try:
        return self["__call__"](*args, **kwargs)  # not sure if self is already bound to the method, if not that …
Run Code Online (Sandbox Code Playgroud)

python inheritance super

9
推荐指数
0
解决办法
716
查看次数

朋友班不工作

我得到典型的'...在此上下文中是私有'错误.你能告诉我我做错了什么吗?代码缩短了可读性.

在类SceneEditorWidgetController中:( settingsdialog和此处使用的变量在头文件中定义)

SceneEditorPluginWidgetController::SceneEditorPluginWidgetController()
{
}
void SceneEditorPluginWidgetController::configured()
{
    priorKnowledge_setting = settingsDialog->priorKnowledgeProxyFinder->getSelectedProxyName().toStdString(); //This is the context
}
Run Code Online (Sandbox Code Playgroud)

我的类SettingsController.h

namespace Ui {
    class SettingsController;
}
namespace GuiController {
    class SettingsController : public QDialog
    {
        Q_OBJECT
        friend class SceneEditorPluginWidgetController;
    public:
        explicit SettingsController(QWidget *parent = 0);
        ~SettingsController();

    private: //it is private here
        Ui::SettingsController* ui;
        IceProxyFinderBase* priorKnowledgeProxyFinder;
    };
}
Run Code Online (Sandbox Code Playgroud)

我无法修改IceProxyFinderBase类,但它之前使用的确如此(我可能是盲目的?)

有人可以解释一下我做错了什么吗?

c++ private class friend

7
推荐指数
1
解决办法
6343
查看次数

Kubeflow 管道终止通知

我尝试添加一个逻辑,当管道因某些错误而终止时,该逻辑将发送松弛通知。我试图用ExitHandler. 但是,似乎ExitHandler不能依赖任何操作。你有什么好主意吗?

kubernetes kubeflow kfp

7
推荐指数
1
解决办法
1676
查看次数

使用 OpenCV 在平面图中分隔房间

输入平面图图像

上面的图像是我输入的平面图,我需要分别识别每个房间,然后裁剪这些房间。之后,我可以将这些图像用于后续步骤。到目前为止,我能够使用 cv2.connectedComponentsWithStats 从输入平面图中删除小项目。所以我认为这将有助于轻松识别墙壁。之后我的输入图像看起来像这样。

去除小物体后的输出图像

然后我做了 MorphologicalTransform 从图像中删除文本和其他符号,只留下墙壁。之后我的输入图像看起来像这样。

形态变换后

所以我能够识别墙壁。然后我如何使用这些墙从原始输入平面图中裁剪房间。有人能帮我吗?您可以在此链接中找到我的 Python 代码。下载我的代码

#Import packages
import os
import cv2
import numpy as np
import tensorflow as tf
import sys

# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")

# Import utilites
from utils import label_map_util
from utils import visualization_utils as vis_util

# Name of the directory containing the object detection module we're using
MODEL_NAME = 'inference_graph'
IMAGE_NAME = 'floorplan2.jpg'
#Remove Small Items
im_gray = …
Run Code Online (Sandbox Code Playgroud)

python opencv image-processing

6
推荐指数
1
解决办法
6396
查看次数

访问python枚举成员时如何检测和调用函数

我有一个枚举,其中一些成员已被弃用:

from enum import Enum

class Foo(Enum):
    BAR = "bar"
    BAZ = "baz"  # deprecated

Run Code Online (Sandbox Code Playgroud)

它如何获得以下行为:

  • 当有人写作时Foo.BAR,一切都正常
  • 当有人写入时Foo.BAZDeprecationWarning使用warnings.warn("BAZ is deprecated", DeprecationWarning). 之后一切正常。
  • 当以其他方式访问成员时,应该应用相同的行为,例如Foo("baz")Foo["BAZ"]应该引发DeprecationWarning.

我尝试过但失败的事情:

  • 覆盖_missing_而不定义BAZ. 不起作用,因为最后我仍然需要返回现有成员一段时间(直到我们的数据库清除了已弃用的值)。但我不能动态地将成员添加到枚举中。如果我定义它,_missing_则不调用。
  • 覆盖任何__getattr__, __getattribute__. 这些在访问成员的属性时被调用,例如Foo.BAZ.boo,在访问时不调用Foo.BAZ。我想这可能是工作,如果我能改写__getattr__EnumMeta,然后进行Enum使用子元类。但是,我也不知道如何做到这一点
  • overwrite __class_getitem__:保留用于静态类型,无论如何都不会调用。
  • 滥用_generate_next_value_。此函数仅在创建类时调用,因此当类被调用一次时,无论是否调用已弃用的成员,我都会收到弃用警告。但这不是我想要的。
  • 看看这个问题。它不能解决我的问题,因为目标是在迭代过程中过滤已弃用的成员。

TLDR:访问枚举成员时如何检测和调用函数?

我正在使用 python 3.8,所以新功能很好。

python enums deprecation-warning

6
推荐指数
1
解决办法
384
查看次数

如何在 kubeflow 管道中定义管道级卷以在组件之间共享?

kubernetes容器间通信教程定义了以下管道 yaml:

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:

  restartPolicy: Never

  volumes:                      <--- This is what I need
  - name: shared-data
    emptyDir: {}

  containers:

  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
Run Code Online (Sandbox Code Playgroud)

请注意,volumes键是在 下定义的spec,因此该卷可用于所有定义的容器。我想使用kfp实现相同的行为,它是 kubeflow 管道的 API。

但是,我只能将卷添加到单个容器,而不能使用kfp.dsl.ContainerOp.container.add_volume_mount指向先前创建的卷(kfp.dsl.PipelineVolume)的整个工作流程规范,因为该卷似乎只在容器内定义。

这是我尝试过的,但卷始终在第一个容器中定义,而不是“全局”级别。我如何获取它以便op2可以访问该卷?我原以为它位于 …

python google-kubernetes-engine kubeflow-pipelines kfp

5
推荐指数
1
解决办法
5346
查看次数

如何全局种子 np.random.default_rng 进行单元测试

numpy 创建随机数的推荐方法是创建一个np.random.Generator像这样的

import numpy as np

def foo():
    # Some more complex logic here, this is the top level method that creates the rng
    rng = np.random.default_rng()
    return rng.random()
Run Code Online (Sandbox Code Playgroud)

现在假设我正在为我的代码库编写测试,并且我需要为 rng 提供种子以获得可重现的结果。

是否可以告诉 numpy 每次都使用相同的种子,无论在哪里default_rng()调用?这基本上是 的旧行为np.random.seed()。我需要这个的原因是因为我有很多这样的测试,并且必须模拟调用default_rng以对每个测试使用种子,因为在 pytest 中,您必须在使用某些东西的位置而不是定义它的位置进行模拟。因此,像这个答案一样在全球范围内嘲笑它是行不通的。

使用旧方法,可以定义一个固定装置,在 conftest.py 中自动设置每个测试的种子,如下所示:

# conftest.py

import pytest
import numpy as np

@pytest.fixture(autouse=True)
def set_random_seed():
    # seeds any random state in the tests, regardless where is is defined
    np.random.seed(0)
Run Code Online (Sandbox Code Playgroud)
# …
Run Code Online (Sandbox Code Playgroud)

python unit-testing numpy pytest random-seed

5
推荐指数
1
解决办法
2042
查看次数

定义一个行为类似于打字的自定义类型。Any

我需要创建一个类型,其行为类似于typing.Any类型检查器(mypy)查看时的行为,但与typing.Any.

用例是一些漂亮的“元”代码,需要从一组可以用 注释的其他变量中找到用此类型注释的变量typing.Any。请注意,我永远不需要实际创建此类型的实例,我只需要它在数据类上下文中进行类型注释。例子:

from dataclasses import dataclass, fields
from typing import Any


MyAny = ... # What to put here?

@dataclass()
class Test:

    a: Any
    b: MyAny = None


for field in fields(Test):
    if field.type == MyAny:
        print(f"It's {field.name}")   # This should print "It's b"
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情:

  1. 不起作用,因为你不能继承 Any:TypeError: Cannot subclass <class 'typing._SpecialForm'>

    class MyAny(Any):
       pass
    
    Run Code Online (Sandbox Code Playgroud)
  2. 不起作用,因为它与正常的 Any 没有区别(上面代码片段的结果是It's a\nIt's b

    MyAny = Any
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在运行时工作,但 mypy 抱怨默认值: Mypy: Incompatible types in assignment …

python types type-hinting mypy python-dataclasses

3
推荐指数
1
解决办法
1万
查看次数

如何在python中动态创建变量名并赋值?

我想动态分配变量(矩阵名称)并分配一些值。

下面是我正在尝试的示例代码。预期输出为 mat1 和 mat2,分别包含值 1 和 1。

stri = "mat"

for i in range(1,2):  
     "_".join([stri, i])=1
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
4499
查看次数