I am confused as to why the following code snippet would not exit when called in the thread, but would exit when called in the main thread. The docs for sys.exit() state that the call should exit from Python. I can see from the output of this program that “post thread exit” is never printed, but t…
Tag: python
Explain to me what the big deal with tail-call optimization is and why Python needs it
Apparently, there’s been a big brouhaha over whether or not Python needs tail-call optimization (TCO). This came to a head when someone shipped Guido a copy of SICP, because he didn’t “get it.” I’m in the same boat as Guido. I understand the concept of tail-call optimization. I j…
Send HTML emails with Python
How to send HTML content in email using Python? I can send simple texts. Answer From Python v2.7.14 documentation – 18.1.11. email: Examples: Here’s an example of how to create an HTML message with an alternative plain text version:
How to ignore deprecation warnings in Python
I keep getting this : How do I make this message go away? Is there a way to avoid warnings in Python? Answer From documentation of the warnings module: If you’re on Windows: pass -W ignore::DeprecationWarning as an argument to Python. Better though to resolve the issue, by casting to int. (Note that in …
How to dynamically compose an OR query filter in Django?
From an example you can see a multiple OR query filter: For example, this results in: However, I want to create this query filter from a list. How to do that? e.g. [1, 2, 3] -> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3)) Answer You could chain your queries as follows:
How to limit the maximum value of a numeric field in a Django model?
Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField. Although the former can be restricted to the number of decimal places stored and the overall number of characters stored, is there any way to restrict it to storing only numbers within a certain range, e…
Is there a portable way to get the current username in Python?
What is a portable way (e.g. for Linux and Windows) to get the current user’s username? Something similar to os.getuid() would be nice: The pwd module works for Unix only. Some people suggest that getting the username under Windows can be complicated in certain circumstances (e.g., running as a Windows …
How do you validate a URL with a regular expression in Python?
I’m building an app on Google App Engine. I’m incredibly new to Python and have been beating my head against the following problem for the past 3 days. I have a class to represent an RSS Feed and in this class I have a method called setUrl. Input to this method is a URL. I’m trying to use th…
Delete digits in Python (Regex)
I’m trying to delete all digits from a string. However the next code deletes as well digits contained in any word, and obviously I don’t want that. I’ve been trying many regular expressions with no success. Thanks! Result: This must not b deletd, but the number at the end yes Answer Add a sp…
`final` keyword equivalent for variables in Python?
I couldn’t find documentation on an equivalent of Java’s final in Python, is there such a thing? I’m creating a snapshot of an object (used for restoration if anything fails); once this backup variable is assigned, it should not be modified — a final-like feature in Python would be nic…