在redis中操作有向图的python库

pygraph_redis的Python项目详细描述


pygraph_redis

在redis中操作有向图的简单python库

Travis CINumber of PyPI downloads

许可证

pygraph_redis是在麻省理工学院的公共许可下发布的

说明

pygraph_redis是一个简单的库,用于操作redis数据库中的有向图。

在这个库中,图是一组节点,每个节点都知道它的前辈 以及它的继任者。节点可以存储一些属性(字符串或字符串集)。

依赖性

pygraph_redis依赖于redisredis-py

对于事务的原子性,它需要lua脚本支持(redis py>;=2.7.0和redis>;=2.6.0),但它提供了一种传统模式,而对旧的redis和redis py没有原子性。

写原子性

使用正确的版本,pygraph_redis在添加或删除节点时提供事务的原子性。

安装

要安装:

$ python setup.py install

or

$ pip install pygraph_redis

如何使用

首先你需要一个redis数据库,由你来安装它。

图书馆本身很简单:

备忘单

#              initialization#       arg1      |    arg2    |     arg3#--------------------------------------------# redis connexion | graph_name |    logger#    redis obj    |  unicode   |  logger objmygraph1=Directed_graph(r_server,u'mygraph1',logger)#optional args:#   arg4    |    arg5#-----------------------# separator | has_root# unicode   |   boolmygraph1=Directed_graph(r_server,u'mygraph1',logger,u'mysep',True))
#                    create or add elements to a node#    arg1   |     arg2     |     arg3     |             arg4#---------------------------------------------------------------------------# node name |  successors  | predecessors |           attributs#  unicode  | unicode list | unicode list |      dictionnary of unicode#           |              |              | or set of unicode (key: unicode)mygraph1.write_on_node(u'm1',[u's2'],[u'p1'],{u'a3':set([u'69']),u'a2':u'42'})
#             delete elements from a node#    arg1   |     arg2     |     arg3     |      arg4#----------------------------------------------------------# node name |  successors  | predecessors | attributs names#  unicode  | unicode list | unicode list | list of unicodemygraph1.write_off_node(u'm1',[u's2'],[u'p1'],[u'attr3',u'attr2']
# delete a node#     arg1#--------------#  node name#   unicodemygraph1.remove_node(u'm1')
# get attributs list#     arg1#--------------#  node name#   unicodemygraph1.get_attributs_list(u'm1')
# get an attribut#     arg1     |     arg2#--------------|--------------#  node name   | attribut name#   unicode    |    unicodemygraph1.get_attribut(u'm1',u'a2')
# get an attribut length#     arg1     |     arg2#--------------|--------------#  node name   | attribut name#   unicode    |    unicodemygraph1.get_attribut_len(u'm1',u'a2')
# get successors#     arg1#--------------#  node name#   unicodemygraph1.get_successors(u'm1')
# get predecessors#     arg1#--------------#  node name#   unicodemygraph1.get_predecessors(u'm1')

初始化

创建“有向图”的实例:

#importing directed_graphfrompygraph_redis.directed_graphimportDirected_graphimportredis#creating a basic loggerimportlogginglogging.basicConfig(format=u'%(message)s')logger=logging.getLogger(u'redis')logger.parent.setLevel(logging.DEBUG)#creating the redis connexionr_server=redis.Redis("localhost")#creating the graph objectmygraph1=Directed_graph(r_server,u'mygraph1',logger)#creating the graph object with a different separatormygraph2=Directed_graph(r_server,u'mygraph2',logger,separator=u'mysep')#creating the graph object with a "root" (improper name, I know)mygraph2=Directed_graph(r_server,u'mygraph2',logger,has_root=True)#"has_root = True" ensures that every node has a predecessor#if enabled, a node has at least root as a predecessor,#but if it has any other predecessor it doesn't have root as predecessor

节点操作

节点创建:

