Beispiel #1
0
def test_can_cast_3_args():
    casted = rel("Alice", "KNOWS", "Bob")
    assert isinstance(casted, neo4j.Relationship)
    assert not casted.bound
    assert casted.start_node == neo4j.Node("Alice")
    assert casted.type == "KNOWS"
    assert casted.end_node == neo4j.Node("Bob")
Beispiel #2
0
def test_can_cast_3_tuple():
    casted = rel(("Alice", "KNOWS", "Bob"))
    assert isinstance(casted, neo4j.Relationship)
    assert casted.is_abstract
    assert casted.start_node == neo4j.Node("Alice")
    assert casted.type == "KNOWS"
    assert casted.end_node == neo4j.Node("Bob")
Beispiel #3
0
def test_can_cast_3_tuple_with_unbound_rel():
    casted = rel(("Alice", ("KNOWS", {"since": 1999}), "Bob"))
    assert isinstance(casted, neo4j.Relationship)
    assert casted.is_abstract
    assert casted.start_node == neo4j.Node("Alice")
    assert casted.type == "KNOWS"
    assert casted.end_node == neo4j.Node("Bob")
    assert casted["since"] == 1999
Beispiel #4
0
def test_can_cast_4_args():
    casted = rel("Alice", "KNOWS", "Bob", {"since": 1999})
    assert isinstance(casted, neo4j.Relationship)
    assert casted.is_abstract
    assert casted.start_node == neo4j.Node("Alice")
    assert casted.type == "KNOWS"
    assert casted.end_node == neo4j.Node("Bob")
    assert casted["since"] == 1999
Beispiel #5
0
def test_can_cast_4_tuple():
    casted = rel(("Alice", "KNOWS", "Bob", {"since": 1999}))
    assert isinstance(casted, neo4j.Relationship)
    assert not casted.bound
    assert casted.start_node == neo4j.Node("Alice")
    assert casted.type == "KNOWS"
    assert casted.end_node == neo4j.Node("Bob")
    assert casted["since"] == 1999
Beispiel #6
0
def test_can_cast_3_args_with_mid_tuple_and_props():
    casted = rel("Alice", ("KNOWS", {"since": 1999}), "Bob", foo="bar")
    assert isinstance(casted, neo4j.Relationship)
    assert casted.is_abstract
    assert casted.start_node == neo4j.Node("Alice")
    assert casted.type == "KNOWS"
    assert casted.end_node == neo4j.Node("Bob")
    assert casted["since"] == 1999
    assert casted["foo"] == "bar"
Beispiel #7
0
def test_can_cast_4_args_and_props():
    casted = rel("Alice", "KNOWS", "Bob", {"since": 1999}, foo="bar")
    assert isinstance(casted, neo4j.Relationship)
    assert not casted.bound
    assert casted.start_node == neo4j.Node("Alice")
    assert casted.type == "KNOWS"
    assert casted.end_node == neo4j.Node("Bob")
    assert casted["since"] == 1999
    assert casted["foo"] == "bar"
Beispiel #8
0
def find_by_label(user_id, label):
    nodes = []
    for n in [neo4j.Node(path) for path in db().find(label)]:
        nn = n.get_properties()
        nn['id'] = n._id
        nn['likes'] = len(list(db().match(rel_type='like', end_node=n)))
        nn['is_liked'] = len(
            list(db().match(rel_type='like',
                            end_node=n,
                            start_node=user_node(user_id))))
        nodes.append(nn)
    return nodes
Beispiel #9
0
    def writePairsToDatabase(self, pairs, tagName):
        
        batch = neo4j.WriteBatch(self.dbInterface.j.graphDb)
        graphDbURL = self.dbInterface.j.getGraphDbURL()
        if graphDbURL[-1] == '/': graphDbURL = graphDbURL[:-1]

        for (nodeId, tagVal) in pairs:
                    
            nodeURL = graphDbURL + '/node/' + str(nodeId)
            node = neo4j.Node(nodeURL)
            batch.set_property(node, tagName , tagVal)
        
        batch.submit()
Beispiel #10
0
def user_node(id):
    global usr_node
    if usr_node:
        return usr_node
    else:
        l = [
            neo4j.Node(path) for path in db().find(
                "User", property_key="uid", property_value=id)
        ]
        if len(l):
            usr_node = l[0]
            if len(l) > 1:
                for k in l[1:]:
                    k.delete()
        else:
            usr_node, = db().create(node({"uid": id}))
            usr_node.add_labels("User")
    return usr_node
Beispiel #11
0
def seed():
    db().clear()

    def ss(arr, label, parents=None):
        i = 0
        for a in arr:
            n, = db().create(node(a))
            n.add_labels(label)
            if parents:
                db().create(rel((n, "realate_to", parents[i])))
                i += 1

    countries = [
        {
            'code': 'UA',
            'name': 'Ukraine'
        },
        {
            'code': 'AT',
            'name': 'Atlantida'
        },
        {
            'code': 'UK',
            'name': 'Kuda'
        },
    ]
    ss(countries, "Country")

    universities = [
        {
            'code': 'KPI',
            'name': 'Kiev Pol Inst'
        },
        {
            'code': 'MTI',
            'name': 'Mosc Tech Inst'
        },
        {
            'code': 'LPF',
            'name': 'Lol pol F**k'
        },
    ]
    ss(universities, "University",
       [neo4j.Node(path) for path in db().find("Country")])
Beispiel #12
0
from py2neo import neo4j, node, rel

graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")

die_hard = graph_db.create(
    node(name="Bruce Willis"),
    node(name="John McClane"),
    node(name="Alan Rickman"),
    node(name="Hans Gruber"),
    node(name="Nakatomi Plaza"),
    rel(0, "PLAYS", 1),
    rel(2, "PLAYS", 3),
    rel(1, "VISITS", 4),
    rel(3, "STEALS_FROM", 4),
    rel(1, "KILLS", 3),
)

n = node(neo4j.Node("http://localhost:7474/db/data/node/1"))
# print type(n)
print n['name']

# http://localhost:7474/webadmin/
# Start the Neo4j server with bin/neo4j start from a terminal.
Beispiel #13
0
def by_id(id):
    return node(neo4j.Node("http://localhost:7474/db/data/node/%d" % int(id)))