gpt4 book ai didi

database - Django模型/SQLAlchemy肿!有没有真正的Pythonic数据库模型?

转载 作者:太空狗 更新时间:2023-10-29 22:05:14 24 4
gpt4 key购买 nike

使事情尽可能简单,但不要简单。

我们能否找到解决Python数据库世界的解决方案?

更新:A 'lustdb' prototype has been written by Alex Martelli-如果您知道任何轻量级的,具有多个后端的高级数据库库,我们可以包装语法糖蜜,请权衡一下!

from someAmazingDB import *  
#we imported a smart model class and db object which talk to database adapter/s
class Task (model):
title = ''
done = False #native types not a custom object we have to think about!

db.taskList = []
#or
db.taskList = expandableTypeCollection(Task) #not sure what this syntax would be

db['taskList'].append(Task(title='Beat old sql interfaces',done=False))
db.taskList.append(Task('Illustrate different syntax modes',True)) # ok maybe we should just use kwargs

#at this point it should be autosaved to a default db option

#by default we should be able to reload the console and access the default db:

>> from someAmazingDB import *
>> print 'Done tasks:'
>> for task in db.taskList:
>> if task.done:
>> print task.title
'Illustrate different syntax modes'

我通常是Python,webPy和Cherry Py以及 KISS 的粉丝。

我们正在谈论将Python自动转换为SQL类型或NoSQL。
我们不必完全兼容SQL!只是一个可扩展的子集,或者忽略它!

Re:model更改,可以在开发人员尝试更改它或具有一组合理的默认值时询问开发人员。

这是挑战:上面的代码应该可以在很少的修改或思考的情况下工作。当我们知道得更多时,为什么我们必须忍受妥协呢?

在2010年,我们应该能够在 sleep 中编写可扩展的简单数据库。

如果您认为这很重要,请投票!

最佳答案

出于特定原因,无论您要求什么,都无法在Python 2中完成。您要写:

class Task(model): 
title = ''
isDone = False

在Python 2.anything中, 可能是 model可能是什么,但不能通过 来预测两个字段的任何“排序”,因为class语句的语义是:
  • 执行主体,从而准备一个dict
  • 查找元类并运行其特殊方法

  • 无论元类是什么,步骤1都破坏了字段顺序的任何可预测性。

    因此,您希望在代码段中使用位置参数:
    Task('Illustrate different syntax modes', True)

    无法将参数的值与模型的各个字段关联。 (尝试通过类型关联来猜测-希望没有两个字段具有相同的类型-比您表达的无意且可互换地使用db.tasklistdb['tasklist']的愿望更加令人难以置信)。

    Python 3中向后不兼容的更改之一是专门针对这种情况而引入的。在Python 3中,自定义元类可以定义一个__prepare__函数,该函数在上述简化列表中的“步骤1”之前运行,这使它可以更好地控制类的主体。具体来说,引用PEP 3115 ...:

    __prepare__ returns a dictionary-like object which is used to store the class member definitions during evaluation of the class body. In other words, the class body is evaluated as a function block (just like it is now), except that the local variables dictionary is replaced by the dictionary returned from __prepare__. This dictionary object can be a regular dictionary or a custom mapping type.


    ...

    An example would be a metaclass that uses information about the ordering of member declarations to create a C struct. The metaclass would provide a custom dictionary that simply keeps a record of the order of insertions.



    您不想像本例中那样“创建C结构”,但是字段的顺序至关重要(以允许使用所需的位置参数),因此自定义元类(通过基本model获得)将具有__prepare__类方法返回有序字典。这消除了特定的问题,但是,当然,只有在您愿意使用此“魔术ORM”将所有代码切换到Python 3时,您才能这么做吗?

    解决之后,问题便是,您要执行什么数据库操作以及如何执行。当然,您的示例根本无法澄清这一点。 taskList属性名称是否特殊,或者分配给db对象的任何其他属性是否应在使用时“自动保存”(按名称和其他特性)并“自动检索”?是否有删除实体,更改实体,定位实体的方法(不是曾经被列出在db对象的同一属性中)?您的示例代码如何知道要使用的数据库服务以及如何对其进行身份验证(例如,通过用户名和密码)(如果需要身份验证)?

    您列出的特定任务将很容易实现(例如,在Google App Engine的存储服务之上,该服务不需要身份验证,也不需要指定“要使用的数据库服务”)。 model的元类将内省(introspection)该类的字段并为该类生成GAE Modeldb对象将使用__setattr__设置一个atexit触发器以存储属性的最终值(作为实体,当然是另一种Model中的实体) ,以及__getattr__从存储中取回该属性的信息。当然,如果没有一些额外的数据库功能,所有这些都将毫无用处;-)。

    编辑:我做了一个小原型(prototype)(Python 2.6,基于sqlite),放在http://www.aleax.it/lustdb.zip上-这是一个3K zip文件,包括225行lustdb.py(太长了,无法在此处发布)和两个大致等效的小测试文件到OP的原件:test0.py是...:
    from lustdb import *  

    class Task(Model):
    title = ''
    done = False

    db.taskList = []
    db.taskList.append(Task(title='Beat old sql interfaces', done=False))
    db.taskList.append(Task(title='Illustrate different syntax modes', done=True))

    test1.p1是...:
    from lustdb import *

    print 'Done tasks:'
    for task in db.taskList:
    if task.done:
    print task

    运行test0.py(在具有可写/tmp目录的机器上-即任何Unix-y OS,或者在Windows上以前曾运行过mkdir \tmp的操作系统上;-)没有输出;之后,运行test1.py输出:
    Done tasks:
    Task(done=True, title=u'Illustrate different syntax modes')

    请注意,这些在很多方面都比OP的示例“疯狂的神奇”少得多,例如...:
    1. no (expletive delete) redundancy whereby `db.taskList` is a synonym of `db['taskList']`, only the sensible former syntax (attribute-access) is supported
    2. no mysterious (and totally crazy) way whereby a `done` attribute magically becomes `isDone` instead midway through the code
    3. no mysterious (and utterly batty) way whereby a `print task` arbitrarily (or magically?) picks and prints just one of the attributes of the task
    4. no weird gyrations and incantations to allow positional-attributes in lieu of named ones (this one the OP agreed to)

    当然,原型(prototype)(如原型(prototype)一样;-)在许多方面(声明,文档,单元测试,优化,错误检查和诊断,不同后端之间的可移植性,尤其是DB功能所不包含的隐含功能)还有很多需要改进的地方。问题)。缺少的数据库功能是众多的(例如,OP的原始示例无法识别模型的“主键”或任何其他种类的唯一性约束,因此重复操作会比比皆是;并且从那里开始只会变得更糟;- )。不过,对于225行(190行中包含空行,注释和文档字符串;-),在我偏颇的观点中还不错。

    继续进行此项目的正确方法当然是在code.google.com(或任何其他具有问题跟踪,Wiki,代码审查支持,在线浏览,DVCS支持等)-我自己做,但是我可以在code.google.com上发起的开源项目数量已接近极限,并且不想“刻录” “以这种方式的最后一两个;-)。

    顺便说一句,按照lustdb和 friend 的传统,模块的lustdb名称是带有OP首字母(名字和姓氏的每个前两个字母)的单词的游戏-我认为听起来不错(以及大多数其他明显的名称,例如因为采用awksimpledb ;-)。

    关于database - Django模型/SQLAlchemy肿!有没有真正的Pythonic数据库模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2978138/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com