将功能转换为OOP

时间:2018-06-12 16:17:34

标签: python arrays oop recursion initialization

我有一个像这样的json dictonary:

{
  "name": "Root Folder",
  "type": "folder",
  "children": [
    {
      "name": "Folder A",
      "type": "folder",
      "children": [
        {
          "name": "Folder A-A",
          "type": "folder",
          "children": [
            {
              "name": "File A-A-1",
              "type": "file",
              "children": []
            }
          ]
        },
        {
          "name": "Folder A-B",
          "type": "folder",
          "children": []
        }
      ]
    },
    {
      "name": "Folder B",
      "type": "folder",
      "children": [
        {
          "name": "Folder B-A",
          "type": "folder",
          "children": [
            {
              "name": "Folder B-A-A",
              "type": "folder",
              "children": []
            }
          ]
        },
        {
          "name": "Folder B-B",
          "type": "folder",
          "children": []
        }
     ]
   },
   {
      "name": "File Root",
      "type": "file",
      "children": []
   }
  ]
}

从这个dictonary我想自动获得价值"路径","名称"和"键入"对于每个成员以及级别,因此root是level0,Folder A级别1,文件夹AA级别2等等。

为了实现这个目标,我创建了以下Python代码,它接受一个json-dictionary并将其中的值放入数组中:

import json
import os
from jinja2 import Template


with open('Folder_structure.json') as f:
    data = json.load(f)

def arr4tmpl(data,arr_path,arr_name,arr_type,arr_level):

    for child in data['children']:

        arr_path.extend([child['path']])
        arr_name.extend([child['name']])
        arr_type.extend([child['type']])
        arr_level.extend([arr4tmpl.level])

        arr4tmpl.level+=1
        arr4tmpl(child,arr_path,arr_name,arr_type,arr_level)
        arr4tmpl.level-=1

    return(arr_path,arr_name,arr_type,arr_level)

def main():
    arr4tmpl.level = 0
    arr=[]
    arr_path=[]
    arr_name=[]
    arr_type=[]
    arr_level=[]

(arr_path,arr_name,arr_type,arr_level)=arr4tmpl(data,arr_path,arr_name,arr_type,arr_level)

这很好用,但现在必须转变为面向对象的解决方案。所以我想到了以下(根据答案编辑,谢谢你)

import json
import os
from jinja2 import Template

class CreateTemplate(object):

    def __init__(self):
        self.arr_path=[]
        self.arr_name=[]
        self.arr_type=[]
        self.arr_level=[]
        self.level=0


    def arr4tmpl(self,data,level):
        for child in data['children']:

            arr_path.extend([child['path']])
            arr_name.extend([child['name']])
            arr_type.extend([child['type']])
            arr_level.extend([level])

            level+=1
            arr4tmpl(self,child,level)
            level-=1


        return(arr_path,arr_name,arr_type,arr_level)


with open('Folder_structure.json') as f:
    data = json.load(f)
template=CreateTemplate()
template.arr4tmpl(data,level)

想法是通过 init 方法初始化参数,然后在arr4tmpl方法中扩展它们。 据我所知,创建对象应该执行init方法,但是在执行代码时我得到错误:

File "Folder_structure.py", line 34, in <module>
template.arr4tmpl(data,level)
NameError: name 'level' is not defined

2 个答案:

答案 0 :(得分:0)

如果要修改对象变量,则必须使用self

引用它们
TextElParams<T>

答案 1 :(得分:0)

是的,我确实找到了一种方法:

import json
import os
from jinja2 import Template

class CreateTemplate(object):

    arr_path=[]
    arr_name=[]
    arr_type=[]
    arr_level=[]
    level=0



    def arr4tmpl(self,data):
        for child in data['children']:

            self.arr_path.extend([child['path']])
            self.arr_name.extend([child['name']])
            self.arr_type.extend([child['type']])
            self.arr_level.extend([self.level])

            self.level+=1
            self.arr4tmpl(child)
            self.level-=1


        return(self.arr_path,self.arr_name,self.arr_type,self.arr_level)


with open('Folder_structure.json') as f:
    data = json.load(f)
template=CreateTemplate()

template.arr4tmpl(data)

不确定为什么init也不起作用,因为我认为它会在创建对象时初始化参数,然后这些变量可以用于方法arr4tmpl。

也不确定是否可以改进arr4tmpl方法,但至少现在确实创建了这些数组。谢谢!