我正在使用并行python使用称为OrcFxAPI的模块运行多个动态仿真。如果该程序作为python程序在我的计算机上运行,则该程序运行完美,但是如果我使用py2exe将其转换为exe文件,然后运行,则会收到以下错误消息:
Traceback (most recent call last):
File "Analysis.pyc", Line 500, in multiprocessor
File "pp.pyc", Line 342, in __init__
File "pp.pyc", Line 506, in set_ncpus
File "pp.pyc", Line 140, in __init__
File "pp.pyc", Line 152, in start
File "pptransport.pyc", Line 140, in receive
RuntimeError: Communication pipe read error
Run Code Online (Sandbox Code Playgroud)
我的程序在这一行失败了:
job_server = pp.Server(ppservers=ppservers)
Run Code Online (Sandbox Code Playgroud)
但我认为这可能与提交作业时用于导入OrcFxAPI模块的路径有关:
job = job_server.submit(max_seastate, (gui_vars, case_list, case, line_info, output_vars), (), ("OrcFxAPI",), callback=finished, callbackargs=(case_no, no_of_cases,))
Run Code Online (Sandbox Code Playgroud) 仅使用 Optional 或 ZeroOrMore 时,pyparsing 似乎进入了无限循环。以下代码有效,但“# 应该与 pp.Optional() 一起使用”部分确实应该是 Optional 而不是 OneOrMore。在这种情况下,我应该放置某种 stopOn 吗?
字典如下图所示:
其中 [expr] 表示可选 expr,而 [expr]... 表示可以重复的可选 expr,所以 ZeroOrMore:
[PINS numPins ;
[ – pinName + NET netName
[+ SPECIAL]
[+ DIRECTION {INPUT | OUTPUT | INOUT | FEEDTHRU}]
[+ NETEXPR "netExprPropName defaultNetName"]
[+ SUPPLYSENSITIVITY powerPinName]
[+ GROUNDSENSITIVITY groundPinName]
[+ USE {SIGNAL | POWER | GROUND | CLOCK | TIEOFF | ANALOG | SCAN | RESET}]
[+ ANTENNAPINPARTIALMETALAREA value [LAYER layerName]] ...
[+ ANTENNAPINPARTIALMETALSIDEAREA value [LAYER …Run Code Online (Sandbox Code Playgroud) 在我作为工程师(不是软件)的工作中,有时我必须用 Python 编程。按照教程,我了解了字典以及它们如何用于存储数据,它们的速度很快等等。
我嵌套的字典可以达到 7 级,如下所示:
mydict['level1']['level2']['level3']['level4']['level5']['level6']['level7']
Run Code Online (Sandbox Code Playgroud)
我有这些小怪物,因为我解析了一个带有库的文档,该库返回如下内容:
mydict['level1']['level2']['level3']['level4']['level5']['level6']['level7']
Run Code Online (Sandbox Code Playgroud)
我还有其他深度嵌套的字典。
我面临的问题是我需要迭代到最后一级来获取和过滤结果,因为我事先不知道键,所以我像下面的伪代码一样迭代:
components['comp1']['comp_name']
components['comp1']['comp_dimension']
components['comp1']['subcomponents']['subcomp1']
components['comp1']['subcomponents']['subcomp1']['comp_name1']
etc.
Run Code Online (Sandbox Code Playgroud)
filter_subcomps = ["comp1", "comp10"]
filter_value = 10.0
for component in components
comp_name = components[component]['comp_name']
comp_dimension = components[component]['comp_dimension']
if components[component].get('subcomponents', False):
subcomp_keys = filter_subcomps if filter_subcomps else components[component]['subcomponents'].keys()
for subcomponent in subcomp_keys:
etc etc
if value > X:
return value
Run Code Online (Sandbox Code Playgroud)
这些是一个相当简单的例子,有时代码会出现超过 10 级的缩进,这看起来很糟糕,我不得不水平滚动页面。除此之外,即使是我在几天或几周后阅读代码也很困难。
我从人们那里阅读了一些教程,这些教程将表格数据转换为嵌套的字典,反之亦然,甚至有人使用 xpath 访问字典键。
无论如何,遵循python的禅宗我当然不尊重“扁平比嵌套更好”,因为我的代码进入了天文级别的缩进。
我正在考虑将所有嵌套的 dict 转换为 SQLite 并使用 SQL 语言进行查询,而不是使用这些丑陋的 for 循环和 if …
我有以下代码将嘈杂的方波转换为无噪声的方波:
import numpy as np
threshold = 0.5
low = 0
high = 1
time = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
amplitude = np.array([0.1, -0.2, 0.2, 1.1, 0.9, 0.8, 0.98, 0.2, 0.1, -0.1])
# using list comprehension
new_amplitude_1 = [low if a<threshold else high for a in amplitude]
print(new_amplitude_1)
# gives: [0, 0, 0, 1, 1, 1, 1, 0, 0, 0]
# using numpy's where
new_amplitude_2 = np.where(amplitude > threshold)
print(new_amplitude_2)
# gives: (array([3, 4, …Run Code Online (Sandbox Code Playgroud)