def FormatOutput(beta_zero, beta_one, correlation_r, correlation_square_r, estimated_proxy_size=False, prediction=False): ''' OutPut whit the optimal Format. Parameters ------- beta_zero: float, Beta Zero beta_one: float, Beta One correlation_r: float, Correlation Index R correlation_square_r: float, Square Correlation Index R estimated_proxy_size: float, Estimated Proxy Size prediction: float, Prediction for estimated_proxy_size ''' #Print Header print "====================================================================" print string.expandtabs("BetaZero\tBetaOne\tCorrelation R\tCorrelation R^2", 16) print "====================================================================" #Print Values print string.expandtabs("%s\t%s\t%s\t%s" % ( beta_zero,beta_one, correlation_r,correlation_square_r) ,16) print "====================================================================" if estimated_proxy_size and prediction: print ("For E=%s The prediction is P=%s" % (estimated_proxy_size, prediction))
def get_playerNames(numPlayers, playerNames_hum, playerNames_comp): players = ["Player 1", "Player 2", "Player 3"] print string.center(("-" * 80), 80) print string.center(("-" * 80), 80) for i in range(numPlayers): name = "" while name == "": name = raw_input(players[i] + ", what is your name? ") name = name.title() if name == "": print string.center(("-" * 80), 80) print string.center(("-" * 80), 80) print string.expandtabs("ERROR, FIELD EMPTY") print string.expandtabs("Please try again.") print string.center(("-" * 80), 80) print string.center(("-" * 80), 80) players[i] = name if numPlayers == 3: print string.center(("-" * 80), 80) print "Welcome", players[0] + ",", players[1] + ", and", players[2] + "!" if numPlayers == 2: players[2] = playerNames_comp[0] print string.center(("-" * 80), 80) print "Welcome", players[0] + " and", players[1] + "! Today you will be playing against", players[2] + "." if numPlayers == 1: players[1] = playerNames_comp[0] players[2] = playerNames_comp[1] print string.center(("-" * 80), 80) print "Welcome", players[0] + "! Today you will be playing against", players[1], "and", players[2] + "." return players
def LOCCountDir(root, extensions=PY_EXTENSION): ''' Locate all files matching supplied filename pattern in and below supplied root directory. Parameters ---------- root : str, path of programs search file for counting lines.. extensions : list, List of extensions Extension to search programs ''' locate = LocateFiles(root, extensions) great_total = 0 while True: try: file_to_count = locate.next() log.info("Counting File: %s" % file_to_count) (code_lines, parts, total_lines, blank_lines, comment_lines)=LOCCountFile(file_to_count) #print "====================================================================" print string.expandtabs("\t%s" % file_to_count.split("/")[-1],16) FormatOutput(code_lines, parts, total_lines, blank_lines, comment_lines) great_total+=code_lines except StopIteration: log.info("Fin De Archivado") break print "====================================================================" print string.expandtabs("TOTAL\t\t\t%s" % great_total,16) print "===================================================================="
def send_literal_data(self, data): if not data: return lines = string.splitfields(data, '\n') text = string.expandtabs(lines[0]) for l in lines[1:]: self.OutputLine(text, 1) text = string.expandtabs(l) self.OutputLine(text, 0) self.atbreak = 0
def parseJR(fwReport): try: print fwReport lfnText = None pfnText = None guid = None doc = xml.dom.minidom.parse(fwReport) #get the output LFN fileNode = doc.getElementsByTagName('File') #print fileNode[0].toxml() print fileNode if ( fileNode is not None ): print len(fileNode) print type(fileNode) fileNode = fileNode[0] lfnNode = fileNode.getElementsByTagName('LFN') print 'lfnNode %s' % lfnNode if ( lfnNode is not None): lfnText = getText(lfnNode[0].childNodes) #replace all tabs with 0 space lfnText = expandtabs(lfnText, 0) lfnText = lfnText.replace('\n','') #pfnNode = xpath.Evaluate('descendant::File/PFN', doc.documentElement) pfnNode = fileNode.getElementsByTagName('PFN') print 'pfnNode %s ' % pfnNode if ( pfnNode is not None): pfnText = getText(pfnNode[0].childNodes) pfnText = expandtabs(pfnText, 0) pfnText = pfnText.replace('\n','') #guidNode = xpath.Evaluate('descendant::File/GUID', doc.documentElement) guidNode = fileNode.getElementsByTagName('GUID') #print 'guidNode %s' % guidNode if ( guidNode is not None): guid = getText(guidNode[0].childNodes) guid = expandtabs(guid, 0) guid = guid.replace('\n','') #print 'LFN: %s' % lfnText #print 'PFN: %s' % pfnText #print 'GUID: %s' % guid return {'LFN':lfnText, 'PFN': pfnText, 'GUID': guid} except: print 'problem addToDataCache:%s, %s' % \ (sys.exc_info()[0], sys.exc_info()[1]) traceback.print_exc(file=sys.stdout) return {'Error':'Parse_Error'}
def getcomments(object): """Get lines of comments immediately preceding an object's source code. Returns None when source can't be found. """ try: lines, lnum = findsource(object) except (IOError, TypeError): return None if ismodule(object): start = 0 if lines and lines[0][:2] == '#!': start = 1 while start < len(lines) and string.strip(lines[start]) in ('', '#'): start = start + 1 if start < len(lines) and lines[start][:1] == '#': comments = [] end = start while end < len(lines) and lines[end][:1] == '#': comments.append(string.expandtabs(lines[end])) end = end + 1 return string.join(comments, '') elif lnum > 0: indent = indentsize(lines[lnum]) end = lnum - 1 if end >= 0 and string.lstrip(lines[end])[:1] == '#' and indentsize(lines[end]) == indent: comments = [string.lstrip(string.expandtabs(lines[end]))] if end > 0: end = end - 1 comment = string.lstrip(string.expandtabs(lines[end])) while comment[:1] == '#' and indentsize(lines[end]) == indent: comments[:0] = [comment] end = end - 1 if end < 0: break comment = string.lstrip(string.expandtabs(lines[end])) while comments and string.strip(comments[0]) == '#': comments[:1] = [] while comments and string.strip(comments[-1]) == '#': comments[-1:] = [] return string.join(comments, '') return None
def cleandoc(doc): try: lines = string.split(string.expandtabs(doc), '\n') except UnicodeError: return None margin = sys.maxint for line in lines[1:]: content = len(string.lstrip(line)) if content: indent = len(line) - content margin = min(margin, indent) if lines: lines[0] = lines[0].lstrip() if margin < sys.maxint: for i in range(1, len(lines)): lines[i] = lines[i][margin:] while lines and not lines[-1]: lines.pop() while lines and not lines[0]: lines.pop(0) return string.join(lines, '\n')
def generate_assigns_buffer(invert_reset, bindings, internal_bindings, debug=False): buf = "" if len(internal_bindings) > 0: buf += "//Internal Bindings\n" for key in internal_bindings: if key == "clk": continue if key == "rst": continue if key == internal_bindings[key]["signal"]: continue buf += "assign\t{0:<20}=\t{1};\n".format(key, internal_bindings[key]["signal"]) buf += "\n\n" if len(bindings) > 0: buf += "//Bindings\n" buf += "always @ (*) begin\n" for key in bindings: if key == "clk": continue if key == "rst": continue if key == bindings[key]["loc"]: continue if (bindings[key]["direction"] == "input") and ("reg" in bindings[key]): buf += "\t{0:<20}=\t{1};\n".format(key, bindings[key]["loc"]) #buf += "assign\t{0:<20}=\t{1};\n".format(key, bindings[key]["loc"]) buf += "end\n" for key in bindings: if key == "clk": continue if key == "rst": continue if key == bindings[key]["loc"]: continue if (bindings[key]["direction"] == "input") and ("reg" not in bindings[key]): buf += "assign\t{0:<20}=\t{1};\n".format(key, bindings[key]["loc"]) for key in bindings: if key == bindings[key]["loc"]: continue if bindings[key]["direction"] == "output": buf += "assign\t{0:<20}=\t{1};\n".format(bindings[key]["loc"], key) if invert_reset: buf += "\n" buf += "//Invert Reset for this board\n" buf += "always @ (*) begin\n" buf += "\t{0:<20}=\t{1};\n".format("rst_n", "~rst") buf += "end\n" return string.expandtabs(buf, 2)
def print_html(self): # For each line, we scan through looking for magic # strings, outputting verbatim any intervening text scan_re = re.compile( r"(?:(?P<emph>'{2,3})" + r"|(?P<ent>[<>&])" + r"|(?P<word>\b(?:[A-Z][a-z]+){2,}\b)" + r"|(?P<rule>-{4,})" + r"|(?P<url>(http|ftp|nntp|news|mailto)\:[^\s'\"]+\S)" + r"|(?P<email>[-\w._+]+\@[\w.-]+)" + r"|(?P<li>^\s+\*)" + r"|(?P<pre>(\{\{\{|\}\}\}))" + r"|(?P<macro>\[\[(TitleSearch|FullSearch|WordIndex" + r"|TitleIndex|RecentChanges|GoTo)\]\])" + r")") blank_re = re.compile("^\s*$") bullet_re = re.compile("^\s+\*") indent_re = re.compile("^\s*") eol_re = re.compile(r'\r?\n') raw = string.expandtabs(self.raw) for line in eol_re.split(raw): if not self.in_pre: # XXX: Should we check these conditions in this order? if blank_re.match(line): print '<p>' continue indent = indent_re.match(line) print self._indent_to(len(indent.group(0))) print re.sub(scan_re, self.replace, line) if self.in_pre: print '</pre>' print self._undent()
def smart_indent_event(self, event): # if intraline selection: # delete it # elif multiline selection: # do indent-region & return # indent one level text = self.text first, last = self.editwin.get_selection_indices() text.undo_block_start() try: if first and last: if index2line(first) != index2line(last): return self.indent_region_event(event) text.delete(first, last) text.mark_set("insert", first) prefix = text.get("insert linestart", "insert") raw, effective = classifyws(prefix, self.tabwidth) if raw == len(prefix): # only whitespace to the left self.reindent_to(effective + self.indentwidth) else: if self.usetabs: pad = '\t' else: effective = len(string.expandtabs(prefix, self.tabwidth)) n = self.indentwidth pad = ' ' * (n - effective % n) text.insert("insert", pad) text.see("insert") return "break" finally: text.undo_block_stop()
def __init__(self, filename, title, out = sys.stdout): """ Store the source text. """ raw = file(filename).read() self.title = title self.raw = string.strip(string.expandtabs(raw)) self.out = out
def __init__(self, raw, out=sys.stdout, not_covered=[]): """ Store the source text. """ self.raw = string.strip(string.expandtabs(raw)) self.out = out self.not_covered = not_covered # not covered list of lines self.cover_flag = False # is there a <span> tag opened?
def collect_paras(lines): paras = [] line_number = 1 lineacc = [] previndent = None for line in lines: line = string.expandtabs(string.rstrip(line, '\r\n')) (prefix, line) = _ws_prefix_re.match(line).groups() indent = len(prefix) if previndent is None: previndent = indent if previndent != indent or not line: if lineacc: paras.append(Paragraph(previndent, prevline_number, lineacc)) previndent = indent lineacc = [] if line: if not lineacc: prevline_number = line_number lineacc.append(line) line_number = line_number + 1 if lineacc: paras.append(Paragraph(previndent, prevline_number, lineacc)) return paras
def __init__(self,line,name=None,detectorname='',level=None,signature=None,inpnum=None,dimnum=None,configured=None,edge=None,delay=None,deltamin=None,deltamax=None): if line=='': self.name = name self.detectorname = detectorname self.level = level self.signature = signature self.inpnum = inpnum self.dimnum = dimnum self.configured = configured self.edge = edge self.delay = delay self.deltamin = deltamin self.deltamax = deltamax self.l0fdefinition = None return self.line = line self.name = None self.detectorname = None self.level = None self.signature = None self.inpnum = None self.dimnum = None self.configured = None self.l0fdefinition = None self.edge = 0 self.delay = 0 self.deltamin = 1000 self.deltamax = 1000 # parse line (extract input name) self.line = string.expandtabs(self.line) self.line = string.strip(self.line,'\n') ninps = string.split(self.line,'=') if len(ninps)!=2: PrintError("CTP input definition (or detector it belongs to) missing for "+line) return self.name = string.strip(ninps[0]) if self.name[:3]=='l0f' or self.name[:3]=='l0A' or self.name[:3]=='l0B': self.l0fdefinition = string.strip(ninps[1]) return # parse the remaining line (input description) li = string.split(ninps[1],' ') for ix in range(len(li)-1, -1, -1): if li[ix]=='': del li[ix] self.detectorname = li[0] if len(li) == 1: self.configured = 0 elif len(li) >= 9: self.level = int(li[1]) self.signature = int(li[2]) self.inpnum = int(li[3]) self.dimnum = int(li[4]) self.configured = int(li[5]) # elif len(li) >= 9: self.edge = int(li[6]) self.delay = int(li[7]) self.deltamin = int(li[8]) self.deltamax = int(li[9]) else: PrintError("Bad CTP input definition:"+line)
def getdoc(object): """Get the documentation string for an object. All tabs are expanded to spaces. To clean up docstrings that are indented to line up with blocks of code, any whitespace than can be uniformly removed from the second line onwards is removed.""" try: doc = object.__doc__ except AttributeError: return None if not isinstance(doc, types.StringTypes): return None try: lines = string.split(string.expandtabs(doc), '\n') except UnicodeError: return None else: # Find minimum indentation of any non-blank lines after first line. margin = sys.maxint for line in lines[1:]: content = len(string.lstrip(line)) if content: indent = len(line) - content margin = min(margin, indent) # Remove indentation. if lines: lines[0] = lines[0].lstrip() if margin < sys.maxint: for i in range(1, len(lines)): lines[i] = lines[i][margin:] # Remove any trailing or leading blank lines. while lines and not lines[-1]: lines.pop() while lines and not lines[0]: lines.pop(0) return string.join(lines, '\n')
def __init__(self, raw): """ Store the source text. """ self.out_lines = [] for line in string.strip(string.expandtabs(raw)).split('\n'): self.raw = line self.out = "" self.out_lines += [self.format(None, None)]
def __init__( self, raw, output = sys.stdout, color_mapping = _colors ): """ Store the source text""" self._colors = color_mapping self._outfile = output self._raw = string.strip(string.expandtabs(raw))
def parse(self, source): """ Parse and send the colored source. """ self.source = string.expandtabs(source) self.tokenlist = [] # store line offsets in self.offset self.offset = [0, 0] self.lines = 0 pos = 0 while pos < len(self.source): self.lines = self.lines + 1 pos = string.find(self.source, '\n', pos) + 1 if not pos: break self.offset.append(pos) self.offset.append(len(self.source)) # parse the source self.pos = 0 text = cStringIO.StringIO(self.source) try: tokenize.tokenize(text.readline, self) except tokenize.TokenError, ex: msg = ex[0] line = ex[1][0] raise ParseError("ERROR %s\n%s" % ( msg, self.source[self.offset[line]:]))
def __getCalls(self, lines): """returns a tuple containing the list of defined subroutines and the list of used subroutines of a fortran source file Return defined : a list of subroutine defined in the fortran source code used : a list of subroutine that are used by the code included : a list of things included in the code """ used = [] for lsave in lines: l=string.expandtabs(string.lower(lsave)[:-1],1) words=string.split(string.lstrip(l)) if len(words) > 0: if (words[0][0] == '!'): continue for i in range(len(words)-1): if words[i] == 'call': name = string.split(words[i+1],'(')[0] used.append(name) # We delete all dependencies that are present several number of times. used = list(set(used)) return used
def scrub_asm_x86(asm, args): # Scrub runs of whitespace out of the assembly, but leave the leading # whitespace in place. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) # Expand the tabs used for indentation. asm = string.expandtabs(asm, 2) # Detect shuffle asm comments and hide the operands in favor of the comments. asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm) # Detect stack spills and reloads and hide their exact offset and whether # they used the stack pointer or frame pointer. asm = SCRUB_X86_SPILL_RELOAD_RE.sub(r'{{[-0-9]+}}(%\1{{[sb]}}p)\2', asm) # Generically match the stack offset of a memory operand. asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) if getattr(args, 'x86_scrub_rip', False): # Generically match a RIP-relative memory operand. asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm) # Generically match a LCP symbol. asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm) if getattr(args, 'extra_scrub', False): # Avoid generating different checks for 32- and 64-bit because of 'retl' vs 'retq'. asm = SCRUB_X86_RET_RE.sub(r'ret{{[l|q]}}', asm) # Strip kill operands inserted into the asm. asm = common.SCRUB_KILL_COMMENT_RE.sub('', asm) # Strip trailing whitespace. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) return asm
def getdoc(object): """Get the documentation string for an object. All tabs are expanded to spaces. To clean up docstrings that are indented to line up with blocks of code, any whitespace than can be uniformly removed from the second line onwards is removed.""" try: doc = object.__doc__ except AttributeError: return None if not isinstance(doc, (str, unicode)): return None try: lines = string.split(string.expandtabs(doc), '\n') except UnicodeError: return None else: margin = None for line in lines[1:]: content = len(string.lstrip(line)) if not content: continue indent = len(line) - content if margin is None: margin = indent else: margin = min(margin, indent) if margin is not None: for i in range(1, len(lines)): lines[i] = lines[i][margin:] return string.join(lines, '\n')
def compute_bracket_indent(self, _find=string.find): self._study2() assert self.continuation == C_BRACKET j = self.lastopenbracketpos str = self.str n = len(str) origi = i = string.rfind(str, '\n', 0, j) + 1 j = j+1 # one beyond open bracket # find first list item; set i to start of its line while j < n: m = _itemre(str, j) if m: j = m.end() - 1 # index of first interesting char extra = 0 break else: # this line is junk; advance to next line i = j = _find(str, '\n', j) + 1 else: # nothing interesting follows the bracket; # reproduce the bracket line's indentation + a level j = i = origi while str[j] in " \t": j = j+1 extra = self.indentwidth return len(string.expandtabs(str[i:j], self.tabwidth)) + extra
def reformat_paragraph(data, limit=70): lines = string.split(data, "\n") i = 0 n = len(lines) while i < n and is_all_white(lines[i]): i = i+1 if i >= n: return data indent1 = get_indent(lines[i]) if i+1 < n and not is_all_white(lines[i+1]): indent2 = get_indent(lines[i+1]) else: indent2 = indent1 new = lines[:i] partial = indent1 while i < n and not is_all_white(lines[i]): # XXX Should take double space after period (etc.) into account words = re.split("(\s+)", lines[i]) for j in range(0, len(words), 2): word = words[j] if not word: continue # Can happen when line ends in whitespace if len(string.expandtabs(partial + word)) > limit and \ partial != indent1: new.append(string.rstrip(partial)) partial = indent2 partial = partial + word + " " if j+1 < len(words) and words[j+1] != " ": partial = partial + " " i = i+1 new.append(string.rstrip(partial)) # XXX Should reformat remaining paragraphs as well new.extend(lines[i:]) return string.join(new, "\n")
def adjust_whitespace(text): """remove the left-whitespace margin of a block of Python code.""" state = [False, False] (backslashed, triplequoted) = (0, 1) def in_multi_line(line): current_state = state[backslashed] or state[triplequoted] if re.search(r"\\$", line): state[backslashed] = True else: state[backslashed] = False line = re.split(r"#", line)[0] triples = len(re.findall(r"\"\"\"|\'\'\'", line)) if triples == 1 or triples % 2 != 0: state[triplequoted] = not state[triplequoted] return current_state def _indent_line(line, stripspace=""): return re.sub(r"^%s" % stripspace, "", line) stream = StringIO() stripspace = None for line in re.split(r"\r?\n", text): if in_multi_line(line): stream.write(line + "\n") else: line = string.expandtabs(line) if stripspace is None and re.search(r"^[ \t]*[^# \t]", line): stripspace = re.match(r"^([ \t]*)", line).group(1) stream.write(_indent_line(line, stripspace) + "\n") return stream.getvalue()
def __init__(self,line,nameweb=None,namectp=None,eq=None,sin=None,sout=None,ctpin=None): if line=='': self.nameweb=nameweb self.namectp=namectp self.eq=eq self.sin=sin self.sout=sout self.ctpin=ctpin self.l0inputId=-1 return; self.line = string.expandtabs(line) self.line = string.strip(self.line,'\n') li = string.split(self.line,' ') for ix in range(len(li)-1, -1, -1): if li[ix]=='': del li[ix] if len(li)>6: PrintError('Failed to parse switch configuration line:'+line) self.nameweb = li[0] self.namectp = li[1] self.eq = int(li[2]) self.sin = int(li[3]) self.sout = int(li[4]) if len(li)>5: self.ctpin = int(li[5]) else: self.ctpin = self.sout-1 self.l0inputId=-1
def histUser(self, userId, userIdtoSearch): self._log.info("user:"******" command:histImg args={userIdtoSearch:" + userIdtoSearch + "}") output = {} output ['head'] = "User Id Used Disk \t\t Last Login \t\t #Owned Images \n" output ['head'] = string.expandtabs(output ['head'], 8) stradd = "" for i in range(len(output['head'])): stradd += "-" output ['head'] += stradd if (userIdtoSearch == "None"): userIdtoSearch = None users = self.userStore.queryStore(userId, userIdtoSearch) if(users != None): for key in users.keys(): spaces = "" num = 8 - len(users[key]._userId) if (num > 0): for i in range(num): spaces += " " output[key] = users[key]._userId + spaces + " " + str(users[key]._fsUsed).split(".")[0] + " \t\t " + \ str(users[key]._lastLogin) + " \t " + str(users[key]._ownedImgs).split(".")[0] + "\n" return output
def wrap_text(text, width): if text is None: return [] if len(text) <= width: return [text] text = string.expandtabs(text) text = string.translate(text, WS_TRANS) chunks = re.split('( +|-+)', text) chunks = filter(None, chunks) lines = [] while chunks: cur_line = [] cur_len = 0 while chunks: l = len(chunks[0]) if cur_len + l <= width: cur_line.append(chunks[0]) del chunks[0] cur_len = cur_len + l else: if cur_line and cur_line[-1][0] == ' ': del cur_line[-1] break if chunks: if cur_len == 0: cur_line.append(chunks[0][0:width]) chunks[0] = chunks[0][width:] if chunks[0][0] == ' ': del chunks[0] lines.append(string.join(cur_line, '')) return lines
def run(self, edit, width=0): width = get_width(self.view) settings = self.view.settings() # Make sure tabs are handled as per the current buffer tab_width = 8 if settings.get("tab_size", False): try: tab_width = int(self.view.settings().get("tab_size")) except TypeError: pass if tab_width == 0: tab_width == 8 paragraphs = [] for s in self.view.sel(): #self.view.insert(edit, s.begin(), self.MARKER) paragraphs.extend( par.all_paragraphs_intersecting_selection(self.view, s)) if len(paragraphs) > 0: self.view.sel().clear() for p in paragraphs: self.view.sel().add(p) # This isn't an ideal way to do it, as we loose the position of the # cursor within the paragraph: hence why the paragraph is selected # at the end. for s in self.view.sel(): wrapper = textwrap.TextWrapper() wrapper.expand_tabs = False wrapper.replace_whitespace = settings.get( 'replace_whitespace', True) wrapper.drop_whitespace = settings.get( 'drop_whitespace', True) wrapper.width = width prefix = self.extract_prefix(s) if prefix: wrapper.initial_indent = prefix[0] wrapper.subsequent_indent = prefix[1] wrapper.width -= self.width_in_spaces(prefix, tab_width) if wrapper.width < 0: continue txt = self.view.substr(s) if prefix: txt = txt.replace(prefix[0], u"") txt = string.expandtabs(txt, tab_width) txt = wrapper.fill(txt) + u"\n" self.view.replace(edit, s, txt) # It's unhelpful to have the entire paragraph selected, just leave the # selection at the end ends = [s.end() - 1 for s in self.view.sel()] self.view.sel().clear() for pt in ends: self.view.sel().add(sublime.Region(pt))
def writeAlnFile(fh, n): # A regular expression object that indicates the # start of a new pattern patRegex = re.compile("^pattern (?P<patNo>[0-9]+)") instanceRegex = re.compile("^\s+(?P<seqNo>[0-9]+)\s+(?P<posNo>[0-9]+)\s+(?P<string>[A-Z]+)") # Find the pattern we want i=0 for line in fh: m = patRegex.match(line) if m != None and int(m.group('patNo')) == n: break i+=1 i=0 alnOutput = "" for line in fh: m = instanceRegex.match(line) if m == None: break else: alnOutput = alnOutput + "seq%s_%s\t\t%s\n" % (m.group('seqNo'), m.group('posNo'), m.group('string')) i+=1 alnOutput = string.expandtabs(alnOutput, 2) (fileno,alnFile) = tempfile.mkstemp(prefix=sys.argv[0]) try: alnFileH = open(alnFile, "w") except IOError, (errno, strerror): print "Error %s: %s" % (errno, strerror) sys.exit()
def cleandoc(doc): """Clean up indentation from docstrings. Any whitespace that can be uniformly removed from the second line onwards is removed.""" try: lines = string.split(string.expandtabs(doc), '\n') except UnicodeError: return None else: # Find minimum indentation of any non-blank lines after first line. margin = sys.maxint for line in lines[1:]: content = len(string.lstrip(line)) if content: indent = len(line) - content margin = min(margin, indent) # Remove indentation. if lines: lines[0] = lines[0].lstrip() if margin < sys.maxint: for i in range(1, len(lines)): lines[i] = lines[i][margin:] # Remove any trailing or leading blank lines. while lines and not lines[-1]: lines.pop() while lines and not lines[0]: lines.pop(0) return string.join(lines, '\n')
def scrub_asm_riscv(asm, args): # Scrub runs of whitespace out of the assembly, but leave the leading # whitespace in place. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) # Expand the tabs used for indentation. asm = string.expandtabs(asm, 2) # Strip trailing whitespace. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) return asm
def getcomments(object): """Get lines of comments immediately preceding an object's source code. Returns None when source can't be found. """ try: lines, lnum = findsource(object) except (IOError, TypeError): return None if ismodule(object): # Look for a comment block at the top of the file. start = 0 if lines and lines[0][:2] == '#!': start = 1 while start < len(lines) and string.strip(lines[start]) in ('', '#'): start = start + 1 if start < len(lines) and lines[start][:1] == '#': comments = [] end = start while end < len(lines) and lines[end][:1] == '#': comments.append(string.expandtabs(lines[end])) end = end + 1 return string.join(comments, '') # Look for a preceding block of comments at the same indentation. elif lnum > 0: indent = indentsize(lines[lnum]) end = lnum - 1 if end >= 0 and string.lstrip(lines[end])[:1] == '#' and \ indentsize(lines[end]) == indent: comments = [string.lstrip(string.expandtabs(lines[end]))] if end > 0: end = end - 1 comment = string.lstrip(string.expandtabs(lines[end])) while comment[:1] == '#' and indentsize(lines[end]) == indent: comments[:0] = [comment] end = end - 1 if end < 0: break comment = string.lstrip(string.expandtabs(lines[end])) while comments and string.strip(comments[0]) == '#': comments[:1] = [] while comments and string.strip(comments[-1]) == '#': comments[-1:] = [] return string.join(comments, '')
def getcomments(object): try: lines, lnum = findsource(object) except (IOError, TypeError): return None if ismodule(object): start = 0 if lines and lines[0][:2] == '#!': start = 1 while start < len(lines) and string.strip(lines[start]) in ('', '#'): start = start + 1 if start < len(lines) and lines[start][:1] == '#': comments = [] end = start while end < len(lines) and lines[end][:1] == '#': comments.append(string.expandtabs(lines[end])) end = end + 1 return string.join(comments, '') elif lnum > 0: indent = indentsize(lines[lnum]) end = lnum - 1 if end >= 0 and string.lstrip(lines[end])[:1] == '#' and indentsize( lines[end]) == indent: comments = [string.lstrip(string.expandtabs(lines[end]))] if end > 0: end = end - 1 comment = string.lstrip(string.expandtabs(lines[end])) while comment[:1] == '#' and indentsize(lines[end]) == indent: comments[:0] = [comment] end = end - 1 if end < 0: break comment = string.lstrip(string.expandtabs(lines[end])) while comments and string.strip(comments[0]) == '#': comments[:1] = [] while comments and string.strip(comments[-1]) == '#': comments[-1:] = [] return string.join(comments, '')
def scrub_asm_mips(asm, args): # Scrub runs of whitespace out of the assembly, but leave the leading # whitespace in place. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) # Expand the tabs used for indentation. asm = string.expandtabs(asm, 2) # Strip trailing whitespace. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) global last_frame_size last_frame_size = None stackframe_size_regex = re.compile( r'(?P<instr>cincoffset \$c11, \$c11), -(?P<size>\d+)\n *.cfi_def_cfa_offset (?P<cfa>\d+)' ) asm = stackframe_size_regex.sub(do_stackframe_size_sub, asm, count=1) if not last_frame_size: mips_stackframe_size_regex = re.compile( r'(?P<instr>daddiu \$sp, \$sp), -(?P<size>\d+)\n *.cfi_def_cfa_offset (?P<cfa>\d+)' ) asm = mips_stackframe_size_regex.sub(do_stackframe_size_sub, asm, count=1) if not last_frame_size: stackframe_size_fallback_regex = re.compile( r'(?P<instr>cincoffset \$c11, \$c11), -(?P<size>\d+)\n') asm = stackframe_size_fallback_regex.sub( do_stackframe_size_fallback_sub, asm, count=1) if not last_frame_size: mips_stackframe_size_fallback_regex = re.compile( r'(?P<instr>daddiu \$sp, \$sp), -(?P<size>\d+)\n') asm = mips_stackframe_size_fallback_regex.sub( do_stackframe_size_fallback_sub, asm, count=1) # Expand .cfi offsets and clc offset to #CAPS_SIZE for CHERI128/256 stack_store_cap_re = re.compile( r'(?P<insn>csc|clc) (?P<reg>\$\w+), \$zero, (?P<offset_sign>\-)?(?P<offset>\d+)\(\$c11\) *# (?P<cap_size>16|32)\-byte Folded (Spill|Reload)' ) asm = stack_store_cap_re.sub(do_clc_csc_sub, asm) stack_store_dword_re = re.compile( r'(?P<insn>csd|cld) (?P<reg>\$\w+), \$zero, (?P<offset_sign>\-)?(?P<offset>\d+)\(\$c11\) *# 8\-byte Folded (Spill|Reload)' ) asm = stack_store_dword_re.sub(do_save_load_dword_sub, asm) cfi_offset_regex = re.compile( r'\.cfi_offset (?P<reg>[$\w]+), -(?P<offset>\d+)') asm = cfi_offset_regex.sub(do_cfi_offset_sub, asm) stackframe_inc_return_regex = re.compile( r'cjr \$c17\n *cincoffset \$c11, \$c11, \d+') asm = stackframe_inc_return_regex.sub( 'cjr $c17\n cincoffset $c11, $c11, [[#STACKFRAME_SIZE]]', asm) # Finally try to replace all other stack cincoffsets if last_frame_size: asm = re.sub("cincoffset\s+\$c11,\s+\$c11, " + str(last_frame_size), "cincoffset $c11, $c11, [[#STACKFRAME_SIZE]]", asm) asm = re.sub("daddiu\s+\$sp,\s+\$sp, " + str(last_frame_size), "daddiu $sp, $sp, [[#STACKFRAME_SIZE]]", asm) return asm
def adjust_whitespace(text): """remove the left-whitespace margin of a block of Python code.""" state = [False, False] (backslashed, triplequoted) = (0, 1) def in_multi_line(line): start_state = (state[backslashed] or state[triplequoted]) if re.search(r"\\$", line): state[backslashed] = True else: state[backslashed] = False def match(reg, t): m = re.match(reg, t) if m: return m, t[len(m.group(0)):] else: return None, t while line: if state[triplequoted]: m, line = match(r"%s" % state[triplequoted], line) if m: state[triplequoted] = False else: m, line = match(r".*?(?=%s|$)" % state[triplequoted], line) else: m, line = match(r'#', line) if m: return start_state m, line = match(r"\"\"\"|\'\'\'", line) if m: state[triplequoted] = m.group(0) continue m, line = match(r".*?(?=\"\"\"|\'\'\'|#|$)", line) return start_state def _indent_line(line, stripspace=''): return re.sub(r"^%s" % stripspace, '', line) lines = [] stripspace = None for line in re.split(r'\r?\n', text): if in_multi_line(line): lines.append(line) else: line = string.expandtabs(line) if stripspace is None and re.search(r"^[ \t]*[^# \t]", line): stripspace = re.match(r"^([ \t]*)", line).group(1) lines.append(_indent_line(line, stripspace)) return "\n".join(lines)
def handle_literal(self, text): lines = string.splitfields(text, '\n') for i in range(1, len(lines)): lines[i] = string.expandtabs(lines[i], 8) for line in lines[:-1]: self.fmt.addword(line, 0) self.fmt.flush() self.fmt.nospace = 0 for line in lines[-1:]: self.fmt.addword(line, 0)
def __getModules(self): """returns a tuple containing the list of defined modules and the list of used modules of a fortran source file Return defined : a list of procedures defined in the fortran source code used : a list of modules that are used by the code included : a list of things included in the code """ f = open(self.filename, 'r') lines = f.readlines() f.close() defined = [] used = [] included = [] for lsave in lines: l = string.expandtabs(string.lower(lsave)[:-1], 1) words = string.split(string.lstrip(l)) if len(words) > 0: if words[0] == 'use': used.append(string.split(words[1], ',')[0]) if words[0] == 'module': if len(words) == 2 or words[1] != "procedure": defined.append(words[1]) if words[0] == 'include': newstring = string.replace(words[1], '\'', '') newstring = string.replace(newstring, '\"', '') included.append(newstring) l = string.expandtabs(lsave[:-1], 1) words = string.split(string.lstrip(l)) if len(words) > 0: if words[0] == '#include': newstring = string.replace(words[1], '\"', '') included.append(newstring) # We delete all dependencies that are present several number of times. used = list(set(used)) return defined, used, included
def trim(string): # type: (str) -> str """ Trim a string in PEP-256 compatible way """ lines = string.expandtabs().splitlines() lines = list(map(str.lstrip, lines[:1])) + left_trim_lines(lines[1:]) return "\n".join(trim_leading_lines(trim_trailing_lines(lines)))
def get_playerNames(numPlayers, playerNames_hum, playerNames_comp): players = ["Player 1", "Player 2", "Player 3"] print "" ## print string.center(("-" * 80), 80) ## print string.center(("-" * 80), 80) for i in range(numPlayers): name = "" while name == "": name = raw_input(players[i] + ", what is your name? ") name = name.title() if name == "": print "" print string.center(("-" * 80), 80) print string.expandtabs("ERROR, FIELD EMPTY") print string.expandtabs("Please try again.") print string.center(("-" * 80), 80) print "" players[i] = name if numPlayers == 3: print "" ## print string.center(("-" * 80), 80) print string.center(("-" * 80), 80) print "" print "Welcome", players[0] + ",", players[1] + ", and", players[ 2] + "!" print "" if numPlayers == 2: players[2] = playerNames_comp[0] print "" ## print string.center(("-" * 80), 80) print "Welcome", players[0] + " and", players[ 1] + "! Today you will be playing against", players[2] + "." if numPlayers == 1: players[1] = playerNames_comp[0] players[2] = playerNames_comp[1] print "" ## print string.center(("-" * 80), 80) print "Welcome", players[ 0] + "! Today you will be playing against", players[ 1], "and", players[2] + "." return players
def engine(text, style=None, width=79, refill=1, tabify=0, position=None): """\ Add, delete or adjust a boxed comment held in TEXT, according to STYLE. STYLE values are explained at beginning of this file. Any zero attribute in STYLE indicates that the corresponding attribute should be recovered from the currently existing box. Produced lines will not go over WIDTH columns if possible, if refilling gets done. But if REFILL is false, WIDTH is ignored. If TABIFY is true, the beginning of produced lines will have spaces replace by TABs. POSITION is either None, or a character position within TEXT. Returns four values: the old box style, the new box style, the reformatted text, and either None or the adjusted value of POSITION in the new text. The reformatted text is returned as None if the requested style does not exist. """ last_line_complete = text and text[-1] == '\n' if last_line_complete: text = text[:-1] lines = string.split(string.expandtabs(text), '\n') # Decide about refilling and the box style to use. new_style = 111 old_template = guess_template(lines) new_style = merge_styles(new_style, old_template.style) if style is not None: new_style = merge_styles(new_style, style) new_template = template_registry.get(new_style) # Interrupt processing if STYLE does not exist. if not new_template: return old_template.style, new_style, None, None # Remove all previous comment marks, and left margin. if position is not None: marker = Marker() marker.save_position(text, position, old_template.characters()) lines, margin = old_template.unbuild(lines) # Ensure only one white line between paragraphs. counter = 1 while counter < len(lines) - 1: if lines[counter] == '' and lines[counter - 1] == '': del lines[counter] else: counter = counter + 1 # Rebuild the boxed comment. lines = new_template.build(lines, width, refill, margin) # Retabify to the left only. if tabify: for counter in range(len(lines)): tabs = len(re.match(' *', lines[counter]).group()) / 8 lines[counter] = '\t' * tabs + lines[counter][8 * tabs:] # Restore the point position. text = string.join(lines, '\n') if last_line_complete: text = text + '\n' if position is not None: position = marker.get_position(text, new_template.characters()) return old_template.style, new_style, text, position
def __init__(self, raw, out = sys.stdout): self.raw = string.strip(string.expandtabs(raw)) self.out = out self.inComment = False self.commentType = None self.commentText = None self.possibleMultilineComment = False self.possibleMultilineChar = None self.backupMode = False self.tok = None self.nextTok = None
def scrub_body(body, tool_basename): # Scrub runs of whitespace out of the assembly, but leave the leading # whitespace in place. body = SCRUB_WHITESPACE_RE.sub(r' ', body) # Expand the tabs used for indentation. body = string.expandtabs(body, 2) # Strip trailing whitespace. body = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', body) if tool_basename == "llc": body = scrub_asm(body) return body
def scrub_asm_powerpc64(asm, args): # Scrub runs of whitespace out of the assembly, but leave the leading # whitespace in place. asm = SCRUB_WHITESPACE_RE.sub(r' ', asm) # Expand the tabs used for indentation. asm = string.expandtabs(asm, 2) # Stripe unimportant comments asm = SCRUB_LOOP_COMMENT_RE.sub(r'', asm) # Strip trailing whitespace. asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) return asm
def scrub_asm_arm_eabi(asm): # Scrub runs of whitespace out of the assembly, but leave the leading # whitespace in place. asm = SCRUB_WHITESPACE_RE.sub(r' ', asm) # Expand the tabs used for indentation. asm = string.expandtabs(asm, 2) # Strip kill operands inserted into the asm. asm = SCRUB_KILL_COMMENT_RE.sub('', asm) # Strip trailing whitespace. asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) return asm
def make_safe(s): """string = make_safe(string) Remove control characters from a string.""" s = string.expandtabs(s, 8) new = '' for i in s: if ord(i) > 31: new = new + i return new
def wrap_text(text, width): if text is None: return [] if len(text) <= width: return [text] text = string.expandtabs(text) text = string.translate(text, WS_TRANS) chunks = re.split(r'( +|-+)', text) chunks = filter(None, chunks) # ' - ' results in empty strings lines = [] while chunks: cur_line = [] # list of chunks (to-be-joined) cur_len = 0 # length of current line while chunks: l = len(chunks[0]) if cur_len + l <= width: # can squeeze (at least) this chunk in cur_line.append(chunks[0]) del chunks[0] cur_len = cur_len + l else: # this line is full # drop last chunk if all space if cur_line and cur_line[-1][0] == ' ': del cur_line[-1] break if chunks: # any chunks left to process? # if the current line is still empty, then we had a single # chunk that's too big too fit on a line -- so we break # down and break it up at the line width if cur_len == 0: cur_line.append(chunks[0][0:width]) chunks[0] = chunks[0][width:] # all-whitespace chunks at the end of a line can be discarded # (and we know from the re.split above that if a chunk has # *any* whitespace, it is *all* whitespace) if chunks[0][0] == ' ': del chunks[0] # and store this line in the list-of-all-lines -- as a single # string, of course! lines.append(string.join(cur_line, '')) # while chunks return lines
def scrub_asm_powerpc(asm, args): # Scrub runs of whitespace out of the assembly, but leave the leading # whitespace in place. asm = common.SCRUB_WHITESPACE_RE.sub(r' ', asm) # Expand the tabs used for indentation. asm = string.expandtabs(asm, 2) # Strip unimportant comments, but leave the token '#' in place. asm = common.SCRUB_LOOP_COMMENT_RE.sub(r'#', asm) # Strip trailing whitespace. asm = common.SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) # Strip the tailing token '#', except the line only has token '#'. asm = common.SCRUB_TAILING_COMMENT_TOKEN_RE.sub(r'', asm) return asm
def parse(self, inFileName): """ Read and parse the source. """ fd = open(inFileName) try: srcLines = fd.read() self.srcLines = string.expandtabs(srcLines) finally: fd.close() self.tokenlist = [] self.__computeOffsets() self.__parseSource()
def __init__(self, raw, times, counts, thresholdT=0.01, out=sys.stdout): """ Store the source text. """ self.raw = string.strip(string.expandtabs(raw)) self.out = out self.times = times self.counts = counts self.totTime = sum(times.values()) self.maxTime = max(max(times.values()), 1e-3) self.cover_flag = False self.line_flag = False self.thresholdT = thresholdT
def msg_emit(lev, tag, msg): if msg_verbose >= lev: msg = string.split(msg, "\n") msg = map(string.rstrip, msg) while msg and msg[-1] == "": msg.pop() pad = "|" + " " * (len(tag) - 1) for s in msg: s = string.expandtabs(s) print "%s%s" % (tag, s) tag = pad
def get_html(self): res = ["<h1>%s</h1>" % self.word] raw = string.expandtabs(self.raw) for line in eol_re.split(raw): if not self.in_pre: if blank_re.match(line): res.append('<p>') continue res.append(re.sub(scan_re, self.replace, line)) res.append("\n") if self.in_pre: res.append('</pre>') return "".join(res)
def tabify(self,data): ''' from moinmoin tokenizer example ''' self.raw = string.strip(string.expandtabs(data)) #print "read ",len(self.raw) self.lines = [0,0] pos = 0 while True: pos = string.find(self.raw,'\n',pos) + 1 if not pos: break self.lines.append(pos) self.lines.append(len(self.raw))
def __init__(self, raw, colors, title, out=sys.stdout): ''' Store the source text. ''' self.raw = string.strip(string.expandtabs(raw)) self.out = out self.title = os.path.basename(title) self.ClassFlag = 0 self.DefFlag = 0 self.colors = colors # Name: Date stamp top self.header = 0 # Name: Date stamp bottom self.footer = 0
def makeHtmlSection(text, bgcolor='#FFA0FF'): """Create HTML code for a section. This is usually a header for all classes or functions. u """ text = htmlescape(expandtabs(text)) result = [] result.append("""<TABLE WIDTH="100\%" BORDER="0">""") result.append("""<TR><TD BGCOLOR="%s" VALIGN="CENTER">""" % bgcolor) result.append("""<H2>%s</H2>""" % text) result.append("""</TD></TR></TABLE>""") result.append('') return join(result, '\n')
def code_filter(): '''This function does all the work.''' global language, backend inline_comment = inline_comments[language] blk_comment = block_comments[language] if blk_comment: blk_comment = (re.escape(block_comments[language][0]), re.escape(block_comments[language][1])) stag, etag = commenttags[backend] in_comment = 0 # True if we're inside a multi-line block comment. tag_comment = 0 # True if we should tag the current line as a comment. line = sys.stdin.readline() while line: line = string.rstrip(line) line = string.expandtabs(line, tabsize) # Escape special characters. line = string.replace(line, '&', '&') line = string.replace(line, '<', '<') line = string.replace(line, '>', '>') # Process block comment. if blk_comment: if in_comment: if re.match(r'.*' + blk_comment[1] + r'$', line): in_comment = 0 else: if re.match(r'^\s*' + blk_comment[0] + r'.*' + blk_comment[1], line): # Single line block comment. tag_comment = 1 elif re.match(r'^\s*' + blk_comment[0], line): # Start of multi-line block comment. tag_comment = 1 in_comment = 1 else: tag_comment = 0 if tag_comment: if line: line = stag + line + etag else: if inline_comment: pos = string.find(line, inline_comment) else: pos = -1 if pos >= 0: # Process inline comment. line = re.sub(r'\b(?P<word>\w+)\b',sub_keyword,line[:pos]) \ + stag + line[pos:] + etag else: line = re.sub(r'\b(?P<word>\w+)\b', sub_keyword, line) sys.stdout.write(line + os.linesep) line = sys.stdin.readline()
def makeHtmlSubSection(text, bgcolor='#AAA0FF'): """Create HTML code for a subsection. This is usually a class or function name. """ text = htmlescape(expandtabs(text)) result = [] result.append("""<TABLE WIDTH="100\%" BORDER="0">""") result.append("""<TR><TD BGCOLOR="%s" VALIGN="CENTER">""" % bgcolor) result.append("""<H3><TT><FONT SIZE="+2">%s</FONT></TT></H3>""" % text) result.append("""</TD></TR></TABLE>""") result.append('') return join(result, '\n')
def compute_backslash_indent(self): self._study2() assert self.continuation == C_BACKSLASH str = self.str i = self.stmt_start while str[i] in " \t": i = i+1 startpos = i # See whether the initial line starts an assignment stmt; i.e., # look for an = operator endpos = string.find(str, '\n', startpos) + 1 found = level = 0 while i < endpos: ch = str[i] if ch in "([{": level = level + 1 i = i+1 elif ch in ")]}": if level: level = level - 1 i = i+1 elif ch == '"' or ch == "'": i = _match_stringre(str, i, endpos).end() elif ch == '#': break elif level == 0 and ch == '=' and \ (i == 0 or str[i-1] not in "=<>!") and \ str[i+1] != '=': found = 1 break else: i = i+1 if found: # found a legit =, but it may be the last interesting # thing on the line i = i+1 # move beyond the = found = re.match(r"\s*\\", str[i:endpos]) is None if not found: # oh well ... settle for moving beyond the first chunk # of non-whitespace chars i = startpos while str[i] not in " \t\n": i = i+1 return len(string.expandtabs(str[self.stmt_start : i], self.tabwidth)) + 1
def _munge_whitespace(self, text): """_munge_whitespace(text : string) -> string Munge whitespace in text: expand tabs and convert all other whitespace characters to spaces. Eg. " foo\tbar\n\nbaz" becomes " foo bar baz". """ if self.expand_tabs: text = string.expandtabs(text) if self.replace_whitespace: if type(text) == type(''): text = string.translate(text, self.whitespace_trans) elif isinstance(text, unicode): text = string.translate(text, self.unicode_whitespace_trans) return text
def scrub_asm(asm): # Scrub runs of whitespace out of the assembly, but leave the leading # whitespace in place. asm = ASM_SCRUB_WHITESPACE_RE.sub(r' ', asm) # Expand the tabs used for indentation. asm = string.expandtabs(asm, 2) # Detect shuffle asm comments and hide the operands in favor of the comments. asm = ASM_SCRUB_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm) # Generically match the stack offset of a memory operand. asm = ASM_SCRUB_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) # Generically match a RIP-relative memory operand. asm = ASM_SCRUB_RIP_RE.sub(r'{{.*}}(%rip)', asm) # Strip kill operands inserted into the asm. asm = ASM_SCRUB_KILL_COMMENT_RE.sub('', asm) return asm
def _flush_adjusted_lines(self): stripspace = None self._reset_multi_line_flags() for entry in self.line_buffer: if self._in_multi_line(entry): self.stream.write(entry + "\n") else: entry = string.expandtabs(entry) if stripspace is None and re.search(r"^[ \t]*[^# \t]", entry): stripspace = re.match(r"^([ \t]*)", entry).group(1) self.stream.write(self._indent_line(entry, stripspace) + "\n") self.line_buffer = [] self._reset_multi_line_flags()