Why this is unexpected indentation in Python? - python

#!/usr/bin/python
try:
f = open("/home/masi/r.raw", "r+")
aBuf = f.seek(4)
except:
print "Error at position : ", position
events = []
for i in range(100):
aBuf = f.seek(4);
try:
if aBuf[:4] == b'\xFA\xFA\xFA\xFA':
print("E")
except:
# print "\0"
f.close()
when I commented out the print of except, I get the error
f.close()
^
IndentationError: expected an indented block
I think I need to have try-except because the if-clause is false often.
Why do I get this unexpected indendation?

except is expecting one or more statements.
You could write:
try:
if aBuf[:4] == b'\xFA\xFA\xFA\xFA':
print("E")
except:
pass
Warning
As stated in the comments, it is not a good practice to use except: pass.
See Why is “except: pass” a bad programming practice?.

Your code from events = [] and down needs to be inside of the first try..except block. The f.close() is out of scope.

Related

How to repeat a step in a loop if an action fails?

In the case I am looping into a list, where for value msg_list = 0 it will execute action(0), usr). This action can fail for a determined user, I should choose aother user if it happens, perform all the actions related to the user
How can I do to repeat action[0] if it fails?
for msg in range(len(msg_list)):
# in this case msg = 0
usr = select_random_user()
multiple_actions_for(usr) # This are lots of code lines I don't want to repeat!!
try:
action(msg, usr)
more_actions(usr.related_stuff)
except Exception as e:
go_back_to(msg =0 instead of looping into msg=1) # this is what I want to do
How can I do to get that? Repeat the loop for msg = i instead of passing to msg = i + 1?
Put your code into the endless while-loop with exiting from it if try was successful:
for msg in range(len(msg_list)):
while True:
usr = select_random_user()
multiple_actions_for(usr)
try:
action(msg, usr)
more_actions(usr.related_stuff)
except Exception as e:
continue
else:
break
It really depends. Is it okay to go back to the beginning of the loop? If so, you can call "continue", which stops the current iteration, and restarts the loop. Otherwise, I don't think there's something similar to a goto in Python, no. It's a very structured language.
Try using while loop instead of for loop. The idea is as shown bellow:
bool still_looping = True
msg = 0
while still_looping:
usr = select_random_user()
multiple_actions_for(usr)
try:
action(msg, usr)
more_actions(usr.related_stuff)
if (msg < len (msg_list)):
msg += 1
except Exception as e:
# Do whatever you want here. msg was not incremented
if (msg == len(msg_list))
still_looping = False #verify if this was the last execution

How to check for several exceptions within one exception block?

I want to catch several exceptions on the same line, but have different outcome depending on which exception is triggered. I'm trying to summarize a set of numbers in a text file and want to check on value and io errors.
try:
filex = open('test.txt', 'r')
number = filex.readline().rstrip('\n')
added = 0
while number != '':
added += int(number)
number = filex.readline().rstrip('\n')
print(added)
filex.close()
except (IOError,ValueError):
if IOError:
print('IOError')
else:
print('ValueError')
The issue I'm having is that it will always trigger on the first condition of the IF test.
can use two except for this condition like this
try
.......
except IOError:
print('IOError')
except ValueError:
print('ValueError')
I recommend using one clause per exception. Here is the "I'm stubborn" version:
try:
filex = open('test.txt', 'r')
number = filex.readline().rstrip('\n')
added = 0
while number != '':
added += int(number)
number = filex.readline().rstrip('\n')
print(added)
filex.close()
except (IOError,ValueError) as e:
if isinstance(e, IOError):
print('IOError')
else:
print('ValueError')
Here's the clause-per-exception version:
try:
filex = open('test.txt', 'r')
number = filex.readline().rstrip('\n')
added = 0
while number != '':
added += int(number)
number = filex.readline().rstrip('\n')
print(added)
filex.close()
except IOError:
print('IOError')
except ValueError:
print('ValueError')
You can see that in the first version, you have to implement type checking, which Python's exception handling would otherwise give you for free. And the second version is slightly shorter, too.
if IOError:
I think this line checks the trueness of the class IOError. It ignores the type of error that was raised.
You could maybe do something like :
except (IOError, ValueError) as e:
if instanceof(e, IOError):
#...

