我最近安装了 Pyenv 和 Poetry,想创建一个新的 Python 3.8 项目。我已经将python的global和local版本设置为3.8.1使用适当的 Pyenv 命令(pyenv global 3.8.1例如)。当我pyenv version在终端中运行时,输出3.8.1.符合预期。
现在,问题是当我用 Poetry( poetry new my-project)创建一个新的 python 项目时,生成的pyproject.toml文件用 python 2.7 创建了一个项目:
[tool.poetry]
name = "my-project"
version = "0.1.0"
description = ""
authors = ["user <user@email.com>"]
[tool.poetry.dependencies]
python = "^2.7"
[tool.poetry.dev-dependencies]
pytest = "^4.6"
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
Run Code Online (Sandbox Code Playgroud)
Poetry 似乎默认回 Python 的系统版本。如何更改它以使用与 Pyenv 一起安装的版本?
编辑
我使用的是 MacOS,它与 Python 2.7 捆绑在一起。我认为这可能会导致这里的一些问题。我已经使用 Pyenv 再次重新安装了 …
Julia中是否有一个函数返回所需类型的数组副本,即numpys astype函数的等价物?我有一个"任何"类型的数组,并希望将其转换为Float数组.我试过了:
new_array = Float64(array)
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误
LoadError: MethodError: `convert` has no method matching
convert(::Type{Float64}, ::Array{Any,2})
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
Closest candidates are:
call{T}(::Type{T}, ::Any)
convert(::Type{Float64}, !Matched::Int8)
convert(::Type{Float64}, !Matched::Int16)
...
while loading In[140], in expression starting on line 1
in call at essentials.jl:56
Run Code Online (Sandbox Code Playgroud)
我可以编写一个遍历数组的函数并返回每个元素的浮点值,但是如果没有内置方法来执行此操作,我会发现它有点奇怪.
使用 Flask 的应用程序工厂模式时,我无法将表创建到数据库 (PostgresSQL) 中。
我查看了Stackoverflow和 Flask-SQLAlchemy 源代码中的不同示例。我的理解是,使用工厂应用程序模式,我需要先设置所谓的上下文,然后才能尝试创建表。但是,当我创建上下文时,Flask 应用程序的配置字典会重置,并且不会向前传播配置。
这是我的model.py
import datetime
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class MyModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
some = db.Column(db.DateTime, nullable=False)
random = db.Column(db.String, nullable=False)
model = db.Column(db.String, nullable=False)
fields = db.Column(db.Integer, nullable=False)
def __init__(self, some: datetime.datetime, random: str, model: str,
fields: int) -> None:
self.some = some
self.random = random
self.model = model
self.fields = fields
def __repr__(self):
return f"""<MyModel(some={self.some}, random={self.random},
model={self.model}, fields={self.fields})>"""
Run Code Online (Sandbox Code Playgroud)
这是应用程序的__init__.py文件 …
我试图弄清楚是否有一种方法可以抽象我为我的 React 应用程序使用 Typescripts 泛型创建的数据获取挂钩。不幸的是,我对 Typescript 的理解并不像我希望的那么好,所以我陷入了困境。
我使用的钩子基于我正在使用的数据获取库 SWR 给出的示例。他们在他们的存储库中有一个我想要构建的示例,但它使用的是 Axios,而我使用的是 fetch(或者确切地说是 isomorphic-unfetch)。
明确地说,我有useUsers一个钩子可以从我的用户端点获取所有用户,如下所示:
import useSWR from 'swr';
import fetcher from '../fetcher';
import { User } from '@prisma/client';
type UserHookData = {
appUser?: User;
isLoading: boolean;
isError: any;
};
type UserPayload = {
user?: User;
};
function useUsers(): UserHookData {
const { data, error } = useSWR<UserPayload>('/api/users/', fetcher);
const userData = data?.user;
return {
appUser: userData,
isLoading: !error && !data,
isError: error,
};
}
export …Run Code Online (Sandbox Code Playgroud) 在useReducerReact 中使用钩子时,是否可以使用调度函数发送多个动作?我尝试将一系列操作传递给它,但这会引发未处理的运行时异常。
明确地说,通常会有一个初始状态对象和一个减速器,如下所示:
const initialState = { message1: null, message2: null }
const messageReducer = (state, action) => {
switch(action.type) {
case SET_MESSAGE1:
return {...state, message1: action.payload.message1}
case SET_MESSAGE2:
return {...state, message2: action.payload.message2}
default:
throw new Error("Something went wrong!")
}
}
Run Code Online (Sandbox Code Playgroud)
然后可以像这样使用 useReducers 调度函数处理反应应用程序中的状态。
[state, dispatch] = useReducer(messageReducer, initialState)
...
dispatch({type: SET_MESSAGE1: payload: {message1: "setting message1"})
dispatch({type: SET_MESSAGE2: payload: {message2: "setting message2"})
Run Code Online (Sandbox Code Playgroud)
我想要做的是将这两个突变发送到一个数组中,这样我只需要调用一次 dispatch,就像这样:
dispatch([
{type: SET_MESSAGE1: payload: {message1: "setting message1"},
{type: SET_MESSAGE2: payload: {message2: "setting message2"}
]) …Run Code Online (Sandbox Code Playgroud) 我正在使用挂钩来获取应用程序中的数据。钩子看起来像这样:
const initialState = {
response: null,
loading: true,
error: null
}
const useGetFetch(url, token, user, parser) => {
const [state, dispatch] = useReducer(fetchReducer, initialState)
useEffect (() => {
if(user){
fetch(...)
.then((data)=> data.json())
.then(dispatch(...)) // setting the state here
.catch((error) => dispatch(...)) // set error state
}
}, [user])
return [state.response, state.loading, state.error]
}
Run Code Online (Sandbox Code Playgroud)
具体来说,我在表单组件内使用此挂钩来获取一些数据以用作选择元素内的选项。
但是,我需要能够让用户动态添加新选项。为了促进这一点,我在选择元素旁边创建了一个按钮。如果用户按下此按钮,将弹出一个模式,其中包含另一个表单,可以创建新选项。这个新选项通过发布请求发送到服务器,如果成功,模式将关闭。
我面临的问题是,模式关闭后,我无法更新使用自定义数据获取挂钩获取的初始数据,除非刷新页面。有没有办法让这种情况发生而不触发页面刷新?
我最初的预感是使用回调函数并将其传递给useGetFetch并将其绑定到useEffect钩子。然后,我可以将其传递给模式中的表单,并在成功提交后调用该函数,但这不起作用。
我正在寻找的另一个选择是钩子useCallback,但我目前对它的理解还不够好,不知道它是否有帮助。
编辑
具体来说,我所做的是在父组件外部创建一个触发器函数:
const trigger = () => console.log("click")
Run Code Online (Sandbox Code Playgroud)
将其绑定到 fetch hook:
const useGetFetch(url, token, user, …Run Code Online (Sandbox Code Playgroud) 我最近运行了一个 git 子模块init并update在我的暂存分支中提交。这导致 git 从子模块下载了一堆更改,我不想将这些更改放入暂存阶段并希望完全丢弃。
Changes not staged for commit:\n (use "git add <file>..." to update what will be committed)\n (use "git restore <file>..." to discard changes in working directory)\n (commit or discard the untracked or modified content in submodules)\n modified: path-to/submodule1 (modified content)\n modified: path-to/submodule2 (new commits,modified content)\n \xe2\x80\xa6\n\n\nno changes added to commit (use "git add" and/or "git commit -a")\nRun Code Online (Sandbox Code Playgroud)\n我想放弃所有这些更改,并且不向当前分支添加任何内容。我该怎么做呢?
\nchildren当使用命名函数而不是箭头函数时,在以打字稿编写的 React 组件中使用关键字传递子节点的正确方法是什么?
在箭头函数中我可以写这样的东西:
const MyComponent: React.FC = ({children}) => {
return <div>{children}</div>
}
export default MyComponent;
Run Code Online (Sandbox Code Playgroud)
但是我如何用函数做同样的事情normal呢?我试过
import React from "react";
function MyComponent({children}): JSX.Element{
return <div>{children}</div>
}
export default MyComponent;
Run Code Online (Sandbox Code Playgroud)
但是 eslint 会抛出以下错误:
var children: any
Object pattern argument should be typed. eslint(@typescript-eslint/explicit-module-boundary-types)
'children' is missing in props validation eslint(react/prop-types)
Run Code Online (Sandbox Code Playgroud)
我可以通过从 React 导出来消除错误ReactNode。
import React, { ReactNode } from "react"
function MyComponent({children}: ReactNode): JSX.Element{
return <div>{children}</div>
}
export default MyComponent;
Run Code Online (Sandbox Code Playgroud)
但这是否被认为是解决此问题的可行方法,或者还有其他被认为是最佳实践的方法吗?
javascript ×4
reactjs ×3
fetch ×2
python ×2
react-hooks ×2
typescript ×2
arrays ×1
flask ×1
git ×1
julia ×1
pyenv ×1
python-3.x ×1
sqlalchemy ×1
swr ×1
use-effect ×1
use-reducer ×1
virtualenv ×1