Tuesday, May 19, 2009

The problem of pm-hibernate solved

Abstract: Solve the problem of pm-hibernate restarts computer.

When I try to hibernate my laptop running Arch Linux with pm-hibernate which is also used by GNOME behind the scene, it restarts immediately after hibernate to disk. I checked Arch wiki and find a link to opensuse wiki Pm-Utils. After add a s2ram force option as instructed, the problem is solved. I added the following lins to /etc/pm/config.d/config

HIBERNATE_MODE="shutdown"
S2RAM_OPTS="-f -p -m"

The "-p" parameter was the key to a successful hibernate for me.

Sunday, May 17, 2009

Script to synchronize VIM settings

Abstract: My scripts to backup vim setting and synchonize between Windows and Linux.


Finally I cannot bare the boring work of copying my vim settings around my labtop(with both windows and linux installed), lab pc and working server. So I write a few scripts to automatic this work for me:)


The first script is used to copy the scirpts to a certain dir on my laptop that can be share accross Windows and Linux systems. tr and sed are used to convert file format and handle some syntax difference.

#!/bin/bash
CURDIR=`pwd`
BACKDIR="/cygdrive/e/vim-setting"
if [ -d $BACKDIR ] ; then
echo "Backup to $BACKDIR"
else
mkdir "$BACKDIR"
echo "Make backup dir $BACKDIR"
fi
cd "$BACKDIR"
rm .vimrc
rm .gvimrc
rm -rf .vim
cp '/cygdrive/c/Documents and Settings/Yuanjie/_vimrc' .vimrc
tr -d '\15\32' < '/cygdrive/c/Documents and Settings/Yuanjie/_gvimrc' | sed 's/set guifont=\([^:]*\):h\([0-9]*\)/set guifont=\1\\ \2/' >.gvimrc
cp -r '/cygdrive/c/Documents and Settings/Yuanjie/vimfiles' .vim
cd $CURDIR

After files are moved to the synchronize folder, I init a git repo and push it to a host. The synchronize script on the Linux side goes like this:

#!/bin/bash
CURDIR=`pwd`
EMOUNT=0
BACKDIR="/media/e/vim-setting"
if [ ! -d "$BACKDIR" ] ; then
echo "trying to mount drive e ..."
pmount /dev/sda9
if [ "$?"=0 ] ; then
echo "drive e mounted"
EMOUNT=1
else
echo "drive e cannot be mounted"
exit
fi
fi
cd $HOME
tar cjf .vim-setting.$(date +%F).tar.bz2 .vim/ .vimrc .gvimrc
rm -rf .vim/ .vimrc .gvimrc
cp -r $BACKDIR/.vim $HOME
cp $BACKDIR/.vimrc $HOME
cp $BACKDIR/.gvimrc $HOME
if [ "$EMOUNT"=1 ] ; then
pumount /dev/sda9
if [ "$?"=0 ] ; then
echo "drive e unmounted"
else
echo "drive e cannot be unmounted"
exit
fi
fi
cd $CURDIR

The scirpt above tar the original setting files and copy the new settings from the synchronize dir. Since the drive is not always mount, a dir test is used and drive will be mounted with pmount. The synchonize scirpt on Windows is similar but simpler than this, but guifont setting in gvimrc will have to be changed back like this:

sed 's/set guifont=\([A-Za-z\\ ]*\)\\ \([0-9]*\)/set guifont=\1:h\2/'

Notice that you may have to modify this in case there's number in your font name...

Thursday, May 14, 2009

EE in side?

Well I promised that I will blog about technique-related stuffs in English, but that is NOT always easy, especially when I want to talk about something not exactly technical… So let me try ;)

Today I helped Emma understand the confusing chip manual from TI, which is actually quite ordinary for a guy who was majored in EE – like me ;) Computers, cellphones, PDA’s and all lovely electronic gadgets are just chips connected on PCB in my eyes, and all programs to me are just bit streams flowing on the binary circuit… Parallelism has never be something hard for me since all the parts on a circuit should be working simultaneously (though some of the chips can be disabled to save energy or for some other reason…)

I do find some differences between me and those ‘genuine CS’ guys XD – What occurs to me when I meet difficulty in my experiments is to build a ‘lab-made’ hardware environments of my own instead of buying from some vendor; CS people say that this CPU features loading 8 instructions a time, while I say this CPU could work better with block-reading storage devices since reading multiple continuously stored instructions at a time is more efficient...

Currently I’m working on GCC and studying machine learning and data mining techniques, which are all interesting, but sometimes I do miss those days with FPGA and various chips and I’m still dreaming of building my CPU (perhaps with FPGA) which implements my instruction set, designing a circuit to support it, and finally writing a kernel, a compiler and several applications to form a minimal environment in my programming language for it. I don't believe this will be of any academic significance... but it really sounds INTERESTING~

Sunday, May 10, 2009

Converting between WAV and MP3 with simple open source tools

Abstract

A short memo on converting between mp3 and wav files

Today I find some conf call audio records are so big for my little hard disk so I decided to convert them from WAV to MP3 format. I make use of a simple (more precisely, simple to use) tools to do the job -- lame.

Since all my wav files to convert sit in the same folder, I make use of a little bash:

$ for f in `ls -1 *.wav | sed s/\.wav$//` ; do lame $f.wav $f.mp3 ; rm -v $f.wav ; done

Easy, isn't it?

Converting mp3 files back is just as easy, though there should be some quarlity loss which I don't believe can be noticed for a conf call record;) The mpg123 is as simple as its name 123, you just have to give it the name of the file to convert

$ for f in `ls -1 *.mp3 | sed s/\.mp3$//` ; do mpg123 -w $f.wav $f.mp3 ; done