How to pass when variable = null in python 3

so far I have this:
import datetime
f = open("log.txt", "a", encoding='UTF-8')
print ("Log file created")
print ("Enter /close to leave.")
spc = " "
while 1:
msg = input(">>:")
now = datetime.datetime.now()
now1 = str(now)
if msg == None:
pass
if msg == " ":
pass
else:
msg2 = now1+spc+msg+"\n"
if msg == "/close":
exit()
f.write(msg2)
f.flush()
However, this line is not functioning as I want it, it still returns a blank line on the log file:
if msg == None:
pass
I want it to not return anything and simply continue the while loop, How would I fix this?
You should be using
if msg is None:
pass
Edit
You're missing what the pass function is all about. I would re-write your look like so. This way we're only processing this if the msg is not one of the bad input. Once we're done we break out of the loop.
...
while 1:
msg = input(">>:")
now = datetime.datetime.now()
now1 = str(now)
if not msg in [None, " "]
msg2 = now1+spc+msg+"\n"
if msg == "/close":
exit()
f.write(msg2)
f.flush()
break
Evaluation of the rest of the loop will still continue after pass, and as None does not equal " ", this means the block beginning with msg2 = now1+spc+msg+"\n" will be executed. You need to either unite the if ... if ... else into a single if block by changing if msg == " ": to elif msg == " ": or else change if msg == None: pass to if msg == None: continue.
try:
msg2 = now1+spc+msg+"\n"
if msg == "/close":
exit()
f.write(msg2)
f.flush()
except:
pass #example for a function: return None or raise
Your condition doesn't make any sense. The input function will never return None, only strings.
If you want to skip empty strings, a better test would be if not msg (empty strings are "falsy"). Or, if you want to reject any all-whitespace strings, try if not msg.strip() (which removes leading and trailing whitespace before checking if the rest of the string is empty or not).
Further, it's rarely a good idea to write an if statement that just contains pass. Instead, invert the test so that the condition is true for cases where you want to run some code (in this case, when msg is not empty or all whitespace) and simply omit the cases where you'd do nothing:
while 1:
msg = input(">>:")
now = datetime.datetime.now()
now1 = str(now)
if msg.strip(): # msg is not empty or all whitespace
msg2 = now1+spc+msg+"\n"
if msg == "/close":
exit()
f.write(msg2)
f.flush()
One final issue (unrelated to the main question). Python's exit function is primarily intended for use in the interactive interpreter. It is added to the builtins by the site module, and so it won't exist if Python was run with the -S flag. If you want to close the interpreter, you should instead call sys.exit, raise a SystemExit exception, or just run off the end of the main module (a break statement would probably do that for the loop you've shown here, or perhaps a return if you're in a function somewhere).

If else code help in try/ except block

SomeDict = {'Sarah':20, 'Mark': 'hello', 'Jackie': 'bye'}
try:
result = ""
theKey = raw_input("Enter some key: ")
val = someDict[theKey]
except keyErrorr:
result "hello"
else:
result = result + "" + "done"
print result
I understand the try block you can insert and code to try and see what error comes up, and the error then can be caught by the except block. I am trying to figure out the best way to insert a if / else in the try and except block for the same key error that is present in this code. I was thinking that i could just replace the try and except with If/else or is it possible to just add a if/else in the try and except. Any help on how to insert a if/else into this code for key error would be greatly appreciated. So basically i want to add a if/else code into the try and except block for the same key error.
SomeDict = {'Sarah':20, 'Mark': 'hello', 'Jackie': 'bye'}
try:
result = "" #could i insert something like if result == "" : #for this line?
theKey = raw_input("Enter some key: ")
val = someDict[theKey]
except keyErrorr:
result "hello"
else:
result = result + "" + "done"
print result
One reasonable option is to initialize result = None, then test if result is None:.
It's better to use None than the empty string, since someday you might want a dictionary value to be the empty string, plus None is probably clearer to the casual reader of your code.
You could also just skip the try-except, and use if theKey in someDict:.
you can add another except without a specification what exception it should handle.
try:
# do something
except KeyError:
# do something because of the Keyerror
except:
# do what you need to do if the exception is not a KeyError
someDict = {'Sarah':20, 'Mark': 'hello', 'Jackie': 'bye'} # corrected dict name
result = ""
theKey = raw_input("Enter some key: ")
try: # just try the code where the error could be
val = someDict[theKey]
except KeyError: # corrected exception name and indent level
result = "hello" # corrected syntax
else: # corrected indent level
result = result + "" + "done" # why add "" ?
print result
does this work for you?

