python 基础操作

字典级别目录(导航)

用递归方法进行级别导航,将字典中name: level_list的键值对找到,将目录级别放在text_list列表中。

此操作应用场景是在html里展示导航路径,如图:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
a_list = [
    {
        'text': "系统",
        'icon': "fa-bed",
        'children': [
            {'text': "级别1",
             'url': "/level/list/",
             'children': [
                 {
                     'text': "级别2",
                     'name': "level_list1"
                 },
                 {
                     'text': "级别3",
                     'name': "level_list3"
                 }
             ]},
        ]
    },
    {
        'text': "用户",
        'icon': "fa-bed",
        'children': [
            {'text': "管理1",
             'url': "/level/list/",
             'children': [
                 {
                     'text': "管理1-1",
                     'children': [
                         {
                             'text': "管理1-1-1",
                             'children': [
                                 {'text': "管理1-1-1-1", 'name': "level_list2"},
                                 {'text': "管理1-1-1-2", 'name': "level_list5"}
                             ]
 
                         },
                         {
                             'text': "管理1-1-2",
                             'children': [{
                                 'text': "管理1-1-2-1",
                                 'children': [
                                     {
                                         'text': '',
                                         'name': "level_list"
                                     }, {
                                         'text': '',
                                         'name': "level_list7"
                                     },
                                 ]
 
                             }]
 
                         }]
 
                 }
             ]},
            {'text': "管理2",
             'url': "/level/list/",
             'children': [
                 {
                     'text': "管理2-1",
                     'name': "level_list1"
                 },
                 {
                     'text': "管理2-2",
                     'name': "level_list3"
                 }
             ]},
            {'text': "管理3",
             'url': "/level/list/",
             'children': [
                 {'text': "管理3-1",
                  'url': "/level/list/",
                  'children': [
                      {
                          'text': "管理3-1-1",
                          'name': "level_list1"
                      },
                      {
                          'text': "管理3-1-2",
                          'name': "level_list2"
                      }
                  ]
                  },
             ]},
 
        ]
    },
 
]
 
text_list = []
 
 
def key_exists_in_nested_dict(nested_list, name='name', text='level_list'):
    for nested_dict in nested_list:
 
        if name in nested_dict:
            if nested_dict[name] == text:
                return True  # text_list.append(nested_dict['text'])
            # name 的value不是'level_list',判断是列表最后一个字典就将上个级别目录删除
            elif nested_dict == nested_list[-1]:
                text_list.pop(-1)
            continue
        text_list.append(nested_dict['text'])
        # 递归到进入下一级别
        if key_exists_in_nested_dict(nested_dict['children'], name='name', text='level_list'):
            return True
        # 分支的情况,内部循环执行完后未退出,判断是children列表最后一个字典则删除上级别目录
        elif len(text_list) and nested_dict == nested_list[-1]:
            text_list.pop(-1)
    # text_list.pop(-1)
    #     for key, value in nested_dict['children'].items():
    #         if isinstance(value, dict):
    #             if key_exists_in_nested_dict(value, key_to_check):
    #                 return True
    return False
 
 
key_exists_in_nested_dict(a_list, name='name', text='level_list')
print(text_list)

 

面向对象__str__,实例化直接输出

  

1
2
3
4
5
6
7
class Foo:
    def __init__(self,name):
        self.name=name
    def __str__(self):
        return self.name
obj = Foo('ddd')
print(obj)  # ddd

 

面向对象继承关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class BootStrapForm(object):
    exclude_data_list = []
 
    def __init__(self):
        print(self.exclude_data_list)
 
 
class UserForm(BootStrapForm):
    exclude_data_list = [11, 22, 33]
 
 
v1 = BootStrapForm()  # []
v2 = UserForm()  # [11, 22, 33]
v3 = UserForm()  #

 

django页面插件

forms.ModelForm

1
2
3
4
5
widget=forms.TextInput
widget=forms.PasswordInput
widget=forms.Select
widget=forms.Textarea
widget=forms.RadioSelect + CSS样式

 

  

 

posted @   ding-ding  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示