我有一些测试需要计算函数引发的警告数量.在Python 2.6中,这很简单,使用
with warnings.catch_warnings(record=True) as warn:
...
self.assertEquals(len(warn), 2)
Run Code Online (Sandbox Code Playgroud)
不幸的是,with在Python 2.4中没有,所以我还能使用什么呢?我不能简单地检查是否有一个警告(使用action='error'和try/的警告过滤器catch),因为警告的数量很大.
filterous被使用 iterparse来解析简单的XML StringIO对象中一个单元测试.但是,在尝试访问该StringIO对象之后,Python将以" ValueError: I/O operation on closed file"消息退出.根据iterparse文档,"从lxml 2.3开始,.close()方法也将在错误情况下被调用,"但我没有收到任何错误消息或Exception来自iterparse.我的IO-foo显然没有达到速度,所以有人有建议吗?
命令和(希望)相关代码:
$ python2.6 setup.py test
Run Code Online (Sandbox Code Playgroud)
setup.py:
from setuptools import setup
from filterous import filterous as package
setup(
...
test_suite = 'tests.tests',
Run Code Online (Sandbox Code Playgroud)
测试/ tests.py:
from cStringIO import StringIO
import unittest
from filterous import filterous
XML = '''<posts tag="" total="3" ...'''
class TestSearch(unittest.TestCase):
def setUp(self):
self.xml = StringIO(XML)
self.result = StringIO()
...
def test_empty_tag_not(self):
"""Empty …Run Code Online (Sandbox Code Playgroud) 有什么方法可以psql将字段和记录分开\0,也就是 NUL?这是能够将任意数据传递给 Bash 脚本的唯一方法。
根据Matthew Wood的回答,我希望这会1在新初始化的数据库上打印更多内容:
declare -i count=0
echo "\pset recordsep '\000'
\f '\000'
select typname from pg_type" | \
sudo -iu postgres psql --no-align --quiet --tuples-only -d dbname -U username | while IFS= read -r -d ''
do
#echo "$REPLY"
let count++
done
if [ -n "$REPLY" ]
then
#echo "$REPLY"
let count++
fi
echo $count
Run Code Online (Sandbox Code Playgroud)
解决方法:如果SELECT结果是唯一的,您可以使用此解决方法一次处理一个:
next_record() {
psql --no-align --quiet --tuples-only -d dbname …Run Code Online (Sandbox Code Playgroud) 随着date现场我可以做这个:
ORDER BY ABS(expiry - CURRENT_DATE)
Run Code Online (Sandbox Code Playgroud)
使用timestamp字段我收到以下错误:
函数abs(interval)不存在
从Bash手册:
Bash通过执行命令并用命令的标准输出替换命令替换来执行扩展,删除任何尾随换行符.
这意味着在处理具有有意义的尾随换行符的输出时,可能会出现模糊的错误.一个人为的例子:
user@host:~$ path='test
'
user@host:~$ touch -- "$path"
user@host:~$ readlink -fn -- "$path"
/home/user/test
user@host:~$ full_path="$(readlink -fn -- "$path")"
user@host:~$ ls -- "$full_path"
ls: cannot access /home/user/test: No such file or directory
Run Code Online (Sandbox Code Playgroud)
有关如何将命令的值赋给变量而不丢失语义上有用的数据的任何提示?
Bash的冒险继续另一天!
我有一个脚本(源码)来解析svn info为Bash创建一个合适的字符串$PS1.不幸的是,这不适用于我正在使用的运行Perl 5.8.8的系统 - 它输出所有行而不是仅输出匹配.什么是Perl 5.8.8相当于以下?
__svn_ps1()
{
local result=$(
svn info 2>/dev/null | \
perl -pe 's;^URL: .*?/((trunk)|(branches|tags)/([^/]*)).*;\2\4 ;p')
if [ -n "$result" ]
then
printf "${1:- (%s)}" $result
fi
}
Run Code Online (Sandbox Code Playgroud)
Perl 5.10的输出只包含空格,括号,分支名称,标记名称或trunk最后括号之一.Perl 5.8.8(没有最终版本p)的输出包含此加上svn info输出的每个空格分隔部分的括号版本.
一个可能的解决方法涉及和命令grep '^URL: '之间的简单,但我希望避免这种情况,因为这将针对每个Bash提示执行.svnperl
以下代码导致在Dropwizard 0.9.2和1.0.2中打印JSON服务器响应:
return ClientBuilder
.newBuilder()
.build()
.register(new LoggingFilter(Logger.getLogger(LoggingFilter.class.getName()), true))
Run Code Online (Sandbox Code Playgroud)
例如:
Oct 21, 2016 7:57:42 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Client response received on thread main
1 < 401
1 < Connection: keep-alive
1 < Content-Length: 49
1 < Content-Type: text/plain
1 < Date: Fri, 21 Oct 2016 07:57:42 GMT
1 < Server: […]
1 < WWW-Authenticate: Basic realm="[…]"
Credentials are required to access this resource.
javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
Run Code Online (Sandbox Code Playgroud)
但是,LoggingFilter在1.0.2中已弃用,建议使用LoggingFeature.在LoggingFeature的文档中,它说默认的详细程度是LoggingFeature.Verbosity.PAYLOAD_TEXT …
我试图可视化一个项目的数据库,并看到建议使用它django-extensions来做到这一点,所以我遵循了文档。我所做的是:
安装pyparsing并pydot使用 pip 以及graphviz不使用 pip 进行安装。另外修改我的设置如下:
#settings.py
INSTALLED_APPS = ['blabla', ... 'django-extensions']
GRAPH_MODELS = {
'all_applications': True,
'group_models': True,
}
Run Code Online (Sandbox Code Playgroud)
./manage.py graph_models --pydot -a -g -o my_project_visualized.png我在容器中运行了命令。
pyparsing我看到还有其他线程,人们对和的版本有问题pydot。我没有指定任何版本,因为安装两者以及运行上述命令时都没有问题。
我有一个包含以下内容的 Django REST Framework 序列化程序:
from rest_framework import serializers
class ThingSerializer(serializers.ModelSerializer):
last_changed = serializers.SerializerMethodField(read_only=True)
def get_last_changed(self, instance: Thing) -> str:
log_entry = LogEntry.objects.get_for_object(instance).latest()
representation: str = serializers.DateTimeField('%Y-%m-%dT%H:%M:%SZ').to_representation(log_entry.timestamp)
return representation
Run Code Online (Sandbox Code Playgroud)
这是有问题的,因为如果日期时间格式发生变化,它将与所有其他datetimes 不同。我想重用 DRF 用于序列化其他datetime字段的代码路径。
到目前为止我尝试过的:
rest_framework.serializers.DateTimeField().to_representation(log_entry.timestamp),rest_framework.fields.DateTimeField().to_representation(log_entry.timestamp)并rest_framework.fields.DateTimeField(format=api_settings.DATETIME_FORMAT).to_representation(log_entry.timestamp)没有任何工作; 它们以微秒精度生成字符串。我已经用调试器验证了 DRF 在序列化其他字段时调用后者,所以我不明白为什么它会在我的情况下产生不同的结果。LogEntry.timestamp被声明为 a django.db.DateTimeField,但如果我尝试类似的事情,LogEntry.timestamp.to_representation(log_entry.timestamp)它会失败:
AttributeError: 'DeferredAttribute' 对象没有属性 'to_representation'
python ×4
bash ×2
django ×2
postgresql ×2
datetime ×1
dropwizard ×1
iterparse ×1
java ×1
jersey ×1
json ×1
newline ×1
nul ×1
perl ×1
perl5.8 ×1
python-2.4 ×1
regex ×1
sql-order-by ×1
stringio ×1
warnings ×1