什么是好的Python或m解决方案?

时间:2021-09-06 06:55:51

I'm evaluating and looking at using CherryPy for a project that's basically a JavaScript front-end from the client-side (browser) that talks to a Python web service on the back-end. So, I really need something fast and lightweight on the back-end that I can implement using Python that then speaks to the PostgreSQL DB via an ORM (JSON to the browser).

我正在评估并考虑使用CherryPy作为一个项目,它基本上是一个JavaScript前端,从客户端(浏览器)到后台的Python web服务。因此,我确实需要一些在后端快速、轻量级的东西,我可以使用Python实现,然后通过ORM(浏览器的JSON)与PostgreSQL DB进行通信。

I'm also looking at Django, which I like, since its ORM is built-in. However, I think Django might be a little more than I really need (i.e. more features than I really need == slower?).

我也在关注Django,我喜欢它,因为它的ORM是内置的。然而,我认为Django可能比我真正需要的要多一点(也就是说,比我真正需要的特性多一点=慢一点?)

Anyone have any experience with different Python ORM solutions that can compare and contrast their features and functionality, speed, efficiency, etc.?

任何人都有不同的Python ORM解决方案的经验,可以比较和对比它们的特性和功能、速度、效率等等。

12 个解决方案

#1


81  

SQLAlchemy is more full-featured and powerful (uses the DataMapper pattern). Django ORM has a cleaner syntax and is easier to write for (ActiveRecord pattern). I don't know about performance differences.

SQLAlchemy功能更全面,功能更强大(使用DataMapper模式)。Django ORM的语法更简洁,更易于编写(ActiveRecord模式)。我不知道性能差异。

SQLAlchemy also has a declarative layer that hides some complexity and gives it a ActiveRecord-style syntax more similar to the Django ORM.

SQLAlchemy还有一个声明层,它隐藏了一些复杂性,并为它提供了与Django ORM更类似的activerecord样式的语法。

I wouldn't worry about Django being "too heavy." It's decoupled enough that you can use the ORM if you want without having to import the rest.

我不会担心Django会“太重”。它已经解耦了,你可以使用ORM,如果你不想导入其他的。

That said, if I were already using CherryPy for the web layer and just needed an ORM, I'd probably opt for SQLAlchemy.

也就是说,如果我已经在web层上使用CherryPy并且只需要一个ORM,我可能会选择SQLAlchemy。

#2


93  

If you're looking for lightweight and are already familiar with django-style declarative models, check out peewee: https://github.com/coleifer/peewee

如果您正在寻找轻量级的,并且已经熟悉了django样式的声明模型,请查看peewee: https://github.com/coleifer/peewee

Example:

例子:

import datetime
from peewee import *

class Blog(Model):
    name = CharField()

class Entry(Model):
    blog = ForeignKeyField(Blog)
    title = CharField()
    body = TextField()
    pub_date = DateTimeField(default=datetime.datetime.now)

# query it like django
Entry.filter(blog__name='Some great blog')

# or programmatically for finer-grained control
Entry.select().join(Blog).where(Blog.name == 'Some awesome blog')

Check the docs for more examples.

查看文档以获得更多的示例。

#3


77  

Storm has arguably the simplest API:

Storm可以说是最简单的API:

  from storm.locals import *

  class Foo:
      __storm_table__ = 'foos'
      id = Int(primary=True)


  class Thing:
      __storm_table__ = 'things'
      id = Int(primary=True)
      name = Unicode()
      description = Unicode()
      foo_id = Int()
      foo = Reference(foo_id, Foo.id)

  db = create_database('sqlite:')
  store = Store(db)

  foo = Foo()
  store.add(foo)
  thing = Thing()
  thing.foo = foo
  store.add(thing)
  store.commit()

And it makes it painless to drop down into raw SQL when you need to:

当你需要的时候,它可以让你轻松地进入原始SQL:

store.execute('UPDATE bars SET bar_name=? WHERE bar_id like ?', []) 
store.commit()

#4


27  

I usually use SQLAlchemy. It's pretty powerful and is probably the most mature python ORM.

我通常使用SQLAlchemy。它非常强大,可能是最成熟的python ORM。

If you're planning on using CherryPy, you might also look into dejavu as it's by Robert Brewer (the guy that is the current CherryPy project leader). I personally haven't used it, but I do know some people that love it.

如果您打算使用CherryPy,您也可以查看dejavu,因为它是由Robert Brewer(现在CherryPy项目的负责人)开发的。我个人还没有使用过它,但我确实认识一些喜欢它的人。

SQLObject is a little bit easier to use ORM than SQLAlchemy, but it's not quite as powerful.

SQLObject比SQLAlchemy更容易使用ORM,但它没有SQLAlchemy强大。

Personally, I wouldn't use the Django ORM unless I was planning on writing the entire project in Django, but that's just me.

就我个人而言,我不会使用Django ORM,除非我正在计划在Django中编写整个项目,但那只是我自己。

#5


17  

SQLAlchemy's declarative extension, which is becoming standard in 0.5, provides an all in one interface very much like that of Django or Storm. It also integrates seamlessly with classes/tables configured using the datamapper style:

SQLAlchemy的声明性扩展在0.5中逐渐成为标准,它在一个接口中提供了all,非常类似于Django或Storm。它还与使用datamapper风格配置的类/表无缝集成:

Base = declarative_base()

class Foo(Base):
    __tablename__ = 'foos'
    id = Column(Integer, primary_key=True)

class Thing(Base):
    __tablename__ = 'things'

    id = Column(Integer, primary_key=True)
    name = Column(Unicode)
    description = Column(Unicode)
    foo_id = Column(Integer, ForeignKey('foos.id'))
    foo = relation(Foo)

engine = create_engine('sqlite://')

Base.metadata.create_all(engine)  # issues DDL to create tables

session = sessionmaker(bind=engine)()

foo = Foo()
session.add(foo)
thing = Thing(name='thing1', description='some thing')
thing.foo = foo  # also adds Thing to session
session.commit()

#6


9  

We use Elixir alongside SQLAlchemy and have liked it so far. Elixir puts a layer on top of SQLAlchemy that makes it look more like the "ActiveRecord pattern" counter parts.

除了SQLAlchemy,我们还使用了Elixir,到目前为止,我们都很喜欢它。Elixir在SQLAlchemy上添加了一个层,使其看起来更像“ActiveRecord模式”的计数器部件。

#7


4  

This seems to be the canonical reference point for high-level database interaction in Python: http://wiki.python.org/moin/HigherLevelDatabaseProgramming

这似乎是Python中高级数据库交互的标准参考点:http://wiki.python.org/moin/HigherLevelDatabaseProgramming

From there, it looks like Dejavu implements Martin Fowler's DataMapper pattern fairly abstractly in Python.

从这里看来,Dejavu在Python中相当抽象地实现了Martin Fowler的数据映射模式。

#8


1  

I think you might look at:

我想你可以看看

Autumn

秋天

Storm

风暴

#9


1  

There is no conceivable way that the unused features in Django will give a performance penalty. Might just come in handy if you ever decide to upscale the project.

Django中未使用的特性将会导致性能损失,这是无法想象的。如果你决定升级这个项目的话,可能会派上用场。

#10


0  

SQLAlchemy is very, very powerful. However it is not thread safe make sure you keep that in mind when working with cherrypy in thread-pool mode.

SQLAlchemy非常非常强大。但是它不是线程安全的,请确保在使用线程池模式下的cherrypy时记住这一点。

#11


-1  

I used Storm + SQLite for a small project, and was pretty happy with it until I added multiprocessing. Trying to use the database from multiple processes resulted in a "Database is locked" exception. I switched to SQLAlchemy, and the same code worked with no problems.

我在一个小项目中使用了Storm + SQLite,并对它非常满意,直到我添加了multiprocessing。尝试使用来自多个进程的数据库会导致“数据库被锁定”异常。我切换到SQLAlchemy,同样的代码没有问题。

#12


-5  

I'd check out SQLAlchemy

我看看SQLAlchemy

