Archive for the ‘Programming’ Category

What’s the difference between sys.exit() and exit()?

Friday, August 19th, 2011

I was trying to create a Windows binary using py2exe. When I run the exe file (it is a command line program) without any argument, it should print a usage message and exit. To my great surprise I saw this:

>> NameError: name ‘exit’ is not defined

Very weird. So I changed that line from exit() to sys.exit() and recomplied using py2exe, everything works perfectly now.

So what is the difference between sys.exit() and exit()?

(more…)

Migrating SVN repository data

Thursday, July 14th, 2011

Migrating the data is very simple, you can do this via svnsync but here it how to use a svn dump file:

* All the repos should be addressed using a path. e.g. /srv/svn/repo

Two liner:
$ svnadmin create newrepo
$ svnadmin dump oldrepo | svnadmin load newrepo

Or you can keep a dump file

  1. Dump your old repository to a file:
    $ svnadmin dump oldrepo > oldrepo.svndump
  2. Create your new repository:
    $ svnadmin create newrepo
  3. Load & rock:
    $ svnadmin load newrepo < oldrepo.svndump

You can have more fun using this method, such as specify a revision range or merge different repositories. See the SVN book for yourself :)

Summerised from the SVN book: http://svnbook.red-bean.com/en/1.5/svn.reposadmin.maint.html#svn.reposadmin.maint.migrate

Convert Trac DB, from SQLite to MySQL

Thursday, July 14th, 2011

I got bored at work so I decided to attempt some random stuff, like migrating Trac :)

By default Trac will use SQLite as its database but you may have better reason of using MySQL instead.

Naively I just dumped the SQLite database to an SQL file, and tried to import it to MySQL => FAIL!

Why? Because SQL dumps are vender specific databases can use different SQL dialects.

So this is not a straight forward process, you can check the differnces between SQLlite’s dump file and MySQL’s in a discussion somewhere on Stackoverflow. But i will just provide a convertor to help :) [Summerised and modified from: http://trac.edgewall.org/wiki/SqLiteToMySql]

* Please note this process was tested on Ubuntu Server 11.04 only. It should work for most *nixes, however you might need to find your our ways around Windows.

(more…)

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Wednesday, June 22nd, 2011

Ah, I choked again on Python 2.x’s unicode with my Pymote project. It used Javascript and Python utilising XML-RPC and couple of other 3rd party APIs. As you can imagine I need to keep all of the components happy with Unicode (so it can support Chinese!), and that is not an easy task.

A good (refresher) article is recommended for any one of you who write any code:

(more…)

Enable Remote Access for MySQL Database

Tuesday, February 22nd, 2011

By default remote access to MySQL database server is disabled for security reasons. However, some time you need to provide remote access to database server from home or a web server.

This can be done very easily, I did it on a CentOS, here is the how-to:

Log into the database server, enter the MySQL shell and select the database you want.

shell> mysql -u root -p

mysql> use database_name;

Then use this SQL command:

mysql> GRANT ALL PRIVILEGES ON *.* TO ‘root’@'%’;

Done!

(more…)

C Header Files Include Syntax

Tuesday, November 30th, 2010

It is very basic but this tutorial has a very clear explanation of the syntax:

#include <file>
This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the `-I’ option (see section 12. Invocation).

#include "file
This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the same directories used for <file>.

FYI: The “-I” opption is used as:

$ gcc –I path/to/header code.cpp –o output

How to create Windows exe from a python script

Thursday, August 26th, 2010

Nice tutorial here, very simple: http://logix4u.net/Python/Tutorials/How_to_create_Windows_executable_exe_from_Python_script.html

I packaged my “get my screen back” script (2KB), and it have grown to 5MB!!! :S

Removing list elements in a loop

Tuesday, June 29th, 2010

Have you tried this? Removing elements from a list, one each time, in a loop:

>>> ll=['1', '2', '3', '4', '1', '2', '3', '4']
>>> for l in ll:
…     ll.remove(l)
…     print ll
…
['2', '3', '4', '1', '2', '3', '4']
['2', '4', '1', '2', '3', '4']
['2', '4', '2', '3', '4']
['2', '4', '2', '4']
>>> ll
['2', '4', '2', '4']

You would expect to see something different? :D
Think about the process:
Loop0: ll=['1', '2', '3', '4', '1', '2', '3', '4'], loop index: 0 (’1′ is removed)
Loop1: ll=['2', '3', '4', '1', '2', '3', '4'], loop index: 1 (’3′ is removed)
Loop2: ll=['2', '4', '1', '2', '3', '4'], loop index: 2 (’1′ is removed)

Understood? lol, I encountered this today and found it interesting, this is a very common source of confusion.

So what is the correct way of doing this?
(more…)

Cafebabe

Thursday, April 15th, 2010

今天randomly看Wikipedia发现个好玩的…

Magic Number: constant used to identify a file format or protocol in the hacker’s language

Java Bytecode的Magic Number就是上面的: CAFEBABE

lol,我打开了个.class file,HEX里面果然是以cafebabe开始的… why cafebabe?

好吧,如果你想学习一下的话把这个也看了把,是讲Java Class file的结构的: http://viralpatel.net/blogs/2009/01/tutorial-java-class-file-format-revealed.html

(more…)

解决了某Java在Mac下不能运行的问题!

Sunday, January 10th, 2010

太帅了,我总算把Candy的project在Mac上 run 起来了。。。

一开始Candy在她的Mac上面怎么都run不了她project的code demo,后来我试了一下,也不行。。我可是完全follow instruction的e。。。因为试NullPointer我就怀疑试path的问题,因为那个demo要take一个directory path作为args。。。但无论我用各种valid的path都不行,不管试relative的还是absolute的path,通通无效。。

后来我把那个demo拿的我windows上面run,,啥问题都没有。。。为什么就Mac有问题呢?

奇怪了吧?在linux下面似乎也ok的e?

(more…)