Python: Import stuffs from parent directory

Categories: blog



Import modules from parent directory.

 

import sys
sys.path.append('..')
import module_in_parent_directory

Elapsed time

Categories: blog

Added a function to my personal helper, mka_helper.php which I used for displaying elapsed time for notes that have been submitted to a particular item.

Initially I tried to search the internet, then I realized this kind of thing is really common, so it must be in the cookbook. So there it is, inside my PHP Cookbook, 2nd Edition (August 2006), Recipe 3.5 Finding the Difference of Two Dates (Elapsed Time).

Same goes for other stuffs, instead of googling, I usually start searching through the cookbooks first (for python, Dive into Python by Mark Pilgrim or the recipes at ActiveState website).

function elapsed_time($past_time, $current_time=0)
{
    if(!$current_time)
    {
        $current_time = strtotime('now');
    }

    $diff_seconds  = $current_time - $past_time;
    $diff_weeks    = floor($diff_seconds/604800);
    $diff_seconds -= $diff_weeks   * 604800;
    $diff_days     = floor($diff_seconds/86400);
    $diff_seconds -= $diff_days    * 86400;
    $diff_hours    = floor($diff_seconds/3600);
    $diff_seconds -= $diff_hours   * 3600;
    $diff_minutes  = floor($diff_seconds/60);
    $diff_seconds -= $diff_minutes * 60;

    if($diff_hours)
    {
        $elapsed_time_str = "$diff_hours hrs ago.";
    }
    elseif($diff_minutes)
    {
        $elapsed_time_str = "$diff_minutes mins ago.";
    }
    else
    {
        $elapsed_time_str = "$diff_seconds secs ago.";
    }
    return $elapsed_time_str;
}

Let our computers do the heavy lifting..

Categories: blog

Here’s the problem.

Go through the list of Numbers from number_list.txt

Search for username using the Numbers from userlist.csv, username in 3rd column (this file doesn’t have a password)

Search for otherdetails using the username from accountlist.csv, otherdetails in 3rd column

Combine the No., Username and Otherdetails to one file.

Previously someone tried to match the Number from the number_list to the username through the web interface one by one and match it to the accountlist file. Manually!

And complains that there’s like 400 records to be checked. *sigh*. Why can’t a programmer think like a programmer and let our tools do the heavy lifting? I’ve done this countless times and involved hundreds of thousand of records (haven’t reach the million/billion threshold yet).

After requesting the userlist from our resident .NET guardian, I proceeded with the simple script (even though its simple, I’m quite rusty with Python). It took me 60 minutes to develop it after fumbling through scriptutil.py and another 20 more minutes to debug some stuff. Another text processing problem solved in python.

"""
Go through the list of Numbers from number_list.txt
  Search for username using the Numbers from userlist.csv, username in 3rd column (this file doesn't have a otherdetails)
	Search for otherdetails using the username from accountlist.csv, otherdetails in 3rd column
		Combine the No., Username and Otherdetails to one file.
"""
import scriptutil as SU
import re

output_file = open('result.csv', 'w')
output_file = open('result.csv', 'a')
adm_list = open('number_list.txt', 'r')
lines = 0
for line in adm_list:
	flist = SU.ffindgrep('.', shellglobs=('userlist.txt', ), regexl=(line.strip(), ))
	data = flist.values()[0]
	datalist = data.rsplit(',')
	username = datalist[2]

	flist = SU.ffindgrep('.', shellglobs=('accountlist.csv',), regexl=(username.strip(),))
	data = flist.values()[0]
	datalist = data.rsplit(',')
	otherdetails = datalist[2]
	# remove duplicates
	otherdetails = otherdetails.split('\n')[0]

	tmp_str = "%s,%s,%s\n" % (line.strip(),username.strip(),otherdetails.strip())
	output_file.write(tmp_str)
	lines = lines + 1
print 'Lines: ', lines

The Wiki

Categories: blog
Hope!

I have been advocating the use of wiki in our department for quite some time. Whatever the problem, I request it to be inserted into the wiki (if its plausible) to make sure that we learn something from it and minimize whatever "History repeats itself" kind of disaster and learn something collaboratively in the process.

Sadly, to no avail.

But then, there is hope! An authorative figure in the department have started using it. Yes! Other than going through the corridors of power myself (aka climbing the food chain) and make my voice heard (which at the time I would entitle myself as The Wiki Overlord), influencing an authorative figure is the best and quickest way.

There is still hope!

Playing nice with jQuery

Categories: blog

While testing a few scripts out there for file upload. I tried out fancyupload which uses Mootools. Few years back I was an avid user of Mootools, then I needed other effects which requires Scriptaculous and I can’t seem to make them work together.

So unsatisfied with the result, I search for other frameworks. YUI is too big, and doing simple stuffs is such a hassle. Then I found jQuery. Look for the effects that I needed and tried it out and hooked ever since.

Back to my original problem. What I want to say is, jQuery have the ability to work with other libraries. I don’t know how long its been in jQuery but thats nice. So anyway I tried to use it with fancyupload and it still doesn’t work. Fedup with this I’m gonna go with SWFUpload.


October’s Cloud

Categories: blog

Its this time of day again. Thanks to google calendar. I get to keep track of it. Time for my October’s Tag Cloud on Delicious!

October's Cloud

Any emerging patterns? I can’t see one right now. Maybe its too early.

Stolen

Categories: blog

My bike got stolen. :(

On Saturday (18 October 2008) or maybe Friday.

Now I have to go around with busses and walking and carrying Python Pocket Reference in case I get bored while waiting for bus or for long walks.

The reason I’m carrying the pocket reference is that it fits into my pocket unlike stuffs that I like, PC Gamer which is a hassle to carry around unless I wear my jacket.

CSS Framework

Categories: blog

There’s been quite a lot of noise coming out from the post by Jeff Croft about CSS Frameworks and its follow-up post to clarify the original post. I’ve been using Blueprint for quite some time, a year or so and I’ve been using it for all of my projects.

Things that blueprint can’t do doesn’t bother me as I can do it myself.

Life as a wiki admin

Categories: blog, rants

Well, today really sucks. I was pissed off by a colleague giving sarcastic remarks when I was trying to do what I think is something good for everyone, by politely requesting them to put information into our department’s wiki. Thus ruining the whole day.

I really don’t know why I bother with the wiki. Nobody is giving any cooperation.

Everyone is keen on using word of mouth and take notes on their own. This is killing my enthusiasm and my spirit. Very depressing. I guess I’ll try confront this colleague and give a piece of my mind tomorrow.

I’m gonna set my expectations of everyone to none. Just face it. Nobody is going to update the wiki with info (except the librarian). Nobody is using it as reference (except for me). *sigh*

Research and planning for scaling..

Categories: blog

Argh! Somehow after the time I took to write this post it got lost and was not saved by wordpress. Now I can’t remember what I typed but I’ll try anyway. So here goes whats left in my short term memory…

I’ve been doing lots of research on MySQL and application clustering, load balancing, web application testing and other kind of stuff to make the company’s application scale and meet future demands.

There’s no blueprint in making an application scale but there’s lots of guidelines to make it work. Posts from HighScalability.com lets you see the stories behind few of the biggest web applications on the web today and how they are doing it. Centalized? Distributed? These decisions are made with the network team (since they know the network better than us developers). Use real options in strategic decision making.

So anyway, to make a long story short, I voiced my opinion on centralized system with support of CDNs. It is much more cost effective than opening up regional centres and doing synchronization between regional system (which I think would be a nightmare). They decided to go with the CDN (I’m so happy they listened to me :D ) and now I have to plan on making this all work!

Lots of planning and less coding for me nowadays. Hopefully I’d be able to comeback up to speed in developing applications and posts more of my code snippets.

GimpStyle Theme design by Horacio Bella.