Created
March 23, 2014 21:48
-
-
Save eddieantonio/9730403 to your computer and use it in GitHub Desktop.
Creates some entities/relations that can be imported using mongoimport.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import yaml | |
import json | |
from bson.objectid import ObjectId | |
from itertools import count | |
def parse_database(database): | |
entities = create_entities(database['entities']) | |
id_for = lambda name: entities[name]['_id'] | |
relations = create_relations(database['relations'], id_for) | |
return {'entities': entities.values(), 'relations': relations} | |
def create_relations(relations, id_for): | |
all_relations = (relations_for(name, partial_relations, id_for) | |
for name, partial_relations in relations.items()) | |
return [r for relations in all_relations for r in relations] | |
def relations_for(source_name, partial_relations, id_for): | |
rank = count() | |
source = id_for(source_name) | |
def create_relation(partial_relation): | |
relation, target_name = partial_relation | |
return { | |
'relation': relation, | |
'source': source, | |
'target': id_for(target_name), | |
'rank': next(rank), | |
'_id': new_id() | |
} | |
return (create_relation(pr) for pr in partial_relations.items()) | |
def create_entities(entities): | |
return {e['name']: to_mongo_entity(e) for e in entities} | |
def new_id(): | |
oid = ObjectId() | |
return {"$oid": str(oid)} | |
def to_mongo_entity(e): | |
return { | |
'_id': new_id(), | |
'name': e['name'], | |
'entity': { | |
'name': e['name'], | |
'keys': {}, | |
'types': [e.get('type', 'misc')], | |
'aliases': e.get('aliases', []), | |
} | |
} | |
def write_line_json(writer, sequence): | |
for row in sequence: | |
f.write(json.dumps(row)) | |
f.write("\n") | |
if __name__ == '__main__': | |
with open('database.yml') as yaml_file: | |
database = yaml.load(yaml_file.read()) | |
mongo_dump = parse_database(database) | |
with open('enty.json', 'wb') as f: | |
write_line_json(f, mongo_dump['entities']) | |
with open('rel.json', 'wb') as f: | |
write_line_json(f, mongo_dump['relations']) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
entities: | |
- name: Howard Moon | |
aliases: [Papa Moon] | |
type: person | |
- name: Old Gregg | |
aliases: [Gregg] | |
type: person | |
- name: Bailey's | |
- name: Curly Jefferson | |
type: person | |
- name: Home economics | |
- name: The Funk | |
type: person | |
- name: Bootsy Collins | |
type: person | |
- name: Funk shakes | |
- name: Parliament | |
type: organization | |
- name: Rick Wakeman | |
type: person | |
- name: The Bee Gees | |
type: organization | |
- name: George Clinton | |
type: person | |
- name: The Mothership | |
- name: Naan bread | |
type: person | |
relations: | |
Old Gregg: | |
loves: Howard Moon | |
has: The Funk | |
studied: Home economics | |
dealed with: Curly Jefferson | |
found: The Funk | |
Howard Moon: | |
playing love games with: Old Gregg | |
The Funk: | |
done with: Parliament | |
Bootsy Collins: | |
milked: The Funk | |
drank: Funk shakes | |
member of: Parliament | |
Rick Wakeman: | |
wants: The Funk | |
The Bee Gees: | |
wants: The Funk | |
Parliament: | |
travelling on: The Mothership | |
George Clinton: | |
member of: Parliament | |
kicked: The Funk | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment