ReXX Style translate() function for strings ?

Stephen Ferg steve at ferg.org
Thu Nov 1 18:11:35 EST 2001


I think That Emile's and Greg's suggestions for a Python version of
translate() came close, but didn't quite make it all the way.

Emile's code was very close, but didn't allow for the default value of
the input character set, which is x'00' thru x'FF'.

Greg's routine didn't allow for optional arguments, but his use of
maketrans() was nice, so I swiped it.

I combined the two solutions and added a bit of code of my own, and
here's what I came up with.  The code may wrap a bit (does anybody
know how to stop long lines of code wrapping in a posting?)

#####################################################################################
def translate(s, output=None, input=None, pad=" "):
    """Function:
    emulate the REXX translate() built-in function
    """
    # authors: Emile van Sebill, Greg Ewing, Steve Ferg

    if output == None and input == None: return s.upper()

    # make sure that pad is exactly one character long
    if len(pad) == 0: raise "Pad character passed to REXX translate()
must not be a null string"
    if len(pad)  > 1: raise "Pad character passed to REXX translate()
must not contain more than one character"

    # the default for output is the null string
    if output == None: output = ""

    # the default for input is all characters from hex"00" through
hex"FF"
    if input  == None:
        input = ""
        for n in xrange(256): # produce numbers 0 through 255
            input = input + chr(n)

    # insure that output is as long as input by adding the pad
character, if necessary.
    if len(output) < len(input):
        output = output + pad*(len(input)-len(output))

    return string.translate(s, string.maketrans(input, output))

##########################################################################3

And here's some code to test it with...

##################### test it #################################
print "Testing..."
assert translate('abc123DEF')              == 'ABC123DEF'
assert translate('abbc','&','b')       == 'a&&c'
assert translate('abcdef','12','ec')       == 'ab2d1f'
assert translate('abcdef','12','abcd','.') == '12..ef'
assert translate('4123','abcd','1234')     == 'dabc'
assert translate('cdefghij', None, 'aeiou', '*')  == 'cd*fgh*j'
assert translate("ABC", "."*67)                  == ".. "

old_date = "20001122"  # change a date from yyyymmdd to mm/dd/yyyy
print old_date, "==>", translate('56/78/1234', old_date, '12345678')
print "Done!\n"
##########################################################################

I'm a REXX programmer that is converting to Python.  I've developed a
number of REXX work-alikes in Python.  Here, for example, is strip(),
which you asked about...

###############################################################
def strip(s, strip_option=None, strip_char=None):
    """Function: emulate the REXX strip() built-in function
    """

    if strip_option == None: strip_option = "B"
    if strip_char   == None: strip_char   = " "

    strip_option = strip_option.upper()
    if strip_option == "L" or strip_option == "T" or strip_option ==
"B": pass
    else:   raise 'Invalid strip_option passed to REXX strip():
"'+strip_option+'"'

    if strip_char == "": strip_char = " "
    if len(strip_char) != 1: raise 'Invalid strip_char passed to REXX
strip(): "'+strip_char+'"'

    if strip_option == "B" or strip_option == "L": # BOTH or LEADING
        while s[:1] == strip_char :
            s = s[1:]

    if strip_option == "B" or strip_option == "T": # BOTH or TRAILING
        while s.endswith(strip_char):
            s = s[:-1]

    return s

###############################################################

I'm working on a Python module called rexx.py that implements
REXX-like functions in Python.  I will post it on my web page this
evening, at
http://www.ferg.org

If you know of anybody that is trying to make the transition from REXX
to Python, and trying to do something similar (or already has done
it!!) please let me know.

-- Steve Ferg (steve at ferg.org)



More information about the Python-list mailing list