Re: How to handle exceptions properly in a pythonic way?
在 2015年11月4日星期三 UTC-6下午10:18:33,zlju...@gmail.com写道: > > Which would you prefer? > > So if I am just checking for the ConnectionError in get_html and a new > exception arises, I will have traceback to the get_html function showing that > unhandled exception has happened. > Now I have to put additional exception block for managing the new exception > in the get_html function and I am covered. > > Is that what you wanted to say? Hi, If I may, I feel you are trying to address a few separate questions, although they do relate to each other. 1. Coding Design: with the try/except block inside or outside the function 2. Exception Handling: What to do with new un-anticipated exceptions 3. How to record the exception for reference: logging or special value My feeling is: 1. Personally prefer to put try/except block outside the function, to keep the code clean and easy to follow. 2. I would suggest follow the Test Driven Development (TDD) approach. You are not able to anticipate all types of possible exceptions. However, I'm sure you have a pretty good idea about what exceptions are more likely to happen and cause you problem. In that case, just develop your code to pass these tests, and refactor it in the future if really needed. Not necessary to push your code to be perfect, able to handle every possible exception. 3. Kind of personal choice here, since no matter which way you go, you do need to follow it up and deal with them. Probably I would log it just for record keeping purpose. As long as you follow up and trace back to the root cause, they seem to serve the same goal. Hope this helps... All the best, Tian -- https://mail.python.org/mailman/listinfo/python-list
Re: How to handle exceptions properly in a pythonic way?
Hi, If I may, I feel you are tying to address a few questions at the same time, although they are related 1. Where to put the try/except block, inside or outside the function 2. How to deal with un-anticipated exceptions 3. How to keep record My personal feelings are: 1. Kind of prefer try/except block outside the function though. This way it looks clean and easy to follow. In terms of logging the url, since you pass it to the function anyway, there should be ways to keep this option, even with the block written outside the function. 2. From code development perspective, would suggest you follow Test Driven Development (TDD) approach. Nobody can anticipate all of the possible outcomes, but I'm sure you have a pretty good idea about the most likely scenarios, and just make sure that your code is suitable for these scenarios. When future new exceptions arise, you can always refactor your code as needed. 3. Feel it's a personal choice here as being pointed out earlier. No mater which way you go, you just have to follow it up and deal with it, trace it back to the root cause. I kind of prefer logging, so to keep a good record for myself, and you can make it very informative. Hope this helps. All the best, Tian -- https://mail.python.org/mailman/listinfo/python-list
Re: raw_input and break
在 2015年11月4日星期三 UTC-6下午3:45:09,input/ld...@casema.nl写道: > I have an continues loop with "while True:" > Now I want to use "raw_input" and when I press "s" on the keybord that it > will > "break" the continues loop. > > I tried: > choices = raw_input > if choises == s: > break > > But even when I not press "s" it "break" > I want that I not press "s" the script continues. > > Any ideas ? > > Thanks > > > > -- > - --- -- - > Posted with NewsLeecher v7.0 Beta 2 > Web @ http://www.newsleecher.com/?usenet > --- - -- - while True: inp = input("Enter whatever you want, letter 's' to stop: ") if inp == 's': print("Program stopped.") break print("You just entered:", inp) Is this along the line that you are thinking? As pointed out earlier, it will be much easier for people to help if you can share the exact code you put into and the error messages as well. Thanks and good luck! All the best, Tian -- https://mail.python.org/mailman/listinfo/python-list