•  


GitHub - carlosescri/DottedDict: Python library that provides a method of accessing lists and dicts with a dotted path notation.
Skip to content

Python library that provides a method of accessing lists and dicts with a dotted path notation.

License

Notifications You must be signed in to change notification settings

carlosescri/DottedDict

Folders and files

Name Name
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dotted

Latest Version

image

A Python library that provides a method of accessing lists and dicts with a dotted path notation. It is useful to access a deep path inside a complex object composed of lists and dicts.

Quick & Dirty:

from
 dotted
.
collection
 import
 DottedCollection
, 
DottedDict
, 
DottedList


obj
 =
 DottedCollection
.
factory
(
dict_or_list
)
obj
 =
 DottedCollection
.
load_json
(
json_value
)
obj
 =
 DottedDict
(
a_dict
)
obj
 =
 DottedList
(
a_list
)

from
 dotted
.
utils
 import
 dot
, 
dot_json


obj
 =
 dot
(
dict_or_list
)
obj
 =
 dot_json
(
json_value
)

DottedDict and DottedList have the same accessors as dict and list so you can iterate them as usual. Both type of objects support access via a dotted path key.

Examples

Example #1: DottedList

obj
 =
 DottedList
([
0
, 
1
, 
2
, 
3
, [
4
, 
5
, 
6
], 
7
, 
8
, [
9
, 
10
]])

All of these are true:

obj
[
0
]     
==
  0

obj
[
'1'
]   
==
  1

obj
[
'4.0'
] 
==
  4

obj
[
'4.2'
] 
==
  6

obj
[
5
]     
==
  7

obj
[
'7.1'
] 
==
 10

If you want to append you can do:

obj
.
append
(
12
)

or:

obj
[
8
] 
=
 11

but the latter only works if index == len(obj) . In other case you will get a very pretty exception.

Example #2: DottedDict

obj
 =
 DottedDict
({
'hello'
: {
'world'
: {
'wide'
: 
'web'
}}})

All of these are true:

obj
[
'hello'
] 
==
 {
'world'
: {
'wide'
: 
'web'
}}
obj
[
'hello.world'
] 
==
 {
'wide'
: 
'web'
}
obj
[
'hello.world.wide'
] 
==
 'web'


obj
.
hello
 ==
 {
'world'
: {
'wide'
: 
'web'
}}
obj
.
hello
.
world
 ==
 {
'wide'
: 
'web'
}
obj
.
hello
.
world
.
wide
 ==
 'web'

Example #3: Both working together

obj
 =
 DottedCollection
.
factory
({
    
'hello'
: [{
'world'
: {
'wide'
: [
'web'
, 
'web'
, 
'web'
]}}]
})

You can access:

obj
[
'hello'
][
0
][
'world'
][
'wide'
][
0
]
obj
.
hello
[
0
].
world
.
wide
[
0
]
obj
.
hello
[
0
].
world
[
'wide'
][
0
]
obj
.
hello
[
0
].
world
[
'wide.0'
]
obj
.
hello
[
'0.world'
].
wide
[
0
]
...
obj
[
'hello.0.world.wide.0'
]

Example #4: When new values are dicts or lists

obj
 =
 DottedCollection
.
factory
(
some_obj
)

obj
[
'some.path'
] 
=
 {
'hello'
: 
'world'
}  
# will be converted to a DottedDict

obj
[
'another.path'
] 
=
 [
'hello'
]  
# will be converted to a DottedList

Example #5: Shortcuts

from
 dotted
.
utils
 import
 dot
, 
dot_json


obj
 =
 dot
({
'hello'
: 
'world'
})
obj
 =
 dot_json
(
'{"hello": "world"}'
)

Example #6: Keys with dots inside!

Well, you can actually use escaped keys, but it's better to avoid them:

from
 dotted
.
utils
 import
 dot
, 
dot_json

obj
 =
 dot
({
"hello\.world"
: 
"Hello!"
})
obj
 =
 dot_json
(
'{"hello
\\
\\
.world": "Hello!"}'
)
value
 =
 obj
[
"hello\.world"
]  
# Hello!

That's all!

Tests

Run in the terminal from the parent directory:

python -m dotted.test.test_collection

Special Thanks

About

Python library that provides a method of accessing lists and dicts with a dotted path notation.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본