Python从嵌套字典中为函数调用中的args拉取值

时间:2016-08-31 20:51:44

标签: python json dictionary

所以我写了一些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 )    

1 个答案:

答案 0 :(得分:2)

这会将所有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 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。