简体   繁体   English

简单/智能,Pythonic数据库解决方案,可以使用Python类型+语法吗? (键/值字典,数组,可能是有序字典)

[英]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) 手动编写SQL查询(Python可能更多OO不通过DSL字符串)
  • Using non-Python datatypes for a supposedly required model definition 将非Python数据类型用于所需的模型定义
  • Using a new class of types rather than perfectly good native Python types 使用新类型而不是完美的原生Python类型

Boast

  • Using Python objects 使用Python对象
  • Using Object Oriented and key based retrieval and creation 使用面向对象和基于密钥的检索和创建
  • Quick protoyping 快速原型设计
  • No SQL table to make 没有SQL表
  • Model /Type inference or no model 模型/类型推断或没有模型
  • Less lines and characters to type 输入更少的行和字符

Easily output to and from JSON , maybe XML or even Protocol Buffers . 轻松输出和输出JSON ,可能是XML甚至是协议缓冲区

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 . 就像一个不同的导入语句,也许更多,但Django模型和SQLAlchemy不要削减它

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. 我并不反对将Python类用于模型,但它们应该非常直接,不像你用Django和类似的东西。

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 目前还没有自述或足够的测试,但是... 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. LiteMap类的行为与内置字典非常相似,但它仍然存在于SQLite数据库中。 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 (eg like appending to the list in your example), but the API is really simple. 它也不跟踪可变类的更改(例如,在示例中附加到列表中),但API非常简单。

数据库访问并没有比SQLAlchemy更好。

Care to explain what about Django's models you don't find straightforward? 注意解释Django的模型你觉得哪些不直接? Here's how I'd do what you have in Django: 这就是我在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. 此外,导致稍微更清洁的表/对象命名方案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. 更棘手的部分是使用Django的DB模块与Django的其余部分分开,如果这就是你所追求的,但它可以完成。

Using web2py: 使用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. 支持10种不同的数据库后端+谷歌应用引擎。

Question looks strikingly similar to http://api.mongodb.org/python/1.9%2B/tutorial.html 问题看起来与http://api.mongodb.org/python/1.9%2B/tutorial.html非常相似

So answer is pymongo, what else ;) 所以答案是pymongo,还有什么;)

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. BTW Redish也很有趣也很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM