new method for string objects
Andrew Dalke
dalke at acm.org
Mon Apr 3 01:38:35 EDT 2000
Fredrik Lundh wrote:
>assuming that "string value" means the "raw contents of the
>sequence object", you should use a buffer objects instead.
>you can use the 'buffer' builtin to turn most sequence objects
>into buffers:
I had never heard of 'buffer' before. I looked into it a bit.
It seems like it only works with C based structures as there's
no way to get the buffer of an object.
I have a "Seq" class which looks like:
class Seq:
def __init__(self, letters, alphabet = Alphabet.generic_alphabet):
assert type(letters) == type("") # input must be a string
self.letters = letters
self.alphabet = alphabet
def tostring(self):
return self.letters
and I have a translation function which looks like:
def translate(seq, table = TranslationTable.standard_table):
s = seq # or "s = seq.tostring()"
letters = []
for i in range(0, len(s), 3):
letters.append(table[s[i:i+3]])
return Seq(string.join(letters, table.forward_alphabet))
If the input is a string or an array.array, then I can
replace the "s = seq" with your buffer suggestion.
s = buffer(seq)
However, if the sequence is a Seq object, then buffer won't
work. (I added a __getattr__ to the Seq object to see if
the buffer() gets an attribute from an instance; it doesn't.)
On the other hand, if strings have a "tostring" method, then I
can always use:
s = seq.tostring()
Andrew Dalke
dalke at acm.org
More information about the Python-list
mailing list