コード例 #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
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))
コード例 #3
0
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()

pd = {
    'cnic': '37405-4564665-7',
    'dob': '2016-09-12',
    'name': 'Kashif Iftikhar'
}
data, errors = Person._Schema().load(pd)
new_person = Person._load(pd)

new_col = Collection('new_collection')
コード例 #4
0
ファイル: university_graph.py プロジェクト: xyder/arango-orm
    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")
        uni_graph.expand(bruce, depth=1, direction='any')

        print('bruce._relations')
        pp(bruce._relations)

        students_of_bruce = [r._next for r in bruce._relations['teacher']]
        print('Students of Bruce')
        for s in students_of_bruce:
            pp(s)
コード例 #5
0
ファイル: test_extra_fields.py プロジェクト: xyder/arango-orm
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)

db.add(Setting(_key='boolean', value=True))
db.add(Setting(_key='integer', value=100))
db.add(Setting(_key='float', value=12.22))
db.add(Setting(_key='list', value=[1, 2, 3]))
db.add(Setting(_key='dict', value={'a': 1, 'b': 2}))
コード例 #6
0
    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)

c3 = db.query(Car).filter_by(make='Acme').first()

print(c1.owner)
print(c1.owner.name)
print(c2.owner.name)
print(c3.owner.name)

print(p.cars)
print(p.cars[0].make)