It's really easy to use and the models you work with aren't bad at all. Django uses SQLAlchemy for it's ORM but using it by itself lets you use it's full power.

它真的很容易使用,你使用的模型一点也不坏。Django为它的ORM使用SQLAlchemy,但是单独使用它可以让您使用它的全部功能。

Here's a small example on creating and selecting orm objects

这里有一个创建和选择orm对象的小示例

>>> ed_user = User('ed', 'Ed Jones', 'edspassword')
>>> session.add(ed_user)
>>> our_user = session.query(User).filter_by(name='ed').first() 
>>> our_user
    <User('ed','Ed Jones', 'edspassword')>

#1


81  

SQLAlchemy is more full-featured and powerful (uses the DataMapper pattern). Django ORM has a cleaner syntax and is easier to write for (ActiveRecord pattern). I don't know about performance differences.

SQLAlchemy功能更全面,功能更强大(使用DataMapper模式)。Django ORM的语法更简洁,更易于编写(ActiveRecord模式)。我不知道性能差异。

SQLAlchemy also has a declarative layer that hides some complexity and gives it a ActiveRecord-style syntax more similar to the Django ORM.

SQLAlchemy还有一个声明层,它隐藏了一些复杂性,并为它提供了与Django ORM更类似的activerecord样式的语法。

I wouldn't worry about Django being "too heavy." It's decoupled enough that you can use the ORM if you want without having to import the rest.

我不会担心Django会“太重”。它已经解耦了,你可以使用ORM,如果你不想导入其他的。

That said, if I were already using CherryPy for the web layer and just needed an ORM, I'd probably opt for SQLAlchemy.

也就是说,如果我已经在web层上使用CherryPy并且只需要一个ORM,我可能会选择SQLAlchemy。

#2


93  

If you're looking for lightweight and are already familiar with django-style declarative models, check out peewee: https://github.com/coleifer/peewee

如果您正在寻找轻量级的,并且已经熟悉了django样式的声明模型,请查看peewee: https://github.com/coleifer/peewee

Example:

例子:

import datetime
from peewee import *

class Blog(Model):
    name = CharField()

class Entry(Model):
    blog = ForeignKeyField(Blog)
    title = CharField()
    body = TextField()
    pub_date = DateTimeField(default=datetime.datetime.now)

# query it like django
Entry.filter(blog__name='Some great blog')

# or programmatically for finer-grained control
Entry.select().join(Blog).where(Blog.name == 'Some awesome blog')

Check the docs for more examples.

查看文档以获得更多的示例。

#3


77  

Storm has arguably the simplest API:

Storm可以说是最简单的API:

  from storm.locals import *

  class Foo:
      __storm_table__ = 'foos'
      id = Int(primary=True)


  class Thing:
      __storm_table__ = 'things'
      id = Int(primary=True)
      name = Unicode()
      description = Unicode()
      foo_id = Int()
      foo = Reference(foo_id, Foo.id)

  db = create_database('sqlite:')
  store = Store(db)

  foo = Foo()
  store.add(foo)
  thing = Thing()
  thing.foo = foo
  store.add(thing)
  store.commit()

And it makes it painless to drop down into raw SQL when you need to:

当你需要的时候,它可以让你轻松地进入原始SQL:

store.execute('UPDATE bars SET bar_name=? WHERE bar_id like ?', []) 
store.commit()

#4


27  

I usually use SQLAlchemy. It's pretty powerful and is probably the most mature python ORM.

我通常使用SQLAlchemy。它非常强大,可能是最成熟的python ORM。

If you're planning on using CherryPy, you might also look into dejavu as it's by Robert Brewer (the guy that is the current CherryPy project leader). I personally haven't used it, but I do know some people that love it.

如果您打算使用CherryPy,您也可以查看dejavu,因为它是由Robert Brewer(现在CherryPy项目的负责人)开发的。我个人还没有使用过它,但我确实认识一些喜欢它的人。

SQLObject is a little bit easier to use ORM than SQLAlchemy, but it's not quite as powerful.

SQLObject比SQLAlchemy更容易使用ORM,但它没有SQLAlchemy强大。

