Profile

Click to view full profile
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time. Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more...

Saturday, December 11, 2010

How to check subclass in python

An object oriented in python is different from full-support object oriented like java or etc. To check subclass, we need to use built-in function from python called "issubclass"

Ok, let's begin with issubclass(class, classinfo).
class A(object):
 pass

class B(A):
 pass

a = A()
b = B()

print issubclass(a.__class__,A)
print issubclass(b.__class__,B) 

It is easy right? ^^

Wednesday, December 1, 2010

How to get current Date/Time in Python

In my project, sometimes I have to used current date to compare. So, we need to get date from the system.
import datetime
dt = datetime.datetime.now()
print dt.year
print dt.month
print dt.day
print dt.hour
print dt.minute
print dt.second
Very easy right ^^

Divinity 2: Statue Puzzle at Keara’s Headquarters

Divinity 2 is an interesting game for me in the story and game play. While I playing this game, I feel I'm in the game. The broad story is very simple. It's the story between dragon knight and dragon slayer. And once our character will be a dragon slayer who hunt the dragons but ,by chance, our character will be a dragon knight. And both sides become our opponents.

So far, I walk to Keara's Headquaters, and face with the puzzle problems in the games. I'm interested in this puzzle about how the creator think this puzzle? for me, it's very hard to solve it.

There 5 five statues. We have to identify their names to open the last door in order to fight with Keara.
There are 2 types of statue. First one will be statue with the fire blades, another one will be statue with the blades.
I will give 5 statues into the number of 1, 2, 3, 4 and 5  for easy understanding.
There are 5 five names: Mayhem, Chaos, Devastation, Havoc, Waste.

The first room, (1) stay alone and it's a fire blades type.
The second room, (2), (3) stay together. (2) is a blades and (3) is a fire blades.
The third room, (4), (5) stay together. (4) is a fire blades and (5) is a blades.

Each statue will give us clues.
(1) hints: Mayhem is not in the same room as Havoc. Devastation is in
the same room as Chaos.

(2) hints: I am either Devastation or Chaos. Waste is in the same room
as Mayhem.

(3) hints: Mayhem is alone in his room. Havoc is in this room.

(4) hints: I am neither Chaos, nor Havoc. Chaos is neither in my room,
nor in Waste's.

(5) hints: I am in the same room as Waste. I am neither Mayhem, nor
Waste.

We have to identify each statue name with those hints. At last, I can pass but take a long time - -".

Hints: Some sentences are lying, we have to change them into opposite way.
Answer: (1) is Devastation, (2) is Waste, (3) is Chaos, (4) is Havoc, and (5) is Mayhem.
(Drag over to see hints and answer.)

Hope that it can help ^^

Sunday, November 21, 2010

How to run parallel programs(MPI) in Windows

Long time ago, I had tried HPC in Windows for running MPI. Fortunately, I just found it in my old tweet --".
So, if you want to try go to this link: http://blogs.msdn.com/b/risman/archive/2009/01/04/ms-mpi-with-visual-studio-2008.aspx

Saturday, November 20, 2010

How to set an environment for running parallel programs(MPI) with MPICH in Linux

One of my class in this semester is Parallel Programming. Normally, I have to use cluster computer in the faculty by using remote log in, but, I don't like when I have to modify codes or create new codes, I have to upload it to the server. It's not convenience for me. So, I just find the solution to run parallel code (MPI) in one single machine that can work as the cluster with one machine.

The solution is we can use mpich2 package. The step to install is here.

Requirement:
You have to use Linux (I try with Ubuntu 10.10 and Mint 9). You may run this os through a Virtual Machine.

Step:
1. If your machine doesn't install gcc then type : "sudo apt-get install gcc"
2. Then you install mpich2 by using this command : "sudo apt-get install mpich2"
3. After you install come to your home folder e.g. /home/[your username] by : "cd $HOME"
4. Then you go to that directory and create the file name ".mpd.conf" and the content is "MPD_SECRETWORD=mr45-j9z"
5. type "chmod 600 .mpd.conf"
6. The directory that want to run code have to create the file name "mpd.hosts" which content is "localhost"
7. Finish configuration.

Test:
1. You have to boot mpd by "mpdboot -n 1"
2. Your machine Must be connected to the access point.
3. Compile program by "mpicc -o x x.c" ; where x is your code file name .
4. Run the code by "mpirun -np y ./x" ; where y is process number and x is object code name.

Now, You can run MPI code in your machine.
More Information: https://help.ubuntu.com/community/MpichCluster

Saturday, November 13, 2010

Barcamp Bangkok #4 2010 - My experience (Part2)

Last time, I talked about sections that I have no ideas and knowledge. But, in this part, I'm interested in topics then went to those rooms.

15:00 - 15:30 - Room 1001 - Seeking Hyper Productivity by @juacompy
The speaker come from Proteus Technologies. He is an employee in international company. In this section, he said that his company can do hyper productivity. In my opinion (conclusion), separate the factors that effect to the productivity into two sections: Project management and member personality. Project management includes group flow, how to manage works should be divided in functions not divide by tasks because if we divides work by tasks, for example, Jim gather requirements and John design database. How can John doing it? he know nothing about project requirements. It needs a lot of communication to succeed. So, he suggest to divide works by functions and then do work that related to own function. Also, which technologies we should use to achieve the project, he said he uses dynamic and appropriate languages to build. Doing from easy parts first and then follow by hard parts can increase your confident to make your productivity improve a lot. Second, Member personality, he said a project manager should understand his teammates very well, and also each member should know himself/herself very well to know what time they can have the highest productivity. Then manage themselves to do works in their prefer styles.

