1. Pierre Carbonnelle
  2. pyDatalog
  3. Issues
Issue #9 resolved

create_terms for dynamic queries

Pierre Carbonnelle
repo owner created an issue

A pyDatalog user, Vesa, reported to me that the following program results in an error:

from pyDatalog import pyDatalog
from pyDatalog.pyDatalog import load, ask,create_terms
from datetime import datetime

def todate(s):
    return(datetime.strptime(s, '"%Y-%m-%dT%H:%M:%S.%fZ"'))

load('''a(Y) <= (X == '"2014-08-11T07:39:59.788Z"') & (Y == todate(X))''')

Indeed, terms created by create_terms are for in-line clauses and queries, and are currently not recognized in dynamic queries ("ask").

A way around that is to use a predicate resolver written in python. See the 4th section in advanced topics.

You could then write :

def todate2(S,D):
    yield (S.id, datetime.strptime(S.id, "%Y-%m-%dT%H:%M:%S.%fZ"))

print(pyDatalog.ask('todate("2014-08-11T07:39:59.788Z", Y)'))
#>> {(datetime.datetime(2014, 8, 11, 7, 39, 59, 788000),)}