Giter VIP home page Giter VIP logo

Comments (3)

mahmoud avatar mahmoud commented on June 1, 2024 1

Ah, the grandparent key, sure! This isn't as pretty as I would like, but here's the working spec:

(
        T.items(), 
        Iter(
          (A.globals.gp_item, T[1].values(), 
           Iter({'val': 'c', 
                 'path': S.globals.gp_item[0]}))
        ).flatten().all()
)

And I get:

[
    {
        "val": 1,
        "path": "a"
    }
]

Here's a link to a glompad where you can fool around with it in your browser.

Two key things to note:

  • I'm using the globals member of the scope (and the A assignment shortcut) to save the grandparent item before we descend. Probably when you used S the value was being lost because the scope was local.
    • If this is part of a much larger spec, you can create your own non-local, non-global scope with Vars documented in the link above.
    • I found it simpler to save the grandparent item (key and value), instead of just the value, and then only emit the key (gp_item[0]).
  • Notice I'm using two Iter specs. glom doesn't yet have full tree traversal, so the nesting of the spec is going to roughly match the nesting of your data. If you need this to work in an arbitrary nested structure, I recommend remap, the recursive map function, part of boltons.iterutils.

Hope this helps!

from glom.

therve avatar therve commented on June 1, 2024

Thanks a lot, I almost reached that one myself! For some reason I was focused on trying to save just the key T[0] instead of the whole items structure, this works.

And a related thank you for that great library.

from glom.

mahmoud avatar mahmoud commented on June 1, 2024

Yeah, I know what you mean re: keys vs items. I might add this example to the docs, in case it's instructive for others too :)

from glom.

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.