This question already has answers here: How do you properly determine the current script directory? (16 answers) Closed 6 months ago. I’m building a simple helper script for work that will copy a couple of template files in our code base to the current directory. I don’t, however, have the absolut…
Category: Questions
os.system() execute command under which linux shell?
I am using /bin/tcsh as my default shell. However, the tcsh style command os.system(‘setenv VAR val’) doesn’t work for me. But os.system(‘export VAR=val’) works. So my question is how can I know the os.system() run command under which shell? Answer os.system() just calls the syst…
Why does sys.exit() not exit when called inside a thread in Python?
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…
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…