WH = Rev( 'Hello World!' )
print WH.forw, WH.back
nnn = Rev( range( 1, 10 ) )
print nnn.forw
print nnn
produces output:
Hello World! !dlroW olleH
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>>rrr = Rev( nnn )
>>>rrr
<1, 2, 3, 4, 5, 6, 7, 8, 9>
# NOT [1, 2, ... 9] i.e. it doesn't check:
if type(self) == type( Rev([]) ) && self.__class__ == Rev( [] ).__class__
but THAT belongs to an OLD thread...
WHICH brings up the need for building a list of values for joinfields
which doen't work on a pseudo sequence object:
>>>joinfields( Rev( string.split( 'This is a test' )), ':::' )
Stack backtrace (innermost last):
File "<stdin>", line 1
TypeError: first argument must be list/tuple
Unnecessarily pedantic typechecking ?
Not in string.py (as far as I can tell). It must be from the internal
strop version. Is there any way to import the joinfields from
string.py over the one in strop module ? ( Or is the solution
to fix strop's joinfields ? )
Needs and __add__ method before you can "print WH.forw + WH.back".
- Steve
#-----------
from string import joinfields
class Rev:
def __init__( self, seq ):
self.forw = seq
self.back = self
def __len__( self ):
return len( self.forw )
def __getitem__( self, j ):
return self.forw[ -( j + 1 ) ]
def __repr__( self ):
seq = self.forw
if type(seq) == type( [] ) :
wrap = '[]'
sep = ', '
elif type(seq) == type( () ) :
wrap = '()'
sep = ', '
elif type(seq) == type( '' ) :
wrap = ''
sep = ''
else:
wrap = '<>'
sep = ', '
outstrs = []
for item in self.back :
outstrs.append( str( item ) )
return wrap[:1] + joinfields( outstrs, sep ) + wrap[-1:]