我创建了一个数据类Foo,它接受任何可以转换为的类型int:
import dataclasses
@dataclasses.dataclass
class Foo:
a: int
def __post_init__(self):
# Here `self.a` is converted to int, so this class accepts any type that can be converted to int
self.a = int(self.a)
# mypy error: Argument 1 to "Foo" has incompatible type "str"; expected "int",
foo = Foo("1")
print(foo)
print(foo.a + 2)
Run Code Online (Sandbox Code Playgroud)
输出:
Foo(a=1)
3
Run Code Online (Sandbox Code Playgroud)
但是,mypy 报告以下错误:
error: Argument 1 to "Foo" has incompatible type "str"; expected "int"
Run Code Online (Sandbox Code Playgroud)
Foo.a如果我修复to的类型Union[str, int],mypy 会报告另一个错误:
error: Unsupported …Run Code Online (Sandbox Code Playgroud) 嗨,我是Tensorflow的新手.
我想改变Tensor的维度,我找到了3种类型的方法来实现它,如下所示:
a = tf.constant([[1,2,3],[4,5,6]]) # shape (2,3)
# change dimention of a to (2,3,1)
b = tf.expand_dims(a,2) # shape(2,3,1)
c = a[:,:,tf.newaxis] # shape(2,3,1)
d = tf.reshape(a,(2,3,1)) # shape(2,3,1)
Run Code Online (Sandbox Code Playgroud)
3种方法之间是否有任何差异,例如在性能方面?
我应该使用哪种方法?
VSCode 中的Markdown是否有路径自动完成扩展?
我尝试了Path Intellisense但这似乎仅在使用(双)引号时才有效。
我想在插入存储在本地目录中的图像时使用自动完成功能,例如:

Run Code Online (Sandbox Code Playgroud) 我想在我的主机上运行的 Jupyter Notebook 的 docker 容器中使用 ipython 内核。
我知道我可以通过将 Jupyter 安装到容器中来使用内核,但这不是我想做的。那是因为我正在使用 Jupyter Notebook 的一些扩展(例如 vim),并且我想在使用 docker 容器内的内核时使用它们。
我该怎么办?
我想检查一个路径是否是另一个路径的子目录:
use std::path::Path;
let path = Path::new("/foo/bar/");
let child = Path::new("/foo/bar/baz");
assert_eq!(is_subdirectory(path, child), true);
Run Code Online (Sandbox Code Playgroud)
这个怎么做?
我写了下面的代码,但我不能写生命时间约束来工作并得到一个错误:
use futures::Future;
async fn foo<'a>(a: &'a str) -> &'a str {
let task = get();
f(a, task).await
}
async fn f<T>(v: T, task: impl Future<Output = T>) -> T {
if true {
v
} else {
task.await
}
}
async fn get() -> &'static str {
"foo"
}
Run Code Online (Sandbox Code Playgroud)
错误:
error[E0759]: `a` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
--> src/lib.rs:3:18
|
3 | async fn foo<'a>(a: &'a str) -> &'a str {
| …Run Code Online (Sandbox Code Playgroud) 我想在 docker 容器中使用 python 环境,同时在主机上编辑源文件。我知道 VSCode 提供devcontainer功能,但我想使用 VIM 和 coc-pyright。怎么做?
我正在使用 actix-web 构建一个 Web 服务器,其中一种方法使用 reqwest 向外部 API 发出 HTTP 请求:
#[get("/foo")]
async fn foo() -> impl Responder {
let resp = reqwest::get("https://externalapi.com/bar").await?; # GET request inside server
...
}
Run Code Online (Sandbox Code Playgroud)
为了提高性能,我想重用 reqwest 的客户端,因为根据doc ,它拥有一个连接池。但是,我无法使用Arc共享客户端,因为文档还有以下声明:
您不必将 Client 包装在 Rc 或 Arc 中即可重用它,因为它已经在内部使用了 Arc。
如何在函数调用之间共享客户端?或者,我应该使用不同的库在 Web 服务器内创建 HTTP 请求吗?
我想输入与上面一行中的字符数一样多的特定字符。
前:
FooBar
Run Code Online (Sandbox Code Playgroud)
后:
FooBar
------
Run Code Online (Sandbox Code Playgroud)
我想用这个函数写reStructuredText
我创建了一个命令memo如下:
memo() {
vi $HOME/memo/$1
}
Run Code Online (Sandbox Code Playgroud)
我想将 bash-completion 应用到我memo要打开的目录中的$HOME/memo文件:
$ memo [TAB] # to show files in $HOME/memo
Run Code Online (Sandbox Code Playgroud)
$HOME/memo包含目录,因此列出其下的文件memo是不够的。换句话说,我想将ls命令中使用的内容应用$HOME/memo到memo:
$ ls [TAB]
foo.md bar/
Run Code Online (Sandbox Code Playgroud)
我尝试了以下但它不适用于嵌套目录:
_memo() {
local cur
local files
_get_comp_words_by_ref -n : cur
files=$(ls $MEMODIR)
COMPREPLY=( $(compgen -W "${files}" -- "${cur}") )
}
complete -F _memo memo
Run Code Online (Sandbox Code Playgroud)
MEMODIR=$HOME/memo
我想创建一些对strwith Rc, without cloning 的引用str:
fn main() {
let s = Rc::<str>::from("foo");
let t = Rc::clone(&s); // Creating a new pointer to the same address is easy
let u = Rc::clone(&s[1..2]); // But how can I create a new pointer to a part of `s`?
let w = Rc::<str>::from(&s[0..2]); // This seems to clone str
assert_ne!(&w as *const _, &s as *const _);
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
vim中如何将大括号括在括号内?初始字符串:
{a: b}
Run Code Online (Sandbox Code Playgroud)
最终字符串:
({a: b})
Run Code Online (Sandbox Code Playgroud)
该字符串可能跨越多行:
{
a: b
}
Run Code Online (Sandbox Code Playgroud) 例如,我想要一个foo接受两者A并B在外部包中定义的函数,如下所示:
fun foo<T: { p: Int }>(x: T) {}
data class A(val p: Int, val a: Int)
data class B(val p: Int, val b: Int)
Run Code Online (Sandbox Code Playgroud)
我尝试了Interface,但似乎需要更新A和B的定义,但我不能这样做,因为它们是在外部包中定义的。
我想要在 TypeScript 中做的是这样的:
fun foo<T: { p: Int }>(x: T) {}
data class A(val p: Int, val a: Int)
data class B(val p: Int, val b: Int)
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?