I could use some help making this Python code run faster using only Python code.
Tim Williams
tdwdotnet at gmail.com
Thu Sep 20 19:04:42 EDT 2007
On 20/09/2007, Python Maniac <raychorn at hotmail.com> wrote:
> I am new to Python however I would like some feedback from those who
> know more about Python than I do at this time.
>
> def scrambleLine(line):
> s = ''
> for c in line:
> s += chr(ord(c) | 0x80)
> return s
>
> def descrambleLine(line):
> s = ''
> for c in line:
> s += chr(ord(c) & 0x7f)
> return ''.join( [chr(ord(c) & 0x7f) for c in line] )
>
> def scrambleFile(fname,action=1):
> if (path.exists(fname)):
> try:
> f = open(fname, "r")
> toks = fname.split('.')
> while (len(toks) > 2):
> toks.pop()
> fname = '.'.join(toks)
> if (action == 1):
> _fname = fname + '.scrambled'
> elif (action == 0):
> _fname = fname + '.descrambled'
> if (path.exists(_fname)):
> os.remove(_fname)
> ff = open(_fname, "w+")
> if (action == 1):
> for l in f:
> ff.write(scrambleLine(l))
> elif (action == 0):
> for l in f:
> ff.write(descrambleLine(l))
> except Exception, details:
> print 'ERROR :: (%s)' % details
> finally:
> f.close()
> ff.close()
> else:
> print 'WARNING :: Missing file "%s" - cannot continue.' % fname
>
> --
def scrambleLine(line):
return ''.join( [chr(ord(c) | 0x80) for c in line] )
def descrambleLine(line):
return ''.join( [chr(ord(c) & 0x7f) for c in line] )
def scrambleFile(fname,action=1):
try:
f = open(fname, "r")
fname = '.'.join(fname.split('.')[:2])
if action:
_fname = fname + '.scrambled'
else:
_fname = fname + '.descrambled'
ff = open(_fname, "w")
if action:
ff.write('\r\n.join([scrambleLine(l) for l in f ]))
else :
ff.write('\r\n.join([descrambleLine(l) for l in f ]))
f.close()
ff.close()
except Exception, details:
print 'ERROR :: (%s)' % details
HTH :)
More information about the Python-list
mailing list