Indentation Error: Vim creates invisible indents - python

As you may be able to see (from the screenshot below) there is an additional level of indentation that I did not make and there are no tabs or spaces corresponding to it. Retyping the code does not fix this. The original file was cloned from github. I am using gVim 7+ on Ubuntu 12.04 and to the best of my knowledge the original file was also written on a Linux machine.
Here's the screenshot:
I believe I have indented my code correctly but the indentation guides are showing a new indentation level and Python emits indentation errors.
I am not very clear with line endings and encodings.

I cloned https://github.com/flazzarini/conkyconfig/blob/master/tools/anowplaying.py from git://github.com/flazzarini/conkyconfig.git and viewed the file in Vim with...
:set listchars=tab:>-,trail:-,eol:$ list
...to highlight tabs, spaces and other whitespace differently.
The original author used tabs for indentation. I suspect you are using using spaces but cannot tell from your screenshot.
Invoking Python with the following option...
-tt -- issue errors about inconsistent tab usage
...will help you keep your indentation consistent.
You should probably set noexpandtab tabstop=8 shiftwidth=8 (perhaps in a modeline) to be consistent with the original source. That way, should you ever submit a patch, only the lines you've really added or updated will be submitted.

Related

How to change the way pylint lints the comments

I need to have this line in the beginning of a python file:
#script_exec_time: 500
There should not be any space between the # and script_exec_time. This is because the python file is parsed by another script and it will not consider the script_exec_time if there is a space.
I cannot modify the script that parses my python file because I do not have access to it.
Now when I lint using pylint, it automatically gives a space between the # and script_exec_time.
I tried using other linters like flake8, but even they do the same thing.
I still want to use the linter because it makes it much easier to code.
Is there any way to modify pylint settings so that it ignores comments from it's automatic formatting? Or is there any workaround I could use here?
Edit: I tried the same thing by disabling the linter. The problem persisted. It was not related to linter I think. Anyway I found a workaround and I am posting that as an answer.
As Vaibhav Vishal pointed out in his comment, linters like Pylint and flake8 do not reformat code, they just flag code that triggers a lint rule.
You most likely have a formatter installed and configured. See the formatting docs on how this is done and basically do the reverse. :)
All I did was to wrap the original comment in side a block comment, like so:
#script_exec_time:5000
'''
#scrript_exec_time:5000
'''
Vscode will not format anything that is there inside the block comments.

How to edit indented codes in python?

Kinda new to python, coming from a C/C++ development experience. Not to rant, but the indentation and spacing things are kinda becoming the bane of my life. So here is the problem. Take this code snippet, typed sequentially, never erasing or inserting any edit between existing codes.
while condition:
line 1
line 2
line3 #outside the loop
It is working fine. But, suppose, I want to add another instruction inside the loop. (Editing using jupyter notebook and sublime, on Linux) So I add the line as
while condition:
line 1
line 1.5
line 2
line3 #outside the loop
That is when the problem starts. No matter what I try, I can never make it work. It is giving me errors like
line 2
^
IndentationError: unexpected indent
or some variations depending on the circumstances. It seems somehow adding a single line within a large and complicated nested block structure somehow totally messes up the whole thing.
I do not insert the spaces/indents manually, I just let my editor take care of that. When I hit enter, the cursor moves down to the next line, indented at the same level as the previous line (unless the previous line ends with a :, in which case it adds another indentation).
When I need to end a while loop or if block, I just hit the backspace to retract one indentation level. The blocks look perfectly okay by eye inspection, but have no clue how the interpreter sees it.
Any help, on what am I doing wrong, or at least what are the good practices to avoid this problem? Particularly, adding even a single line within an existing nested block seems nigh impossible.
You may mix of using space and tab indentation. You can use either tab or space but not both. However, it's recommend to use space as The PEP8 Python Style Guide
If you are using Visual Studio Code, you can install pylint extension to easily detect such kind of these errors. Something like this:
pylint output
Sublime text editor uses tabs for indentation by default. If you initially used spaces for indentation and edit later, you would have a mix of both spaces and tabs for indentation, which is what is causing the indentation error. This is because Python 3 disallows mixing the use of tabs and spaces for indentation.
The PEP8 Python Style Guide recommends using 4 spaces per indentation level. Go back and replace the tabs with spaces, and the code will work just fine.
(PS: It's easy enough to change the default indentation settings for Sublime)

`pylint`: how do you enable errors for tabs or spaces indentation?

I am trying to enforce some simple python formatting rules. I found pylint and I have been very happy. However one of the more simple formatting checks I need to enforce is: tabs-only or spaces-only indentation.
In pylint, how do you enable errors or warnings for tabs or spaces indentation?
I see that pylint has w0311 "Used when an unexpected number of indentation's tabulations or spaces has been found". But w0311 does not enforce tabs-only or spaces-only... it still supports tabs or spaces.
I need all my python files to be only one type of indentation.
(
p.s. If you are curious how I use pylint to enforce my rules. I have a shell script that runs pylint and I use set -o errexit and this is hooked in with the build. So if pylint finds something it exits with nonzero value and causes the build to fail.
)
Well what you are looking for might be this:
mixed-indentation (W0312):
Found indentation with %ss instead of %ss Used when there are some mixed tabs and spaces in a module.
Which is raised when:
Description
Used when there are some mixed tabs and spaces in a module.
Explanation
Python interprets tabs and spaces differently, so consistent
indentation is critical to the correct interpretation of blocks in
Python syntax.
This warning is raised when a mix of both spaces and tabs is used in
indentation—or more precisely, when an indent is detected that is not
consistent with the indent-string option. By default, indent-string is
set to four spaces, the style of indentation recommended in PEP 8.
Edit #1:
Pylint does not have an option that I know of (and as of the moment) to "enforce" only tabs/spaces in your coding style. The above mentioned W0312 exists to warn for indentation inconsistencies on your code.
There is a trick that I use to enforce indentation: Open your project on PyCharm (works on the community edition also which is free) if the IDE detects inconsistencies it will warn you about them and it will give you an option to change indentation of the current file, or keep it as it is!

why Python string changes to italic looking in the Canopy editor when moved to new line?

This is probably an editor snag. But thought to check in case, as I do not know Python and just started to learn it.
Comparing the following
And I simply move the second field one line below by hitting return
The code in the second line became italic. I thought at first I have some syntax error or the editor is warning me about something, but there is no problem and the code run just as before. This only happens when the """ use. With a single " then the code does not become italic.
Do you think this is just an editor issue, or is the editor trying to tell something I should not be doing?
Specs:
Windows 7, Enthought Canopy 1.5.4 (64 bit)
code in plain text:
data = [r"""123""",r"""456"""]
Without being able to see the documentation for your editor, I would guess that the italics is there to signify that the line is wrapped and is not trying to warn you of anything.
As stated in PEP, "Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent". wrapping lines like you're doing is completely ok and the editor shouldn't be warning you of anything.

How to avoid the \xc2 character or in my code snippets?

I've just started a coding blog, and I'm using the SyntaxHighlighter Evolved Wordpress plugin for Syntax Highlighting of my snippets.
I've just about finished writing a Pythonic post, and wanted to test out my code snippets before publishing.
If you double click code from inside my snippets, the plugin will stop highlighting the code, allowing you to select it as plain text. However, if I copy and paste some Python code from my snippets, it includes \xc2 or chracters in. This causes Python to winge about the encoding:
SyntaxError: Non-ASCII character '\xc2' in file ex2.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
I don't particularly want to be declaring encodings for every single Python snippet I write - and I don't even know if this will solve the issue.
The best solution would of course to be to get my plugin to not use characters in the plain text version. Or would it?
Does anyone have any ideas as to how I can get around this issue?
Ah, got it. Just a bit of poking around in the plugin's source fixed this issue for me...
If you beautify the syntaxhighlighter3/scripts/shCore.js file, then you can see there is a config variable, which includes:
space: " "
All I had to do was change it to space: " " and repack it.
I ran into this problem when python code had been copied from skype. Since I use vim to edit, I went ahead and found all of these by doing this:
:hls
/<space>
This shows where these odd space characters aren't because they're not highlighted.
Yank one of the characters which will store it into register 0.
Use the substitute command and use <ctrl-R> <0> to paste that character into the command prompt.
:%s/<ctrl-R><0>/ /g
It will look like
:%s/ / /g
but when run, it will correct the problem.
NBSP isn't considered whitespace for the purpose of indentation anyways, so you should take a look at what the pre select user script does and mimic it.

Categories