-1

I really hate having spread out code, I am looking at a bunch of long code with parameters and arguments that are taking up way to much space.

    def __init__(self,
                 network,
                 value_coef, 
                 entropy_coef, 
                 lr=None,
                 eps=None,
                 max_grad_norm=None,
                 conv=False):

Seems the guy who wrote it forced a 50 character line limit, I whole heartedly disagree. I would much rather it looked like this.

    def __init__(self, network, value_coef, entropy_coef, lr=None, eps=None, max_grad_norm=None, conv=False):

There is also more nonsense like this which I would like to get rid of.

        if self.conv:
            grid_obs = rollouts.grid_obs[:-1]\
                .view(-1, *rollouts.grid_obs.size()[2:])
            dest_obs = rollouts.dest_obs[:-1]\
                .view(-1, *rollouts.dest_obs.size()[2:])
            obs = (grid_obs, dest_obs)

I am using VS code for the python and am an ex Intellij user and am missing all the built in code formatting code tools. Any one got any tips? I have been looking at autopep8 but it seems they are missing that functionality.

Max Hunter
  • 40
  • 4

1 Answers1

2

First, that's not 50 chars limit but 79 (as per pep8 conventions) and the way you would like to have it wouldn't be pep8 compliant as it's over 100 columns.

So, for the first snippet you can have it the way you don't like it (which is the correct way) or let your formatter know that you want the line-length to be over 79 columns.

For the second snippet you can remove the escape character \ and let the formatter do its job. I don't think it's 'nonsense' as you call it, but feel free to format it differently.

Autopep8 or Black both work very well and they are not missing any functionality.

Provided you installed one or the other, you have to add the proper key/value pair to your settings.json:

"python.formatting.provider": "autopep8" // (or "black")

If you use autopep8, for example, you can specify the line length you want (150 in your case) by adding this to your settings.json file:

"python.formatting.autopep8Args": [
    "--line-length=150"
]

The same goes for black. In that case the value would be:

"python.formatting.blackArgs": [
    "--line-length=150"
]

Formatting with that parameter will wrap your code to that amount.

You can format code with alt+shift+f (on a Mac) or right click on the editor and "Format Document".

Guillermo Brachetta
  • 3,857
  • 3
  • 18
  • 36
  • Firstly thanks for your answer! So if I understand you correctly then all that I need to do is configure pep to have line length to 150 (what I would prefer) and then reformat the file using pep and it should reformat the code to put all the parameters on the same line? Yes I guess removing the \ newline could be done by using a recursive script to replace all the instances of that in the code base. – Max Hunter Jan 31 '21 at 18:07
  • I just updated my answer with the setting you need for the line length. I believe that escape characters will be automatically removed when you format with this line length. – Guillermo Brachetta Jan 31 '21 at 20:48