一个小型快速跟踪/关注者数据库。

friendlydb的Python项目详细描述


friendlydb是一个用 Python。它可以直接从python代码中使用,也可以通过http 使用小型Web API。

friendlydb并不是一个完整的用户系统;它应该用来增强 现有的跟踪关系的系统。

警告

从v2.0.0开始,friendlydb是not向后兼容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脚本。

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

  • 在10000个用户之间创建了1000000个关系: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许可证。

author:Daniel Lindsley
version:2.0.0
date:2013-01-17

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
基于属性值的JAXB java解析XML文件   windows xp Java Web启动多个快捷方式   java我不能使用步进计数器传感器   java如何从Activator获取视图?   java复制二进制数据   java For JDBC准备的语句,当SQL语句中提供实际值时,如何确定变量的位置   java如何解析通过REST中的选择框获得的字符串列表   在Java中使用什么模式来解密来自iPhone的RSA消息?   java我想更新字符串数据,其中包含双引号和分号   java Mongoddb在插入文档后返回\u id   java无法从服务器上的MySql数据库获取响应   java如何从服务器获取会话cookie并将其设置到api头中?   java检查对象是否包含空值   Spring项目中的Heroku部署问题   java如何在spring中只返回特定字段   java对OOP和过程编程的这种肯定正确吗?   java如何循环文件中的问题   java RxJava自定义事件订阅服务器   POST中的java Spring启动API Base64编码错误