Multi-line Comments In Python

13th April 2012 - 2 minutes read time

Python doesn't officially support multi-line comments, but there is a way of implementing the same functionality using an existing language construct. Single line comments in Python are written like this:

# This is a single line comment.

Multi-line comments are not officially supported in Python. That said, you can create the same effect in Python by using a multi-line string. Unless it is part of a docstring at the start of a class, function or module then it parsed into a string variable, but nothing is done with it.

Stopping Code Execution In Python

13th April 2012 - 2 minutes read time

I am currently in the process of learning Python, so I thought I would start a series of mini blog posts detailing different things that I have found useful whilst learning how to use the language.

To stop code execution in Python you first need to import the sys object. After this you can then call the exit() method to stop the program running. It is the most reliable, cross-platform way of stopping code execution. Here is a simple example.

Tip On Adding Code To Drupal Forms

7th April 2012 - 2 minutes read time

There are various different forms and modules in Drupal that allow for PHP code to be embedded into them. I have even talked about adding PHP code to forms in a previous post. These form elements can have their uses, modules like Views allow for PHP code to be run when collecting arguments which can allow for some advanced funcationality.

However, it can lead to problems. The code in the form is essentially outside of source control which means that anyone can mess about with the code and there is no way to revert changes or get back the code if the form save action failed for whatever reason.

Using Phing To Create Apache Virtual Hosts

6th April 2012 - 10 minutes read time

Phing is an awesome tool for automating things and I use it more and more for automating all kinds of different tasks. One of the tasks that I don't tend to do all that much is setting up a new local virtual host for Apache on my development machines. I know how to do it, but there is always something I forget to do, or a convention that I don't follow which means that I have to repeat myself at a later date to fix something I have missed.

So to make my life a little easier I decided to create a Phing build file to automatically create a virtual host and the everything associated with it in one go. Essentially, I would need to do the following tasks:

Adding A Context Menu Item For Phing On Windows

21st March 2012 - 2 minutes read time

To make things a bit quicker when using Phing on Windows use the following registry entry to create a right click option that integrates with Phing.

Getting Started With PHP DocBlox

20th March 2012 - 6 minutes read time

DocBlox is the new defacto PHP class documentation generator. It was developed as an alternative to PHPDocumentor, but it looks as though it will replace it and become the new PHPDocumentor2. As a result I thought I would put together a quick tutorial on getting started.

This post assumes that you know what PHPDoc comments are and that you have a project that you want to generate API documentation for. To get started on PHPDoc take a look at the Wikipedia page on PHPDoc.

Whereas PHPDocumentor would scan and generate all of the code in one go, DocBlox will scan the source code and generate a series of XML files based on the code it finds. It will then use these files to generate the API documentation in a number of different formats, although HTML is probably the most common.

Local And Global Variables In JavaScript

18th March 2012 - 4 minutes read time

I have seen a lot of this sort of thing, so though I would put together a quick lesson in JavaScript variable scope. There are some important differences between local and global variables in JavaScript that will cause grey hairs if you don't know what's going on.

Scope is something that dictates what variables can be seen by code. If a variable is created inside a function then it can be said to be local as only the locally running code can see it. A variable that can be accessed by any part of the code is said to be global. This is special in JavaScript, which I will come onto later.

This scope concept is important as if a variable is outside the current scope then the code can't see it. Everything in JavaScript is a variable so understanding how and when variables can be accessed is important.

To create a local variable in JavaScript do this:

Adding The autocomplete Attribute To Forms And Password Fields In Drupal 6

22nd February 2012 - 3 minutes read time

Many modern browsers now come with auto-complete functionality so that users can fill in their details quickly without having to type in their username and password every time they want to log on. This can be turned off by adding the attribute "autocomplete" to the form and password elements and setting its value to 'off'.

Setting the autocomplete attribute to off on password fields (and forms containing password fields) can add an added level of security to your Drupal site. This is especially important as if the computer is stolen it is more likely to contain saved passwords that will allow access to sensitive systems. So turning this feature off might be beneficial for certain systems, especially those with very sensitive information.

How To Change No Search Results Text In Drupal 6

2nd February 2012 - 2 minutes read time

Every Drupal project I finish will usually have the same request at some point. This usually happens when the client tries to do a search that produces no results and sees the search hinting text about blue smurfs.

The text about blue smurfs that is printed when there are no search results is called directly in the search module so it isn't possible to edit or override it. The solution is to use the theme_box() theme hook and override the text just before it is sent to the page. Just drop the following code into your theme.php file, rename the function to fit your theme and clear your caches.

IE8 Downloading docx Files As zip On Apache

23rd January 2012 - 2 minutes read time

I recently built a Drupal site on an Apache server setup and everything seemed fine until someone running IE8 tried to download a .docx file. For some reason IE8 insisted that this file was a .zip file and would open it as such, causing a bit of confusion as to what the problem was. I assumed that this was due to the mime type of the file not being set properly, but I decided to do a quick search to see if I could find anything to corroborate this. I thought it might be something odd that IE8 was doing as everything else was downloading the file perfectly.

After wading through multiple support forums and finding very little of any help I eventually gave up and went with my first hunch and set the mime type for the .docx file extension in the sites .htaccess file. Here is the rule I used: