Tuesday, December 30, 2014

Python's venv problem with ensurepip in Ubuntu

Ubuntu 14.04's Python 3.4 installation has a problem with ensurepip module, as described in this bug. So if you follow the steps mentioned in the official documentation, you would see an error message like this
➜  Python  python3 -m venv py3.4venv
Error: Command '['/home/thefourtheye//py34venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1
To resolve this problem, first install the venv, without pip, like this
python3 -m venv py3.4venv --without-pip
And then if you install pip, like this, it will still fail
(py3.4venv) ➜  py3.4venv  python -m ensurepip --upgrade
/home/thefourtheye/Python/py3.4venv/bin/python: No module named ensurepip
(py3.4venv) ➜  py3.4venv  python
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
...
So, you need to install, pip separately, like mentioned in the pip's official documentation. So, the actual list of steps go like this
➜  Python  python3 -m venv py3.4venv --without-pip 
➜  Python  cd py3.4venv 
➜  py3.4venv  source bin/activate
(py3.4venv) ➜  py3.4venv  wget https://bootstrap.pypa.io/get-pip.py
--2014-12-30 14:35:34--  https://bootstrap.pypa.io/get-pip.py
Resolving bootstrap.pypa.io (bootstrap.pypa.io)... 103.245.222.175
Connecting to bootstrap.pypa.io (bootstrap.pypa.io)|103.245.222.175|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1581355 (1.5M) [text/x-python]
Saving to: ‘get-pip.py’

100%[=====================================================================================================================>] 15,81,355    129KB/s   in 8.9s   

2014-12-30 14:35:43 (173 KB/s) - ‘get-pip.py’ saved [1581355/1581355]

(py3.4venv) ➜  py3.4venv  python get-pip.py 
Collecting pip
  Downloading pip-6.0.3-py2.py3-none-any.whl (1.3MB)
    100% |################################| 1.3MB 139kB/s 
Collecting setuptools
  Downloading setuptools-9.1-py2.py3-none-any.whl (552kB)
    100% |################################| 552kB 180kB/s 
Installing collected packages: setuptools, pip


Successfully installed pip-6.0.3 setuptools-9.1
(py3.4venv) ➜  py3.4venv  deactivate 
➜  py3.4venv  source bin/activate                      
(py3.4venv) ➜  py3.4venv  pip install django
Collecting django
  Using cached Django-1.7.1-py2.py3-none-any.whl
Installing collected packages: django

Successfully installed django-1.7.1
(py3.4venv) ➜  py3.4venv  which pip
/home/thefourtheye/Python/py3.4venv/bin/pip
(py3.4venv) ➜  py3.4venv  

Monday, September 15, 2014

Sending POST/PUT requests, with JSON form body, in Node.js

Today one of my friends asked me to help him with sending a PUT request to a remote server, in Node.js. I started Googling and as usual I found this excellent Stackoverflow answer.
var http = require('http');

var options = {
host: 'localhost',
path: '/users/1',
port: 3000,
method: 'PUT'
};

var callback = function(response) {
var str = '';

//another chunk of data has been recieved, so append it to `str`
response.on('data', function(chunk) {
str += chunk;
});

//the whole response has been recieved, so we just print it out here
response.on('end', function() {
console.log(str);
});
};

http.request(options, callback).end();
This works well. It creates a HTTP PUT request, to the server hosted at localhost on port 3000 and in the path '/users/1'. Now the interesting part is, normally, when we send PUT/POST requests, we used to send parameters. These parameters will be represented normally as key-value pairs. It will be easy to represent them in JSON format. So, the above code just needs few more changes to send the request with a JSON body.
var http = require('http');

var bodyString = JSON.stringify({
    username: 'thefourtheye',
    password: '********'
});

var headers = {
    'Content-Type': 'application/json',
    'Content-Length': bodyString.length
};

var options = {
    host: 'localhost',
    path: '/users/1',
    port: 3000,
    method: 'PUT',
    headers: headers
};

// callback is same as in the above seen example.
...
...

http.request(options, callback).write(bodyString);
What we are actually doing here is, creating a JSON structure for the form body and then we are converting that to a valid JSON string. This is important, since we are sending the body as a JSON structure, it should conform to the JSON semantics. So, if you printed the bodyString, you would get something like this
{"username":"thefourtheye","password":"********"}
Now, we constructed the body string. But, how will we let the server know that the request is not over immediately after the HTTP Request line and the headers in the HTTP Request.

