在Pypher中使用Python自动分配函数

时间:2019-02-22 21:45:04

标签: python function neo4j cypher

我正在制作一个Python脚本,该脚本将从.JSON中获取输入,并使用Pypher自动创建Cypher脚本。这是.JSON输入文件的示例:

{
    "nodes": [
        {"id": "n1", "type": "AAAA"},
        {"id": "n2", "type": "BBBB"},
        {"id": "n3", "type": "CCCC"}
    ],
    "edges": [
        {"id": "e1", "from": "n2", "to": "n1", "type": "REL1"},
        {"id": "e2", "from": "n2", "to": "n3", "type": "REL2"}
    ],
    "filters": [
        {"id": "f1", "on": "n1", "attribute": "ATT1", "dtype": "int", "operation": "is", "value": "null"},
        {"id": "f2", "on": "n3", "attribute": "ATT1", "dtype": "int", "operation": "is", "value": "null"},
        {"id": "f3", "on": "n1", "attribute": "ATT2", "dtype": "str", "operation": "=", "value": "V"},
        {"id": "f4", "on": "n2", "attribute": "ATT3", "dtype": "str", "operation": "is", "value": "not null"}
    ]
}

“返回”部分以及其他内容将在以后添加。我正在使用的图形的缺点之一是,每个属性(无论内容如何)都是字符串数据类型,因此,我需要一种自动分配toInteger(和类似)操作的方法。这就是“过滤器”中“ dtype”项的目的;例如,“ int”表示我需要toInteger。

实现此目的的一种方法是处理大量的IF语句,我宁愿避免,尤其是因为“ IF语句的大量混乱”是我在代码中插入“ operation”项的解决方案。这是代码的相关部分:

from pypher import Pypher
q = Pypher()

if(len(motif['filters']) >= 1):
    if(motif['filters'][0]['operation'] != 'is'):
        if(motif['filters'][0]['operation'] == '='):
            q.Where(motif['filters'][0]['on']+'.'+motif['filters'][0]['attribute']) == motif['filters'][0]['value']
        elif(motif['filters'][0]['operation'] == '>'):
            q.Where(motif['filters'][0]['on']+'.'+motif['filters'][0]['attribute']) > motif['filters'][0]['value']
        elif(motif['filters'][0]['operation'] == '>='):
            q.Where(motif['filters'][0]['on']+'.'+motif['filters'][0]['attribute']) >= motif['filters'][0]['value']
        elif(motif['filters'][0]['operation'] == '<'):
            q.Where(motif['filters'][0]['on']+'.'+motif['filters'][0]['attribute']) < motif['filters'][0]['value']
        elif(motif['filters'][0]['operation'] == '<='):
            q.Where(motif['filters'][0]['on']+'.'+motif['filters'][0]['attribute']) <= motif['filters'][0]['value']

    elif(motif['filters'][0]['operation'] == 'is'):
        if(motif['filters'][0]['value'] == 'null'):
            q.Where(motif['filters'][0]['on']+'.'+motif['filters'][0]['attribute']).IS_NULL()
        if(motif['filters'][0]['value'] == 'not null'):
            q.Where(motif['filters'][0]['on']+'.'+motif['filters'][0]['attribute']).IS_NOT_NULL()

    if(len(motif['filters']) >= 2):
        for k in range(len(motif['filters'])-1):
            if(motif['filters'][k+1]['operation'] != 'is'):
                if(motif['filters'][k+1]['operation'] == '='):
                    q.And(motif['filters'][k+1]['on']+'.'+motif['filters'][k+1]['attribute']) == motif['filters'][k+1]['value']
                elif(motif['filters'][k+1]['operation'] == '>'):
                    q.And(motif['filters'][k+1]['on']+'.'+motif['filters'][k+1]['attribute']) > motif['filters'][k+1]['value']
                elif(motif['filters'][k+1]['operation'] == '>='):
                    q.And(motif['filters'][k+1]['on']+'.'+motif['filters'][k+1]['attribute']) >= motif['filters'][k+1]['value']
                elif(motif['filters'][k+1]['operation'] == '<'):
                    q.And(motif['filters'][k+1]['on']+'.'+motif['filters'][k+1]['attribute']) < motif['filters'][k+1]['value']
                elif(motif['filters'][k+1]['operation'] == '<='):
                    q.And(motif['filters'][k+1]['on']+'.'+motif['filters'][k+1]['attribute']) <= motif['filters'][k+1]['value']
            else:
                if(motif['filters'][k+1]['value'] == 'null'):
                    q.And(motif['filters'][k+1]['on']+'.'+motif['filters'][k+1]['attribute']).IS_NULL()
                if(motif['filters'][k+1]['value'] == 'not null'):
                    q.And(motif['filters'][k+1]['on']+'.'+motif['filters'][k+1]['attribute']).IS_NOT_NULL()

toInteger(或toFloat,toBoolean,toString)命令需要应用于脚本的motif['filters'][k+1]['on']+'.'+motif['filters'][k+1]['attribute']部分,例如:

q.And(__.toInteger(motif['filters'][k+1]['on']+'.'+motif['filters'][k+1]['attribute'])).IS_NULL()

有人知道如何使Python自动分配__。toInteger()(或__。toBoolean等)而不弄乱吗?同样,有人知道没有所有IF语句自动分配运算符的方法吗?只需将操作项放入:

q.Where(motif['filters'][0]['on']+'.'+motif['filters'][0]['attribute']) motif['filters'][0]['operation'] motif['filters'][0]['value']

q.Where(motif['filters'][0]['on']+'.'+motif['filters'][0]['attribute']) + motif['filters'][0]['operation'] + motif['filters'][0]['value']

不起作用。

1 个答案:

答案 0 :(得分:0)

由于缺乏更好的主意,我的解决方案是使用大量的条件语句。效果很好,有点难看。