I have but what I end up is catching any kind of HTTP error. I want to catch only if the specified webpage doesn’t exist (404?). Answer Python 3 Python 2 Just catch HTTPError, handle it, and if it’s not Error 404, simply use raise to re-raise the exception. See the Python tutorial. Here is a compl…
Tag: python
Actual meaning of ‘shell=True’ in subprocess
I am calling different processes with the subprocess module. However, I have a question. In the following code: and Both work. After reading the docs, I came to know that shell=True means executing the code through the shell. So that means in absence, the process is directly started. So what should I prefer f…
Mac OS X Python GUI Administrator Prompt
I have an OS X Python application that uses wxPython for it’s GUI controls. I’m looking to prompt the user for administrative rights (akin to using the Authorization Service API in Objective-C) before starting a network service. The closest library I have found is Bob Ippolito’s Authorizatio…
__getattr__ for static/class variables
I have a class like: Whenever MyClass.Foo or MyClass.Bar is invoked, I need a custom method to be invoked before the value is returned. Is it possible in Python? I know it is possible if I create an instance of the class and I can define my own __getattr__ method. But my scnenario involves using this class as…
Find and replace string values in list [duplicate]
This question already has answers here: Apply function to each element of a list (4 answers) Closed 5 months ago. The community reviewed whether to reopen this question 5 months ago and left it closed: Original close reason(s) were not resolved I got this list: What I would like is to replace [br] with some f…
How do I make Python remember settings?
I wrote the beautiful python example code below. Now how do I make it so when I exit then restart the program it remembers the last position of the scale? Edit: I tried the following code But get the following error Answer Write the scale value to a file and read it in on startup. Here’s one way to do
“assert” statement with or without parentheses
Here are four simple invocations of assert: Note that the last one does not raise an error. What is the difference between calling assert with or without parenthesis that causes this behavior? My practice is to use parenthesis, but the above suggests that I should not. Answer The last assert would have given …
How do I get the client IP of a Tornado request?
I have a RequestHandler object for incoming post()s. How can I find the IP of the client making the request? I’ve browsed most of RequestHandler’s methods and properties and seem to have missed something. Answer RequestHandler.request.remote_ip (from RequestHandler’s instance) you can inspec…
inserting newlines in xml file generated via xml.etree.ElementTree in python
I have created a xml file using xml.etree.ElementTree in python. I then use to write out the document to a file. But when I open filename using a text editor, there are no newlines between the tags. Everything is one big line How can I write out the document in a “pretty printed” format so that th…
How slow is Python’s string concatenation vs. str.join?
As a result of the comments in my answer on this thread, I wanted to know what the speed difference is between the += operator and ”.join() So what is the speed comparison between the two? Answer From: Efficient String Concatenation Method 1: Method 4: Now I realise they are not strictly representative,…