尽我所能,我似乎无法正确捕获sqlalchemy IntegrityError:
from sqlalchemy import exc
try:
insert_record()
except exc.IntegrityError, exc:
print exc # this is never called
handle_elegantly() # this is never called
Run Code Online (Sandbox Code Playgroud)
正如人们所期望的那样:
IntegrityError: (IntegrityError) insert or update on table "my_table"
violates foreign key constraint "my_table_some_column_fkey"
Run Code Online (Sandbox Code Playgroud)
我试图明确地说:
from sqlalchemy.exc import IntegrityError
Run Code Online (Sandbox Code Playgroud)
更新:
我找到了一些似乎适合这里发生的事情,在会话被刷新到db之后,并且在执行try/ exceptblocks 之后不会抛出Integrity Error :尝试使用SQLAlchemy捕获完整性错误
但是,session.flush()在try块中添加会产生InvalidRequestError:
ERROR:root:This Session's transaction has been rolled back due to a previous
exception during flush. To begin a new transaction with …Run Code Online (Sandbox Code Playgroud) python error-handling exception-handling sqlalchemy exception
我正在尝试在OS X终端中配置我的IPython输出,但似乎我试图设置的所有更改都没有生效.我正在尝试配置显示设置,以便更大的输出像大DataFrame输出而不会截断或作为摘要信息.
在将pandas导入我的脚本后,我有一些选项设置,我尝试了一大堆,但任何一个(或所有,就此而言)似乎没有生效.我正在使用IPython运行脚本%run.我在这里做错了吗?
import pandas as pd
pd.set_option('display.expand_max_repr', False)
pd.set_option('display.max_columns', 30)
pd.set_option('display.width', None)
pd.set_option('display.line_width', 200)
Run Code Online (Sandbox Code Playgroud)
我已经查看了Stack 上的一些线程,并且pandas FAQ无济于事,即使在display namespace(或没有)下使用它们,正如我在这里尝试的那样.
据我所知,有一些解决这个办法,如调用to_string()或describe()在你的输出方式,但这些都是手动的,非常不总是工作打算在某些情况下,像一个在那里我有叫to_string()上一个groupby对象得到:
id type
106125 puzzle gameplay_id sitting_id user_id ...
106253 frames gameplay_id sitting_id user_id ...
106260 trivia gameplay_id sitting_id user_id ...
Run Code Online (Sandbox Code Playgroud)
我的终端窗口大小足以容纳宽度,并且调用pd.util.terminal.get_terminal_size()正确地找到窗口大小元组,因此看起来自动检测大小也不起作用.任何见解将不胜感激!
我正在使用IPython处理允许该DataFrame对象的pandas模块.当我运行一些代码时,我得到一个输出,其中DataFrame输出在我的终端宽度之前包装,尽管终端宽度应该适应长度.这个问题似乎只与pandas Series和DataFrame对象隔离,而不是说很长的列表.
pip uninstall readline通过easy_install 运行然后重新安装readline并重新启动IPython并没有解决问题.
看到我的数据没有像这样分解会很有帮助,但老实说我不知道从哪里开始解决这个问题.任何见解?

我在python中得到了似乎很奇怪的行为.我无法弄清楚这是一个怪癖还是我做错了什么.为什么传入一个对象的self.var作为其方法之一的参数不允许我分配已经初始化的self.var?
class Test():
def __init__(self):
self.num1 = 0
self.num2 = 0
def selfexp(self, n, metric):
result = n ** n
metric = result
print metric
a = Test()
a.selfexp(2, a.num1)
a.selfexp(5, a.num2)
print a.num1
print a.num2
Run Code Online (Sandbox Code Playgroud)
输出:
4
3125
0 # expected 4
0 # expected 3125
Run Code Online (Sandbox Code Playgroud) python ×4
ipython ×2
pandas ×2
terminal ×2
class ×1
exception ×1
python-2.7 ×1
self ×1
sqlalchemy ×1