Category Archives: Programming

Spawning Django Subprocesses

We have some maintenance tasks that require some run time, that we’d like to launch from the web browser. Programmatically, the most natural thing is to spawn a process that performs the task and completes asynchronously. The results are recorded in the database for later harvesting.

As far as I can tell, the same general rules for forking apply when forking from within Django: close database connections, close open file handles, and release other resources that cannot be shared across process boundaries.

Django, apparently, will automatically re-connect to the database if the connection is closed. This makes the job much simpler. Different web sites say that the parent process should close its database connection, and others say that the child process should close its database  connection.

In the face of this conflicting information, I chose to close the parent process’ database connection before calling os.fork(). Reöpening database connections incurs a small penalty that are not a concern as this is done once.

Before forking:

[python]
from django.db import connection

Don’t fork with a database connection open.

connection.close()

new_pid = os.fork()
if not new_pid:

Child process

[/python]

Thus far there seem to be no side effects from taking this approach. As always, additional information is welcomed.

Reverting File Changes With git

This is not obvious to those of us with lingering Subversion habits comprar viagra madrid. If you’ve edited a file and simply want to discard its changes (à la Subversion’s revert), use:

git checkout filename

If it so happens that your file name is the same as a branch, you’ll need to use:

git checkout -- filename

Use the Right Tool for the Job

You may be tempted to use git reset --hard, but that will reset all uncommitted changes. If you just want to undo the changes to a single file, that’s the wrong tool.

 

Updating Array Elements in CoffeeScript Loops

Notes on updating array elements in CoffeeScript loops.

CoffeeScript doesn’t use the JavaScript C-style for(;;) loops. When one wishes to conditionally modify the array that’s being iterated over, how one might go about it may not be obvious at first.

If I wish to update an array without otherwise disturbing it, the “obvious” answer will not work:

[ruby]
shouldUpdate = (colour) ->
…. details ….

colours = [‘red’, ‘green’, ‘blue’]
for colour in colours
colour = "foo" if shouldUpdate(colour)
[/ruby]

This is because the compiler produces code like:

[javascript]
for (_i = 0, _len = colours.length; _i < _len; _i++) {
colour = colours[_i];
if (shouldUpdate(colour)) {
colour = "foo";
}
}
[/javascript]

Note that in line 2 the value in colours[_i] is copied to a separate variable colour, thus assigning to colour will not affect the array itself.

If we wish to modify the array in place, then we can tell CoffeeScript to provide us with the index thus:

[ruby]
shouldUpdate = (colour) ->
…. details ….

colours = [‘red’, ‘green’, ‘blue’]
for colour, index in colours
colours[index] = "foo" if shouldUpdate(colour)
[/ruby]

There may be a slicker way to do this, but for now it suits my purposes.

 

Django 1.7, PyCharm, and Apache

I’ve had some ongoing problems with PyCharm support for Django since the 1.7 release. Here is a summary.

WSGI Broke

The entire app failed to run under Apache. This caused some moments of terror. The WSGI file needed to be edited.

This code started on version 1.3. The old WSGI configuration worked until 1.7. This discussion thread described the behaviour, which bites projects started pre-1.4. See also Issue #23437 describes the WSGI problem, and the official release notes under app-loading changes.

The correct call to initialize WSGI is:

[python]
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
[/python]

 

PyCharm Testing Broke

Tests in PyCharm broke. This a case of being bitten by older code again. Django uses the standard Python unittest module now. I had to go through dozens of files and tweak the import statements.

Update: LH asked for more information.

In my unit testing, I used the old-style Django test classes and had import statements like:

[python]
from django.test import TestCase
[/python]

which must be changed to:

[python]
from unittest import TestCase
[/python]

in order to use the built-in Python unit testing core.

PyCharm Django Console Broke

This is kind of annoying. The problem isn’t so much PyCharm itself but Django “fixed” a long-standing “bug”. Now what used to work fine doesn’t. Per the app-loading changes in the 1.7 release notes, one must, every time they start the Django console from within PyCharm, execute the following commands:

[python]
import django
django.setup()
[/python]

