为什么 shell 和 IDE 之间的 Neo4j Bolt Driver 不一致?
Why is the Neo4j Bolt Driver inconsistent between shell and IDE?
我发现 neo4j-bolt-driver
有一个奇怪的行为。当我使用 Pycharm
到 运行 我的代码时它工作得很好,对于 neo4j
的单个查询我得到以下响应:
type: neo4j.node # I pulled out the type of the element.
<Node id=3820 labels={'city'} properties={'ID': 'xddy', 'name': 'california'}>
现在,当我打包我的代码并从中创建一个 .egg
,然后使用终端将脚本 运行 用于相同数据库的相同输入时,我得到以下响应:
type: neo4j.node # I pulled out the type of the element.
(_3820:city {ID: 'xddy', name: 'california'})
现在看看响应的差异,类型是相同的,只是缺少对象的 keys
。
And this leads to an AttributeError
. Worse is I have to manually parse the data into a dict so that I can process it.
副作用:
try:
props = node[admin].properties
node_chain[list(node[admin].labels)[0]] = props
address.append(props['name'])
except AttributeError:
# try to convert (_3820:city {ID: 'xddy', name: 'california'})
# to {'ID': 'xddy', 'name': 'california'}
# and add it to an existing dict with the key `city`
string_rep = str(node[admin])
splitted = string_rep.split('{')
label = splitted[0].split(':')[-1].strip()
payload_string = "{ " + splitted[1][:-1]
clean = payload_string.replace("'", " ").replace(":", "':'").replace(",", "','")\
.replace("{", "{'").replace("}", "'}")
temp_dict = ast.literal_eval(clean)
payload_dict = {k.strip(): v.strip() for k, v in temp_dict.items()}
address.append(payload_dict['name'])
node_chain[label] = payload_dict
我正在寻找两个答案:
- 螺栓驱动器是否有问题,或者当 运行 来自
egg
时,这只是我的代码
- 有没有更好的方法将无效内容解析成dict?
您的执行环境存在差异。
即使您为 shell 执行和 PyCharm 项目解释器使用相同的虚拟环境,当执行 .egg 时,执行环境可能会被修改以引入所有的新副本这些库不一定安装到 "global" 模块路径("global" 这里意味着在不使用 virtualenv 时在系统范围内,或者在 virtualenv 的 python 模块中)。
您的 PyCharm 指出它使用的是 neo4j-driver
模块的版本 1.5.3
,但是 pip 拉入您的 .egg 执行环境的版本是 1.6.0a
, 依赖解析时的最新版本。因此,当从 shell 执行它时,您使用的是 neo4j-driver
.
的不同版本
这本身并没有那么糟糕,但是...
目前在 neo4j-driver 1.5.3 和 1.6.0 之间有重大变化。
1.6.0 更改了一些模块的路径,因此特定的导入可能会中断。它似乎也改变了一些数据对象的格式,正如您在示例中看到的那样。
乍一看,这似乎只是使用不稳定版本标签的结果,但这些更改很可能会保留下来,因为它是一个新版本。
为确保 pip/setuptools 安装的版本相同,固定版本号。
1.6.0 在某些方面彻底改变了 API。由于您是针对 1.5.3 开发的,因此您将不得不更改 API 以处理这两个版本(并且可能会冒着在未来发布更新时再次崩溃的风险),或者将其固定到特定版本。
要固定它,请通过从构建目录中删除所有二进制文件来清除 neo4j-driver
的任何现有版本,使用 pip 将其卸载,然后更新您的 setup.py
或其他依赖管理工具以指向您正在开发的特定版本。
对于 setup.py
,在 install_requires
、tests_require
或其他相关部分的库名称末尾添加 ==1.5.3
。
我发现 neo4j-bolt-driver
有一个奇怪的行为。当我使用 Pycharm
到 运行 我的代码时它工作得很好,对于 neo4j
的单个查询我得到以下响应:
type: neo4j.node # I pulled out the type of the element.
<Node id=3820 labels={'city'} properties={'ID': 'xddy', 'name': 'california'}>
现在,当我打包我的代码并从中创建一个 .egg
,然后使用终端将脚本 运行 用于相同数据库的相同输入时,我得到以下响应:
type: neo4j.node # I pulled out the type of the element.
(_3820:city {ID: 'xddy', name: 'california'})
现在看看响应的差异,类型是相同的,只是缺少对象的 keys
。
And this leads to an
AttributeError
. Worse is I have to manually parse the data into a dict so that I can process it.
副作用:
try:
props = node[admin].properties
node_chain[list(node[admin].labels)[0]] = props
address.append(props['name'])
except AttributeError:
# try to convert (_3820:city {ID: 'xddy', name: 'california'})
# to {'ID': 'xddy', 'name': 'california'}
# and add it to an existing dict with the key `city`
string_rep = str(node[admin])
splitted = string_rep.split('{')
label = splitted[0].split(':')[-1].strip()
payload_string = "{ " + splitted[1][:-1]
clean = payload_string.replace("'", " ").replace(":", "':'").replace(",", "','")\
.replace("{", "{'").replace("}", "'}")
temp_dict = ast.literal_eval(clean)
payload_dict = {k.strip(): v.strip() for k, v in temp_dict.items()}
address.append(payload_dict['name'])
node_chain[label] = payload_dict
我正在寻找两个答案:
- 螺栓驱动器是否有问题,或者当 运行 来自
egg
时,这只是我的代码
- 有没有更好的方法将无效内容解析成dict?
您的执行环境存在差异。
即使您为 shell 执行和 PyCharm 项目解释器使用相同的虚拟环境,当执行 .egg 时,执行环境可能会被修改以引入所有的新副本这些库不一定安装到 "global" 模块路径("global" 这里意味着在不使用 virtualenv 时在系统范围内,或者在 virtualenv 的 python 模块中)。
您的 PyCharm 指出它使用的是 neo4j-driver
模块的版本 1.5.3
,但是 pip 拉入您的 .egg 执行环境的版本是 1.6.0a
, 依赖解析时的最新版本。因此,当从 shell 执行它时,您使用的是 neo4j-driver
.
这本身并没有那么糟糕,但是...
目前在 neo4j-driver 1.5.3 和 1.6.0 之间有重大变化。
1.6.0 更改了一些模块的路径,因此特定的导入可能会中断。它似乎也改变了一些数据对象的格式,正如您在示例中看到的那样。
乍一看,这似乎只是使用不稳定版本标签的结果,但这些更改很可能会保留下来,因为它是一个新版本。
为确保 pip/setuptools 安装的版本相同,固定版本号。
1.6.0 在某些方面彻底改变了 API。由于您是针对 1.5.3 开发的,因此您将不得不更改 API 以处理这两个版本(并且可能会冒着在未来发布更新时再次崩溃的风险),或者将其固定到特定版本。
要固定它,请通过从构建目录中删除所有二进制文件来清除 neo4j-driver
的任何现有版本,使用 pip 将其卸载,然后更新您的 setup.py
或其他依赖管理工具以指向您正在开发的特定版本。
对于 setup.py
,在 install_requires
、tests_require
或其他相关部分的库名称末尾添加 ==1.5.3
。