【问题标题】:Automatically assigning functions with Python in Pypher在 Pypher 中使用 Python 自动分配函数
【发布时间】:2019-07-17 01:13:29
【问题描述】:

我正在制作一个 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"}
    ]
}

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

一种方法是使用大量的 IF 语句,我宁愿避免这种情况,特别是因为“大量 IF 语句”是我将“操作”项插入代码的解决方案。这是代码的相关部分:

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']

没用。

【问题讨论】:

    标签: python function neo4j cypher


    【解决方案1】:

    由于缺乏更好的想法,我的解决方案是使用大量的条件语句。很好用,就是有点丑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多