How to fix "EOF when reading a line" using plugin Python-Notepad++ - python-2.7

I'm using python plugin in Notepad++ and trying to use input() in a script I get this error
>>> name=input()
Traceback (most recent call last):
File "<console>", line 1, in <module>
EOFError: EOF when reading a line
I've tried solutions in other threads of SO like using try/except block as below, but still getting the error. Is there a workaround for this? Thank you.
>>> try:
... input("Please enter something")
... except:
... break
File "<console>", line 4
SyntaxError: 'break' outside loop
>>> try:
... name=input()
... except EOFError:
... break
File "<console>", line 4
SyntaxError: 'break' outside loop
Traceback (most recent call last):
UPDATE
>>> try:
... input("Please enter something: ")
... except EOFError as e:
... print(e)
...
Please enter something: EOF when reading a line
>>> abc
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'abc' is not defined

change break to pass and you should pass that error or use
except EOFError as e:
print(e)
so you will know when it happened. All I can help as I can't replicate it here, works just fine for me. Use break only in loops.

Related

traceback nameerror in python2

I am relatively new to programming,
and i was trying to do a checkbutton and i'm getting a traceback (most recent call last): and NameError message. please help. thanks!
chack_button = Chackbutton(root, text="show password", commend=show_password)
chack_button.place(x=290, y=170)
Traceback (most recent call last):
File "new2.py", line 19, in <module>
chack_button = Chackbutton(root, text="show password", commend=show_password)
NameError: name 'Chackbutton' is not defined
You've written Chackbutton instead of Checkbutton

Why isdigit is working but isdecimal is not for a simple age code

This is the part of code I am talking about:
while True:
print 'What is your age(only numeric):'
age = raw_input()
if age.isdigit():
break
print 'Please try again. We take only numeric value.'
When I replace isdigit with isdecimal, I get this error no matter what value i enter.
Traceback (most recent call last):
File "test.py", line 4, in <module>
if age.isdecimal():
AttributeError: 'str' object has no attribute 'isdecimal'
Although it runs fine with isdigit.
Also, when I replace raw_input with input, it give this error.
Traceback (most recent call last):
File "test.py", line 4, in <module>
if age.isdigit():
AttributeError: 'int' object has no attribute 'isdigit'
Am I doing something wrong which I am not able to catch? I am new to python and did my research about isdigit, is decimal, input and raw_input before putting down question here. It may be very minute error but I am not sure what is it.
For the first part:
In Python 2, isdecimal is a method of the unicode class, not of str (i.e. bytes). Hence:
>>> '3.2'.isdecimal()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'isdecimal'
>>> u'3.2'.isdecimal()
For the second part, input tries to guess what kind of input it gets, and will properly cast "1" into an int, while raw_input will always return the raw string. Since isdigit is a method defined in the str class, you get the error.

Formatting error reporting

I am logging all errors to a file, since there is no other way to see them properly in my case, especially in prod environment. Like this:
sys.stderr = open('py/log/my_logfile.error.log', 'a')
I am getting something similar to this:
Traceback (most recent call last):
File "my_awesome_file.py", line 50, in <module>
revision = session.Query()
AttributeError: 'Awesome' object has no attribute 'SomeSortOfAttribute'
Traceback (most recent call last):
File "my_awesome_file.py", line 50, in <module>
revision = session.Query()
AttributeError: 'Awesome' object has no attribute 'SomeSortOfAttribute'
It's two errors here (well, same error logged twice). I'd like to have if formatted a bit nicer (i.e. additional newline in between errors), and add a datetime if possible. Can I do it using this method of error logging?
After a little bit of reading Python manuals, it seems the solution is rather easy - instead of directly using file open() as a stream for stderr, I just have to use a custom stream. It's so painfully obvious it puzzles me why I didn't come up with it immediately. Well, it was Friday evening after all.
A very basic version would be:
import time
class LogStream(object):
def write(self, text):
f = open('py/log/my.custom.error.log', 'a')
f.write(time.strftime("%H:%M:%S ") + text + "\r\n")
f.close()
And then
sys.stderr = LogStream()
Result:
12:03:05 Traceback (most recent call last):
12:03:05 File "py/stuff.py", line 38, in <module>
12:03:05
12:03:05 raise Exception("just some test")
12:03:05 Exception
12:03:05 :
12:03:05 just some test
12:03:05
I might want to customize it a bit more but it's good enough already.