This became annoying enough I hacked the Django helper

/Applications/PyCharm.app/helpers/pycharm/django_manage_shell.py

and added the two lines to that file. Problem resolved.

 

Display a Server-Supplied Drop Down List Using AngularJS

These are my notes on displaying a list of server-supplied objects in a drop down list using AngularJS.

Background

I have a server that supplies lists of lookup objects that are used in an AngularJS-based single-page application (SPA). The SPA obtains a list through an API call to the server. The server returns an ordered list of JSON objects. Every object in every list includes a key value, a display value, and supplementary data. For the purposes of this article, only the key and display values are of any concern.

For example, the SPA needs a list of units of measure. The server supplies a list of objects along these lines. They key value is called code and the display value is called display:

[javascript light=”true”]
[
{
code: "L",
display: "L",
description: "litres"
},
{
code: "ML",
display: "mL",
description: "millilitres"
},
… etc …
]
[/javascript]

In the SPA code, each lookup table is wrapped in its own Angular service.

From List of Objects to Dropdown Using <select>

Angular can be told to create a dropdown list using an array of objects thus:

[html light=”true”]
<select ng-model="product.uom"
        ng-options="u.display for u in units" />
[/html]

Here ng-options tells Angular to build the dropdown list showing the display attribute of each object. Whenever the user chooses an item, the entire associated object is stored in $scope.product.uom (uom means units of measure). For my purposes this is very handy since I want access to the entire object.

Defaulting to a Value

This works beautifully until an edit page is shown. When displaying data from the server, the dropdown shows a blank selection even though $scope.product.uom contains an object with all the correct values!

The problem is that Angular matches based on object references, not object contents. This can be illustrated thus:

[javascript]
var a = {foo: "bar"};
var b = {foo: "bar"};
var c = a;
[/javascript]

Variables a and b contain two separate objects that by chance have attributes with the same values. Variables a and c contain the same object pointer.

In the example above, Angular will recognize the value in $scope.product.uom only if it points to an object in the master list $scope.units. The fact that the server-supplied object has identical attributes is irrelevant — Angular only cares whether the object pointers are identical.

[Edit]

There are a couple of ways around this. If there is a unique key on the object, one can use the AngularJS track by clause to tell AngularJS to compare the keys. Continuing the above examples, each object has a code attribute that has a unique string which JavaScript can compare.

[html light=”true”]
<select ng-model="product.uom"
ng-options="u.display for u in units track by u.code" />
[/html]

[Original Article]

Otherwise,  when an object is loaded from the server for editing, the lookup values are replaced with pointers to the corresponding objects in the dropdown list. An unsophisticated but functional bit of code to perform this substitution might be:

[javascript]
// Wrapper function to retrieve a product
// from the server, keyed on productId.
apiProduct.lookup(productId, function(product) {
$scope.product = product;

// Replace the server-supplied lookup value
// with the matching value
// in the $scope.units array.
$scope.product.uom = lookup_by_code(product.uom.code, $scope.units);
});

function lookup_by_code(code, data) {
for(var i=0; i
if(data[i].code == code)
return data[i];

return null;
};
[/javascript]

Update

There is a JSFiddle that demonstrates the value/reference problem concisely.

Upgrading Node.js using npm

The Node.js ecosystem provides a tool to update Node from within npm called, simply, “n”.

Install n thus:

[bash]
sudo npm cache clean -f
sudo npm install -g n
[/bash]

I don’t know that clearing the cache is actually necessary, but a number of people have recommended doing so.

Update to the latest version of node using:

[bash]
sudo n stable
[/bash]

n allows node versions to be changed easily. The n package listing has details.

Strange npm Errors

I’ve gotten some strange errors with npm which were resolved by clearing npm’s cache. The brute force method is:

[bash]
sudo npm cache clean -f
[/bash]

This falls under the same category as strange C/C++ behaviour resolved by removing all .o files, strange Python behaviour resolved by removing all .pyc files. Caching or otherwise keeping around intermediaries is a boon for speed, but can bite when the cache gets stuffed up.