Comments (3)
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 theA
assignment shortcut) to save the grandparent item before we descend. Probably when you usedS
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]
).
- If this is part of a much larger spec, you can create your own non-local, non-global scope with
- 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 recommendremap
, the recursive map function, part ofboltons.iterutils
.
Hope this helps!
from glom.
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.
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)
- glom.grouping is super useful actually HOT 1
- Inconsistent Delete Behavior HOT 5
- Extract nodes from json based on user input preserveing a portion of the higher level object as well HOT 1
- unprintable PathAccessError when using Scope/S
- Assign a target with an object which has a `__setattr__` which returns a callable produces a false positive for `glom.core._has_callable_glomit` HOT 4
- Feature Discussion: More granular `skip_exc` in `Coalesce` HOT 2
- Recurive wildcard produces a StopIteration exception when called on objects containing iterators HOT 2
- GitHub releases not updated for 23.1.0 and 23.1.1 HOT 2
- Scope usage changes from v22 to v23 HOT 7
- Question, how to pull items into an array in a spec? HOT 1
- Replace deprecated imp module with importlib HOT 2
- Traceback mismatch in a couple tests on 3.11+ HOT 8
- `skip` option for Coalesce doesn't seems to work HOT 2
- Inspect output is not clear HOT 1
- replicate logic with Switch HOT 2
- Missing assert in test_mutation.py HOT 1
- Missing assert in test_cli.py
- Enumerate and Assign HOT 2
- Recursive Delete-If-Empty? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from glom.