一个小巧且快速的跟随/关注数据库。
项目描述
friendlydb 是一个用Python编写的轻量级且快速的跟随/关注数据库。它可以直接从Python代码中使用,也可以通过HTTP和小的Web API使用。
FriendlyDB 并不打算作为一个完整的用户系统;它应该被用来增强现有的系统以跟踪关系。
警告
从v2.0.0版本开始,FriendlyDB 不与v0.4.0及之前版本兼容。在v2.0.0之前,数据存储在文件系统中,但在v2.0.0及以后,数据存储在Redis中。
重写为使用Redis的几个原因
更好的性能
减少对硬盘的磨损/损坏
更简单的代码
但这确实意味着您需要运行自己的Redis服务器版本(推荐2.6.4+)。
如果您需要从旧版本安装迁移到v2.0.0,请参阅下面的说明。
用法
从Python使用FriendlyDB看起来像这样
from friendlydb.db import FriendlyDB
# Start using the DB (assumes Redis default host/port/db).
fdb = FriendlyDB()
# Alternatively, ``fdb = FriendlyDB(host='127.0.0.2', port=7100, db=3)``
# Grab a user by their username.
daniel = fdb['daniel']
# Follow a couple users.
daniel.follow('alice')
daniel.follow('bob')
daniel.follow('joe')
# Check the following.
daniel.following()
# Returns:
# [
# 'alice',
# 'bob',
# 'joe',
# ]
# Check joe's followers.
fdb['joe'].followers()
# Returns:
# [
# 'daniel',
# ]
# Unfollow.
daniel.unfollow('bob')
# Check the following.
daniel.following()
# Returns:
# [
# 'alice',
# 'joe',
# ]
# Dust off & nuke everything from orbit.
fdb.clear()
从HTTP使用FriendlyDB看起来像(所有尾随斜杠都是可选的)
# In one shell, start the server.
python friendlydb/server.py -d /tmp/friendly
# From another, run some URLs.
curl -X GET http://127.0.0.1:8008/
# {"version": "0.3.0"}
curl -X GET http://127.0.0.1:8008/daniel/
# {"username": "daniel", "following": [], "followers": []}
curl -X POST http://127.0.0.1:8008/daniel/follow/alice/
# {"username": "daniel", "other_username": "alice", "followed": true}
curl -X POST http://127.0.0.1:8008/daniel/follow/bob/
# {"username": "daniel", "other_username": "bob", "followed": true}
curl -X POST http://127.0.0.1:8008/daniel/follow/joe/
# {"username": "daniel", "other_username": "joe", "followed": true}
curl -X POST http://127.0.0.1:8008/daniel/unfollow/joe/
# {"username": "daniel", "other_username": "joe", "unfollowed": true}
curl -X GET http://127.0.0.1:8008/daniel/
# {"username": "daniel", "following": ["alice", "bob"], "followers": []}
curl -X GET http://127.0.0.1:8008/daniel/is_following/alice/
# {"username": "daniel", "other_username": "alice", "is_following": true}
curl -X GET http://127.0.0.1:8008/alice/is_followed_by/daniel/
# {"username": "alice", "other_username": "daniel", "is_followed_by": true}
curl -X GET http://127.0.0.1:8008/alice/is_followed_by/joe/
# {"username": "alice", "other_username": "joe", "is_followed_by": false}
需求
Python 2.6+ 或 Python 3.3+
redis.py >= 2.7.2
(可选)gevent用于HTTP服务器
(可选)unittest2用于运行测试
安装
使用pip,您可以使用 pip install friendlydb 安装它。
性能
您可以通过运行包含的 benchmark.py 脚本来查看FriendlyDB的性能。
在一台2011年MacBook Pro(i7)上的测试中,基准脚本展示了
创建了10,000个用户之间的一百万个关系:179秒(比0.4.0版本快2.5倍)
获取一个用户关注者的平均时间:0.0016秒
内存RSS从未超过41MB
从v0.4.0迁移到2.0.0
首先,安装并运行Redis服务器。
其次,运行 pip install redis>=2.7.2。
要迁移您的数据,最简单的方法是在您的旧版FriendlyDB安装(使用HTTP服务器)的基础上,创建一个带有Redis的新安装,然后运行如下代码:
import requests
import json
# The new version.
from friendlydb import FriendlyDB
old_url = 'http://127.0.0.1:8008/'
fdb = FriendlyDB()
for username in users:
user = fdb[username]
# Following.
resp = requests.get("{0}/{1}/following/".format(old_url, username))
data = json.loads(resp.content)
for f_username in data.get("following", []):
user.follow(f_username)
您应该创建自己的脚本并验证迁移后的数据。上述代码的有效性和准确性不提供任何保证。
运行测试
friendlydb始终保持通过测试。只需运行
python -m unittest2 tests
贡献
为了使贡献有资格合并,它必须满足以下要求
补丁干净地解决了问题
添加了测试覆盖率(现在通过)以暴露错误并检查回归
如果行为影响最终用户,必须有关于更改的文档
补丁/测试必须与New BSD许可兼容
提交贡献的最佳方式是通过在GitHub上分叉项目,将更改应用于新分支,将更改推送到GitHub,并通过GitHub界面提交拉取请求。
许可证
New BSD许可。
- 作者::
Daniel Lindsley
- 版本::
2.0.0
- 日期::
2013-01-17
项目详情
下载文件
下载您平台上的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。