如何在 TypeScript 中迭代枚举项?我尝试了 for-in,但这会迭代字符串。我需要为每个枚举值调用一个函数。
for (const foo in FooType) {
// here I have error that string is not assignable to parameter of type FooType
this.doCalculation(foo)
}
private doCalculation(value: FooType): void {
// some logic
}
Run Code Online (Sandbox Code Playgroud)
枚举FooType看起来像这样:
export enum SupportedFiat {
VALUE_A = 'VALUE_A',
VALUE_B = 'VALUE_B',
VALUE_C = 'VALUE_C'
}
Run Code Online (Sandbox Code Playgroud) 当我线程matplotlib并且我不经常关闭图形窗口(通过鼠标 - 关闭窗口按钮)时,我收到以下错误:
Exception RuntimeError: RuntimeError('main thread is not in main loop',) in <bound method PhotoImage.__del__ of <Tkinter.PhotoImage instance at 0x7ff408080998>> ignored Exception RuntimeError: RuntimeError('main thread is not in main loop',) in <bound method PhotoImage.__del__ of <Tkinter.PhotoImage instance at 0x7ff3fbe373f8>> ignored
Tcl_AsyncDelete: async handler deleted by the wrong thread
Aborted
Run Code Online (Sandbox Code Playgroud)
我做什么:我有一个主循环调用实例的方法.该matplotlib方法由该实例的init函数进行线程化.我不能matplotlib在实例中调用方法,我不知道为什么,所以我通过__init__以下方式调用它:
def __init__(self):
....
thread = threading.Thread(target=self.drawcharts, args=())
thread.daemon = False
thread.start()
Run Code Online (Sandbox Code Playgroud)
线程方法:
def drawcharts(self):
global endthread
..do some math..
try:
plt.plot(k)
plt.plot(x1)
plt.plot(x2) …Run Code Online (Sandbox Code Playgroud) 我一直在使用Cython将我的Python文件编译成C文件,然后使用MinGW从C文件创建可执行文件.Cython工作正常,我可以输入cython test.pyx命令行并获得一个C文件.问题是当我尝试从C文件编译可执行文件时.如果我输入,gcc test.c我会收到以下错误:
test.c:4:20: fatal error: Python.h: No such file or directory
#include "Python.h"
^
compilation terminated.
Run Code Online (Sandbox Code Playgroud)
我真的很感激一些帮助.我正在运行Windows 7和python 3.5.
我有一个简单的应用程序,可以调用 API。我正在使用反应查询来进行获取并缓存数据。然而,即使该函数被包装在 中React.memo,useQuery也会无缘无故地导致多次重新渲染。应该是2个效果图而不是8个。
import React, { memo } from "react";
import logo from "./logo.svg";
import "./App.css";
import { useQuery } from "react-query";
import Axios from "axios";
const fetchTodoList = async (key, id) => {
const { data } = await Axios.get(
`https://jsonplaceholder.typicode.com/posts/`
);
return data;
};
function App() {
const { isLoading, isError, data, error } = useQuery("todos", fetchTodoList);
console.log(isLoading, data);
if (isLoading) {
return <span>Loading...</span>;
}
if (isError) {
return <span>Error: {error.message}</span>;
}
// also …Run Code Online (Sandbox Code Playgroud) 我有一个使用useQueryReact Query的组件,如下所示:
const { data } = useQuery(
["products", selectedStore],
fetchProductsByStoreId
)
Run Code Online (Sandbox Code Playgroud)
selectedStore是一个可以从 UI 更改的本地状态变量,触发对新产品的 API 的请求。我的问题是,我还需要在另一个组件中使用来自请求的数据,最好是通过queryCache.getQueryData,但要做到这一点,我需要提供一个键,不仅是字符串,"Products"而且是整个数组["products", selectedStore]。
另一个组件无权访问selectedStore,所以我的问题是,另一个组件是否可以在不提升selectedStore到全局状态的情况下访问此查询数据?我想将数据保留在其中queryCache而不是提升它。
我有一个包含很多表单字段的应用程序。我想要一种可以避免重新渲染的方法。我正在使用 Formik 来管理表单。
我正在使用 FormikuseField来访问子组件中的 Formik 表单值,因为React.memo仅进行浅层比较。我想为 memo 方法编写一个自定义回调
function MainForm3() {
const initialValues = { txtArea: "" };
return (
<Formik initialValues={initialValues}>
{(formik) => {
return (
<>
<TextArea
name={"txtArea"}
rows={3}
id={"txtArea"}
label={"txtArea"}
/>
</>
);
}}
</Formik>
);
}
function TextArea(props: any) {
const [field] = useField(props);
return <TextField {...field} />;
}
React.memo(TextArea, (prev, next) => {
//how do i get access to the formik to perform a comparison
});
Run Code Online (Sandbox Code Playgroud)
由于上一个值和下一个值只能访问传递的直接属性,因此我无法比较备注函数中的 Formik 字段值。 …
在 monorepo 中,如果您在一个包中具有依赖项,您是否可以在另一个包中使用该依赖项而不将其添加到该包中?为了更清楚,请检查下面的布局。
Project
|-packages
|- packageA
|- package.json (Here let say I have lodash dependency)
|- index.js
|- packageB
|-package.json (I haven't added lodash here.)
|- index.js
Run Code Online (Sandbox Code Playgroud)
那么,我可以在 packageB 的 index.js 中导入 lodash 还是必须在那里添加它?
我记得有这是做什么或后填补空白相当于一个虚拟的声明if,elif,else,和for语句,以保持期望的压痕.
以下示例不起作用
if True:
#I want to simply pass this branch
# ... NOP command here
else:
print "False"
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
a =[1,2]
for entry in a:
entry = entry + 1
print a
Run Code Online (Sandbox Code Playgroud)
列表不应该变异[2,3]吗?结果出来了[1,2].为什么?
我最近发现了如何通过这个方法在python中动态创建变量:
vars()['my_variable'] = 'Some Value'
Run Code Online (Sandbox Code Playgroud)
从而创建变量my_variable.
我的问题是,这是一个好主意吗?或者我应该提前声明变量吗?
python ×5
reactjs ×3
javascript ×2
react-query ×2
c ×1
cython ×1
enums ×1
formik ×1
gcc ×1
matplotlib ×1
monorepo ×1
performance ×1
plot ×1
python-3.5 ×1
typescript ×1
variables ×1