簡體   English   中英

Python從嵌套字典中為函數調用中的args提取值

[英]Python pull values from nested dictionary for args in function call

因此,我正在編寫一些Python代碼來讀取包含對象類型和參數的JSON數據。 我本質上需要遍歷此數據並調用幾個函數之一,這些函數使用這些參數每次創建一個新的唯一對象,以供以后使用。 JSON數據如下所示:

 {
 "objects" : {
  "1" : {
    "type" : "sphere",
    "radius" : "100",
    "centerx" : "30",
    "centery" : "40",
    "centerz" : "50"
},
  "2" : {
    "type" : "box",
    "lengthx" : "30",
    "lengthy" : "40",
    "lengthz" : "50",
    "centerx" : "60",
    "centery" : "70",
    "centerz" : "80"
},
  "3" : {
    "type" : "cone",
    "length" : "30",
    "radius1" : "40",
    "radius2" : "50",
    "centerx" : "60",
    "centery" : "70",
    "centerz" : "80"
},
  "4" : {
    "type" : "cylinder",
    "length" : "30",
    "radius" : "40",
    "centerx" : "60",
    "centery" : "70",
    "centerz" : "80"
    }
   }
}

這是我現在更新的代碼:

display, start_display, add_menu, add_function_to_menu = init_display()
with open('C:\Users\willi_000\Documents\Code\document (9).json') as   data_file:
    data = json.load(data_file)

funcs = {
    'sphere': BRepPrimAPI_MakeSphere,
    'box': BRepPrimAPI_MakeBox,
    'cone': BRepPrimAPI_MakeCone,
    'cylinder': BRepPrimAPI_MakeCylinder
}
shapes = []
for index, kwargs in data['objects'].iteritems():
    mypoint = gp_Pnt(float(kwargs.pop('centerx')),     float(kwargs.pop('centery')), float(kwargs.pop('centerz')))
    function = funcs[kwargs.pop('type')]
    #print mypoint
    #print function
    myshape = function(mypoint,float(**kwargs)).Shape()
    start_display() 

現在晚上我收到以下錯誤

Traceback (most recent call last):
  File "C:/Users/willi_000/Documents/Code/dumbbox.py", line 32, in <module>
    myshape = function(mypoint,**kwargs).Shape()
TypeError: keywords must be strings"

供參考,以下是make函數期望的兩個示例

BRepPrimAPI_MakeSphere::BRepPrimAPI_MakeSphere  (   const gp_Pnt &  Center,
const Standard_Real     R )     


BRepPrimAPI_MakeBox::BRepPrimAPI_MakeBox    (   const gp_Pnt &  P,
const Standard_Real     dx,
const Standard_Real     dy,
const Standard_Real     dz )    

這會將所有Shapes添加到列表中。

data ={
 "objects" : {
  "1" : {
    "type" : "sphere",
    "radius" : "100",
    "centerx" : "30",
    "centery" : "40",
    "centerz" : "50"
},
  "2" : {
    "type" : "box",
    "lengthx" : "30",
    "lengthy" : "40",
    "lengthz" : "50",
    "centerx" : "60",
    "centery" : "70",
    "centerz" : "80"
},
  "3" : {
    "type" : "cone",
    "length" : "30",
    "radius1" : "40",
    "radius2" : "50",
    "centerx" : "60",
    "centery" : "70",
    "centerz" : "80"
},
  "4" : {
    "type" : "cylinder",
    "length" : "30",
    "radius" : "40",
    "centerx" : "60",
    "centery" : "70",
    "centerz" : "80"
    }
   }
}

funcs = {
    'sphere': BRepPrimAPI_MakeSphere,
    'box': BRepPrimAPI_MakeBox,
    'cone': BRepPrimAPI_MakeCone,
    'cylinder': BRepPrimAPI_MakeCylinder
}
shapes = []
for index, kwargs in data['objects'].iteritems():
    function = funcs[kwargs.pop('type')]
    shapes.append(function(**kwargs).Shape())

我不得不偽造函數和.Shape()部分,但是它對我有用。


更新

根據評論(@williamwatts),我認為這就是您想要的。 for循環替換for

for index, kwargs in data['objects'].iteritems():
    function = funcs[kwargs.pop('type')]
    kwargs = {k:float(v) for k, v in kwargs.iteritems()}
    mypoint = gp_Pnt(kwargs.pop('centerx'), kwargs.pop('centery'), kwargs.pop('centerz'))
    myshape = function(mypoint, **kwargs).Shape()
    shapes.append(myshape)
    display.DisplayShape(myshape, update=True)

假設所有形狀都將具有centerxcenterycenterz屬性。


更新2

由於這些函數是使用* args定義的,因此不能使用關鍵字參數。 您將不得不使用此代替:

funcs = {
    'sphere': {
        'function': BRepPrimAPI_MakeSphere,
        'argnames': ['radius']
    },
    'box': {
        'function': BRepPrimAPI_MakeBox,
        'argnames': ['lengthx', 'lengthy', 'lengthz']
    },
    'cone': {
        'function': BRepPrimAPI_MakeCone,
        'argnames': ['length', 'radius1', 'radius2']
    },
    'cylinder': {
        'function': BRepPrimAPI_MakeCylinder,
        'argnames': ['length', 'radius']
    }
}
shapes = []
for index, kwargs in data['objects'].iteritems():
    print kwargs
    shapeinfo = funcs[kwargs.pop('type')]
    kwargs = {k:float(v) for k, v in kwargs.iteritems()}
    mypoint = gp_Pnt(kwargs.pop('centerx'), kwargs.pop('centery'), kwargs.pop('centerz'))
    args = [kwargs[name] for name in shapeinfo['argnames']]
    myshape = shapeinfo['function'](mypoint, *args).Shape()
    shapes.append(myshape)
    display.DisplayShape(myshape, update=True)

這樣可以確保以正確的順序發送args。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM