我理解this
在lambda中捕获(修改对象属性)的正确方法如下:
auto f = [this] () { /* ... */ };
Run Code Online (Sandbox Code Playgroud)
但我很好奇我所看到的以下特点:
class C {
public:
void foo() {
// auto f = [] () { // this not captured
auto f = [&] () { // why does this work?
// auto f = [&this] () { // Expected ',' before 'this'
// auto f = [this] () { // works as expected
x = 5;
};
f();
}
private:
int x;
};
Run Code Online (Sandbox Code Playgroud)
我很困惑的奇怪(并希望得到回答)是以下工作的原因:
auto f = …
Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法确定(给定一个包含lambda的变量)它包含的lambda参数的数量.原因是,我希望根据参数的数量有条件地调用函数.
我正在寻找什么
def magic_lambda_parameter_counting_function(lambda_function):
"""Returns the number of parameters in lambda_function
Args:
lambda_function - A lambda of unknown number of parameters
"""
Run Code Online (Sandbox Code Playgroud)
所以我可以做点什么
def my_method(lambda_function):
# ...
# (say I have variables i and element)
parameter_count = magic_lambda_parameter_counting_function(lambda_function)
if parameter_count == 1:
lambda_function(i)
elif parameter_count == 2:
lambda_function(i, element)
Run Code Online (Sandbox Code Playgroud) 我正在研究这个Python 项目模板。它们用于poetry
定义开发依赖项
[tool.poetry.dev-dependencies]
black = {version = "*", allow-prereleases = true}
flake8 = "*"
isort = "^5.6"
mypy = ">0.900,<1"
...
Run Code Online (Sandbox Code Playgroud)
它们还用于pre-commit
检查 git 和 CI 工作流程的内务工作(例如,格式化、linting、分类等):
minimum_pre_commit_version: 2.8.0
default_stages: [commit, push, manual]
repos:
- repo: https://github.com/psf/black
rev: 21.11b1
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
args: [--max-line-length=88]
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
args: [--filter-files]
- ...
Run Code Online (Sandbox Code Playgroud)
就我而言,我肯定想要一个由 Poetry 为我的 IDE 管理的本地版本的开发包,并且我还想“按原样”利用预提交框架,而不需要切换到 …
我正在尝试在工作中进行预提交工作(我让它在个人计算机上工作)。我们的安全设置不允许预先提交引用外部存储库并从中安装外部包。
看来我的选择是:
在决定采取哪条路径之前,我想更多地了解本地存储库的工作原理,但在预提交网站(或其他地方)上找不到大量有关具体细节的文档。
我有 .pre-commit-config.yml 设置,如下例所示。
repos:
- repo: local
hooks:
- id: isort
name: Run isort
entry: isort
language: system
- id: black
name: Run black
entry: black
language: system
- id: flake8
name: Run flake8
entry: flake8
language: system
- id: pydocstyle
name: Run pydocstyle
entry: pydocstyle
language: system
Run Code Online (Sandbox Code Playgroud)
如果我使用上面的 .pre-commit-config.yml,则使用什么系统版本的软件包?它是活动 conda 环境中的版本(我正在使用 conda)吗?我认为情况会是这样,但预提交挂钩似乎正在运行,即使我没有isort
、black
、 和flake8
或pydocstyle
我认为情况确实如此,但即使我在激活的 conda 环境中
这对我来说似乎很奇怪,但我无法在网上找到任何内容来确认这些软件包的系统版本将在本地存储库设置中使用。
language: python
另外,如果我使用而不是会发生什么language: system
? …
>>> import string
>>> import locale
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> locale.getpreferredencoding()
'UTF-8'
>>> string.letters
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
Run Code Online (Sandbox Code Playgroud)
有没有解决方法呢?
平台:Linux Python2.6.7和Python2.7.3似乎受到影响,在Python3中运行良好(带ascii_letters
)
有些钩子可能需要一段时间才能运行,我想在推送之前运行它们,但不是在每次特定提交之前运行(例如,pylint 可能有点慢)。
我见过以下内容:
但目前还不清楚我应该如何设置它。
这是我尝试过的:
default_stages: [commit]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
- id: black
stages: [push]
Run Code Online (Sandbox Code Playgroud)
由此,我期望前几个钩子在提交之前运行(它们确实如此),但我期望黑色在推送之前运行,但事实并非如此。
为了测试我是否创建了以下文件:
"""This is a docstring."""
print('this should be formatted')
Run Code Online (Sandbox Code Playgroud)
这肯定不是被黑色格式化的。
git clone的默认完成(在下面重现)为选项提供了标签完成--*
:
_git_clone ()
{
case "$cur" in
--*)
__gitcomp_builtin clone
return
;;
esac
}
Run Code Online (Sandbox Code Playgroud)
(具体的例子,macos high sierra + brew安装了bash-completion/git)
在bash-completion 1.x世界中,要覆盖它,我会(在.bashrc
/中.bash_profile
)定义我自己的_git_clone
完成函数:
# https://github.com/scop/bash-completion/blob/d2f14a7/bash_completion#L498
__ltrim_colon_completions() {
if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then
# Remove colon-word prefix from COMPREPLY items
local colon_word=${1%"${1##*:}"}
local i=${#COMPREPLY[*]}
while [[ $((--i)) -ge 0 ]]; do
COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"}
done
fi
}
_git_clone() {
case "$cur" in
--*) …
Run Code Online (Sandbox Code Playgroud) 我很好奇,查找时间调用std::get<>
上std::tuple<>
发生.一些简短的谷歌搜索(包括通常有这些信息的参考页面)没有结果.
我最初的直觉(和恐惧)是元组的递归结构(如果它被实现为可变参数模板)将导致需要N次查找的顺序(调用get<3>(t)
看起来像t.rest().rest().first()
.我希望我离开这里...
然后,我希望编译器能够优化它以直接返回正确的偏移量,而不需要N个调用的开销.
基本上我想要的是:运行时有保证吗?这是否限制了如何std::tuple
实施?
在Python 3.5+ .decode("utf-8", "backslashreplace")
中,处理部分Unicode,部分未知的遗留编码二进制字符串是一个非常好的选择.将解码有效的UTF-8序列,并将无效的序列保留为转义序列.例如
>>> print(b'\xc2\xa1\xa1'.decode("utf-8", "backslashreplace"))
¡\xa1
Run Code Online (Sandbox Code Playgroud)
这就失去了b'\xc2\xa1\xa1'
和之间的区别b'\xc2\xa1\\xa1'
,但是如果你在"只是给我一些不太有损的东西,以后我可以手工修理"的心态,那可能就行了.
但是,这是Python 3.5中的一项新功能.我正在研究的程序也需要支持3.4和2.7.在这些版本中,它会抛出异常:
>>> print(b'\xc2\xa1\xa1'.decode("utf-8", "backslashreplace"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
TypeError: don't know how to handle UnicodeDecodeError in error callback
Run Code Online (Sandbox Code Playgroud)
我找到了一个近似值,但不是一个确切的等价物:
>>> print(b'\xc2\xa1\xa1'.decode("latin1")
... .encode("ascii", "backslashreplace").decode("ascii"))
\xc2\xa1\xa1
Run Code Online (Sandbox Code Playgroud)
行为不依赖于解释器版本是非常重要的.任何人都可以建议的方式来获得精确地在2.7和3.4的Python 3.5的行为?
(2.x或3.x的旧版本不需要工作.猴子修补codecs
是完全可以接受的.)
python encoding backwards-compatibility python-2.7 python-3.x
python ×5
c++ ×2
c++11 ×2
git ×2
lambda ×2
bash ×1
encoding ×1
javascript ×1
mocking ×1
pre-commit ×1
python-2.7 ×1
python-3.x ×1
sinon ×1
unit-testing ×1