class ShortStr(str):
    """Small class to extend str functionality to add several (?)
custom methods
    """

    def __init__(self, value):
        self.value = value

    def shorten(self, length=20, etc=' ..'):
        """ Shortens the string to length 'length' and
        adds 'etc' to the end of the string
        """
        new_value = self.value.strip()
        if len(new_value) > length:
            new_value = '%s%s' % (new_value[:length + 1], etc)
        return new_value

if __name__ == '__main__':
    # Sample
    mystr = 'Commander, we have received a stress signal from Gul
Dukat'
    print 'Original  Version  : %s' % (mystr)
    print 'Shortened Version 1: %s' % (ShortStr(mystr).shorten())
    print 'Shortened Version 2: %s' % (ShortStr(mystr).shorten
(length=10, etc=' [more]'))

Output:

Original  Version  : Commander, we have received a stress signal from
Gul Dukat
Shortened Version 1: Commander, we have re ..
Shortened Version 2: Commander,  [more]

And the reason of the subclassing of "str" is so you can do something
like this: ShortStr(mystr).lower() --> commander, we have received a
stress signal from gul dukat

hth

On Oct 8, 5:33 am, "Jason (spot) Brower" <encomp...@gmail.com> wrote:
> I have some long strings.  How do I shorten them with the "..." feature?
> So when I talk on and on it will... :P
> Regards,
> Jason Brower
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to 
web2py+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to