Giter Site home page Giter Site logo

Comments (9)

Julian avatar Julian commented on May 22, 2024

Hi.

Not sure I'm understanding any of this, sorry.

ErrorTrees are essentially just nested dicts. The error tree in your example is essentially equivalent to the dict {"a" : {"b" : {"c" : ["spam"]}}}, but it carries along some extra information at each node, like possible errors at a, b, and c themselves, or keeping references to actual error objects. I see you have some funky behavior there iterating over the tree, which tells me you're probably on 0.7. Specifically, seeing a 0 there is totally wrong, the 0 is in tree["A"]["B"]["C"]. This should be fixed in the most recent release (v0.8.0). You should see:

>>> for x in tree:
...     print(x)
... 
A

This is the same as you'd get if you iterated over {"a" : {"b" : {"c" : ["spam"]}}} – it's telling you which keys the dict has, or in the case of ErrorTree, which children it has (which means which children of your instance had errors, in this case all of them are in the A child). If you want to dig further, you access that child and ask it for the same thing (and can do this programmatically certainly).

If you had a nested container in Python (a tree) and you wanted to iterate over all of its children, you'd do so. ErrorTrees are these kind of objects, so you iterate over it as you would any other tree (or nested dict), i.e. via recursion or with a stack or whatever. The simplest example being, say:

>>> def walk(tree):
...     errors = tree.errors
...     if errors:
...         yield errors
...     for child in tree:
...         for errors in walk(tree[child]):
...             yield errors
...         
...     
... 
>>> list(walk(tree))
[{'minItems': ValidationError(u"['spam'] is too short", None, ())}, {'type': ValidationError(u"'spam' is not of type 'number'", None, ())}]

If you wanted to keep track of the path at each recursion you could certainly modify that to do so, however if you really wanted that, as it sounds like you do, ValidationErrors already know their path and keep it in a way like it sounds like you want, so you would just iterate over the errors from iter_errors and pull off each of the path attributes and do what you'd like with them.

Let me know if this helps (I definitely can't tell what you want with #53, but if you have more ideas I'd love to hear them, just narrowing down on where exactly you're having trouble).

from jsonschema.

ertyz avatar ertyz commented on May 22, 2024
print(jsonschema.__version__)
for x in tree:
    print(x)
0.8.0
0
A

It strange. I try to use your code, but list(walk(tree)) return me []

Maybe the reason in my python version (CPython 3.3 for Win64)

from jsonschema.

Julian avatar Julian commented on May 22, 2024

Interesting.

I don't have a Windows machine, but I'll try this out on 3.3 when I get home just to see what's up.

from jsonschema.

ertyz avatar ertyz commented on May 22, 2024

I definitely can't tell what you want with #53

In my project I have a simple task:

  1. load large json datagram
  2. check values in loaded json
  3. if value is not valid for my terms -> then load default(empty) values
    but i need to save all other valid values in loaded json data
    Now I implement it by many blocks if-then in my code. And try to rewrite project to use jsonschema for this task.

from jsonschema.

Julian avatar Julian commented on May 22, 2024

Cool. So do you have something specific that'd make that easier in mind?

I'd do that the same way I showed here, do a depth first search of the tree and yield elements that don't have errors, only this time yield them from the instance, not the error tree.

from jsonschema.

Julian avatar Julian commented on May 22, 2024

I can't reproduce either the behavior you described for walk or the presence of 0 on 3.3. Can you pastebin a full interpreter session showing exactly what you're running perhaps?

from jsonschema.

ertyz avatar ertyz commented on May 22, 2024

damn. it's my fault. I didn't clear test code of print(tree[0].errors). This procedure create 0, when was invoked
Thank you for helping and great work!

from jsonschema.

Julian avatar Julian commented on May 22, 2024

Are we good to close this and #53 or was there still something you wanted to propose?

from jsonschema.

Julian avatar Julian commented on May 22, 2024

Closing, feel free to open a new ticket or reopen if you have something in mind.

from jsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.