PyCQA / redbaron

Bottom-up approach to refactoring in python
http://redbaron.pycqa.org/
694 stars 74 forks source link

[Question, feature request?] After modifying the tree, how to print elements back ? #105

Closed vindarel closed 8 years ago

vindarel commented 8 years ago

Hi, So I'm using RedBaron to help me edit code, that's pretty exciting. My difficulty now is to re-write the tree I modified, sometimes just nodes. It doesn't seem to be a method on redbaron objects for that.

For instance, let's take the code

def foo(self, key=[], second): pass

I parse it, I sort the arguments and I want to print them back. It's easy with simple arguments that have no value, but with "key=[]" I don't find its textual representation, I find its "first_formatting" that is a python list.

{'first_formatting': [], 'type': 'def_argument', 'target': {'type': 'name', 'value': 'key'}, 'value': {'type': 'name', 'value': 'val'}, 'second_formatting': []}

So I could treat this "[]" as a special case and return the string "[]", but how will I handle a list with something in it, or a crazy argument with nested lists ?

Hope I was clear; thanks !

Psycojoker commented 8 years ago

Hello,

Are you looking for something like that? http://redbaron.readthedocs.io/en/latest/basics.html#dumps-transform-the-tree-into-source-code

Not sure I've fully understood you question ^^'

Cheers,

vindarel commented 8 years ago

Heeey, I knew (vaguely) about it, but comes a moment where I don't manipulate RedBaron objects anymore but lists of dicts, without .dumps to help me. Your answer makes me think about my algorithm though.

This is what I am doing.

txt = "def rst(**kwargs, first): pass" # I want to sort the arguments and print them back  
fst = red.fst()[0]
args = fst['arguments']
args        # prints: txt = def rst(**kwargs, first): pass  : OK  
print args 
# [{'formatting': [], 'type': 'dict_argument', 'value': {'type': 'name', 'value': 'kwargs'}}, {'first_formatting': [], 'type': 'def_argument', 'target': {'type': 'name', 'value': 'first'}, 'value': {}, 'second_formatting': []}]

sorted_args = my_custom_sort(args)  # here I must use a new name that doesn't have a link to fst['arguments']. I got confused by that, because then "args" will always show the same__repr__. Anyway.
print sorted_args # looks OK, it's just a list of dicts. You notice that "kwargs" is last.
# [{'first_formatting': [], 'type': 'def_argument', 'target': {'type': 'name', 'value': 'first'}, 'value': {}, 'second_formatting': []}, {'formatting': [], 'type': 'dict_argument', 'value': {'type': 'name', 'value': 'kwargs'}}]
# but those dicts don't have RedBaron's dumps. 
sorted_args   # it does not print the nice output like "args" on line 4

So, the last "sorted_args" is just a list of dicts. How can I get their textual repr ? I may need to create a new RedBaron object and give it the sorted_args… will read the doc then.

Is it clearer now ? Any hint will be appreciated :)

vindarel commented 8 years ago

Ok I'm finding a way with a new RedBaron object and setting its arguments, like shows the doc. Thx for listening ! I'll tell you if/when I come up with something cool…

vindarel commented 8 years ago

FYI, that's what I wanted to do. Already much handy for me. Will try to build upon it.