tinkering around but never mastering javascript

I normally hack javascript when i need to, but i have noticed that many people also do this.The reason for this i attribute to the internet and especially google.

There are plenty of resources on the internet offering solutions on how to deal with a specific problem, so one ends up googling how to do something in javascript without going through the basics of the language.

What we end up with is skills at googling and fixing problems but no mastership of the language.This can be said to be the double edge of having plenty of resources available at your finger tips.

For instance, stack overflow is an excellent resource.But i think one should turn to stack as the last resort.The habit of rushing to stack overflow and copying code does not offer any self improvement.A better alternative would be to hit the docs and read a good portion.Still this should come after attempting to solve the problem.

Nevertheless programming is a craft thats all about experience, if you continue hacking at javascript each day for 10 years, you'll eventually master it, but the point is it would be easier if you took time to go through the docs and learn the basics of a language.

This goes for jquery too, its possible to use jquery without knowing a good portion of javascript.This is a bad idea and sooner or later, if you're writing serious code, problems are going to arise that need more than just the basic knowledge of jquery.

So what will be the right time to master javascript?
Why not try a daily schedule.Taking on a chapter a day, there are plenty of good books on javascript out there.

Mac as a development environment

Apple pcs and notebooks are very elegant and offer a high level of aesthetics compared to standard hardware, but what about using a mac for development.

Experience with a macbook pro has proved that mac's are excellent for development.Want a vague proof of this, just take a look at conference videos, most serious developers use macs.And its just not for the aesthetics but there are heavier reasons for this.

Historically linux was the choice environment for many.The popularity of linux is waning due to recurrent issues involving GUI systems and the classic old drivers problem.Its just a pain that should be abstracted, and this is what mac osx does best.The UI is clean and elegant and you still get the power of the unix shell.

The write once run everywhere nature of web apps has also promoted the use of osx.Due to the fact that you can create a web app from any on any operating system that will be available to any system with a browser, this has removed the constraint of having to develop on a particular environment.Thus developers are free to choose which system provides the best user experience, and apple is well known for this.

A general trend I have noted is that most development software and library creators tend to cater for mac users since its a fact that many debs are on that platform.



pep 8 on pycharm

Pep 8 standard is a good convention to use when coding especially when working as a team.Standards and conventions make reading other peoples code easier.Getting yourself to start adhere to these  standards takes some time and effort, but by using the right tools it can take a shorter time.By the way, taking your time to learn these standards is quite worth the effort.

Pycharm is a wonderful python IDE which i would recommend to anybody doing development in python.Using pycharm you can set pep8 as an external tool, which you can invoke on any file you are currently working on to  check whether it violates any standards.This can be done this way.

Install pep8 package from the python package index

pip install pep8

go to: File > Settings > External Tools > Add new


if your using virtual env, use the path to pep8 installed in the virtualenv's bin directory.Otherwise point it to the location where pep8 is installed, use the whereis command to find it.
Now add an output filter as follows.

You can now access the command from the tools menu or conveniently right click anywhere within the file currently being edited and select the command.
The output should be quite helpful in identifying changes which need to be made.


django factory boy - better test management

Django factory boy is an excellent replacement for fixtures, most of the time you will encounter problems with maintenance of fixtures.For instance if you do migrations often, the set of fixtures will become invalid and has to be edited to fit the new schema, this can be extremely painful if you have a complex schema and lots of data.
Factory boy makes it easy to create objects when you need them for tests.For example..

lets say you have this set of models,
from django.db import models

class Owner(models.Model):
name = models.CharField(max_length=200)
phone = models.CharField(max_length=50)

def __unicode__(self):
return self.name

class Car(models.Model):
name = models.CharField(max_length=200)
owner = models.ForeignKey(Owner)

def __unicode__(self):
return self.name

Then your factories.py for these two will correspond to something like this
import factory
from models import Owner, Car

class OwnerFactory(factory.Factory):
FACTORY_FOR = Owner
name = 'bruce'
phone = '+254728037573'

class CarFactory(factory.Factory):
FACTORY_FOR = Car
name = 'jeep'
owner = factory.lazy_attribute(lambda a: OwnerFactory())

The tests for the factories themselves go something like this
from models import Car,Owner
from django.test.testcases import TestCase

class FactoryTestCase(TestCase):

def test_car_factory(self):
car = CarFactory()
self.assertTrue(isinstance(car, Car))

def test_owner_factory(self):
owner = OwnerFactory()
self.assertTrue(isinstance(owner, Owner))
Many to many fields are a bit tricky to emulate but this does the trick. For instance lets say you have this set of models.

