Commits

Pierre Carbonnelle committed f880bc0

add format_ function

  • Participants
  • Parent commits 9c648b7

Comments (0)

Files changed (3)

pyDatalog/examples/test.py

         assert ask((X==2) & (Y==2/X)) == set([(2,1)])
         assert ask((X==2) & (Y==2//X)) == set([(2,1)])
         
+        assert ask((Y==5) & (X==format_('5 is {}', Y))) == set([(5, '5 is 5')])
+        assert ask(X==format_('5 is {} {}', 5, 'test'))  == set([('5 is 5 test',)])
+        
     """ Conjunctive queries                                             """
 
     @pyDatalog.program()
         assert (ask(plus[plus[1,2]+1, 2+plus[2,3]] == plus[4, plus[2,5]])) == set([()])
         assert (ask(plus[plus[1,2]+1, 2+plus[2,3]] <  plus[5, plus[2,5]])) == set([()])
         assert (ask(plus[plus[1,2]+1, 2+plus[2,3]] <  plus[1, plus[2,5]])) == None
-        
+                
     @pyDatalog.program()
     def function_comparison(): 
         assert ask(f[X]==Y) == set([('a', 'c')])

pyDatalog/pyEngine.py

                 return Interned.of(lhs.id / rhs.id)
             elif self.operator == '//':
                 return Interned.of(lhs.id // rhs.id)
+            elif self.operator == '%':
+                return Interned.of(lhs.id.format(*(rhs.id)))
             elif isinstance(self.operator, type(lambda: None)):
                 return Interned.of(self.operator(*(rhs.id)))
             assert False # dead code

pyDatalog/pyParser.py

                 return len(args[0]) 
         elif self._pyD_name == 'range_':
             return Operation(None, '.', args[0])
+        elif self._pyD_name == 'format_':
+            return Operation(args[0], '%', args[1:])
         else: # create a literal
             literal = Literal.make(self._pyD_name, tuple(args))
             return literal