Skip to content

Instantly share code, notes, and snippets.

@Qalthos
Created August 1, 2012 21:22
Show Gist options
  • Save Qalthos/3230859 to your computer and use it in GitHub Desktop.
Save Qalthos/3230859 to your computer and use it in GitHub Desktop.
Example code for using the newly emancipated Knowledge module
#!/usr/bin/env python2
from sqlalchemy import create_engine
from knowledge.model import Fact, Entity, DBSession, init_model, metadata
def inject_knowledge():
knowledge = DBSession
monster = Entity(u'Monster')
fairy = Entity(u'Fairy')
rjbean = Entity(u'rjbean')
monster[u'color'] = u'Green'
monster[u'name'] = u'Lotharrr'
fairy[u'flies'] = True
fairy[u'name'] = u'Bell'
rjbean[u'name'] = u'ralph'
rjbean[u'flies'] = False
rjbean[u'hacks'] = True
knowledge.add(monster)
knowledge.add(fairy)
knowledge.add(rjbean)
knowledge.commit()
def crawl():
# Get the Knowledge Session
knowledge = DBSession
# Make the base Query, all Entities
knowledge_query = knowledge.query(Entity).all()
# Print out each Entity, and the values of each of their facts
for entities in knowledge_query:
print entities, entities.facts.values()
engine = create_engine('sqlite:///knowledge.db')
init_model(engine)
metadata.create_all(engine)
inject_knowledge()
crawl()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment