I am following the Nicole White Neo4j Blog tutorial using the py2neo V3, but I am using the V.2020.1.1 instead.
I would like to use the function GregorianCalendar
, but as my code runs
from py2neo.ext.calendar import GregorianCalendar
# the way to import GregorianCalendar in py2neo V3
I get
ModuleNotFoundError: No module named 'py2neo.ext'
So I have been looking for GregorianCalendar
in py2neo V.2020.1.1 documentation, but I can't find it.
Does the function exist in this version?
If not, is there a similar one? Is it possible to get the same results with some workaround?
Since the py2neo.ext.calendar module no longer exist in py2neo V. 2020.1,
I implemented myself a custom gregorian_calendar
function.
gregorian_calendar(graph, time1=some_time, node1=some_node)
Given the desired date and graph as input, it is useful to create a chain of
(: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' })
If time is not passed as parameter, the function uses the time resulting from datetime.now()
to determine this year, month and day.
if some_node
is also passed as input, the function also creates (merge
s) the branch
(: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)