Cohen 自己的 Python 实现只需要稍作调整(如下)即可独立运行。它在两个文件中,我通过谷歌搜索“difflib 耐心”找到了其中的副本:
http://stuff.mit.edu/afs/athena/system/i386_deb50/os/usr/share/pyshared/bzrlib/patiencediff.py
和
http://stuff.mit.edu/afs/athena/system/i386_deb50/ os/usr/share/pyshared/bzrlib/_patiencediff_py.py
第一个文件可以像 diff 一样从命令行运行。第二个是内部循环的 Python 实现。(单个文件??读者练习!)在 bzrlib 中,还有一个内部循环的 C 实现。
这里(在程序本身的帮助下)是我让它们独立运行的补丁:
Sandy$ patiencediff.py --patience orig/patiencediff.py patiencediff.py
--- orig/patiencediff.py
+++ patiencediff.py
@@ -15,14 +15,20 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+try:
+ from bzrlib.lazy_import import lazy_import
+ lazy_import(globals(), """
+ import os
+ import sys
+ import time
+ import difflib
+ """)
+except:
+ import os
+ import sys
+ import time
+ import difflib
-from bzrlib.lazy_import import lazy_import
-lazy_import(globals(), """
-import os
-import sys
-import time
-import difflib
-""")
__all__ = ['PatienceSequenceMatcher', 'unified_diff', 'unified_diff_files']
@@ -135,11 +141,18 @@
PatienceSequenceMatcher_c as PatienceSequenceMatcher
)
except ImportError:
- from bzrlib._patiencediff_py import (
- unique_lcs_py as unique_lcs,
- recurse_matches_py as recurse_matches,
- PatienceSequenceMatcher_py as PatienceSequenceMatcher
- )
+ try:
+ from bzrlib._patiencediff_py import (
+ unique_lcs_py as unique_lcs,
+ recurse_matches_py as recurse_matches,
+ PatienceSequenceMatcher_py as PatienceSequenceMatcher
+ )
+ except ImportError:
+ from _patiencediff_py import (
+ unique_lcs_py as unique_lcs,
+ recurse_matches_py as recurse_matches,
+ PatienceSequenceMatcher_py as PatienceSequenceMatcher
+ )
def main(args):
Sandy$ patiencediff.py --patience orig/_patiencediff_py.py _patiencediff_py.py
--- orig/_patiencediff_py.py
+++ _patiencediff_py.py
@@ -15,11 +15,16 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
+from __future__ import print_function
from bisect import bisect
import difflib
-from bzrlib.trace import mutter
+try:
+ from bzrlib.trace import mutter
+except:
+ import sys
+ def mutter(msg):
+ print (msg, file=sys.stderr)
__all__ = ['PatienceSequenceMatcher', 'unified_diff', 'unified_diff_files']
Sandy$