我正在制作一个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语句的巨大混乱”是我将“操作”项插入到代码中的解决方案。这是守则的有关部分:
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']不起作用。
发布于 2019-03-05 21:31:32
由于缺乏更好的想法,我的解决方案是使用大量的条件语句。很好,只是有点丑。
https://stackoverflow.com/questions/54835608
复制相似问题