Skip to content

Instantly share code, notes, and snippets.

@blippy
Created December 11, 2015 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blippy/e5769a102dd42be9b9c2 to your computer and use it in GitHub Desktop.
Save blippy/e5769a102dd42be9b9c2 to your computer and use it in GitHub Desktop.
Using pyDatalog to perform a data join
from pyDatalog import pyDatalog as pdl
# set up some basic terms
pdl.create_terms('F,G,Q,what,eat')
# F G
+what('pork', 'meat')
+what('lamb', 'meat')
+what('peas', 'veg')
+what('beans', 'veg')
# F Q
+eat('pork', 3)
+eat('lamb', 4)
+eat('peas', 2)
+eat('beans', 1)
# set up the relationships
pdl.create_terms('geat,sumg,qtys')
geat(G, Q) <= (what(F, G) & eat(F, Q))
(sumg[G] == sum_(Q, for_each = G)) <= geat(G, Q)
qtys(G, Q) <= (sumg[G] == Q)
# Now onto the crucial question: how much of each food group did I eat?
print(qtys(G, Q))
# If we want to manipulate the data in vanilla Python
result = qtys(G, Q).data
print(result)
@blippy
Copy link
Author

blippy commented Dec 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment