10

如果我将CapacityMin类和 unittest 类放在同一个 .py 文件中,一切都很好。但是在我将 CapacityMin类移动到一个单独的文件并运行单元测试之后,我得到了这个错误:

需要 SQL 表达式、列或映射实体

细节:

InvalidRequestError: SQL expression, column, or mapped entity expected - got '<module 'Entities.CapacityMin' from 'D:\trunk\AppService\Common\Entities\CapacityMin.pyc'>'

但这不好。

最小容量.py

import sqlalchemy
from sqlalchemy import *
from  sqlalchemy.ext.declarative  import  declarative_base

Base  =  declarative_base()

class  CapacityMin(Base):
    '''

    table definition:
        ID        INT NOT NULL auto_increment,
        Server    VARCHAR (20) NULL,
        FeedID    VARCHAR (10) NULL,
        `DateTime` DATETIME NULL,
        PeakRate  INT NULL,
        BytesRecv INT NULL,
        MsgNoSent INT NULL,
        PRIMARY KEY (ID)
    '''

    __tablename__  =  'capacitymin'

    ID  =  Column(Integer,  primary_key=True)
    Server  =  Column(String)
    FeedID  =  Column(String)
    DateTime  =  Column(sqlalchemy.DateTime)
    PeakRate = Column(Integer)
    BytesRecv = Column(Integer)
    MsgNoSent = Column(Integer)

    def __init__(self, server, feedId, dataTime, peakRate, byteRecv, msgNoSent):
        self.Server = server
        self.FeedID = feedId
        self.DateTime = dataTime
        self.PeakRate = peakRate
        self.BytesRecv = byteRecv
        self.MsgNoSent = msgNoSent

    def __repr__(self):
        return "<CapacityMin('%s','%s','%s','%s','%s','%s')>" % (self.Server, self.FeedID ,
                self.DateTime ,self.PeakRate,
                self.BytesRecv, self.MsgNoSent)



if __name__ == '__main__':
    pass
4

1 回答 1

19

您正在使用模块,而不是模块中的类。

我怀疑你是这样使用它的:

from Entities import CapacityMin

当您打算使用时:

from Entities.CapacityMin import CapacityMin

这种混淆是Python 样式指南 (PEP 8)建议为您的模块使用小写名称的原因之一。您的导入将是:

from entities.capacitymin import CapacityMin

你的错误会更容易被发现。

于 2012-08-10T09:21:39.907 回答