我正在尝试做相当于:
class A:
def __init__(self):
self.b = self.get_b()
def get_b(self):
return 1
Run Code Online (Sandbox Code Playgroud)
使用@dataclass。我想在这里使用 a ,@dataclass因为还有其他(省略的)字段是从提供的构造函数参数初始化的。b是从实例方法的结果初始化的字段之一。执行这个:
@dataclass
class A:
b = self.get_b()
def get_b(self):
return 1
Run Code Online (Sandbox Code Playgroud)
表明self未在 的b范围内定义。这里可以参考一下吗self?
我有一个Dockerfile基于nvidia/cuda这样的:
FROM nvidia/cuda:11.0-base
...
Run Code Online (Sandbox Code Playgroud)
我希望能够Dockerfile在我们没有 Nvidia GPU 的 CI 服务器上构建它。当我尝试这样做时,我收到此错误:
------
> [1/6] FROM docker.io/nvidia/cuda:11.0-base:
------
failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = failed to build LLB: failed to load cache key: docker.io/nvidia/cuda:11.0-base not found
Run Code Online (Sandbox Code Playgroud)
该错误表示未找到图像,但我认为这有点误导。我已经能够将问题与是否存在 GPU 隔离开来。
在Dockerfile带有 Nvidia GPU 的服务器上构建它时,我没有收到此错误。是否可以在没有 GPU 的服务器Dockerfile上构建基于nvidia/cuda图像的图像?这将节省我们 CI 服务器的成本。
我计划将生成的 docker 容器部署在具有 GPU 的服务器上,换句话说,是否可以将 GPU 的存在推迟到运行时间而不是构建时间?
我读过Can Anybodyterpret docker.sock来了解/var/run/docker.sock它的作用,但它在 GitLab CI 的Use Docker socket binding中的使用让我感到困惑。
这是他们的注册命令示例gitlab-runner:
sudo gitlab-runner register -n \
--url https://gitlab.com/ \
--registration-token REGISTRATION_TOKEN \
--executor docker \
--description "My Docker Runner" \
--docker-image "docker:19.03.12" \
--docker-volumes /var/run/docker.sock:/var/run/docker.sock
Run Code Online (Sandbox Code Playgroud)
我看到可以从两个地方获取生成的容器docker。
/var/run/docker.sock。docker包含在基础映像中的二进制文件docker:19.03.12。这不是PATH冲突吗?我认为应该是其中之一,我可以docker从主机的 unix 套接字或基本映像中获得使用的能力。
我认为--docker-image应该是ubuntu:latest或者不带有 的东西docker,因为已经来自主机套接字PATH。docker或者,可以移除 docker 套接字安装座。
关于 的双重包含,实际上发生了什么docker?
unix-socket docker gitlab-ci gitlab-ci-runner docker-in-docker
我有以下程序:
fn main() {
let x = 0;
println!("Example 1: {:p}", &x);
println!("Example 1: {:p}", &x);
println!("Example 2: {:p}", &&x);
println!("Example 2: {:p}", &&x);
}
Run Code Online (Sandbox Code Playgroud)
这是一个示例输出:
Example 1: 0x7ffcb4e72144
Example 1: 0x7ffcb4e72144
Example 2: 0x7ffcb4e72238
Example 2: 0x7ffcb4e72290
Run Code Online (Sandbox Code Playgroud)
for 的输出"Example 1"始终相同,而 for的输出"Example 2"始终不同。
我已经阅读了是否 println! 借用或拥有变量?,而我从给定的答案中了解到的是,println!默默地参考。换句话说,这听起来像是println!增加了一个额外的间接级别。
我原以为输出"Example 1"也会有所不同。看到它println!悄悄地采取了另一个间接级别,"Example 1"实际上正在与 一起工作&&x,并且"Example 2"正在与 一起工作&&&x。这似乎与我链接的答案一致,特别是:"If you write println!("{}", &x), you …
鉴于这种:
from typing import Generic, TypeVar
T = TypeVar('T')
class Parent(Generic[T]):
pass
Run Code Online (Sandbox Code Playgroud)
我可以int从Parent[int]使用中得到typing.get_args(Parent[int])[0]。
问题变得有点复杂,如下所示:
class Child1(Parent[int]):
pass
class Child2(Child1):
pass
Run Code Online (Sandbox Code Playgroud)
为了支持任意长的继承层次结构,我提出了以下解决方案:
import typing
from dataclasses import dataclass
@dataclass(frozen=True)
class Found:
value: Any
def get_parent_type_parameter(child: type) -> Optional[Found]:
for base in child.mro():
# If no base classes of `base` are generic, then `__orig_bases__` is nonexistent causing an `AttributeError`.
# Instead, we want to skip iteration.
for generic_base in getattr(base, "__orig_bases__", ()):
if typing.get_origin(generic_base) is Parent: …Run Code Online (Sandbox Code Playgroud) 通过以下方式启动 firestore 模拟器有什么区别:
firebase emulators:start --only firestore
Run Code Online (Sandbox Code Playgroud)
和:
gcloud beta emulators firestore start
Run Code Online (Sandbox Code Playgroud)
这两个选项都允许我的 python 应用程序实现与数据库的连接,如下所示:
import google
from google.cloud import firestore
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8081"
os.environ["FIRESTORE_EMULATOR_HOST_PATH"] = "localhost:8081/firestore"
os.environ["FIRESTORE_HOST"] = "http://localhost:8081"
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
client = firestore.Client(credentials=credentials)
Run Code Online (Sandbox Code Playgroud)
我自己注意到的一个区别是,它firebase似乎尊重我的firebase.json,特别是指定的主机端口,如下所示:
{
"emulators": {
"firestore": {
"port": "8081"
}
}
}
Run Code Online (Sandbox Code Playgroud)
另一方面,gcloud忽略firebase.json并选择随机端口,除非我明确地通过 传递端口--host-port。这部分是两者之间较大的区别吗?还有哪些其他区别?
python firebase google-cloud-platform gcloud google-cloud-firestore
foo.py:
kwargs = {"a": 1, "b": "c"}
def consume(*, a: int, b: str) -> None:
pass
consume(**kwargs)
Run Code Online (Sandbox Code Playgroud)
mypy foo.py:
error: Argument 1 to "consume" has incompatible type "**Dict[str, object]"; expected "int"
error: Argument 1 to "consume" has incompatible type "**Dict[str, object]"; expected "str"
Run Code Online (Sandbox Code Playgroud)
这是因为object是intand的超类型,str因此被推断出来。如果我声明:
from typing import TypedDict
class KWArgs(TypedDict):
a: int
b: str
Run Code Online (Sandbox Code Playgroud)
然后注释kwargs为KWArgs,mypy检查通过。这实现了类型安全,但需要我复制consumein的关键字参数名称和类型KWArgs。有没有办法TypedDict在类型检查时从函数签名中生成它,以便我可以最大限度地减少维护中的重复?
我正在尝试完成以下任务(请参阅mypy Playground):
from typing import TypedDict, Final
account_schema: Final = {"name": str, "email": str}
Account = TypedDict("Account", account_schema)
AccountPatch = TypedDict("AccountPatch", account_schema, total=False)
Run Code Online (Sandbox Code Playgroud)
我的想法是,我可以在一个地方指定我的模式,一个版本需要所有字段(Account插入数据库时),另一个版本使所有字段可选(AccountPatch更新数据库时)。
来自PEP 586:
限定符
Final用作声明变量有效的简写Literal。
但mypy错误如下:
error: TypedDict() expects a dictionary literal as the second argument
Run Code Online (Sandbox Code Playgroud)
为什么不允许TypedDict字典Final作为其第二个参数?
对于我的核心问题,我是否可以对两个TypedDicts 使用相同的架构(一个具有整体性,一个不具有整体性),而不必复制架构?
我一直在尝试学习具有 Python 背景的 JS 并发模型。
在 Python 中运行以下命令:
async def myFunction():
print("abc")
myFunction()
Run Code Online (Sandbox Code Playgroud)
不会打印任何内容。在 JavaScript 中运行以下命令:
async function myFunction() {
console.log("abc")
}
myFunction()
Run Code Online (Sandbox Code Playgroud)
将打印"abc". 在这两种情况下我都没有await myFunction,但评价却不同。
我明白为什么Python程序什么也不打印。myFunction是一个不解析任何内容的协程函数,更具体地说,它返回一个Awaitable[None]. 但要真正执行这个可等待的副作用,我必须等待它。
我还读过承诺是否被延迟评估?答案是否定的,谈论如何保证对 Promise 的急切评估。
尽管我已经分别研究了这两种并发模型,但它们的差异仍然令人困惑。虽然这里的对比的总体清晰性会非常有帮助,但我也确实有一个具体的问题:在 JavaScript 中是否有一个点需要等待 aPromise解析为空,并且应该只执行它的副作用?换句话说,即使它们在这里给出了相同的输出,但在不同的情况下,它们的行为是否可能存在差异await myFunction()?myFunction()
python ×6
mypy ×3
type-hinting ×3
docker ×2
async-await ×1
concurrency ×1
dictionary ×1
firebase ×1
gcloud ×1
gitlab-ci ×1
gpu ×1
jakarta-ee ×1
java ×1
javascript ×1
jta ×1
pointers ×1
promise ×1
rust ×1
self ×1
transactions ×1
unix-socket ×1