在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
每当有一个内联断言规则需要针对 bool 语句进行验证时,在 VSCode 中使用 python black 格式化程序都会破坏该行,导致 flake8 发出有关规则 W503 的警告
line break before binary operatorflake8(W503)
assert (
...
!= ...
)
Run Code Online (Sandbox Code Playgroud)
有没有解决这个问题而不是忽略该规则的方法?
所以我试图实现类似于单元测试框架如何执行以下操作:
class BaseTest(T.TestCase):
# Disables this test from being run
__test__ = False
def test_foo(self): pass
# However this test is picked up because it doesn't directly have __test__ set
class InheritingTest(BaseTest): pass
Run Code Online (Sandbox Code Playgroud)
我觉得奇怪的一件事:
# >> InheritingTest.__test__
# False
Run Code Online (Sandbox Code Playgroud)
这将表明,我认为这是不使用元类设置 __test__
到True
建筑的类型.
我尝试了通过python库,find . -name "*.py" | xargs grep '__test__'
但似乎没有发现任何与此相关的内容.
我解决这个问题的"猜测"方法是执行以下操作:
def is_class_tested(cls):
return cls.__dict__.get('__test__', True)
Run Code Online (Sandbox Code Playgroud)
然而,这对我来说感觉很脆弱......有没有更清洁/更好的方法来做到这一点在所有情况下都有效?班级是否有可能没有__dict__
财产?
我flake8
与一堆插件一起使用(,,flake8-docstrings
)。我已将它们全部预安装到.flake8-isort
flake8-black
venv
我的仓库要检查pre-commit
:
pyproject.toml
(配置black
和isort
)setup.cfg
(配置flake8
和pydocstyle
)\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.cfg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (the package)\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.cfg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (the package)\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 venv\n
Run Code Online (Sandbox Code Playgroud)\n我想为这两个包调用flake8
via 。pre-commit
这是我目前的做法:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.cfg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (the package)\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.cfg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (the package)\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 venv\n …
Run Code Online (Sandbox Code Playgroud) 我的团队在我们的存储库中使用预提交来运行各种代码检查和格式化程序。我的大多数队友都使用它,但有些人通过使用git commit --no-verify
. 无论如何,是否可以在 CI/CD 中运行某些内容以确保所有预提交挂钩都通过(我们正在使用 GitHub 操作)。如果至少有一个钩子失败,则抛出一个错误。
我想通过预提交忽略多个文件模式。例如“迁移/”。'和'测试/。' 但预提交配置文件中可用的排除参数仅接受字符串而不接受列表。我当前的配置文件:
.pre-commit-config.yaml
repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
language_version: python3.8
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
exclude: 'migrations/.*'
Run Code Online (Sandbox Code Playgroud)
尝试将排除更改为列表,并放入 2 个排除类别。两者都是无效配置
exclude:
- 'migrations/.*'
- 'tests/.*'
Run Code Online (Sandbox Code Playgroud)
exclude: 'migrations/.*'
exclude: 'tests/.*'
Run Code Online (Sandbox Code Playgroud) 以前我手动使用的Makefile看起来像这样:
.PHONY: all
all: tests
.PHONY: tests
tests: py_env
bash -c 'source py_env/bin/activate && py.test tests'
py_env: requirements_dev.txt setup.py
rm -rf py_env
virtualenv py_env
bash -c 'source py_env/bin/activate && pip install -r requirements_dev.txt'
Run Code Online (Sandbox Code Playgroud)
这有一个很好的副作用,如果我更改了requirements_dev.txt或setup.py,它将重建我的virtualenv.但感觉有点笨重.
我想tox
用来做类似的事情.我明白tox
有一个--recreate
选择,但我宁愿只在我需要时才打电话.
我的新设置是这样的:
# Makefile
.PHONY: all
all: tests
.PHONY: tests
tests:
tox
Run Code Online (Sandbox Code Playgroud)
和
# tox.ini
[tox]
project = my_project
envlist = py26,py27
[testenv]
install_command = pip install --use-wheel {opts} {packages}
deps = -rrequirements_dev.txt
commands =
py.test …
Run Code Online (Sandbox Code Playgroud) 在我的 python 项目中,我有 pre-commit-config.YAML,我想在其中创建我的自定义文件。
如果 python lint 错误大于特定数字,则此文件的目的是使 git commit 失败。以下命令将用于计算行数
pylint api/ | wc -l
Run Code Online (Sandbox Code Playgroud)
有人可以建议一些方法。我是 MAC 和 Python 生态系统的新手?
编辑 sh 文件看起来像这样。
#!/bin/sh
a=$(pylint source/ | wc -l)
b=20
errorsCount="$(echo "${a}" | tr -d '[:space:]')"
if [ $errorsCount -gt $b ]
then
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
我试过
repos:
- repo: local
hooks:
- id: custom-script-file
name: custom-script-file
entry: hooks/pre-commit.sh
language: script
types: [python]
pass_filenames: false
Run Code Online (Sandbox Code Playgroud)
但它不会奏效。
我正在尝试从 PyCharm 中的“提交”按钮启动我的预提交挂钩(v.2020.2)。我使用 conda venv (使用 创建conda create -n py38 python=3.8
)在其中安装了模块pip install
。
我的.pre-commit-config.yaml
读物是:
repos:
- repo: local
hooks:
- id: black
name: black
language: system
entry: black --check
types: [python]
- id: isort
name: isort
language: system
entry: isort --check-only
types: [python]
Run Code Online (Sandbox Code Playgroud)
我在这里使用local
repo 是因为我会将我的代码推送到未连接到互联网的 Intranet 存储库。
pre-commit run --all-files
在我的本地计算机上从命令行运行运行良好。但是当我尝试从 PyCharm 提交时(),它会引发以下错误:
Traceback (most recent call last):
File "c:\bib\envs\py38\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None, File "c:\bib\envs\py38\lib\runpy.py", line 86, …
Run Code Online (Sandbox Code Playgroud) 我尝试运行此命令,但它总是显示此错误,无论如何我都无法修复它。请帮帮我!
(venv)<...>预提交安装
[错误] 胆怯地拒绝安装带有core.hooksPath
套件的挂钩。
暗示:git config --unset-all core.hooksPath
python ×7
pre-commit ×6
git ×3
flake8 ×2
encoding ×1
makefile ×1
pycharm ×1
python-2.7 ×1
python-3.x ×1
python-black ×1
tox ×1
virtualenv ×1