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...
Wednesday, August 04, 2010
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
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:
I installed numpy and matplotlib with easy_install:
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 numpyThe installation of matplotlib got an error message as below:
$ sudo easy_install2.6 matplotlib
error: Setup script exited with error: command 'gcc' failed with exit status 1I 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 matplotlibSo 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:
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.
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.
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 functionThat'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.
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?
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:
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!
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:
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.
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]
[label_test2.py]
---
Ref:
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,
Friday, May 28, 2010
[QnA] Use xmodmap to set modifier keys
In the previous article, I described how to change the modifier key for wmii from Mod1(Alt) to Mod4(Windows key). That's no problem for my PCs, but not okay for my laptop, an IBM X31. The laptop has NO Windows key, so I cannot just modify the wmiirc file to make things run.
Fortunately, Google helped me to find the solution again. There is a command called xmodmap to modify keymaps.
The steps are:
---
Reference:
---
Edit (2010/06/01):
After changing Caps Lock key to Windows key, wmii cannot move windows to desktop#2.
Fortunately, Google helped me to find the solution again. There is a command called xmodmap to modify keymaps.
The steps are:
- Create a file, for example, ~/.Xmodmap
- Paste the following lines in the created file
- Run xmodmap to enable the settings
! No Caps Lock
clear lock
! Caps Lock as Win key
add mod4 = Caps_Lock
$ xmodmap ~/.Xmodmap
---
Reference:
---
Edit (2010/06/01):
After changing Caps Lock key to Windows key, wmii cannot move windows to desktop#2.
[SW] Change modifier key for wmii
I have adopted wmii as my main WM for months and it has given pretty good experience in my daily usage. There has been one thing, however, bothering me a bit. The default modifier key of wmii is Alt, which is presented as Mod1 in wmiirc. Because Alt is a frequently used key for tools such as BASH, this default setting could cause some inconveniences. For example, when you want to move forward in BASH commands, Alt+F won't give you the expected moving but will make the working window run in full screen. Fortunately, wmii has been featured as its configurability.
In my system, which is Ubuntu 8.04, the confiuration files are located in
/etc/X11/wmii-3.5/
There are two files look like something to be the configureation file: rc.wmii and wmiirc. To make sure which one is used, use the following commands
ps aux | grep wmii | grep rc
In my case, it is the wmiirc file.
In the beginning of wmiirc, there are several lines which set the keys.
MODKEY=Mod1
UP=k
DOWN=j
LEFT=h
RIGHT=l
I set MODKEY from Mod1 to Mod4 which is the Windows key. Re-login and you can use wmii without the ``Alt conflict'' with many other programs.
In my system, which is Ubuntu 8.04, the confiuration files are located in
/etc/X11/wmii-3.5/
There are two files look like something to be the configureation file: rc.wmii and wmiirc. To make sure which one is used, use the following commands
ps aux | grep wmii | grep rc
In my case, it is the wmiirc file.
In the beginning of wmiirc, there are several lines which set the keys.
MODKEY=Mod1
UP=k
DOWN=j
LEFT=h
RIGHT=l
I set MODKEY from Mod1 to Mod4 which is the Windows key. Re-login and you can use wmii without the ``Alt conflict'' with many other programs.
Wednesday, May 26, 2010
[SW] Internet radio for Ubuntu
Rhythmbox was the software I used to listen music and internet radios. Due to reasons I don't understand, Rhythmbox does not work properly for the internet radios anymore, and I've tired to find out the solution. I started to search possible alternatives for me to listen internet radios. Firstly I found Exaile and it looked good, but I didn't get its plugin (shoutcast radio) work.
Finally, I found Tunapie. I installed it and Stremripper via Synaptic. After the installation, I set the audio player as VLC in Tunapie's preference, then everything goes well. :-)
(But Hot 108 Jamz station cannot be played... Orz)
Finally, I found Tunapie. I installed it and Stremripper via Synaptic. After the installation, I set the audio player as VLC in Tunapie's preference, then everything goes well. :-)
(But Hot 108 Jamz station cannot be played... Orz)
Saturday, May 22, 2010
[Py] Use ``list comprehensions'' to avoid nesting ``for loops'' too deeply
As I have mentioned in the previous post, I am trying to analyze some data using Python as the programming language. The data sets are from 4 sensor nodes, and each nodes give accelerations in 3 axes. Furthermore, I want to process these data sets in sequential mode. So, the original structure of my program looked like this:
That's crazy! These nested for loops are too deep to be followed. Also, I remember once I read an article written by an experienced programmer, who said if your for loops nested too deep, then maybe you need to redesign you program.
I had no idea about how to redesign the structure of my program to fit my original requirements. Fortunately, the list comprehensions could resolve this difficulty. Here is the new version using the list comprehensions:
data = zeros((n_nodes,3))
for i in range( len(databuffer) ):
for nodes in range(4):
for xyz in range(3):
data[nodes][xyz] = databuffer[nodes][xyz][i]
if (some conditions):
do something
That's crazy! These nested for loops are too deep to be followed. Also, I remember once I read an article written by an experienced programmer, who said if your for loops nested too deep, then maybe you need to redesign you program.
I had no idea about how to redesign the structure of my program to fit my original requirements. Fortunately, the list comprehensions could resolve this difficulty. Here is the new version using the list comprehensions:
The list comprehensions remove two loops and make a conciser new structure.
for i in range( len(databuffer[0][0]) ):
data = array([databuffer[nodes][xyz][i]\
for nodes in range(n_nodes) for xyz in range(3)]).reshape(n_nodes,3)
if(some conditions):
do something
Thursday, May 20, 2010
[Py] Multi-dimensional list
So far, the list in Python has bothered me a lot. I think it's because I do not understand it enough. These days I've been writing a little program to analyze some data, and encountered unexpected situations which almost got me down.
One unexpected situation was the multi-dimensional list, or the list of lists. The following page describes exactly what had troubled me:
(An Unofficial) Python FAQ Wiki (old link)
How do I create a multidimensional list?
This surprised me and made me thought again: ``Do I really know how to program in Python?'' The answer is definitely no. The low threshold to begin Python programming made me overestimate my understanding of it. Therefore, I told myself to learn more before stepping further.
---
Another situation surprised me is also about the list creation. The following test shows it:
>>> a = b = [[]]*3
>>> a[1] = 1
>>> b
[[], 1, []]
To assign values to list a also affects list b. To avoid this situation, you should create the lists separately.
>>> a = [[]]*3
>>> b = [[]]*3
>>> a[1] = 1
>>> b
[[], [], []]
One unexpected situation was the multi-dimensional list, or the list of lists. The following page describes exactly what had troubled me:
How do I create a multidimensional list?
This surprised me and made me thought again: ``Do I really know how to program in Python?'' The answer is definitely no. The low threshold to begin Python programming made me overestimate my understanding of it. Therefore, I told myself to learn more before stepping further.
---
Another situation surprised me is also about the list creation. The following test shows it:
>>> a = b = [[]]*3
>>> a[1] = 1
>>> b
[[], 1, []]
To assign values to list a also affects list b. To avoid this situation, you should create the lists separately.
>>> a = [[]]*3
>>> b = [[]]*3
>>> a[1] = 1
>>> b
[[], [], []]
Monday, May 17, 2010
[Py] PyODE installation and test
I read about ODE (no, not the ODE in math) in a thesis last week, and searched it to see what it is. ODE is the abbreviation of Open Dynamic Engine, an library for simulating rigid body dynamics. As a Python fan, it's reasonable for me to search related module of ODE written in Python. The keywords ``python ode'' brought me the PyODE, a set of Python bindings for ODE.
There are three examples on the PyODE page. I tried to test it, and installed what my system lacked accordingly.
The installation in Ubuntu is quite easy as usual. My procedures are listed as the follows.
First, get the PyODE module, which is the basic one to run the 1st example:
$ sudo apt-get install python-pyode
Then if you want to run the 2nd example, you need Pygame:
$ sudo apt-get install python-pygame
Finally, the 3rd example demands PyOpenGL:
$ sudo apt-get install python-opengl
After these steps, I found the best way to get all these examples is to install the documents of PyODE:
$ sudo apt-get install python-pyode-doc
You could find the source code of all the three examples in
/usr/share/doc/python-pyode-doc/examples
There are two more examples in the folder, but they needs more libraries. To run the ``transforms.py'' example, the error message showed there was no module named ``cgtypes.'' It seems to be in the cgkit package. This package cannot be found in Ubuntu repository, so I downloaded the source and read the README. The SCons and Boost are necessary:
$ sudo apt-get install scons
$ sudo apt-get install libboost-dev
But I couldn't install the package and still get no luck so far.
I decide not to bother with the Scons and Boost by now. If I have some time, I would like to play with the original 3 examples to see whether I can learn something fun and useful from them.
When I ran the setup.py to install the package, there came lots of error messages. With some efforts, I noticed one of the error message read there was no file named ``boost/python.hpp.'' And with some more efforts, it came to me that maybe something like ``libboost-dev'' is needed. Therefore, I typed libboost in the terminal and used the Tab key to check available packages. Yes, there was a library named ``libboost-python-dev'' which looked like what I needed, so I installed it by typing:
$ sudo apt-get install libboost-python-dev
Then, everything was ready to install the cgkit in the downloaded and extracted folder:
$ sudo python setup.py install
The cgkit has been installed, though, the example transforms.py cannot be executed correctly. According to the error message (ImportError: No module named cgtypes), I edited the line in the transforms.py from
from cgtypes import *
to
from cgkit.cgtypes import *
After the modification, new problem emerged. The window showed up and then closed, with the eorror message given in the terminal:
freeglut ERROR: Function called without first calling 'glutInit'.
Then I found the page talking about this issue. So what I had to do was to find somewhere to put the glutInit function call. I tested for several times but totally had no idea where to add the glutInit, then I noticed the keyword in the error message: Function . It took me to the right location of the code to add a line as follows:
glutInit(argc, argv)
glutSolidCube(1)
It didn't work and threw back a name error:
NameError: global name 'argc' is not defined
I thought there might be no argument for this function call in the code, so I deleted the argc and argv to make the glutInit be called as the following:
glutInit()
glutSolidCube(1)
Bingo! Although I am still not sure whether the transforms.py runs properly, at least the window showed up with several moving cubes inside.
By far, the problem to run the transforms.py is solved. However, there is still an example code named vehicle.py cannot be run properly in my system. I have to do other works. Maybe I will come back to find out the solution for the vehicle.py.
There are three examples on the PyODE page. I tried to test it, and installed what my system lacked accordingly.
The installation in Ubuntu is quite easy as usual. My procedures are listed as the follows.
First, get the PyODE module, which is the basic one to run the 1st example:
$ sudo apt-get install python-pyode
Then if you want to run the 2nd example, you need Pygame:
$ sudo apt-get install python-pygame
Finally, the 3rd example demands PyOpenGL:
$ sudo apt-get install python-opengl
After these steps, I found the best way to get all these examples is to install the documents of PyODE:
$ sudo apt-get install python-pyode-doc
You could find the source code of all the three examples in
/usr/share/doc/python-pyode-doc/examples
There are two more examples in the folder, but they needs more libraries. To run the ``transforms.py'' example, the error message showed there was no module named ``cgtypes.'' It seems to be in the cgkit package. This package cannot be found in Ubuntu repository, so I downloaded the source and read the README. The SCons and Boost are necessary:
$ sudo apt-get install scons
$ sudo apt-get install libboost-dev
I decide not to bother with the Scons and Boost by now. If I have some time, I would like to play with the original 3 examples to see whether I can learn something fun and useful from them.
When I ran the setup.py to install the package, there came lots of error messages. With some efforts, I noticed one of the error message read there was no file named ``boost/python.hpp.'' And with some more efforts, it came to me that maybe something like ``libboost-dev'' is needed. Therefore, I typed libboost in the terminal and used the Tab key to check available packages. Yes, there was a library named ``libboost-python-dev'' which looked like what I needed, so I installed it by typing:
$ sudo apt-get install libboost-python-dev
Then, everything was ready to install the cgkit in the downloaded and extracted folder:
$ sudo python setup.py install
The cgkit has been installed, though, the example transforms.py cannot be executed correctly. According to the error message (ImportError: No module named cgtypes), I edited the line in the transforms.py from
from cgtypes import *
to
from cgkit.cgtypes import *
After the modification, new problem emerged. The window showed up and then closed, with the eorror message given in the terminal:
freeglut ERROR: Function
Then I found the page talking about this issue. So what I had to do was to find somewhere to put the glutInit function call. I tested for several times but totally had no idea where to add the glutInit, then I noticed the keyword in the error message: Function
glutInit(argc, argv)
glutSolidCube(1)
It didn't work and threw back a name error:
NameError: global name 'argc' is not defined
I thought there might be no argument for this function call in the code, so I deleted the argc and argv to make the glutInit be called as the following:
glutInit()
glutSolidCube(1)
Bingo! Although I am still not sure whether the transforms.py runs properly, at least the window showed up with several moving cubes inside.
By far, the problem to run the transforms.py is solved. However, there is still an example code named vehicle.py cannot be run properly in my system. I have to do other works. Maybe I will come back to find out the solution for the vehicle.py.
Friday, April 16, 2010
[Chat] Simple is not so simple -- The hacker emblem and Conway's game of life
I am not so good at programming, but I am always willing to know or to learn something new about programming. It's natural for me to getting more and more interesting at the open-source and then the hackers' culture. Of course, I'd tried to find some information about ``how to become a hacker'' and the very same sentence made Google bring me the article ``How To Become A Hacker'' written by Eric Steven Raymond.
Raymond's article is delightful and easy to read. Howevr, I didn't read it thoroughly at the first reading, and the following tiny picture, which is called ``glider,'' didn't attract me too much. I just wondered what it was and then forgot it.

