Moving my blog (Last Post to this one)

March 10, 2011

Hi, I just moved my blog to http://www.yilmazhuseyin.com/blog/dev/ . I will leave this one as it is,but my new posts will be in my new location.


Installing compiz fusion in ubuntu

October 3, 2010

Ubuntu comes with some desktop effects. But you can use a lot more by installing compiz. Here is how I install compiz.

sudo apt-get install ccsm

With this package you can get com extra funtionality

sudo apt-get install confiz-fusion-plugin-extra

in ubuntu you can find compiz setting manager under System>Preferences

Advertisement

How to setup emacs for python (django) development

September 29, 2010

Lately, I decided to use emacs for all my development. If, I use emacs I will have my development environment trough terminal. so I can remotely connect my computer and do my development. When I discover how emacs works (and what emacs is), I really like the idea of using emacs for my work and personal projects.

Even though I made my mind for using emacs for development, there was a little problem. Comparing to newer IDE’s like eclipse, emacs was really hard to modify, I had really hard time to understand how it works. So I decided to write my experience for others to speed up to beginning process (which seems to be the hardest part of switching to emacs)

Before I start to write how did I setup my environment, I would like to tell you about sources I read. The best place you can go to understand how emacs work is emacs manual. This manual is about every aspect of emacs. You can also find same manual in emacs itself so try to learn how it is constructed. There is also a wiki for emacs: emacswiki.org .

Here is how to modify emacs. Emacs plug-ins are written in elisp (emacs lisp: A language specifically written to implement emacs. You can also use it in emacs to modify emacs on the fly.) When emacs is opened. ~/.emacs file is executed. There should be elisp code in this file. As a name convention you also create ~/.emacs.d directory to hold plug-in codes. emacs does not check this directory. you have to add it to emacs load path. So I created .emacs file and .emacs.d directory under my home directory. And inside .emacs file I added following code
(add-to-list ‘load-path “~/.emacs.d/”)
This turned out to be a elisp code.

Add Python Support

To use django I need to have a python environment. So I found a blog post. But this post was  not telling how to set his environment but just giving the source code. Since I don’t know elisp I couldn’t really modify his version. After going trough similar pages I found a helpful page in emacs wiki. So I just follow comments in this page.
first of all I installed rope, ropemacs , pymacs and ropemode like it suggested. you can install ropemacs, ropemacs , pymacs from ubuntu repo.
sudo apt-get install python-rope python-ropemacs pymacs ropemacs can be install from its homepage. just download the package. call
python setup.py build
sudo python setup.py install
within the package.

After that I installed autocompete.el as page explains. At this point, I had auto-complete functionality for python and it is really cool. I also installed python-mode from ubuntu repo. but I don’t really know that it did to emacs. (sudo apt-get install python-mode).

Lastly I followed the wiki and installed pylookup. so I can lookup python documentation within emacs.

For now this is my set up. It is minimal but in time I will add new features to it.

Here is a screenshot of what result is look like:

Enable IDO Mode

Ido mode is what makes finding buffers and opening files a snap. normally finding a file is really really hard and if you want to open another file you have to go trough same cumbersome process. Emacs is just not usable without ido mode. Ido mode gives you autocompletion in opening buffers an files. it searches in current list and gives you your alternatives. It is just really easy to open files with ido mode. To enable it just follow instructions in emacs wiki page.

TODO: add js support. add python validation.

NOTE: I wrote another version of this post: It can be found in my new blog . In new version I covered my django emacs setup in three different blog post

http://www.yilmazhuseyin.com/blog/dev/basic-emacs-setup/

http://www.yilmazhuseyin.com/blog/dev/emacs-setup-python-development/

http://www.yilmazhuseyin.com/blog/dev/emacs-setup-web-development/


Promote js

September 26, 2010

http://promotejs.com/ is a great movement by javascript community to promote javascript documentation. I recommend everyone to contribute.
JavaScript JS Documentation: JS Number toPrecision, JavaScript Number toPrecision, JS Number .toPrecision, JavaScript Number .toPrecision


How to install postgreSQL+django to Ubuntu 10.4

September 22, 2010

I was triyng to install django and post gre on ubuntu 10.4 and I found a nice blog post here
http://programmingzen.com/2007/12/26/installing-django-with-postgresql-on-ubuntu/
But in this post, django is being pulled from svn repo and you have to manually connect django installation to python( Which is not hard but I am kind of lazy and unexperienced for this this process). Then I found out that django is already in ubuntu repository. With a little modification to original post I installed django and postgre with following command.
sudo apt-get install postgresql pgadmin3 python-psycopg2 python-django

This will install postgreSQL , postgreSQL admin, postgreSQL python adapter and django farmework and django framework will come pre-configured.

You still have to follow original post to set postgreisql password. Here is a sniplet from original post:

sudo su –
passwd postgres
su postgres
psql template1
The last instruction should open the psql shell, where you can run the following:
ALTER USER postgres WITH ENCRYPTED PASSWORD ‘mypassword’;

First time I run a django app, a couldnt run my application. syncdb just gave a connection error. so I edit file

/etc/postgresql/8.4/main/pg_hba.conf and change the autantication type of postgres user here is the new line

# Database administrative login by UNIX sockets
local   all         postgres                          password

In the second line, I changed ‘ident’ to ‘password’


Javascript Logger

September 22, 2010

For my javascript project, I wrote a simple logger that logs to browser console. Here is the project page in github http://github.com/huseyinyilmaz/javascript-logger .  Just add it to your project and you can use it.
<script type=”text/javascript” src=”logger.js”></script>

When your page is loaded you should enable logger. Because logger does not work by default. you should enable it to use it. that way you can use logger in development and disable it in production. To enable logger:
logger.enableLog = true;

To add log functionality ,start your function with
logger.startLog(“someFunction”);
and end it with
logger.endLog();


If you call another logged function inside this one it will be showed with indent in firebug or some other console on any browser. I recommend firebug or chrome’s default console because they support timing information too.

To see timing information of your pages:
logger.showTiming = true;
It is really easy to use. Go to githup repository and see sample.html for a simple and fully woking example.


Basic git commands

August 30, 2010

Recently, I switched from SVN to GIT for my personal projects. At first Git seemed like a lot harder, because I was using command line to manipulate my git repository. I know it sounds awful but actually it is pretty easy. Here is basic commands

git init

This will create an empty repository on current directory. After running this command you will see a .git directory on current directory. All the repository information is under that directory. Git does not create any other directory anywhere else. For example, there won’t be any other repository under included sub-directories.

git init −−bare

This command creates an empty bare repository under current directory. Difference between bare repository and regular repository is that bare repositories does not have source code every  thing under .git directory will actually be under current-directory. Bare repositories can be changed by pushing changes to them from other repositories. Therefore bare repositories act like an SVN server. For example, my main project repository is a bare repository. you can also create a backup repository and push changes to it in regular intervals.

git status

Gives status of the git repository under current directory. you can check if there is a git repo under current direcytory , is there a updated file , is there a new or moved file.

git add

Adds new updated or moved file to commit area. Normally when you commit updated code nothing will be commited you can tell git to track files or add updated files to next commit.
So before you commit your changes, you should tell git what to commit with calling add method for example “git add build.xml” command will add build.xml file to commit area. so next time you commit, this file will be commited. “git add .” will add everything to commit area. it will also add subdirectories recursively.

git commit

Commits changes to repository. to commit changes, first of all you should tell git what to commit, than call commit. After calling commit, git will open a text editor and let you enter a commit message. if you have a short commit message you can use -m paramether(git commit -m “commit message”).”git commit -a” will first add every change to commit area and then commit changes. So you won’t have to call “git add”.

git log

Gives commit information. you can see commit dates ,commit messages and commit id.
“git log −−oneline” command will show you commit information but every commit will take only one line.

git branch

Used to create, delete branches. “git branch” will list branches. “git branch second” will clone current branch as a new branch named second. “git branch -d second” will delete branch second

git checkout

“git checkout second” will change current branch to another branch named “second”.

git remote add

“git remote add origin “file://gitrepos/myproject.git” will add a new remote repository. in this example we are adding a new repository in directory “/gitrepos/myproject.git” please beware that this is not a file it is a directory.  here is another example that adds a github repository as a remote “git remote add origin git://github.com/huseyinyilmaz/javascript-logger.git”

git remote rm

“git remote rm second” removes remote repository named second.

git clone

Clones a remote repository and adds it as a origin remote. “git clone git://github.com/huseyinyilmaz/javascript-logger.git” will pull remote repository. and if you run “git remote” command in this new repository, you will see that source repository is added as a remote repository named origin

git push origin master

Pushes changes on current repository(and current branch) to a remote repository named origin(and branch named master).

git pull origin master

Pulls changes from master branch of remote repository named origin to current branch.

git diff

Gives a difference info between two branches. To use diff command, first run  git log and find two commits that you want to see the diff report. Then get first four characters of commit ids in my case numbers were 4623 and 8dca  so to see diff report I have to run “git diff 4623..8dca” to see I want to use head as one of the commits I can run it as “git diff 4623..HEAD” this will give diff report of commit id 4623 and the last commit on this repository.


Closure in javascript Part 3

July 19, 2010

Using closure to hide collections

Here is a way to hide an array in closure

var objectContainer =  function(){
     var array = [];
     return {
          push : function(xVal,yVal){
               array.push({x:xVal,y:yVal});
               },//push
          get : function(i){
               return array[i];
               }//get
          };
     }();

