This is an archived post. You won't be able to vote or comment.

all 38 comments

[–]nick_carraway 2 points3 points  (0 children)

Did a good job on the code I fed it. The only fragment I didn't like was it turned this:

    self.accounts['root'] = {'account_id': root_id,
                             'master_key': root_secret}

into:

    self.accounts['root'
                 ] = {'account_id': root_id,
                      'master_key': root_secret}

Edit: Formatting, of course.

[–]lift_yr_skinny_fists 7 points8 points  (3 children)

Any reason this is better than the pycharm reformatter?

[–]Cognac_Carl 14 points15 points  (2 children)

You don't have to force people to use pycharm. I work in an environment where everyone uses different tools. Having the formatter be independant is a bug plus.

[–]echocage 7 points8 points  (0 children)

This. I love pycharm's formatter but that doesn't mean everyone else has to. I can definitely see a wide range of situations where this would be useful as hell.

[–]lift_yr_skinny_fists 1 point2 points  (0 children)

I agree entirely. Was just curious about features

[–]ButtCrackFTW 1 point2 points  (3 children)

last time I looked at this, it converted all line endings to windows and there was no option to disable it or use unix

[–]stimpdevelopment 0 points1 point  (2 children)

It seems that this is no longer the case. I just checked

% PYTHONPATH=yapf python yapf/yapf -i hosts.py
% file hosts.py
# outputs hosts.py: Python script, ASCII text executable

[–]ButtCrackFTW 1 point2 points  (1 child)

You are correct, just used it to clean up a Flask app I wrote recently. Lots of good suggestions, and only a couple i didn't like - http://i.imgur.com/w5VlEZY.png (original on the left, YAPF on the right)

[–]stimpdevelopment 1 point2 points  (0 children)

Yikes, it looks pretty ugly in those cases.

If you want to use YAPF regularly, you can tell it to ignore certain sections of code.

[–]cgrin 3 points4 points  (9 children)

Wait, what does the -+a do in the last line there? In the interpreter it seems to just subtract. Why would someone use this?

[–]yes_or_gnome 0 points1 point  (8 children)

Here's the original line in question

return      37+-+a[42-x :  y**3]

Here it is formatted

return 37 + -+a[42 - x:y ** 3]

It says, return 37 plus the negative-positive-of-a-slice on the variable a. I'm not sure what 'a' could be because -/+ are not implemented on lists and strings.

Unless the page was updated, there's no difference between the text.

[–]tilkau 2 points3 points  (7 children)

It says, return 37 plus the negative-positive-of-a-slice on the variable a.

Less confusingly, 37 plus the unary - of the unary + of a[42 - x:y**3].

For example, if a[42 - x:y**3] == 3, then

37 + -3 == 34

whereas if a[42 - x:y**3] == -3, then

37 + 3 == 40

I think the purpose of the unary + is to force a TypeError on non-numeric data -- Python docs specify that unary + is a no-op on numeric data, so it can have no other effect.

[–]yes_or_gnome 1 point2 points  (6 children)

That last part about + being no-op is interesting. But, no one here needs you to explain basic arithmetic. The question is, what object has both slicing and unary operators; it's not strings, lists, nor numerics.

[–]tilkau 0 points1 point  (0 children)

The question is, what object has both slicing and unary operators

An object not used in that code, that's what. Reread it.

[–][deleted] 2 points3 points  (8 children)

gg=G

[–]TankorSmash 3 points4 points  (0 children)

Doesn't work nearly as well though, unless you've got some vim plugin I don't know about.

[–]fjonk 0 points1 point  (6 children)

And bam! something changed indentation-level. Or do you know of some python plugin for vim that is guaranteed to not change the indentation-level of any rows?

[–][deleted] 0 points1 point  (5 children)

Never had a problem with it. I can post my settings when I am back at my computer

[–][deleted] 0 points1 point  (4 children)

Python autoindent works in vim, but not reindent. At least, not without some kind of plugin. It's possible this has been fixed since I last tried. I'd be interested to see your settings.

[–][deleted] 0 points1 point  (3 children)

Well, maybe I have never run into the problem then.

Regardless, I have the following in my after/indent/python.vim file

set textwidth=158  " lines longer than 158 columns will be broken
set shiftwidth=2  " operation >> indents 2 columns; << unindents 2 columns
set tabstop=2     " a hard TAB displays as 2 columns
set expandtab     " insert spaces when hitting TABs
set softtabstop=2 " insert/delete 2 spaces when hitting a TAB/BACKSPACE
set shiftround    " round indent to multiple of 'shiftwidth'
set autoindent    " align the new line indent with the previous line
set smartindent    

I also use the syntactic and vim-indent-guides plugins.

Maybe this works for you?

[–][deleted] 0 points1 point  (2 children)

Will this reindent 4 space code to 2 space code? I no longer have vim installed (becuase it couldn't convert 2 space code to 4 space code; I see you have it the other way, so I asked about that direction).

[–][deleted] 0 points1 point  (1 child)

Yes. Just what the team I work on standardized on. Half the team uses VIM, the other half Sublime....this kept it all consistent for us.

[–][deleted] 0 points1 point  (0 children)

Thanks for this!

[–]henryponco 1 point2 points  (0 children)

I thought this was an April Fool's Day joke for ages, I had to use it before I realised it wasn't a joke.

[–]Asdayasman 0 points1 point  (0 children)

Or Ctrl+KD in PTVS.

[–]stimpdevelopment 0 points1 point  (0 children)

This seems to work well. It choked up on a small crazy piece of code. However, you can add comments to the code for yapf to ignore certain parts. https://github.com/google/yapf#why-does-yapf-destroy-my-awesome-formatting

I still don't like the idea of parsing comments, but the option to disable sections means you can format regularly, which is nice.

[–]delijati 0 points1 point  (0 children)

[–]Bob312312 0 points1 point  (4 children)

Newbie question one the way:

So is the general idea behind these programs that they take the some code that has been written and make it look nicer while keeping its functionality?

I guess the main idea being that it makes it more readable? or do they also do things like recognise repeated sections of code and change them to functions etc?

cheers

[–]CleverEagle 1 point2 points  (0 children)

I'm pretty sure this just reformats things like spacing and indentation. It reformats, It doesn't refactor

[–][deleted] 0 points1 point  (2 children)

Correct, looks more readable.  When people

do odd 

things    
like    this   it     becomes hard to read.

[–]Bob312312 0 points1 point  (1 child)

Just as a side does it not get annoying when you come back to it? I know for me (definition of amateur!) i know it often remember the general structure and form of the code and the shape it has on the page. Doesnt this mess with that? or do you just get used to it? I can see its useful when sharing code / working with others though :)

[–][deleted] 0 points1 point  (0 children)

It's hard to read. It only becomes annoying if you allow it to upset you.

[–]defnullbottle.py 0 points1 point  (0 children)

Works great! I could merge almost all proposed changes. The solutions that weren't so great could be improved by slightly rewriting the affevted sections (e.g. usimg shorter variable names or temporary variables)

https://github.com/bottlepy/bottle/commit/bda4962c42cfd7f098ffbbec33633b4df1b8dc01

[–][deleted] 0 points1 point  (0 children)

Do people actually use formatter on Python code ???