stefankoegl / python-json-pointer

Resolve JSON Pointers in Python
https://python-json-pointer.readthedocs.org/
Other
141 stars 43 forks source link

Support for set_pointer and indexing arbitrary objects via __getitem__/_setitem__ #5

Closed christopherjwhite closed 10 years ago

christopherjwhite commented 10 years ago

This adds support to modify documents based on jsonpointers as well as support for any object whose class supports the necessary methods getitem (for resolving pointers to get data), and setitem (for the set_pointer operation).

See the tests for an example.

I considered wrapping the support for non-dict/list types via getitem with a boolean "check_getitem", but in the end I figured it was unnecessary complexity.

coveralls commented 10 years ago

Coverage Status

Coverage remained the same when pulling 4e754984c8e1aa21263d925e923386b166d1fe51 on riverbed:master into 48dce313141ba5bf0d2f3dd2e590042c05755e53 on stefankoegl:master.

stefankoegl commented 10 years ago

The patch looks good in general, but please fix the tests for Python 3.2 and 3.3 (see https://travis-ci.org/stefankoegl/python-json-pointer/builds/11633865)

christopherjwhite commented 10 years ago

Hmm. The doctests are failing. In 2.7 the strings embedded in the objects are converted to unicode, thus come out with u'anArray' whereas in 3.x they just print out as strings without the u prefix.

I don't have a lot of experience with 3.2. The simple fix is to drop the examples from the docstrings, but not sure if that means I'm hiding something.

I'll research further. Let me know if you have encountered this before and have a better solution.

...cj

On 9/21/13 2:28 PM, Stefan Kögl wrote:

The patch looks good in general, but please fix the tests for Python 3.2 and 3.3 (see https://travis-ci.org/stefankoegl/python-json-pointer/builds/11633865)

— Reply to this email directly or view it on GitHub https://github.com/stefankoegl/python-json-pointer/pull/5#issuecomment-24868055.

coveralls commented 10 years ago

Coverage Status

Coverage remained the same when pulling 0f873fd506746f1ad6033486b14cebe4537482b4 on riverbed:master into 48dce313141ba5bf0d2f3dd2e590042c05755e53 on stefankoegl:master.

christopherjwhite commented 10 years ago

This is apparently a somewhat known issue and I could find no clean solution. It apparently looks better in Python 3.3. As such, I took the easy route and simply commented out those example commands so that they don't run at all, thus cannot fail. Unfortunate, but other solutions look rather horribly complex just for 2 examples. Hopefully you're cool with it this way.

stefankoegl commented 10 years ago

Commenting out docstrings is not really a nice solution...

Instead of

>>> set_pointer(obj, '/foo/anArray/0/prop', 55)
{'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}}

please try something like

>>> set_pointer(obj, '/foo/anArray/0/prop', 55) ==  {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}}
True
coveralls commented 10 years ago

Coverage Status

Coverage remained the same when pulling 69db17be2d771af67d382831c67cddb1c67208d1 on riverbed:master into 48dce313141ba5bf0d2f3dd2e590042c05755e53 on stefankoegl:master.

christopherjwhite commented 10 years ago

Good suggestion, that seems to work well!

...cj

On 9/22/13 5:22 AM, Stefan Kögl wrote:

Commenting out docstrings is not really a nice solution...

Instead of

set_pointer(obj, '/foo/anArray/0/prop', 55) {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}}

please try something like

set_pointer(obj, '/foo/anArray/0/prop', 55) == {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}} True

— Reply to this email directly or view it on GitHub https://github.com/stefankoegl/python-json-pointer/pull/5#issuecomment-24878776.

stefankoegl commented 10 years ago

I've squashed your commits into one, fixed some whitespace-only changes, and merged everything into master: 19f9f2152480c72e0f83f48930088bd22f1eded5.

Thanks :)