Archive for the ‘How To’ Category

How-To: Install KDE’s kate Text Editor in Fedora 15

The Fedora Project has moved the kate text editor (KDE’s version of the Windows application Notepad++ IMHO) to the kdesdk package. So if are like me and really need this application here is how to install it:

# yum install kdesdk

That’s it, now code away.

2010-11 NHL Roster by Birth Location

I was at lunch a few weeks ago at a sports bar with several co-workers and started discussing sports with one of our developers.   Our discussion led to the question about how the NHL consisted of players from so many different countries compared to other US based sports.  When I got back to work I did a couple of Google searches looking to see if someone already had that information for me.  But the search didn’t turn up any results (at least not for this year ).  But it did lead the the NHL.com website that had all the information about the players but it was displayed in a HTML table that span over 30 webpages.  This is where being a programmer pays off.

I decided to write a quick script to parse out this data and place it in an CSV file.  I was going to write it in Python but since I was in Windows I decided to to just use my favorite Windows scripting language, AutoIT.  Below you will find the complete script that I used to loop through the 3o webpages on NHL.com and grab the contents of the 2010-11 players bio table.

#include <Array.au3>
#include <IE.au3>
#include <File.au3>
 
$csvFile = "c:\temp\players.csv"
 
for $i = 1 to 30
 
	$ieObject = _IECreate ("http://www.nhl.com/ice/app?service=page&page=playerstats&fetchKey=20112ALLAASAll&viewName=bios&sort=player.birthCountryAbbrev&pg=" & $i)
 
	$table = _IETableGetCollection ($ieObject, 3)
	$aTableData = _IETableWriteToArray ($table, True)
 
	FileOpen($csvFile, 1);
	For $r = 1 to UBound($aTableData,1) - 1
 
		For $c = 0 to UBound($aTableData,2) - 1
			FileWrite ($csvFile, """" & StringStripWS($aTableData[$r][$c], 3) & """")
			if $c = 18 Then
				FileWrite ($csvFile, @CRLF)
			Else
				FileWrite ($csvFile, ",")
			EndIf
 
		Next
 
	Next
 
Next

Note: This doesn’t do any error checking.

I then imported the data to a MySQL database and ran couple of PHP scripts againts the data to get these results.

Here is the number of players in the NHL broken down by birth country.

Country the NHL players were born in.

AUT: 3 BLR: 3 BRA: 1
BRN: 1 CAN: 482 CHE: 3
CZE: 39 DEU: 10 DNK: 6
FIN: 22 FRA: 1 ITA: 1
JPN: 1 KAZ: 1 LTU: 1
LVA: 4 NOR: 2 POL: 1
RUS: 26 SVK: 12 SVN: 2
SWE: 54 UKR: 4 USA: 211

Another stat that I though would be interesting was to find out what state the US born NHL players came from. So here it is:
State that US born NHL player where born in.

AK: 6 CA: 8 CO: 3
CT: 8 DE: 1 FL: 1
IL: 10 IN: 2 MA: 17
MD: 1 MI: 34 MN: 41
MO: 3 NC: 2 ND: 3
NE: 1 NH: 3 NJ: 4
NY: 33 OH: 2 PA: 14
TX: 1 UT: 1 WA: 2
WI: 10

Arduino The Documentary

How To: Update Kubuntu 10.10 to KDE 4.5.3

Kubtuntu 10.10 is shipped with KDE 4.5.1, here is how to update to the latest release version 4.5.3 using the Kubuntu’s PPA.

Add the repository

sudo apt-add-repository ppa:kubuntu-ppa

Update the repositories

sudo apt-get update

Upgrade

sudo apt-get dist-upgrade
reboot

IMPORTANT: After my first reboot I was able to log in however once I entered my username and password I was then presented with a black screen. To fix this I hit ATL+CTRL+F2 to get to a bash shell. Once logged in type sudo apt-get install plasma-desktop then rebooted. To eliminate this issue before the first reboot you might be able to do the distro-update and then the apt-get install plasma-desktop then reboot. I am not able to test this theory since I have already updated my system.

Note: Doing just a regular apt-get upgrade will not get all of the necessary packages and looks like it want to remove a bunch of KDE packages.

Writing Traceroute in Python

Ksplice.com has put together a tutorial on how to write the traceroute/tracert application in Python in just 8 easy steps.  This is a good introduction on how the traceroute application works and socket programming in Python.

Here is the finished product but please read thier post to see all of the steps.

#!/usr/bin/python
 
    import socket
 
    def main(dest_name):
        dest_addr = socket.gethostbyname(dest_name)
        port = 33434
        max_hops = 30
        icmp = socket.getprotobyname('icmp')
        udp = socket.getprotobyname('udp')
        ttl = 1
        while True:
            recv_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
            send_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, udp)
            send_socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
            recv_socket.bind(("", port))
            send_socket.sendto("", (dest_name, port))
            curr_addr = None
            curr_name = None
            try:
                _, curr_addr = recv_socket.recvfrom(512)
                curr_addr = curr_addr[0]
                try:
                    curr_name = socket.gethostbyaddr(curr_addr)[0]
                except socket.error:
                    curr_name = curr_addr
            except socket.error:
                pass
            finally:
                send_socket.close()
                recv_socket.close()
 
            if curr_addr is not None:
                curr_host = "%s (%s)" % (curr_name, curr_addr)
            else:
                curr_host = "*"
            print "%d\t%s" % (ttl, curr_host)
 
            ttl += 1
            if curr_addr == dest_addr or ttl > max_hops:
                break
 
    if __name__ == "__main__":
        main('google.com')

View Oracle dbms_output with Python

After doing lots of searching on the Internet for how to get the dbms_output from an Oracle SQL query with Python I finally found something.

import cx_Oracle
 
conn = cx_Oracle.Connection("userid/password@tns")
curs = conn.cursor()
curs.callproc("dbms_output.enable")
 
sqlCode = """
some long
sql code 
with dbms_output
"""
 
curs.execute(sqlCode)
 
statusVar = curs.var(cx_Oracle.NUMBER)
lineVar = curs.var(cx_Oracle.STRING)
while True:
  curs.callproc("dbms_output.get_line", (lineVar, statusVar))
  if statusVar.getvalue() != 0:
    break
  print lineVar.getvalue()