Search is not available for this dataset
text
stringlengths
75
104k
def zfill(x, width): """zfill(x, width) -> string Pad a numeric string x with zeros on the left, to fill a field of the specified width. The string x is never truncated. """ if not isinstance(x, basestring): x = repr(x) return x.zfill(width)
def translate(s, table, deletions=""): """translate(s,table [,deletions]) -> string Return a copy of the string s, where all characters occurring in the optional argument deletions are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256. The deletions argument is not allowed for Unicode strings. """ if deletions or table is None: return s.translate(table, deletions) else: # Add s[:0] so that if s is Unicode and table is an 8-bit string, # table is converted to Unicode. This means that table *cannot* # be a dictionary -- for that feature, use u.translate() directly. return s.translate(table + s[:0])
def replace(s, old, new, maxreplace=-1): """replace (str, old, new[, maxreplace]) -> string Return a copy of string str with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, only the first maxreplace occurrences are replaced. """ return s.replace(old, new, maxreplace)
def 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'] """ if not n > 0: raise ValueError("n must be > 0: %r" % (n,)) if not 0.0 <= cutoff <= 1.0: raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,)) result = [] s = SequenceMatcher() s.set_seq2(word) for x in possibilities: s.set_seq1(x) if s.real_quick_ratio() >= cutoff and \ s.quick_ratio() >= cutoff and \ s.ratio() >= cutoff: result.append((s.ratio(), x)) # Move the best scorers to head of list result = heapq.nlargest(n, result) # Strip scores for the best n matches return [x for score, x in result]
def _count_leading(line, ch): """ Return number of `ch` characters at the start of `line`. Example: >>> _count_leading(' abc', ' ') 3 """ i, n = 0, len(line) while i < n and line[i] == ch: i += 1 return i
def _format_range_unified(start, stop): 'Convert range to the "ed" format' # Per the diff spec at http://www.unix.org/single_unix_specification/ beginning = start + 1 # lines start numbering with one length = stop - start if length == 1: # return '{}'.format(beginning) return '%s' % (beginning) if not length: beginning -= 1 # empty ranges begin at line just before the range return '%s,%s' % (beginning, length)
def unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n'): r""" Compare two sequences of lines; generate the delta as a unified diff. Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three. By default, the diff control lines (those with ---, +++, or @@) are created with a trailing newline. This is helpful so that inputs created from file.readlines() result in diffs that are suitable for file.writelines() since both the inputs and outputs have trailing newlines. For inputs that do not have trailing newlines, set the lineterm argument to "" so that the output will be uniformly newline free. The unidiff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification times are normally expressed in the ISO 8601 format. Example: >>> for line in unified_diff('one two three four'.split(), ... 'zero one tree four'.split(), 'Original', 'Current', ... '2005-01-26 23:30:50', '2010-04-02 10:20:52', ... lineterm=''): ... print line # doctest: +NORMALIZE_WHITESPACE --- Original 2005-01-26 23:30:50 +++ Current 2010-04-02 10:20:52 @@ -1,4 +1,4 @@ +zero one -two -three +tree four """ started = False for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n): if not started: started = True # fromdate = '\t{}'.format(fromfiledate) if fromfiledate else '' fromdate = '\t%s' % (fromfiledate) if fromfiledate else '' # todate = '\t{}'.format(tofiledate) if tofiledate else '' todate = '\t%s' % (tofiledate) if tofiledate else '' # yield '--- {}{}{}'.format(fromfile, fromdate, lineterm) yield '--- %s%s%s' % (fromfile, fromdate, lineterm) # yield '+++ {}{}{}'.format(tofile, todate, lineterm) yield '+++ %s%s%s' % (tofile, todate, lineterm) first, last = group[0], group[-1] file1_range = _format_range_unified(first[1], last[2]) file2_range = _format_range_unified(first[3], last[4]) # yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm) yield '@@ -%s +%s @@%s' % (file1_range, file2_range, lineterm) for tag, i1, i2, j1, j2 in group: if tag == 'equal': for line in a[i1:i2]: yield ' ' + line continue if tag in ('replace', 'delete'): for line in a[i1:i2]: yield '-' + line if tag in ('replace', 'insert'): for line in b[j1:j2]: yield '+' + line
def _format_range_context(start, stop): 'Convert range to the "ed" format' # Per the diff spec at http://www.unix.org/single_unix_specification/ beginning = start + 1 # lines start numbering with one length = stop - start if not length: beginning -= 1 # empty ranges begin at line just before the range if length <= 1: # return '{}'.format(beginning) return '%s' % (beginning) # return '{},{}'.format(beginning, beginning + length - 1) return '%s,%s' % (beginning, beginning + length - 1)
def context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n'): r""" Compare two sequences of lines; generate the delta as a context diff. Context diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three. By default, the diff control lines (those with *** or ---) are created with a trailing newline. This is helpful so that inputs created from file.readlines() result in diffs that are suitable for file.writelines() since both the inputs and outputs have trailing newlines. For inputs that do not have trailing newlines, set the lineterm argument to "" so that the output will be uniformly newline free. The context diff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification times are normally expressed in the ISO 8601 format. If not specified, the strings default to blanks. Example: >>> print ''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(1), ... 'zero\none\ntree\nfour\n'.splitlines(1), 'Original', 'Current')), *** Original --- Current *************** *** 1,4 **** one ! two ! three four --- 1,4 ---- + zero one ! tree four """ prefix = dict(insert='+ ', delete='- ', replace='! ', equal=' ') started = False for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n): if not started: started = True # fromdate = '\t{}'.format(fromfiledate) if fromfiledate else '' fromdate = '\t%s' % (fromfiledate) if fromfiledate else '' # todate = '\t{}'.format(tofiledate) if tofiledate else '' todate = '\t%s' % (tofiledate) if tofiledate else '' # yield '*** {}{}{}'.format(fromfile, fromdate, lineterm) yield '*** %s%s%s' % (fromfile, fromdate, lineterm) # yield '--- {}{}{}'.format(tofile, todate, lineterm) yield '--- %s%s%s' % (tofile, todate, lineterm) first, last = group[0], group[-1] yield '***************' + lineterm file1_range = _format_range_context(first[1], last[2]) # yield '*** {} ****{}'.format(file1_range, lineterm) yield '*** %s ****%s' % (file1_range, lineterm) if any(tag in ('replace', 'delete') for tag, _, _, _, _ in group): for tag, i1, i2, _, _ in group: if tag != 'insert': for line in a[i1:i2]: yield prefix[tag] + line file2_range = _format_range_context(first[3], last[4]) # yield '--- {} ----{}'.format(file2_range, lineterm) yield '--- %s ----%s' % (file2_range, lineterm) if any(tag in ('replace', 'insert') for tag, _, _, _, _ in group): for tag, _, _, j1, j2 in group: if tag != 'delete': for line in b[j1:j2]: yield prefix[tag] + line
def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK): r""" Compare `a` and `b` (lists of strings); return a `Differ`-style delta. Optional keyword parameters `linejunk` and `charjunk` are for filter functions (or None): - linejunk: A function that should accept a single string argument, and return true iff the string is junk. The default is None, and is recommended; as of Python 2.3, an adaptive notion of "noise" lines is used that does a good job on its own. - charjunk: A function that should accept a string of length 1. The default is module-level function IS_CHARACTER_JUNK, which filters out whitespace characters (a blank or tab; note: bad idea to include newline in this!). Tools/scripts/ndiff.py is a command-line front-end to this function. Example: >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), ... 'ore\ntree\nemu\n'.splitlines(1)) >>> print ''.join(diff), - one ? ^ + ore ? ^ - two - three ? - + tree + emu """ return Differ(linejunk, charjunk).compare(a, b)
def _mdiff(fromlines, tolines, context=None, linejunk=None, charjunk=IS_CHARACTER_JUNK): r"""Returns generator yielding marked up from/to side by side differences. Arguments: fromlines -- list of text lines to compared to tolines tolines -- list of text lines to be compared to fromlines context -- number of context lines to display on each side of difference, if None, all from/to text lines will be generated. linejunk -- passed on to ndiff (see ndiff documentation) charjunk -- passed on to ndiff (see ndiff documentation) This function returns an iterator which returns a tuple: (from line tuple, to line tuple, boolean flag) from/to line tuple -- (line num, line text) line num -- integer or None (to indicate a context separation) line text -- original line text with following markers inserted: '\0+' -- marks start of added text '\0-' -- marks start of deleted text '\0^' -- marks start of changed text '\1' -- marks end of added/deleted/changed text boolean flag -- None indicates context separation, True indicates either "from" or "to" line contains a change, otherwise False. This function/iterator was originally developed to generate side by side file difference for making HTML pages (see HtmlDiff class for example usage). Note, this function utilizes the ndiff function to generate the side by side difference markup. Optional ndiff arguments may be passed to this function and they in turn will be passed to ndiff. """ import re # regular expression for finding intraline change indices change_re = re.compile('(\++|\-+|\^+)') # create the difference iterator to generate the differences diff_lines_iterator = ndiff(fromlines,tolines,linejunk,charjunk) def _make_line(lines, format_key, side, num_lines=[0,0]): """Returns line of text with user's change markup and line formatting. lines -- list of lines from the ndiff generator to produce a line of text from. When producing the line of text to return, the lines used are removed from this list. format_key -- '+' return first line in list with "add" markup around the entire line. '-' return first line in list with "delete" markup around the entire line. '?' return first line in list with add/delete/change intraline markup (indices obtained from second line) None return first line in list with no markup side -- indice into the num_lines list (0=from,1=to) num_lines -- from/to current line number. This is NOT intended to be a passed parameter. It is present as a keyword argument to maintain memory of the current line numbers between calls of this function. Note, this function is purposefully not defined at the module scope so that data it needs from its parent function (within whose context it is defined) does not need to be of module scope. """ num_lines[side] += 1 # Handle case where no user markup is to be added, just return line of # text with user's line format to allow for usage of the line number. if format_key is None: return (num_lines[side],lines.pop(0)[2:]) # Handle case of intraline changes if format_key == '?': text, markers = lines.pop(0), lines.pop(0) # find intraline changes (store change type and indices in tuples) sub_info = [] def record_sub_info(match_object,sub_info=sub_info): sub_info.append([match_object.group(1)[0],match_object.span()]) return match_object.group(1) change_re.sub(record_sub_info,markers) # process each tuple inserting our special marks that won't be # noticed by an xml/html escaper. for key,(begin,end) in sub_info[::-1]: text = text[0:begin]+'\0'+key+text[begin:end]+'\1'+text[end:] text = text[2:] # Handle case of add/delete entire line else: text = lines.pop(0)[2:] # if line of text is just a newline, insert a space so there is # something for the user to highlight and see. if not text: text = ' ' # insert marks that won't be noticed by an xml/html escaper. text = '\0' + format_key + text + '\1' # Return line of text, first allow user's line formatter to do its # thing (such as adding the line number) then replace the special # marks with what the user's change markup. return (num_lines[side],text) def _line_iterator(): """Yields from/to lines of text with a change indication. This function is an iterator. It itself pulls lines from a differencing iterator, processes them and yields them. When it can it yields both a "from" and a "to" line, otherwise it will yield one or the other. In addition to yielding the lines of from/to text, a boolean flag is yielded to indicate if the text line(s) have differences in them. Note, this function is purposefully not defined at the module scope so that data it needs from its parent function (within whose context it is defined) does not need to be of module scope. """ lines = [] num_blanks_pending, num_blanks_to_yield = 0, 0 while True: # Load up next 4 lines so we can look ahead, create strings which # are a concatenation of the first character of each of the 4 lines # so we can do some very readable comparisons. while len(lines) < 4: try: lines.append(diff_lines_iterator.next()) except StopIteration: lines.append('X') s = ''.join([line[0] for line in lines]) if s.startswith('X'): # When no more lines, pump out any remaining blank lines so the # corresponding add/delete lines get a matching blank line so # all line pairs get yielded at the next level. num_blanks_to_yield = num_blanks_pending elif s.startswith('-?+?'): # simple intraline change yield _make_line(lines,'?',0), _make_line(lines,'?',1), True continue elif s.startswith('--++'): # in delete block, add block coming: we do NOT want to get # caught up on blank lines yet, just process the delete line num_blanks_pending -= 1 yield _make_line(lines,'-',0), None, True continue elif s.startswith(('--?+', '--+', '- ')): # in delete block and see an intraline change or unchanged line # coming: yield the delete line and then blanks from_line,to_line = _make_line(lines,'-',0), None num_blanks_to_yield,num_blanks_pending = num_blanks_pending-1,0 elif s.startswith('-+?'): # intraline change yield _make_line(lines,None,0), _make_line(lines,'?',1), True continue elif s.startswith('-?+'): # intraline change yield _make_line(lines,'?',0), _make_line(lines,None,1), True continue elif s.startswith('-'): # delete FROM line num_blanks_pending -= 1 yield _make_line(lines,'-',0), None, True continue elif s.startswith('+--'): # in add block, delete block coming: we do NOT want to get # caught up on blank lines yet, just process the add line num_blanks_pending += 1 yield None, _make_line(lines,'+',1), True continue elif s.startswith(('+ ', '+-')): # will be leaving an add block: yield blanks then add line from_line, to_line = None, _make_line(lines,'+',1) num_blanks_to_yield,num_blanks_pending = num_blanks_pending+1,0 elif s.startswith('+'): # inside an add block, yield the add line num_blanks_pending += 1 yield None, _make_line(lines,'+',1), True continue elif s.startswith(' '): # unchanged text, yield it to both sides yield _make_line(lines[:],None,0),_make_line(lines,None,1),False continue # Catch up on the blank lines so when we yield the next from/to # pair, they are lined up. while(num_blanks_to_yield < 0): num_blanks_to_yield += 1 yield None,('','\n'),True while(num_blanks_to_yield > 0): num_blanks_to_yield -= 1 yield ('','\n'),None,True if s.startswith('X'): raise StopIteration else: yield from_line,to_line,True def _line_pair_iterator(): """Yields from/to lines of text with a change indication. This function is an iterator. It itself pulls lines from the line iterator. Its difference from that iterator is that this function always yields a pair of from/to text lines (with the change indication). If necessary it will collect single from/to lines until it has a matching pair from/to pair to yield. Note, this function is purposefully not defined at the module scope so that data it needs from its parent function (within whose context it is defined) does not need to be of module scope. """ line_iterator = _line_iterator() fromlines,tolines=[],[] while True: # Collecting lines of text until we have a from/to pair while (len(fromlines)==0 or len(tolines)==0): from_line, to_line, found_diff =line_iterator.next() if from_line is not None: fromlines.append((from_line,found_diff)) if to_line is not None: tolines.append((to_line,found_diff)) # Once we have a pair, remove them from the collection and yield it from_line, fromDiff = fromlines.pop(0) to_line, to_diff = tolines.pop(0) yield (from_line,to_line,fromDiff or to_diff) # Handle case where user does not want context differencing, just yield # them up without doing anything else with them. line_pair_iterator = _line_pair_iterator() if context is None: while True: yield line_pair_iterator.next() # Handle case where user wants context differencing. We must do some # storage of lines until we know for sure that they are to be yielded. else: context += 1 lines_to_write = 0 while True: # Store lines up until we find a difference, note use of a # circular queue because we only need to keep around what # we need for context. index, contextLines = 0, [None]*(context) found_diff = False while(found_diff is False): from_line, to_line, found_diff = line_pair_iterator.next() i = index % context contextLines[i] = (from_line, to_line, found_diff) index += 1 # Yield lines that we have collected so far, but first yield # the user's separator. if index > context: yield None, None, None lines_to_write = context else: lines_to_write = index index = 0 while(lines_to_write): i = index % context index += 1 yield contextLines[i] lines_to_write -= 1 # Now yield the context lines after the change lines_to_write = context-1 while(lines_to_write): from_line, to_line, found_diff = line_pair_iterator.next() # If another change within the context, extend the context if found_diff: lines_to_write = context-1 else: lines_to_write -= 1 yield from_line, to_line, found_diff
def restore(delta, which): r""" Generate one of the two sequences that generated a delta. Given a `delta` produced by `Differ.compare()` or `ndiff()`, extract lines originating from file 1 or 2 (parameter `which`), stripping off line prefixes. Examples: >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), ... 'ore\ntree\nemu\n'.splitlines(1)) >>> diff = list(diff) >>> print ''.join(restore(diff, 1)), one two three >>> print ''.join(restore(diff, 2)), ore tree emu """ try: tag = {1: "- ", 2: "+ "}[int(which)] except KeyError: raise ValueError, ('unknown delta choice (must be 1 or 2): %r' % which) prefixes = (" ", tag) for line in delta: if line[:2] in prefixes: yield line[2:]
def _make(cls, iterable, new=tuple.__new__, len=len): 'Make a new Match object from a sequence or iterable' result = new(cls, iterable) if len(result) != 3: raise TypeError('Expected 3 arguments, got %d' % len(result)) return result
def set_seq1(self, a): """Set the first sequence to be compared. The second sequence to be compared is not changed. >>> s = SequenceMatcher(None, "abcd", "bcde") >>> s.ratio() 0.75 >>> s.set_seq1("bcde") >>> s.ratio() 1.0 >>> SequenceMatcher computes and caches detailed information about the second sequence, so if you want to compare one sequence S against many sequences, use .set_seq2(S) once and call .set_seq1(x) repeatedly for each of the other sequences. See also set_seqs() and set_seq2(). """ if a is self.a: return self.a = a self.matching_blocks = self.opcodes = None
def set_seq2(self, b): """Set the second sequence to be compared. The first sequence to be compared is not changed. >>> s = SequenceMatcher(None, "abcd", "bcde") >>> s.ratio() 0.75 >>> s.set_seq2("abcd") >>> s.ratio() 1.0 >>> SequenceMatcher computes and caches detailed information about the second sequence, so if you want to compare one sequence S against many sequences, use .set_seq2(S) once and call .set_seq1(x) repeatedly for each of the other sequences. See also set_seqs() and set_seq1(). """ if b is self.b: return self.b = b self.matching_blocks = self.opcodes = None self.fullbcount = None self.__chain_b()
def find_longest_match(self, alo, ahi, blo, bhi): """Find longest matching block in a[alo:ahi] and b[blo:bhi]. If isjunk is not defined: Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where alo <= i <= i+k <= ahi blo <= j <= j+k <= bhi and for all (i',j',k') meeting those conditions, k >= k' i <= i' and if i == i', j <= j' In other words, of all maximal matching blocks, return one that starts earliest in a, and of all those maximal matching blocks that start earliest in a, return the one that starts earliest in b. >>> s = SequenceMatcher(None, " abcd", "abcd abcd") >>> s.find_longest_match(0, 5, 0, 9) Match(a=0, b=4, size=5) If isjunk is defined, first the longest matching block is determined as above, but with the additional restriction that no junk element appears in the block. Then that block is extended as far as possible by matching (only) junk elements on both sides. So the resulting block never matches on junk except as identical junk happens to be adjacent to an "interesting" match. Here's the same example as before, but considering blanks to be junk. That prevents " abcd" from matching the " abcd" at the tail end of the second sequence directly. Instead only the "abcd" can match, and matches the leftmost "abcd" in the second sequence: >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") >>> s.find_longest_match(0, 5, 0, 9) Match(a=1, b=0, size=4) If no blocks match, return (alo, blo, 0). >>> s = SequenceMatcher(None, "ab", "c") >>> s.find_longest_match(0, 2, 0, 1) Match(a=0, b=0, size=0) """ # CAUTION: stripping common prefix or suffix would be incorrect. # E.g., # ab # acab # Longest matching block is "ab", but if common prefix is # stripped, it's "a" (tied with "b"). UNIX(tm) diff does so # strip, so ends up claiming that ab is changed to acab by # inserting "ca" in the middle. That's minimal but unintuitive: # "it's obvious" that someone inserted "ac" at the front. # Windiff ends up at the same place as diff, but by pairing up # the unique 'b's and then matching the first two 'a's. a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.isbjunk besti, bestj, bestsize = alo, blo, 0 # find longest junk-free match # during an iteration of the loop, j2len[j] = length of longest # junk-free match ending with a[i-1] and b[j] j2len = {} nothing = [] for i in xrange(alo, ahi): # look at all instances of a[i] in b; note that because # b2j has no junk keys, the loop is skipped if a[i] is junk j2lenget = j2len.get newj2len = {} for j in b2j.get(a[i], nothing): # a[i] matches b[j] if j < blo: continue if j >= bhi: break k = newj2len[j] = j2lenget(j-1, 0) + 1 if k > bestsize: besti, bestj, bestsize = i-k+1, j-k+1, k j2len = newj2len # Extend the best by non-junk elements on each end. In particular, # "popular" non-junk elements aren't in b2j, which greatly speeds # the inner loop above, but also means "the best" match so far # doesn't contain any junk *or* popular non-junk elements. while besti > alo and bestj > blo and \ not isbjunk(b[bestj-1]) and \ a[besti-1] == b[bestj-1]: besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 while besti+bestsize < ahi and bestj+bestsize < bhi and \ not isbjunk(b[bestj+bestsize]) and \ a[besti+bestsize] == b[bestj+bestsize]: bestsize += 1 # Now that we have a wholly interesting match (albeit possibly # empty!), we may as well suck up the matching junk on each # side of it too. Can't think of a good reason not to, and it # saves post-processing the (possibly considerable) expense of # figuring out what to do with it. In the case of an empty # interesting match, this is clearly the right thing to do, # because no other kind of match is possible in the regions. while besti > alo and bestj > blo and \ isbjunk(b[bestj-1]) and \ a[besti-1] == b[bestj-1]: besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 while besti+bestsize < ahi and bestj+bestsize < bhi and \ isbjunk(b[bestj+bestsize]) and \ a[besti+bestsize] == b[bestj+bestsize]: bestsize = bestsize + 1 return Match(besti, bestj, bestsize)
def get_matching_blocks(self): """Return list of triples describing matching subsequences. Each triple is of the form (i, j, n), and means that a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in i and in j. New in Python 2.5, it's also guaranteed that if (i, j, n) and (i', j', n') are adjacent triples in the list, and the second is not the last triple in the list, then i+n != i' or j+n != j'. IOW, adjacent triples never describe adjacent equal blocks. The last triple is a dummy, (len(a), len(b), 0), and is the only triple with n==0. >>> s = SequenceMatcher(None, "abxcd", "abcd") >>> s.get_matching_blocks() [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)] """ if self.matching_blocks is not None: return self.matching_blocks la, lb = len(self.a), len(self.b) # This is most naturally expressed as a recursive algorithm, but # at least one user bumped into extreme use cases that exceeded # the recursion limit on their box. So, now we maintain a list # ('queue`) of blocks we still need to look at, and append partial # results to `matching_blocks` in a loop; the matches are sorted # at the end. queue = [(0, la, 0, lb)] matching_blocks = [] while queue: alo, ahi, blo, bhi = queue.pop() i, j, k = x = self.find_longest_match(alo, ahi, blo, bhi) # a[alo:i] vs b[blo:j] unknown # a[i:i+k] same as b[j:j+k] # a[i+k:ahi] vs b[j+k:bhi] unknown if k: # if k is 0, there was no matching block matching_blocks.append(x) if alo < i and blo < j: queue.append((alo, i, blo, j)) if i+k < ahi and j+k < bhi: queue.append((i+k, ahi, j+k, bhi)) matching_blocks.sort() # It's possible that we have adjacent equal blocks in the # matching_blocks list now. Starting with 2.5, this code was added # to collapse them. i1 = j1 = k1 = 0 non_adjacent = [] for i2, j2, k2 in matching_blocks: # Is this block adjacent to i1, j1, k1? if i1 + k1 == i2 and j1 + k1 == j2: # Yes, so collapse them -- this just increases the length of # the first block by the length of the second, and the first # block so lengthened remains the block to compare against. k1 += k2 else: # Not adjacent. Remember the first block (k1==0 means it's # the dummy we started with), and make the second block the # new block to compare against. if k1: non_adjacent.append((i1, j1, k1)) i1, j1, k1 = i2, j2, k2 if k1: non_adjacent.append((i1, j1, k1)) non_adjacent.append( (la, lb, 0) ) self.matching_blocks = map(Match._make, non_adjacent) return self.matching_blocks
def get_opcodes(self): """Return list of 5-tuples describing how to turn a into b. Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the tuple preceding it, and likewise for j1 == the previous j2. The tags are strings, with these meanings: 'replace': a[i1:i2] should be replaced by b[j1:j2] 'delete': a[i1:i2] should be deleted. Note that j1==j2 in this case. 'insert': b[j1:j2] should be inserted at a[i1:i1]. Note that i1==i2 in this case. 'equal': a[i1:i2] == b[j1:j2] >>> a = "qabxcd" >>> b = "abycdf" >>> s = SequenceMatcher(None, a, b) >>> for tag, i1, i2, j1, j2 in s.get_opcodes(): ... print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])) delete a[0:1] (q) b[0:0] () equal a[1:3] (ab) b[0:2] (ab) replace a[3:4] (x) b[2:3] (y) equal a[4:6] (cd) b[3:5] (cd) insert a[6:6] () b[5:6] (f) """ if self.opcodes is not None: return self.opcodes i = j = 0 self.opcodes = answer = [] for ai, bj, size in self.get_matching_blocks(): # invariant: we've pumped out correct diffs to change # a[:i] into b[:j], and the next matching block is # a[ai:ai+size] == b[bj:bj+size]. So we need to pump # out a diff to change a[i:ai] into b[j:bj], pump out # the matching block, and move (i,j) beyond the match tag = '' if i < ai and j < bj: tag = 'replace' elif i < ai: tag = 'delete' elif j < bj: tag = 'insert' if tag: answer.append( (tag, i, ai, j, bj) ) i, j = ai+size, bj+size # the list of matching blocks is terminated by a # sentinel with size 0 if size: answer.append( ('equal', ai, i, bj, j) ) return answer
def get_grouped_opcodes(self, n=3): """ Isolate change clusters by eliminating ranges with no changes. Return a generator of groups with up to n lines of context. Each group is in the same format as returned by get_opcodes(). >>> from pprint import pprint >>> a = map(str, range(1,40)) >>> b = a[:] >>> b[8:8] = ['i'] # Make an insertion >>> b[20] += 'x' # Make a replacement >>> b[23:28] = [] # Make a deletion >>> b[30] += 'y' # Make another replacement >>> pprint(list(SequenceMatcher(None,a,b).get_grouped_opcodes())) [[('equal', 5, 8, 5, 8), ('insert', 8, 8, 8, 9), ('equal', 8, 11, 9, 12)], [('equal', 16, 19, 17, 20), ('replace', 19, 20, 20, 21), ('equal', 20, 22, 21, 23), ('delete', 22, 27, 23, 23), ('equal', 27, 30, 23, 26)], [('equal', 31, 34, 27, 30), ('replace', 34, 35, 30, 31), ('equal', 35, 38, 31, 34)]] """ codes = self.get_opcodes() if not codes: codes = [("equal", 0, 1, 0, 1)] # Fixup leading and trailing groups if they show no changes. if codes[0][0] == 'equal': tag, i1, i2, j1, j2 = codes[0] codes[0] = tag, max(i1, i2-n), i2, max(j1, j2-n), j2 if codes[-1][0] == 'equal': tag, i1, i2, j1, j2 = codes[-1] codes[-1] = tag, i1, min(i2, i1+n), j1, min(j2, j1+n) nn = n + n group = [] for tag, i1, i2, j1, j2 in codes: # End the current group and start a new one whenever # there is a large range with no changes. if tag == 'equal' and i2-i1 > nn: group.append((tag, i1, min(i2, i1+n), j1, min(j2, j1+n))) yield group group = [] i1, j1 = max(i1, i2-n), max(j1, j2-n) group.append((tag, i1, i2, j1 ,j2)) if group and not (len(group)==1 and group[0][0] == 'equal'): yield group
def ratio(self): """Return a measure of the sequences' similarity (float in [0,1]). Where T is the total number of elements in both sequences, and M is the number of matches, this is 2.0*M / T. Note that this is 1 if the sequences are identical, and 0 if they have nothing in common. .ratio() is expensive to compute if you haven't already computed .get_matching_blocks() or .get_opcodes(), in which case you may want to try .quick_ratio() or .real_quick_ratio() first to get an upper bound. >>> s = SequenceMatcher(None, "abcd", "bcde") >>> s.ratio() 0.75 >>> s.quick_ratio() 0.75 >>> s.real_quick_ratio() 1.0 """ matches = reduce(lambda sum, triple: sum + triple[-1], self.get_matching_blocks(), 0) return _calculate_ratio(matches, len(self.a) + len(self.b))
def quick_ratio(self): """Return an upper bound on ratio() relatively quickly. This isn't defined beyond that it is an upper bound on .ratio(), and is faster to compute. """ # viewing a and b as multisets, set matches to the cardinality # of their intersection; this counts the number of matches # without regard to order, so is clearly an upper bound if self.fullbcount is None: self.fullbcount = fullbcount = {} for elt in self.b: fullbcount[elt] = fullbcount.get(elt, 0) + 1 fullbcount = self.fullbcount # avail[x] is the number of times x appears in 'b' less the # number of times we've seen it in 'a' so far ... kinda avail = {} availhas, matches = avail.__contains__, 0 for elt in self.a: if availhas(elt): numb = avail[elt] else: numb = fullbcount.get(elt, 0) avail[elt] = numb - 1 if numb > 0: matches = matches + 1 return _calculate_ratio(matches, len(self.a) + len(self.b))
def real_quick_ratio(self): """Return an upper bound on ratio() very quickly. This isn't defined beyond that it is an upper bound on .ratio(), and is faster to compute than either .ratio() or .quick_ratio(). """ la, lb = len(self.a), len(self.b) # can't have more matches than the number of elements in the # shorter sequence return _calculate_ratio(min(la, lb), la + lb)
def compare(self, a, b): r""" Compare two sequences of lines; generate the resulting delta. Each sequence must contain individual single-line strings ending with newlines. Such sequences can be obtained from the `readlines()` method of file-like objects. The delta generated also consists of newline- terminated strings, ready to be printed as-is via the writeline() method of a file-like object. Example: >>> print ''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(1), ... 'ore\ntree\nemu\n'.splitlines(1))), - one ? ^ + ore ? ^ - two - three ? - + tree + emu """ cruncher = SequenceMatcher(self.linejunk, a, b) for tag, alo, ahi, blo, bhi in cruncher.get_opcodes(): if tag == 'replace': g = self._fancy_replace(a, alo, ahi, b, blo, bhi) elif tag == 'delete': g = self._dump('-', a, alo, ahi) elif tag == 'insert': g = self._dump('+', b, blo, bhi) elif tag == 'equal': g = self._dump(' ', a, alo, ahi) else: raise ValueError, 'unknown tag %r' % (tag,) for line in g: yield line
def _dump(self, tag, x, lo, hi): """Generate comparison results for a same-tagged range.""" for i in xrange(lo, hi): yield '%s %s' % (tag, x[i])
def _fancy_replace(self, a, alo, ahi, b, blo, bhi): r""" When replacing one block of lines with another, search the blocks for *similar* lines; the best-matching pair (if any) is used as a synch point, and intraline difference marking is done on the similar pair. Lots of work, but often worth it. Example: >>> d = Differ() >>> results = d._fancy_replace(['abcDefghiJkl\n'], 0, 1, ... ['abcdefGhijkl\n'], 0, 1) >>> print ''.join(results), - abcDefghiJkl ? ^ ^ ^ + abcdefGhijkl ? ^ ^ ^ """ # don't synch up unless the lines have a similarity score of at # least cutoff; best_ratio tracks the best score seen so far best_ratio, cutoff = 0.74, 0.75 cruncher = SequenceMatcher(self.charjunk) eqi, eqj = None, None # 1st indices of equal lines (if any) # search for the pair that matches best without being identical # (identical lines must be junk lines, & we don't want to synch up # on junk -- unless we have to) for j in xrange(blo, bhi): bj = b[j] cruncher.set_seq2(bj) for i in xrange(alo, ahi): ai = a[i] if ai == bj: if eqi is None: eqi, eqj = i, j continue cruncher.set_seq1(ai) # computing similarity is expensive, so use the quick # upper bounds first -- have seen this speed up messy # compares by a factor of 3. # note that ratio() is only expensive to compute the first # time it's called on a sequence pair; the expensive part # of the computation is cached by cruncher if cruncher.real_quick_ratio() > best_ratio and \ cruncher.quick_ratio() > best_ratio and \ cruncher.ratio() > best_ratio: best_ratio, best_i, best_j = cruncher.ratio(), i, j if best_ratio < cutoff: # no non-identical "pretty close" pair if eqi is None: # no identical pair either -- treat it as a straight replace for line in self._plain_replace(a, alo, ahi, b, blo, bhi): yield line return # no close pair, but an identical pair -- synch up on that best_i, best_j, best_ratio = eqi, eqj, 1.0 else: # there's a close pair, so forget the identical pair (if any) eqi = None # a[best_i] very similar to b[best_j]; eqi is None iff they're not # identical # pump out diffs from before the synch point for line in self._fancy_helper(a, alo, best_i, b, blo, best_j): yield line # do intraline marking on the synch pair aelt, belt = a[best_i], b[best_j] if eqi is None: # pump out a '-', '?', '+', '?' quad for the synched lines atags = btags = "" cruncher.set_seqs(aelt, belt) for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes(): la, lb = ai2 - ai1, bj2 - bj1 if tag == 'replace': atags += '^' * la btags += '^' * lb elif tag == 'delete': atags += '-' * la elif tag == 'insert': btags += '+' * lb elif tag == 'equal': atags += ' ' * la btags += ' ' * lb else: raise ValueError, 'unknown tag %r' % (tag,) for line in self._qformat(aelt, belt, atags, btags): yield line else: # the synch pair is identical yield ' ' + aelt # pump out diffs from after the synch point for line in self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi): yield line
def _qformat(self, aline, bline, atags, btags): r""" Format "?" output and deal with leading tabs. Example: >>> d = Differ() >>> results = d._qformat('\tabcDefghiJkl\n', '\tabcdefGhijkl\n', ... ' ^ ^ ^ ', ' ^ ^ ^ ') >>> for line in results: print repr(line) ... '- \tabcDefghiJkl\n' '? \t ^ ^ ^\n' '+ \tabcdefGhijkl\n' '? \t ^ ^ ^\n' """ # Can hurt, but will probably help most of the time. common = min(_count_leading(aline, "\t"), _count_leading(bline, "\t")) common = min(common, _count_leading(atags[:common], " ")) common = min(common, _count_leading(btags[:common], " ")) atags = atags[common:].rstrip() btags = btags[common:].rstrip() yield "- " + aline if atags: yield "? %s%s\n" % ("\t" * common, atags) yield "+ " + bline if btags: yield "? %s%s\n" % ("\t" * common, btags)
def make_file(self,fromlines,tolines,fromdesc='',todesc='',context=False, numlines=5): """Returns HTML file of side by side comparison with change highlights Arguments: fromlines -- list of "from" lines tolines -- list of "to" lines fromdesc -- "from" file column header string todesc -- "to" file column header string context -- set to True for contextual differences (defaults to False which shows full differences). numlines -- number of context lines. When context is set True, controls number of lines displayed before and after the change. When context is False, controls the number of lines to place the "next" link anchors before the next change (so click of "next" link jumps to just before the change). """ return self._file_template % dict( styles = self._styles, legend = self._legend, table = self.make_table(fromlines,tolines,fromdesc,todesc, context=context,numlines=numlines))
def _tab_newline_replace(self,fromlines,tolines): """Returns from/to line lists with tabs expanded and newlines removed. Instead of tab characters being replaced by the number of spaces needed to fill in to the next tab stop, this function will fill the space with tab characters. This is done so that the difference algorithms can identify changes in a file when tabs are replaced by spaces and vice versa. At the end of the HTML generation, the tab characters will be replaced with a nonbreakable space. """ def expand_tabs(line): # hide real spaces line = line.replace(' ','\0') # expand tabs into spaces line = line.expandtabs(self._tabsize) # replace spaces from expanded tabs back into tab characters # (we'll replace them with markup after we do differencing) line = line.replace(' ','\t') return line.replace('\0',' ').rstrip('\n') fromlines = [expand_tabs(line) for line in fromlines] tolines = [expand_tabs(line) for line in tolines] return fromlines,tolines
def _split_line(self,data_list,line_num,text): """Builds list of text lines by splitting text lines at wrap point This function will determine if the input text line needs to be wrapped (split) into separate lines. If so, the first wrap point will be determined and the first line appended to the output text line list. This function is used recursively to handle the second part of the split line to further split it. """ # if blank line or context separator, just add it to the output list if not line_num: data_list.append((line_num,text)) return # if line text doesn't need wrapping, just add it to the output list size = len(text) max = self._wrapcolumn if (size <= max) or ((size -(text.count('\0')*3)) <= max): data_list.append((line_num,text)) return # scan text looking for the wrap point, keeping track if the wrap # point is inside markers i = 0 n = 0 mark = '' while n < max and i < size: if text[i] == '\0': i += 1 mark = text[i] i += 1 elif text[i] == '\1': i += 1 mark = '' else: i += 1 n += 1 # wrap point is inside text, break it up into separate lines line1 = text[:i] line2 = text[i:] # if wrap point is inside markers, place end marker at end of first # line and start marker at beginning of second line because each # line will have its own table tag markup around it. if mark: line1 = line1 + '\1' line2 = '\0' + mark + line2 # tack on first line onto the output list data_list.append((line_num,line1)) # use this routine again to wrap the remaining text self._split_line(data_list,'>',line2)
def _line_wrapper(self,diffs): """Returns iterator that splits (wraps) mdiff text lines""" # pull from/to data and flags from mdiff iterator for fromdata,todata,flag in diffs: # check for context separators and pass them through if flag is None: yield fromdata,todata,flag continue (fromline,fromtext),(toline,totext) = fromdata,todata # for each from/to line split it at the wrap column to form # list of text lines. fromlist,tolist = [],[] self._split_line(fromlist,fromline,fromtext) self._split_line(tolist,toline,totext) # yield from/to line in pairs inserting blank lines as # necessary when one side has more wrapped lines while fromlist or tolist: if fromlist: fromdata = fromlist.pop(0) else: fromdata = ('',' ') if tolist: todata = tolist.pop(0) else: todata = ('',' ') yield fromdata,todata,flag
def _collect_lines(self,diffs): """Collects mdiff output into separate lists Before storing the mdiff from/to data into a list, it is converted into a single line of text with HTML markup. """ fromlist,tolist,flaglist = [],[],[] # pull from/to data and flags from mdiff style iterator for fromdata,todata,flag in diffs: try: # store HTML markup of the lines into the lists fromlist.append(self._format_line(0,flag,*fromdata)) tolist.append(self._format_line(1,flag,*todata)) except TypeError: # exceptions occur for lines where context separators go fromlist.append(None) tolist.append(None) flaglist.append(flag) return fromlist,tolist,flaglist
def _make_prefix(self): """Create unique anchor prefixes""" # Generate a unique anchor prefix so multiple tables # can exist on the same HTML page without conflicts. fromprefix = "from%d_" % HtmlDiff._default_prefix toprefix = "to%d_" % HtmlDiff._default_prefix HtmlDiff._default_prefix += 1 # store prefixes so line format method has access self._prefix = [fromprefix,toprefix]
def _convert_flags(self,fromlist,tolist,flaglist,context,numlines): """Makes list of "next" links""" # all anchor names will be generated using the unique "to" prefix toprefix = self._prefix[1] # process change flags, generating middle column of next anchors/links next_id = ['']*len(flaglist) next_href = ['']*len(flaglist) num_chg, in_change = 0, False last = 0 for i,flag in enumerate(flaglist): if flag: if not in_change: in_change = True last = i # at the beginning of a change, drop an anchor a few lines # (the context lines) before the change for the previous # link i = max([0,i-numlines]) next_id[i] = ' id="difflib_chg_%s_%d"' % (toprefix,num_chg) # at the beginning of a change, drop a link to the next # change num_chg += 1 next_href[last] = '<a href="#difflib_chg_%s_%d">n</a>' % ( toprefix,num_chg) else: in_change = False # check for cases where there is no content to avoid exceptions if not flaglist: flaglist = [False] next_id = [''] next_href = [''] last = 0 if context: fromlist = ['<td></td><td>&nbsp;No Differences Found&nbsp;</td>'] tolist = fromlist else: fromlist = tolist = ['<td></td><td>&nbsp;Empty File&nbsp;</td>'] # if not a change on first line, drop a link if not flaglist[0]: next_href[0] = '<a href="#difflib_chg_%s_0">f</a>' % toprefix # redo the last link to link to the top next_href[last] = '<a href="#difflib_chg_%s_top">t</a>' % (toprefix) return fromlist,tolist,flaglist,next_href,next_id
def make_table(self,fromlines,tolines,fromdesc='',todesc='',context=False, numlines=5): """Returns HTML table of side by side comparison with change highlights Arguments: fromlines -- list of "from" lines tolines -- list of "to" lines fromdesc -- "from" file column header string todesc -- "to" file column header string context -- set to True for contextual differences (defaults to False which shows full differences). numlines -- number of context lines. When context is set True, controls number of lines displayed before and after the change. When context is False, controls the number of lines to place the "next" link anchors before the next change (so click of "next" link jumps to just before the change). """ # make unique anchor prefixes so that multiple tables may exist # on the same page without conflict. self._make_prefix() # change tabs to spaces before it gets more difficult after we insert # markup fromlines,tolines = self._tab_newline_replace(fromlines,tolines) # create diffs iterator which generates side by side from/to data if context: context_lines = numlines else: context_lines = None diffs = _mdiff(fromlines,tolines,context_lines,linejunk=self._linejunk, charjunk=self._charjunk) # set up iterator to wrap lines that exceed desired width if self._wrapcolumn: diffs = self._line_wrapper(diffs) # collect up from/to lines and flags into lists (also format the lines) fromlist,tolist,flaglist = self._collect_lines(diffs) # process change flags, generating middle column of next anchors/links fromlist,tolist,flaglist,next_href,next_id = self._convert_flags( fromlist,tolist,flaglist,context,numlines) s = [] fmt = ' <tr><td class="diff_next"%s>%s</td>%s' + \ '<td class="diff_next">%s</td>%s</tr>\n' for i in range(len(flaglist)): if flaglist[i] is None: # mdiff yields None on separator lines skip the bogus ones # generated for the first line if i > 0: s.append(' </tbody> \n <tbody>\n') else: s.append( fmt % (next_id[i],next_href[i],fromlist[i], next_href[i],tolist[i])) if fromdesc or todesc: header_row = '<thead><tr>%s%s%s%s</tr></thead>' % ( '<th class="diff_next"><br /></th>', '<th colspan="2" class="diff_header">%s</th>' % fromdesc, '<th class="diff_next"><br /></th>', '<th colspan="2" class="diff_header">%s</th>' % todesc) else: header_row = '' table = self._table_template % dict( data_rows=''.join(s), header_row=header_row, prefix=self._prefix[1]) return table.replace('\0+','<span class="diff_add">'). \ replace('\0-','<span class="diff_sub">'). \ replace('\0^','<span class="diff_chg">'). \ replace('\1','</span>'). \ replace('\t','&nbsp;')
def _MakeParallelBenchmark(p, work_func, *args): """Create and return a benchmark that runs work_func p times in parallel.""" def Benchmark(b): # pylint: disable=missing-docstring e = threading.Event() def Target(): e.wait() for _ in xrange(b.N / p): work_func(*args) threads = [] for _ in xrange(p): t = threading.Thread(target=Target) t.start() threads.append(t) b.ResetTimer() e.set() for t in threads: t.join() return Benchmark
def visit_function_inline(self, node): """Returns an GeneratedExpr for a function with the given body.""" # First pass collects the names of locals used in this function. Do this in # a separate pass so that we know whether to resolve a name as a local or a # global during the second pass. func_visitor = block.FunctionBlockVisitor(node) for child in node.body: func_visitor.visit(child) func_block = block.FunctionBlock(self.block, node.name, func_visitor.vars, func_visitor.is_generator) visitor = StatementVisitor(func_block, self.future_node) # Indent so that the function body is aligned with the goto labels. with visitor.writer.indent_block(): visitor._visit_each(node.body) # pylint: disable=protected-access result = self.block.alloc_temp() with self.block.alloc_temp('[]πg.Param') as func_args: args = node.args argc = len(args.args) self.writer.write('{} = make([]πg.Param, {})'.format( func_args.expr, argc)) # The list of defaults only contains args for which a default value is # specified so pad it with None to make it the same length as args. defaults = [None] * (argc - len(args.defaults)) + args.defaults for i, (a, d) in enumerate(zip(args.args, defaults)): with self.visit_expr(d) if d else expr.nil_expr as default: tmpl = '$args[$i] = πg.Param{Name: $name, Def: $default}' self.writer.write_tmpl(tmpl, args=func_args.expr, i=i, name=util.go_str(a.arg), default=default.expr) flags = [] if args.vararg: flags.append('πg.CodeFlagVarArg') if args.kwarg: flags.append('πg.CodeFlagKWArg') # The function object gets written to a temporary writer because we need # it as an expression that we subsequently bind to some variable. self.writer.write_tmpl( '$result = πg.NewFunction(πg.NewCode($name, $filename, $args, ' '$flags, func(πF *πg.Frame, πArgs []*πg.Object) ' '(*πg.Object, *πg.BaseException) {', result=result.name, name=util.go_str(node.name), filename=util.go_str(self.block.root.filename), args=func_args.expr, flags=' | '.join(flags) if flags else 0) with self.writer.indent_block(): for var in func_block.vars.values(): if var.type != block.Var.TYPE_GLOBAL: fmt = 'var {0} *πg.Object = {1}; _ = {0}' self.writer.write(fmt.format( util.adjust_local_name(var.name), var.init_expr)) self.writer.write_temp_decls(func_block) self.writer.write('var πR *πg.Object; _ = πR') self.writer.write('var πE *πg.BaseException; _ = πE') if func_block.is_generator: self.writer.write( 'return πg.NewGenerator(πF, func(πSent *πg.Object) ' '(*πg.Object, *πg.BaseException) {') with self.writer.indent_block(): self.writer.write_block(func_block, visitor.writer.getvalue()) self.writer.write('return nil, πE') self.writer.write('}).ToObject(), nil') else: self.writer.write_block(func_block, visitor.writer.getvalue()) self.writer.write(textwrap.dedent("""\ if πE != nil { \tπR = nil } else if πR == nil { \tπR = πg.None } return πR, πE""")) self.writer.write('}), πF.Globals()).ToObject()') return result
def _import_and_bind(self, imp): """Generates code that imports a module and binds it to a variable. Args: imp: Import object representing an import of the form "import x.y.z" or "from x.y import z". Expects only a single binding. """ # Acquire handles to the Code objects in each Go package and call # ImportModule to initialize all modules. with self.block.alloc_temp() as mod, \ self.block.alloc_temp('[]*πg.Object') as mod_slice: self.writer.write_checked_call2( mod_slice, 'πg.ImportModule(πF, {})', util.go_str(imp.name)) # Bind the imported modules or members to variables in the current scope. for binding in imp.bindings: if binding.bind_type == imputil.Import.MODULE: self.writer.write('{} = {}[{}]'.format( mod.name, mod_slice.expr, binding.value)) self.block.bind_var(self.writer, binding.alias, mod.expr) else: self.writer.write('{} = {}[{}]'.format( mod.name, mod_slice.expr, imp.name.count('.'))) # Binding a member of the imported module. with self.block.alloc_temp() as member: self.writer.write_checked_call2( member, 'πg.GetAttr(πF, {}, {}, nil)', mod.expr, self.block.root.intern(binding.value)) self.block.bind_var(self.writer, binding.alias, member.expr)
def _write_except_dispatcher(self, exc, tb, handlers): """Outputs a Go code that jumps to the appropriate except handler. Args: exc: Go variable holding the current exception. tb: Go variable holding the current exception's traceback. handlers: A list of ast.ExceptHandler nodes. Returns: A list of Go labels indexes corresponding to the exception handlers. Raises: ParseError: Except handlers are in an invalid order. """ handler_labels = [] for i, except_node in enumerate(handlers): handler_labels.append(self.block.genlabel()) if except_node.type: with self.visit_expr(except_node.type) as type_,\ self.block.alloc_temp('bool') as is_inst: self.writer.write_checked_call2( is_inst, 'πg.IsInstance(πF, {}.ToObject(), {})', exc, type_.expr) self.writer.write_tmpl(textwrap.dedent("""\ if $is_inst { \tgoto Label$label }"""), is_inst=is_inst.expr, label=handler_labels[-1]) else: # This is a bare except. It should be the last handler. if i != len(handlers) - 1: msg = "default 'except:' must be last" raise util.ParseError(except_node, msg) self.writer.write('goto Label{}'.format(handler_labels[-1])) if handlers[-1].type: # There's no bare except, so the fallback is to re-raise. self.writer.write( 'πE = πF.Raise({}.ToObject(), nil, {}.ToObject())'.format(exc, tb)) self.writer.write('continue') return handler_labels
def listdir(path): """List directory contents, using cache.""" try: cached_mtime, list = cache[path] del cache[path] except KeyError: cached_mtime, list = -1, [] mtime = os.stat(path).st_mtime if mtime != cached_mtime: list = os.listdir(path) list.sort() cache[path] = mtime, list return list
def annotate(head, list): """Add '/' suffixes to directories.""" for i in range(len(list)): if os.path.isdir(os.path.join(head, list[i])): list[i] = list[i] + '/'
def pprint(o, stream=None, indent=1, width=80, depth=None): """Pretty-print a Python o to a stream [default is sys.stdout].""" printer = PrettyPrinter( stream=stream, indent=indent, width=width, depth=depth) printer.pprint(o)
def pformat(o, indent=1, width=80, depth=None): """Format a Python o into a pretty-printed representation.""" return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(o)
def format(self, o, context, maxlevels, level): """Format o for a specific context, returning a string and flags indicating whether the representation is 'readable' and whether the o represents a recursive construct. """ return _safe_repr(o, context, maxlevels, level)
def action(inner_rule, loc=None): """ A decorator returning a function that first runs ``inner_rule`` and then, if its return value is not None, maps that value using ``mapper``. If the value being mapped is a tuple, it is expanded into multiple arguments. Similar to attaching semantic actions to rules in traditional parser generators. """ def decorator(mapper): @llrule(loc, inner_rule.expected) def outer_rule(parser): result = inner_rule(parser) if result is unmatched: return result if isinstance(result, tuple): return mapper(parser, *result) else: return mapper(parser, result) return outer_rule return decorator
def Eps(value=None, loc=None): """A rule that accepts no tokens (epsilon) and returns ``value``.""" @llrule(loc, lambda parser: []) def rule(parser): return value return rule
def Tok(kind, loc=None): """A rule that accepts a token of kind ``kind`` and returns it, or returns None.""" @llrule(loc, lambda parser: [kind]) def rule(parser): return parser._accept(kind) return rule
def Loc(kind, loc=None): """A rule that accepts a token of kind ``kind`` and returns its location, or returns None.""" @llrule(loc, lambda parser: [kind]) def rule(parser): result = parser._accept(kind) if result is unmatched: return result return result.loc return rule
def Rule(name, loc=None): """A proxy for a rule called ``name`` which may not be yet defined.""" @llrule(loc, lambda parser: getattr(parser, name).expected(parser)) def rule(parser): return getattr(parser, name)() return rule
def Expect(inner_rule, loc=None): """A rule that executes ``inner_rule`` and emits a diagnostic error if it returns None.""" @llrule(loc, inner_rule.expected) def rule(parser): result = inner_rule(parser) if result is unmatched: expected = reduce(list.__add__, [rule.expected(parser) for rule in parser._errrules]) expected = list(sorted(set(expected))) if len(expected) > 1: expected = " or ".join([", ".join(expected[0:-1]), expected[-1]]) elif len(expected) == 1: expected = expected[0] else: expected = "(impossible)" error_tok = parser._tokens[parser._errindex] error = diagnostic.Diagnostic( "fatal", "unexpected {actual}: expected {expected}", {"actual": error_tok.kind, "expected": expected}, error_tok.loc) parser.diagnostic_engine.process(error) return result return rule
def Seq(first_rule, *rest_of_rules, **kwargs): """ A rule that accepts a sequence of tokens satisfying ``rules`` and returns a tuple containing their return values, or None if the first rule was not satisfied. """ @llrule(kwargs.get("loc", None), first_rule.expected) def rule(parser): result = first_rule(parser) if result is unmatched: return result results = [result] for rule in rest_of_rules: result = rule(parser) if result is unmatched: return result results.append(result) return tuple(results) return rule
def SeqN(n, *inner_rules, **kwargs): """ A rule that accepts a sequence of tokens satisfying ``rules`` and returns the value returned by rule number ``n``, or None if the first rule was not satisfied. """ @action(Seq(*inner_rules), loc=kwargs.get("loc", None)) def rule(parser, *values): return values[n] return rule
def Alt(*inner_rules, **kwargs): """ A rule that expects a sequence of tokens satisfying one of ``rules`` in sequence (a rule is satisfied when it returns anything but None) and returns the return value of that rule, or None if no rules were satisfied. """ loc = kwargs.get("loc", None) expected = lambda parser: reduce(list.__add__, map(lambda x: x.expected(parser), inner_rules)) if loc is not None: @llrule(loc, expected, cases=len(inner_rules)) def rule(parser): data = parser._save() for idx, inner_rule in enumerate(inner_rules): result = inner_rule(parser) if result is unmatched: parser._restore(data, rule=inner_rule) else: rule.covered[idx] = True return result return unmatched else: @llrule(loc, expected, cases=len(inner_rules)) def rule(parser): data = parser._save() for inner_rule in inner_rules: result = inner_rule(parser) if result is unmatched: parser._restore(data, rule=inner_rule) else: return result return unmatched return rule
def Star(inner_rule, loc=None): """ A rule that accepts a sequence of tokens satisfying ``inner_rule`` zero or more times, and returns the returned values in a :class:`list`. """ @llrule(loc, lambda parser: []) def rule(parser): results = [] while True: data = parser._save() result = inner_rule(parser) if result is unmatched: parser._restore(data, rule=inner_rule) return results results.append(result) return rule
def Newline(loc=None): """A rule that accepts token of kind ``newline`` and returns an empty list.""" @llrule(loc, lambda parser: ["newline"]) def rule(parser): result = parser._accept("newline") if result is unmatched: return result return [] return rule
def Oper(klass, *kinds, **kwargs): """ A rule that accepts a sequence of tokens of kinds ``kinds`` and returns an instance of ``klass`` with ``loc`` encompassing the entire sequence or None if the first token is not of ``kinds[0]``. """ @action(Seq(*map(Loc, kinds)), loc=kwargs.get("loc", None)) def rule(parser, *tokens): return klass(loc=tokens[0].join(tokens[-1])) return rule
def single_input(self, body): """single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE""" loc = None if body != []: loc = body[0].loc return ast.Interactive(body=body, loc=loc)
def file_input(parser, body): """file_input: (NEWLINE | stmt)* ENDMARKER""" body = reduce(list.__add__, body, []) loc = None if body != []: loc = body[0].loc return ast.Module(body=body, loc=loc)
def eval_input(self, expr): """eval_input: testlist NEWLINE* ENDMARKER""" return ast.Expression(body=[expr], loc=expr.loc)
def decorator(self, at_loc, idents, call_opt, newline_loc): """decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE""" root = idents[0] dec_loc = root.loc expr = ast.Name(id=root.value, ctx=None, loc=root.loc) for ident in idents[1:]: dot_loc = ident.loc.begin() dot_loc.begin_pos -= 1 dec_loc = dec_loc.join(ident.loc) expr = ast.Attribute(value=expr, attr=ident.value, ctx=None, loc=expr.loc.join(ident.loc), attr_loc=ident.loc, dot_loc=dot_loc) if call_opt: call_opt.func = expr call_opt.loc = dec_loc.join(call_opt.loc) expr = call_opt return at_loc, expr
def decorated(self, decorators, classfuncdef): """decorated: decorators (classdef | funcdef)""" classfuncdef.at_locs = list(map(lambda x: x[0], decorators)) classfuncdef.decorator_list = list(map(lambda x: x[1], decorators)) classfuncdef.loc = classfuncdef.loc.join(decorators[0][0]) return classfuncdef
def funcdef__26(self, def_loc, ident_tok, args, colon_loc, suite): """(2.6, 2.7) funcdef: 'def' NAME parameters ':' suite""" return ast.FunctionDef(name=ident_tok.value, args=args, returns=None, body=suite, decorator_list=[], at_locs=[], keyword_loc=def_loc, name_loc=ident_tok.loc, colon_loc=colon_loc, arrow_loc=None, loc=def_loc.join(suite[-1].loc))
def varargslist__26(self, fparams, args): """ (2.6, 2.7) varargslist: ((fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']) """ for fparam, default_opt in fparams: if default_opt: equals_loc, default = default_opt args.equals_locs.append(equals_loc) args.defaults.append(default) elif len(args.defaults) > 0: error = diagnostic.Diagnostic( "fatal", "non-default argument follows default argument", {}, fparam.loc, [args.args[-1].loc.join(args.defaults[-1].loc)]) self.diagnostic_engine.process(error) args.args.append(fparam) def fparam_loc(fparam, default_opt): if default_opt: equals_loc, default = default_opt return fparam.loc.join(default.loc) else: return fparam.loc if args.loc is None: args.loc = fparam_loc(*fparams[0]).join(fparam_loc(*fparams[-1])) elif len(fparams) > 0: args.loc = args.loc.join(fparam_loc(*fparams[0])) return args
def tfpdef(self, ident_tok, annotation_opt): """(3.0-) tfpdef: NAME [':' test]""" if annotation_opt: colon_loc, annotation = annotation_opt return self._arg(ident_tok, colon_loc, annotation) return self._arg(ident_tok)
def expr_stmt(self, lhs, rhs): """ (2.6, 2.7, 3.0, 3.1) expr_stmt: testlist (augassign (yield_expr|testlist) | ('=' (yield_expr|testlist))*) (3.2-) expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) | ('=' (yield_expr|testlist_star_expr))*) """ if isinstance(rhs, ast.AugAssign): if isinstance(lhs, ast.Tuple) or isinstance(lhs, ast.List): error = diagnostic.Diagnostic( "fatal", "illegal expression for augmented assignment", {}, rhs.op.loc, [lhs.loc]) self.diagnostic_engine.process(error) else: rhs.target = self._assignable(lhs) rhs.loc = rhs.target.loc.join(rhs.value.loc) return rhs elif rhs is not None: rhs.targets = list(map(self._assignable, [lhs] + rhs.targets)) rhs.loc = lhs.loc.join(rhs.value.loc) return rhs else: return ast.Expr(value=lhs, loc=lhs.loc)
def print_stmt(self, print_loc, stmt): """ (2.6-2.7) print_stmt: 'print' ( [ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ] ) """ stmt.keyword_loc = print_loc if stmt.loc is None: stmt.loc = print_loc else: stmt.loc = print_loc.join(stmt.loc) return stmt
def del_stmt(self, stmt_loc, exprs): # Python uses exprlist here, but does *not* obey the usual # tuple-wrapping semantics, so we embed the rule directly. """del_stmt: 'del' exprlist""" return ast.Delete(targets=[self._assignable(expr, is_delete=True) for expr in exprs], loc=stmt_loc.join(exprs[-1].loc), keyword_loc=stmt_loc)
def return_stmt(self, stmt_loc, values): """return_stmt: 'return' [testlist]""" loc = stmt_loc if values: loc = loc.join(values.loc) return ast.Return(value=values, loc=loc, keyword_loc=stmt_loc)
def yield_stmt(self, expr): """yield_stmt: yield_expr""" return ast.Expr(value=expr, loc=expr.loc)
def raise_stmt__26(self, raise_loc, type_opt): """(2.6, 2.7) raise_stmt: 'raise' [test [',' test [',' test]]]""" type_ = inst = tback = None loc = raise_loc if type_opt: type_, inst_opt = type_opt loc = loc.join(type_.loc) if inst_opt: _, inst, tback = inst_opt loc = loc.join(inst.loc) if tback: loc = loc.join(tback.loc) return ast.Raise(exc=type_, inst=inst, tback=tback, cause=None, keyword_loc=raise_loc, from_loc=None, loc=loc)
def raise_stmt__30(self, raise_loc, exc_opt): """(3.0-) raise_stmt: 'raise' [test ['from' test]]""" exc = from_loc = cause = None loc = raise_loc if exc_opt: exc, cause_opt = exc_opt loc = loc.join(exc.loc) if cause_opt: from_loc, cause = cause_opt loc = loc.join(cause.loc) return ast.Raise(exc=exc, inst=None, tback=None, cause=cause, keyword_loc=raise_loc, from_loc=from_loc, loc=loc)
def import_name(self, import_loc, names): """import_name: 'import' dotted_as_names""" return ast.Import(names=names, keyword_loc=import_loc, loc=import_loc.join(names[-1].loc))
def import_from(self, from_loc, module_name, import_loc, names): """ (2.6, 2.7) import_from: ('from' ('.'* dotted_name | '.'+) 'import' ('*' | '(' import_as_names ')' | import_as_names)) (3.0-) # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+) 'import' ('*' | '(' import_as_names ')' | import_as_names)) """ (dots_loc, dots_count), dotted_name_opt = module_name module_loc = module = None if dotted_name_opt: module_loc, module = dotted_name_opt lparen_loc, names, rparen_loc = names loc = from_loc.join(names[-1].loc) if rparen_loc: loc = loc.join(rparen_loc) if module == "__future__": self.add_flags([x.name for x in names]) return ast.ImportFrom(names=names, module=module, level=dots_count, keyword_loc=from_loc, dots_loc=dots_loc, module_loc=module_loc, import_loc=import_loc, lparen_loc=lparen_loc, rparen_loc=rparen_loc, loc=loc)
def import_as_name(self, name_tok, as_name_opt): """import_as_name: NAME ['as' NAME]""" asname_name = asname_loc = as_loc = None loc = name_tok.loc if as_name_opt: as_loc, asname = as_name_opt asname_name = asname.value asname_loc = asname.loc loc = loc.join(asname.loc) return ast.alias(name=name_tok.value, asname=asname_name, loc=loc, name_loc=name_tok.loc, as_loc=as_loc, asname_loc=asname_loc)
def dotted_as_name(self, dotted_name, as_name_opt): """dotted_as_name: dotted_name ['as' NAME]""" asname_name = asname_loc = as_loc = None dotted_name_loc, dotted_name_name = dotted_name loc = dotted_name_loc if as_name_opt: as_loc, asname = as_name_opt asname_name = asname.value asname_loc = asname.loc loc = loc.join(asname.loc) return ast.alias(name=dotted_name_name, asname=asname_name, loc=loc, name_loc=dotted_name_loc, as_loc=as_loc, asname_loc=asname_loc)
def dotted_name(self, idents): """dotted_name: NAME ('.' NAME)*""" return idents[0].loc.join(idents[-1].loc), \ ".".join(list(map(lambda x: x.value, idents)))
def global_stmt(self, global_loc, names): """global_stmt: 'global' NAME (',' NAME)*""" return ast.Global(names=list(map(lambda x: x.value, names)), name_locs=list(map(lambda x: x.loc, names)), keyword_loc=global_loc, loc=global_loc.join(names[-1].loc))
def exec_stmt(self, exec_loc, body, in_opt): """(2.6, 2.7) exec_stmt: 'exec' expr ['in' test [',' test]]""" in_loc, globals, locals = None, None, None loc = exec_loc.join(body.loc) if in_opt: in_loc, globals, locals = in_opt if locals: loc = loc.join(locals.loc) else: loc = loc.join(globals.loc) return ast.Exec(body=body, locals=locals, globals=globals, loc=loc, keyword_loc=exec_loc, in_loc=in_loc)
def nonlocal_stmt(self, nonlocal_loc, names): """(3.0-) nonlocal_stmt: 'nonlocal' NAME (',' NAME)*""" return ast.Nonlocal(names=list(map(lambda x: x.value, names)), name_locs=list(map(lambda x: x.loc, names)), keyword_loc=nonlocal_loc, loc=nonlocal_loc.join(names[-1].loc))
def assert_stmt(self, assert_loc, test, msg): """assert_stmt: 'assert' test [',' test]""" loc = assert_loc.join(test.loc) if msg: loc = loc.join(msg.loc) return ast.Assert(test=test, msg=msg, loc=loc, keyword_loc=assert_loc)
def if_stmt(self, if_loc, test, if_colon_loc, body, elifs, else_opt): """if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]""" stmt = ast.If(orelse=[], else_loc=None, else_colon_loc=None) if else_opt: stmt.else_loc, stmt.else_colon_loc, stmt.orelse = else_opt for elif_ in reversed(elifs): stmt.keyword_loc, stmt.test, stmt.if_colon_loc, stmt.body = elif_ stmt.loc = stmt.keyword_loc.join(stmt.body[-1].loc) if stmt.orelse: stmt.loc = stmt.loc.join(stmt.orelse[-1].loc) stmt = ast.If(orelse=[stmt], else_loc=None, else_colon_loc=None) stmt.keyword_loc, stmt.test, stmt.if_colon_loc, stmt.body = \ if_loc, test, if_colon_loc, body stmt.loc = stmt.keyword_loc.join(stmt.body[-1].loc) if stmt.orelse: stmt.loc = stmt.loc.join(stmt.orelse[-1].loc) return stmt
def while_stmt(self, while_loc, test, while_colon_loc, body, else_opt): """while_stmt: 'while' test ':' suite ['else' ':' suite]""" stmt = ast.While(test=test, body=body, orelse=[], keyword_loc=while_loc, while_colon_loc=while_colon_loc, else_loc=None, else_colon_loc=None, loc=while_loc.join(body[-1].loc)) if else_opt: stmt.else_loc, stmt.else_colon_loc, stmt.orelse = else_opt stmt.loc = stmt.loc.join(stmt.orelse[-1].loc) return stmt
def for_stmt(self, for_loc, target, in_loc, iter, for_colon_loc, body, else_opt): """for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]""" stmt = ast.For(target=self._assignable(target), iter=iter, body=body, orelse=[], keyword_loc=for_loc, in_loc=in_loc, for_colon_loc=for_colon_loc, else_loc=None, else_colon_loc=None, loc=for_loc.join(body[-1].loc)) if else_opt: stmt.else_loc, stmt.else_colon_loc, stmt.orelse = else_opt stmt.loc = stmt.loc.join(stmt.orelse[-1].loc) return stmt
def try_stmt(self, try_loc, try_colon_loc, body, stmt): """ try_stmt: ('try' ':' suite ((except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] | 'finally' ':' suite)) """ stmt.keyword_loc, stmt.try_colon_loc, stmt.body = \ try_loc, try_colon_loc, body stmt.loc = stmt.loc.join(try_loc) return stmt
def with_stmt__26(self, with_loc, context, with_var, colon_loc, body): """(2.6, 3.0) with_stmt: 'with' test [ with_var ] ':' suite""" if with_var: as_loc, optional_vars = with_var item = ast.withitem(context_expr=context, optional_vars=optional_vars, as_loc=as_loc, loc=context.loc.join(optional_vars.loc)) else: item = ast.withitem(context_expr=context, optional_vars=None, as_loc=None, loc=context.loc) return ast.With(items=[item], body=body, keyword_loc=with_loc, colon_loc=colon_loc, loc=with_loc.join(body[-1].loc))
def with_stmt__27(self, with_loc, items, colon_loc, body): """(2.7, 3.1-) with_stmt: 'with' with_item (',' with_item)* ':' suite""" return ast.With(items=items, body=body, keyword_loc=with_loc, colon_loc=colon_loc, loc=with_loc.join(body[-1].loc))
def with_item(self, context, as_opt): """(2.7, 3.1-) with_item: test ['as' expr]""" if as_opt: as_loc, optional_vars = as_opt return ast.withitem(context_expr=context, optional_vars=optional_vars, as_loc=as_loc, loc=context.loc.join(optional_vars.loc)) else: return ast.withitem(context_expr=context, optional_vars=None, as_loc=None, loc=context.loc)
def except_clause(self, except_loc, exc_opt): """ (2.6, 2.7) except_clause: 'except' [test [('as' | ',') test]] (3.0-) except_clause: 'except' [test ['as' NAME]] """ type_ = name = as_loc = name_loc = None loc = except_loc if exc_opt: type_, name_opt = exc_opt loc = loc.join(type_.loc) if name_opt: as_loc, name_tok, name_node = name_opt if name_tok: name = name_tok.value name_loc = name_tok.loc else: name = name_node name_loc = name_node.loc loc = loc.join(name_loc) return ast.ExceptHandler(type=type_, name=name, except_loc=except_loc, as_loc=as_loc, name_loc=name_loc, loc=loc)
def old_lambdef(self, lambda_loc, args_opt, colon_loc, body): """(2.6, 2.7) old_lambdef: 'lambda' [varargslist] ':' old_test""" if args_opt is None: args_opt = self._arguments() args_opt.loc = colon_loc.begin() return ast.Lambda(args=args_opt, body=body, lambda_loc=lambda_loc, colon_loc=colon_loc, loc=lambda_loc.join(body.loc))
def comparison(self, lhs, rhs): """ (2.6, 2.7) comparison: expr (comp_op expr)* (3.0, 3.1) comparison: star_expr (comp_op star_expr)* (3.2-) comparison: expr (comp_op expr)* """ if len(rhs) > 0: return ast.Compare(left=lhs, ops=list(map(lambda x: x[0], rhs)), comparators=list(map(lambda x: x[1], rhs)), loc=lhs.loc.join(rhs[-1][1].loc)) else: return lhs
def star_expr__30(self, star_opt, expr): """(3.0, 3.1) star_expr: ['*'] expr""" if star_opt: return ast.Starred(value=expr, ctx=None, star_loc=star_opt, loc=expr.loc.join(star_opt)) return expr
def star_expr__32(self, star_loc, expr): """(3.0-) star_expr: '*' expr""" return ast.Starred(value=expr, ctx=None, star_loc=star_loc, loc=expr.loc.join(star_loc))
def power(self, atom, trailers, factor_opt): """power: atom trailer* ['**' factor]""" for trailer in trailers: if isinstance(trailer, ast.Attribute) or isinstance(trailer, ast.Subscript): trailer.value = atom elif isinstance(trailer, ast.Call): trailer.func = atom trailer.loc = atom.loc.join(trailer.loc) atom = trailer if factor_opt: op_loc, factor = factor_opt return ast.BinOp(left=atom, op=ast.Pow(loc=op_loc), right=factor, loc=atom.loc.join(factor.loc)) return atom
def subscriptlist(self, subscripts): """subscriptlist: subscript (',' subscript)* [',']""" if len(subscripts) == 1: return ast.Subscript(slice=subscripts[0], ctx=None, loc=None) elif all([isinstance(x, ast.Index) for x in subscripts]): elts = [x.value for x in subscripts] loc = subscripts[0].loc.join(subscripts[-1].loc) index = ast.Index(value=ast.Tuple(elts=elts, ctx=None, begin_loc=None, end_loc=None, loc=loc), loc=loc) return ast.Subscript(slice=index, ctx=None, loc=None) else: extslice = ast.ExtSlice(dims=subscripts, loc=subscripts[0].loc.join(subscripts[-1].loc)) return ast.Subscript(slice=extslice, ctx=None, loc=None)
def dictmaker(self, elts): """(2.6) dictmaker: test ':' test (',' test ':' test)* [',']""" return ast.Dict(keys=list(map(lambda x: x[0], elts)), values=list(map(lambda x: x[2], elts)), colon_locs=list(map(lambda x: x[1], elts)), loc=None)
def classdef__26(self, class_loc, name_tok, bases_opt, colon_loc, body): """(2.6, 2.7) classdef: 'class' NAME ['(' [testlist] ')'] ':' suite""" bases, lparen_loc, rparen_loc = [], None, None if bases_opt: lparen_loc, bases, rparen_loc = bases_opt return ast.ClassDef(name=name_tok.value, bases=bases, keywords=[], starargs=None, kwargs=None, body=body, decorator_list=[], at_locs=[], keyword_loc=class_loc, lparen_loc=lparen_loc, star_loc=None, dstar_loc=None, rparen_loc=rparen_loc, name_loc=name_tok.loc, colon_loc=colon_loc, loc=class_loc.join(body[-1].loc))
def classdef__30(self, class_loc, name_tok, arglist_opt, colon_loc, body): """(3.0) classdef: 'class' NAME ['(' [testlist] ')'] ':' suite""" arglist, lparen_loc, rparen_loc = [], None, None bases, keywords, starargs, kwargs = [], [], None, None star_loc, dstar_loc = None, None if arglist_opt: lparen_loc, arglist, rparen_loc = arglist_opt bases, keywords, starargs, kwargs = \ arglist.args, arglist.keywords, arglist.starargs, arglist.kwargs star_loc, dstar_loc = arglist.star_loc, arglist.dstar_loc return ast.ClassDef(name=name_tok.value, bases=bases, keywords=keywords, starargs=starargs, kwargs=kwargs, body=body, decorator_list=[], at_locs=[], keyword_loc=class_loc, lparen_loc=lparen_loc, star_loc=star_loc, dstar_loc=dstar_loc, rparen_loc=rparen_loc, name_loc=name_tok.loc, colon_loc=colon_loc, loc=class_loc.join(body[-1].loc))
def arglist(self, args, call): """arglist: (argument ',')* (argument [','] | '*' test (',' argument)* [',' '**' test] | '**' test)""" for arg in args: if isinstance(arg, ast.keyword): call.keywords.append(arg) elif len(call.keywords) > 0: error = diagnostic.Diagnostic( "fatal", "non-keyword arg after keyword arg", {}, arg.loc, [call.keywords[-1].loc]) self.diagnostic_engine.process(error) else: call.args.append(arg) return call
def yield_expr__26(self, yield_loc, exprs): """(2.6, 2.7, 3.0, 3.1, 3.2) yield_expr: 'yield' [testlist]""" if exprs is not None: return ast.Yield(value=exprs, yield_loc=yield_loc, loc=yield_loc.join(exprs.loc)) else: return ast.Yield(value=None, yield_loc=yield_loc, loc=yield_loc)
def yield_expr__33(self, yield_loc, arg): """(3.3-) yield_expr: 'yield' [yield_arg]""" if isinstance(arg, ast.YieldFrom): arg.yield_loc = yield_loc arg.loc = arg.loc.join(arg.yield_loc) return arg elif arg is not None: return ast.Yield(value=arg, yield_loc=yield_loc, loc=yield_loc.join(arg.loc)) else: return ast.Yield(value=None, yield_loc=yield_loc, loc=yield_loc)
def urlparse(url, scheme='', allow_fragments=True): """Parse a URL into 6 components: <scheme>://<netloc>/<path>;<params>?<query>#<fragment> Return a 6-tuple: (scheme, netloc, path, params, query, fragment). Note that we don't break the components up in smaller bits (e.g. netloc is a single string) and we don't expand % escapes.""" tuple = urlsplit(url, scheme, allow_fragments) scheme, netloc, url, query, fragment = tuple if scheme in uses_params and ';' in url: url, params = _splitparams(url) else: params = '' return ParseResult(scheme, netloc, url, params, query, fragment)