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”

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”

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.

Setting up Dango i18n, i10n

Anything related to i18n/i10n subject seem to be somehow quirkier than it looks at first glance. Python (2.x) itself and handling of unicode is a story apart.

This time I was looking into building a small website that has to provide UI in different languages. As this is one of the things you want to have right away I’ve started experimenting with adding i18n and i10n support.

First step is easy, the settings.py already had proper settings. Then for .py files it is rather straightforward to add e.g. (for forms):


from django.utils.translation import ugettext_lazy as _
...
city_name = forms.CharField( required = False, label = _('City:'))

For the .html files something like


{% load i18n %}

{% trans "Hello there!" %}

Then create under project folder folder ‘conf/locale’ (if you don’t do this it will complain), and then run

django-admin.py makemessages -l ru

Edit the resulting django.po file, add translations to your messages.

Warning: don’t forget to edit the following field, which comes EMPTY first, even while you have given it a parameter! Otherwise this file will be not used properly.

"Language: ru\n"

Then compile your nice and shiny translations:

django-admin.py compilemessages

Now we get all messages available. At least they should. But there is another trick missed in the Django documentation/tutorials: you HAVE TO specify the location of the message files explicitly in your settings.py otherwise your texts will continue coming up in default (en) language no matter how hard you try. E.g.:

LOCALE_PATHS = (
os.path.join(os.path.dirname(__file__), 'conf', 'locale').replace('\\','/'),
)

Well, after all this it seems to work. But it costs quite some searching and poking around to come to this. I can imagine after several rounds this becomes obvious, but you don’t get any errors, warnings, whatsoever, it just does not what you want it to do. Well, I hope it will do it for you now :).

Happy Djangoing!