from django.db import models

class Pizza(models.Model):
name = models.CharField(max_length=120)
toppings = models.ManyToManyField(Topping)

def __unicode__(self):
return self.name

class Topping(models.Model):
name = models.CharField(max_length=200)

def __unicode__(self):
return self.name

Then your factories will go something like this.

from models import Pizza, Topping
import factory

class ToppingFactory(factory.Factory):
FACTORY_FOR = Topping

name = 'mushrooms'

class PizzaFactory(factory.Factory):
FACTORY_FOR = Pizza

name = 'mushroom delight'

@classmethod
def _prepare(cls, create, **kwargs):
topping = ToppingFactory()
pizza = super(PizzaFactory, cls)._prepare(create, **kwargs)
pizza.toppings.add(topping)
return pizza

So the key here is to override the _prepare method and assign the manytomanyfield objects here, since you have and instance of the object. In conclusion factories are a good alternative to fixtures when used in the right way.

building web applications

The best method to use when building web applications is to use frameworks. Frameworks save on time and adhere to the principle of not repeating code all the time (DRY). So if you decide to use a framework, what should your choice be.There are many frameworks out there and therefore it would not be okay just to point you to one.To gain a deeper understanding of building web applications using frameworks it is advisable to go down the path of discovery.

  1. Pick a framework based on any language you know of and learn it, this should be the beginning.
  2. After taking a week or two to learn the new framework, do a side project with it, not a serious one but something far from the basics.Try to implement some idea you've always had.
  3. Add the side project to your portfolio and start over again from step 1 this time with another framework which is related.
After going through the first iteration, try implement the idea you had done using the new framework you have just learned.This aids you in discovering weaknesses and strengths in a framework.

Be patient
Nothing comes easy, everything requires time and effort, so don't expect to excel at it immediately.With continuous practice and diligent effort you will notice your skills and knowledge increasing.
I wish to stress the importance of persistence here.It's what will bear the results.

Django Tastypie with android client part 2

This post picks up from part 1 where we had defined the api using tastypie.The purpose of this second part is to create an android client to consume the api. here are the files that make up the android application. RecipesActivity.java
package com.thecodachi.recipe;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class RecipesActivity extends Activity {

public static String api_url = "http://10.0.2.2:8000/api/";

private List recipesList = null;
private ProgressDialog progDialog;
private ProgressThread progThread;
private ListView recipes;

private DatabaseHelper databaseHelper = null;

private ArrayAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

recipes = (ListView)findViewById(R.id.lvRecipes);

showDialog(0);

try{
//get our Dao and query for all customers in db
Dao recipes = getHelper().getRecipeDataDao();
recipesList = recipes.queryForAll();

}catch(SQLException e){
Log.e("LOG", "error getting products");
}


adapter = new ArrayAdapter(this,
R.layout.recipe_row,
R.id.recipeLabel,
recipesList);
recipes.setAdapter(adapter);
}

@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case 0:
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setMessage("Loading data...");
progThread = new ProgressThread();
progThread.start();
return progDialog;
default:
return null;
}
}

private class ProgressThread extends Thread{

private static final String LOG_TAG = "PROGRESS_THREAD";

@Override
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(api_url+"recipes/?format=json&limit=0");
HttpResponse response = null;
try{

response = httpClient.execute(httpGet);

}catch(Exception e){
System.out.println(e.toString());
}
if(response!=null){
HttpEntity entity = response.getEntity();
String apiResponse = null;
try {
apiResponse = _getResponseBody(entity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(apiResponse!=null){
try{
Dao recipesDao = getHelper().getRecipeDataDao();

JsonElement json = new JsonParser().parse(apiResponse);
JsonObject rootObject = json.getAsJsonObject();
JsonArray array= rootObject.getAsJsonArray("objects");
Iterator iterator = array.iterator();

while(iterator.hasNext()){
JsonElement json2 = (JsonElement)iterator.next();
Gson gson = new Gson();
Recipe recipe = gson.fromJson(json2, Recipe.class);
recipesDao.createIfNotExists(recipe);
Log.d(LOG_TAG, "--------------------------------created recipe");
}

}catch(Exception e){
Log.e(LOG_TAG, "error creating recipe from json");
}


}

}
dismissDialog(0);
adapter.notifyDataSetChanged();
}
}
public static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException {

if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); }

InputStream instream = entity.getContent();

if (instream == null) { return ""; }

if (entity.getContentLength() > Integer.MAX_VALUE) { throw new IllegalArgumentException(

"HTTP entity too large to be buffered in memory"); }

String charset = getContentCharSet(entity);

if (charset == null) {

charset = HTTP.DEFAULT_CONTENT_CHARSET;

}

Reader reader = new InputStreamReader(instream, charset);

StringBuilder buffer = new StringBuilder();

try {

char[] tmp = new char[1024];

int l;

while ((l = reader.read(tmp)) != -1) {

buffer.append(tmp, 0, l);

}

} finally {

reader.close();

}

return buffer.toString();

}
public static String getContentCharSet(final HttpEntity entity) throws ParseException {

if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); }

String charset = null;

if (entity.getContentType() != null) {

HeaderElement values[] = entity.getContentType().getElements();

if (values.length > 0) {

NameValuePair param = values[0].getParameterByName("charset");

if (param != null) {

charset = param.getValue();

}

}

}

return charset;

}
private DatabaseHelper getHelper(){
if(databaseHelper == null){
databaseHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
}
return databaseHelper;
}
}
Recipe.java class which defines a recipe object which can be persisted in the sqlite database using ormlite.
package com.thecodachi.recipe;

import com.j256.ormlite.field.DatabaseField;

public class Recipe {

@DatabaseField(id=true)
private int id;

@DatabaseField
private String name;

@DatabaseField
private String content;

Recipe(){
//used by ormlite
}


public int getId() {
return id;
}


public void setId(int id) {
this.id = id;
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}


@Override
public String toString() {
// TODO Auto-generated method stub
return name;
}

}
DatabaseHelper.java responsible for persisting and retreiving objects from sqlite database using ormlite.
package com.thecodachi.recipe;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

//name of the database file for the application
private static final String DATABASE_NAME = "Recipe.db";
private static final int DATABASE_VERSION = 1;
/*
* the DAO objects we use to access the recipes*
*/

private Dao recipesDao = null;

public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try{
Log.i(DatabaseHelper.class.getName(), "onCreate");

TableUtils.createTable(connectionSource, Recipe.class);


}catch(java.sql.SQLException e){
Log.e(DatabaseHelper.class.getName(),"can't create db");
throw new RuntimeException();
}
}

//called when your application is upgraded

@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion,
int newVersion) {
try{
Log.i(DatabaseHelper.class.getName(), "onUpgrade");
TableUtils.dropTable(connectionSource, Recipe.class, true);

//after dropping old databases, we create the new ones
onCreate(db, connectionSource);

}catch(java.sql.SQLException e){
Log.e(DatabaseHelper.class.getName(), "can't drop databases");
throw new RuntimeException(e);
}
}


/* methods that returns the Database access object(Dao) for our product class
* will create it or just give the cached value
*/

public Dao getRecipeDataDao() throws java.sql.SQLException{
if(recipesDao == null){
recipesDao = getDao(Recipe.class);
}
return recipesDao;
}


/*
* close all database connections and clear any cached DAOs
*/
@Override
public void close(){
super.close();
recipesDao = null;
}
}
You can take a look at the rest of the resources belonging to this app from the repo which i will share. The implementation of the REST architecture i took in this example is not the best and it is highly recommended not to use this approach when building an android application.The reason for this is that the app does network requests within an activity.This should be done within a service For a more detailed and recommended architecture for creating RESTful android application look here. Otherwise this is just a demo.

Django Tastypie with android client

I've been using django-tastypie recently to create restful apis and i wanted to use it especially with an android client. I've put together a sample application, django rest backend and an android client to demonstrate this. So here is a quick walk through of the code. Here is the models.py which describes a recipe.

from django.db import models

class Recipe(models.Model):
name = models.CharField(max_length=50)
content = models.TextField()

def __unicode__(self):
return self.name
We set up a Resource for our API using tastypie.

from tastypie.resources import ModelResource
from recipes.models import Recipe

class RecipeResource(ModelResource):
class Meta:
queryset = Recipe.objects.all()
resource_name = 'recipes'
allowed_methods = ['get']
include_resource_uri = False

def alter_list_data_to_serialize(self, request, data_dict):
if isinstance(data_dict, dict):
if 'meta' in data_dict:
#Get rid of the meta object
del(data_dict['meta'])

return data_dict
Now we set up the urlpattern to access the data

from django.conf.urls.defaults import patterns, include, url
from recipes.api.api import RecipeResource
from django.contrib import admin
admin.autodiscover()

recipe_resource = RecipeResource()

urlpatterns = patterns('',
url(r'^api/', include(recipe_resource.urls)),
url(r'^admin/', include(admin.site.urls)),
)

