Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
python-json-pointer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OpenStack
python
python-json-pointer
Commits
896102d6
Unverified
Commit
896102d6
authored
3 years ago
by
Stefan Kögl
Committed by
GitHub
3 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #46 from PeterlitsZo/patch-1
Add method and add classmethod tag
parents
7d146bd7
a5e9f91e
No related branches found
No related tags found
1 merge request
!1
Fix some issues reported by lintian
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
jsonpointer.py
+10
-4
10 additions, 4 deletions
jsonpointer.py
tests.py
+22
-1
22 additions, 1 deletion
tests.py
with
32 additions
and
5 deletions
jsonpointer.py
+
10
−
4
View file @
896102d6
...
...
@@ -193,7 +193,7 @@ class JsonPointer(object):
for
part
in
self
.
parts
[:
-
1
]:
doc
=
self
.
walk
(
doc
,
part
)
return
doc
,
self
.
get_part
(
doc
,
self
.
parts
[
-
1
])
return
doc
,
JsonPointer
.
get_part
(
doc
,
self
.
parts
[
-
1
])
def
resolve
(
self
,
doc
,
default
=
_nothing
):
"""
Resolves the pointer against doc and returns the referenced object
"""
...
...
@@ -228,7 +228,8 @@ class JsonPointer(object):
parent
[
part
]
=
value
return
doc
def
get_part
(
self
,
doc
,
part
):
@classmethod
def
get_part
(
cls
,
doc
,
part
):
"""
Returns the next step in the correct type
"""
if
isinstance
(
doc
,
Mapping
):
...
...
@@ -239,7 +240,7 @@ class JsonPointer(object):
if
part
==
'
-
'
:
return
part
if
not
self
.
_RE_ARRAY_INDEX
.
match
(
str
(
part
)):
if
not
JsonPointer
.
_RE_ARRAY_INDEX
.
match
(
str
(
part
)):
raise
JsonPointerException
(
"'
%s
'
is not a valid sequence index
"
%
part
)
return
int
(
part
)
...
...
@@ -252,12 +253,17 @@ class JsonPointer(object):
else
:
raise
JsonPointerException
(
"
Document
'
%s
'
does not support indexing,
"
"
must be mapping/sequence or support __getitem__
"
%
type
(
doc
))
def
get_parts
(
self
):
"""
Returns the list of the parts. For example, JsonPointer(
'
/a/b
'
).get_parts() == [
'
a
'
,
'
b
'
]
"""
return
self
.
parts
def
walk
(
self
,
doc
,
part
):
"""
Walks one step in doc and returns the referenced part
"""
part
=
self
.
get_part
(
doc
,
part
)
part
=
JsonPointer
.
get_part
(
doc
,
part
)
assert
hasattr
(
doc
,
'
__getitem__
'
),
"
invalid document type %s
"
%
(
type
(
doc
),)
...
...
This diff is collapsed.
Click to expand it.
tests.py
+
22
−
1
View file @
896102d6
...
...
@@ -70,10 +70,31 @@ class SpecificationTests(unittest.TestCase):
ptr
=
JsonPointer
(
path
)
self
.
assertEqual
(
path
,
ptr
.
path
)
parts
=
ptr
.
parts
parts
=
ptr
.
get_parts
()
self
.
assertEqual
(
parts
,
ptr
.
parts
)
new_ptr
=
JsonPointer
.
from_parts
(
parts
)
self
.
assertEqual
(
ptr
,
new_ptr
)
def
test_parts
(
self
):
paths
=
[
(
""
,
[]),
(
"
/foo
"
,
[
'
foo
'
]),
(
"
/foo/0
"
,
[
'
foo
'
,
'
0
'
]),
(
"
/
"
,
[
''
]),
(
"
/a~1b
"
,
[
'
a/b
'
]),
(
"
/c%d
"
,
[
'
c%d
'
]),
(
"
/e^f
"
,
[
'
e^f
'
]),
(
"
/g|h
"
,
[
'
g|h
'
]),
(
"
/i
\\
j
"
,
[
'
i\j
'
]),
(
"
/k
\"
l
"
,
[
'
k
"
l
'
]),
(
"
/
"
,
[
'
'
]),
(
"
/m~0n
"
,
[
'
m~n
'
]),
(
'
/
\xee
'
,
[
'
\xee
'
]),
]
for
path
in
paths
:
ptr
=
JsonPointer
(
path
[
0
])
self
.
assertEqual
(
ptr
.
get_parts
(),
path
[
1
])
class
ComparisonTests
(
unittest
.
TestCase
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment