Wednesday, March 19, 2008

collecting dmidecode information

dmidecode is very useful to look into system bios information. However, dmidecode -s requires long parameter. Since I prefer dump all information on one screen, I wrote this simple tool.

import os, sys, commands, re
for p in commands.getoutput('dmidecode -s').split('\n'):
  if'^ ',p):
    print '%-25s: %s' % (p.strip(), \
        commands.getoutput('dmidecode -s %s' % p) )

Use this with 'sudo'. Especially this is useful for 'DELL service TAG' information. Service tag text can be copy and pasted. :)

Anyone wants to challenge stuffing this into one line? One line bash with pipes are welcome, too.

Thursday, March 13, 2008

Calculating Weekdays in Python

Calculating weekdays is simple for human being. But, it isn't for a computer. The task requires that the logic understands calendar due to adding/subtracting dates. Doomsday algorithm is needed to get which weekday for a given day.
Python has two time packages. time and datetime. time package works like standard C library package except its basic representation is so called time-tuple rather than epoch. ( it knows how to convert to epoch ) datetime provides 1) higher level interface, 2) capability to manipulate dates, and 3) compatibility to time package. So, working with datetime is the most cases.
Hey, let's just see the code.

from datetime import timedelta, date
def weekdays(givenDate):
  start = givenDate if givenDate else
  start = start - timedelta(start.weekday())
  end = start + timedelta(days=5)
  return start, end

Pretty simple. Now, we can use some decorator to make it useful. For example, to provide postgresql where clause, we can write like this.

def pgsqlRangeWks(givenDate=None):
  fmt = '%Y-%m-%d %H:%M'
  start, end = weekdays(givenDate)
  return "ts > '%s' and ts < '%s'" % \
    ( start.strftime(fmt), end.strftime(fmt))

Again, python is well balanced language in performance and robustness.

Tuesday, March 11, 2008

eject command to close CD tray

I happen to find 'eject' command today, and I found that I can close CD tray without bending my back to reach to CD. :)

eject [device] will open,
eject -t [device] will close.

man eject. This is interesting.

Wednesday, March 5, 2008

FreeBSD 7.0

After one line qsort() craziness, I still didn't get back to sleep. I read about FreeBSD 7.0. FreeBSD has been proud of making better codes in the kernel than Linux evolves. They heavily used asynchronous IO before Linux introduced asynch layer. epoll() in Linux still needs more work. This time, FreeBSD proved their superiority once again. Linearly scalable SMP. According to their history, it took seven years to complete the solution. They staged into sub-solution for each releases (5.x, 6.x), and the war was over at 7.0. When I looked at the graph, it was definitely attractive.
They added more stable wireless support. But, my stupid BCM943xx card is not supported. Due to lack of my wireless support, I lost interest about FBSD 7. I am not good at BSD system, anyway. But, I swear that I wouldn't buy Dell laptop anymore. If I am buying a Dell once again ( I doubt this may happen ), the wireless should be Intel. PERIOD!

1 line qsort() in python

quick sort is very simple, but generally fast algorithm. If it is implemented in python, seeing the simplicity is obvious. Thanks to ease of boundary check, code will look like this:

def qsort(lst):
    if len(lst) <= 1: return lst
    left = [ e for e in lst[1:] if e <= lst[0] ]
    right = [ e for e in lst[1:] if e > lst[0] ]
    return qsort(left) + [lst[0]] + qsort(right)

Now, if evil lambda comes into play, we express this in one line.

qs = lambda lst: qs([ e for e in lst[1:] if e<=lst[0] ]) + \
    [lst[0]] + qs([ e for e in lst[1:] if e>lst[0]]) \
    if len(lst) > 1 else lst

hehe.. Okay, I admit that it is not simple one line. I am insisting this to be one line.
I appologize for trolling :) Maybe I am going wierd at 3:30 in the morning.