Skip to content

Commit

Permalink
new: added udiff to get unified diffs of strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
vaab committed Feb 4, 2015
1 parent e8da901 commit 3837b26
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using ``kids.txt``:
- You'll have a ``indent`` / ``dedent`` / ``shorten`` command also in python 2.
- You'll be able to ``wrap`` text keeping the paragraph separated.
- minor helper like ``ucfirst`` function.
- produce unified diffs between 2 strings easily with ``udiff``.


Installation
Expand Down Expand Up @@ -134,6 +135,22 @@ replace the last 2 characters by a '..' to indicate truncation::
'sup..'


udiff
-----

Shows the unified diff between to text::

>>> print(txt.udiff('a\n\nc', 'b\n\nc'))
--- None
+++ None
@@ -1,3 +1,3 @@
-a
+b
<BLANKLINE>
c
<BLANKLINE>


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

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

from .txt import indent, dedent, paragraph_wrap, ucfirst, shorten
from .diff import udiff
36 changes: 36 additions & 0 deletions src/kids/txt/diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-

import difflib


def udiff(a, b, fa=None, fb=None):
r"""Returns a classical unified diff between two given strings.
Straightforward to use::
>>> print(udiff('a\n\nc', 'b\n\nc'))
--- None
+++ None
@@ -1,3 +1,3 @@
-a
+b
<BLANKLINE>
c
<BLANKLINE>
Note that if the input texts do not ends with a new line, it'll be
added automatically, so::
>>> udiff('a', 'b') == udiff('a\n', 'b') == udiff('a', 'b\n')
True
"""
if not a.endswith("\n"):
a += "\n"
if not b.endswith("\n"):
b += "\n"
return "".join(
difflib.unified_diff(
a.splitlines(1), b.splitlines(1),
fa, fb))

0 comments on commit 3837b26

Please sign in to comment.