Skip to content

Instantly share code, notes, and snippets.

@GuilhermeHideki
Created March 29, 2016 12:01
Show Gist options
  • Save GuilhermeHideki/6a6185cb75d5399659cd to your computer and use it in GitHub Desktop.
Save GuilhermeHideki/6a6185cb75d5399659cd to your computer and use it in GitHub Desktop.
toying with OrientDB to build a genre graph from a YAML file
import pyorient
import yaml
import os, sys, codecs
def cp65001(name):
if name.lower()=='cp65001':
return codecs.lookup('utf-8')
codecs.register(cp65001)
os.environ["PYTHONIOENCODING"] = "cp65001"
c = pyorient.OrientDB("localhost", 2424) # host, port
c.connect("guilherme", "guilherme")
try:
c.db_drop("multimedia")
except:
pass
c.db_create("multimedia", pyorient.DB_TYPE_GRAPH, pyorient.STORAGE_TYPE_MEMORY)
c.db_open("multimedia", "guilherme", "guilherme")
def createV(cli, name, extends=u'V'):
cli.command("create class {} extends {}".format(name, extends))
def insert(client=c, cls='Genre', x=''):
c.record_create(11,{'@Genre': { 'name': x}})
def select(x):
x = repr(x.replace("'","\\'"))
return "select * from Genre where name={}".format(x)
def all():
createV(c, "Genre")
createV(c, "is_subgenre_of", "E")
genres = set()
relationships = set()
def relationship(x, y, r='is_subgenre_of'):
return "CREATE EDGE {}".format(r) + " from (" + select(x) + ") to (" +select(y)+ ")" if x else ''
def do(data, x=u'', depth=1):
if isinstance(data, list):
for ls in data:
# List of genres
if isinstance(ls, str):
#
# print("[INFO] list >> str")
#
# print("genre: {}".format(ls))
#
genre = ls
genres.add(genre)
genres.add(x)
# print(relationship(x, genre))
relationships.add(relationship(x, genre))
# List of dicts
elif isinstance(ls, dict):
#
# print("[INFO] list >> dict")
#
genres.add(x)
do(ls, x, ++depth)
else:
#
print("[INFO] ??????")
#
# do(ls, x, ++depth)
# Dict
elif isinstance(data, dict):
for key, val in data.items():
#
# print("[INFO] dict >> key val")
#
# print ("[INFO] " + str(depth) + ": " + str(key))
if isinstance(val, list):
depth += 1
for item in data:
if (relationship(x, item) != ''):
# print(relationship(x, item))
relationships.add(relationship(x, item))
genres.add(item)
do(val, x=key, depth=depth)
elif isinstance(data, str):
print(relationship(x, data))
with open("genres-tree.yaml", 'r', encoding='utf-8') as stream:
try:
a = yaml.load(stream)
do(a)
except yaml.YAMLError as exc:
print(exc)
def x():
# db.command("INSERT INTO Genre SET name = 'continental jazz'")
for item in genres:
insert(c, "Genre", item)
# print ("db.command(\"INSERT INTO Genre Set name = '{}'\")".format(item))
x()
for item in relationships:
print(item)
c.command(item)
if __name__ == "__main__":
all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment