[code-quality] RedBaron, a bottom-up refactoring lib/tool for python

Kay Hayen kay.hayen at gmail.com
Sun Nov 23 18:08:26 CET 2014


Hello Laurent,

you wrote:

> I think Redbaron might be much better suited to achieve that, it seems.
>
> Yep, this is totally an use case for it. Actually you can go a level
> lower and directly do that with baron by writing a custom "dumper".
>

Before reading this, I already managed to pretty much immediately get
this to work. I checked your pointers now, and I think they wouldn't do
what I want to do, because I don't want to change layout, and when I
want, I would be highly context sensitive.

f(
  a  = 1
  bb = 2
)

and

f(a = 1, bb = 2)

are both OK to me. But in one case, always one space is used, and in
other cases, vertical alignment is done.

Many more things, where I would be context sensitive. I like e.g.
contractions
to be written like this:

a = [
  some_operation(a)
  for a in
  range(b)
  if a != 2
]

unless in cases where I don't do it, typically because there is no merit to
reviewing it.

So, I am more like patching, or at least detecting cases where the existing
white space layout is inconsistent.


> > Performance might be a red herring there, but xpath queries that make me
> > avoid touching nodes already properly formatted, would probably also be
> > faster. Most likely performance is not an issue at all. But xpath can be
> > run time compiled in principle.
> >
> > Let me know what you think of that.
>
> Since Baron (the AST) is lossless, as long as you do a lossless
> convertion between Baron and anything else you can do wathever you
> want. I actually wanted to do that with xml (as a "joke" to be honest
> with you, I don't have a very good appreciation of xml while I
> understand the power of some of its tools) to demonstration this
> property, I was not expecting people to actually request it.
>

I appreciate the power of the xpath query language. It's really good at
saying find me nodes with attributes of that kind, and children with
attributes of another kind, and in a certain range.

It doesn't technically have to written as XML ever. But as a query API
it's fantastic. I have employed it with decoded binary data that systems
output, even wireshark captures, and with internal data recorded.

Given a good tree, that's the thing to have for me. It's much like what
SQL is to databases. A standard for querying.

I am not an XML fanboy otherwise. :-)

 * I will eventually do it (a baron <-> xml convertor lib), but this

>   isn't very high in my todolist
>

This is mainly an idea for you. I am OK with working with the Python
based API. I am mailing you with strangeness as I encountered it,
but it's absolutely workable, and for a Python programmer, not a big
deal.


> * as a practical solution, you will end up with a very low level data
>   structure (just like baron) especially for modifications. Basically,
>   you will face every low level details that I have abstracted for you
>   in RedBaron. If you want to follow this path, I really recommend you
>   to read RedBaron documentation first before doing so to evaluate if
>   the price to pay is worth it.
>

I am not sure, why the XML tree would be all that different from what
"node.help()" does. Surely the first, second, third, etc. formatting will
simply become children, few people will look at.

Yours,
Kay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/code-quality/attachments/20141123/11d3090a/attachment.html>


More information about the code-quality mailing list