LineProxyList usage examples

In [1]: red = RedBaron("def a():\n    pif\n\n    paf\n    pouf\n")

Please refer to python list documentation if you want to know the exact behavior or those methods (or send a patch to improve this documentation).

append

In [2]: red
Out[2]: 
0   def a():
        pif
    
        paf
        pouf
    

In [3]: red[0].value.append("")

In [4]: red[0].value.append("stuff")

In [5]: red
Out[5]: 
0   def a():
        pif
    
        paf
        pouf
    
        stuff
    

In [6]: red[0].value
Out[6]: 
0   pif
1   '\n    '
2   paf
3   pouf
4   '\n    '
5   stuff

Appending an empty string or a string containing a n adds a new line.

insert

In [7]: red
Out[7]: 
0   def a():
        pif
    
        paf
        pouf
    
        stuff
    

In [8]: red[0].value.insert(1, "\n")

In [9]: red[0].value.insert(1, "print(caribou)")

In [10]: red
Out[10]: 
0   def a():
        pif
        print(caribou)
    
    
        paf
        pouf
    
        stuff
    

In [11]: red[0].value
Out[11]: 
0   pif
1   print(caribou)
2   '\n'
3   '\n    '
4   paf
5   pouf
6   '\n    '
7   stuff

Inserting an empty string or a string containing a n adds a new line.

extend

In [12]: red
Out[12]: 
0   def a():
        pif
        print(caribou)
    
    
        paf
        pouf
    
        stuff
    

In [13]: red[0].value.extend(["a", "\n", "if a:\n    pass", "stuff"])

In [14]: red
Out[14]: 
0   def a():
        pif
        print(caribou)
    
    
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
        stuff
    

In [15]: red[0].value
Out[15]: 
0   pif
1   print(caribou)
2   '\n'
3   '\n    '
4   paf
5   pouf
6   '\n    '
7   stuff
8   a
9   '\n    '
10  if a:
            pass
    
11  stuff

Extending with an empty string or a string containing a n adds a new line.

pop

In [16]: red
Out[16]: 
0   def a():
        pif
        print(caribou)
    
    
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
        stuff
    

In [17]: red[0].value.pop()

In [18]: red
Out[18]: 
0   def a():
        pif
        print(caribou)
    
    
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [19]: red[0].value
Out[19]: 
0   pif
1   print(caribou)
2   '\n'
3   '\n    '
4   paf
5   pouf
6   '\n    '
7   stuff
8   a
9   '\n    '
10  if a:
            pass
    

In [20]: red[0].value.pop(3)

In [21]: red
Out[21]: 
0   def a():
        pif
        print(caribou)
    
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [22]: red[0].value
Out[22]: 
0   pif
1   print(caribou)
2   '\n    '
3   paf
4   pouf
5   '\n    '
6   stuff
7   a
8   '\n    '
9   if a:
            pass

__getitem__

In [23]: red
Out[23]: 
0   def a():
        pif
        print(caribou)
    
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [24]: red[0].value
Out[24]: 
0   pif
1   print(caribou)
2   '\n    '
3   paf
4   pouf
5   '\n    '
6   stuff
7   a
8   '\n    '
9   if a:
            pass
    

In [25]: red[0].value[2]
Out[25]: '\n    '

__setitem__

In [26]: red
Out[26]: 
0   def a():
        pif
        print(caribou)
    
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [27]: red[0].value[2] = "plop"

In [28]: red
Out[28]: 
0   def a():
        pif
        print(caribou)
        plop
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [29]: red[0].value
Out[29]: 
0   pif
1   print(caribou)
2   plop
3   paf
4   pouf
5   '\n    '
6   stuff
7   a
8   '\n    '
9   if a:
            pass

remove

In [30]: red
Out[30]: 
0   def a():
        pif
        print(caribou)
        plop
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [31]: red[0].value.remove(red[0].value[2])

In [32]: red
Out[32]: 
0   def a():
        pif
        print(caribou)
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [33]: red[0].value
Out[33]: 
0   pif
1   print(caribou)
2   paf
3   pouf
4   '\n    '
5   stuff
6   a
7   '\n    '
8   if a:
            pass

index

In [34]: red
Out[34]: 
0   def a():
        pif
        print(caribou)
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [35]: red[0].value
Out[35]: 
0   pif
1   print(caribou)
2   paf
3   pouf
4   '\n    '
5   stuff
6   a
7   '\n    '
8   if a:
            pass
    

In [36]: red[0].value.index(red[0].value[2])
Out[36]: 2

count

In [37]: red
Out[37]: 
0   def a():
        pif
        print(caribou)
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [38]: red[0].value
Out[38]: 
0   pif
1   print(caribou)
2   paf
3   pouf
4   '\n    '
5   stuff
6   a
7   '\n    '
8   if a:
            pass
    

In [39]: red[0].value.count(red[0].value[2])
Out[39]: 1

len

In [40]: red
Out[40]: 
0   def a():
        pif
        print(caribou)
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [41]: red[0].value
Out[41]: 
0   pif
1   print(caribou)
2   paf
3   pouf
4   '\n    '
5   stuff
6   a
7   '\n    '
8   if a:
            pass
    

In [42]: len(red[0].value)
Out[42]: 9

__delitem__

In [43]: red
Out[43]: 
0   def a():
        pif
        print(caribou)
        paf
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [44]: del red[0].value[2]

In [45]: red
Out[45]: 
0   def a():
        pif
        print(caribou)
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [46]: red[0].value
Out[46]: 
0   pif
1   print(caribou)
2   pouf
3   '\n    '
4   stuff
5   a
6   '\n    '
7   if a:
            pass

in

In [47]: red
Out[47]: 
0   def a():
        pif
        print(caribou)
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [48]: red[0].value[2] in red[0].value
Out[48]: False

__iter__

In [49]: red
Out[49]: 
0   def a():
        pif
        print(caribou)
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [50]: for i in red[0].value:
   ....:     print(i.dumps())
   ....: 
pif
print(caribou)
pouf

    
stuff
a

    
if a:
        pass

__getslice__

In [51]: red
Out[51]: 
0   def a():
        pif
        print(caribou)
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [52]: red[0].value
Out[52]: 
0   pif
1   print(caribou)
2   pouf
3   '\n    '
4   stuff
5   a
6   '\n    '
7   if a:
            pass
    

In [53]: red[0].value[2:4]
Out[53]: 0   pouf

__setslice__

In [54]: red
Out[54]: 
0   def a():
        pif
        print(caribou)
        pouf
    
        stuff
        a
    
        if a:
            pass
    
    

In [55]: red[0].value[2:4] = ["a", "b", "c"]

In [56]: red
Out[56]: 
0   def a():
        pif
        print(caribou)
        a
        b
        c
        stuff
        a
    
        if a:
            pass
    
    

In [57]: red[0].value
Out[57]: 
0   pif
1   print(caribou)
2   a
3   b
4   c
5   stuff
6   a
7   '\n    '
8   if a:
            pass

__delslice__

In [58]: red
Out[58]: 
0   def a():
        pif
        print(caribou)
        a
        b
        c
        stuff
        a
    
        if a:
            pass
    
    

In [59]: red[0].value[2:5]
Out[59]: 
0   a
1   b
2   c

In [60]: del red[0].value[2:5]

In [61]: red
Out[61]: 
0   def a():
        pif
        print(caribou)
        stuff
        a
    
        if a:
            pass
    
    

In [62]: red[0].value
Out[62]: 
0   pif
1   print(caribou)
2   stuff
3   a
4   '\n    '
5   if a:
            pass