Thursday, November 04, 2010

Floating calculation in bash (with formatted output)

What I wanted was to get a list of incremental floating numbers, like:
0.0 0.2 0.4 0.6 ... 2.0

It is possible to accomplish this mission in bash, with the help of bc. My own script is listed below:
#!/bin/bash
for i in {0..20..2}
do
#f=$(printf %3.1f `echo "scale=1; $i/10" | bc`) # old version
printf -v f %3.1f `echo "scale=1; $i/10" | bc` # new version
echo $f
done

The bc command was used because bash cannot handle floating calculation directly.

Sunday, October 24, 2010

[QnA] Activate the ethernet card of Toshiba L600 laptop (for Ubuntu 10.04)

I've bought a laptop (Toshiba Satellite L600) several days ago. Everything seems alright when I inserted the Live CD of Ubuntu 10.04 to play example audio and video files. So I installed Ubuntu 10.04 in my new laptop. After the installation, I tried to connect to the Internet for system upgrade and found the pppoeconf complained with ``no Ethernet card'' messages.

From the searched results and based on my own incomplete test, here are the steps to activate the Ethernet card of the Toshiba Satellite L600:

Step 1: Check the type of card.
$ lspci | grep Ethernet
04:00.0 Ethernet controller: Atheros Communications AR8152 v1.1 Fast Ethernet (rev c1)


Step 2: Use the keyword of the card to search the driver. What I found is AR81Family-linux-v1.0.1.13.tar.gz

Step 3: Untar the downloaded files and install the driver.
$ make
$ sudo make install
$ cd src/
$ sudo cp atl1e.ko /lib/modules/2.6.32-21-generic/kernel/drivers/net/atlx/
$ sudo depmod -a

Step 4: Reboot the system then go for pppoeconf to set up the DSL connection.

Thursday, October 14, 2010

[Py] Assign values of one list to another

It is easy to ``copy'' lists in Python, but it is also easy to get things wrong, especially when you ignore something important like what I did.

Consider the following situation:
>>> A = [1,2,3]
>>> B = A
>>> B[0]=-1
>>> A
[-1, 2, 3]

What I want is to create a new list B which contains identical elements of the original list A. This doesn't work, however. As you can see in the above test, any modifications made on list B will affect list A. The reason is that when we type ``B=A'', the list B is just another name of list A. They are identical and of course are pointed to the same address. See the following tests:

>>> B is A
True
>>> B.index, A.index
(, )

So, if we need an independent list B which has a set of values' copy in list A, use ``list slicing'':
>>> B = A[:]
>>> B is A
False
>>> B.index, A.index
(, )
---
Ref: An Introduction to Python Lists

[Py] Be careful when create multi-dimensional lists

I wrote a note about the creation of multi-dimensional lists in Python, when I had not yet encountered another problem which have emerged recently. The problem is about appending items to the multi-dimensional lists.

Consider the following example:

>>> A = [[]]*3
>>> A
[[], [], []]
>>> for i in range(3):
...    for j in range(3):
...        A[i].append(i+j)
...
>>> A
[[0, 1, 2, 1, 2, 3, 2, 3, 4], [0, 1, 2, 1, 2, 3, 2, 3, 4], [0, 1, 2, 1, 2, 3, 2, 3, 4]]

But what I really want is something like
A = [[0, 1, 2], [1, 2, 3], [2, 3, 4]]

My guess is that the creation approach doesn't create a list which contains three independent rows, but just create three rows which actually point to the same address or something like that.

The solution (perhaps not the best one) is to create the rows in the form of list comprehension:

>>> A = [[] for rows in range(3)]
>>> A
[[], [], []]
>>> for i in range(3):
...    for j in range(3):
...        A[i].append(i+j)
...
>>> A
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

Friday, October 01, 2010

[QnA] Change the permission of viminfo to enable the command histroy of Vim

After upgrading from Ubuntu 8.04 to 10.04, I found the command history of Vim was missing. The reason is simple: in Ubuntu 10.04, the ~/.viminfo file has a 600 permission so only the root can access it.

To get the command history of vim back, you may change the permission from 600 to 666. I don't know whether this change has any side effects, however. At least for only one user using one computer, this is a quick trick. :-)

---
Ref:

Thursday, September 30, 2010

[QnA] Problems when using fbi/fbgs in virtual consoles of Ubuntu 10.04 (Lucid Lynx)

I upgraded all my computers (2 PCs and 1 laptop) from Ubuntu 8.04 to 10.04 these days. Most things are fine (and even great!), but there is one thing, the GRUB 2,t has brought me some troubles. I could not use fbi and fbgs in the consoles. Of course I ran into Google several times but got no lucks, until yesterday when I found the following thread and links:


The second link gives a complete guide.

FYI: To check the resolution given by framebuffer of your computer, use ``sudo hwinfo --framebuffer'' command in the console.

Sunday, September 19, 2010

[SW] Use GIMP to save layers of animated GIF

It is easy to use GIMP to create animation in GIF format (just use Google and you can find many examples). However, to extract individual layers of an animated GIF is another story, and I can't find a straightforward approach in GIMP.

Fortunately, the solution has been exist. There is a script by

Monday, September 06, 2010

[Py] Rubik's cube game

Although I am not good at solving Rubik's Cube, I am a fan of it. Of course, I am always wondering whether there are programs which can show a 3D Rubik's Cube in my computer so that I can play the cube interactively. And the answer is YES!

On Pygame site, there is a wonderful program called Rubik's Cube Game meets all my needs. Actually, I've downloaded and tested it several months ago, but until today I finally get clear steps to make the program run properly. My modifications are as follows.
  1. rename Images/*.PNG to Images/*.png
  2. apt-get install python-opengl
  3. download gameobjects and install it
  4. remove *.pyc
  5. rename *.pyw to *.py
  6. run ``python Rubik's Cube.py''

Thursday, September 02, 2010

Converting filenames from UPPERCASE to lowercase

I searched and found the following thread:
http://blog.mc-thias.org/?title=rename-files-from-upper-case-filename-to&more=1&c=1&tb=1&pb=1

Based on the post given by Jadu Saikia, I got the bash script of my own version as follows:
ls * | sed -e p -e 's/.*/\L&/g' |xargs -n 2 mv

One new thing I've learned in this example is the ``\L'' part, which can be found in sed's FAQ.

Wednesday, August 04, 2010

[Py] Install pygame-1.9.1 in Ubuntu (with running a game: Cave Copter)

I went to pygame's website to see whether there are some very simple examples for me to begin with. After several trials, I found the pygame version of Ubuntu 8.04 was 1.7 and it was too old to run some games, so I decided to install pygame from the source.

Things were almost easy. Just download and extract the source files and run the setup.py. Some error messages showed up:
sh: sdl-config: not found
sh: smpeg-config: not found


It means you need to install the following libraries.
$ sudo apt-get install libsdl-dev libsmpeg-dev

Ran the setup.py again and I got other warning messages as follows.
FONT : not found
IMAGE : not found
MIXER : not found
PNG : not found
JPEG : not found
PORTMIDI: not found
PORTTIME: not found

What I installed were:
libjpeg-dev libpng12-dev libportmidi-dev libsdl-ttf2.0-dev libsdl-image1.2-dev libsdl-mixer1.2-dev

Finally, ran
$ sudo python setup.py
and got new version pygame work!

---
After installing pygame-1.9.1, I tried to test the game, Cave Copter.

Cave Copter is a game using pygame. To make it run properly in Ubuntu, there still some minor works to do.

First, we need to change all the double backslashes in the original CaveCopter.py to single slash. Second, we have to change the *.PNG to *.png so that these png files can be recognized.

Hope I will have time to play with and to learn something from the source code...

Thursday, July 01, 2010

[Py] pty module testing note

The pty module can be used for pseudo terminals, about which I actually know very little.

Here are some simple tests I have conducted in my own Ubuntu PC, and I write down it just as a reminding note.

---
Open pseudo terminal pairs

At the beginning, check the content of /dev/pts:
$ ls /dev/pts
0 1 ptmx

Open another terminal for IPython and then test with pty module as the following:
In [1]: import pty

In [2]: pty.openpty()
Out[2]: (3, 4)

Now check /dev/pts again:
$ ls /dev/pts
0 1 2 ptmx

Go back to IPython:
In [3]: pty.openpty()
Out[3]: (5, 6)

The content of /dev/pts becomes:
$ ls /dev/pts
0 1 2 3 ptmx

Exit IPython, then check the /dev/pts:
$ ls /dev/pts
0 1 ptmx

---
Read and Write test

To read from the pseudo terminal, it seems necessary to write something first.
In [1]: import pty

In [2]: pty.openpty()
Out[2]: (3, 4)

In [3]: pty._writen(3,"test")

In [4]: pty._read(3)
Out[4]: 'test'

---
Ref:
how to use /dev/ptmx for create a virtual serial port?
Python: module pty

Wednesday, June 23, 2010

[Py] Work with multiple versions of Python

