By Vasudev Ram
I started watching this recent PyCon US 2016 video. I found it informative and interesting:
Alex Martelli: Exception and error handling in Python 2 and Python 3
Alex Martelli is the author or co-author of some well-known Python books like Python in a Nutshell and the Python Cookbook (for at least one of the versions); I've bought and read some of them, and they are pretty good.
[ Interestingly, the Wikipedia article about Alex, linked above, says that he is also noted for coining the phrase duck typing. ]
The video is also embedded below:
- Enjoy.
- Vasudev Ram - Online Python training and consulting Get updates on my software products / ebooks / courses. Jump to posts: Python DLang xtopdf Subscribe to my blog by email My ActiveState recipes FlyWheel - Managed WordPress Hosting
Showing posts with label exception-handling. Show all posts
Showing posts with label exception-handling. Show all posts
Monday, December 12, 2016
Sunday, November 13, 2016
Trapping KeyboardInterrupt and EOFError for program cleanup
By Vasudev Ram
Ctrl-C and Ctrl-Z handling
I had written this small Python utility for my own use, to show the ASCII code for any input character typed at the keyboard. Since it was a quick utility, I was initially just using Ctrl-C to exit the program. But that leaves behind a messy traceback, so I thought of trapping the exceptions KeyboardInterrupt (raised by Ctrl-C) and EOFError (raised by Ctrl-Z). With that, the program now exits cleanly on typing either of those keys.
Here is the resulting utility, char_to_ascii_code.py:
Another run shows a few more codes and the trapping of the Ctrl-Z key combination.
- Vasudev Ram - Online Python training and consulting Get updates on my software products / ebooks / courses. Jump to posts: Python DLang xtopdf Subscribe to my blog by email My ActiveState recipes
Ctrl-C and Ctrl-Z handling
I had written this small Python utility for my own use, to show the ASCII code for any input character typed at the keyboard. Since it was a quick utility, I was initially just using Ctrl-C to exit the program. But that leaves behind a messy traceback, so I thought of trapping the exceptions KeyboardInterrupt (raised by Ctrl-C) and EOFError (raised by Ctrl-Z). With that, the program now exits cleanly on typing either of those keys.
Here is the resulting utility, char_to_ascii_code.py:
from __future__ import print_function """ char_to_ascii_code.py Purpose: Show ASCII code for a given character, interactively, in a loop. Show trapping of KeyboardInterrupt and EOFError exceptions. Author: Vasudev Ram Web site: https://vasudevram.github.io Blog: https://jugad2.blogspot.com Product store: https://gumroad.com/vasudevram """ print("This program shows the ASCII code for any given ASCII character.") print("Exit the program by pressing Ctrl-C or Ctrl-Z.") print() while True: try: c = raw_input( \ "Enter an ASCII character to see its ASCII code: ") if len(c) != 1: print("Error: need a string of length 1; retry.") continue print("Character:", c) print("Code:", ord(c)) except KeyboardInterrupt as ki: print("Caught:", repr(ki)) print("Exiting.") break except EOFError as eofe: print("Caught:", repr(eofe)) print("Exiting.") breakHere is a sample run, that shows the ASCII codes for the comma, tab and pipe characters, which are commonly used as field delimiters in Delimiter-Separated Value (DSV) files.
$ python char_to_ascii_code.py This program shows the ASCII code for any given ASCII character. Exit the program by pressing Ctrl-C or Ctrl-Z. Enter an ASCII character to see its ASCII code, or Ctrl-C to exit: , Character: , Code: 44 Enter an ASCII character to see its ASCII code, or Ctrl-C to exit: Character: Code: 9 Enter an ASCII character to see its ASCII code, or Ctrl-C to exit: | Character: | Code: 124 Enter an ASCII character to see its ASCII code, or Ctrl-C to exit: Caught: KeyboardInterrupt() Exiting. $I pressed the Ctrl-C key combination to exit the program. Ctrl-C does not show on the screen, but the exception handler for it is activated, and prints the last message above.
Another run shows a few more codes and the trapping of the Ctrl-Z key combination.
$ python char_to_ascii_code.py This program shows the ASCII code for any given ASCII character. Exit the program by pressing Ctrl-C or Ctrl-Z. Enter an ASCII character to see its ASCII code, or Ctrl-C to exit: ! Character: ! Code: 33 Enter an ASCII character to see its ASCII code, or Ctrl-C to exit: ~ Character: ~ Code: 126 Enter an ASCII character to see its ASCII code, or Ctrl-C to exit: ^Z Caught: EOFError() Exiting.
- Vasudev Ram - Online Python training and consulting Get updates on my software products / ebooks / courses. Jump to posts: Python DLang xtopdf Subscribe to my blog by email My ActiveState recipes
Subscribe to:
Posts (Atom)