0

想知道 sqlalchemy中的Base是否继承自Object。详见下文。

目前使用 Python3 和 SQLAlchemy 0.8

在我的项目中,我用 (object) 声明了一个新的样式类,然后我继续使用“属性”在我的类中定义属性。该类工作正常“下面的示例代码”

class LoadFile(object):

    def _set_year(self, value): 
        '''set the year'''    
        value = value[6:10]
        if valid_integer(value) != True:
            raise Exception ("File Name Error: The year value should be a numeral")
        self._year = value
    def _get_year(self):
        '''get the year'''
        return self._year

    year = property(_get_year, _set_year)

随着我的项目的增长,我开始使用 SQLAlchemy 并运行了这个类,它抛出了错误

class LoadFile(object):
    __tablename__ = "load_file"
    id = Column(Integer(10), primary_key = True)
    file_name = Column(String(250), nullable = False, unique = True)
    file_path = Column(String(500), nullable = False)
    date_submitted = Column(DateTime, nullable = False)
    submitted_by = Column(String, nullable = False)

    def _set_year(self, value): 
        '''set the year'''    
        value = value[6:10]
        if valid_integer(value) != True:
            raise Exception ("File Name Error: The year value should be a numeral")
        self._year = value
    def _get_year(self):
        '''get the year'''
        return self._year

    year = property(_get_year, _set_year)

它抛出的错误是:

AttributeError: 'LoadFile' object has no attribute '_sa_instance_state'

File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/orm/session.py", line 1369, in add
raise exc.UnmappedInstanceError(instance)
sqlalchemy.orm.exc.UnmappedInstanceError: Class '__main__.LoadFile' is not mapped

所以我注意到我没有从“ Base ”继承,所以我将我的课程改为:

class LoadFile(Base):

这样,sqlalchemy 工作正常,表创建成功。但是,我现在注意到我收到关于 eclipse 的警告说明

Use of "property" on an old style class"

所以我想知道, Base不是继承自 Object 吗?以为我之前读过它确实如此。否则,为什么我现在会收到这个“警告”。我知道我可以忽略它,但只是想找出确切的原因以及如何纠正它。

谢谢。

更新

我到处使用装饰器,如下所示。这样,上面的“警告”就消失了

@property
def year(self):
    '''get the year'''
    return self._year
@year.setter
def year(self, value): 
    '''set the year'''
    if valid_integer(value) != True:
        raise Exception ("File Name Error: The year value should be a numeral")
    self._year = value

这样就可以处理警告。但我仍然不明白为什么以前的方法有警告....另外,我不太确定哪个是使用装饰器的最佳方法还是以前的方法。

4

1 回答 1

0

Python 3 中不存在旧式类,因此警告是虚假的。检查 Eclipse 中的 Preferences 和 Project Properties,也许有对 Python 2.x 的引用。

于 2013-02-12T07:39:24.233 回答