Source: http://www.tcpipguide.com/free/t_HTTPRequestMessageFormat.htm


As we see in the picture, after the headers section, there is the body section, which is where the body string we generated will be put in. But how will the web-server know the format of the body section and where it actually ends? That is why we put in two 'headers' in the 'options' object.
var headers = {
    'Content-Type': 'application/json',
    'Content-Length': bodyString.length
};

Here we specify the type of the body and the actual length of it. Okay, now that we specified the type and the length, how are we going to send the string? We simply write it to the 'ClientRequest' object returned by the 'http.request', like this
http.request(options, callback).write(bodyString);
That is it :-)

Saturday, March 1, 2014

Node.js - modules and exports

I think most of us haven't understood the concept of modules in Node.js properly. Let us discuss the basics of that in this post.

Module System

In Node.js, when you create a new JavaScript file, that will be considered as a separate module. Inside that module, you can access the module itself with module object. You can check that, like this

console.log(module);
which produces something like this
{ id: '.',
  exports: {},
  parent: null,
  filename: '/home/thefourtheye/Desktop/Test.js',
  loaded: false,
  children: [],
  paths:
   [ '/home/thefourtheye/Desktop/node_modules',
     '/home/thefourtheye/node_modules',
     '/home/node_modules',
     '/node_modules' ] }

exports and module.exports

As you can see, it is just a plain JavaScript object. The important thing to be noted here is, the exports object in module. In every module, JavaScript, by default, offers another variable called exports. That is nothing but the same object in module object of the same name. You can check that like this

exports.jabberwocky = "blah blah blah";
console.log(module.exports);            // { jabberwocky: 'blah blah blah' }

So, they are one and the same. But, when some other module requires this module, the object returned will be module.exports only. As long as you are augmenting module.exports and exports, there will be no problem. But when you assign something to either exports or module.exports, they no longer refer to the same object.

exports = {"king": "Sourav Ganguly"};
console.log(module.exports);           // {}
console.log(exports);                  // { king: 'Sourav Ganguly' }

You are making both exports and module.exports refer to different objects. So, when this module is exported, an empty object will be exported (remember, only module.exports will be exported when required from other files), even though we assigned a valid object to exports. So, care should be taken when you replace either of those objects. That is the reason why we often see something like this

exports = module.exports = ...

Scope

All the variables and functions declared within the module will be accessible only inside the module (as long as they are created with var keyword). Quoting from the modules documentation

Variables local to the module will be private, as though the module was wrapped in a function.
Happy modularizing the code :)

Tuesday, January 28, 2014

Bad UI Design - Twitter Account page

I am going to consider this as a bug in Twitter. When I wanted to change my profile information (in Chrome 32, Ubuntu 13.04), I don't see a save or update button. Highly frustrating. Hope they fix it soon.

Sunday, January 5, 2014

Un-hiding Menu Bar in Sublime Text 3 - Ubuntu

Edit on 13-Jan-2016
I found another easy way to fix this problem. You can toggle the menu with a keyboard shortcut. You can read more about that in this blog post of mine.


It has been a very long wait to get the "Hide Menu" feature in Linux versions of Sublime Text 3. Finally it was released. But the problem is, if it hidden once, there is no straight forward way to get the Menu back. I tried F10, Alt etc etc and nothing worked. So, I am sharing the trick which worked for me in this post.

It is simple. Close the Sublime Text 3, if it is open. Open

~/.config/sublime-text-3/Local/Session.sublime_session
in any of your favorite editors and then make sure that, "menu_visible" is "true" in all places in that file.
"menu_visible": true,
Thats it :) Open Sublime Text 3 now and ta-da... Menu is back :)


Edit On Jan 10 - 2016

As I was doing the same task more often, I decided that put that in a script, like this

export FILE=/home/thefourtheye/.config/sublime-text-3/Local/Session.sublime_session
export TEMP_FILE=/home/thefourtheye/.config/sublime-text-3/Local/Session.sublime_session.bkp
sed -e 's/"menu_visible": false/"menu_visible": true/g' $FILE > $TEMP_FILE
mv $TEMP_FILE FILE
and then I added that to my shell script's rc file as an alias like this
alias sublime_fix_menu=~/.sublime_fix_menu.zsh
That's it :-) Now, I just have to close Sublime Text 3, type sublime_fix_menu in the shell, open Sublime again :-)