Querying — RedBaron 0.8 documentation

# Querying

As you have seen in the previous section, you can navigate into RedBaron tree only using attribute access and index access on list of nodes with the use of the `.help()` method to know what you can do. However, RedBaron offers way more powerful and convenient tools to do that.

## .find()

To retrieve a single node, you can use the `.find()` method by passing it one of the identifiers listed in `.help()` of node you want to get, this way:

    In [1]: red = RedBaron("a = 1")

    In [2]: red.help()
    0 -----------------------------------------------------
    AssignmentNode()
      # identifiers: assign, assignment, assignment_, assignmentnode
      operator=''
      target ->
        NameNode()
          # identifiers: name, name_, namenode
          value='a'
      annotation ->
        None
      value ->
        IntNode()
          # identifiers: int, int_, intnode
          value='1'

    In [3]: red.find('NameNode').help()
    NameNode()
      # identifiers: name, name_, namenode
      value='a'

    In [4]: red.find('namenode').help()  # identifiers are not case sensitive
    NameNode()
      # identifiers: name, name_, namenode
      value='a'

    In [5]: red.find('name')
    Out[5]: a

This will recursively travel the tree and return the first node of that type.

You can also specify attributes of the node that you want to match:

    In [6]: red = RedBaron("a = b")

    In [7]: red.find('name').help()
    NameNode()
      # identifiers: name, name_, namenode
      value='a'

    In [8]: red.find('name', value='b').help()
    NameNode()
      # identifiers: name, name_, namenode
      value='b'

If you don’t want a recursive approach but only on the first level on the current node or node list, you can pass `recursive=False` to `.find()`.

Like BeautifulSoup, RedBaron provides a shorthand to `.find()`, you can write the name of the target as an attribute of the node and this will do a `.find()` in the same fashion:

    In [9]: red = RedBaron("a = b")

    In [10]: red.find('name')
    Out[10]: a

    In [11]: red.name
    Out[11]: a

You might have noticed that some identifiers end with a `_`, those are for the case where the identifier might be a Python reserved keyword like `if`, or `while` for example.

Be aware that if you do a `red.something_that_can_be_a_node_identifier` and this is also not an attribute of a node, this will raise an `AttributeError`.

## .find_all()

`.find_all()` is extremely similar to `.find()` except it returns a node list containing all the matching queries instead of a single one. Like in BeautifulSoup, `__call__` is aliased to `find_all` (meaning that if you try to call the node this way `node(some_arguments)` this will call `.find_all()` with the arguments).

    In [12]: red = RedBaron("a = b")

    In [13]: red.find_all("NameNode")
    Out[13]:
    0   a
    1   b

    In [14]: red.find_all("name")
    Out[14]:
    0   a
    1   b

    In [15]: red.findAll("name")
    Out[15]:
    0   a
    1   b

    In [16]: red.findAll("name", value="b")
    Out[16]: 0   b

    In [17]: red("name", value="b")
    Out[17]: 0   b

`.find_all()` also supports the option `recursive=False`.

## Advanced querying

`.find()` and `.find_all()` offer more powerful comparison mean than just equality comparison.

### Callable (lambda)

Instead of passing a string to test properties of the identifier of a node, you can pass a callable, like a lambda. It will receive the value as first argument:

    In [18]: red = RedBaron("a = [1, 2, 3, 4]")

    In [19]: red.find("int", value=lambda value: int(value) % 2 == 0)
    Out[19]: 2

    In [20]: red.find_all("int", value=lambda value: int(value) % 2 == 0)
    Out[20]:
    0   2
    1   4

    In [21]: red.find(lambda identifier: identifier == "comma")
    Out[21]: ,

    In [22]: red.find_all(lambda identifier: identifier == "comma")
    Out[22]:
    0   ,
    1   ,
    2   ,

### Regex

Instead of passing a string to test properties of a node, you can pass a compiled regex:

    In [23]: import re

    In [24]: red = RedBaron("abcd = plop + pouf")

    In [25]: red.find("name", value=re.compile("^p"))
    Out[25]: plop

    In [26]: red.find_all("name", value=re.compile("^p"))
    Out[26]:
    0   plop
    1   pouf

    In [27]: red.find(re.compile("^n"))
    Out[27]: abcd

    In [28]: red.find_all(re.compile("^n"))
    Out[28]:
    0   abcd
    1   plop
    2   pouf

Having to compile regex is boring, so you can use this shorthand syntax instead (prefixing a string with “re:”):

    In [29]: red = RedBaron("abcd = plop + pouf")

    In [30]: red.find("name", value="re:^p")
    Out[30]: plop

    In [31]: red.find_all("name", value="re:^p")
    Out[31]:
    0   plop
    1   pouf

    In [32]: red.find("re:^n")
    Out[32]: abcd

    In [33]: red.find_all("re:^n")
    Out[33]:
    0   abcd
    1   plop
    2   pouf

### Globs

Same than in a shell, you can use globs by prefixing the string with “g:”:

    In [34]: red = RedBaron("abcd = plop + pouf")

    In [35]: red.find("name", value="g:p*")
    Out[35]: plop

    In [36]: red.find_all("name", value="g:p*")
    Out[36]:
    0   plop
    1   pouf

    In [37]: red.find("g:n*")
    Out[37]: abcd

    In [38]: red.find_all("g:n*")
    Out[38]:
    0   abcd
    1   plop
    2   pouf

In the background, the comparison is done using the fnmatch module of the standard lib.

### List or tuple

You can pass a list as a shorthand to test if the tested attribute is in any of the member of the list/tuple:

    In [39]: red = RedBaron("foo\nbar\nbaz")

    In [40]: red.find("name", value=["foo", "baz"])
    Out[40]: foo

    In [41]: red.find("name", value=("foo", "baz"))
    Out[41]: foo

    In [42]: red("name", value=["foo", "baz"])
    Out[42]:
    0   foo
    1   baz

    In [43]: red("name", value=("foo", "baz"))
    Out[43]:
    0   foo
    1   baz

    In [44]: red = RedBaron("1\nstuff\n'string'\n")

    In [45]: red.find(["int", "string"])
    Out[45]: 1

    In [46]: red(["int", "string"])
    Out[46]:
    0   1
    1   'string'

### *args and default value

You can also pass as many callable as args (without giving it a key) as you want, those callables will receive the node itself as first argument (and must return a value that will be tested as a boolable):

    In [47]: red = RedBaron("a = [1, 2, 3, 4]")

    In [48]: red.find("int", lambda node: int(node.value) % 2 == 0)
    Out[48]: 2

    In [49]: red.find_all("int", lambda node: int(node.value) % 2 == 0)
    Out[49]:
    0   2
    1   4

    In [50]: red.find("int", lambda node: int(node.value) % 2 == 0, lambda node: int(node.value) == 4)
    Out[50]: 4

To ease the usage of RedBaron in ipython (and in general), you can pass any of the previous testing methods (except the lambda) as the first argument of *args, it will be tested against the default testing attribute which is the “value” attribute by default. This mean that: `red.find("name", "foo")` is the equivalent of `red.find("name", value="foo")`.

If the default tested attribute is different, it will be shown in `.help()`. For now, the 2 only cases where this happens is on class node and funcdef node where the attribute is “name”.

    In [51]: red = RedBaron("foo\ndef bar(): pass\nbaz\ndef badger(): pass")

    In [52]: red.find("name", "baz")
    Out[52]: baz

    In [53]: red.find("def", "bar")
    Out[53]: def bar(): pass

    In [54]: red.find("def").help()
    DefNode()
      # identifiers: def, def_, defnode, funcdef, funcdef_
      # default test value: name
      async=False
      name='bar'
      return_annotation ->
        None
      decorators ->
      arguments ->
      value ->
        * PassNode()
            # identifiers: pass, pass_, passnode

### Next

To learn how to modify stuff in RedBaron read Modifying.