Wednesday, December 29, 2010

Run Django with SQLite

In begin with check whether SQLite has been installed in your Ubuntu
sudo apt-get install sqlite

then in settings.py file:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'path/to/db'
# the rest of the fields are not used

This trick very useful for development stuff because i.e. with Postgres tests run very slow, and etcetera

Monday, December 27, 2010

Disable Firefox cache

To disable go to about:config page (copy to the address bar of FF) and set network.http.use-cache to false (double click it or right-click and 'Toggle').

Wednesday, November 17, 2010

Add jQuery to any website

Sometimes I need to perform some manipulations with a webiste's data. For this purpose I like to use jQuery but not every website it has. So I need to add this support. For that I made a bookmarklet. To use it just drag jQuery to your bookmar bar.

Tuesday, October 26, 2010

Merge many text files to one

import glob

search_pattern = '*.vcf'
dest_file = '__.txt'

with open(dest_file, 'w') as F:
    for infile in glob.glob(search_pattern):
        with open(infile) as f:
            F.write(f.read())
            F.write("\n\n")

print 'All done'
raw_input('Press enter...')

Saturday, October 23, 2010

Run GAE SDK for Python and Django on Ubuntu Maverick

First, I make the following structure in my project's directory:
~/projects/gae/ - directory for GAE's projects
~/projects/gae/google_appengine/ - downloaded GAE sources
~/projects/gae/google_appengine/test/ - test project using GAE

So, to run this test project as well as other further projects that use GAE
  1. Download Google App Engine SDK for Python and unpack it to ~/projects/gae/.
  2. Install python-2.5
    sudo add-apt-repository ppa:fkrull/deadsnakes
    sudo apt-get update
    sudo apt-get install python2.5
    
  3. Install easy_install for Python 2.5
    cd /tmp
    wget http://pypi.python.org/packages/2.5/s/setuptools/setuptools-0.6c11-py2.5.egg
    sh setuptools-0.6c11-py2.5.egg
    
  4. Install Django 1.1 for Python 2.5
    easy_install-2.5 django==1.1.1
    
  5. Put into ~/projects/gae/google_appengine/test file
    echo "#!/bin/bash
    
    python2.5 ../google_appengine/dev_appserver.py ./" > run
    chmod a+x run
    

When you need to run development server for the Test project you should do
cd ~/projects/gae/google_appengine/test/
./run

Friday, October 22, 2010

Default opening software

This file ~/.local/share/applications/mimeapps.list contains all overridden openers for particular MIME types.

Google Chrome and Flash player

Having installed new Ubuntu 10.10 I found that Google Chrome cannot display flash content and asks to install flash player. But at flash player download website it says that I have already installed one.
Googling a bit gives me an advise. I made the step below and it gets work.
$ sudo apt-get install flashplugin-installer
$ sudo locate libflashplayer.so
/usr/lib/flashplugin-installer/libflashplayer.so
/usr/share/ubufox/plugins/npwrapper.libflashplayer.so
/var/lib/flashplugin-installer/npwrapper.libflashplayer.so
$ ln -s libflashplayer.so /usr/lib/flashplugin-installer/libflashplayer.so

Clock widget hangup

If you are very annoyed by system hangup when you have clicked clock widget to watch calendar or whatever in your Ubuntu 10.10 I can advise to do the following:
sudo apt-get purge evolution-data-server evolution-couchdb evolution evolution-exchange
That's all. After this you are avoided such unpleasant behavior.
It, however, removes Evolution mail client but if you don't use it as me that is not a problem.

Thursday, September 16, 2010

Install lxml on Windows

To install easy_install on Windows:

Tuesday, September 14, 2010

Default UTF-8 for all files

In <Netbeans install dir>/etc/netbeans.conf file add to netbeans_default_options parameter
-J-Dfile.encoding=UTF-8

Friday, September 10, 2010

Sort by the key