15:30 - 16:30 - Room 1002 - Agile Architecture by Ben Scherrey
In this topics,  I totally forgot about a story the speaker said on that day - -".
I know only he said he uses  test driven development and scrum to collaborate with his customer to reduce overhead. Also, has small cross function teams. For each team, one person know everything. Uses continuous integration entire processes. Write an automate testing to test the system.

16:30 - 17:30 - Room 1006 - All Mashed Up - HTML5 + JQuery + Google Code + YQL by Naoroll
I think the speaker come from Google Sigapore. He show how to connect HTML5 + JQuery + Google App Engine + YQL together. He also uses Google spreadsheet as database. Each sheet is similar to a table. Uses YQL to query everything from the internet like select * from internet for mesh up data which you can get it from Yahoo Developer Network. At the end, he gave a Google shirt if show him about our work. At that time, I forget about my Android game - -" to show him for a shirt. Again, he gave a Google sticker for each audience but, unfortunately, ran out of stickers in front of my eyes T_T

Next time, I will write about day 2.
Part 1: http://darkkung-blog.blogspot.com/2010/11/barcamp-bangkok-4-2010-my-experience.html
Part 3: http://darkkung-blog.blogspot.com/2011/02/barcamp-bangkok-4-2010-my-experience.html

Tuesday, November 9, 2010

Barcamp Bangkok #4 2010 - My experience (Part1)

In last month, Barcamp 4 was held on Oct 23rd and 24th at Sripatum University. In short, BarCamp is an international network of user-generated conferences — open, participatory workshop-events, whose content is provided by participants. More details at http://www.barcampbangkok.org/overview

The first day schedule is here: http://www.barcampbangkok.org/events/barcamp-4/2010-10-23
The second day schedule is here: http://www.barcampbangkok.org/events/barcamp-4/2010-10-24

My first day:
11:00 - 11:30 - Room 902 - Ruby Version Manager by Ake Koomsin
In this section, the speaker will talk about RVM to mangae version of ruby for pakage to install/uninstakk or use which version for each project. I think the speaker is great beacause he's not native but he can present in English and there are many foreign audiences who're interested in ruby too.

11:30 - 12:00 - Room 1003 - Introduction to GIT by Till
In this section, he talk about GIT which is versioning system to keep code in respository like SVN and manage code version. One downside of GIT is we have to use it with command line only(console).
Example Commands:
git add => to add a file into git server.
git commit -a => to update a file when we change something
git status => check status
git revert => reverse changes

12:30 - 13:00 - Room 905 - Why Eco Tourism sucks by Thomas Wahhoff
In short, he said eco tourism in some countries can't help the life of local people, and the quality and service are poor in rural areas especially foods. He said, in trips, he's very exhausted and can't conserve the nature.
I agree with him from what he said that a traveler want convenience, safe food, entertain, relax, and real experience. But there is no branding of eco tourism. So, it's suck.

14:30 - 15:00 - Room 1003 - How to bootstrap a company by Phil L
Because I'm not the person who want to create by own company (means that I've a little knowledge about it), so I can't understand some contents in this section. Basically, we have to create many interesting ideas first (10,000). Then, filter only good and practical ideas(20). Next, adjust or develop the remaining ideas(2). Finally, you will get the perfect idea(1).

p.s. It's not finish yet ^^, I will continue soon.
Part 2: http://darkkung-blog.blogspot.com/2010/11/barcamp-bangkok-4-2010-my-experience_13.html
Part 3: http://darkkung-blog.blogspot.com/2011/02/barcamp-bangkok-4-2010-my-experience.html

Saturday, November 6, 2010

How to list files in the directory in Python

Python can easily list files in the specific directory by using "dircache".

For example,

1. I have a folder "Test" under "C:\\", and this folder has 4 files contains 1.txt, 2.txt, 3.txt, 4.txt

2. Open the command at "C:\\" using "SHIFT + RIGHT CLICK"
more at => Windows: http://darkkung-blog.blogspot.com/2010/10/open-command-window-at-current-path-in.html
Linux-Ubuntu: http://darkkung-blog.blogspot.com/2010/10/quick-open-teminal-in-ubuntu-by-using.html

3. Type "python" to open Python Interactive Shell

4. Use the following code to see your files in that directory.

import dircache
flist = dircache.listdir("/Test/")
print flist

This technique can be used with "os.path" to check the contents in that directory which one is file or directory such as "os.path.isdir()", "os.path.isfile()".
more at => http://docs.python.org/library/os.path.html#os.path.islink


p.s. This technique is operating system independent.
Credit: http://www.programmersheaven.com/mb/python/191482/191482/list-files-in-a-directory/

Thursday, November 4, 2010

Add/Get current path to the system in Python

Some time, I may have to know the current path that I uses python interactive mode because I forget it = =", then may also to add it to the system path for something.
The solution is following:

1. Get the current path by using "os.getcwd()"
import os
os.getcwd()

2. Add the current path to the system by using "sys.path"
import sys
import os
# sys.path => will retrive the list of the system paths.
sys.path.add(os.getcwd())

Now, the current path will be added to the system path.

Monday, November 1, 2010

Differences between xrange() and range() functions in Python

Those 2 functions purpose is very similar when you use them in python, but the major different is range() function will generate the new list but xrange() is not. So, if your purpose doesn't use the advantage of the list to access you can just use xrange() function to make your code use less resources. For example, for-loop statement you can just use xrange() function to enumerate your loop.

for x in xrange(5):
 print x

Even you change xrange() function to range() function the result will be the same as:
0
1
2
3
4

But, As I already said, behind the scene range() function will generate it as a list that may make it slow when generate the big list for normal enumerate, xrange() function is enough.