小编pal*_*asb的帖子

`pub(self)`的可见性与没有`pub`属性有什么不同?

看到函数的可见性可以声明pub(self)在模块中.这与没有pub属性的私有函数有什么不同?如果它们没有不同,为什么存在这种语法?

visibility rust

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

计算C#素数的最快方法?

我实际上有一个问题的答案,但它没有并行化,所以我对改进算法的方法很感兴趣.无论如何,它对某些人来说可能是有用的.

int Until = 20000000;
BitArray PrimeBits = new BitArray(Until, true);

/*
 * Sieve of Eratosthenes
 * PrimeBits is a simple BitArray where all bit is an integer
 * and we mark composite numbers as false
 */

PrimeBits.Set(0, false); // You don't actually need this, just
PrimeBits.Set(1, false); // remindig you that 2 is the smallest prime

for (int P = 2; P < (int)Math.Sqrt(Until) + 1; P++)
    if (PrimeBits.Get(P))
        // These are going to be the multiples of P …
Run Code Online (Sandbox Code Playgroud)

.net c# algorithm performance bitarray

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

一元增量算子的原子行为

在某处我读到一元运算符本质上是原子的,所以它们可以在多线程环境中使用.为了证实这一点,我写了两个单独的程序

  1. 我使用变量x并使用一元运算符++ x递增
  2. 我使用变量x并使用x = x + 1递增

我比较了两个程序的反汇编,发现没有区别.请提供您的意见.

c c++ atomic increment

8
推荐指数
3
解决办法
281
查看次数

尝试导入 cv2(opencv-python) 包时出错

我正在尝试使用 cv2(opencv-python) 包访问我的网络摄像头。

当我尝试导入它时,出现此错误:

Traceback (most recent call last):
  File "server.py", line 6, in <module>
    import cv2
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)

注意:我正在尝试在 Linode 服务器上的腻子上导入此包 - 这可能是有用的信息。

如果有人可以向我解释发生了什么,也许可以解决问题,我将不胜感激!

python-3.x cv2 opencv-python

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

为返回参数类型的 Python 函数指定类型注释

如何正确键入注释下面的函数?

def f(cls: type) -> ???:
    return cls()

# Example usage:
assert f(int) == 0
assert f(list) == []
assert f(tuple) == ()
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以???使用涉及的内容进行类型注释cls,而不仅仅是Any或省略返回类型注释?如果我必须更改参数的类型注释也可以cls

python dependent-type python-typing

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

在 tf 函数内迭代 tf.Tensor 以生成基于 NamedTuple 的数据集项列表

typing.NamedTupletf.data.Dataset. 下面是一个例子。

# You can run all the code in this question by pasting all
# the code blocks consecutively into a Python file

import tensorflow as tf
from typing import *
from random import *
from pprint import *

class Coord(NamedTuple):
    x: float
    y: float

    @classmethod
    def random(cls): return cls(gauss(10., 1.), gauss(10., 1.))

class Box(NamedTuple):
    min: Coord
    max: Coord

    @classmethod
    def random(cls): return cls(Coord.random(), Coord.random())

class Boxes(NamedTuple):
    boxes: List[Box]

    @classmethod
    def random(cls): return cls([Box.random() for …
Run Code Online (Sandbox Code Playgroud)

python tensorflow tensorflow2.0

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

在我自己的名称空间中定义size_t会造成歧义或其他错误吗?

我有以下的代码定义size_t为等价的std::size_t::size_t,如果我包括<cstddef>

// h.hpp
namespace N {
  using size_t = decltype(sizeof(int));
}
Run Code Online (Sandbox Code Playgroud)
// a.hpp
#include <h.hpp>
namespace N {
  class C {
    size_t size() const;
  };
  void f(size_t);
}
// ^^^ These use N::size_t!
Run Code Online (Sandbox Code Playgroud)

这是否以任何方式违反C ++标准,并且会在使用这些标头和定义std::size_t和的其他标准标头的代码中引起任何错误::size_t吗?如果有人在任何情况下都无法使用std::size_t并且N::size_t可以互换,或者仅size_t在任何情况下使用都会引起歧义,我也将认为它是一个错误。

// someOtherFile.cpp
#include <a.hpp>
#include <cstdint>
namespace N {
  // Can there be any problem here? (Inside N::)
}
// Or here? (Outside N::)
Run Code Online (Sandbox Code Playgroud)

我的猜测不会是因为我和标准size_t …

c++ namespaces name-lookup type-alias

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

如何在Python中运行标记为“@unittest.skip”的测试用例?

假设有以下测试套件:

# test_module.py
import unittest

class Tests(unittest.TestCase):

  @unittest.skip
  def test_1(self):
    print("This should run only if explicitly asked to but not by default")

  # assume many other test cases and methods with or without the skip marker
Run Code Online (Sandbox Code Playgroud)

当通过调用unittest库时,python -m unittest是否有任何参数可以传递给它实际运行而不是跳过Tests.test_1而不修改测试代码并运行任何其他跳过的测试?

python -m unittest test_module.Tests.test_1正确地选择此作为唯一要运行的测试,但它仍然会跳过它。

如果不修改测试代码就无法做到这一点,那么我可以做的最惯用的更改是什么来有条件地撤消@unittest.skip并运行一个特定的测试用例?

在所有情况下,我仍然希望python -m unittest discover(或任何其他未显式打开测试的调用)跳过测试。

python python-unittest

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