Debug django tests

I see that TestCase has a method Debug(), but I can't find any example on how to implement it. As far as I've tried, nothing works.
Can anyone provide some code as to how to use it?
debugunit.py
from unittest import TestCase
class MyTest(TestCase):
def test1(self):
print 'before'
self.assertEquals(2+2, 5)
print 'after'
run
python -i debugunit.py
To run a test interactively, create a TestCase instance, giving it the test name as a parameter. To run it, call the resulting object.
>>> print MyTest('test1')()
before
None
The "2+2!=5" exception is consumed by the unittest machinery. To get the set to run (with setUp and tearDown, etc), run the debug() method:
>>> MyTest('test1').debug()
before
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/unittest/case.py", line 400, in debug
getattr(self, self._testMethodName)()
File "debugunit.py", line 6, in test1
self.assertEquals(2+2, 5)
File "/usr/lib/python2.7/unittest/case.py", line 515, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/lib/python2.7/unittest/case.py", line 508, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 4 != 5

os.system in a loop

For each item in a list, I am modifying a file and then I want to run the python script, "ncall.py", which imports the modified file called, "new_basketparams.txt". I also want to make sure ncall.py completes its process before recalling it, but that is perhaps another topic. I am working on an ubuntu linux machine with python 2.7.
Here's the issue: My code seems to call ncall.py infinite times, stuck inside of the loop. I cannot even use a keyboard shortcut to kill the process and must close the window to kill the process. Here is my code and if anyone can help explain a solution, or guide me, that would be greatly appreciated:
import os
import re
datelist=['2014-05-16','2014-05-15','2014-05-14','2014-05-13','2014-05-12']
for date in datelist:
f=open('basketparams.txt')
g=open('new_basketparams.txt','w')
for line in f:
if "Start" in line:
line=line.split(";")
line[2]=re.sub('\d\d\d\d-\d\d-\d\d',date,line[2])
line=';'.join(line)
elif "End" in line:
line=line.split(";")
line[2]=re.sub('\d\d\d\d-\d\d-\d\d',date,line[2])
line=';'.join(line)
else:
pass
g.write(line)
os.system("python ncall.py")
Here are some diagnostics. The import MySQL error is strange because ncall works when called by itself from the terminal
'import site' failed; use -v for traceback
Traceback (most recent call last):
File "ncall.py", line 1, in <module>
import MySQLdb
ImportError: No module named MySQLdb
[]
calling ncall.py no. 1 2014-05-21 07:30:46.400011
new_basketparams.txt
Traceback (most recent call last):
File "ncall.py", line 15, in <module>
Price_Min=display.input[2]
IndexError: list index out of range
[]
calling ncall.py no. 1 2014-05-21 07:30:46.352573
new_basketparams.txt
Traceback (most recent call last):
File "ncall.py", line 15, in <module>
Price_Min=display.input[2]
IndexError: list index out of range
[]
calling ncall.py no. 1 2014-05-21 07:30:46.305043
new_basketparams.txt
Traceback (most recent call last):
File "ncall.py", line 15, in <module>
Price_Min=display.input[2]
IndexError: list index out of range
Now I am including a portion of code from ncall.py as well as display.py:
#ncall.py
import MySQLdb
import csv
import display
import time
from display import fileinput
from datetime import datetime, date, time, timedelta
import datelist
now0=datetime.now()
print fileinput
Start_Datetime='%s 14:00:00'%(datelist.date)
End_Datetime='%s 14:59:59'%(datelist.date)
Price_Min=display.input[2]
Price_Max=display.input[3]
Under=display.input[4]
#display.py
fileinput='new_basketparams.txt'
f=open(fileinput)
input=[]
for line in f:
print 'reading line in fileinput in display.py\'s loop: ', line
try:
line=line.split(';')
parameter=line[0]
Input=line[2]
x=Input.split('\r')
input.append(x[0])
except :
continue