#add node 'm1' to 'mygraph1' with:#successors: 's1' and 's2'#predecessors: 'p1' and 'p2'#attributs:#   * 'attr1': set([u'51',u'69'])#   * 'attr2': '42'mygraph1.write_on_node(u'm1',[u's1',u's2'],[u'p1',u'p2'],{u'attr1':set([u'51',u'69']),u'attr2':u'42'})

关于后续节点前置节点,如果节点已声明为 在其后继项中,不必在节点后继项集中添加此后继项。 与前辈相同。

示例:

mygraph1.write_on_node(u'pred',[u'succ'],[],{})
mygraph1.write_on_node(u'succ',[],[],{})

得到的结果相同:

mygraph1.write_on_node(u'pred',[u'succ'],[],{})
mygraph1.write_on_node(u'succ',[],[u'pred'],{})

节点版本:

#add new elements or edit existing elements of a node#it's exactly the same function as beforemygraph1.write_on_node(u'm1',[u's4'],[],{u'attr3':set([u'16',u'32',u'64']),u'attr2':u'5150'})#remove some elements of a node (successors, predecessors, attributs)mygraph1.write_off_node(u"m1",[u"s1"],[u"p2"],[u'attr2'])#completely delete a nodemygraph1.remove_node(u'm1')

节点属性操作

要操作节点的属性:

#create the node 'm2'mygraph1.write_on_node(u'm2',[u's1',u's2'],[u'p1',u'p2'],{u'attr1':set([u'51',u'69']),u'attr2':u'42'})#get the set of attribut namesset_of_attributs=mygraph1.get_attributs_list(u'm2')printset_of_attributs#get a specific attributattr2=mygraph1.get_attribut(u'm2',u'attr2')printattr2#get a specific attribut length# 1 if it's a string# cardinal of set if it's a set# 0 if attribut doesn't existsattr2=mygraph1.get_attribut_len(u'm2',u'attr2')printattr2

图形导航

要在图形中导航,您有两个功能:

#get the predecessors of 'm2'predecessors=mygraph1.get_predecessors(u'm2')printpredecessors#get the successors of 'm2'successors=mygraph1.get_successors(u'm2')

如果您有has\u根标志启用:

#get the "root" nameroot=mygraph1.get_root_name()printroot#get the successors of 'root'successors=mygraph1.get_successors(root)printsuccessors

关于redis键

redis密钥格式:

<graph name><sep><node_name><sep><variable_name>[<sep><other>]*

<graph name>: name of the graph
<sep>: the key fields separator
     (this string should not be in node_name or variable_name,
      otherwise, there is a redis key collision possibility)
<node_name>: name of the node
<variable_name>: name of the variable
[<sep><other>]: optional extension

为了避免密钥冲突,您必须仔细选择密钥分隔符, 它不能包含在任何节点名或节点属性名中(可能发生redis密钥冲突)。

关于日志

这个库提供了很多日志,主要是调试、一些信息(例如:传统模式)、一些警告(例如:可能的密钥冲突)

Bitdeli badge

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

推荐PyPI第三方库


热门话题
java我可以在更新JDK后删除旧文件夹吗   java如何在POSTMAN中选择formdata时使用Reformation2发布数据   HttpURLConnection上的java FileNotFoundException。getInputStream   java我应该使用try/catch检查缺少的输入吗?   java这个枚举是否违反了单一责任原则?   java Oracle JDBC欧洲字符   在java中,在不将int转换为String的情况下,是否可以将int添加到stringarraylist中   如何使用Jersey(Java)通过RESTAPI上传csv文件?   JPanel中的java菜单   如何在java控制台输出中打印带下划线的字符串   java日期转换无法正常工作   java如何用try/catch最优雅地围绕代码   客户端在浏览器中键入的jsp日期不保存在java POJO中(客户端发送的请求在语法上不正确)   在java中使用BufferedOutputStream打印整数   如何在ubuntu中安装java软件的依赖项。?   java有没有一种方法可以让NetBeans在单独的一行中自动创建括号?   java如何使用真正的浮动操作按钮(FAB)扩展?   10000毫秒后,java无法从/192.168.0.102(端口47108)连接到/192.168.0.101(端口443)   java为什么要使用notify?