Test the api using curl.

$ curl -H 'Accept: application/json' http://localhost:8000/api/recipes/
Now that the api works as expected the next part is to create an android app client to consume the api. This will be a simple app for viewing recipes.Later we will add the ability to add recipes to our recipe database from the android client. This will be covered in part 2 of this tutorial.

It's okay to leave if things are not as expected

Lately i have been reading a series of articles about individuals who are leaving or have left their former places of  employment and giving their reasons for leaving.It is very interesting to note that most express dislike about the way things were being steered by their former companies.What is being brought out is that these big companies had turned evil and deviated from their original well meaning motives, if they ever existed.

For one, i tend to think that this is natural.In the world there exists no organisation with an altruistic motive.To think of it, the golden rule is always "whats in it for me".What is actually happening is that eager graduates are drawn in to companies thinking that they are contributing to a greater good, only to realize this years later.Of course there's the usual bouts of philanthropy that usually exist, but deep down to the core, profit is the main motivation.

Startup founders are encouraged to formulate brilliant ideas by looking for problems to solve, or scratching common itches.Ones success is achieved, it all becomes about maximizing profits.Soon the users needs are swept aside.The outlook currently is that the future is in startups, but remember too that the big corporations which have turned evil were once startups.Inevitably as a startup grows into a large  company, moral decay starts occuring.This comes with size and does not occur within a close tightly knit community around a startup because everybody knows everybody and there is close interaction.

There exists a theory in the field of Organization management that brings out this aspect, that as an organization grows it becomes more and more unstable.so it's expected.

Data access on android made easy with OrmLite

OrmLite is a wonderful Object Relational Mapper available for the android operating system.

If your using the normal methods to access the sqlite database, then you are bound to find the
process very tedious.I realized i needed an orm when i found myself writing redundant code and it came to my attention that i was actually writing my own orm.

OrmLite is suited for android because it is lightweight and therefore easy to import to an android project.
Furthermore it uses java annotations to mark classes to be persisted which make your code look clean and easier to understand.


import com.j256.ormlite.field.DatabaseField;

public class Customer {

@DatabaseField
int id;

@DatabaseField
String name;

@DatabaseField
String country;

@DatabaseField
String phone;

@DatabaseField
String address;

@DatabaseField
String businessName;


Customer(){
//used by ormlite
}

@Override
public String toString(){
return this.name;
}

}

To create a table we use TableUtils' createTable method

TableUtils.createTable(connectionSource, Customer.class);
Data access is done using Daos(Data access Object),these offer convenientmethods for querying, creating, deleting... objects on the sqlite database

private Dao<Customer, Integer> customersDao = getDao(Customer.class);

customersDao.create(customer);


customersDao.delete(customer);

customersDao.queryForAll();



In conclusion, using ormlite saves more time and is more convenient.

Transaction management in django

ACID refers to an acronym in the database transactions.Which in full means.Atomic, Consistent, Independent, Durable(or atomicity, consistency, independency and durability).

Atomicity here refers to the idea that a transaction commits(goes through) as a whole or does not commit at all(all or nothing).Various scenarios necessitate the need to maintain atomicity in a transaction.

For instance if you are creating objects which require foreign key relationships with other objects.
It may be necessary to create the object it is related to on the fly and save it just to make sure the foreign key attribute is satisfied.

when controlling transaction management in views the best approach is to use decorators

from django.db import transaction

@transaction.commit_on_success
def viewfunc(request):
# ...
# this code executes inside a transaction
# ...

There are three types of decorators, autocommit, commit_on_success and commit_manually.
autocommit does django's default commit behaviour
commit_on_success does a single transaction for all the work done in a function, this can be the view function.
commit_manually lets you control transactions manually by calling commit() and rollback().

Care should be taken when using commit manually() because some transactions may occur in unexpected places which require a commit.This raises an exception in django.

A point to note is that some databases do not support transaction management.It is surprising to note that SQLite db supports transactions while MySQL db does not.

programming is all about experience

Having spent a couple of years coding and exploring this industry i tend to think the major factor that influences ones proficiency in coding is experience.

Having experience guarantees that you have made certain mistakes that everybody must make in order to become a professional.It also guarantees that you have tried out solutions to some common problems encountered every day,have encountered the common design patterns that exist for creating software and have tried out the major flavors of programming.

There is the common rule that to master any craft you have to spend 10,000 hours at it, which also tends to stress the importance of experience.

General Problem solving workflow