We have an objectContainer object that has a push and get methods. Push method adds a new coordinate to array and get returns an object. In this example our array is safe because it is hidden. However, our objects in array are not. Getter method is returning real object that is stored in our array. So we can do unintended changes on that object. Can we make that part safe too? The answer is yes you can.In order to to that, you should change returned object’s get method with the following

 get :  function(i){
      var value = array[i]
      return {
           getX:function(){return value.x},
           getY:function(){return value.y}
           };
      }//get

As you see, we are also hiding our object under closure of get method. As a result, it will always be safe. We can now put any validation in our returned object. Our array structure is now indestructible.

Part 1Part 2


Closure in javascript Part 2

July 19, 2010

Usage of closure in object factory functions

If you need to create the same object multiple times in your code, it is usually a good idea to put your object creation code in a function (which I like to call object factory functions). Lets make an object factory for number object we made in first part of this post.

var objectFactory1 = {
     getNewNumber:function(value){
          var number = 0;
          if (typeof value == "number")
               number = value;
          return {
               getNumber:function(){
                    return number;
                    },//getNumber
               setNumber:function(value){
                    if (typeof value == "number"){
                         number = value;
                    }else {
                         //TODO show an error or something
                    }
                    }//setNumber
               };//Returned Object
           }//getNewNumber
           };

Here we have an object that has a getNewNumber method which gets number value as a parameter and returns a number object instance. As you can see returned method is safe because value property is in closure of getNewNumber function.

This is a nice way of creating objects. However objects that created by this object factory have different prototypes. You need to make some changes in your code if you need to use prototype structure.

var objectFactory2 = function(){
     var baseNumber = {};//prototype
     return {
          getNewNumber:function(value){
               var number = value;
               var F = function(){
                    this.getNumber = function(){return number;};
                    this.setNumber = function(value){number = value;};
                    };
               F.prototype = baseNumber;
               return new F();
               }//getNewNumber
          };//return
     }();

In this version ve have a baseNumber object. This is an empty object and it will be our prototype. (any change on this object will effect all number objects). If you don’t need prototype structure, do not use new keyword for object creation. Json notation will always be faster and simpler.

Part 1Part 3


Closure in javascript Part 1

July 18, 2010

Why do we need closure

Closure is the only way to use information hiding in javascript.  Unfortunately it is hard to code too. So here is some examples to hopefully show you how to use closure.

Before showing anything, lets take a look at  why we need closure. Take a look at folowing code:

var object01 = {
      number:0,
      getNumber:function(){return this.number;},
      setNumber:function(value){
           if (typeof value == "number") {
                this.number = value;
           }else {
                //TODO show an error or something
           }
           }//setNumber
           };//object01

We create a simple number holder. It has a getter and setter. But when we set our number we check if given value is really a number. If value is not a number, we don’t change current value. That way we can be sure that our number object always have a number value inside. Since our number object is ready let’s write some more code to test it.

     object01.setNumber(5); //object01 is set to 5

Here we set our number to value 5. Since 5 is a number, assignment works and our object stores value 5.

     object01.setNumber(window);//invalid assignment object01 is still has value 5

Now we have tried to set our number to window object and it was not a number so assignment did not work. Our number object still has value 5. Unfortunately this object cannot guarantee that it will always have a number value inside, because field of our object that we hold our value is accessible from outside of this class. So instead of using setter we can directly change number field. Here is how:

 object01.number = window;

At that point, our number object is compromised. It does not have a number inside. Instead, it holds window object. So places that uses this object which expect it to hold only numbers will have unexpected behaviour. For example following code will result NaN(Not a number) now.

     resultNumber = object01.getNumber() * 100;

How to create basic closure

In order to avoid such problems we can use closure to hide data that is meant only for inner use of our object.

var myObject = function(){

     return {};
     }();

In this code we create an anonymous function and directly call it (notice parenthesis at the end of function.) Inside anonymous function, we create our object and return it. After this code executes, myObject variable will have empty object that is created inside closure function.It does not have any use right now. Let’s add our number variable.

var myObject = function(){
     var number = 0;
     return {};
     }();

Now we have a variable that is declared in our closure function. Here is the trick: Since this is a local variable, it is only accessible inside this function. So we can access it inside this function including inside the empty object we just created. Lets add a getter in our empty object.

var myObject = function(){
     var number = 0;
     return {
          getNumber:function(){return number;}
     };
     }();

At that point number variable is only accessible through getter function “getNumber”. Let’s make a safer version of our first object object01.

var object02 = function(){
     //private variable
     var number = 0;
     return {
          getNumber:function(){
               return number;
               },//getNumber
          setNumber:function(value){
               if (typeof value == "number"){
                    number = value;
               }else {
                    //TODO show an error or something
               }
               }//setNumber
       };//Returned Object
}();

We declare private data as local variable of closure function, that way they will not be directly accessible from outside even though getter and setter methods will still be able to access it. You may not need closure if you are just moving divs around. But for anything more complex, you should definitely consider using this technique.

Part 2Part 3