좋은 Python ORM 솔루션은 무엇입니까? [닫은]
저는 기본적으로 백엔드의 Python 웹 서비스와 통신하는 클라이언트 측 (브라우저)의 JavaScript 프론트 엔드 프로젝트에 CherryPy를 사용하고 평가하고 있습니다. 따라서 백엔드에는 실제로 Python을 사용하여 구현할 수있는 ORZ (브라우저의 JSON)를 통해 PostgreSQL DB와 통신 할 수있는 빠르고 가벼운 것이 필요합니다.
ORM이 내장되어 있기 때문에 내가 좋아하는 Django 도보 고 있습니다. 그러나 장고는 내가 필요로하는 것보다 조금 더 많을 수도 있다고 생각합니다 (즉, 실제로 필요한 것보다 더 많은 기능 == 느림).
누구나 다른 Python ORM 솔루션에 대한 경험이 있으십니까? 기능과 속도, 효율성 등을 비교하고 대조 할 수 있습니까?
SQLAlchemy는 기능이 더 강력하고 강력합니다 (DataMapper 패턴 사용). Django ORM은 구문이 더 깔끔하고 (ActiveRecord 패턴)을 작성하기가 더 쉽습니다. 성능 차이에 대해 모르겠습니다.
SQLAlchemy는 또한 약간의 복잡성을 숨기고 Django ORM과 더 유사한 ActiveRecord 스타일의 구문을 제공 하는 선언적 계층 을 가지고 있습니다.
Django가 "너무 무겁다"고 걱정하지 않습니다. 나머지를 가져 오지 않고 원하는 경우 ORM을 사용할 수있을만큼 분리되었습니다.
즉, 웹 계층에 이미 CherryPy를 사용하고 있고 ORM이 필요한 경우 SQLAlchemy를 선택했을 것입니다.
경량을 찾고 있으며 장고 스타일 선언 모델에 이미 익숙한 경우 peewee를 확인하십시오. https://github.com/coleifer/peewee
예:
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')
더 많은 예제 는 문서 를 확인하십시오 .
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()
그리고 다음을 수행해야 할 때 원시 SQL로 쉽게 넘어갈 수 있습니다.
store.execute('UPDATE bars SET bar_name=? WHERE bar_id like ?', [])
store.commit()
나는 보통 SQLAlchemy 사용 합니다. 꽤 강력하고 아마도 가장 성숙한 파이썬 ORM 일 것입니다.
CherryPy를 사용하려는 경우 Robert Brewer (현재 CherryPy 프로젝트 리더 인 사람)가 dejavu 를 살펴볼 수도 있습니다 . 나는 개인적으로 그것을 사용하지 않았지만 그것을 좋아하는 사람들을 알고 있습니다.
SQLObject 는 SQLAlchemy보다 ORM을 사용하는 것이 약간 쉽지만 강력하지는 않습니다.
개인적으로, Django에서 전체 프로젝트를 작성할 계획이 아니라면 Django ORM을 사용하지 않을 것입니다.
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:
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()
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.
This seems to be the canonical reference point for high-level database interaction in Python: http://wiki.python.org/moin/HigherLevelDatabaseProgramming
From there, it looks like Dejavu implements Martin Fowler's DataMapper pattern fairly abstractly in Python.
I think you might look at:
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.
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.
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.
I'd check out 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.
Here's a small example on creating and selecting orm objects
>>> 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')>
참고URL : https://stackoverflow.com/questions/53428/what-are-some-good-python-orm-solutions
'Programing' 카테고리의 다른 글
C ++에서 int를 열거 형으로 캐스팅하는 방법은 무엇입니까? (0) | 2020.05.09 |
---|---|
String replace ()와 replaceAll ()의 차이점 (0) | 2020.05.09 |
마크 다운 테이블의 줄 바꿈? (0) | 2020.05.09 |
힘내 : "당신이 누구인지 알려주세요"오류 (0) | 2020.05.09 |
Windows의 로컬 파일 시스템에서 GIT 복제 저장소 (0) | 2020.05.09 |