Skip to content

Commit

Permalink
new: added ucfirst and shorten.
Browse files Browse the repository at this point in the history
  • Loading branch information
vaab committed Feb 4, 2015
1 parent 965371e commit 9f1ab87
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ Wrap paragraph separately::
Notice that that each paragraph has been wrapped separately.


ucfirst
-------

This function will return the given string with the first character forced to
uppercase::

>>> txt.ucfirst('foo')
'Foo'


shorten
-------

This function will truncate the given string to the given length, if necessary. It'll
replace the last 2 characters by a '..' to indicate truncation::

>>> txt.shorten('fool', l=5)
'fool'
>>> txt.shorten('supercalifragilisticexpialidocious', l=5)
'sup..'


Contributing
============

Expand Down
2 changes: 1 addition & 1 deletion src/kids/txt/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Package placeholder

from .txt import indent, dedent, paragraph_wrap
from .txt import indent, dedent, paragraph_wrap, ucfirst, shorten
16 changes: 16 additions & 0 deletions src/kids/txt/txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,19 @@ def paragraph_wrap(text, regexp="\n\n"):
regexp = re.compile(regexp, re.MULTILINE)
return "\n".join("\n".join(textwrap.wrap(paragraph.strip()))
for paragraph in regexp.split(text)).strip()


def ucfirst(msg):
"""Return altered msg where only first letter was forced to uppercase."""
return msg[0].upper() + msg[1:]


## Provided in textwrap.shorten in python 3
def shorten(s, l):
"""Return given string truncated to given length."""
if len(s) > l:
return s[:l - 2] + ".."
return s


truncate = shorten

0 comments on commit 9f1ab87

Please sign in to comment.