Hi all,

So this week, I’ve been in a crunch to get some last minute cash for spring break. My Dad had a little opportunity for me to write a Python script for him to use at the office. The back story is basically that the warehouse where he works has to take inventory using some sort of hand-held scanning devices.

He wanted me to write a script that, when placed in a directory with any given number of output files from the devices, generates a document stating each file and how many cartons are in each pallet. Sounds like a perfect job for Python, right? Exactly right. It literally took me about 45 minutes to 1 hour and I had the thing completely off the ground running. I’ve decided to post it not so much because this exact program is really useful to anyone else, but because I definitely learned some cool I/O stuff while doing it.

First, I think it’s useful to get an idea of how the files looked coming in. They were basically Wordpad documents generated by Windows CE and didn’t have much format to them. Here’s a little example:

* This is a Microsoft WordPad document.
* For further details visit the Microsoft Windows CE web site, at
*         http://www.microsoft.com/windowsce
* Or search MSDN for 'Microsoft WordPad'
}\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
{\colortbl ;}
\viewkind4\uc1\pard\cf0\f0\fs22 008022376\par

As you can see, It’s a little hard to decipher. Also, keep in mind that this is not a full file as I didn’t want to post 600 lines in there. Anywhere there is a ‘\par’, except for the lone one at the end, there is a carton in the warehouse. All I had to do was figure out a way to add.

My program gave the following output:

Inventory Count
Generated: 03/09/2010 21:45:37
Filename           # of Cartons
m468                602
m466                286
m461                144
m465                245
m464                199

So, as you can see, it seems to work pretty well. I can also imagine this is WAY easier than counting by hand. So, here is the code that converts one to the other. Be sure to notice the usefulness of the calls like getcwd(), which proved to be awesome for finding all the files in the directory.

#Spencer Smith
#Inventory Calculator
#Created 3/9/2010
import sys, os
from time import strftime
#Name of output file
filename2 = "inventory_count.txt"
#Get all files in current directory
total_files = os.listdir(os.getcwd())
pwd_files = list()
#Find all files with '.pwd' extension
for x in total_files:
   if x.find(".pwd") != -1:
#Return error to 'error.log' if there are no '.pwd' files in the directory
if len(pwd_files) == 0:
  FILE = open("error.log","w")
  FILE.write(strftime("%m/%d/%Y %H:%M:%S") + " -- No files '.pwd' files in directory to read.")
#Open output file for writing
OUT = open(filename2,"w")
#Create header
OUT.write("Inventory Count\n")
OUT.write("Generated: "+strftime("%m/%d/%Y %H:%M:%S")+"\n")
OUT.write("Filename           # of Cartons\n")
#Iterate through available '.pwd' files
for name in pwd_files:
#Open file for reading
   FILE = open(name,"r")
   counter = 0
   suffix = "\par"
#Search each line for "\par". "\par" on the line means that there is a carton on this line.
#Increment carton counter if found
   for line in FILE:
      if line.find(suffix) != -1:
        counter = counter + 1
#After reading all lines in file, write filename and count to output file.
   OUT.write("%s                %d\n" % (name[0:-4],counter-1))
#Close current file and reset.
#Close output file so that it can be read.

Enjoy! Hope someone finds it useful!

I am in a Distributed Computing class this semester. We just had to write a brute-force password cracker using the Condor grid we have on-campus. It really took forever to generate an 8 character (a-z) code even though I ran it 676 different times. Total run-time was about 25 minutes. Anyways, we also had to create a password cracker using a dictionary file. I thought this was an awesome project and it was so much faster! I’ve decided to post the dictionary Python script. It should crack any word, given that it is in whatever dictionary you choose to use it against. My word for the project was “schnecke”, which is German for “snail”. Knowing that, I checked it against the Ubuntu New German dictionary. The run-time was ~.3 secs.

Check it out!

P.S. In order to get language dictionaries to check against in Ubuntu, it is as easy as “apt-get install ngerman” or whatever language package you need. I found a list of these packages with a quick Google search. If you’re doing more evil than good, you can also easily find a list of “common passwords” in text form.

#Spencer Smith
#Usage: python dict_crack.py <md5 encrypted password> <dictionary path>
#Example: python dict_crack.py ...
#... 75d3d991f75c3bcbecbbcaa712baa678 /usr/shared/dict/ngerman
import time
import hashlib
import sys
#start runtime clock
start = time.time()
#set md5 encrypted password from command line we are looking for
solution = str(sys.argv[1])
guess=" "
sol = "no solution found"
#open dictionary file
filename = open(str(sys.argv[2]),'r')
#for each entry, generate md5 encryption and check ...
# ... against desired solution
for line in filename:
  m = hashlib.md5()
  #cut off '\n' character
  guess = m.hexdigest()
  if guess == solution:
    sol = line
#close dictionary file
#stop time
end = time.time()
#calculate rough estimate of run-time
t_time = end - start
print "total runtime was -- ",t_time," seconds and the answer was:",sol