Programming articles

Why use Virtual env wrapper and how!

Having worked on many python/Django projects over the last few years I have come to be very familiar with virtual environments as the easiest way for me to manage multiple project requirements on just one machine. Without them my main python packages folder would be constantly fighting over different versions of Django, PIL and every other library under the sun.

Virtual environments are one of the best ways to create multiple unique development environments on one machine as they keep each projects packages contained with the specific versions required all in one folder.

Once you have multiple virtual environments it starts to get a bit complicated, especially if you have to switch between multiple ones in a day. This is where virtualenvwrapper becomes so useful. virtualenvwrapper adds an extra management layer on top of the virtualenv so that you can navigate to and start the environments with a single command. It also helps to organise your environments by keeping them in one location (previously I kept environments next to the project)

I’m not ...

Read post... Posted in 'Programming'

Updating node.js using npm

Node.js is a rapidly developing open-source cross-platform runtime environment. Because it’s actively developed there are often updates to the current version. Because of this lots of packages end up using versions of node.js that are newer than the one on your machine, and that can mean that the the latest plugins and utils will end up breaking.

To update node.js you can either download the installer from the node.js website or you can type a few lines of code and away you go!

Check the current version:

node -v 
—> v0.10.0

Update npm and install the latest node.js version. You can also specify a particular version of node by replacing 'stable' with a specific version number such as '0.11.00'.

sudo npm cache clean -f  
sudo npm install -g n  
sudo n stable  

Check the latest version just to make sure it’s worked:

node -v
—> v0.12.2
Read post... Posted in 'Programming'

Installing MySQL-Python on OS X with brew

OS X can be an absolute pain to get MySQL running so when you have finally resorted to using brew to install its, as I have on many occasion there is another pitfall you might face when you try to install other python packages that need to know the MySQL path.

The best and probably most common package that this happens to seems to be MySQL-Python so the following example will be what I use to demo the fix.

Assuming your virtualenv is up and running (or your installing straight on your machine) we need to do the following:

export PATH=$PATH:/usr/local/mysql/bin
pip install MySQL-Python

MySQL-Python (or your other MySQL needing package) should now install and you can get back to coding up some awesome websites.

Read post... Posted in 'Programming'

Swift timer

This is a simple timer, using Swift.

var theTimer = NSTimer()

//function to start the timer (if needs be)
func startTimer() {
  //start the timer with a 5 second delay
  self.theTimer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("timerIsDone"), userInfo: nil, repeats: false)
}

func stopTimer() {
  //start the timer with a 5 second delay  
  self.theTimer.invalidate()
}

//The function called when the timer is done
func timerIsDone() {
  println("Do something")
  stopTimer()
}

//start the timer - this can be in called as needed from another function or an init
startTimer()
Read post... Posted in 'Programming'

Installing specific versions with easy install

Recently I have been working on quite a few projects that require different versions of packages to run. Each setup has been running in a virtual environment (that is a post for another day) and setting them up with the required package rather than the latest that easy_install can find is really simple.

    easy_install "django == 1.3"

That will find and install Django version 1.3... simple as that!

Read post... Posted in 'Programming'

Flipping a UIImage

Flipping an image using objective-c is so easy once you know how...

//Create a normal image
UIImage* img = [UIImage imageNamed:@"myImage.png"];
//Flip it
UIImage* flippedImage = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:  UIImageOrientationDownMirrored];

That's it! Now to have it as an method we can use more conveniently, try this:

-(UIImage *)flipImage:(UIImage *)img {
    UIImage* flippedImage = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:  UIImageOrientationDownMirrored];
    return flippedImage;
}

Then we can send any image to it and it will return a flipped version.

Read post... Posted in 'Programming'

Basics of the NSNotificationCenter

The uses of NSNotificationCenter can be many; It is handy for using to notify the app that a background download of data, a post of some kind is done, or some calculation is finished. It can also be useful for letting sub views know when some root level functions happen in the app such as shutdown/sending to background because the user tapped the home button. In this example I will be doing just that.

In the AppDelegate you should find a method named:

- (void)applicationDidEnterBackground:(UIApplication *)application

This is called when the app starts the process to pause into the background state. Since this method can only be called by the app delegate and not a sub view notifications are ideal for subviews so that they can do what they need such as save data or stop heavy processing of data.

//.m
- (void)applicationDidEnterBackground:(UIApplication *)application{
   [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidEnterBackgroundNotification" object:self];
}

'UIApplicationDidEnterBackgroundNotification' is the name of the notification we send out. If we name notifications in a smart way it makes sense when ...

Read post... Posted in 'Programming'

Accelerometer setup and usage

To use the accelerometer in an iPhone application you need to add the <UIAccelerometerDelegate> into the .h file of your application:

@interface ApplicationViewController : UIViewController {
...
}

Then in your .m file you must setup the accelerometer:

- (void)viewDidLoad {
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1/30;
}

Update interval is in seconds so 1/30 is a 30th of a second. Now the accelerometer is setup you can use it:

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

xValue = acceleration.x;
yValue = acceleration.y;
zValue = acceleration.z;
}
Read post... Posted in 'Programming'

Creating and using an Array of objects

First we need an array to hold the objects (in this case bullets)

NSMutableArray *myBullets = [[NSMutableArray alloc] init];

Create and then add the objects

[code lang="c_mac"]
for (int i=0; i<5; i++){
//create
     Bullet *newBullet = [[Bullet alloc] init];
//add to the array
     [myBullets addObject: newBullet];
//release the object here as its retained in the array
     [newBullet release];
}
//myBullets now has 5 bullets in it.

To modify the object:

theBullet = [myBullets objectAtIndex:i];
//do stuff or modify theBullet
theBullet.position.x += 1; //add 1 to the x position

To remove an object from the array use:

[myBullets removeObjectAtIndex:2];
//removes the 3rd item
//remember an array starts at 0!
Read post... Posted in 'Programming'

Changing the iPhone status bar

Just use this code to change the type of status bar shown. Stick it in the applicationDidFinishLaunching method which is usually in your MyAppDelegate.m

[application setStatusBarStyle:UIStatusBarStyleBlackOpaque];
Read post... Posted in 'Programming'