Here is a neat problem solving workflow i borrowed here.
It is really helpful when you're in the middle of a sprint and you've hit a
blocker.
Another strategy which i would advocate for and which normally works is
describing the problem to a colleague, most of the time you will answer yourself
while in the process of explaining the problem.
Heavy reliance on google isn't good as many have pointed that out.Try to solve it yourself if you really want to sharpen your skills.

Also i would also recommend taking a nap.Research shows that it leads to higher levels of productivity especially during the late hours of the day, you can find a good guide on taking a nap here.

1) Try it.
2) Get up and walk away.
3) Try again.
4) Go for a walk / do some pushups (gets the blood flowing)
4) Try again.
5) Read the documentation.
6) Try again.
7) Go to the whiteboard and draw it out.
8) Keep trying.
9) Go back to the docs.
10) Try again
11) Look through some sample source code.
12) Try again.
13) Meditate.
14) Try again
15) Keep trying
16) Google it
17) Find a good answer on stack overflow.

Linux: How to download flash videos part 2

As a follow up to part 1 entitled Linux: How to download flash videos from any site i went ahead and created a shell script for automating the entire process.So instead of typing the whole series of commands, just create this script and execute it whenever you wish to download a video.Remember, make sure the video has loaded fully before running the script.The video will be downloaded to the desktop.

#!/bin/bash
# shell script for downloading flash video files from the active browser

proc_number=`pgrep -f flashplayer | wc -l`

if [ $proc_number = "1" ]
then
proc_id=`pgrep -f flashplayer`
cd /proc/$proc_id/fd
file_number=`ls -l | grep 'deleted' | awk '{ print $8 }'`
cp $file_number ~/Desktop
echo "One file copied to desktop, name is $file_number"
else
echo "No files copied"
fi


Save the file and run chmod 755 to make it executable.Now run it by typing ./[filename]

Secure Login to your Google Account

If you find yourself in a situation where you need to log into your google account using a public computer, the following steps will let you login without having to type your password on the computer.

This is a useful protective feature by Google to that can help keep your password safe from keyloggers (tools that monitor key presses to determine what the user was typing). Unfortunately, it only works if you have a phone with a QR code scanner.

Steps:

  1. Go to https://accounts.google.com/sesame
  2. Scan the QR code that is presented to you.
  3. Open the provided link on your phone browser and login to your Google Account.
  4. Confirm that your really want to give the computer access to you account
  5. Once you confirm, the page with the QR code on the computer's browser will automatically reload into your account.
NB: Always remember to logout from your Google Account once you are done if using a public computer.

Linux: How to download flash videos from any site

Navigate to the site on your browser and allow the video to load fully.

Open up the shell and type the following.

~$ pgrep -f flashplayer

This will give you some process id(s) of currently active flash player, note this id.
Change directory to the processes folder, and into the fd folder.

~$ cd /proc/(pid)/fd
~$ ls -l

From the list, find an entry which is located in the tmp directory and has the name deleted in brackets.Note the number just before the path.In this case below it's 25, this represents the file name of the video.

lr-x------ 1 name name 64 2010-01-13 14:41 25 -> /tmp/FlashXXLMneo8 (deleted)

copy the video to somewhere convenient like your desktop.

~$ cp 25 /home/user/Desktop

Check your desktop, the file should be present, rename it appropriately.

How to create an emacs alias for shell mode

Most of the time, i launch emacs from the shell.I usually prefer to use it inside the shell, that is why using the command brings up emacs in windowed mode which is rather annoying.

~$ emacs

to use emacs in shell mode you have to give it the no window flag, this way.

~$ emacs -nw

so, this works fine but the problem is that you have to type this every time with the no window flag.To overcome this, you can create a shell alias.This is done by editing the .bashrc file which is usually present in your home directory.At the bottom add the following line, also from examples you can easily learn how to create aliases.

alias emacs='emacs -nw'

Save the changes and restart the shell, you can now try it out.

how to send mail with django

import the following.


from django.core.mail impore EmailMessage


in your settings.py put the following:

EMAIL_HOST defines the host for sending email
EMAIL_PORT defines the port for sending email

EMAIL_HOST_USER (optional)used to authenticate to the smtp server
EMAIL_HOST_PASSWORD (optional) also used to authenticate to the smtp server

EMAIL_USE_TLS (True/False) controls whether a secure connection is used

create an EmailMessage object


email = EmailMessage('Title', 'Body', 'from@email.com',
['to@email.com'], ['bcc@email.com'],
headers = {'Reply-To': 'someone@anonymous.com'})

send email

email.send(fail_silently=True)

or

email.send(fail_silently=False)

or

email.send()