Skip to main content

一个小而快速的追随者/追随者数据库。

项目描述

friendlydb是一个用 Python 编写的小而快的追随者/追随者数据库。它可以直接从您的 Python 代码中使用,也可以通过带有小型 Web API 的 HTTP 使用。

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

  • (可选)HTTP 服务器的 gevent

  • (可选)unittest2 用于运行测试

安装

使用 pip,您可以使用pip install friendlydb安装它。

表现

您可以通过运行包含的benchmark.py脚本来确定 FriendlyDB 的性能。

在 2011 款 MacBook Pro (i7) 上的测试中,基准脚本显示:

  • 在 10,000 个用户之间创建 1,000,000 个关系:179 秒(比 0.4.0 快约 2.5 倍)

  • 获取用户关注者的平均时间:0.0016 秒

  • 永远不会超过 41Mb 的 RAM RSS

从 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

贡献

为了考虑合并贡献,它必须满足以下要求:

  • 补丁干净地解决了问题

  • 添加了测试覆盖率(现在通过)以暴露错误并检查回归

  • 如果行为影响最终用户,则必须有有关更改的文档

  • 补丁/测试必须与新 BSD 兼容许可

提交贡献的最佳方式是在 Github 上分叉项目,在新分支上应用您的更改,将这些更改推送回 GH 并通过 GitHub 界面提交拉取请求。

执照

新的 BSD 许可证。

作者

丹尼尔林兹利

版本

2.0.0

日期

2013-01-17

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

friendlydb-2.0.0.tar.gz (7.1 kB 查看哈希)

已上传 source

内置分布

friendlydb-2.0.0-py2.py3-none-any.whl (10.1 kB 查看哈希

已上传 2 7