我在某处读到KeyboardInterrupt
异常只在Python的主线程中引发.我还读到在子线程执行时主线程被阻塞.那么,这是否意味着CTRL+ C永远不会到达子线程.我尝试了以下代码:
def main():
try:
thread = threading.Thread(target=f)
thread.start() # thread is totally blocking (e.g., while True)
thread.join()
except KeyboardInterrupt:
print "Ctrl+C pressed..."
sys.exit(1)
def f():
while True:
pass # do the actual work
Run Code Online (Sandbox Code Playgroud)
在这种情况下,CTRL+ C对执行没有影响.它就像是无法收听信号.我理解这是错误的方式吗?有没有其他方法可以使用CTRL+ 杀死线程C?
统一初始化是一个重要且有用的C++ 11特性.但是,您不能{}
随处使用,因为:
std::vector<int> a(10, 0); // 10 elements of value zero
std::vector<int> b({10, 0}); // 2 elements of value 10 and 0 respectively
std::vector<int> c{10, 0}; // 2 elements of value 10 and 0 respectively
std::vector<int> d = {10, 0}; // 2 elements of value 10 and 0 respectively
auto e(0); // deduced type is int
auto f = 0; // deduced type is int
auto g{0}; // deduced type is std::initializer_list<int>
auto h = {0}; // …
Run Code Online (Sandbox Code Playgroud) importlib_resources
Python < 3.7 的importlib.resources
标准库模块的backport在 setup.cfg 文件中有以下部分:
[options]
python_requires = >=2.7,!=3.0,!=3.1,!=3.2,!=3.3
setup_requires =
setuptools
wheel
install_requires =
pathlib2; python_version < '3'
typing; python_version < '3.5'
packages = find:
Run Code Online (Sandbox Code Playgroud)
为什么setup_requires
包括setuptools
?这似乎没有意义,因为:
setup.py 文件的第一行导入setuptools
,因此当setup
调用该函数并读取指示安装setuptools
它的 setup.cfg 文件时,安装已经为时已晚setuptools
:
from setuptools import setup
setup()
Run Code Online (Sandbox Code Playgroud)setuptools
已经安装在任何新的 Python 安装上(嗯,仅在 Windows 10 和 MacOS 10.15 和 Python 3.8.0 上测试过):
$ python -V
Python 3.8.0
$ pip list
Package Version
---------- -------
pip 19.2.3
setuptools …
Run Code Online (Sandbox Code Playgroud)[class.access/1] 中的 C++ 标准规定(强调我的):
一个类的成员可以是
- 私人的; 也就是说,它的名字只能被声明它的类的成员和朋友使用。
- 受保护;也就是说,它的名称只能由声明它的类的成员和朋友、从该类派生的类及其朋友使用(参见 [class.protected])。
- 民众; 也就是说,它的名字可以在任何地方使用而不受访问限制。
那么为什么编译器会在下面的 C++ 程序中引发这个错误呢?
#include <iostream>
class B {
protected:
static int const i = 1;
};
class D: public B {
public:
void f();
friend void g();
};
void D::f() {
B b;
std::cout << b.i; // OK
}
void g() {
B b;
std::cout << b.i; // error: 'i' is a protected member of 'B'
}
int main() {
D d;
d.f();
g();
return 0;
} …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Oracle 11g 快捷版。目前,当我使用SELECT * FROM nls_database_parameters;
它检查 NLS 字符集参数时,它给出了默认值:
NLS_CHARACTERSET: AL32UTF8
NLS_NCHAR_CHARACTERSET: AL16UTF16
Run Code Online (Sandbox Code Playgroud)
我想将这两个参数都设置为 UTF8。我怎么能这样做?我刚刚安装了Oracle 11g XE,所以除了Oracle本身需要的数据外,没有数据。
在macOS上的Java Swing应用程序中,当使用macOS 外观和屏幕菜单栏时,Cocoa会自动将名为Spotlight for Help的搜索字段绑定到框架菜单栏中标有"Help"的第一个菜单.
System.setProperty("apple.laf.useScreenMenuBar", "true");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
JMenuBar menuBar = new JMenuBar();
JMenu helpMenu = new JMenu("Help");
menuBar.add(helpMenu);
frame.setJMenuBar(menuBar);
Run Code Online (Sandbox Code Playgroud)
但是我的应用程序已本地化,英语字符串"帮助"在其他语言环境中转换为
在这些情况下,Cocoa 不会向帮助菜单提供Spotlight for Help字段.
我怎么还能得到这个搜索字段?
如何打破这条Python线以保持在PEP 8的79个字符的限制内?
config["network"]["connection"]["client_properties"]["service"] = config["network"]["connection"]["client_properties"]["service"].format(service=service)
Run Code Online (Sandbox Code Playgroud) 在PEP 366 -主模块显式相对导入中引入了模块范围变量__package__
以允许在子模块中显式相对导入,有以下摘录:
当主模块由其文件名指定时,该
__package__
属性将设置为None
. 为了在直接执行模块时允许相对导入,在第一个相对导入语句之前需要类似于以下的样板:Run Code Online (Sandbox Code Playgroud)if __name__ == "__main__" and __package__ is None: __package__ = "expected.package.name"
请注意,仅当顶级包已经可以通过
sys.path
. 需要额外的操作代码sys.path
才能直接执行工作,而无需导入顶级包。这种方法也有与使用兄弟模块的绝对导入相同的缺点——如果脚本被移动到不同的包或子包,样板将需要手动更新。它的优点是每个文件只需进行一次此更改,而不管相关导入的数量如何。
我尝试在以下设置中使用此样板:
目录布局:
foo
??? bar.py
??? baz.py
Run Code Online (Sandbox Code Playgroud)
bar.py 子模块的内容:
if __name__ == "__main__" and __package__ is None:
__package__ = "foo"
from . import baz
Run Code Online (Sandbox Code Playgroud)
当从文件系统执行子模块 bar.py 时,样板工作(PYTHONPATH
修改使包 foo/ 可访问sys.path
):
PYTHONPATH=$(pwd) python3 foo/bar.py
Run Code Online (Sandbox Code Playgroud)
当从模块命名空间执行子模块 bar.py 时,样板也可以工作:
python3 -m foo.bar
Run Code Online (Sandbox Code Playgroud)
但是,以下替代样板在这两种情况下都与 bar.py 子模块的内容一样有效:
if __package__:
from …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将这个Python程序翻译成Scheme:
def test(x):
if x > 1:
print('foo')
if x > 10:
return
if x == 4:
print('bar')
test(1)
test(2) # prints 'foo'
test(4) # prints 'foo\nbar'
test(11) # prints 'foo'
Run Code Online (Sandbox Code Playgroud)
return
方案中的声明是什么?
I\xe2\x80\x99m 尝试使用新的 ESLint 配置文件eslint.config.js (因为旧的 ESLint 配置文件.eslintrc.* 和 package.json 将在 ESLint 9.0.0 中弃用)。推荐的 ESLint 规则是由"extends": "eslint:recommended"
旧配置文件中的属性启用的,但该属性在依赖于语句的新配置文件中不再存在import
。
所以我import
在 eslint.config.js 中尝试了这个:
import config from \'eslint-config-standard\';\n\nexport default [\n {\n rules: config.rules\n }\n];\n
Run Code Online (Sandbox Code Playgroud)\n但我得到了这个TypeError
:
$ npm exec eslint .\nOops! Something went wrong! :(\n\nESLint: 8.39.0\n\nTypeError: Key "rules": Key "import/export": Could not find plugin "import".\n
Run Code Online (Sandbox Code Playgroud)\n如何解决这个问题呢?
\npython ×4
c++ ×2
boilerplate ×1
c++11 ×1
eslint ×1
friend ×1
java ×1
javascript ×1
kill ×1
line-breaks ×1
localization ×1
macos ×1
oracle ×1
oracle-apex ×1
oracle-xe ×1
oracle11g ×1
pep8 ×1
protected ×1
python-wheel ×1
return ×1
scheme ×1
setuptools ×1
spotlight ×1
sql ×1
swing ×1