Confusing indentation issue - python-3.x

I'm using an API from this GIT repository to control a JDS6600 signal generator.
Now some of the commands work & some dont due I think to the unit I'm using being slightly different to the one used by the author of the API.
So to fix this I tried to see what data the device was sending back before an error is raised. I thought a simple print(data) would do it but I keep getting an indentation error, sorry screen shots but I think you get a better idea from a picture in this case.
First with the newly added print(data), note little red cross on the left
Second without,
So how come I'm getting this error? No matter where I place that print(data) within the method I get an indentation error & the code falls over.
EDIT: The problem was fixed running reindent on the offending file, which converts all the tabs to spaces. As pointed out by Tyberius in a comment below the file was a mix of tabs & spaces. It seems Spyder has a unique attitude to such cases.

You're seeing an error highlighted by the IDE (lint error), not an actual python error (you would get it after running and seeing a traceback).
In general anything after : is expected to be in the next line and IDE is probably making that assumption.
The general style guide is defined by PEP-8 and it covers compound statements. The way you're trying to use is generally considered within the "rather not" section.
So really it should be
if a not in (0, 1):
raise RuntimeError(a)
print(data)
If you would use something like black autoformatter it would sort it out for you.

The line will actually fail. For example, look below:
if a in [0.5,0.4]:
raise TypeError(a)
print("what")
File "<stdin>", line 3
print("what")
^
SyntaxError: invalid syntax
Now, why does this happen?
answer is mentioned here. Basically, as you define the error in the previous line, the error gets caught in the print. For example, it is technically similar to the following:
>>> while True print('Hello world')
File "<stdin>", line 1
while True print('Hello world')
^
SyntaxError: invalid syntax
So that is why, your print is raising the error instead of the previous line. Also, there is no meaning of printing after you raise the error, as it will never get executed as the error will halt the execution. So the solution will be to put the print statement before the raise.

Related

I'm trying to use the turtle methods, etc. listed on the official python page but I keep getting errors

I'm using turtle to create a video game.
I'm making this program for a virtual class, and I don't know the exact limitations of the (online) IDLE provided. I'm not currently able to test my code in any other environment. I don't want to use pygame or anything because I don't know if I can and also I don't want to rewrite a bunch of my code.
The problems I'm having are mostly with the TurtleScreen/Screen. I can't call .Screen() or .TurtleScreen(), .bgcolor(), .turtlesize(), and probably a lot more I haven't checked yet. Here's an example:
bg = turtle.Screen()
turt = turtle.Turtle()
bg.bgcolor("black")
I just receive a
ParseError: bad input
when I run it. Any input is much appreciated!
A parse error usually comes from python being unable to parse your code rather than an issue with Turtle.
The code snippet you posted above works fine so the problem is likely with some other lines of code that precede it. Double check your syntax on lines before the error occurs and make sure there aren't any missing parenthesis, brackets, quotes, etc that would impact later lines.

Python script in Colab keeps throwing error message: IndentationError: unindent does not match any outer indentation level

I'm currently using Google Colab in order to take advantage of its free GPU. I was trying to modify a code that I copy and pasted from machinelearningmaster.com. However, whenever I try to add a new code line, for example "print("some words"), I get an indention error.
I have tried adding tabs or spaces before the print call but I still get the error. for example:
space,space,print("some words")
tab, tab ,print("some words")
I have also checked the colab editor settings, currently the indention width setting are set to two spaces.
The first three lines are part of the original code, the print statement is
my addition. I copy and pasted this directly from the colab editor. In Colab all four lines are aligned. As you can see here only the first three lines are aligned. I don't know what's going on.
img_path = images_dir + filename
ann_path = annotations_dir + image_id + '.xml'
count=count+1
print("this is count: ", count)
I expected this to print the value of count, instead I get an error message telling me:
IndentationError: unindent does not match any outer indentation level
Okay, after much searching and frustration, I have an idea of what went wrong, but even better, a solution to fix it.
It appears that the Google Collaborator (Colab) editor does not have a way to set it for tabs "\t" versus space (space-bar entries). From the settings tab on the cell you can set the width of the tab from 2 to 4, but these will be interpreted as 2 to 4 space-bar entries. Usually, this isn't a problem. However, if you're like me and you want to test out code from the web, or be lazy and just copy paste from your editor, problems can arise.
Here's how I fixed it. Before pasting the copied code into Colab, first put it into notepad++. Go to View> Show Symbols >Show All Characters, click on this, you should now be able so see all the characters in the code. Find a tab, it will look like an arrow pointing to the right -->, right click and copy it. Open Search> Find, open the Replace tab. Depending on your version of notepad++ the tab you copied will automatically be entered and the replace will already be set to four spaces. Hit "Replace all". This will automatically replace all tabs with equivalent spaces. Copy the code from notepad++ back to Colab. Now there will be no more conflicts.
I think using a simple find and replace tool will just work fine. I also came across this error recently in Colab and I went through #Rice Man solution. The only difference was I used Libre office writer instead of Notepad++. I also found this tool to be helpful. I am not proficient in using Colab but this solution worked for me.
Another quick fix that worked for me related to this question.
I was trying to run a python script in colab and faced this error though the line seems at an appropriate indentation in that script.
I checked with the !cat filename.py cmd, and found out that the actual indentation appears different than it is in the script (hence the error).
Taking that unindented line (according to the colab) at the start of the line and using space afterward fixed the error.
I used this website to fix the error.
Copy your code to the site, then click beautify button on top left. This will remove indention errors.
If you want to know where the indention error is coming from, use #Prachi answer.