Personally, I wouldn't use the Django ORM unless I was planning on writing the entire project in Django, but that's just me.

就我个人而言,我不会使用Django ORM,除非我正在计划在Django中编写整个项目,但那只是我自己。

#5


17  

SQLAlchemy's declarative extension, which is becoming standard in 0.5, provides an all in one interface very much like that of Django or Storm. It also integrates seamlessly with classes/tables configured using the datamapper style:

SQLAlchemy的声明性扩展在0.5中逐渐成为标准,它在一个接口中提供了all,非常类似于Django或Storm。它还与使用datamapper风格配置的类/表无缝集成:

Base = declarative_base()

class Foo(Base):
    __tablename__ = 'foos'
    id = Column(Integer, primary_key=True)

class Thing(Base):
    __tablename__ = 'things'

    id = Column(Integer, primary_key=True)
    name = Column(Unicode)
    description = Column(Unicode)
    foo_id = Column(Integer, ForeignKey('foos.id'))
    foo = relation(Foo)

engine = create_engine('sqlite://')

Base.metadata.create_all(engine)  # issues DDL to create tables

session = sessionmaker(bind=engine)()

foo = Foo()
session.add(foo)
thing = Thing(name='thing1', description='some thing')
thing.foo = foo  # also adds Thing to session
session.commit()

#6


9  

We use Elixir alongside SQLAlchemy and have liked it so far. Elixir puts a layer on top of SQLAlchemy that makes it look more like the "ActiveRecord pattern" counter parts.

除了SQLAlchemy,我们还使用了Elixir,到目前为止,我们都很喜欢它。Elixir在SQLAlchemy上添加了一个层,使其看起来更像“ActiveRecord模式”的计数器部件。

#7


4  

This seems to be the canonical reference point for high-level database interaction in Python: http://wiki.python.org/moin/HigherLevelDatabaseProgramming

这似乎是Python中高级数据库交互的标准参考点:http://wiki.python.org/moin/HigherLevelDatabaseProgramming

From there, it looks like Dejavu implements Martin Fowler's DataMapper pattern fairly abstractly in Python.

从这里看来,Dejavu在Python中相当抽象地实现了Martin Fowler的数据映射模式。

#8


1  

I think you might look at:

我想你可以看看

Autumn

秋天

Storm

风暴

#9


1  

There is no conceivable way that the unused features in Django will give a performance penalty. Might just come in handy if you ever decide to upscale the project.

Django中未使用的特性将会导致性能损失,这是无法想象的。如果你决定升级这个项目的话,可能会派上用场。

#10


0  

SQLAlchemy is very, very powerful. However it is not thread safe make sure you keep that in mind when working with cherrypy in thread-pool mode.

SQLAlchemy非常非常强大。但是它不是线程安全的,请确保在使用线程池模式下的cherrypy时记住这一点。

#11


-1  

I used Storm + SQLite for a small project, and was pretty happy with it until I added multiprocessing. Trying to use the database from multiple processes resulted in a "Database is locked" exception. I switched to SQLAlchemy, and the same code worked with no problems.

我在一个小项目中使用了Storm + SQLite,并对它非常满意,直到我添加了multiprocessing。尝试使用来自多个进程的数据库会导致“数据库被锁定”异常。我切换到SQLAlchemy,同样的代码没有问题。

#12


-5  

I'd check out SQLAlchemy

我看看SQLAlchemy

It's really easy to use and the models you work with aren't bad at all. Django uses SQLAlchemy for it's ORM but using it by itself lets you use it's full power.

它真的很容易使用,你使用的模型一点也不坏。Django为它的ORM使用SQLAlchemy,但是单独使用它可以让您使用它的全部功能。

Here's a small example on creating and selecting orm objects

这里有一个创建和选择orm对象的小示例

>>> ed_user = User('ed', 'Ed Jones', 'edspassword')
>>> session.add(ed_user)
>>> our_user = session.query(User).filter_by(name='ed').first() 
>>> our_user
    <User('ed','Ed Jones', 'edspassword')>