So once again I have stumbled upon a script where I needed to send notifications. E-mail comes as the default choice, but there are so many referneces and… what works on one machine does not seem to work on another. Why?.. Read on.
Category: Python
Tips for using JSON WordPress API for JetPack-powered websites
Being a lazy person I prefer to automate as much as possible instead of performing routine work manually. Well, you need to make it once to know what you need to automate, but that is where it stops for me.
So I had a goal of preparing and publishing a number of posts on my (only in Russian for now, sorry) website for motorcycle enthusiasts, where the data could nicely fit into a database and when available it can be wrapped in a template and posted. OK, I need to get the data myself, but publishing tenth of posts is NOT an option. Luckily I had already Jetpack installed, which gave me access to the JSON API.
I will describe separately how to obtain the proper authorization tokens (not sure I can repeat it, it is made for confusion, not sure about security though). When you do have those you can use API. My favorite language of choice for automation is Python, therefore I started typing code before even thinking of selecting a different language.
Continue reading “Tips for using JSON WordPress API for JetPack-powered websites”
Pylab.show() does not show graph
Nothing showing up when using pylab.show()? Change the default non-interactive ‘agg’ backend to something that can actually show something:
import matplotlib matplotlib.use('Qt4Agg')
If this fails, on Ubuntu you may need to install Qt first:
sudo apt-get install python-qt4
Enjoy!
Adding basic logging to your Python script
Whether you like it or not, but there are many situations when you need to add logging information. It is easy to start with some print statements, but what if at certain point you want to put this information to a file? Redirecting output to a file will result in all-or-nothing situation since the contents of the file will be only visible at the end of execution. A more flexible and proper solution would be using a proper logging module.
Continue reading “Adding basic logging to your Python script”
Get Amazon S3 metadata in Python using boto
Another example of something that costs time because it is simply not well documented (at least at the moment of writing). When working with Amazon S3 objects (identified by keys in buckets) you may want to get meta-data associated with those keys. In my case I was storing versions (revisions) of backups in meta-data.
Continue reading “Get Amazon S3 metadata in Python using boto”
Getting list of files from a folder in Python
I find myself searching and (re-)implementing this on a regular basis. The os.walk
is often given as a starting point, but then there are still few lines that are needed to wrap-up.
Continue reading “Getting list of files from a folder in Python”
Get path of current python file
Although rather trivial, but every time I am still looking it up:
import os dir = os.path.dirname(os.path.realpath(__file__))
C++11 vs Python – not quite there yet
Although not exactly fair, while reading the C++ Primer book to refresh my C++11 knowledge during Christmas break (OK, after kids went asleep), I came across the example given for the new C++11 features (lambdas), strings and STL usage in chapter 16, which does nothing more than counting words in a text file. This is a rather classical example and I happen to have it as a part of the Python course I give to my colleagues. Of course I couldn’t hold myself from comparing C++11 and Python as C++11 actually tries to get higher level and closer to languages like Python. Here is what I’ve got.
Continue reading “C++11 vs Python – not quite there yet”
Python notes
This post is inspired by post (in Russian) on
Habrahabr. Few things were not that apparent for already a few years of using Python, but can be very handy!
Continue reading “Python notes”
Configuring Django under wsgi
A small reminder for settings up Django under wsgi. Apparently the following snippet helps getting the path correct avoiding the dreadful
self.load_middleware()
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/utils/functional.py", line 184, in inner
self._setup()
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/conf/__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'xxx.settings' (Is it on sys.path?): No module named xxx.settings
messages from Apache (well, instead of ‘xxx’ your project name will be mentioned of course).
The fix
import sys
# Correct path.
app_path = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..'))
if app_path not in sys.path:
sys.path.append(app_path)
For completeness, the sites-enables/xxx looks like
WSGIPythonPath /var/www/path/to/project
<VirtualHost *:80>
DocumentRoot /var/www/path/to/project
ServerAdmin admin@example.com
ServerName my.example.com
Alias /static/ /var/www/path/to/project/
Order deny,allow
Allow from all
WSGIDaemonProcess project_name
WSGIScriptAlias / /var/www/path/to/project/wsgi.py
</VirtualHost>
Happy Djangoing!
UPDATE: when multiple Django applications are configured under Apache apparently a request could be routed to the Apache instance hosting wrong application. The server will not load application and Apache log files would show:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/core/handlers/wsgi.py", line 219, in __call__
self.load_middleware()
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/utils/functional.py", line 184, in inner
self._setup()
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.7/dist-packages/Django-1.4.3-py2.7.egg/django/conf/__init__.py", line 95, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'xxx.settings' (Is it on sys.path?): No module named xxx.settings
basically failing to load settings from the wrong application. More on this topic in this blog post, but the fix seem to be specifying WSGIDaemonProcess per application.