Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xpngtc
Python-Read-Yaml-Dict
提交
13d14222
P
Python-Read-Yaml-Dict
项目概览
xpngtc
/
Python-Read-Yaml-Dict
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python-Read-Yaml-Dict
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
13d14222
编写于
9月 11, 2020
作者:
xpngtc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add new file
上级
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
136 addition
and
0 deletion
+136
-0
dictClass.py
dictClass.py
+136
-0
未找到文件。
dictClass.py
0 → 100644
浏览文件 @
13d14222
#!/usr/bin/python3
import
copy
class
dictinfo
()
:
''' show dict info '''
def
__init__
(
self
,
dictin
)
:
self
.
mdict
=
dictin
self
.
keylist
=
[]
self
.
key
=
[]
self
.
depth
=
[]
self
.
outv
=
''
self
.
dictparsermain
(
self
.
mdict
)
def
refresh
(
self
,
data
)
:
''' refresh data '''
self
.
mdict
=
copy
.
deepcopy
(
data
)
self
.
key
=
[]
self
.
depth
=
[]
self
.
keylist
=
[]
self
.
dictparsermain
(
self
.
mdict
)
def
showkey
(
self
)
:
''' show all key combination '''
return
self
.
keylist
def
haskey
(
self
,
klist
)
:
''' confirm key is existed '''
if
klist
in
self
.
keylist
or
klist
==
[]
:
return
True
else
:
return
False
def
getvalue
(
self
,
klist
)
:
'''get value '''
if
self
.
haskey
(
klist
)
:
klistin
=
copy
.
deepcopy
(
klist
)
klistin
.
reverse
()
self
.
loopgetv
(
self
.
mdict
,
klistin
)
return
self
.
outv
else
:
return
None
def
loopgetv
(
self
,
data
,
klist
)
:
if
klist
!=
[]
:
key
=
klist
[
-
1
]
klist
.
pop
()
self
.
loopgetv
(
data
[
key
]
,
klist
)
else
:
self
.
outv
=
data
def
dictparsermain
(
self
,
data
)
:
self
.
depth
.
append
(
len
(
data
.
keys
()))
for
k
in
data
:
self
.
key
.
append
(
k
)
stack
=
copy
.
deepcopy
(
self
.
key
)
if
not
stack
in
self
.
keylist
:
self
.
keylist
.
append
(
stack
)
if
isinstance
(
data
[
k
]
,
dict
)
:
self
.
dictparsermain
(
data
[
k
])
else
:
stack
=
copy
.
deepcopy
(
self
.
key
)
if
not
stack
in
self
.
keylist
:
self
.
keylist
.
append
(
stack
)
while
True
:
if
self
.
depth
==
[]
:
break
if
self
.
depth
[
-
1
]
==
1
:
self
.
key
.
pop
()
self
.
depth
.
pop
()
else
:
self
.
depth
[
-
1
]
=
self
.
depth
[
-
1
]
-
1
self
.
key
.
pop
()
break
''' modify dicct '''
def
dictmerge
(
dict1
,
dict2
)
:
'''
merge dict
'''
for
k
in
dict2
:
if
k
not
in
dict1
:
dict1
[
k
]
=
copy
.
deepcopy
(
dict2
[
k
])
else
:
if
isinstance
(
dict1
[
k
]
,
dict
)
:
dictmerge
(
dict1
[
k
]
,
dict2
[
k
])
else
:
dict1
[
k
]
=
copy
.
deepcopy
(
dict2
[
k
])
def
setvalue
(
dictin
,
keylist
,
value
)
:
if
len
(
keylist
)
==
0
:
return
k
=
keylist
[
0
]
if
len
(
keylist
)
==
1
:
dictin
[
k
]
=
copy
.
deepcopy
(
value
)
else
:
if
keylist
[
0
]
in
dictin
:
keylist
.
pop
(
0
)
setvalue
(
dictin
[
k
]
,
keylist
,
value
)
else
:
dictin
[
k
]
=
{}
keylist
.
pop
(
0
)
setvalue
(
dictin
[
k
]
,
keylist
,
value
)
''' test '''
d1
=
{
'a'
:{
'cb'
:
'4'
,
'ab'
:{
'abc'
:{
'abcd'
:
'1'
}}
,
'bb'
:{
'bbc'
:{
'bbcd'
:
'2'
}
,
'cbc'
:{
'cbcd'
:
'3'
}}}}
print
(
d1
)
md1
=
dictinfo
(
d1
)
print
(
md1
.
showkey
())
d2
=
{
'aa'
:{
'cb'
:
'4'
,
'ab'
:{
'abc'
:{
'abcd'
:
'1'
}}
,
'bb'
:{
'bbc'
:{
'bbcd'
:
'2'
}
,
'cbc'
:{
'cbcd'
:
'3'
}}}}
md1
.
refresh
(
d2
)
print
(
md1
.
showkey
())
print
(
md1
.
getvalue
([
'aa'
,
'ab'
,
'abc'
,
'abcd'
]))
a
=
{
'a'
:{
'b'
:
2
}}
b
=
{
'a'
:{
'c'
:
2
}}
dictmerge
(
a
,
b
)
print
(
a
)
setvalue
(
a
,
[
'a'
,
'b'
]
,
22
)
print
(
a
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录