Archive for the ‘tips & triks’ Category

Glade and GtkAdjustment

Monday, March 1st, 2010

Today I discovered a strange behavior while working with glade and gtk-adjustments, in particular I noticed that the default value of an adjustment was not properly loaded inside my spinbutton widget. So I wrote on the mailing-list and they pointed me out this.
The solution? Edit the xml file hand, and modify the adjustment object in order to make the value attribute being placed after the upper and the lower one.

Gnome menu icons

Thursday, February 25th, 2010

I’ve lost half an hour wondering why my icons were not displayed beside the menu labels.
In the end I found the solution:

gconftool-2 --type bool --set /desktop/gnome/interface/menus_have_icons

Python: packaging

Sunday, February 7th, 2010

Given a structure of a project ..

  gstream/
    bin/
      gstream
    gstreamlib/
      __init__.py
      gui.py
      player.py
      playlist.py

.. you can create a simple setup.py script containing:

#!/usr/bin/env python

from distutils.core import setup

setup(name='gstream',
      version='0.5',
      author='Matteo Landi',
      author_email='landimatte@gmail.com',
      scripts=['bin/gstream'],
      package_dir={'gstreamlib': 'gstreamlib'},
      packages=['gstreamlib'],
     )

Rename a MySql database

Sunday, January 10th, 2010

This morning I needed to rename my database and found these three commands I’m going to show you:

mysqldump -u username -p -v olddatabase > olddbdump.sql
mysqladmin -u username -p create newdatabase
mysql -u username -p newdatabase < olddbdump.sql

[ source: stackoverflow.com ]

Backup and restore your linux workstation

Monday, January 4th, 2010

Take this as a note rather than a guide, by the way don’t forget to execute these commands as root.
Backup:

cd /
tar cvpzf backup.tgz --exclude=/proc --exclude=/lost+found \
    --exclude=/backup.tgz --exclude=/mnt --exclude=/sys --exclude=/dev /

Restore:

tar xvpfz backup.tgz -C /
mkdir proc lost+found mnt sys dev

[ source Ubuntu Forums ]

Python: defaults arguments

Tuesday, December 29th, 2009

Here is a funny snippet of code:

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class foo:
...   def __init__(self, x=[]):
...     self.y = x
...
>>> a = foo()
>>> a.y.append("123456")
>>> a.y
['123456']
>>> b = foo()
>>> b.y
['123456']
>>> a.y.append("987654")
>>> a.y
['123456', '987654']
>>> b.y
['123456', '987654']

You better define foo as following:

>>> def foo():
>>>  def __init__(self, x=None):
>>>    if x is None: x = []
>>>    self.y = x

Python: From a list of digits, to a number

Tuesday, September 22nd, 2009
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n = reduce(lambda s, x: s*10 + x, l)
print l, n

Python: Multiple Replace

Monday, September 14th, 2009

Create the dictionary used for the substitusions.

repl_dict = {'a': "aRbFR", 'b': "LFaLb"}

Pre-compile the regular expression.

regex = re.compile("(%s)" % "|".join(map(re.escape, repl_dict.keys())))

Create some text, and do the replace on it.

old = "Fa"
new = regex.sub(lambda mo: repl_dict[mo.string[mo.start():mo.end()]], old)

In the example below you can see the evolution of the string which describes the Heighway Dragon at n-th step.

import re

repl_dict = {'a': "aRbFR", 'b': "LFaLb"}
regex = re.compile("(%s)" % "|".join(map(re.escape, repl_dict.keys())))

d = "Fa"
n_iterations = 5
for i in xrange(n_iterations + 1):
  if i == 0: d = "Fa"
  else: d = regex.sub(lambda mo: repl_dict[mo.string[mo.start():mo.end()]], d)

[ source: Active State Code ]

Python: for .. else

Saturday, September 12th, 2009
r = [1, 3, 5, 11, 55, 89]

for elem in r:
  if elem == 15:
    print 'Found an element equal to 15'
    break
else:
  print 'Given array does not contain any elements equal to 15'

Python: double for loop statement

Sunday, September 6th, 2009

Imagine you have a N x M matrix and you need to loop over each element of it.The first thing coming into my mind was to use a double for loop such as:

# ...
for i in xrange(N):
  for j in xrange(M):
    # do some action with element (i, j)
# ...

Now imagine to put this double-for statement inside a single-for one because you need to repeat the same action, let’s say, T times:

# ...
for t in xrange(T):
  for i in xrange(N):
    for j in xrange(M):
      # do some action with element (i, j)
# ...

Wait a moment: is all i have written necessary? Why should i need to generate all the M elements every-time? Here is a pretty solution:

def Cross(a, b):
  for i in a:
    for j in b:
      yield (i, j)

# ...
for t in xrange(T):
  for (i, j) in Cross(xrange(N), xrange(M)):
    # do some action with element (i, j)
# ...

[ source: Google Code Jam ]