To sort query by the key:
model = MyModel()
model.order('-__key__') # descending sort

Sunday, September 5, 2010

Monday, August 16, 2010

Play WMV, MP3 and other restricted formats in Ubuntu

Some audio and video formats are not included into Ubuntu by default. And to enable them in your system you must do it by your own.

Install them

Original Post

Fix mouse clicks in Flash-player in Ubuntu

In /usr/lib/nspluginwrapper/i386/linux/npviewer add
export GDK_NATIVE_WINDOWS=1
so, the content in this file become
#!/bin/sh
TARGET_OS=linux
TARGET_ARCH=i386
export GDK_NATIVE_WINDOWS=1
. /usr/lib/nspluginwrapper/noarch/npviewer
Restart browser and that's all.

Thursday, July 22, 2010

Restore grub2 after Windows installation

To run a command as administrator (user "root"), use "sudo ".
See "man sudo_root" for details.
ubuntu@ubuntu:~$ sudo fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x14311431

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       29089   233657361    7  HPFS/NTFS
/dev/sda2           29090       32129    24418800   83  Linux
/dev/sda3           60304       60801     4000185   82  Linux swap / Solaris
/dev/sda4           32130       60303   226307655   83  Linux

Partition table entries are not in disk order

ubuntu@ubuntu:~$ sudo mount /dev/sda2 /mnt
ubuntu@ubuntu:~$ sudo grub-install --root-directory=/mnt/ /dev/sda
Installation finished. No error reported.

Tuesday, May 18, 2010

Checkbox select all jQuery plugin for a table

/*
 * File: jquery.checkboxselection.js
 *
 * Plugin to create checkbox in the table
 * It needs table has correct structure (at least with head and body) and
 * there are checkboxs in row's first cell both in header in body
 */

(function($) {
    $.fn.checkboxSelection = function() {
        return this.each(function() {
            var table = this;
            var headCheckbox = $('thead th:first :checkbox', table).click(function() {
                $('tbody tr', table).find('td:first :checkbox').attr('checked', this.checked);
            })
            var checkSelection = function() {
                var trs = $('tbody tr', table).find('td:first');
                headCheckbox.attr('checked', trs.find('input:checked').length == trs.find(':checkbox').length);
            }
            $('tbody tr', this).find('td:first :checkbox').click(checkSelection);
            checkSelection();
        });
    }
})(jQuery);
HTML:
<table>
    <thead>
        <tr>
            <th><input type="checkbox"></th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox" name="a[1]"></td>
            <td>John Smith</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="a[2]"></td>
            <td>Jack Daniels</td>
        </tr>
    </tbody>
</table>
Using:
$('table').checkboxSelection();

Wednesday, April 28, 2010

How to limit joined records in MySQL

I have to tables with one-to-many relationship. I needed to select all from the master table and join only one record which contains the oldest date.
The solution is:
SELECT parent.*,
       c1.*
  FROM parent
       JOIN child c1
          ON c1.parent_id = parent.id
       LEFT JOIN child c2
          ON c2.parent_id = c1.parent_id AND c2.sort < c1.sort -- field to determine the order (maybe whatever you want)
GROUP BY parent.id,
         parent.name,
         c1.id,
         c1.name
HAVING COUNT(c2.id) < 1 -- limitation of joined records
ORDER BY parent.name, c1.name;
I found it there.

Saturday, April 3, 2010

ATI driver problems after upgrading Ubuntu 9.10 to 10.04

I have upgraded my desktop Ubuntu 9.10 to 10.04. Everything was fine except ATI driver didn't want to be installed. An error was in fglrx-amdcccle package,After that, I've reported the bug to Ubuntu Bug Tracker, and today I got the decision of my problem:
apt-get remove xorg-driver-fglrx fglrx fglrx-amdcccle
dpkg-divert --remove /usr/lib/xorg/modules/extensions/libdri.so
dpkg-divert --remove /usr/lib/xorg/modules/extensions/libglx.so
apt-get install fglrx