Getting an invalid syntax <Unknown, line 6> error for print function w/ Pylint on VSCode

For my weekly course assignment, we're tasked w/ creating simple user entry points for a fictional employee. I ran the code once and everything worked out as I needed it to w/ it asking the user to input data, once input it presented it w/ a title. I forgot to add a field for the employeeName and the "----" lines, so I added all of that in, copy and pasting some of it to get done as the code is highly similar. I then got the syntax error. I checked my spelling for print, deleted the print function, and then used VS Code intellisense to autofill the function, no changes to the error, went through the line to see if I forgot something or added extra nonsense. I looked through similar posts, but they aren't quite getting at my specific issue from what I can tell. Any help is much appreciated.
Regards
# user input for an employee's SSN
print("Please enter your SSN #:", end=' ')
employeeSSN = input(str())
print('SSN:', employeeSSN)
print("PLease enter your SSN #:", end=' ')
^
SyntaxError: invalid syntax
Expected results should print the string inside quotations and allow for user input.
I found my own error after some more checking and testing. I forgot to put in an ending parenthesis for line 4 of my code. I found it by taking away the last set of things I worked on and retesting to a good point, then slowly re-adding in the rest. I noticed the missing parenthesis in that process. I wish the error would have pointed my towards that missing part in line 4 then on line 6 and the function. Had me thinking something totally different.
Example of code mess-up
print("----------------------------", employeeName, "----------------------------"
How I fixed it
print("----------------------------", employeeName, "----------------------------")
Sometimes, its the simplest things right in our face. Hopefully this helps someone else.

AutoPEP8, Anaconda, Sublime Text 3 Inconsistent Error Reporting

Please reference the attached screenshot.
I am working on a scrapy pipeline. I am using Sublime Text 3 with Anaconda/AutoPep8. I just moved over from ST2 where I did not have AutoPep8 or Anaconda.
The huge white dot is telling me that this line has invalid syntax. I could not figure out what the problem was, so I copied the line and pasted it here on line 1 of what was then an empty file. The syntax error still showed, suggesting it was not a problem with a prior line missing a closing parenthesis.
Then I made up some similar code, lines 3, 6, & 9. As you can see, they all pass muster. (I don't care about the other little errors, they are not the issue).
Then I pasted in the entire method over onto this new document, and now, as you can see, even the suspect line (22) passes.
Then I copied and pasted the whole class, and again, as you can see, line 49, the code it was complaining about before now doesn't cause any issues at all.
I tried using the command palette - PEP8: Preview Changes, (see https://github.com/wistful/SublimeAutoPEP8) but nothing happened.
I also tried the right click menu Anaconda > Autoformat PEP8 Errors but the E501 errors (line too long) were ignored and nothing happened with the supposed syntax error in the if statement.
Note: When I first started using AutoPep8 to fix E501 errors last week, I noticed that about halfway through a script, it suddenly stopped reporting errors at all. Don't know if this is related.
I also get this error when ST3 starts: https://github.com/DamnWidget/anaconda/issues/514. However, the consensus seems to be that if Sublime works even after you click through this error, it is nothing to worry about. I just mention this to give you as much as I know so you can help.
I also tried turning len('advocate') into a string, but the syntax error remains.
What is going on here? Is there a bug in Anaconda, AutoPep8, ST3, or my code?
Ubuntu 16.04, Python 3.4, (but 'automatic' build in ST3) ST3 Build 3126 (I don't know how the Build number lines up with a version number) Anaconda, AutoPep8 version numbers unknown, but I got them less than two weeks ago.
Well, it took a lot of very patient Googling and a comparison test in VSCode, but I have the astoundingly simple answer: I should have put '==', not '=' ! Yes, dear friends, it is basic Python. = means assignment, == mean equality or comparison. My if is of the latter type, because you can't make an assignment in an if statement. Now VSCode didn't make this any clearer, I think they both use Pylint, but it at least let me know that this was probably a valid error, whether I understood it or not. (Unless of course I know more that the devs at Pylint...Yea, not likely).
Kudos to Jean Mark Gawron, (who must be related to the Klingon Emperor) for giving me the answer: http://gawron.sdsu.edu/python_for_ss/course_core/book_draft/programming_intro/boolean_results.html

LaTeX document errors where no errors exist

I am writing my thesis with LaTeX and since today very curious errors turn up. I wrote something and made a citication in the file references.bib, and then the errors turned up. Befor writing that few lines everything worked great. Thus, I deleted everything I've added. But the errors still turn up. One Error is: Extra }, or forgotten $. ...ckoverflow Permission SYSTEM_ALERT_WINDOW}. I think my citication (which I've deleted after the errors turned up) is still stored somewhere , because "ckoverflow Permission SYSTEM_ALERT_WINDOW" was a part of the title of the cite.
I hadn't found any error in any file.
The next weird thing is that a mate of mine can compile that files without any problems. He is using WinEdt. I am using TeXStudio, but I've tried it with WinEdt, too.
There is a screenshot of my errors:
Please help me, any suggestions are appreciated.
Best regards
Looks like you might need to escape your _ characters there, as they are normally used for subscript, but only in math mode; hence the "missing $ inserted".
(La)TeX error messages make me sad... :(

Resources