Sunday, January 17, 2010

How to get bpython working with django for lazy people


bpython is a fancy interface to the Python interpreter for Unix-like operating systems (I hear it works fine on OS X). It is released under the MIT License.
(http://www.bpython-interpreter.org/)


There are many ways to get bpython working with django project you can find them by using google. But to use bpython with django isn't that complicated.
So, let's get it done.
  • If you're using Unix-like OS, you do have env.

    # In your project directory
    env DJANGO_SETTINGS_MODULE="settings" bpython

  • If the command you see above is too long and you are too lazy to do that.


    # In your project directory
    bpython -i manage.py validate


    bpython -i is used when we want to drop to bpython shell after running file instead of exiting, so that we can use bpython -i manage.py shell then Ctrl+D to get into bpython shell instead.


    # In your project directory
    bpython -i manage.py shell

    Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15)
    [GCC 4.4.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> # Then press (Ctrl+D).
    >>> # You will get bpython shell here.








Basic Javascript for programmer

Variable definition

There are two way to define variable 1. define in local scope 2. define in global scope. So you can define global variable even in function.
var a = 0;

// Function definition
function define_c() {
    c = 1;      // if c is defined it will be overided
    var b = 0;  // define a in local scope
    
}

define_c();

alert(a);
alert(c);       // You can access c in global scope
alert(b);       // b is undefined in global scope


// Array definition
var array1 = [1, 2, 3];
var array2 = new Array(1, 2, 3);

// String definition
var text = "hello";         // type of "text" is "string"
var text2 = new String("hello");    // type of "text2" is "object"

special operator

There are many operators exist in javascript. Most of them come from C/C++ and Java but there are something different.
var text = "hello";
var text2 = new String("hello");

// typeof

typeof text         // "string"

typeof text2        // "object"

typeof text3        // "undefined"

if(typeof text == "string") {
    alert("text is string");
}

if(typeof text2 == "object") {
    alert("text2 is object");
}

/*
There are several type in javascript
* number
* string
* boolean
* object
* function
* undefined
*/



// instanceof

text instanceof String      // false
text2 instanceof String     // true


// comparison

text === text2      // false, because they are different type
text !== text2      // true

text == text2       // true, because they have same value
text != text2       // false

Function definition

// Define f1 as function
function f1(){
    
}


// Define f2 as function
var f2 = function() {
    
}

// There are something different

typeof f1.name      // string
f1.name == "f1"     // true

typeof f2.name      // string
f2.name == ""       // true, Oh name of f2 is empty

var f3 = function myfunction() {

}

typeof f3.name      // string
f3.name == "myfunction"       // true


To be continued...