I thank Jean-Baptiste Lallement for help!

Monday, March 8, 2010

Check class

In case if you have to use pure Javascript:
    var hasClass = function(element,className) {
        return (function() {
            var classes = element.className.split(' ');
            for(var i in classes) {
                if(classes[i] == className) {
                    return true;
                }
            }
            return false;
        })();
    }

Tuesday, March 2, 2010

CSS attribute selectors


[rel=value] — attribute equals to value

<h1 rel="value">Attribute Equals</h1>

h1[rel=value] { color: red; }

[rel*=value] — attribute contains value anywhere

<h1 rel="xxxvaluexxx">Attribute Contains</h1>

h1[rel*=value] { color: red; }

[rel^=value] — attribute starts with value

<h1 rel="value-link yep">Attribute Begins</h1>

h1[rel^=value] { color: red; }

[rel$=value] — attribute ends with value

<h1 rel="friend value">Attribute Ends</h1>

h1[rel$=value] { color: red; }

[rel~=value] — attribute contains value in space-delimited list

<h1 rel="friend value sandwich">Attribute Space Separated</h1>

h1[rel~=value] { color: red; }

[rel|=value] — attribute contains value in dash-delimited list

<h1 rel="friend-value-sandwich">Attribute Dash Separated</h1>

h1[rel|=value] { color: red; }

Table checkboxes selection

To select or unselect checkboxes in a table I use the follow code
(function() {
    // checkboxes in the table
    var cbs = document.getElementsByName('checkboxes');
    // header checkbox
    var all = document.getElementById('header_checkbox');
    all.onclick = function() {
        for(var i=0;i<cbs.length;i++) {
            cbs[i].checked = this.checked;
        }
    }
    var cbClick = function() {
        all.checked = (function() {
            for(var i=0;i<cbs.length;i++) {
                if(cbs[i].checked != true) {
                    return false;
                }
            }
            return true;
        })();
    }
    for(var i=0;i<cbs.length;i++) {
        cbs[i].addEventListener('click', cbClick, true);
    }
})();

Monday, February 22, 2010

Universal archive unpacking tool

###   Handy Extract Program

extract () {
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2) tar xvjf $1   ;;
            *.tar.gz)  tar xvzf $1   ;;
            *.bz2)     bunzip2 $1    ;;
            *.rar)     unrar x $1    ;;
            *.gz)      gunzip $1     ;;
            *.tar)     tar xvf $1    ;;
            *.tbz2)    tar xvjf $1   ;;
            *.tgz)     tar xvzf $1   ;;
            *.zip)     unzip $1      ;;
            *.Z)       uncompress $1 ;;
            *.7z)      7z x $1       ;;
            *)         echo "'$1' cannot be extracted via >extract<" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
}
Поместить в ~/.bashrc или profile

original

Thursday, February 4, 2010

Setting up a network adapter alias

sudo ifconfig eth0:[0-254] 192.168.1.2 up.

Instead of eth0 you can use any of existing interfaces

Wednesday, January 27, 2010

Firefox extension. Thoughts.

While I try to see into Firefox Extension development, I found not evident things for me. And here I'll list'em.
  • You want to append event handler to created by you DOM-element somewhere in '1body' descendants. Don't try to use onclick or like it - you'll get a fail. Use addEventHandler method, and you'll get success :)

Tuesday, January 26, 2010

Django model field inheritance

To inherit model field in Django is not enough simply inherit a field class. It is needed to add attribute __metaclass__ = models.SubfieldBase. After that, you can overload any method you want.

Firefox extension. The first tries

I am developing the service which I'll describe further. And I decide to create a Firefox extension for it. And what I have dug:
  • To create a developer profile may that
    firefox -profilemanager
  • To run another -- development -- copy of Firefox
    firefox -no-remote -P <profile name>
And I assume firefox binary in your path.