Page 1 of 1

Geek Rave - Python programming language

Posted: March 21 08, 7:45 am
by jim
Over the years this has become my native tounge, replacing C and C++. It is so easy to work with, last night I had a database administrator come up to me at 4pm pulling her hair out becuase she upgraded a Postgres database from 8.0 to 8.2 and was having problems migrating the data. Apparenltly,8.2 only accepts valid utf-8 codes and there was an occasional funny character that it was choking on. The dump file was about 1/2 gig, and she had been banging her head against a wall trying to find the needles in a haystack.

At 4:30, I had this script running, and at 4:45 the data was scrubbed. Now granted, it's a quick hack, but that is what python is good for. I was even able to put a few flags to just get a count of records etc... But this little 30 minute job probably saved weeks of head banging. It simply replaces funny characters with an underscore.

I work on so many large projects that can take more than a year to complete that I love the occasional small job. Kind of like painting a room, you can leave work at the end of the day and feel like you really did something.

Code: Select all

#################################################
# python scrub.py -i <infile> -o<outfile> -v -c
#
#  -c just returns a count of errors with no processing
#  -v verbose.
#################################################

import getopt
import sys


def fix(line):
   fixed_line = ''
   for c in line:
      if ord(c) > 127:
         c = '_'
      fixed_line = fixed_line + c
   return fixed_line


verbose_flag = 0
justcount_flag = 0
infile = '/tmp/input.file'
outfile = '/tmp/output.file'

try:
  optlist,args = getopt.getopt(sys.argv[1:],"i:o:vc")
except getopt.error, error_msg:
  print error_msg
  print 'Usage: scrub.py -i<infile> -o<outfile> -v'
  sys.exit(1)

for opt in optlist:
   if opt[0] == '-i':
      infile = opt[1]
   elif opt[0] == '-o':
      outfile = opt[1]
   elif opt[0] == '-v':
      verbose_flag = 1
   elif opt[0] == '-c':

try:
   infd = open(infile)
except:
   print 'Error opening input file ' + infile
   sys.exit(1)

try:
   outfd = open(outfile,'w')
except:
   print 'Error opening output file ' + outfile
   sys.exit(1)

ctr = 0
for line in infd.readlines():
   try:
      unicode(line,'utf-8')
      outfd.write(line)
   except:
      if justcount_flag == 0:
         outfd.write(fix(line))
      ctr = ctr + 1

print "Found " + str(ctr) + " errors"

Re: Geek Rave - Python programming language

Posted: March 21 08, 7:51 am
by sighyoung
Tell me--when you use it, do you hear John Phillip Sousa's "Liberty Bell March" in the background?

[youtube=http://www.youtube.com/watch?v=oA0sgn5B ... re=related][/youtube]

Re: Geek Rave - Python programming language

Posted: March 21 08, 7:55 am
by jim
No, I don't hear that, but let's just say Viagra has nothing on this script. Better than watching something burn.

Re: Geek Rave - Python programming language

Posted: March 21 08, 2:04 pm
by Arthur Dent
Any project that doesn't involve working with other people's code is good. I always get stuck being annoyed about the way it is written instead of making the needed changes.

Re: Geek Rave - Python programming language

Posted: March 21 08, 2:15 pm
by jim
Arthur Dent wrote:Any project that doesn't involve working with other people's code is good. I always get stuck being annoyed about the way it is written instead of making the needed changes.
It is. It's like college.

What I love about python is it is good for the quick hack, but I also work on a project that uses all of the bells and whistles. A couple hundred thousand lines of code, 20 levels of object hiearchy, web based app using modpython and ZODB database...

In other words, you can prototype with it, sript with it, or really do something heavy duty. Granted it's not the most efficient in the world, so I don't suspect the Higgs will be discovered with Python code, but it's definitely nice.