Tux Drawing

I'm not sure whether is a good idea to put it here, anyway, I think Tux is technique-related...

Done in OpenCavas(Maybe I should use Linux and GIMP to do this, but I really don't like GIMP)... This took me several minutes after a pencil sketch on real paper.

Saturday, May 9, 2009

How to use git-http-fetch with a proxy

Abstract

This post offers a tip on how to use git with a http proxy

I noticed a mail in the Chinese Google Summer of Code mail list today, which askes how to use git-http-fetch when a http proxy have to be used to access the git host. I don't believe there is any way to do so with GIT, but I don't think this should be a tough problem either. Remember git is only a common program, though it does many magic>. So it should work with http proxy just as other programs do.

Remember how we access the internet with a proxy in bash? All we have to do is to set the http_proxy environment variable.In bash we simply do:

export http_proxy=”http://<proxy-server-ip>:<port>”

Or you may:

http_proxy=”http://<proxy-server-ip>:<port>” git http-fetch <your-params>

And be aware that the name of the environment variable is in lower case.

Wednesday, May 6, 2009

How to process large text file efficiently in Python

Abstract

Three different ways of processing text file line by line are given in the order of increasing efficiency.

I have to handle a large text file of space-separated data in python, and the data goes like this:

tag1 tag2 tag3
12 34 12
123 345 12

the first line is tags for each column, and the rest lines hold data. Since the tags are fixed, I can code it directly in to my script, that is to say the first line should be skipped. My first script goes like this:

file = open('foo.txt', 'r')
for line in file.readlines()[1:]:
#do something

This script requires a vast amount of RAM, since it has to store a list of all lines! So it is wise to use iterator:

file = open('foo.txt', 'r')
first = True
for line in file:
if first:
first = False
else:
#do something

The second script works much better than the first one, because the lines are read one by one from the file by using a iterator. However, the first flag is not a neat way to skip the first line for we have to test the flag many times, which makes no sense. And the problem is solved in the third script:

file = open('foo.txt', 'r')
file.readline()
for line in file:
#do something

the 'fileread.line()' command will perfectly move the file position one line forward, and the iter will then start from the second line:)

Monday, May 4, 2009

How many lines does it take to learn a new language?

It seems to be impossible for me to learn a new programing language by reading books without practice, so the question in the title occurs to me – how many lines does it take to learn a language?

The number obviously depends on many factors: whether it is the first programming language to me, if I have learned a language with similar grammar/design before, the size of the standard library… and a lot!

And it’s even harder to define when I should say I have learned a programming language… Having been programming in C for years, I still check the standard library now and then :(

Maybe I should define a metric of the familiarity of a programming language, ask all my friends in this trade to help me collect data, and take some statistical approach to obtain a equation of lines needed to practice a language... Hmm, a lot work to answer this simple question, isn't it XD

a python script to generate cue file

Abstract

A python script to generate cue file from file names in a same folder is given.

Sometimes we have lessless music (ape, flac…) split in to different files, and a cue file may help orgnize them in some music jukebox application, i.e foobar2000. so I create a really silly script to help me…

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys
import glob
from string import Template
from optparse import OptionParser

cue_head='''REM GENRE $GENRE
REM DATE $DATE
REM DISCID $DISCID
PERFORMER "$PERFORMER"
TITLE "$ALBUMTITLE"'''

cue_file='''
FILE "$FILE" WAVE
TRACK $TRACK AUDIO
TITLE "$TRACKTITLE"
INDEX 01 00:00:00'''

def cue_gen():
"""
generate cue file
"""
parser = OptionParser()
parser.add_option('-d', dest='dir',
type='string')
parser.add_option('-s', dest='sfx',
type='string', default='ape')
options, args = parser.parse_args()

if options.dir is None:
directory = os.getcwd()
else:
directory = options.dir
suffix = options.sfx
print directory, suffix

files = glob.glob(os.path.join(directory, '*.'+suffix))
files = [ff[1] for ff in [os.path.split(f) for f in files]]
files.sort()
for file in files:
print file
begin = int(raw_input('Title offset? '))

genre = raw_input('Genre? ')
date = raw_input('Date? ')
discId = raw_input('DiscId? ')
performer = raw_input('Performer? ')
albumTitle = raw_input('Title? ')
d1 = dict(GENRE = genre,
DATE = date,
DISCID = discId,
PERFORMER = performer,
ALBUMTITLE = albumTitle)
head = Template(cue_head).substitute(d1)
cuepath = os.path.join(directory, (albumTitle+'.cue'))

f = open(cuepath, 'w')
f.write(head)

track = 0
for file in files:
trackTitle = file[begin:-(len(suffix)+1)]
track = track + 1
d2 = dict(FILE = file,
TRACK = track,
TRACKTITLE = trackTitle)
body = Template(cue_file).substitute(d2)
f.write(body)

f.close()

if __name__ == '__main__':
cue_gen()

I know it’s quite rough and a lot of enhancement can be made… maybe it should search for the right file extension automatically or even search the genre for the internet, and that’s just why I put it here, hopefully some of you would help to improve it XD

Sunday, May 3, 2009

Migrate to a blog is just like move to a new house...

It's tiring, but offers a great opportunity to review my stuffs - what I have been writing about, what kind of blog I hope to have, whether my posts are well tagged...

And when I was trying to make a list of all programing/makeup languages I've used, I found some of them get so rusty that I cannot even remember how to define a function in it...Hmm it doesn't sound good :(

Fresh New Blog for Tech blogging =)

Hi all,

Finally I've decided to write a new blog for my technological posts, and all posts related to technology (software, Linux system, web technology...) will be migrate here in a short period of time =)