py2neo v2020.1 - ModuleNotFoundError: No module named 'py2neo.ext'

py2neo v2020.1 - ModuleNotFoundError: No module named 'py2neo.ext'

我正在使用 py2neo V3 关注 Nicole White Neo4j 博客教程,但我使用的是 V.2020.1.1。

我想使用函数 GregorianCalendar,但是当我的代码运行时

from py2neo.ext.calendar import GregorianCalendar 
# the way to import GregorianCalendar in py2neo V3 

我明白了

ModuleNotFoundError: No module named 'py2neo.ext'

所以我一直在py2neo V.2020.1.1 documentation中寻找GregorianCalendar,但是找不到。
这个版本有这个功能吗?

如果没有,有类似的吗?是否可以通过一些变通方法获得相同的结果?

此模块已不存在,并且已经有一段时间没有完成了。在最近的版本中没有等效的时间树功能,尽管 Neo4j 现在支持原生 date/time 类型,但当时还没有。

由于py2neo V.2020.1中不再存在py2neo.ext.calendar模块,
我自己实现了一个自定义 gregorian_calendar 函数。

gregorian_calendar(graph, time1=some_time, node1=some_node)

给定所需的日期和图形作为输入,创建一个

链是很有用的
(:Calendar { calendar_type: 'Gregorian' })-[:YEAR]->
(:Year { year: 'that year', key: 'that year' })-[:MONTH]->
(:Month { month: 'that month (in number)', key: 'that month-year' })-[:DAY]->
(:Day { day: 'that day (in number)', key: 'that day-month-year' })

如果时间未作为参数传递,函数将使用 datetime.now() 得到的时间来确定今年、月和日。

如果 some_node 也作为输入传递,该函数还会创建 (merges) 分支

(:Day { day: 'that day (in number)', key: 'that day-month-year' })-[:ON]->(:some_node)

from py2neo import Graph, Node, Relationship
from datetime import datetime


# equivalente del gregorian calendar
def gregorian_calendar(graph, time1=None, node1=None):

    if time1 is None:
        time1 = datetime.now()

    # gregorian calendar node
    gregorian_node = Node("Calendar", calendar_type="Gregorian")
    gregorian_node.__primarylabel__ = list(gregorian_node.labels)[0]
    gregorian_node.__primarykey__ = "calendar_type"
    graph.merge(gregorian_node)

    # year node
    that_year_node = Node("Year", year=time1.year, key=time1.strftime("%Y"))
    that_year_node.__primarylabel__ = list(that_year_node.labels)[0]
    that_year_node.__primarykey__ = "year"
    graph.merge(that_year_node)

    # calendar has year
    rel = Relationship(gregorian_node, "YEAR", that_year_node)
    graph.merge(rel)

    # month node
    that_month_node = Node("Month", month=time1.month, key=time1.strftime("%m-%Y"))
    that_month_node.__primarylabel__ = list(that_month_node.labels)[0]
    that_month_node.__primarykey__ = "month"
    graph.merge(that_month_node)

    # year has month
    rel = Relationship(that_year_node, "MONTH", that_month_node)
    graph.merge(rel)

    # day node
    that_day_node = Node("Day", day=time1.day, key=time1.strftime("%d-%m-%Y"))
    that_day_node.__primarylabel__ = list(that_day_node.labels)[0]
    that_day_node.__primarykey__ = "day"
    graph.merge(that_day_node)

    # month has day
    rel = Relationship(that_month_node, "DAY", that_day_node)
    graph.merge(rel)

    # post was published on (gregorian) day
    if node1 is not None:
        rel = Relationship(node1, "ON", that_day_node)
        graph.create(rel)