In Ubuntu 8.04, the default version of Python is 2.5, and I want to try Python 2.6 without messing up Python 2.5. I followed Andreas Bernauer's article but still had problems.

During the installation process, I found another useful article which gives also clear guidance:
Installing multiple versions of Python on Ubuntu from Source

After installing Python 2.6 successfully, I linked python command to Python 2.6, but got some problems. All the third-part packages (installed in the `site-packages' folder) cannot be seen by Python 2.6. I tried to link the folders but got no luck. Then I noticed the ``setuptools'' which claims:
Download, build, install, upgrade, and uninstall Python packages -- easily!
Then followed the steps which also given in Installing multiple versions of Python on Ubuntu from Source. Finally, set link to easy_install, for example:
$ sudo ln -s /opt/python2.6/bin/easy_install-2.6 /usr/bin/easy_install2.6
---
I installed numpy and matplotlib with easy_install:
$ sudo easy_install2.6 numpy
$ sudo easy_install2.6 matplotlib
The installation of matplotlib got an error message as below:
error: Setup script exited with error: command 'gcc' failed with exit status 1
I followed the method proposed here, and installed ``libpq-dev'' but it didn't work. Then I found the solution which is as follows:
$ sudo apt-get build-dep matplotlib
So far, one thing is still bothering me. I have no idea how to let IPython work with Python 2.6.

Wednesday, June 16, 2010

[Py] Use lambda expression for callback functions of widgets

Let's see a quotation about Tkinter callbacks:
A common beginner’s mistake is to call the callback function when constructing the widget. That is, instead of giving just the function’s name (e.g. “callback”), the programmer adds parentheses and argument values to the function
That's true. I just solved one problem of this kind in my code.

In short, we cannot use callbacks of widgets in the form of
Widget(text="some text", command=callback(argv)).pack()

The reason has been stated clearly in the first link of this post. To call the callbacks which have arguments, we need to utilize the lambda expression.

I have tried with 3 test programs. They are listed as follows.

1. from Tkinter import *
2.
3. def cb_test(i):
4. print i
5.
6. root = Tk()
7.
8. for i in range(5):
9. Button(root, text=str(i), command=cb_test(i)).pack() # test 1
9. Button(root, text=str(i), command=lambda:cb_test(i)).pack() # test 2
9. Button(root, text=str(i), command=lambda x=i:cb_test(x)).pack() # test 3
10. Button(root, text="Quit", command=root.quit).pack()
11.
12. root.mainloop()

If you would like to try with my codes, please note there are 3 lines denoted as line 9. Keep only one of them and delete the other two.

[Py] Add new attributes as static variables

One problem I have met when coding with Python is that there seems no static variables to apply. A straightforward approach is to use the global variables, but it is not such a safe way.

Several days ago, when I was writing my nth version of my low-pass/high-pass filtering functions, I found a nice method to keep some values of certain variables which would be used in recursive steps. This method is to create new attributes of the called function, and the created attributes could be used as static variables.

The original information has been given in Ref. and I would like to repeat it again but with my own understanding. The function which have to keep some local variables as static ones could be as follows.

1. def foo(argv):
2. if not "your_static_var" in dir(foo):
3. foo.your_static_var = certain initial values
4. do something with foo.your_static_var
5. return foo.your_static_var

In line 2, we check all the attributes of foo() by the built-in function dir(). If foo() is called for the first time, we could create new attributes with initializations as shown in line 3. I think this is the most brilliant part of the method.

Next time when you need ``local'' static variables for certain functions, this approach may help.

---
Ref: (see the post by Cameron Laird)
Python - Static Variables in Python?

Sunday, June 13, 2010

[SW] Color toggle -- Firefox plugin to swap foreground and background color

Many of us use browsers very often, and maybe some of you just don't like the default white color of many web pages. I don't like either.

I am used to work and view documents in black background with white fonts color, just like the appearance of command line terminals. So I began to search the method which can help me in reversing my browser's foreground and background colors.
The answer for Firefox is Color toggle by Nathan Baker. Color toggle is a simple plugin which is very easy to use for swapping or toggling colors between two color profiles. Therefore with this plugin, you can toggle your foreground and background colors in very quick key pressing (the default is Ctrl+Shift+u).

The original looking:

After toggling:

Friday, June 11, 2010

[QnA] Command line rocks!! Combine several data files...

I have several data files. Each of them has only one column which presents acceleration along one axis. I want to combine them into one single file so that I can read them into my Python program with opening only one file.

To illustrate the situation, here are some sample contents of the files:

ACC_X.txt
126
127
129
127
137

ACC_Y.txt
132
106
109
114
105

ACC_Z.txt
137
139
138
138
144

What I want is to combine them in columns within a single files as:
ACC.txt
126,132,137
127,106,139
129,109,138
127,114,138
137,105,144

The first thing came into my head is using awk. However, I am not familiar with awk script. With brief searching, I found two easy ways in the command line: paste and pr.

The command using paste could be:
$ paste -d, ACC_X.txt ACC_Y.txt ACC_Z.txt > ACC.txt

Or you can use pr like this:
$ pr -mts, ACC_X.txt ACC_Y.txt ACC_Z.txt > ACC.txt

The comma in the commands means I am using it as the delimiter or separator.

Simple and fast. Command line really rocks!

Sunday, June 06, 2010

[Py] To show or update images of label

To show or update images in a label, there is one thing important: You have to keep a reference for the image objects or they will be cleared so you won't see them on your label widgets.

I totally had no idea about this fact and tried for several hours until I read the following note:
Note: When a PhotoImage object is garbage-collected by Python (e.g. when you return from a function which stored an image in a local variable), the image is cleared even if it’s being displayed by a Tkinter widget.

To avoid this, the program must keep an extra reference to the image object. A simple way to do this is to assign the image to a widget attribute, like this:

label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()

It's also true if you want to show images in sequence and then want to keep the last image when the update stops. Without the ``keep reference'' line, you will see the images updated sequentially and disappear after the last image being showed.

Saturday, June 05, 2010

[Py] Update label's content

I am writing a simple program in Python with Tkinter, and got troubles when I wanted to update the text showed in the label. I have a variable which has been changed in certain function and I want to show its latest value on a label of the root window.

Before I find the solutions, I think all I need are Label.config() and time.sleep(). However, they didn't make the program run as what I expected.

Finally, I found the key is the update() function. There are two ways to do what I want, but I don't know the differences between them. Let me just show the test programs I've written.

[label_test1.py]
from Tkinter import *
import time

class App:
def __init__(self, master):

frame = Frame(master).pack()
master.geometry("200x100")

var_text = StringVar()
counter = Label(frame)
counter.config(textvariable=var_text, font=("arial",60,"bold"), bg="green")
counter.pack(expand=YES, fill=BOTH)

for i in range(10):
time.sleep(1)
var_text.set(i)
master.update()

def main():
root = Tk()
app = App(root)
root.mainloop()

if __name__ == "__main__":
main()


[label_test2.py]
from Tkinter import *
import time

class App:
def __init__(self, master):

frame = Frame(master).pack()
master.geometry("200x100")

counter = Label(frame)
counter.config(font=("arial",60,"bold"), bg="green")
counter.pack(expand=YES, fill=BOTH)

for i in range(10):
time.sleep(1)
counter.config(text=i)
master.update()

def main():
root = Tk()
app = App(root)
root.mainloop()

if __name__ == "__main__":
main()

---
Ref:

[Py] Countdown counter

Here is a countdown counter which counts down in HH:MM:SS format. The original example has given by vegaseat, which counts increasingly. I modified it to be a countdown version as the follows.


"""
A ``countdown'' counter using Tkinter
Original version has given by vegaseat 17aug2007
http://www.daniweb.com/code/snippet216971.html

This version is modified from the original one
by thk 2010/06/05
"""

import Tkinter as tk
from itertools import count

def start_counter_down(label):
counter = count(0)
begin_time = 10 # sec.
def update_func():
left_time = begin_time - counter.next()
show_hr = left_time/3600
show_min = (left_time%3600)/60
show_sec = (left_time%3660)%60
label.config(text=\
str(show_hr).zfill(2)+':'+\
str(show_min).zfill(2)+':'+\
str(show_sec).zfill(2))
label.after(1000, update_func) # 1000ms
if left_time <= 0: label.config(text="Time's up!") update_func() root = tk.Tk() root.title("Counting Down") label = tk.Label(root, fg="red") label.pack() start_counter_down(label) button = tk.Button(root, text='Stop & Quit', width=30, command=root.destroy) button.pack() root.mainloop()

Tuesday, June 01, 2010

[Py] Canvas example (correction)

Here are some GUI examples of Python with Tkinter. One of the example shows how to draw lines in the canvas. However, the source code has some errors. I have tested and corrected the errors as the following lines:

21c21
< x =" 250"> y = 250 - (i * 40)
28c28
<> scaled.append((100 + 3*x, 250 - (4*y)/5))
33c33
< width="1,"> canvas.create_oval(xs-6,ys-6,xs+6,ys+6, width=1,