[Snippet] Case Correction in Python
Moshe Zadka
moshez at math.huji.ac.il
Fri Jun 9 07:30:33 EDT 2000
Hi!
Those of you who were in my lecture last Friday will remember I showed a
cute-little case correction function, which used global variables. I have
reworked that example into a class which can be adapted to different file
mistakes you may want to allow.
Here it is for your enjoyment:
==================== cut here =================
# PathCorrect.py
# Released to the public domain by Moshe Zadka, 9.6.2000
import dircache, os, string
class PathCorrect:
def __init__(self):
self._cache = {}
def correct(self, file):
if self._cache.has_key(file):
return self._cache[file]
ret = self._correct(file)
self._cache[file] = ret
return ret
def _correct(self, file):
if os.path.exists(file):
return file
dir, file = os.path.split(file)
dir = self.correct(dir)
if not file:
return dir
files = dircache.listdir(dir)
if file in files:
return os.path.join(dir, file)
file = files[map(self.hash, files).index(self.hash(file))]
return os.path.join(dir, file)
def hash(self, file):
raise NotImplementedError("use subclassable")
class CaseCorrect(PathCorrect):
def hash(self, file):
return string.lower(file)
class UnderScoreCorrect(PathCorrect):
def hash(self, file):
return string.replace(file, '_', '')
def _test():
def randomly_case(s):
import random
functions = [string.lower, string.upper]
l = []
for c in s:
l.append(random.choice(functions)(c))
return string.join(l, '')
c = CaseCorrect()
for file in map(randomly_case,
map(os.path.abspath, dircache.listdir("."))):
assert os.path.exists(c.correct(file))
if __name__=='__main__':
_test()
====================== cut here =====================
Those of you paying attention might have noted I also presented a
reworked randomly_case function in the test function, for your silly mood
days.
# Use like:
from PathCorrect import CaseCorrect
c = CaseCorrect()
for file in sys.argv[1:]:
try:
print file+':', c.correct(file)
except ValueError:
print file, 'does not exist, casing aside'
--
Moshe Zadka <moshez at math.huji.ac.il>
http://www.oreilly.com/news/prescod_0300.html
http://www.linux.org.il -- we put the penguin in .com
More information about the Python-list
mailing list