書いてみた

文字列を適当な長さで区切って diff を表示する

【追記】end 引数を使うのを忘れてたので, ちょっと修正

@atsuoishimoto さんには負けました.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def limit_characters1(s, start, maximum, end=None):
    """
    >>> for i in limit_characters1("12345", 0, 2):
    ...     print(i)
    12
    34
    5

    >>> for i in limit_characters1("123456789", 1, 3):
    ...     print(i)
    234
    567
    89

    >>> for i in limit_characters1("123456789", 2, 3):
    ...     print(i)
    345
    678
    9

    >>> for i in limit_characters1("123456789", 3, 3):
    ...     print(i)
    456
    789
    """
    return (s[start + i * maximum:min((end or len(s)), start + (i + 1) * maximum)] \
                for i in range(((end or len(s)) - start - 1) / maximum + 1))

def _test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    _test()