Hello Kay,

> for my coding conventions, which are a bit unusual, to me e.g. this is the
> correct function call, if every parameter is passed by keyword argument,
> and the "=" signs are aligned on the maximum level. I had looked into
> lib2to3, and found it terribly complex to work with visitors, to pick up
> enough state information, so I dropped my idea to make an automatic source
> code formatter for my preferred style.
> 
> 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".

As an example: I'm doing this very approach right now for pyfmt (which
is a pretty formatter for python code, like gofmt basically) (WARNING:
project is not ready yet). If you need inspiration on how to do that,
you can find the code here:

Old (but easier to understand but less flexible) approach: 
https://github.com/Psycojoker/pyfmt/blob/cf97f25138e866fb70fed7414c46801c92cbbd6e/pyfmt.py
New approach I'm working on: 
https://github.com/Psycojoker/pyfmt/blob/5299a3f2233264ced5158a29dd1cb1d1586bfd81/pyfmt.py#L136-566

The new approach is using this data structure (which describes the
order and content of every node): 
https://github.com/Psycojoker/baron/blob/master/baron/render.py#L114
Documentation: https://baron.readthedocs.org/en/latest/technical.html

> [...]
> 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.

So, my technical opinion on the subject:
* yes this is totally possible and quite easy to do, be sure to use
  this data structure if you want to do it yourself
  https://github.com/Psycojoker/baron/blob/master/baron/render.py#L114
  (documentation: https://baron.readthedocs.org/en/latest/technical.html)
* yes, there is quite a lot of chances that performance will take an hit
* I will eventually do it (a baron <-> xml convertor lib), but this
  isn't very high in my todolist
* 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.

Have a nice weekend,

-- 

Laurent Peuch -- Bram
_______________________________________________
code-quality mailing list
code-quality@python.org
https://mail.python.org/mailman/listinfo/code-quality

Reply via email to