我正在使用新的 Vue 插件来使用组合 API 和 TypeScript,但有些我不太理解。
我应该如何键入计算属性?
import Vue from 'vue'
import { ref, computed, Ref } from '@vue/composition-api'
export default Vue.extend({
setup(props, context) {
const movieName:Ref<string> = ref('Relatos Salvajes')
const country:Ref<string> = ref('Argentina')
const nameAndCountry = computed(() => `The movie name is ${movieName.value} from ${country.value}`);
return { movieName, country, nameAndCountry }
}
})
Run Code Online (Sandbox Code Playgroud)
在这个简单的例子中,我声明了两个引用和一个计算属性来连接两者。VSC 告诉我计算属性正在返回ReadOnly类型......但我无法让它工作。
我使用命令创建了 .env 文件并创建了诸如此类的变量token_id="13423edq234"。我不想使用像 dotenv 这样的外部包来读取文件。我只是想知道是否有任何方法可以加载我创建的 .env 文件,以便 Python 可以读取它。另外我不想将 env 变量添加到 zprofile 中。
vim .env
Run Code Online (Sandbox Code Playgroud)
在 Python 3 shell 中,
import os
os.environ['token_id']
Run Code Online (Sandbox Code Playgroud)
它说,它没有定义。
考虑一个执行类型提升的函数,例如两个数字的简单乘法,两个数字都可以是int或float:
def mul(a: int | float, b: int | float): # return type?
return a * b
Run Code Online (Sandbox Code Playgroud)
该函数返回,但和均为 的float情况除外。abint
如何正确、简洁地注释返回类型?我知道我可以这样做@overload:
from typing import overload
@overload
def mul(a: int, b: int) -> int: ...
@overload
def mul(a: float, b: int | float) -> float: ...
@overload
def mul(a: int | float, b: float) -> float: ...
def mul(a, b):
return a * b
Run Code Online (Sandbox Code Playgroud)
但这非常冗长,并且需要许多重载来处理我想象的某些“类型函数”应该处理的事情。在 C++ 中,这可以通过SFINAE来完成。在Python中,我可以用通用函数来做类似的事情吗?
def mul(a: …Run Code Online (Sandbox Code Playgroud) 有没有办法制作一个类型的完整副本,以便可以在模板推导上下文中区分它们?举个例子:
#include <iostream>
template <typename T>
struct test
{
static int c()
{
static int t = 0;
return t++;
}
};
typedef int handle;
int main()
{
std::cout << test<int>::c() << std::endl;
std::cout << test<handle>::c() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于typedef只为类型创建别名,因此打印0,1而不是所需的0,0.是否有解决方法?
我使用 Git LFS(大文件存储)将一个大文件上传到 gitub。
起初我可以从直接链接下载文件。
raw.githubusercontent.com/userName/reposiotry/master/file.mp4
Run Code Online (Sandbox Code Playgroud)
但是第二天文件开始包含一个文本值
oid sha256:59f24bc922e1a48bb3feeba18b23f0e9622a7ee07166d925650d7a933283f8b1 尺寸 123882252
我如何下载这个文件?如何获得它的直接链接?
我正在使用查询参数,并被介绍到URLSearchParams. 我用它来形成这种要查询的对象,
const x = {
a: 'hello World'
b: 23
c: ''
}
let params = new URLSearchParams(x);
console.log(params.toString()) // a=hello+World&b=23&c=
Run Code Online (Sandbox Code Playgroud)
在这里,我不想拥有那个c=,因为它很丑,而且我的 API 不需要那个。
所以,我想要这个结果a=hello+World&b=23(没有空的查询字符串)但是,我在 mdn 文档上找不到任何东西。
我该怎么做?
这样做不起作用,因为它似乎直接改变了params影响forEach:
const x = {
a: 'hello World',
b: '',
c: ''
};
let params = new URLSearchParams(x);
params.forEach((v, k) => { // never reaches `c`
console.log(k, ' => ', v)
if (v == '')
params.delete(k);
});
console.log(params.toString());Run Code Online (Sandbox Code Playgroud)
io例如,在编写实现类文件接口的类时,我们可以从模块继承抽象基类之一,如调整迭代器以使其行为类似于 Python 中的类文件对象TextIOBase中所示。
另一方面,在类型注释中,我们应该使用派生自typing.IO(例如TextIO)的类来表示此类对象,如文件或类文件对象的类型提示中所示?或Union 中 io.TextIOBase 的类型检查问题。
然而,这似乎并没有像我预期的那样工作:
import io
import sys
import typing
class MyIO(io.TextIOBase):
def write(self, text: str):
pass
def hello(f: typing.TextIO):
f.write('hello')
hello(sys.stdout) # type checks
hello(open('temp.txt', 'w')) # type checks
hello(MyIO()) # does not type check
Run Code Online (Sandbox Code Playgroud)
当在此代码上运行 mypy 时(使用 Python 3.7.3 和 mypy 0.910),我们得到
错误:“hello”的参数 1 具有不兼容的类型“MyIO”;预期“TextIO”
如何MyIO编写该类,使其被接受为类型的函数参数typing.TextIO(而不仅仅是使用typing.cast(typing.TextIO, ...))?
typing.TextIO不能用作基类:
使用时class MyIO(typing.TextIO):
错误:无法使用抽象属性“__enter__”、“__exit__”、...和“writelines”实例化抽象类“MyIO”(抑制了 …
考虑以下:
from __future__ import annotations
class A:
def __init__(self):
print("A")
self.hello = "hello"
# how do I type this so that the return type is A for A.bobo()
# and B for B.bobo()?
@classmethod
def bobo(cls) -> UnknownType:
return cls()
class B(A):
def __init__(self):
print("B")
super().__init__()
self.world = "world"
instance_of_B = B.bobo() # prints "B", then "A", and returns an instance of B
Run Code Online (Sandbox Code Playgroud)
我想对类方法进行类型提示,以便 mypy 可以知道,在s方法bobo的情况下,它不仅仅是返回的实例,而且实际上是 的实例。我真的不清楚如何做到这一点,或者是否可能。我认为类似的东西可能会起作用,但我不确定这对 mypy 是否具有语法意义。BboboABType[cls]
我想从我的远程存储库中删除一些提交.在我的回购中我几乎没有这样的提交:
commits messages
abcd123 some message
xyze456 another commit message
98y65r4 this commit is not required
987xcrt this commit is not required
bl8976t initial commit
Run Code Online (Sandbox Code Playgroud)
我想删除提交 98y65r4和987xcrt我的回购.怎么实现呢?
这是Python中的内存泄漏吗?
import contextvars
contextvar = contextvars.ContextVar('example')
while True:
string = 'hello world'
token = contextvar.set(string)
Run Code Online (Sandbox Code Playgroud)
contextvar 是一个随着你向它推送的次数越多而不断增长的堆栈吗?
如果我从不打电话怎么办contextvar.reset(token)?
或者一切都通过引用计数来处理?
python ×4
mypy ×3
git ×2
c++ ×1
git-lfs ×1
github ×1
javascript ×1
macos ×1
node.js ×1
templates ×1
type-hinting ×1
typescript ×1
typing ×1
url ×1
vue.js ×1