Compiling in geany but not in terminal? - python

So I have been messing around with python with openCV the last couple of weeks and it has worked perfectly. My problem is when I took two desperate code script's and put them together it wouldn't compile. (I know Duh python white space (I fixed all of it,"I think")) It threw an error in the terminal with a line number. At this time I was using text editor in Ubuntu so I threw it into Geany to figure out what line. When I got there I couldn't pick out what the error was all of the indentation was that of the original code and it fitted with that of the rest of the loop it was nested in. So I tried to compile it in Geany and it didn't throw errors at all. I find this extremely weird because Geany is only an editor and it relies on outside compilers to compile the code. I am assuming that the terminal is using the same compilers too (though I know it must not now). I though maybe it has something to do with the library openCV because I am not including it in Geany. So I changed the variable name that was throwing things. After that it still threw the same error so I came here confused.
The error message I am getting is
http://imgur.com/1otMyeZ
My code is at
http://pastebin.com/HYKjnyyc
The part that is giving error is here
http://pastebin.com/6TyXs3uc

Your problem still goes back to the white space you were working on. Everything (except line 297 to the end) below:
# THE DEMO PROGRAM STARTS HERE
is indented, including your three global variables, which makes them appear to the compiler as part of the class Target, which takes the variables out of the global scope, so they are not defined. Remove the unnecessary indentation in that area.
EDIT:
Looking back at your code again, You may also have some indentation problems from the
# Function Definitions
at line 88 and down. From the way the functions are indented they appear to be part of the class, but from the way they are called they appear not to be.
You would find writing in python a lot less frustrating if you use a more standard approach to your indentation/white-space handling. Read this PEP for some best practices in that regard.

Related

Suppress undefined name warning in Spyder?

Being in the process of switching from Matlab to Python/IPython/Spyder I ran into the following issue.
In a Matlab script (and I actually did not notice this until I switched to Python) there is no warning if you are using a variable which you did not define in that script.
What I kept doing all the times in Matlab was to have, say, 2 (or more) scripts which I would then execute one after the other. The second script would typically use variables defined in the first, but would not give me any warning.
In Spyder I noticed that the situation is different. In my hypothetical second script, all the variables which are not defined in the second script itself give me a warning (undefined name '...'), which is not nice too see...
Another typical example, would be the following: I have a main script but there is something in this script I wanna look at better. So, without touching the script, I would create another file where I would copy paste a few lines from the script to play a little with them.
In this new file I would be using variables which are in the console already but that are techically unknown to the file itself, so would give me a warning.
So I guess I am simply asking if there a way to suppress this kind of warnings.
Or there might be a deeper question of whether there s a more pythonic way of working, but there I am really not sure about...

better alternative to flake8's "E902 TokenError: EOF in multi-line statement"

I do understand the E902 error, however I do think it's a bit "messy". This is for 2 reasons:
It feels to me that it should be F902 (a failure rather than an error). After all, as I see it in Flake's world, errors are things that are wrong in the style guide (e.g. having 3 newlines in a row), whereas Failures are things that will not make the program run (e.g. referencing a variable before declaration). In other words, when I save a python file in my IDE, I should be happy to ignore any errors (as long as I'm just hacking away), but want to be alerted of anything that is obviously a mistake (and not just me not caring about things). Obviously writing not valid python is even worse than using a variable before declaring it (which in some cases, will actually run).
More importantly, I feel that it's very unhelpful that this error always points to line 1 character 1. This means it's up to me to start looking in the code (of files sometimes 1000s lines long) to the exact spot where I forgot to close a parenthesis, or run another tool (such as just running python on the file) to find the exact line. Also, this means that my editor (VIM) will not show any error for the line I'm working on; I will only see the error if I scroll up all the way.
I guess my questions are threefold:
Why is E902 an error instead of a failure (not super important, but if there's a rationale behind it' I'd be happy to learn it)
Is there a good reason that E902 error is always on line 1 col 1, or is this just that not enough resources have been spent (yet) to have it point to the right spot in the code).
Most importantly: is there an accepted / better way for this? Perhaps a wrapper around flake8 that, in case of a E902 error uses some other way to find a more accurate spot (note that pyflakes, which is part of flake8, does find the correct line; probably not a good idea to run python since there does not seem to be a way to tell python to only parse the file but not actually run it.). I'm sure I can create such a wrapper myself, but if other/smarter people already made one, I'd prefer to reuse their work.
For completeness, the following code shows the issue:
def x():
pass
x(
a = 1
b = 2
Both pyflakes and python show an error in line 7 (which at least is very close to where the error is made), whereas flake8 will just show the error on 1:1
answering all three of the questions (usually separate questions should be separate questions on SO but...):
the E / F / W codes do not mean error / failure / warning -- the prefix is a coincidence. F codes come from pyflakes, E and W codes come from pycodestyle (though there are two special E codes inherited from pycodestyle that remain -- E902 and E999 for OSError and SyntaxError -- I'd love to change this but the sheer number of people using flake8 makes breaking changes tricky)
it is col 1 line 1 because this error comes from the tokenizer and it reports the error out-of-bounds on the last line of the file. the tokenizer is not very good about giving precise error locations
it is considered a bug, but nobody has volunteered to work on it yet, if I'm reading it correctly it should be a 2 line fix -- you can read more about the bug here: https://github.com/PyCQA/flake8/issues/740
disclaimer: I'm the current flake8 maintainer

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)

Errors don't stop execution

I'm coding using Python and Pyglet, and, as I just have starting the script, it is full of errors, which is normal.
What is less normal is that those errors don't stop execution of the script, even if they are 'basic python' errors. It isn't very bothersome, but a little, since it makes me outsee some errors.
For example, I got :
AttributeError: 'Win32EventLoop' object has no attribute '_next_idle_time'
(by the way, I don't know what this one means. It isn't the main subject of my thread but I'd gladly get answers for this one)
or :
IndexError: list index out of range
(A stupid bug I solved, but it should have stopped the script).
I use Eclipse + pydev, and never experienced that before. It may be pyglet related because it happened with my first pyglet attemps (before, I used pygame).
Thanks for answers,
Fred
Your GUI application is probably running in some kind of loop. This event loop is catching exceptions for You and printing them out.
I recommend writing Your code the way You can unit-test small parts of your program e.g. classes or functions outside of the framework and then glue them together as GUI application. This work-flow will guarantee that you haven't overseen simple bugs just because mindlessly throwing new code into more-or-less working pile of existing code ;)
Read about TDD ;)

Virtual brackets in Python

(Warning: Potential flame-war starter. This is however not my goal, the point here is not to discuss the design choices of Python, but to know how to make the best out of it).
Is there a program, script, method (Unix-based, ideally), to display "virtual" brackets around blocs of code in Python, and to keep them where they are so that the code can still be executed even if indenting is broken ?
I realize that Python only uses indentation to define blocks of code, and that the final program may not contain brackets.
However, I find it very annoying that your program can stop functioning just because of an unfortunate and undetected carriage-return.
So, ideally I would be looking for a plugin in a text editor (kate, gedit...) that would:
Display virtual brackets around blocks of code in my Python program
Keep them in place
Generate dynamically the "correct" Python code with the indentation corresponding to where the brackets belong.
(no flame-war, please !)
I used an editor that does code rollups and understood Python syntax, then I looked for rollups that are in unexpected locations. I don't remember if Kate does that. It's not obvious that there is an issue, but it makes it easier when you are looking for an issue.

Categories