我不想在Python模块中使用"print"语句,因为我们将使用记录器.
我正在尝试生成一个脚本来检查带有pylint的模块.但是,pylint当前不会将此检测为警告或错误.
我想根据我们的内部Python编程标准将"打印"调用检测为错误或警告.
我怎样才能做到这一点?
如果我的 git 目录更改,我有一个 bash PS1 会显示红色,如果没有更改,我会显示绿色。
if [ $? -eq 0 ]; then \
echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
if [ "$?" -eq "0" ]; then \
# @4 - Clean repository - nothing to commit
echo "\n'$Green'"$(__git_ps1 "(%s)"'$Color_Off'); \
else \
# @5 - Changes to working tree
echo "\n'$IRed'"$(__git_ps1 "(%s)"'$Color_Off'); \
fi)\$ "; \
fi)'
Run Code Online (Sandbox Code Playgroud)
这工作正常!但问题是在某些工作目录中非常慢,因为存在许多变化和很大的差异。
在没有完整的情况下获得 git status boolean (yes changed or no changed) 的更好方法是什么?
我试过git status --short …
在 Python 中使用 Psycopg2 和以下代码:
import psycopg2
import getpass
conn = psycopg2.connect("dbname=mydb user=%s" % getpass.getuser())
cursor = conn.cursor()
tables = ["user", "group", "partner", "product"]
for table in tables:
# with sql injection
cursor.execute("SELECT name FROM %s LIMIT 1" % (table,))
print "table", table, "result", len(cursor.fetchone())
# without sql injection
cursor.execute("SELECT name FROM %s LIMIT 1", (table,))
print "table", table, "result", len(cursor.fetchone())
Run Code Online (Sandbox Code Playgroud)
输出是:
table res_partner result 1
Traceback (most recent call last):
File "my_psycopg2_example.py", line 16, in <module>
cursor.execute("SELECT name FROM …
Run Code Online (Sandbox Code Playgroud) 我有一个名为example_dict.py的文件
#This is a valid comment
{
'key1': 'value1',
'key2': 'value2',
'key3': 'value3',
}
Run Code Online (Sandbox Code Playgroud)
然后我读了这个文件并转换dict:
from collections import OrderedDict
with open("example_dict.py") as fp:
dict_from_file = OrderedDict( eval( fp.read() ) )
Run Code Online (Sandbox Code Playgroud)
但是这个"dict_from_file"没有相同的顺序key1,key2,key3.
我怎么能以同样的顺序得到这个字典.