One day, I read one of Raymond's article again, and linked to the FAQ to check out what the ``glider'' is. After I realized what is the glider and what is Conway's Game of Life, all I wanted to say was ``Wow! Cool!'' just as that have been mentioned in Raymond's article.
The rules of Game of Life are simple, but the outcome is definitely not simple. You choose random cells' locations, then let the cells ``evolve'' under the simple rules. Just watch the ``evolution'' and it looks so vivid and fun!
I found LifeWiki might be a great page to dive into. You can find many amazing patterns, and play with those patterns online (with your browser's javascript enabled). Also, you can have Xlife in your system to play and test. If you are using Ubuntu, type the following command to install Xlife:
$ sudo apt-get install xlife
Finally, 2 videos for your reference:
Raymond's article is delightful and easy to read. Howevr, I didn't read it thoroughly at the first reading, and the following tiny picture, which is called ``glider,'' didn't attract me too much. I just wondered what it was and then forgot it.

One day, I read one of Raymond's article again, and linked to the FAQ to check out what the ``glider'' is. After I realized what is the glider and what is Conway's Game of Life, all I wanted to say was ``Wow! Cool!'' just as that have been mentioned in Raymond's article.
The rules of Game of Life are simple, but the outcome is definitely not simple. You choose random cells' locations, then let the cells ``evolve'' under the simple rules. Just watch the ``evolution'' and it looks so vivid and fun!
I found LifeWiki might be a great page to dive into. You can find many amazing patterns, and play with those patterns online (with your browser's javascript enabled). Also, you can have Xlife in your system to play and test. If you are using Ubuntu, type the following command to install Xlife:
$ sudo apt-get install xlife
Finally, 2 videos for your reference:
Tuesday, March 30, 2010
[QnA] Use the latest version of exam.cls for preparing exam papers
When preparing an exam paper, we might want to have two versions of this exam paper: one with answers for teachers and the other has only questions for students.
The exam.cls makes this task easy. But make sure you are using the latest version of it or you will not have the handy ``ifprintanswer'' function.
Here is an sample to show how to use the exam.cls with the ifprintanswer function:
---
Ref:
Using the LaTeX exam class
The exam.cls makes this task easy. But make sure you are using the latest version of it or you will not have the handy ``ifprintanswer'' function.
Here is an sample to show how to use the exam.cls with the ifprintanswer function:
\documentclass{exam} % Set ``exam'' as the document class
\begin{document}
\newcommand{\ans}[1]{\ifprintanswers{#1}\fi} % define a new command to simplify
% the following usage
\printanswers % Add this line to print the answers.
% If no answers printing is wanted, just comment this line out.
\begin{questions}
\question
\ans{The answer} The question's description
\begin{choices}
\choice The choice A
\choice The choice B
\choice The choice C
\choice The choice D
\end{choices}
\end{questions}
\end{document}
---
Ref:
Using the LaTeX exam class
Thursday, March 25, 2010
[QnA] Add cls or sty files for LaTeX
We always use certain packages in the latex files, and sometimes the packages are just not there so we need to add them for further working. It's was a big problem for me when I knew very few about the working principle of LaTeX. Now I think I have got a simple and clear process to add packages, that is, *.cls or *.sty, for LaTeX. Here is an example in which I added a so-called exam package for preparing exam papers.
- Step 1. Search for the exam package and download it from CTAN, and then of course unzip it.
- Step 2. Make a directory for adding the exam package. In my Ubuntu system, it is:
/usr/share/texmf-texlive/tex/latex/exam/
- Step 3. Copy all the files of the downloaded and unzipped exam package to the directory we just made.
- Step 4. Run the texhash command as root. Done.
Subscribe to:
Posts (Atom)