使用Python数据结构表示此的最佳方法

zen*_*ngr 1 python data-structures

  <specification> 
    <propertyName> string </propertyName>
    <value> 
      <number> 
        <value> anyNumberHere </value>
      </number>
      <text> 
        <value> anyTextHere </value>
      </text>
      <URL> 
        <value> anyURIHere </value>
      </URL>
    </value>
    <!-- ... 1 or more value nodes here ... -->
  </specification>
  <!-- ... 1 or more specification nodes here ... -->
Run Code Online (Sandbox Code Playgroud)

我需要为API构建此请求,其中用户将传递这些值.那么,什么应该是表示这种情况的最佳方式,以便方法的用户可以轻松地将相应的值传递给操作?

我在想:

Dicts列表清单:

[specification1, specification2, specification3]
Run Code Online (Sandbox Code Playgroud)

哪里:

specification1= [value1, value2]
Run Code Online (Sandbox Code Playgroud)

哪里:

value1 = {number:anyNumberHere, text:anyTextHere, URL:anyURIHere}
value2 = {number:anyNumberHere, text:anyTextHere, URL:anyURIHere}
Run Code Online (Sandbox Code Playgroud)

但是,我无法容纳:<propertyName>这里.有什么建议?

更重要的是,这听起来很复杂.我们可以像在Java中那样进行对象封装吗?我明白了,我们可以,但我很好奇,在python中推荐的方式是什么?

My logic for now, suggestions (incorrect due to propertyName):
    #specification is a List of List of Dicts
    for spec in specification:
        specification_elem = etree.SubElement(root, "specification")
        propertyName_elem = etree.SubElement(specification_elem,"propertyName")
        propertyName_elem.text = spec_propertyName

        for value in spec:
            value_elem = etree.SubElement(specification_elem, "value")
            for key in value:
                key_elem = etree.SubElement(value_elem, key)
                keyValue_elem = etree.SubElement(key_elem, "value")
                keyValue_elem.text = value[key]
Run Code Online (Sandbox Code Playgroud)

在这里,我将传递spec_propertyName,作为diff参数.所以,用户将通过:specification and spec_propertyName

Jas*_*uit 6

一个Specification对象列表怎么样,每个对象都有一个property_name和一个value字典列表?(values可以是对象列表而不是字典.)

例如:

class Value(object):
    def __init__(self,
                 number=None,
                 text=None,
                 url=None):
        self.number = number
        self.text = text
        self.url = url

class Specification(object):
    def __init__(self, propertyName):
        self.propertyName = propertyName
        self.values = []

spec1 = Specification("Spec1")
spec1.values = [Value(number=3,
                      url="http://www.google.com"),
                Value(text="Hello, World!",
                      url="http://www.jasonfruit.com")]

spec2 = Specification("Spec2")
spec2.values = [Value(number=27,
                      text="I can haz cheezburger?",
                      url="http://stackoverflow.com"),
                Value(text="Running out of ideas.",
                      url="http://news.google.com")]
Run Code Online (Sandbox Code Playgroud)