Python: Calculate percentages

To convert variables into percentages in Python, you need to ensure that it is calculating with floats (or doubles or whatever it is its using)

percentage = (count / total) * 100

Will keep returning 0.

percentage = (1.0 * count / total) * 100

This is the right way of doing it and works fine.

If anyone knows a better way of doing it, let me know!

[ Source ]

Django: Queryset using unnecessary extra tables

When using QuerySet.filter(), be sure to group operations on one table together. Otherwise, you may run into duplicate results due to tables being referenced more than once.

The following (simplified) example causes table A to be included more than once:

q = TestModel.objects.filter(user__firstname = 'John')
q = q.filter(user__surname = 'Smith')

This will cause the table "user" to be included in the SQL query twice. Using "print q.query" to debug:

SELECT testmodel.*
FROM testmodel
INNER JOIN user ON (testmodel.user_id =
INNER JOIN user T3 ON (testmodel.user_id =
WHERE (user.firstname = 'John' AND
T3.surname = 'Smith')

Now this isn't exactly ideal, because we want the person with the name "John Smith", not a combination of anyone with the first name "John" and anyone with the last name "Smith".

Instead, we should group the filters into one so it applies to the one table.

q = TestModel.objects.filter(user__firstname = 'John', user__surname = 'Smith')

Which produces the following SQL query:

SELECT testmodel.*
FROM testmodel
INNER JOIN user ON (testmodel.user_id =
WHERE (user.firstname = 'John' AND
user.surname = 'Smith')

This will return all users with the name "John Smith".

When you DO want the query to reference the same table multiple times, use multiple another filter but remember to restrict it accordingly.

This doubling of tables is especially helpful when a system uses "tags" to help categorise information into various categories.

Linux: Use screen to keep stuff running in background

Its useful to run stuff such as servers in the background without having to open a new console window.

To do that, in your console type:


Now just do whatever you need to do.

When you want to go back to doing other things, press CTRL+A and then "D" to switch back to your console.

To reconnect to the screen and stop the server, type:

screen -r

Python: Scrape pages and extract information

I was amazed at how incredibly easy it was to scrape pages using Python.

To download the page markup, use:

import urllib
content = urllib.urlopen("").read()

Once you have the content, simply use regex to parse the bit you want.

import re
m ='class="pr".*?>(.*?)<', content)

if m:
quote =

[ Source ]

SEO: Valid sitemap links not being indexed

Submitting a sitemap index to Google or Bing can tell them where to look, but small errors may prevent your URLs from being indexed.

In my case, the total URL count was 117 and the indexed URL count was 0, none, nadda. Whyyyyy!?


I eventually found that my sitemap links had "www." appended to the URLs, however the domain it was submitted under did not include the "www".

Now I also needed to submit the "www." and non-"www." version of my site to webmaster tools in order to get it indexed properly.

Changing that fixed the problem and it took about 3 days to start geting indexed.

There is also a nice checklist here, but it doesn't include the "www" note.

[ Source ]

Python: Exit script

To terminate or stop a Python script, use:

import sys


[ Source ]

Python: Reading arguments passed into script

When writing Python scripts, sometimes you will need to read in the arguments given.

import sys
print sys.argv

Using pop() to retrieve the information, we pull out the stuff we need.

For example the script was called using:

python mypass

Then the arguments can be retrieved using:

args = sys.argv

args.pop(0) # filename for current script
email = args.pop(0) # username/email
password = args.pop(0) # password

[ Source ]

Django: Limiting choices in a dropdown select field

In an admin form, you can limit the choices in the select dropdown field by setting an option in the model.

Set the option "limit_choices_to" to a filter of some sort.

limit_choices_to = {'pub_date__lte':}

Refresh and you will notice that a number of options in your dropdown will be removed.

This also applies to other list type elements.

[ Source ]

Python: Replace text in a string

Straight forward snippet to replace some text within a string.

import string

string.replace("TAB", "A", "B")

This will produce "TBB"

[ Source ]

Django: Change widget in admin form

When you use a TextField in your model, the automatic generation for the form uses a TextArea element, which isn't always ideal.

In those instances, you will want to change the control widget so it displays as a different control type.

from django.contrib import admin
from django import forms

class SkinAdminForm(forms.ModelForm):
# Modifies the image_path field so its smaller.
image_path = forms.CharField()

class Meta:
model = Skin

# Admin to allow user to create skins
class SkinAdmin(admin.ModelAdmin):
form = SkinAdminForm, SkinAdmin)

Although the "Skin" model has more than just the image_path field, changing image_path to a CharField will not affect anything else.

[ Source ]

Bash: Find the Nth character in a file

cat "filename.html" | head -c 39950 | less
  • Cat will read the file out
  • Head will truncate it to the Nth character you want.
  • Less will stop the output from flooding your console.

That Awesome Shirt!

The best shirts from a collection of online stores rolled into one site for you to browse.

Think of it as a Failblog or FML for shirts, but in a good way!

  • Tag shirts you want to buy so you're ready to buy as soon as the sales begin!
  • Rate the shirts you like.
  • Share shirts with other people.
  • Find out which stores have been good to customers and which dodgy stores to avoid.

Become a fan on Facebook here!

Drupal: Remove $_GET args from pager

theme_pager() is quite handy, but it will also append alot of form information that you may not need into the page links.

To remove the fields from the URL, prior to your theme_pager() call, unset the variables from $_REQUEST.

// Remove search-button from the pager args
if (isset($_REQUEST['search-button'])) {

After that, they should no longer appear in your pager.

Mobile Phone Development: Detailed list of phone capabilities and technical specifications

Creating a mobile phone application (or mobile friendly website) can be an absolute pain in the ass. Each phone has a slight variation in what it does and does not support.

There is a handy database for mobile phone (or as the yankees call them, "cell phone") specifications called WURFL.

The configuration file they provide lets you know capabilities of a phone such as what HTML support, CSS, Ajax, display size and color depth, image/audio/video formats supported, storage method, streaming support, MMS, J2ME and Flash/PDF support.

Not to mention the basics such as manufacturer name, model, keyboard type, operating system and wireless capabilities.

The documentation page is a bit difficult to find, but linked here (mainly for my own reference).

Be warned, it is a comprehensive list so it takes quite some time to process it. You should not be accessing it on a per view basis.

[ WURFL site ]

Facebook: Photo Uploader Plugin Crashes Browser

The new Facebook photo uploader is nice. Really, it is. However, I've noticed that once I use the photo uploader successfully and then try to add another batch of photos (to either the same or different album), it will cause Firefox to crash in a not so wonderful manner.

Exception caught!

The exception unknown software exception (0xc0000409) occurred in the application at location 0x0944167d.

In that regard, did you guys really have to use a plug-in?

Clicking OK only makes things worse.


The instruction at "0x011f8055" referenced memory at "0x09695e90". The memory could not be "written".

And then... spoiler alert!


I have noticed it happen on Windows XP and Windows 7, both running Firefox v3.5.9 and the Facebook Plugin v1.0.3.0 (The file which crashes is npfbplugin_1_0_3.dll).

This really is an annoying bug, and I'm pretty tempted to whip up a quick program to upload photos straight from the desktop which doesn't crash after one use and maintains the chronological or filename order.

I would usually suggest posting errors on the Facebook Photo "known bugs and errors" forum, but there never seems to be a response from any Facebook staff at all.

So, I suppose the next best thing would be to send them a direct message using this form.


If that doesn't work, we can always resort to threatening them with psychotic nub-armed and dangerous midgets.

*update - 30/05/2010*:

It appears that Facebook has fixed this problem and it was a server side issue of some sort, because I'm still sporting v1.0.3 of the photo uploader.

I've left the plug-in enabled for now. I wonder if it still crashes on random sites...

Block Patterns In robots.txt

It's simple enough to block a search engine from crawling a fixed URL or folder, but case your URLs are dynamic such as /node/345/edit, you will need to define a pattern instead.

Use the following to block patterns:

Disallow: /node/*/edit$

The trailing "$" ensures that the URL ends with "/edit", otherwise any URL containing "/edit" will be blocked.

1179659066772 Now you can kick any incoming crawlers that try to visit those URLs into the bottomless pit in the middle of your town!

[ Source ]

Register Your Site With Search Engines

Submitting your site to a search engine will help the search engine crawl your site and let you configure some options for each search engine.

Bing and Google will also send you notifications if they find any errors with your site.

Google Labs also has some performance profiling tools to help determine some slow points in your website.

Submit your sites at the following URLs:

Each will require you to verify your site. The easiest way to do that is to simply paste a meta tag they provide into your output HTML.

Note: Yahoo generates a meta tag that is not self closing. Be sure to add the closing "/" at the end of the meta tag.

Once verified, you can start optimising the search engine for your website.

Remember to configure your "robots.txt" file to disallow any important folders or URLs.

Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog