开发者

Simple / Smart, Pythonic database solution, can use Python types + syntax? (Key / Value Dict, Array, maybe Ordered Dict)

Looking for solutions that push the envelope and:

Avoid

  • Manually writing SQL queries(Python can be more OO not passing DSL strings)
  • Using non-Python datatypes for a supposedly required model definition
  • Using a new class of types rather than perfectly good native Python types

Boast

  • Using Python objects
  • Using Object Oriented and key based retrieval and creation
  • Quick protoyping
  • No SQL table to make
  • M开发者_运维技巧odel /Type inference or no model
  • Less lines and characters to type

Easily output to and from JSON, maybe XML or even Protocol Buffers.

I do web, desktop and mobile software development so the more portable the better.

python
>> from someAmazingDB import *

>> db.taskList = []

>> db['taskList'].append({title:'Beat old sql interfaces','done':False})
>> db.taskList.append({title:'Illustrate different syntax modes','done':True})

#at this point it should autosave
#we should be able to reload the console and access like:
python
>> from someAmazingDB import *
>> print 'Done tasks'
>> for task in db.taskList:
>>     if task.done:
>>         print task
'Illustrate different syntax modes'

Here is the challenge: The above code should work with very little modification or thinking required. Like a different import statement and maybe a little more but Django Models and SQLAlchemy DO NOT CUT IT.

I'm looking for more interesting library suggestions than just "Try Shelve" or "use pickle"

I'm not opposed to Python classes being used for models but they should be really straight forward, unlike the stuff you see with Django and similar.


I've was actually working on something like this earlier today. There is no readme or sufficient tests yet, but... http://github.com/mikeboers/LiteMap/blob/master/litemap.py

The LiteMap class behaves much like the builtin dict, but it persists into a SQLite database. You did not indicate what particular database you were interested in, but this could be almost trivially modified to any back end.

It also does not track changes to mutable classes (e.g. like appending to the list in your example), but the API is really simple.


Database access doesn't get better than SQLAlchemy.


Care to explain what about Django's models you don't find straightforward? Here's how I'd do what you have in Django:

from django.db import models
class Task(models.Model):
    title = models.CharField(max_length=...)
    is_done = models.BooleanField()
    def __unicode__(self):
        return self.title

----

from mysite.tasks.models import Task
t = Task(title='Beat old sql interfaces', is_done=True)
t.save()

----

from mysite.tasks.models import Task
print 'Done tasks'
for task in Task.objects.filter(is_done=True):
    print task

Seems pretty straightforward to me! Also, results in a slightly cleaner table/object naming scheme IMO. The trickier part is using Django's DB module separate from the rest of Django, if that's what you're after, but it can be done.


Using web2py:

>>> from gluon.sql import DAL, Field
>>> db=DAL('sqlite://stoarge.db')
>>> db.define_table('taskList',Field('title'),Field('done','boolean')) # creates the table

>>> db['taskList'].insert(title='Beat old sql interfaces',done=False)
>>> db.taskList.insert(title='Beat old sql interfaces',done=False)

>> for task in db(db.taskList.done==True).select():
>>     print task.title

Supports 10 different database back-ends + google app engine.


Question looks strikingly similar to http://api.mongodb.org/python/1.9%2B/tutorial.html

So answer is pymongo, what else ;)

from pymongo import Connection
connection = Connection()
connection = Connection('localhost', 27017)
tasklist = db['test-tasklist']
tasklist.append({title:'Beat old sql interfaces','done':False})
db.tasklist.append({title:'Illustrate different syntax modes','done':True})
for task in db.tasklist.find({done:True}):
    print task.title

I haven't tested the code but wont be very different than this

BTW Redish is also interesting and fun.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