Wednesday, September 21, 2011

Python multi-line strings

There are several ways to compose a Python string literal in multiple lines if the string is too long. One way is to use the add operator. For example:
str1 = 'this is a very long string.\n' +\
'second line of the string.'
In this first way, it is annoying that you need to type extra characters each line,
i.e. the plus operator and line continuation mark. Another way you can do this is to use the triple-quotes ''' or """:
str2 = '''this is a very long string.
second line of the string.'''
They all yield the same Python string. But this method can look very ugly because it can violate the indentation in the source code.

In Python if you just put two string literals together, with or without spaces in between, your result string will be the concatenated string of all the strings. For example:
str3 = 'this ' 'is ' 'a ' 'string.'
and value of str3 will be:
'this is a string.'
Thus, my preferred way of doing this is to use the implicit line continuation by parentheses, with the implicit string concatenation. For example:

str4 = ('this is a very long string.\n'
        'second line of the string.'
       )

This will give you the same result of the first two examples.

Nice, isn't it?

Update:

There was a PEP 3126 that suggests implicit string concatenation to be removed from Python 3, but it has been rejected. I tested this in Python 3 and it still works. But the document also suggests a good way to do this with the join function:
"\n".join(["this is a very long string.",
"second line of the string."])

Thursday, August 25, 2011

Meego and Arch Linux dual boot.

Recently I bought a "notbook" ThinkPad x120e (not arrived yet) which is much more powerful than my two year old netbook Toshiba NB201. So I decided to set up a home server (file server, torrent slave, ipv6 router...) using that Intel Atom based netbook. The idea is to keep me away from high electric bills and noise.

I decided to install MeeGo and Arch Linux on it. This is really strange combination (I know...). But the idea is that the latter is the home server OS, while the former as a minimal backup desktop environment, just in case anything happens and I need to have some easy internet.

MeeGo uses syslinux(extlinux) as its bootloader. It doesn't seem to support chainloading grub, also it cannot directly load a OS from another partition. So the easiest way to dual boot is to use grub in Arch Linux.

1. Install MeeGo (I am using netbook version 1.2). Follow the directions here.

When you are asked to partition your hard drive, You need at least have this following 3 partitions,
a. One ext3 partition mount as /boot, this will be used by Arch Linux later as well, I think 200MB is well enough.
b. One btrfs or ext3 partition mount as / for Meego.
c. A swap partition, of course.

After installed Meego, try boot into MeeGo and find out the name of the linux kernel file. To do this, boot in MeeGo, open a Terminal, type
cd /boot
then type
ls -l
There will be a file name starting with vmlinuz-, with a series of numbers after it (Mine is vmlinuz-2.6.37.2-8.27). This should be your linux kernel file for MeeGo. Write it on a paper for later use.

2. Install Arch Linux (I am using version 2011.8.19). Follow the instructions here.

When you are asked to prepare hard drive, do not use Auto-Prepare. Rather, use "Manually partition Hard Drives". Here you will create a new partition for Arch Linux (or more if needed). Then select "Write" to write the partition table.

Then you will need to manually configure mountpoints. Mount the following partition to Arch Linux,
a. The ext3 partition created in 1a. Also mount it to /boot. Do NOT format it!
b. Format the newly created partition as btrfs or ext3. mount it to / for Arch Linux.
c. The swap partition created in 1c.
Now, the installer will write the partition table.

Later, when you are asked to select bootloader, choose GRUB.

After installation of packages, the installer will ask you to install bootloader for the system. You will be asked to edit the configuration file. After the two Arch Linux items, add the following lines to the configuration file,
# (2) MeeGo
title MeeGo (2.6.37.2-8.27)
root   (hd0,0)
kernel /vmlinuz-2.6.37.2-8.27 root=/dev/sda2 ro quiet
Note that you should change line 3 to whatever partition your mountpoint /boot is on, which is created in step 1a. For example, if it is on /dev/sdb3 it should be root (hd1,2) (Note this one counts from 0).

For line 4, the root= part is the partition you created in step 1b. Thus it should be in a different partition.

Now save, and the installer will quit. Reboot and you will get fresh dual boot of MeeGo and Arch Linux, cheers!

Wednesday, March 2, 2011

The Great Wall?

科普一下,今天偶然看见,从中国在1987年9月14日发出的第一封电子邮件的内容是:“Across the Great Wall, we can reach every corner in the world”

我觉得现在应该改成:“Across the Great Fire Wall, we can not reach every corner in the world”吧。

还看到一个段子:

“今年奥斯卡最大的赢家《国王的演讲》和《社交网络》计划引入中国,名字分别叫《认真贯彻落实国家领导人的重要讲话》和《该页无法显示》。 ”

恩,就写这些。

Saturday, January 8, 2011

我的2010

照惯例每年都写一个年终总结之类的,这里简单总结一下我自己的2010吧。

学习:在北卡州立大学读计算机科学硕士第二个学期和第三个学期,成绩保持全A(有一个A-),可以说还算是让人满意的。并且选定了学校的教授St. Amant作为导师做硕士毕业论文。

工作:2月份参加了学校的春季Career Fair,也就是招聘会。在罗利市的一个创业公司Transloc Inc.找到了第一份带薪的实习工作,从5月份到8月份全职工作了整三个月,并且在秋季学期继续兼职工作。在工作中完成了两个重要的模块。9月底去西雅图参加微软的面试,最后拿到了Office产品Outlook组韩国老板的Offer,可是考虑到西雅图的自杀率和我从来都不用Outlook这个产品,拒掉了。

身体:选修了羽毛球和足球课,还蛮有意思的,现在羽毛球水平还是很一搬,而足球水平进步了一些。利用暑假时间长跑和用哑铃成功增长10lb的体重,提高了自己的体能储备。

感情:9月份以后就一直感到很幸福,不解释。

信仰:8月份归入基督,阿门。

游玩:巴尔的摩的海港情调,西雅图的都市喧闹,爷爷山的枫叶红了,弗吉尼亚的野营惊险,卡罗莱娜海滩的蓝天白云,以及回国以后的大吃大喝。