我开始简单:
hoops=# select * from core_school limit 3;
id | school_name | nickname
----+------------------+----------
1 | Marshall |
2 | Ohio |
3 | Houston |
(10 rows)
Run Code Online (Sandbox Code Playgroud)
让我们介绍一个故意错误:
hoops=# select name from core_school;
ERROR: column "name" does not exist
LINE 1: select name from core_school;
Run Code Online (Sandbox Code Playgroud)
但为什么这有效呢?(意外结果!):
hoops=# select core_school.name from core_school limit 3;
name
-----------------
(1,Marshall,"")
(2,Ohio,"")
(3,Houston,"")
(3 rows)
Run Code Online (Sandbox Code Playgroud)
"名称"列在哪里来自第三个查询?
我在打开与paramiko的SFTP连接时遇到了一些麻烦.我目前的代码是:
client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
client.load_system_host_keys()
client.connect('some.example.com', username="myuser", password="mypassword")
sftp_client = client.open_sftp()
sftp_client.put(my_local_file)
Run Code Online (Sandbox Code Playgroud)
但是在我点击client.open_sftp()的时候,我得到了"无法打开频道"的例外.
知道是什么原因引起的吗?我已经能够使用命令行sftp客户端打开与服务器的连接.
我在这里猜测我的调用,如果有人能指出我的例子,这将是伟大的.
我希望pytest-django在创建测试数据库时安装Postgres扩展。我一直在弄混conftest.py尝试使其工作,但是我被卡住了。
我的conftest.py位于项目的顶层(与manage.py相同的目录),并且包含:
from django.db import connection
import pytest_django
@pytest.fixture(scope='session')
def django_db_setup(*args, **kwargs):
pytest_django.fixtures.django_db_setup(*args, **kwargs)
cursor = connection.cursor()
cursor.execute("create extension pg_trgm")
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我得到:
_pytest.vendored_packages.pluggy.PluginValidationError: unknown hook 'pytest_django' in plugin <module 'conftest' from '/path/to/my/conftest.py'>
Run Code Online (Sandbox Code Playgroud) 我要么错过了一些非常简单的东西(并且缺少我的 Google-Fu),要么我偏离了轨道。你来打电话吧!
我想编写一个测试来练习将表单集发布到视图。
# get the modelformset from the view
response = self.client.get("/myview")
formset = response.context['formset']
# change the values in the underlying form
for form in enumerate(formset):
form.instance['something'] = i
# how do I post the formset back to myview? this does NOT work...
response = self.client.post("/myview", formset, follow=True)
AttributeError: 'MyFormSet' object has no attribute 'items'
Run Code Online (Sandbox Code Playgroud)
该错误是完全有道理的,因为我需要传递字典作为第二个参数,而不是表单集。我希望 Formset 上有某种方法可以为我提供适当的字典(带有管理表单信息),但我一生都找不到它。
更新:
我通过这样做让它工作:
data = {}
for field in formset.management_form:
data["-".join((formset.management_form.prefix, field.name))] = field.value()
for form in formset:
for field in …Run Code Online (Sandbox Code Playgroud) 我试图从BeautifulSoup中解脱出来,我喜欢但似乎(积极地)不受支持.我正在尝试使用html5lib和lxml,但我似乎无法弄清楚如何使用"find"和"findall"运算符.
通过查看html5lib的文档,我想出了一个测试程序:
import cStringIO
f = cStringIO.StringIO()
f.write("""
<html>
<body>
<table>
<tr>
<td>one</td>
<td>1</td>
</tr>
<tr>
<td>two</td>
<td>2</td
</tr>
</table>
</body>
</html>
""")
f.seek(0)
import html5lib
from html5lib import treebuilders
from lxml import etree # why?
parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("lxml"))
etree_document = parser.parse(f)
root = etree_document.getroot()
root.find(".//tr")
Run Code Online (Sandbox Code Playgroud)
但是这会返回None.我注意到,如果我这样做,etree.tostring(root)我会收回所有数据,但我的所有标签都以html(例如<html:table>)开头.但root.find(".//html:tr")抛出一个KeyError.
有人能让我回到正轨吗?
我很确定我已经看过这个,但我无法正确理解语法.我想在测试期间覆盖模块"常量".我可以写这样的代码:
import mymodule
try:
hold = mymodule.FOO
mymodule.FOO = 'test value'
do_something()
finally:
mymodule.FOO = hold
Run Code Online (Sandbox Code Playgroud)
在我看来应该有一种方法来做一个"with"语句,如:
with mymodule.FOO = 'test value':
do_something()
Run Code Online (Sandbox Code Playgroud)
我的思想欺骗了我吗?是否有一个简单的语法来做我想要的?