Archive for the ‘how to’ Category

Making a decision – flowchart

Tuesday, June 5th, 2012

If you’re having trouble deciding whether or not you should do something, Supernifty’s new “Should I do it?” flowchart can help you out with many of life’s conundrums.

First, think of something that you are wondering whether or not you should do. For instance, accepting a new job offer. Then, follow the flowchart below.

Should I do it? Decision flowchart

Of course, sometimes making the decision is the easy part – especially now that you have this simple to follow flowchart.

Often it’s actually following through on your decision that is the challenge.

Python localization made easy

Friday, September 16th, 2011

Here’s a set of simple steps to localize a Python application for different translations. This tutorial provides a clear set of steps with sample code.

Step 1: Initialize your application

Here is the code to initialize your application with localization enabled:

If you can’t see the source code try here.

This snippet looks for a resource file based on the users locale. For instance, “res/” for English. If it fails to open the appropriate translation file, it falls back to NullTranslations, which simply performs no translation.

Step 2: Prepare your application for translation

trans.install() generates a global function available to all modules in your application: _().

Find all the strings in your application that you wish to translate, and wrap them with the _() function.

i.e. “Hello” becomes _(“Hello”).

This applies to parameterized strings as well. e.g. “Hello %s” % name becomes _(“Hello %s”) % name.

Step 3: Generate the pot

That’s messages.pot.

Run the command xgettext *.py or pygettext *.py. Under Windows, you might have to look for this tool. Under the Python installation directory, try Tools/i18n.

This command looks for all strings inside the _() function, and generates the file messages.pot.

Step 4: Translate

Send your generated pot file to your translator. They will replace the empty strings with the appropriate translations and return the file to you.

Step 5: Generate the mo

Save the returned file to reflect the new language that your application has been translated to. e.g. messages_De.po

Run the command msgfmt -o res/ messages_De.po to generate the required .mo file.

As with pygettext, if your system doesn’t find this command, look in Tools/i18n under the Python installation directory.

After running this command, the translation file required by the application will be in the res directory. When you’re distributing the application, make sure the res directory goes too.

Step 6: Test

On Windows XP, you can change your locale with the following steps:

  • Start->Control Panel->Regional and Language Options;
  • Under “Regional Options”, choose the locale for the translation file you have created and click “Apply”;
  • Start the Python application;
  • (Hopefully) enjoy your translated application!

In summary…

It’s straightforward to setup localization with Python once you know how.

Getting started with PayPal on Django

Thursday, January 27th, 2011

DjangoIf you’re a Django (or Python) developer, and want to integrate a payment solution into your web app, check out this tutorial by Supernifty: Getting started with PayPal on Django.

It covers the steps involved in getting set up with PayPal and building a simple online store with Django.

The sample code is also available from GitHub.

In summary:

py2exe, python 2.6 and wxPython – hack it #7

Tuesday, July 6th, 2010

If you’ve tried to build a client application with Python 2.6 and wxPython, you might hit a problem.

Python 2.6 no longer includes the Visual Studio dependencies required by wxPython. These dependencies may not be present, either on your build machine, or the target user’s machine.

When building with py2exe, you may encounter an error similar to:

error: MSVCP90.dll: No such file or directory

The solution!

To successfully build your executable, you need the Visual Studio 2008 redistributable package, which can be obtained from Microsoft. Run this on your build machine to install the appropriate DLLs.

If your build still fails, you may need to copy the DLLs into your build directory. Locate msvcp90.dll, msvcm90.dll and msvcr90.dll on your machine and copy them to the build directory.

Once you have a successful Python build, you also want your executable to run on the target machine. To do this, your install script needs to install the vcredist package as part of the installation.

The command you need to run as part of your client installation is:

vcredist_x86.exe /q:a

If you are using NSIS as your installer, the appropriate code is:

File /r vcredist_x86.exe ;to copy the file
ExecWait ‘”$INSTDIR\vcredist_x86.exe” /q:a”‘ ;to install

With the Visual Studio redistributable installed, it will now execute successfully on the target machine. Hurrah!

Remember me with PHP – hack it #6

Monday, March 15th, 2010

PHP persistent sessions
“Remember me” functionality was recently implemented on the Supernifty login screens. The PHP documentation is surprisingly non-obvious so here’s how to do it.

If you’re already using the $_SESSION variable, the default is for this variable to live only for as long as the browser. Once the user closes the browser, the cookie will be deleted, and they’ll be logged out.

To remember a user across browser sessions, here’s what needs to be done.

Tell the browser to remember the session

You do this with the line:

setcookie( session_name(), session_id(), time() + 86400*30 );

This overwrites the default session cookie, with a cookie that will persist on the user’s browser for one month.

Note that since cookies are set in the headers sent to the browser, this needs to be done before writing any content to the browser.

With HTML like

<input type="checkbox" name="remember" value="remember"/>

You could have a handler like:

