Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
difflib Namespace Reference

Data Structures

class  SequenceMatcher
 
class  Differ
 

Functions

def get_close_matches
 
def IS_LINE_JUNK
 
def IS_CHARACTER_JUNK
 
def ndiff
 
def restore
 

Variables

list __all__
 

Function Documentation

def difflib.get_close_matches (   word,
  possibilities,
  n = 3,
  cutoff = 0.6 
)
Use SequenceMatcher to return list of the best "good enough" matches.

word is a sequence for which close matches are desired (typically a
string).

possibilities is a list of sequences against which to match word
(typically a list of strings).

Optional arg n (default 3) is the maximum number of close matches to
return.  n must be > 0.

Optional arg cutoff (default 0.6) is a float in [0, 1].  Possibilities
that don't score at least that similar to word are ignored.

The best (no more than n) matches among the possibilities are returned
in a list, sorted by similarity score, most similar first.

>>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"])
['apple', 'ape']
>>> import keyword as _keyword
>>> get_close_matches("wheel", _keyword.kwlist)
['while']
>>> get_close_matches("apple", _keyword.kwlist)
[]
>>> get_close_matches("accept", _keyword.kwlist)
['except']

Definition at line 571 of file difflib.py.

572 def get_close_matches(word, possibilities, n=3, cutoff=0.6):
573  """Use SequenceMatcher to return list of the best "good enough" matches.
574 
575  word is a sequence for which close matches are desired (typically a
576  string).
577 
578  possibilities is a list of sequences against which to match word
579  (typically a list of strings).
580 
581  Optional arg n (default 3) is the maximum number of close matches to
582  return. n must be > 0.
583 
584  Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities
585  that don't score at least that similar to word are ignored.
586 
587  The best (no more than n) matches among the possibilities are returned
588  in a list, sorted by similarity score, most similar first.
589 
590  >>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"])
591  ['apple', 'ape']
592  >>> import keyword as _keyword
593  >>> get_close_matches("wheel", _keyword.kwlist)
594  ['while']
595  >>> get_close_matches("apple", _keyword.kwlist)
596  []
597  >>> get_close_matches("accept", _keyword.kwlist)
598  ['except']
599  """
600 
601  if not n > 0:
602  raise ValueError("n must be > 0: " + `n`)
603  if not 0.0 <= cutoff <= 1.0:
604  raise ValueError("cutoff must be in [0.0, 1.0]: " + `cutoff`)
605  result = []
606  s = SequenceMatcher()
607  s.set_seq2(word)
608  for x in possibilities:
609  s.set_seq1(x)
610  if s.real_quick_ratio() >= cutoff and \
611  s.quick_ratio() >= cutoff and \
612  s.ratio() >= cutoff:
613  result.append((s.ratio(), x))
614  # Sort by score.
615  result.sort()
616  # Retain only the best n.
617  result = result[-n:]
618  # Move best-scorer to head of list.
619  result.reverse()
620  # Strip scores.
621  return [x for score, x in result]
622 
def difflib.IS_CHARACTER_JUNK (   ch,
  ws = " \t" 
)

Definition at line 990 of file difflib.py.

991 def IS_CHARACTER_JUNK(ch, ws=" \t"):
992  r"""
993  Return 1 for ignorable character: iff `ch` is a space or tab.
994 
995  Examples:
996 
997  >>> IS_CHARACTER_JUNK(' ')
998  1
999  >>> IS_CHARACTER_JUNK('\t')
1000  1
1001  >>> IS_CHARACTER_JUNK('\n')
1002  0
1003  >>> IS_CHARACTER_JUNK('x')
1004  0
1005  """
1006 
1007  return ch in ws
def difflib.IS_LINE_JUNK (   line,
  pat = re.compile(r"\s*#?\s*$").match 
)

Definition at line 974 of file difflib.py.

975 def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match):
976  r"""
977  Return 1 for ignorable line: iff `line` is blank or contains a single '#'.
978 
979  Examples:
980 
981  >>> IS_LINE_JUNK('\n')
982  1
983  >>> IS_LINE_JUNK(' # \n')
984  1
985  >>> IS_LINE_JUNK('hello\n')
986  0
987  """
988 
989  return pat(line) is not None
def difflib.ndiff (   a,
  b,
  linejunk = IS_LINE_JUNK,
  charjunk = IS_CHARACTER_JUNK 
)

Definition at line 1010 of file difflib.py.

1011 def ndiff(a, b, linejunk=IS_LINE_JUNK, charjunk=IS_CHARACTER_JUNK):
1012  r"""
1013  Compare `a` and `b` (lists of strings); return a `Differ`-style delta.
1014 
1015  Optional keyword parameters `linejunk` and `charjunk` are for filter
1016  functions (or None):
1017 
1018  - linejunk: A function that should accept a single string argument, and
1019  return true iff the string is junk. The default is module-level function
1020  IS_LINE_JUNK, which filters out lines without visible characters, except
1021  for at most one splat ('#').
1022 
1023  - charjunk: A function that should accept a string of length 1. The
1024  default is module-level function IS_CHARACTER_JUNK, which filters out
1025  whitespace characters (a blank or tab; note: bad idea to include newline
1026  in this!).
1027 
1028  Tools/scripts/ndiff.py is a command-line front-end to this function.
1029 
1030  Example:
1031 
1032  >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
1033  ... 'ore\ntree\nemu\n'.splitlines(1))
1034  >>> print ''.join(diff),
1035  - one
1036  ? ^
1037  + ore
1038  ? ^
1039  - two
1040  - three
1041  ? -
1042  + tree
1043  + emu
1044  """
1045  return Differ(linejunk, charjunk).compare(a, b)
def difflib.restore (   delta,
  which 
)

Definition at line 1046 of file difflib.py.

References doctest.testmod().

1047 def restore(delta, which):
1048  r"""
1049  Generate one of the two sequences that generated a delta.
1050 
1051  Given a `delta` produced by `Differ.compare()` or `ndiff()`, extract
1052  lines originating from file 1 or 2 (parameter `which`), stripping off line
1053  prefixes.
1054 
1055  Examples:
1056 
1057  >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
1058  ... 'ore\ntree\nemu\n'.splitlines(1))
1059  >>> diff = list(diff)
1060  >>> print ''.join(restore(diff, 1)),
1061  one
1062  two
1063  three
1064  >>> print ''.join(restore(diff, 2)),
1065  ore
1066  tree
1067  emu
1068  """
1069  try:
1070  tag = {1: "- ", 2: "+ "}[int(which)]
1071  except KeyError:
1072  raise ValueError, ('unknown delta choice (must be 1 or 2): %r'
1073  % which)
1074  prefixes = (" ", tag)
1075  for line in delta:
1076  if line[:2] in prefixes:
1077  yield line[2:]

Variable Documentation

list __all__
Initial value:
1 = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher',
2  'Differ']

Definition at line 24 of file difflib.py.