GA Profile ID (for instance to use with gdata library)

I think google’s API and its Python binding are a huge mess.
And poor documentation makes them even worse.
You can find the majority of parameters described in quite understandable way, but you can’t start using them because you have to authorize first, and this topic is among worst explained.

But even if you passed it, you’re stuck with some tricky params that you have to discover SOMEHOW.
Example: for exporting data from GA you have to provide some PROFILE ID (it is often called the TABLE ID in the docs).
Great, have a nice time clicking around your account settings trying to find it.

UPD: I thought the only way to solve it is like that: http://blog.explainum.com/2011/05/how-to-find-profileid-in-new-google.html
But there’s actually an ‘official’ way – click your account settings, then on the Profiles tab pick the profile and click the profile settings tab.
The ID is actually there.

Another nice example – they have the API Key. But it’s not supported by the library so if you’re going to collect some stats on the server, you have to store your email / password, or to mess with OAuth.

And another point. You start with creating a service like

service = gdata.analytics.service.AnalyticsDataService(
                source=settings.GA_APP_NAME,
                email=settings.GA_USERNAME,
                password=settings.GA_PASSWORD
            )
1
2
3
4
5
service = gdata.analytics.service.AnalyticsDataService(
                source=settings.GA_APP_NAME,
                email=settings.GA_USERNAME,
                password=settings.GA_PASSWORD
            )

Good, then try requesting your data — even though you’ve provided your credentials, this bitch didn’t authorize you. Cool?
Call

service.ProgrammaticLogin()
1
service.ProgrammaticLogin()

, then you’re done.

Error trying initialize default postgres db?

$ sudo su postgres -c '/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb'
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
The program "postgres" is needed by initdb but was not found in the
same directory as "initdb".
Check your installation.
1
2
3
4
5
6
7
8
$ sudo su postgres -c '/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb'
shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
could not identify current directory: Permission denied
The program "postgres" is needed by initdb but was not found in the
same directory as "initdb".
Check your installation.

Then first do

cd /opt/local/var/db/postgresql91/
1
cd /opt/local/var/db/postgresql91/

PyCharm / Webstorm JS autocompletion / inspector is great

I’m calling a function accepting a hash of options.

THe function is declared in a separate file included through script.

IDE highlights the keys that are not used in the function being called.

And also SO: Specify type of globals declared in another file.

jQuery.sortItems

A much-requested on SO function for sorting DOM elements.

jQuery.fn.sortItems = function (key) {
    var $items = [],
        $wrap = $('<div/>'),
        i, l;
    this.each(function () {
        $items.push([key(this), $(this)]);
    });
    if (!$items) {
        return;
    }
    $wrap.insertBefore($items[0][1]);
    $items.sort();
    for (l = $items.length, i = l - 1; i >= 0; i--) {
        $items[i][1].insertAfter($wrap);
    }
    $wrap.remove();
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
jQuery.fn.sortItems = function (key) {
    var $items = [],
        $wrap = $('<div/>'),
        i, l;
    this.each(function () {
        $items.push([key(this), $(this)]);
    });
    if (!$items) {
        return;
    }
    $wrap.insertBefore($items[0][1]);
    $items.sort();
    for (l = $items.length, i = l - 1; i >= 0; i--) {
        $items[i][1].insertAfter($wrap);
    }
    $wrap.remove();
};

Demo.

Deluge torrent client

Installed it on my home server under Ubuntu Server 11.04, configured daemon and web UI startup – looks really cool, as a normal full-featured desktop client

Emacs-style shortcuts outside of Emacs

1) on Linux and Mac OSX you can use them in terminal, for example:
ctrl+A to go to the beginning of the line (or God, I needed this so much, but was always forgetting to google for it)
ctrl+E to go to the end of the line
ctrl+T to transpose the characters on either side of the cursor
ctrl+D to delete the character to the right of the cursor
ctrl+K to delete the rest of the current line

2) these shortcuts are also supported by Cocoa native text fields. This means, that on Mac OSX you can use them, for example, in your browser’s address bar. Awsome!

