我刚刚了解了python中的yield关键字 - 非常令人印象深刻且非常有用.
在C和C++语言中是否有任何等价物?
在 python 3 中,我试图读取驻留在 tar.gz 存档中的文件而不提取它们(意思是不将提取文件写入磁盘)。我找到了 tarfile 模块,这就是我写的(大大简化了):
tar = tarfile.open('arhivename.tar.gz',encoding='utf-8')
for x in tar.getmembers():
filelikeobject=tar.extractfile(x)
#pass the filelikeobject to a third party function that accepts file-like object that read strings
#the following lines are for debug:
r=filelikeobject.read()
print(type(r).__name__) #prints out 'bytes' - need 'str'
Run Code Online (Sandbox Code Playgroud)
问题是,tar.extractfile(x) 返回一个文件对象,该对象在调用 read() 时返回字节。我需要它使用 utf-8 编码返回 str
我刚刚开始使用 vscode。我注意到,当我将鼠标悬停在文本和波浪线上时,我会得到一些扩展信息。这有时很有用,但大多数情况下它会分散注意力。我知道如何使用设置 ( ) 一起禁用悬停Editor > Hover: Enabled,但我想要更简单的方法,例如仅在按下按键时启用Alt悬停
VS Code 可以实现吗?也许有扩展名?
这是我的代码
function Label({title}){
return <h1>{title}</h1>
}
function useLabel({title}){
return <h1> {title}</h1>
}
function Diff(){
return <div>
<Label title='this is component'/>
{useLabel({title:'this is custom hook'})}
</div>
}
Run Code Online (Sandbox Code Playgroud)
在代码中,我定义了组件 Label 和自定义钩子 useLabel。它们执行完全相同的工作,只是使用不同的语法进行调用。
我的问题是:
这是我的 React hooks 代码:
function calc_c({a,b}){
//some long calculation that is based on a,b
}
function MyComponent(params){
var a=calc_a(params)
var a=calc_b(params)
var c=React.useMemo(()=>calc_c({a,b},[a,b])
}
Run Code Online (Sandbox Code Playgroud)
我的问题:如何找出哪些参数发生了[a,b]变化并导致了调用calc_c
编辑:我最终使用了 skyboyer 优秀答案的通用版本:
export function useChanged(name,value){
function print_it(){
console.log('changed',name)
}
React.useMemo(print_it,[value])
}
Run Code Online (Sandbox Code Playgroud) 在Windows下,无论如何以编程方式计算同一进程的上下文切换?最好的事情是每当切换一个线程时调用的回调.
有没有简单通用的方法来定位基于命名空间的模式?例如,在下面的 XML 中,我如何找到http://schemas.microsoft.com/developer/msbuild/2003的架构?
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
Run Code Online (Sandbox Code Playgroud) 在浏览器 JavaScript 中,我尝试将一些 CSV 数据写入剪贴板,以便用户可以粘贴到 Excel。这是我的代码:
function onClick(){
var txt=get_some_valid_csv_text()
var items=[
new ClipboardItem({
'text/csv': new Blob([txt], { type: 'text/csv' })
})
]
navigator.clipboard.write(items)
}
Run Code Online (Sandbox Code Playgroud)
问题:它不起作用,我在控制台中收到此错误消息:
未捕获(承诺中)DOMException:写入时不支持净化的 MIME 类型 text/csv。
这是我的反应钩子代码:
function Simple(){
var [st,set_st]=React.useState(0)
var el=React.useRef(null)
if (st<1)
set_st(st+1)//to force an extra render for a grand total of 2
console.log('el.current',el.current,'st',st)
return <div ref={el}>simple</div>
}
ReactDOM.render(<Simple />,document.querySelector('#root') );
Run Code Online (Sandbox Code Playgroud)
我认为它应该渲染两次。第一次 el.current 应该为 null,第二次应该指向 div 的 DOM 对象。运行它时,这是输出
el.current null st 0
el.current null st 1
Run Code Online (Sandbox Code Playgroud)
是的,它确实渲染了两次。但是,第二次渲染 el.current 仍然为空。为什么?
解决方案:如下 Gireesh Kudipudi 所描述。我添加了useEffect
function Simple(){
var [st,set_st]=React.useState(0)
var el=React.useRef(null)
if (st<1)
set_st(st+1)//to force an extra render for a grand total of 2
console.log('el.current',el.current,'st',st)
React.useEffect(_=>console.log('el.current',el.current,'st',st)) //this prints out the el.current correctly
return <div …Run Code Online (Sandbox Code Playgroud) 在PostgreSQL中,我具有以下表定义
create table file(
file_id int generated by default as identity primary key,
file_name text UNIQUE not null
);
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何删除对的唯一约束file_name?
我有这个非常简单的c程序:
#include <stdio.h>
int main (int argc, char ** argv){
printf ("%s\n",argv[1]);
}
Run Code Online (Sandbox Code Playgroud)
在Linux/bash上运行时如下:
./a.out *
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
a.c
Run Code Online (Sandbox Code Playgroud)
为什么?
使用 psycopg2,连接和查询数据库的工作方式如下
conn = psycopg2.connect('connection string')
with conn:
cur=conn.cursor()
cur.execute("SELECT * FROM pg_stat_activity") #simple query
rows = cur.fetchall()
for row in rows:
print (row)
Run Code Online (Sandbox Code Playgroud)
经过反复试验,我发现这with conn是绝对必要的,否则你会得到很多无法解释的锁。
我的问题是:有没有办法设置连接以避免使用它?
python ×3
react-hooks ×3
reactjs ×3
c ×2
postgresql ×2
c++ ×1
dom ×1
glob ×1
javascript ×1
linux ×1
navigator ×1
psycopg2 ×1
python-3.x ×1
shell ×1
use-ref ×1
utf-8 ×1
windows ×1
xml ×1
xsd ×1
yield ×1