Comments on base52 encoder/decoder ?

Andrew Dalke adalke at mindspring.com
Wed Jan 8 19:08:21 EST 2003


Paul Rubin:
> I've sometimes thought there should be a function for splitting a long
> string into a bunch of shorter ones of specified length, but I don't
> know if that would give a worthwhile speedup vs. the obvious list
> comprehension.

List comprehension?  Piffle!  You just got the ex-Perler in me to
speak out.

 >>> import re
 >>> print re.sub("(.{5})", r"\1\n", "Greetings and salutations!")
Greet
ings
and s
aluta
tions
!
 >>>

And now the "oh! I know about Python tricks" in me chimes in with

 >>> import re, codecs
 >>> def wrap_codec(name):
...     if not name.startswith("wrap"):
...         return None
...     s = name[4:]
...     if not s.isdigit():
...         return None
...     n = int(s)
...     def wrap_encode(input, errors = None):
...         return re.sub("(.{%d})" % n, "\\1\n", input), len(input)
...     return wrap_encode, None, None, None
...
 >>> codecs.register(wrap_codec)
 >>> print "Greetings and salutations!".encode("wrap5")
Greet
ings
and s
aluta
tions
!
 >>> print "Greetings and salutations!".encode("wrap9")
Greetings
  and salu
tations!
 >>>

And the "aren't the Pyhton 2.3 modules neat" part reminds everyone
about the 'textwrap' module, which is neat but not quite relevant
to this thread as it tries hard to fold on whitespace.

 >>> import textwrap
 >>> print textwrap.fill("Charlotte said 'Greetings and salutations!'",\
...                      13)
Charlotte
said
'Greetings
and
salutations!'
 >>>

BTW, I do line folding somewhat often, and wouldn't mind having a
function or string method which did that.  Not enough to push for
it though, since it is easy to write.  And some people who want
folds also want a continutation character, like a "\" at the end
or a ">" at the start, but having a library function which supports
those is too much.

Bah, I'll stick with writing bio/chem software instead of worrying
about all of this.  :)


					Andrew
					dalke at dalkescientific.com





More information about the Python-list mailing list