コード例 #1
0
from arango import ArangoClient
from arango_orm.database import Database
from arango_orm.collections import Collection
from tests.data import (UniversityGraph, Student, Teacher, Subject, SpecializesIn, Area,
                   teachers_data, students_data, subjects_data, specializations_data, areas_data)

client = ArangoClient(username='******', password='******')
test_db = client.db('test')

db = Database(test_db)

uni_graph = UniversityGraph(connection=db)

obj = uni_graph.aql("FOR v, e, p IN 1..2 INBOUND 'areas/Gotham' GRAPH 'university_graph'  RETURN p")

bruce = db.query(Teacher).by_key("T001")
amanda = db.query(Teacher).by_key("T003")

traversal_results = db.graph('university_graph').traverse(start_vertex='teachers/T001', direction='any',
                                       vertex_uniqueness='path', min_depth=1, max_depth=1)

uni_graph.expand(amanda, depth=3)

uni_graph.expand(bruce, depth=2)

bruce._relations['specializes_in'][0]._object_to._relations['teaches'][0]._object_from.name
bruce._relations['specializes_in'][0]._next._relations['teaches'][0]._next.name


###############################3
from datetime import date
コード例 #2
0
ファイル: __init__.py プロジェクト: xyder/arango-orm
    def _get_db_obj(cls):

        test_db = cls.get_db()
        db = Database(test_db)

        return db
コード例 #3
0
def initGraph():
    client = ArangoClient(protocol='http',
                          host=config.DATABASE_CONFIG['host'],
                          port=config.DATABASE_CONFIG['port'])
    dev_db = client.db(config.DATABASE_CONFIG['database'],
                       username=config.DATABASE_CONFIG['username'],
                       password=config.DATABASE_CONFIG['password'],
                       verify=config.DATABASE_CONFIG['verify'])
    db = Database(dev_db)

    uni_graph = UniversityGraph(connection=db)
    db.create_graph(uni_graph)

    students_data = [
        Student(_key='S1001', name='John Wayne', age=30),
        Student(_key='S1002', name='Lilly Parker', age=22),
        Student(_key='S1003', name='Cassandra Nix', age=25),
        Student(_key='S1004', name='Peter Parker', age=20)
    ]

    teachers_data = [
        Teacher(_key='T001', name='Bruce Wayne'),
        Teacher(_key='T002', name='Barry Allen'),
        Teacher(_key='T003', name='Amanda Waller')
    ]

    subjects_data = [
        Subject(_key='ITP101',
                name='Introduction to Programming',
                credit_hours=4,
                has_labs=True),
        Subject(_key='CS102',
                name='Computer History',
                credit_hours=3,
                has_labs=False),
        Subject(_key='CSOOP02',
                name='Object Oriented Programming',
                credit_hours=3,
                has_labs=True),
    ]

    areas_data = [
        Area(_key="Gotham"),
        Area(_key="Metropolis"),
        Area(_key="StarCity")
    ]

    for s in students_data:
        db.add(s)

    for t in teachers_data:
        db.add(t)

    for s in subjects_data:
        db.add(s)

    for a in areas_data:
        db.add(a)

    db.add(
        SpecializesIn(_from="teachers/T001",
                      _to="subjects/ITP101",
                      expertise_level="medium"))

    gotham = db.query(Area).by_key("Gotham")
    metropolis = db.query(Area).by_key("Metropolis")
    star_city = db.query(Area).by_key("StarCity")

    john_wayne = db.query(Student).by_key("S1001")
    lilly_parker = db.query(Student).by_key("S1002")
    cassandra_nix = db.query(Student).by_key("S1003")
    peter_parker = db.query(Student).by_key("S1004")

    intro_to_prog = db.query(Subject).by_key("ITP101")
    comp_history = db.query(Subject).by_key("CS102")
    oop = db.query(Subject).by_key("CSOOP02")

    barry_allen = db.query(Teacher).by_key("T002")
    bruce_wayne = db.query(Teacher).by_key("T001")
    amanda_waller = db.query(Teacher).by_key("T003")

    db.add(uni_graph.relation(peter_parker, Relation("studies"), oop))
    db.add(uni_graph.relation(peter_parker, Relation("studies"),
                              intro_to_prog))
    db.add(uni_graph.relation(john_wayne, Relation("studies"), oop))
    db.add(uni_graph.relation(john_wayne, Relation("studies"), comp_history))
    db.add(uni_graph.relation(lilly_parker, Relation("studies"),
                              intro_to_prog))
    db.add(uni_graph.relation(lilly_parker, Relation("studies"), comp_history))
    db.add(uni_graph.relation(cassandra_nix, Relation("studies"), oop))
    db.add(
        uni_graph.relation(cassandra_nix, Relation("studies"), intro_to_prog))

    db.add(
        uni_graph.relation(barry_allen,
                           SpecializesIn(expertise_level="expert"), oop))
    db.add(
        uni_graph.relation(barry_allen,
                           SpecializesIn(expertise_level="expert"),
                           intro_to_prog))
    db.add(
        uni_graph.relation(bruce_wayne,
                           SpecializesIn(expertise_level="medium"), oop))
    db.add(
        uni_graph.relation(bruce_wayne,
                           SpecializesIn(expertise_level="expert"),
                           comp_history))
    db.add(
        uni_graph.relation(amanda_waller,
                           SpecializesIn(expertise_level="basic"),
                           intro_to_prog))
    db.add(
        uni_graph.relation(amanda_waller,
                           SpecializesIn(expertise_level="medium"),
                           comp_history))

    db.add(uni_graph.relation(bruce_wayne, Relation("teaches"), oop))
    db.add(uni_graph.relation(barry_allen, Relation("teaches"), intro_to_prog))
    db.add(uni_graph.relation(amanda_waller, Relation("teaches"),
                              comp_history))

    db.add(uni_graph.relation(bruce_wayne, Relation("resides_in"), gotham))
    db.add(uni_graph.relation(barry_allen, Relation("resides_in"), star_city))
    db.add(
        uni_graph.relation(amanda_waller, Relation("resides_in"), metropolis))
    db.add(uni_graph.relation(john_wayne, Relation("resides_in"), gotham))
    db.add(uni_graph.relation(lilly_parker, Relation("resides_in"),
                              metropolis))
    db.add(uni_graph.relation(cassandra_nix, Relation("resides_in"),
                              star_city))
    db.add(uni_graph.relation(peter_parker, Relation("resides_in"),
                              metropolis))
コード例 #4
0
from datetime import date
from arango import ArangoClient
from arango_orm.database import Database
from arango_orm.collections import Collection
from marshmallow.fields import List, String, UUID, Integer, Boolean, DateTime, Date
from marshmallow import (Schema, pre_load, pre_dump, post_load,
                         validates_schema, validates, fields, ValidationError)

client = ArangoClient(username='******', password='******')
test_db = client.db('test')

db = Database(test_db)


class Person(Collection):
    __collection__ = 'persons'

    class _Schema(Schema):
        cnic = String(required=True)
        name = String(required=True, allow_none=False)
        dob = Date()

    _key_field = 'cnic'


db.query(Person).count()
db.query(Person).all()

p = Person(name='test', cnic='12312', dob=date(year=2016, month=9, day=12))
db.add(p)
db.query(Person).count()
コード例 #5
0
ファイル: university_graph.py プロジェクト: xyder/arango-orm
    username = '******'
    password = ''

    client = ArangoClient(username=username, password=password, verify=True)

    # try:
    #     client.delete_database(name=db_name)
    # except:
    #     pass

    try:
        test_db = client.create_database(name=db_name)
    except:
        test_db = client.db(db_name)

    db = Database(test_db)

    uni_graph = UniversityGraph(connection=db)

    if len(sys.argv) > 1:
        if 'test_data_create' == sys.argv[1]:
            db.create_graph(uni_graph)
            data_create()
        elif 'drop_graph' == sys.argv[1]:
            db.drop_graph(uni_graph)
        elif 'delete_database' == sys.argv[1]:
            client.delete_database(name=db_name)
    else:
        print(usage)

        bruce = db.query(Teacher).by_key("T001")
コード例 #6
0
ファイル: test_extra_fields.py プロジェクト: xyder/arango-orm
from arango import ArangoClient
from arango_orm.database import Database
from arango_orm.collections import Collection
from marshmallow.fields import List, String, UUID, Integer, Boolean, DateTime, Date
from marshmallow import (Schema, pre_load, pre_dump, post_load,
                         validates_schema, validates, fields, ValidationError)

client = ArangoClient(username='******', password='******')
test_db = client.db('test')

db = Database(test_db)


class Setting(Collection):
    __collection__ = 'settings'

    # will have an extra field named value

    class _Schema(Schema):
        _key = String(required=True)


s = db.query(Setting).by_key('project_name')
s._dump()

s1 = Setting(_key='project_name', value='abc')
s1.value
s1._dump()

db.add(s1)
コード例 #7
0
    __collection__ = 'cars'
    _allow_extra_fields = True

    make = String(required=True)
    model = String(required=True)
    year = Integer(required=True)
    owner_key = String()

    owner = relationship(Person, 'owner_key', cache=False)

    def __str__(self):
        return "<Car({} - {} - {})>".format(self.make, self.model, self.year)


client = ArangoClient(username='******', password='******')
db = Database(client.db('test'))

p = Person(_key='kashif', name='Kashif Iftikhar', dob=datetime.today())
db.add(p)
p2 = Person(_key='azeen', name='Azeen Kashif', dob=datetime.today())
db.add(p2)

c1 = Car(make='Honda', model='Civic', year=1984, owner_key='kashif')
db.add(c1)

c2 = Car(make='Mitsubishi', model='Lancer', year=2005, owner_key='kashif')
db.add(c2)

c3 = Car(make='Acme', model='Toy Racer', year=2016, owner_key='azeen')
db.add(c3)