bit manipulation frustration
Peter Schneider-Kamp
peter at schneider-kamp.de
Sun Jul 23 03:38:54 EDT 2000
Courageous wrote:
>
> First, in one fell swoop of antigenious, I seem to have
> forgotten how to convert strings to sequences of char and
> vice versa. Eeek. Help? Will someone please remind me and
> hit me with a rubber mallet??? :)
>>> map(None, "hello courageous")
['h', 'e', 'l', 'l', 'o', ' ', 'c', 'o', 'u', 'r', 'a', 'g', 'e', 'o',
'u', 's']
>>>
But what do you need this for? You can work on a string
like on that list except that the list is mutable.
I am not sure if I have understood your other problem though.
Maybe the following class can help you?
from operator import add
class isotile:
def __init__(self, name, size, inc = 2):
self.up = 1
self.pos = 0
self.num = inc
self.size = size
self.inc = inc
self.data = open(name, "rb").read()
def readline(self):
if not self.num:
return ''
newpos = self.pos + self.num
pad = (self.size - self.num) / 2
line = self.data[self.pos:newpos]
self.pos = newpos
if self.up:
if self.num == self.size:
self.up = 0
self.num = self.num - self.inc
else:
self.num = self.num + self.inc
else:
self.num = self.num - self.inc
return pad * '\000' + line + pad * '\000'
def readlines(self):
result = [self.readline()]
while result[-1]:
result.append(self.readline())
return result[:-1]
def read(self):
return reduce(add, self.readlines())
if __name__ == "__main__":
open("isotile_test.dat","wb").write('112222333333444455')
a = isotile("isotile_test.dat", 6)
print a.readlines()
--
Peter Schneider-Kamp ++47-7388-7331
Herman Krags veg 51-11 mailto:peter at schneider-kamp.de
N-7050 Trondheim http://schneider-kamp.de
More information about the Python-list
mailing list