我对新的 Next.js App Router有疑问。他们删除了 head.js 文件,现在(正如文档中所写)我必须在 layout.ts 中使用元数据。我的图标名称是favicon.png。
我如何在这里指定它:
export const metadata = {
title: 'Create Next App',
description: 'Generated by create next app',
}
Run Code Online (Sandbox Code Playgroud) 我想使用 asyncio (/curio/trio) 和 pytest 同时运行多个测试,但我找不到任何相关信息。我需要自己安排吗?如果我这样做,有没有办法有一个很好的输出来分离(子)测试用例?
这是我正在尝试的一个小玩具示例:
import pytest
import time
import asyncio
pytestmark = pytest.mark.asyncio
expected_duration = 1
accepted_error = 0.1
async def test_sleep():
start = time.time()
time.sleep(expected_duration)
duration = time.time() - start
assert abs(duration-expected_duration) < accepted_error
async def test_async_sleep():
start = time.time()
await asyncio.sleep(expected_duration)
duration = time.time() - start
assert abs(duration-expected_duration) < accepted_error
Run Code Online (Sandbox Code Playgroud) 我不知道为什么pyenv(通过自制软件安装)似乎不起作用。
$PATHpyenv似乎没有正确更新我的变量,因此找不到通过 pyenv 安装的任何解释器。例如,对于 python 版本 3.6.8:
$ pyenv versions
system
* 3.4.10 (set by /Users/cglacet/.pyenv/version)
* 3.5.7 (set by /Users/cglacet/.pyenv/version)
* 3.6.8 (set by /Users/cglacet/.pyenv/version)
* 3.7.3 (set by /Users/cglacet/.pyenv/version)
* 3.8-dev (set by /Users/cglacet/.pyenv/version)
$ pyenv which python3.6
/Users/cglacet/.pyenv/versions/3.6.8/bin/python3.6
$ $(pyenv which python3.6) --version
Python 3.6.8
$ pyenv shell
pyenv: no shell-specific version configured
$ pyenv local
pyenv: no local version configured for this directory
Run Code Online (Sandbox Code Playgroud)
到这里为止,一切看起来都很好,但是:
$ python3.6 --version
-bash: python3.6: command not found
$ …Run Code Online (Sandbox Code Playgroud) 我想使用asyncio获取网页html。
我在jupyter笔记本中运行以下代码:
import aiofiles
import aiohttp
from aiohttp import ClientSession
async def get_info(url, session):
resp = await session.request(method="GET", url=url)
resp.raise_for_status()
html = await resp.text(encoding='GB18030')
with open('test_asyncio.html', 'w', encoding='utf-8-sig') as f:
f.write(html)
return html
async def main(urls):
async with ClientSession() as session:
tasks = [get_info(url, session) for url in urls]
return await asyncio.gather(*tasks)
if __name__ == "__main__":
url = ['http://huanyuntianxiazh.fang.com/house/1010123799/housedetail.htm', 'http://zhaoshangyonghefu010.fang.com/house/1010126863/housedetail.htm']
result = asyncio.run(main(url))
Run Code Online (Sandbox Code Playgroud)
但是,它返回 RuntimeError: asyncio.run() cannot be called from a running event loop
问题是什么?
怎么解决呢?
非常感谢你。
这个问题与同步版本中的同一问题相关。目标是设计将生成器或协程作为输入作为参数的装饰器。我的代码如下所示:
import asyncio
def say_hello(function):
async def decorated(*args, **kwargs):
return_value = function(*args, **kwargs)
if isinstance(return_value, types.AsyncGeneratorType):
print("Hello async generator!")
async for v in return_value:
yield v
else:
print("Hello coroutine!")
return await return_value
return decorated
@helpers.say_hello
async def generator():
for i in range(5):
await asyncio.sleep(0.2)
yield i
@helpers.say_hello
async def coroutine():
await asyncio.sleep(1)
return list(range(5))
async def test():
for v in generator():
print(v)
for v in coroutine():
print(v)
Run Code Online (Sandbox Code Playgroud)
给出的错误是:
'return' with value in async generator
Run Code Online (Sandbox Code Playgroud)
decorated我猜这只是静态检查包含 ayield和 …
python asynchronous python-3.x python-decorators python-asyncio
我想知道是否有充分的理由不在组件的构造函数中而不是在其componentDidMount钩子中发送请求?我看到了一些有趣的(但有点不完整)的答案:在构造函数中获取数据是个好主意吗?答案引用了文档中有趣的一点:
避免在构造函数中引入任何副作用或订阅。对于这些用例,请改用 componentDidMount()。
我的问题是关于这一点,我很想知道副作用有什么问题,例如在这里在构造函数中发送请求。
也许一个简单的例子将有助于消除关于我的问题和链接问题之间差异的任何歧义:
在构造函数中发送请求
class SomeComponent extends React.Component {
constructor(props) {
this.request = fetch(props.someURL);
}
async componentDidMount() {
const data = await this.request;
this.setState({'data': data});
}
}
Run Code Online (Sandbox Code Playgroud)
或者寄过来 componentDidMount
class SomeComponent extends React.Component {
async componentDidMount() {
const data = await fetch(props.someURL);;
this.setState({'data': data});
}
}
Run Code Online (Sandbox Code Playgroud)
或者如链接问题中所建议的那样,绝对不好:在构造函数中获取
class SomeComponent extends React.Component {
constructor(props) {
this.data = await fetch(props.someURL);
}
async componentDidMount() {
this.setState({'data': this.data});
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用NetworkX使用nx.bipartite.random_graph或生成二分图nx.bipartite.gnmk_random_graph,如下所示:
B = bipartite.gnmk_random_graph(5,6,10)
bottom_nodes, top_nodes = bipartite.sets(B)
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误:
networkx.exception.AmbiguousSolution: Disconnected graph: Ambiguous solution for bipartite sets.
Run Code Online (Sandbox Code Playgroud)
它只是一行,所以我不确定我是怎么做错的,以及为什么他们的包会返回(我假设是)一个无效的二分图.
谢谢.
编辑:我刚刚意识到我需要为第三个参数指定最小边数/概率.
例如bipartite.random_graph(5,6,0.6),p>0.5摆脱了错误.同样,bipartite.gnmk_random_graph(5,6,11)在哪里k>n+m.我没有意识到这种情况,因为我假设如果边缘的数量低于连接每个顶点所需的边数,那么就会有一些浮动顶点.
谢谢你的帮助!
有没有办法将pydantic模型转换为fastapi中的查询参数?
我的一些端点通过主体传递参数,但其他一些端点直接在查询中传递它们。所有这些端点共享相同的数据模型,例如:
class Model(BaseModel):
x: str
y: str
Run Code Online (Sandbox Code Playgroud)
我想避免在我的“查询参数端点”的定义中重复我对此模型的定义,例如test_query在此代码中:
class Model(BaseModel):
x: str
y: str
@app.post("/test-body")
def test_body(model: Model): pass
@app.post("/test-query-params")
def test_query(x: str, y: str): pass
Run Code Online (Sandbox Code Playgroud)
这样做最干净的方法是什么?
我试图在嵌套的列表列表中随机打乱一个元素。
例如我有:
list1=[[['a', 'b', 'c', 1], ['a', 'b', 'c', 2]], [['a', 'b', 'c', 3], ['a', 'b', 'c', 4]], [['a', 'b', 'c', 5], ['a', 'b', 'c', 6]]]
Run Code Online (Sandbox Code Playgroud)
我想随机打乱每个子列表(浮点数)的第三个元素,同时保持此嵌套列表中的其余列表条目及其结构完好无损。例如:
list1=[[['a', 'b', 'c', 1], ['a', 'b', 'c', 6]], [['a', 'b', 'c', 4], ['a', 'b', 'c', 2]], [['a', 'b', 'c', 3], ['a', 'b', 'c', 5]]]
Run Code Online (Sandbox Code Playgroud)
到目前为止,我想出了以下几点:
import random
list1 = [[['a', 'b', 'c', 1], ['a', 'b', 'c', 2]], [['a', 'b', 'c', 3], ['a', 'b', 'c', 4]], [['a', 'b', 'c', 5], ['a', 'b', 'c', 6]]] …Run Code Online (Sandbox Code Playgroud) 我很惊讶地发现 python 的bisect.bisect_left比 numpy 等效的numpy.searchsorted更快。这与我使用的值的分布有关还是对于任何输入都如此?
\n>>> input_size = 10**3\n>>> input_list = sorted([random.uniform(0, 300) for _ in range(input_size)])\n>>> numpy_input = np.array(input_list)\n\n>>> %timeit index = bisect.bisect_left(input_list, 100)\n434 ns \xc2\xb1 6.17 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\n>>> %timeit index = np.searchsorted(numpy_input, 100)\n1.8 \xc2\xb5s \xc2\xb1 21.8 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\nRun Code Online (Sandbox Code Playgroud)\nPython 的版本为 3.8.0,numpy 的版本为 1.18.4。
\n我在 ADB 上处于“授权”状态。我无法恢复“USB 调试授权视图”。另一方面,我的手机确实显示“USB 调试已连接。点击以关闭 USB 调试”。
\n\xe2\x9d\xaf adb devices\nList of devices attached\nxxxxxxxx authorizing\nRun Code Online (Sandbox Code Playgroud)\n这曾经正常工作(2-3年),我没有改变我的设置(相同的主机,相同的物理设备,相同的USB端口,相同的电缆)。我几天前更新了 OSX,所以这可能与此问题有关。
\n\xe2\x9d\xaf adb --version\nAndroid Debug Bridge version 1.0.41\nVersion 31.0.3-7562133\n\n\xe2\x9d\xaf sw_vers\nProductName: macOS\nProductVersion: 11.6.1\nBuildVersion: 20G224\nRun Code Online (Sandbox Code Playgroud)\n移动设备是ONEPLUS A6013,运行 Android 11 。
我已经尝试了几件事:
\n~/.android)这些都不允许我在手机上显示要求 USB 调试授权的提示。
\n编辑
\n我们还尝试将另一部手机连接到我的笔记本电脑(也不起作用)。我们尝试将我的手机连接到另一台笔记本电脑,它按预期工作(我还能够选中“始终允许”,然后我成功撤销了此授权)。所以这绝对是我笔记本电脑方面的问题。 …
我想知道如何通过一个简单的例子来获得堆栈溢出错误,例如:
int recursSum (int n)
{
return (n==1)? 1:n+recursSum(n-1);
}
Run Code Online (Sandbox Code Playgroud)
我问这个愚蠢的问题,因为我只有一些Segmentation错误,即使有一个空函数调用自己...
我是否遗漏了某些东西,或者是否有任何保护或阻止我这样做的事情?
python ×5
python-3.x ×4
asynchronous ×2
reactjs ×2
adb ×1
android ×1
bipartite ×1
c++ ×1
curio ×1
fastapi ×1
fetch ×1
homebrew ×1
javascript ×1
macos ×1
networkx ×1
next.js ×1
next.js13 ×1
numpy ×1
path ×1
pydantic ×1
pyenv ×1
pytest ×1
python-trio ×1
random ×1
react-native ×1
side-effects ×1
unix ×1