Author Topic: Python beautifier  (Read 3877 times)

eda

  • Community Member
  • Posts: 17
  • Hero Points: 0
Python beautifier
« on: September 30, 2013, 01:24:04 pm »
Is it possible to get Slick Edit to format Python code according to the rules

  http://docs.python.org/2/tutorial/controlflow.html#intermezzo-coding-style

It seems it does has beautifier for Python.

Matthew

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 990
  • Hero Points: 44
Re: Python beautifier
« Reply #1 on: October 01, 2013, 04:25:34 pm »
We don't have a full-featured beautifier for Python. We do have new beautifiers for most of the C-like languages, and we will be incorporating more languages into this new framework with each release.
For now you can use the Python language options to make sure that indents are using 4 spaces, and not tabs.(4 spaces is already the out-of-the-box default for Python) You can also set the line truncation length to prevent creating lines over 79 columns wide.

JakeMontgomery

  • Community Member
  • Posts: 79
  • Hero Points: 8
Re: Python beautifier
« Reply #2 on: July 20, 2014, 05:16:35 pm »
This is an old post, but in case anyone else wants to beautify python, I will post my workaround. I also wanted to be able to "beautify" python, in accordance with the pep8 standards. Turns out there are a number of python tools that do just that. So I chose autopep8 (https://pypi.python.org/pypi/autopep8/). I use the SE function filter_command() to transform the current buffer using autopep8. (For some reason,  filter_command() is not documented in the SE help, so use at your own risk.) The macro I use is:

Code: [Select]
/**
 * Runs autopep8 on the current buffer, then replaces the current file contents
 * with the autopep8 version.
 */
_command void apply_autopep8() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
   _str pep8_path = get_env('VSPROJ_AUTOPEP8_PATH');
   if (pep8_path == "")
   {
      _message_box('No path for autopep8. Set VSPROJ_AUTOPEP8_PATH');
      return
   }
   _str autopep_settings = get_env('VSPROJ_AUTOPEP8');

   if (p_LangId != "py")
   {
      _message_box('Not a python file');
      return
   }
   _str command = pep8_path " " :+ autopep_settings :+ " -";
   select_all()
   filter_command(command);
   deselect()
}


This requires that the VSPROJ_AUTOPEP8_PATH environment variable contain the full path of the auopep8 executable, and the VSPROJ_AUTOPEP8 environment variable contain any options for autopep8 (it may be empty.) You can set these environment variables on a per-project basis in the "Project->Project Properties->Open" OS settings, or some other way. You could also replace them with hard coded values.

This macro does what I want, which is 'beautify' the whole file, but there is a lot of room for improvement. I hope others find it useful.



Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6582
  • Hero Points: 511
Re: Python beautifier
« Reply #3 on: July 21, 2014, 11:16:52 am »
I took a quick look at autopep8. Does it have all the options you need. It looks like it's missing individual control over things.