how can I refactor this py2neo v4 code to use neo4j 3.4 temporal data types?

Viewed 203

I'm stuck trying to add a date_accepted property to an upload node that represents a scientific paper. Previously, I would have just added a timetree node. However, py2neo v4 no longer supports GregorianCalendar (shame). How would I convert this code snippet to use one of the new temporal data types? I've looked at the docs & online but I'm not yet savvy enough I'm afraid.

from datetime import date, datetime # ??? how to use this...

def getAccepted(year_accepted, month, day):
    with open('/home/pmy/pdf/id.txt') as f:
        id = f.read()
        matcher = NodeMatcher(graph)
        upload = matcher.match("Upload", id = id).first()

        a = year_accepted+month+day
        d = datetime.strptime(a, '%Y%m%d').strftime('%Y-%m-%d') 
        # >>> HOW TO CONVERT d TO A TEMPORAL DATA TYPE HERE? <<<            
        try:
            graph.merge(upload)
            upload['accepted_date']=d
            graph.push(upload)

        except IndexError as e:
            print("type error: " + str(e))
        pass
    return 0

This works but it pushes the datetime string whereas I want to push a neotime temporal date...

It is possible to insert the datetime variable d above into something like this query below, which also works, but I'm winging this & suspect there's a better way...

query='''UNWIND [date({param})] AS date RETURN date'''
result=graph.run(query, param=d).data()
print(result)

which returns

[{'date': neotime.Date(2010, 10, 23)}]

So I could maybe extract the value & push that to the graph? Is this what the developers intended? The Docs are terse and aimed at proper programmers so IDK

Maybe

 accepted=result[0].get('date')  # <class 'neotime.Date'>

& push that to the graph perhaps?

The py2neo v4 neotime temporal types are very recent & there is not much documentation, or basic tutorials to adapt afaik. Hence this long-winded post. Anyone out there with experience care to comment?

1 Answers

Another user posted a similar question here: https://stackoverflow.com/a/61989193/13593049

Essentially, you need to use the neotime package for your dates and times if you want to use Neo4j data types in your graph. (Documentation)

neotime also has functionality to convert neotime objects into datetime objects.

import neotime

date_accepted = neotime.Date(2020, 05, 25)

print(date_accepted.to_native())
# # datetime.date(2020, 5, 25)
Related