$remember = $_POST['remember'];
if ( isset( $remember ) && $remember == 'remember' ) {
setcookie( session_name(), session_id(), time() + 86400*30 );

Tell the server to remember the session

Bizarrely, PHP’s default session time is something ridiculous like 24 minutes.

To tell the server to remember the session for longer, you need to set some PHP environment variables, like so:

ini_set("session.gc_maxlifetime","2592000"); // 24*30 hours
session_set_cookie_params( 86400*30 );

More details about PHP’s session lifetime

With these two pieces in place, you can now implement a “remember me” checkbox on your login screen with PHP, while continuing to use the $_SESSION variable.

Announcing Supernifty Playalong

Sunday, March 8th, 2009

Supernifty is excited to announce that Supernifty PlayAlong is now available.

Playalong plays the song for you, while dynamically displaying chords and lyrics as the song progresses. It’s a combination of YouTube and the Supernifty chord finder.

It’s the easiest way to learn or practice a new song, or play along to an old favourite, with either your guitar or ukulele.

Easily move around the song with the video controls, and you can quickly transpose the song to any key.

Create your own playalong song for the Supernifty song database with the song authoring tools. Import either existing ChoPro files, or previously saved playalong files.

A number of enhancements are coming to Supernifty Playalong, including versions for the PC and Mac, and mobile versions, so you can play along to your favourite songs wherever you are. Stay tuned!

In the meantime, try it out! Feedback or questions are welcomed, especially if you are adding a new song.

Anti Zen – Sage Advice #1

Wednesday, October 15th, 2008

Anti zen cream
For some time I’ve held an interest in Buddhism and Zen, and have read a number of books on the subject. I’m also a reader of Zen Habits, by Leo Duchovny, David Duchovny’s younger brother.

I would argue that Leo’s blog has very little relationship to Zen (*cough* false advertising *cough*), and in fact I would suggest that a more appropriate title would be “Nice guy habits”. But that doesn’t really have the same ring to it.

Zen isn’t really about being nice, or being organised, getting things done, being productive, or any of that hoo-ha. Why do people even care so much about these stupid things? Being productive? Woohoo, way to go, mark that down as your headstone quote.

Zen, without the Buddhism, is simply about living in the present moment, and meditating a lot to get good at that.

So, with that out of the way, Supernifty will now, as is the fashion in blogs, preach with a list of ways to be awesome.

1. Be nice

Ever hear the saying, “nice guys finish last”? Well, it’s true. This means that being nice results in a double whammy of suckfullness. First, you have to be nice. Second, you come last.

Being grumpy, and bad, is the alternative personality to aim for. Grumpiness is fun, and helps you live longer. Also, grumpy people are pretty funny. John McEnroe for instance. His grumpiness gives him an ongoing income stream: every time someone says “you cannot be serious” on commercial television, he gets a dollar.

Girls love bad. You just have to watch TV to know this. There’s even scientific evidence indicating as such.

Unfortunately, if you are too bad, you might end up in jail. Supernifty life tip: avoid jail. This means aiming to be in the smart bad end of town, the guys that don’t get caught. If you think you might be in the stupid bad category, maybe you should just be good instead.

2. Patience is a virtue

I’ve spent a lot of time waiting around for stuff and generally, it’s a waste of time. Time is the one thing you can never get more of in your life so don’t spend it being patient. Seize the day, and don’t wait another moment to go fix yourself a big sugary treat.

3. Only the good die young
Actually no, it’s mainly the bad guys dying young by killing each other, and themselves. Drinking, smoking or shagging themselves to death. Not taking care of themselves, driving too fast, taking stupid risks. All that stuff is the domain of the baddies.

But doesn’t it sound like fun? You only live once, do you want that one life to be the boring, long, life of the nice guy?

4. Live in the moment
Zen tells us the only moment is now.

So forget the past, and forget the future (i.e. consequences of your actions). Do whatever the hell you want, and do it right now.

* Please don’t take all aspects of this article seriously. I actually quite like Leo’s blog.

Backups – Hack It #4

Wednesday, May 21st, 2008

Keep copies
Backups are an essential feature of any IT installation.

You’ve got to have backups. Not backing up is a ridiculous, unnecessary risk.

The average hard drive has a 20% chance of failure each year. How would you fare if your computer was stolen, or melted in a fire? If you don’t have a plan for data recovery, you are asking for trouble.

It shouldn't happen to you!

Try to put a value on losing everything on your hard drive, and remember that fires, robberies and hardware failure are all out of your control.

For instance, a friend recently lost all his photos. Over 10 years worth of memories, gone forever! Man, he was sad.

These days there are great, inexpensive options for backups.

Option 1: buy an external hard drive.
This is a big improvement on not backing up at all, however, there’s a problem. If you keep the external drive with your computer, you’re not covering all the bases.

Any localized event, such as a natural disaster or robbery, will render your backup solution useless.

If you copy your data off your computer, then take your external hard drive away to another location, then this is a fine solution. Otherwise, it’s not recommended.

Option 2: online backup
In recent times, some very competitive options have sprung up. This stores your data safely away from your computer, so this is a winner – you can’t lose both in the one event. The main factor in my opinion is security.

  • Mozy – unlimited storage for $4.95/month.
  • Carbonite – unlimited storage for $49.95/year.
  • Amazon S3 – incremental storage of 15c/GB/month. This is not on its own a backup solution. You need a user-friendly client to connect you to their backup service. For instance, JungleDisk.

I chose S3 and JungleDisk, primarily for security reasons. Most solutions tell you that your data is safe and encrypted, but that they can recover your data if you lose your password.

This means that if the company is hacked, or if you don’t trust company employees, your data is vulnerable. JungleDisk has the option to encrypt your data before it leaves your computer, and cannot be decrypted without your password.

It’s worth pointing out, that with Amazon’s incremental pricing, if you only need to backup a few gigs, then you’ll be paying less than $1/month to keep your data safe.

It’s a tiny price to pay for the guarantee that your data will not be lost.

Tutorial: Tuning with Audio Tuner Mobile

Friday, February 29th, 2008

A tutorial on how to use Audio Tuner Mobile to tune your musical instrument is now available.

If you can’t see the embedded video, watch it at YouTube