How to change the way pylint lints the comments - python

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.

Related

How to disable squiggly lines underline in VSC 1.49.2?

Please help, it's driving me nuts. I tried following another older stack overflow post and it didn't work. I just declare a new variable and I already get squiggly line - what an annoying feature. Using python by the way on VSC.
Something like this:
My json.settings look like this:
Based on the information you provided, I reproduced the problem.
The reason is that Python's code analysis tool Pylint detects that the variable "words_to_remove" is not used, so it displays "Pylint(Unused variable)".
Solution:
Complete the code. for example:
Turn off the prompt of Pylint.
1). Close the "Unused variable" prompt of pylint:
Please use "python.linting.pylintArgs": ["--disable=W0612"], in settings.json:
2). Close Pylint.
Use "python.linting.enabled":false , in settings.json.
In addition, Pylint is an excellent Python code analysis tool. It will provide good suggestions on the format and syntax of the code we edit and it does not affect the execution of the code.
Therefore, if you want to turn off Pylint related prompts to remove the wavy lines, it is better to set it off after the code can be executed.

Make Pylint care about blank lines

I am not a stickler for most things pep-8, but certain things I personally prefer when writing code (as opposed to for work, where I would adhere to the style or lack thereof of the existing code base).
One thing I personally tend to follow is pep-8's suggestion about blank lines:
Surround top-level function and class definitions with two blank
lines.
Method definitions inside a class are surrounded by a single blank
line.
However, I have not been able to get Pylint to warn me when I violate this. I don't see anything that seems relevant disabled in my .pylintrc, and I have not been able to figure out if this is possible in Pylint, and if so, how to enable it.
Based on this answer, it looks like there are certain aspects of pep-8 that Pylint does not (or did not at the time) cover, but I have not been able to ascertain whether this is the case for blank lines.
Is it possible to have Pylint warn about blank lines (too many/not enough) without writing custom extensions?
As mentioned in this other answer, E301 and E303 doesn't seem to be a thing in pylint (yet?).
One alternative would be to use the pycodestyle (previously: pep8) tool directly, which would allow you to check for blank lines.
Hopefully you'll like it as much as pylint, despite maybe being a little bit less configurable.

Is it possible to insert import statements with jedi-vim?

