- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
“使事情尽可能简单,但不要简单。”
我们能否找到解决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 2中完成。您要写:
class Task(model):
title = ''
isDone = False
model
可能是什么,但不能通过
来预测两个字段的任何“排序”,因为class
语句的语义是:dict
Task('Illustrate different syntax modes', True)
db.tasklist
和db['tasklist']
的愿望更加令人难以置信)。__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.
model
获得)将具有__prepare__
类方法返回有序字典。这消除了特定的问题,但是,当然,只有在您愿意使用此“魔术ORM”将所有代码切换到Python 3时,您才能这么做吗?taskList
属性名称是否特殊,或者分配给db
对象的任何其他属性是否应在使用时“自动保存”(按名称和其他特性)并“自动检索”?是否有删除实体,更改实体,定位实体的方法(不是曾经被列出在db
对象的同一属性中)?您的示例代码如何知道要使用的数据库服务以及如何对其进行身份验证(例如,通过用户名和密码)(如果需要身份验证)?model
的元类将内省(introspection)该类的字段并为该类生成GAE Model
,db
对象将使用__setattr__
设置一个atexit
触发器以存储属性的最终值(作为实体,当然是另一种Model
中的实体) ,以及__getattr__
从存储中取回该属性的信息。当然,如果没有一些额外的数据库功能,所有这些都将毫无用处;-)。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')
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)
lustdb
和 friend 的传统,模块的lustdb
名称是带有OP首字母(名字和姓氏的每个前两个字母)的单词的游戏-我认为听起来不错(以及大多数其他明显的名称,例如因为采用awk
和simpledb
;-)。
关于database - Django模型/SQLAlchemy肿!有没有真正的Pythonic数据库模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2978138/
我的问题是如何在 python 中创建一个简单的数据库。我的例子是: User = { 'Name' : {'Firstname', 'Lastname'}, 'Address' : {'Street
我需要创建一个与远程数据库链接的应用程序! mysql 是最好的解决方案吗? Sqlite 是唯一的本地解决方案吗? 我使用下面的方法,我想知道它是否是最好的方法! NSString *evento
给定两台 MySQL 服务器,一台本地,一台远程。两者都有一个包含表 bohica 的数据库 foobar。本地服务器定义了用户 'myadmin'@'%' 和 'myadmin'@'localhos
我有以下灵活的搜索查询 Select {vt:code},{vt:productcode},{vw:code},{vw:productcode} from {abcd AS vt JOIN wxyz
好吧,我的电脑开始运行有点缓慢,所以我重置了 Windows,保留了我的文件。因为我的大脑还没有打开,所以我忘记事先备份我的 MySQL 数据库。我仍然拥有所有原始文件,因此我实际上仍然拥有数据库,但
如何将我的 Access 数据库 (.accdb) 转换为 SQLite 数据库 (.sqlite)? 请,任何帮助将不胜感激。 最佳答案 1)如果要转换 db 的结构,则应使用任何 DB 建模工具:
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
我想在相同的 phonegap 应用程序中使用 android 数据库。 更多说明: 我创建了 phonegap 应用程序,但 phonegap 应用程序不支持服务,所以我们已经在 java 中为 a
Time Tracker function clock() { var mytime = new Date(); var seconds
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
我有一个可以收集和显示各种测量值的产品(不会详细介绍)。正如人们所期望的那样,显示部分是一个数据库+建立在其之上的网站(使用 Symfony)。 但是,我们可能还会创建一个 API 来向第三方公开数据
我们将 SQL Server 从 Azure VM 迁移到 Azure SQL 数据库。 Azure VM 为 DS2_V2、2 核、7GB RAM、最大 6400 IOPS Azure SQL 数据
我正在开发一个使用 MongoDB 数据库的程序,但我想问在通过 Java 执行 SQL 时是否可以使用内部数据库进行测试,例如 H2? 最佳答案 你可以尝试使用Testcontainers Test
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 此问题似乎与 a specific programming problem, a sof
我正在尝试使用 MSI 身份验证(无需用户名和密码)从 Azure 机器学习服务连接 Azure SQL 数据库。 我正在尝试在 Azure 机器学习服务上建立机器学习模型,目的是我需要数据,这就是我
我在我的 MySQL 数据库中使用这个查询来查找 my_column 不为空的所有行: SELECT * FROM my_table WHERE my_column != ""; 不幸的是,许多行在
我有那个基地:http://sqlfiddle.com/#!2/e5a24/2这是 WordPress 默认模式的简写。我已经删除了该示例不需要的字段。 如您所见,我的结果是“类别 1”的两倍。我喜欢
我有一张这样的 table : mysql> select * from users; +--------+----------+------------+-----------+ | userid
我有表: CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL
我是一名优秀的程序员,十分优秀!