What are some good Python ORM solutions? [closed]
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).
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?).
Anyone have any experience with different Python ORM solutions that can compare and contrast their features and functionality, speed, efficiency, etc.?
Asked by: Maria534 | Posted: 28-01-2022
Answer 1
If you're looking for lightweight and are already familiar with django-style declarative models, check out 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.
Answered by: Anna365 | Posted: 01-03-2022Answer 2
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 also has a declarative layer that hides some complexity and gives it a ActiveRecord-style syntax more similar to the Django ORM.
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.
That said, if I were already using CherryPy for the web layer and just needed an ORM, I'd probably opt for SQLAlchemy.
Answered by: Grace380 | Posted: 01-03-2022Answer 3
Storm has arguably the simplest 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:
store.execute('UPDATE bars SET bar_name=? WHERE bar_id like ?', [])
store.commit()
Answered by: Rebecca525 | Posted: 01-03-2022
Answer 4
I usually use SQLAlchemy. It's pretty powerful and is probably the most mature 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.
SQLObject is a little bit easier to use ORM than SQLAlchemy, but it's not quite as powerful.
Personally, I wouldn't use the Django ORM unless I was planning on writing the entire project in Django, but that's just me.
Answered by: Kelvin328 | Posted: 01-03-2022Answer 5
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()
Answered by: Elise794 | Posted: 01-03-2022
Answer 6
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.
Answered by: Briony140 | Posted: 01-03-2022Answer 7
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.
Answered by: Emily450 | Posted: 01-03-2022Answer 8
I think you might look at:
Answered by: Patrick899 | Posted: 01-03-2022Answer 9
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.
Answered by: Lana822 | Posted: 01-03-2022Answer 10
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.
Answered by: Rafael737 | Posted: 01-03-2022Answer 11
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.
Answered by: Ryan370 | Posted: 01-03-2022Answer 12
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')>
Answered by: Ada426 | Posted: 01-03-2022
Similar questions
python - z3 number of solutions
How do I use z3 to count the number of solutions? For example, I want to prove that for any n, there are 2 solutions to the set of equations {x^2 == 1, y_1 == 1, ..., y_n == 1}. The following code shows satisfiability for a given n, which isn't quite what I want (I want number of solutions for an arbitrary n).
#!/usr/bin/env python
from z3 import *
# Add...
python - Using lists to find solutions
I am trying to produce code that will first find me all the perfect squares under modulos p, in a function perfectsq(p).
With that list of perfect squares. I want to find all solutions of the equation y^2=x^3+Ax+B. I am doing this by using the list in perfectsq(p) to check that m=x^3+Ax+B is in that list. Can someone tell me why this code isn't compil...
python - Sympy Solve For All Solutions On Domain
For periodic functions, how do I tell sympy I want ALL the solutions on some domain?
Example:
import sympy
import sympy.parsing.sympy_parser
SympyExpression = sympy.parsing.sympy_parser.parse_expr( 'sin(pi* x)*sin(pi*y)' )
Variables = [sympy.Symbol('x'),sympy.Symbol('y') ]
Zeros = sympy.solve( f = SympyExpression, symbols = Variables )
print Zeros
Output:
python - How to add two solutions
So this code works, but I want it to print two solutions off a text-file. The problem includes trying to print out two lines in a text file, when keywords are used with if 'word' == something then print, I want to print two lines. Any help would be appreciated. I have tried various different things.
problem = False
while problem == False:
foo = open("solutions.txt","r")
print("What i...
python - Sympy step by step solutions
Sympygamma offers step by step solutions for example for derivatives like this one: http://www.sympygamma.com/input/?i=diff%28log%28x%29+%2B+2*x**2%2Cx%29
How can I use this directly with sympy (e.g. in an ipython console or in a python script)?
python - How to Remove this error try different solutions but can't
ImportError at /
No module named forms
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.6.11
Exception Type: ImportError
Exception Value:
No module named forms
Exception Location: /home/zaheer/django-user/login/myapp/views.py in , line 11
Python Executable: /usr/bin/python
Python V...
python - Other solutions for this name error don't work for my code
I can't solve this one. I read some answers to the same error, but their solutions don't work. I'm out of ideas. Why is the error happening? The error is: NameError: name 'self' is not defined
Thank you.
class irNum:
def __innit__(self):
pass
def approx(irNum, digitsNum, digitsDivider, cycles):
self.approximate(irNum, digitsNum, digitsDivider, cycles)
def approximate(se...
math - How to list all solutions in 24 game using Python
Recently I created a 24 game solver with python
Read this website if you do not know what the 24 game is:
https://www.pagat.com/adders/24.html
Here is the code:
from itertools import permutations, product, chain, zip_longest
from fractions import Fraction as F
solutions = []
def ask4():
num1 = input("Enter Firs...
python - Cant get rid of 'nan' from my list. Please suggest solutions what am I doing wrong?
Following is the code where I want to get all the non nan values into a separate files but its not working! I tried blank, I tried isnan, or just 'nan' but I still get a list with the nans in them.
import numpy as np
ponum1 = [i for i in ponu...
python - Different response by screen solutions using Django and jQuery
In a thumbnail website, if I want to display 100x100 thumbs on screnn resolutions lower than 1280x1024 while display 150x150 thumbs for screens higher than 1280x1024, is the following procedure correct?
Render a page frame with no thumbs by view1()
On page frame loaded, it detects client's screen resolution and pass it to another Django view call fetchthumb()
View fetchthumb() create html co...
IPC solutions for Python processes on POSIX compliant system
I have two Python processes that need to communicate with each other on POSIX complaint system, as an IPC I thought that using a named pipe would be the easiest solution, however since I'm new with Python I suspect there are more options available. Anyone care to make a recommendation, besides a named pipe?
Thanks in advance,
John
Memory error solutions in Python
I'm loading a lot of objects with dicts containing large string values. Overall, the program exceeds 2GB and crashes. It doesn't exceed by much, but I could well have even larger data later.
It seems Python 32bit is unable to access more memory. I suppose for the future I need some object database system which is able to handle large data and still not be too slow (i.e. store in DB or harddrive, but keep some in me...
Python Memory error solutions if permanent access is required
first, I am aware of the amount of Python memory error questions on SO, but so far, none has matched my use case.
I am currently trying to parse a bunch of textfiles (~6k files with ~30 GB) and store each unique word. Yes, I am building a wordlist, no I am not planning on doing evil things with it, it is for the university.
I implemented the list of found words as a set (created with words = set([])
python - What tracking solutions are available for server side code?
I'm working on a tracking proxy (for want of a better term) written in Python. It's a simple http (wsgi) application that will run on one (maybe more) server and accepts event data from a desktop client. This service would then forward the tracking data on to some actual tracking platform (DeskMetrics, MixPanel, Google Analytics) so that we don't have to deal with the slicing and dicing of data.
The reason for thi...
python - (Z3Py) checking all solutions for equation
In Z3Py, how can I check if equation for given constraints have only one solution?
If more than one solution, how can I enumerate them?
python - Find unique solutions of N-queens puzzle recursive algorithm
# -*- coding: utf-8 -*-
def puzzle(rows, cols):
if rows == 0:
return [[]]
else:
return new_queen(rows - 1, cols, puzzle(rows - 1, cols))
def new_queen(new_row, cols, plsd_queens):
new_solutions = []
for solution in plsd_queens:
for new_col in range(cols):
if test(new_row, new_col, solution):
new_solutions.append(solution + [new_col])
return...
python - z3 number of solutions
How do I use z3 to count the number of solutions? For example, I want to prove that for any n, there are 2 solutions to the set of equations {x^2 == 1, y_1 == 1, ..., y_n == 1}. The following code shows satisfiability for a given n, which isn't quite what I want (I want number of solutions for an arbitrary n).
#!/usr/bin/env python
from z3 import *
# Add...
Python Circular import: solutions and best practice
I'm writing an application for scientific data analysis and I'm wondering what's the best way to structure the code to avoid (or address) the circular import problem. Currently I'm using a mix of OO and procedural programming.
Other questions
Python function returns None (all trivial solutions checked and they do not work)
This question already has answers here:
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python