I've just started looking at the Vim jedi plugin, and it seems pretty impressive. One feature of some of the Java IDEs I've used is the ability to automatically add required imports. Can Jedi do that? For example, if I enter a line such as
arg1 = sys.argv[1]
and then invoke some Jedi command, is it possible for the plugin to automatically insert an import sys line at the top of the source file (if sys is not already being imported)?
I've looked through the Jedi help, and can't see anything like this - but it's possible I missed something. Alternatively, is there another Vim plugin that would do this? (It needs a certain level of understanding of Python syntax to get it right, which is why I looked to Jedi to be able to do it).
Currently Jedi doesn't do refactoringing. This includes import additions. There's an issue for the whole subject: https://github.com/davidhalter/jedi/issues/667.
It's not that easy to implement this command with good performance. However any help is appreciated. :)
FIY, I've defined a generic import feature that can be used on demand in lh-dev. I use it from my C&C++ suite, and from my snippet engine (mu-template).
So far I don't parse anything to add the missing import/include statements. This part would be complex as Dave said. Instead my snippets know which files need to be imported/included and import/include them if not already imported/included.
It's far from being perfect, but it's a start. mu-template provides a hook to do stuff at the start of the file after the snippet has been expanded, this is where I call lh-dev function. If other snippet engines provide similar hooks, you should be able to call lh#dev#import#add() from your snippets.
Here a proof of concept snippet for Python (I seldom program in Python, and don't have many snippets for it): https://github.com/LucHermitte/mu-template/blob/master/after/template/python/path-exists.template

pydev - can someone please explain these errors

I am developing using PyDev in Eclipse. I have put in some comments in my code. I get wavy red lines underneath certain words. The program runs fine and there are no warnings mentioned. So what is the meaning of these wavy lines.
e.g.
#!/usr/bin/python - I get the line under usr and python
# generated by accessing registry using another script written in VBScript.
# The scripts can do the follwing things.
- I get wavy lines under the words registry and following.
I need these comments as I may run the module on its own later.
Thanks for the help.
Neil speaks the truth, except for telling you to turn it off -- spell check in comments is quite helpful -- those who come after will thank you for ensuring they can read your comments without trying to decode random spelling errors.
The lines are simply pointing out words your IDE thinks are spelled wrong. I don't know why it doesn't understand "registry", but you have, in fact, misspelled "following" (as "follwing"). Fix the latter, ignore the former (or add it to the dictionary, don't remember if there's a convenient mechanism for that There is! See Macke's helpful comment below.).
Might be this is just a spellchecker. You have a typo "follwing" instead of "following".

How do you avoid leaving debugger print/pdb statements in python open source apps when you're debugging them and just finish?

Sometimes when developing using open source software, you need to read it's source code (specially zope/plone). A lot of times I need to write print statements, or debug calls (import pdb), or comment try/except clauses, you name it.
Sometimes I have a lot of files opened when trying to find an issue, and sometimes I forget to remove these print/debug alterations.
So, my question is: how do you keep yourself organized when doing it? Do you write "TODOs" along the modifications and search for them later, do you keep everything opened in your editor and when you find what you were looking for you just revert the files (This approach isn't useful when you're searching for a really big problem that needs days, you need to turn off your computer and return the other day)? Or you just don't do nothing since print statements in development environment is nothing to worry about?
I'm using Vim. I'm just interested to know how other programmers treat this issue.
I used to run into that problem a lot. Now, as part of my check-in process, I run a find/grep script combo that looks for my debugging statements. The only caveat is that I must keep my added debugging statements consistent so grep can find them all.
something like this:
## pre-checkin_scan.bin
find . -name "*.py" -exec grep -H --file=/homes/js/bin/pre-checkin_scan_regexp_list.grep {} \;
## pre-checkin_scan_regexp_list.grep
## (The first pattern is to ignore Doxygen comments)
^##[^#]
pdb
^ *print *( *" *Dbg
^ *print *( *" *Debug
^ *debug
In case of my own projects, the source code is always in version control. Before committing, I always check the graphical diff so that I can see what has changed, what the commit message should be and whether I can split up into smaller commits. That way, I almost always recognize temporary garbage like print statements. If not, I usually notice it shortly afterwards and can do an uncommit if I haven't yet pushed (works for DVCS like git and bzr, not with subversion).
Concerning problems that take multiple days, it's just the same thing. I don't commit until the problem is solved and then look at the diff again.
A text editor that allows editing within the graphical diff view would be really helpful in these cases, but I'm mostly using Eclipse, which doesn't support that.
Well +1 for starting this discussion. Yes sometime this happen to me. I left those pdb and commit the code to the central code base, git. I use 'emacs'. So, Before commit the code I usually search for pdb in the file. But it is hectic checking each file.So, Before committing the code I usually check the diff very carefully. I am also finding the better way to resolve this issue.
I also develop Python with Vim. I have never had to substantially modify the source code for debugging. I do sometimes put debugging print statements, and I have the habit of putting "# XXX" after every one. Then when I want to remove them (before a commit), and just search for the XXX and delete those lines.
For exceptions, I have arranged to run my code in the Vim buffer with an external interpreter that is set up to automatically enter the debugger on any uncaught exception. Then I'm placed automatically in the code at the point the exception occured. I use a modified debugger that can also signal Vim (actually GTK Gvim) to open that source at that line.
Caught exceptions should report meaningful errors, anyway. It is considered by many to be bad practice to do things like:
try:
... some code
except:
handle everything
Since you probably aren't actually handling every possible error case. By not doing that you also enable the automatic debugging.
I can give you three suggestions:
Do not remove debugger statements. By this, I mean leave them in, but make them conditional on being in debug mode:
# Set this to True to enable Debug code
XYZ_Debug = False
if XYZ_Debug:
do_debugging()
Oh, and if the debugging code is just to print things out, you should get familiar with logging (PyMOTW). If you are using logging, you could:
import logging
# Set this to True to enable debug
XYZ_Debug = False
log = logging.getLogger("XYZ")
log.setLevel(logging.DEBUG if XYZ_Debug else logging.INFO)
log.debug("debug output")
Put the same unique tag (in a comment) after each line, or near each block:
do_debug_code() # XYZZY
I then use Emacs' Ibuffer feature, mark all Python buffers then search for occurrences of this tag. Using some combination of find/grep/sed as in other answers would work as well.
If you are using Mercurial and know Mercurial Queues (or might want to learn them), maintain the debug code as a patch in your queue. When you are ready for "production"; or push of the current changes; pop the patch containing the debug code and go. You could achieve something like this outside of version control with diff and patch.

Categories