L26.py
'''\
L26 Functions.
functions:
L80 (str,fp):str
L27 (str,fp):str
L26 (str,fp):str
commands:
main - L80 << input
demo - L26,L27,L80 << 'JeanneMarie Julia Schulte'
test - L26,L27,L80 << various edge-case strings
arguments:
command - main, demo, test,...
input - string to use
[filename] - output file [default: L26.out]
usage:
module.dispatch({command}, args={arguments}) -- calling module from code
$PYTHON module_name {command} {arguments} -- executing module externally
coder@sonnack.com
April 2010
'''
from sys import stdout, stderr, argv
import datetime
def L80 (_s, fp):
'''L80. Encode alphanumeric string to base 80 number (0=0).'''
cs = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz.,;:!?"@#$%&*()-_'
s = reversed(_s)
a = []
for d,ch in enumerate(s):
f = pow(80,d)
n = cs.index(ch)
t = (ch, n, f, n*f)
a.append(t)
for t in reversed(a):
print >> fp, '[%s] %2d * %d = %d' % t
print >> fp
v = reduce(lambda acc,x: acc + x[3], a, 0)
print >> fp, '%s = %d' % (_s, v)
print >> fp
return (v, a)
def L27 (_s, fp):
'''L27. Encode uppercase alphas plus space to base 27 number (spc=0).'''
s = reversed(_s.upper())
b = ord('A') - 1
a = []
for d,ch in enumerate(s):
f = pow(27,d)
n = 0 if ch == ' ' else (ord(ch) - b)
if n < 0: continue
t = (ch, n, f, n*f)
a.append(t)
for t in reversed(a):
print >> fp, '[%s] %2d * %d = %d' % t
print >> fp
v = reduce(lambda acc,x: acc + x[3], a, 0)
print >> fp, '%s = %d' % (_s, v)
print >> fp
return (v, a)
def L26 (_s, fp):
'''L26. Encode uppercase alphas to base 26 number (a=0).'''
s = reversed(_s.upper())
b = ord('A') - 1
a = []
for d,ch in enumerate(s):
f = pow(26,d)
n = 0 if ch == 'Z' else (ord(ch) - b)
if n < 0: continue
t = (ch, n, f, n*f)
a.append(t)
for t in reversed(a):
print >> fp, '[%s] %2d * %d = %d' % t
print >> fp
v = reduce(lambda acc,x: acc + x[3], a, 0)
print >> fp, '%s = %d' % (_s, v)
print >> fp
return (v, a)
def do_test (*args):
print 'test/parameters: %d' % len(args)
input_txt = args[0] if 0 < len(args) else ''
file_name = args[1] if 1 < len(args) else 'L26.out'
fp = open(file_name, 'w')
try:
print >> fp, 100 * '~'
L26('ZABY', fp)
L26('YBAZ', fp)
print >> fp
L27(' ABZ', fp)
L27('ZBA ', fp)
print >> fp
L80('0AZaz_', fp)
L80('_zaZA0', fp)
print >> fp, 100 * '~'
print >> fp
except Exception as e:
print >> fp, e
raise
finally:
fp.close()
return 'Done!'
def do_demo (*args):
print 'demo/parameters: %d' % len(args)
input_txt = args[0] if 0 < len(args) else 'JeanneMarie Julia Schulte'
file_name = args[1] if 1 < len(args) else 'L26.out'
print 'open: %s' % file_name
fp = open(file_name, 'w')
try:
print >> fp, 100 * '~'
print >> fp, 'L26'
L26(input_txt, fp)
print >> fp, 100 * '~'
print >> fp, 'L27'
L27(input_txt, fp)
print >> fp, 100 * '~'
print >> fp, 'L80'
L80(input_txt, fp)
print >> fp, 100 * '~'
print >> fp
print 'wrote: %s' % file_name
except Exception as e:
print >> fp, e
raise
finally:
fp.close()
return 'Done!'
def do_main (*args):
print 'main/parameters: %d' % len(args)
input_txt = args[0] if 0 < len(args) else ''
file_name = args[1] if 1 < len(args) else 'L26.out'
print 'open: %s' % file_name
fp = open(file_name, 'w')
try:
L80(input_txt, fp)
L80(input_txt, stdout)
except Exception as e:
print >> fp, e
raise
finally:
fp.close()
return 'Done!'
def dispatch (cmd, *args):
print 'command: %s' % cmd
print 'arguments: %d' % len(args)
if cmd == 'test': return do_test(*args)
if cmd == 'demo': return do_demo(*args)
if cmd == 'main': return do_main(*args)
return 'Nothing to do!'
if __name__ == '__main__':
print 'autorun: %s' % argv[0]
cmd = argv[1] if 1 < len(argv) else ''
etc = argv[2:] if 2 < len(argv) else []
obj = dispatch(cmd, *etc)
print obj
'''eof'''