BIOS reset with HP 6910p

For 2 days I was burning various Windows PE / FreeDOS / custom tools to the RW trying to erase the BIOS setup password
Then just followed the official guide, released the keyboard in 5 minutes, and unplugged the RTC battery for extra 5 minutes — done.

Eclipse is just amazing…

PyQt4 on MAC OSX

Tiered of installing from sources?
The answer is as easy as

sudo port install py27-pyqt4
1
sudo port install py27-pyqt4

Of course, the python version can be different for you.

Why your top-level anonymous closure MUST end with semicolon

This is good, popular and recommended to wrap every JS file in an anonymous closure function:


(function (){                                                         // (a)
    var module_wide_var = 0;                                          // (1)

    function inc() { module_wide_var += 1; return module_wide_var; }  // (2)
    function dec() { module_wide_var -= 1; return module_wide_var; }

    window.inc = inc;                                                 // (3)
    window.dec = dec;
}())                                                                  // (b)
1
2
3
4
5
6
7
8
9
(function (){                                                         // (a)
    var module_wide_var = 0;                                          // (1)

    function inc() { module_wide_var += 1; return module_wide_var; }  // (2)
    function dec() { module_wide_var -= 1; return module_wide_var; }

    window.inc = inc;                                                 // (3)
    window.dec = dec;
}())                                                                  // (b)

Here we:

  • (1) define a “global” variable,
  • (2) define a couple of functions
  • (3) expose our functions through the true global window object,

and everything is wrapped in an anonymous function (a), that’s instantly executed (b).

This approach implements the module pattern (well, in this case not clearly).

The whole function definition and invocation statement is wrapped in parens, because otherwise browsers fail to handle it as function expression.

There’s also 2 possible syntax for invocation – you can put it like (function (){ ... })() or (function (){ ... }()). I prefer the latter as recommended by Crockford (check for yourself).

OK, then, finally, a tiny point we forgot here: the trailing semicolon is the must.
The script above would work without any questions, until you try concateneting two of the kind. And for any big ptoject you’ll most probably have scripts compression and combining.

So, let’s see what happens:


(function (){ /* file 1 */ }())
(function (){ /* file 2 */ }())
1
2
(function (){ /* file 1 */ }())
(function (){ /* file 2 */ }())

The 1st line is result of compressing the 1st file, the 2nd line, accordingly, represents the 2nd file. Now, see: the 2nd line is wrapped in parens (and it has to be), and from the JS point of view it immediately follows the 1st line, i.e. the code is equivalent to


(function (){ /* file 1 */ }())(function (){ /* file 2 */ }())
1
(function (){ /* file 1 */ }())(function (){ /* file 2 */ }())

There’s no automatic semicolon insertion here.

This snippet is interpreted as function application, where the 1st part ((function (){ /* file 1 */ }())) is expected to return the function (we know, it returns undefined indeed), then parens follow, and inside them there’s function (){ /* file 2 */ }() that’s interpreted as the function argument.

What’s the problem? Well, the 1st part doesn’t return function, and the whole expression fails. With semicolons it would look like


(function (){ /* file 1 */ }());
(function (){ /* file 2 */ }());
1
2
(function (){ /* file 1 */ }());
(function (){ /* file 2 */ }());

which clearly represents 2 separate expressions.

Even if you’re a good boy, you can be punished because of other’s lazyness

I always use anonymous closure for my scripts, and I always put the trailing semicolon. But because of other developer’s lazyness I encountered this error and was relly frustrated until I realized what happened.

Our project included the already minimized code of one jQuery plugin. For some reason (I’m not really interested) this code looked like


/* copyright and license comment */
eval(function(p,a,c,k,e,r){...})
1
2
/* copyright and license comment */
eval(function(p,a,c,k,e,r){...})

This script was compressed with packer, and packer doesn’t care about anything that can follow its results – no semicolon there.

So, followed by my anon-closure-wrapped code, the 2 files combined were interpreted as a function execution, which, of course, failed.