Python psycopg2错误vars函数 [英] Python psycopg2 error vars function

查看:139
本文介绍了Python psycopg2错误vars函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码可以在一台服务器上正常运行,而不能在其他服务器(Linux服务器)上运行

I have a piece of code that work fine on a server and don't work on other server ( Linux servers)

import psycopg2,psycopg2.extras
conn = psycopg2.connect("host=xx.x.x.x dbname=dev user=user password=pass" )
parentId='272'
dbCur = conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
dbCur.execute('select * from "treeItem" where "parentId" = %s order by "order"',(parentId,))
for row in dbCur:
    print type(row)
    print row.__dict__
    vars(row)
dbCur.close()
conn.close()

服务器上的输出错误是:

The output on the server error is :

class 'psycopg2.extras.Record'
Traceback (most recent call last):
  File "test1.py", line 8, in <module>
    print row.__dict__
AttributeError: 'Record' object has no attribute '__dict__'

,但是它可以在其他服务器上正常工作。
相同版本的python(2.7)和psycopg2 2.5

but it work on the other server without problem. Same version of python (2.7) and psycopg2 2.5

psycopg2.extras类的记录如何具有 __ dict __ 在环境中而不在其他环境中?


编辑

在python 2.7.3和psycopg2 2.5(dt dec pq3 ext)上工作

不要在python 2.7.5 psycopg2 2.5.1(dt dec pq3 ext)上工作

How class psycopg2.extras.Record can have __dict__ on an environment and not in other?

Edit
Work on python 2.7.3 and psycopg2 2.5 (dt dec pq3 ext)
Dont work on python 2.7.5 psycopg2 2.5.1 (dt dec pq3 ext)

推荐答案

您正在使用 psycopg2.extras.NamedTupleCursor ,它生成使用 collections.namedtuple()类工厂。这些类使用 __ slots __ 限制其内存使用。

You are using a psycopg2.extras.NamedTupleCursor, which produces instances of a class produced with the collections.namedtuple() class factory. These classes use __slots__ to limit their memory use.

通常,具有 __ slots __ <的类/ code>没有 __ dict __ 属性。但是,在Python 2.7.3中添加了 __ dict __ 属性(请参见修订版26d5f022eb1a ),专门用于支持 vars()使用。 后来的更改再次从Python 2.7.5中删除了该支持,然后是 。该属性充当代理,调用 namedtuple._asdict()生成 OrderedDict 对象。

Normally, classes with __slots__ don't have a __dict__ attribute. However, in Python 2.7.3 a __dict__ property was added (see revision 26d5f022eb1a), specifically to support vars() use. A later change removed the support again from Python 2.7.5, then it was readded for Python 2.7.6. The property acts as a proxy, calling namedtuple._asdict() to produce an OrderedDict object.

您有一个不带属性的Python 2.7.x版本;例如2.7、2.7.1、2.7.2或2.7.5之一。您可以通过调用 namedtuple来解决此问题。 ._asdict()方法代替:

You have a Python 2.7.x release that doesn't have the property; e.g. one of 2.7, 2.7.1, 2.7.2 or 2.7.5. You can work around this by calling the namedtuple._asdict() method instead:

for row in dbCur:
    print type(row)
    print row._asdict()

这篇关于Python psycopg2错误vars函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