Python: Is it possible to have multiple exceptions statments for a try block?

try:
case_no = re.search("Case Number:</span></td><td><span class=\"Value\">([^<]*?)<",br.response().read()).group(1)
except:
try:
try:
case_no = re.search("Citation Number:</span></td><td><span class=\"Value\">([^<]*?)<",br.response().read()).group(1)
except:
case_no = re.search("Citation Number:</span></td><td><span class=\"Value\">([^<]*?)<",br.response().read()).group(1)
except:
case_no = "N/A"
As you can see the above code is quite clumsy. I want to know if there is any way I can do like this.
try:
XYZ
except:
DOXYZ
except:
DOXYZ
Basically I want to be able to use - "try X if exception then try Y if exception then try Z" without nesting too much statemtns.
Probably you shouldn't be checking exception at all?
patterns = [
"Case Number:</span></td><td><span class=\"Value\">([^<]*?)<",
"Citation Number:</span></td><td><span class=\"Value\">([^<]*?)<",
"Citation Number:</span></td><td><span class=\"Value\">([^<]*?)<" # same as #2?
]
text = br.response().read()
case_no = "N/A"
for pattern in patterns:
res = re.search(pattern, text)
if res:
case_no = res.group(1)
break
Yes, it is posible, as long as you define exception condition...
Like:
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
raise
But, you must define the exception type.
A common idiom for the behavior you're looking for is something like:
try: foo()
except: pass
try: bar()
except: pass
But you should always catch a specific exception and make sure it makes sense. In your case it simply doesn't make sense - to see if the regular expression matched, test the result for None:
r = br.response().read()
PATTERN1="..."
PATTERN2="..."
PATTERN3="..."
mo = re.search(PATTERN1, r) or re.search(PATTERN2, r) or re.search(PATTERN3, r)
case_no = mo.group(1) if mo else "N/A"
For performance reasons you can precompile your regexes:
RE1 = re.compile("...")
RE2 = re.compile("...")
RE3 = re.compile("...")
mo = RE1.search(r) or RE2.search(r) or RE3.search(r)
Also, for your specific regex patterns you can easily combine them into one, and using a named group can help readability:
pat = r"""(Case|Citation) Number:</span></td><td><span class="Value">(?P<case_no>[^<]*?)<"""
mo = re.search(pat, r)
case_no = mo.group("case_no") if mo else "N/A"
And finally, using regular expressions to parse HTML is the road to disaster, consider using HTMLParser from the standard lib or Beautiful Soup.
No, it is not possible to do what you want. the semantics of multiple except clauses covers catching different types of exceptions that may be thrown from the same block of code. You must nest the statements or rethink your code to get the desired results.
This might be a case where it would be better to test for the preconditions that you expect to cause an exception.
if test1():
#dox
elif test2():
#doy
elif test3():
#doz
etc.
I would also recommend against using catchall except: phrases except in very specialized circumstances where you know you need them.
I'd avoid the exceptions if I were doing this:
count = 3
caseRe = re.compile("Case Number:</span></td><td><span class=\"Value\">([^<]*?)<")
while count > 0:
text = br.response().read()
mo = caseRe.search(text)
if mo is None:
count -= 1
continue
case_no = mo.group(1)
break
if count == 0:
case_no = "N/A"
You'd be better off restructuring your code:
success = False
for _ in xrange(MAX_ATTEMPTS):
try:
XYZ
success = True
break
except:
pass
if not success:
DOXYZ
It's better to explicitly specify the exception, though. Do you really want to catch KeyboardInterrupts?
If you're doing the same or a similar thing in every try/except block, you might use a loop
case_no = "N/A"
for _ in range(3):
try:
case_no = re.search("Case Number:</span></td><td><span class=\"Value\">([^<]*?)<",br.response().read()).group(1)
break
except SomeExplicitlyCaughtExceptions:
pass
Of course it makes no sense in this form, because trying the same thing three times will yield the same result.

Categories

Resources