Character Encodings and display of strings

Martin Miller ggrp1.20.martineau at dfgh.net
Tue Nov 14 22:59:22 CET 2006


It is possible derive your own string class from the built-in one and
override what 'repr' does (and make it do whatever you want). Here's an
example of what I mean:

##### Sample #####

# -*- coding: iso-8859-1 -*-

# Special string class to override the default
# representation method. Main purpose is to
# prefer using double quotes and avoid hex
# representation on chars with an ord > 128
class MsgStr(str):

    def __repr__(self):
        asciispace = ord(' ')
        if self.count("'") >= self.count('"'):
            quotechar = '"'
        else:
            quotechar = "'"

        rep = [quotechar]
        for ch in self:
            if ord(ch) < asciispace:
                rep += repr(str(ch)).strip("'")
            elif ch == quotechar:
                rep += "\\"
                rep += ch
            else:
                rep += ch
        rep += quotechar

        return "".join(rep)

if __name__ == "__main__":
    s = MsgStr("\tWürttemberg\"")
    print s
    print repr(s)
    print str(s)
    print repr(str(s))




More information about the Python-list mailing list