GithubHelp home page GithubHelp logo

Comments (1)

kurtbrose avatar kurtbrose commented on May 19, 2024

First off, it's a recursive problem so we'd need to use Ref in order to get that problem.

Then, it's a filtration problem, so we will need to use the SKIP marker object to drop things which don't match.

Additionally, there are two inputs here: one is the nested nodes, the other is the list of attribute nodes.

This might be better solved with a direct recursion or the remap() recursion helper from boltons. I don't want to over-promise that glom is the right solution. But, I'll take a shot at it.

Reformulating the problem:

  • we want to recurse on "child" attribute of dict
  • for every dict, if the "node" attribute is in the input set, completely hold onto it and all child nodes
  • on the way back out, we want to drop any nodes that weren't included in the result set, or whose children weren't included in the result set
node_spec = Ref("node-spec", 
   Or(
      # case 1: this node is in the input; return it and all children
      And(lambda t: t["node"] in t[S]["input-nodes"], T),
      # case 2: one of the children is in the input
      And((
         A.node,
         "child",
         [Ref("node-spec")],
         Merge(S.node, 

Oof, even as I'm writing this I can tell it's a bad fit for glom; there are multiple inputs and a lot of internal state. This is really just a recursion problem. I'm not going to bother trying to finish it, it would be really tortured.

def filter_node(cur, to_include):
   if cur["node"] in to_include:
      return cur
   children = []
   for child in cur["child"]:
      filtered = filter_node(child)
      if filtered:
         children.append(filtered)
    if children:
      return {**cur, "child": children}
   return None

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.