我们从Python开源项目中,提取了以下47个代码示例,用于说明如何使用string.maketrans()。
def _ebcdic_to_ascii(self, s): c = self.__class__ if not c.EBCDIC_TO_ASCII_MAP: emap = (0,1,2,3,156,9,134,127,151,141,142,11,12,13,14,15, 16,17,18,19,157,133,8,135,24,25,146,143,28,29,30,31, 128,129,130,131,132,10,23,27,136,137,138,139,140,5,6,7, 144,145,22,147,148,149,150,4,152,153,154,155,20,21,158,26, 32,160,161,162,163,164,165,166,167,168,91,46,60,40,43,33, 38,169,170,171,172,173,174,175,176,177,93,36,42,41,59,94, 45,47,178,179,180,181,182,183,184,185,124,44,37,95,62,63, 186,187,188,189,190,191,192,193,194,96,58,35,64,39,61,34, 195,97,98,99,100,101,102,103,104,105,196,197,198,199,200, 201,202,106,107,108,109,110,111,112,113,114,203,204,205, 206,207,208,209,126,115,116,117,118,119,120,121,122,210, 211,212,213,214,215,216,217,218,219,220,221,222,223,224, 225,226,227,228,229,230,231,123,65,66,67,68,69,70,71,72, 73,232,233,234,235,236,237,125,74,75,76,77,78,79,80,81, 82,238,239,240,241,242,243,92,159,83,84,85,86,87,88,89, 90,244,245,246,247,248,249,48,49,50,51,52,53,54,55,56,57, 250,251,252,253,254,255) import string c.EBCDIC_TO_ASCII_MAP = string.maketrans( \ ''.join(map(chr, range(256))), ''.join(map(chr, emap))) return s.translate(c.EBCDIC_TO_ASCII_MAP)
def op(gen, d): if isinstance(d, int): if d == 0: return opcodes.OP_0 if d == -1 or 1 <= d <= 16: return d + opcodes.OP_1 - 1 # Hmm, maybe time to switch to Python 3 with int.from_bytes? h = "00%x" % (d if d >= 0 else -1-d) if len(h) % 2: h = h[1:] elif h[2] < '8': h = h[2:] if d < 0: import string h = h.translate(string.maketrans('0123456789abcdef', 'fedcba9876543210')) return h.decode('hex') raise ValueError(n)
def removeCommonWords(self, sentence, common_words, tokenized=False): """Takes a sentence and list of stopwords and removes the stopwords from the sentence.""" if not tokenized: words = sentence.split(' ') else: words = sentence final_sentence = [] for word in words: word = word.translate(string.maketrans("", ""), string.punctuation) word = word.lower() if word in common_words: continue else: final_sentence.append(word) return final_sentence
def beautify(uglypath, table, delchars='', stringfunc=None): """Make three changes to a name in an ugly path. The changes are (1) apply a string function, (2) translate characters, and (3) delete characters. >>> table = string.maketrans('', '') >>> beautify('/foo/bar/a"b)c]d e.txt', table, UGLYCHARS) '/foo/bar/abcde.txt' >>> beautify("03 - Blue 'n' Boogie.mp3", table, UGLYCHARS) '03-BluenBoogie.mp3' >>> beautify("My Document #3 - (2005)[1].txt", table, UGLYCHARS) 'MyDocument3-20051.txt' >>> beautify('a_b-c', table, UGLYCHARS, string.upper) 'A_B-C' """ dirname, ugly2pretty, ext = split_dir_base_ext(uglypath) if stringfunc is not None: ugly2pretty = stringfunc(ugly2pretty) # Translate FROMCHARS to TOCHARS and delete DELCHARS ugly2pretty = ugly2pretty.translate(table, delchars) return os.path.join(dirname, ugly2pretty+ext)
def solve(s): '''Find solutions to alphametic equations. >>> solve('SEND + MORE == MONEY') 9567 + 1085 == 10652 ''' words = findall('[A-Za-z]+', s) chars = set(''.join(words)) # characters to be substituted assert len(chars) <= 10 # there are only ten possible digits firsts = set(w[0] for w in words) # first letters of each of word chars = ''.join(firsts) + ''.join(chars - firsts) n = len(firsts) # chars[:n] cannot be assigned zero for perm in permutations('0123456789', len(chars)): if '0' not in perm[:n]: trans = maketrans(chars, ''.join(perm)) equation = s.translate(trans) try: if eval(equation): print equation except ArithmeticError: pass
def get_environment_id(default_ccv): translation_table = string.maketrans('-','_') CONVERT_CCV = DEFAULT_CONTENT_VIEW.translate(translation_table) CONVERT_ORGANIZATION = ORGANIZATION.translate(translation_table) PUPPET_ENV = str("KT_" + CONVERT_ORGANIZATION + "_" + ENVIRONMENT + "_" + CONVERT_CCV) cmd_get_environment_id = hammer_cmd + " --csv environment list" try: perform_cmd = subprocess.Popen(cmd_get_environment_id, shell=True, stdout=subprocess.PIPE) puppet_env_id = perform_cmd.stdout.read() for line in islice(puppet_env_id.strip().split("\n"), 1, None): # print output without CSV header if PUPPET_ENV in line: return line.split(",")[0] break except: print log.ERROR + "ERROR: Puppet environment id not found. Please ensure that the Puppet environment " + PUPPET_ENV + " is configured properly in Satellite." + log.END sys.exit(1)
def is_text(s, threshold=0.3): """ Determine whether a certain string is text or arbitrary bytes. This is derived from Python Cookbook :param s: string, input string :param threshold: float, threshold for the max proportion in a string which can be null translates :return: """ import string text_characters = "".join(map(chr, range(32, 127)))+"\n\r\t\b" _null_trans = string.maketrans("", "") if "\0" in s: return False if not s: return True t = s.translate(_null_trans, text_characters) return len(t)/len(s) <= threshold
def __init__(self,fastafile=None): '''initialize object, lowercase in sequence is automatically converted into uppercase''' self.seqs={} self.IDs=[] self.transtab = maketrans("ACGTNX","TGCANX") self.filename = fastafile tmpseq='' if fastafile is not None: for line in open(fastafile,'r'): line=line.strip(' \n') if line.startswith('>'): if(tmpseq): self.seqs[name]=tmpseq name=line[1:] tmpseq ='' self.IDs.append(name) print >>sys.stderr,"\tloading "+name+' ...' else: tmpseq += line.upper() self.seqs[name]=tmpseq
def assemble_cloudfront_request(resource, key, access_id, expires): """Assemble a CloudFront request.""" # Format a request policy for the resource request_policy = { "Statement": [{"Resource": resource, "Condition": {"DateLessThan": {"AWS:EpochTime": expires}}}] } request_policy = json.dumps(request_policy).replace(' ', '') # Sign and encode request policy signature = base64.b64encode(sign(key, request_policy, 'RSA-SHA1')) # Replace unsafe characters signature = signature.translate(maketrans('+=/', '-_~')) # Format the final request URL cloudfront_request = ("{0}?Expires={1}&Signature={2}&Key-Pair-Id={3}" .format(resource, expires, signature, access_id)) return cloudfront_request
def b64decode(s, altchars=None): """Decode a Base64 encoded string. s is the string to decode. Optional altchars must be a string of at least length 2 (additional characters are ignored) which specifies the alternative alphabet used instead of the '+' and '/' characters. The decoded string is returned. A TypeError is raised if s were incorrectly padded or if there are non-alphabet characters present in the string. """ if altchars is not None: s = s.translate(string.maketrans(altchars[:2], '+/')) try: return binascii.a2b_base64(s) except binascii.Error, msg: # Transform this exception for consistency raise TypeError(msg)
def get_memory_facts(self): # Get free memory. vmstat output looks like: # procs memory page disks traps cpu # r b w avm fre flt re pi po fr sr wd0 fd0 int sys cs us sy id # 0 0 0 47512 28160 51 0 0 0 0 0 1 0 116 89 17 0 1 99 rc, out, err = self.module.run_command("/usr/bin/vmstat") if rc == 0: self.facts['memfree_mb'] = int(out.splitlines()[-1].split()[4]) // 1024 self.facts['memtotal_mb'] = int(self.sysctl['hw.usermem']) // 1024 // 1024 # Get swapctl info. swapctl output looks like: # total: 69268 1K-blocks allocated, 0 used, 69268 available # And for older OpenBSD: # total: 69268k bytes allocated = 0k used, 69268k available rc, out, err = self.module.run_command("/sbin/swapctl -sk") if rc == 0: swaptrans = maketrans(' ', ' ') data = out.split() self.facts['swapfree_mb'] = int(data[-2].translate(swaptrans, "kmg")) // 1024 self.facts['swaptotal_mb'] = int(data[1].translate(swaptrans, "kmg")) // 1024
def getCorpus(): documents = [] txtNames = glob.glob("original/*.txt") for fileName in txtNames: fp = open(fileName) buf = fp.readline() documents.append(buf) stoplist = set('for a of the and to in at'.split()) texts = [[word for word in document.translate(string.maketrans("", ""), string.punctuation).lower().split() if word not in stoplist] for document in documents] #Actually dictionary and corpus are of no use here dictionary = corpora.Dictionary(texts) dictionary.filter_extremes(no_below=10, no_above=0.7, keep_n=50000) dictionary.save('tmp/imdb.dict') corpus = [dictionary.doc2bow(text) for text in texts] corpora.MmCorpus.serialize('tmp/imdb.mm', corpus) return texts
def do_simple_substitution(ciphertext, pt_charset, ct_charset): ''' Perform simple substitution based on character sets Simplifies the use of string.translate(). If, for instance, you wish to transform a ciphertext where 'e' is swapped with 't', you would call this function like so: do_simple_substitution('Simplt subeieueion ciphtrs art silly','et','te') ciphertext - A string to translate pt_charset - The character set of the plaintext, usually 'abcdefghijk...xyz' ct_charset - The character set of the ciphertext ''' #translate ciphertext to plaintext using mapping return string.translate(ciphertext, string.maketrans(ct_charset, pt_charset)) # TODO: Implement chi square
def morse_decode(text, dot='.', dash='-', space=' '): ''' Decodes a Morse encoded message. Optionally, you can provide an alternate single character for dot, dash, and space. Parameters: text - (string) A message to decode dot - (char) An alternate dot char dash - (char) An alternate dash char space - (char) A char to split the text on ''' inverse_morse_table = map(lambda (x,y): (y,x), morse_table.items()) dot_dash_trans = string.maketrans('.-', dot+dash) inverse_morse_table = [(string.translate(x,dot_dash_trans), y) for (x,y) in inverse_morse_table] inverse_morse_table = dict(inverse_morse_table) return ''.join([inverse_morse_table[char] for char in text.split(space) if char in inverse_morse_table.keys()])
def _sanitize(self, text): # removing duplicated spaces text = ' '.join(text.split()) # removing digits text = ''.join([c for c in text if not c.isdigit()]) # removing accents text = unidecode(text) # removnig punctuations text = text.translate( string.maketrans("-'", ' ')).translate(None, string.punctuation) # remove uppercase text = text.lower() return text
def test_trivial(self): # A couple trivial tests self.assertRaises(ValueError, urllib2.urlopen, 'bogus url') # XXX Name hacking to get this to work on Windows. fname = os.path.abspath(urllib2.__file__).replace(os.sep, '/') # And more hacking to get it to work on MacOS. This assumes # urllib.pathname2url works, unfortunately... if os.name == 'riscos': import string fname = os.expand(fname) fname = fname.translate(string.maketrans("/.", "./")) if os.name == 'nt': file_url = "file:///%s" % fname else: file_url = "file://%s" % fname f = urllib2.urlopen(file_url) buf = f.read() f.close()
def b64decode(s, altchars=None): """Decode a Base64 encoded string. s is the string to decode. Optional altchars must be a string of at least length 2 (additional characters are ignored) which specifies the alternative alphabet used instead of the '+' and '/' characters. The decoded string is returned. A TypeError is raised if s is incorrectly padded. Characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. """ if altchars is not None: s = s.translate(string.maketrans(altchars[:2], '+/')) try: return binascii.a2b_base64(s) except binascii.Error, msg: # Transform this exception for consistency raise TypeError(msg)
def makeMask(self, value): # TODO -- we really need to know the size of the key to make the mask properly, # but to find that, we need to parse the headers and header_types from the json if value.startswith("0x"): mask = "F" value = value[2:] prefix = "0x" elif value.startswith("0b"): mask = "1" value = value[2:] prefix = "0b" elif value.startswith("0o"): mask = "7" value = value[2:] prefix = "0o" else: raise Exception("Decimal value "+value+" not supported for ternary key") return value values = "0123456789abcdefABCDEF*" replacements = (mask * 22) + "0" trans = maketrans(values, replacements) m = value.translate(trans) return prefix + value.replace("*", "0") + "&&&" + prefix + m
def _ebcdic_to_ascii(self, s): c = self.__class__ if not c.EBCDIC_TO_ASCII_MAP: emap = (0,1,2,3,156,9,134,127,151,141,142,11,12,13,14,15, 16,17,18,19,157,133,8,135,24,25,146,143,28,29,30,31, 128,129,130,131,132,10,23,27,136,137,138,139,140,5,6,7, 144,145,22,147,148,149,150,4,152,153,154,155,20,21,158,26, 32,160,161,162,163,164,165,166,167,168,91,46,60,40,43,33, 38,169,170,171,172,173,174,175,176,177,93,36,42,41,59,94, 45,47,178,179,180,181,182,183,184,185,124,44,37,95,62,63, 186,187,188,189,190,191,192,193,194,96,58,35,64,39,61,34, 195,97,98,99,100,101,102,103,104,105,196,197,198,199,200, 201,202,106,107,108,109,110,111,112,113,114,203,204,205, 206,207,208,209,126,115,116,117,118,119,120,121,122,210, 211,212,213,214,215,216,217,218,219,220,221,222,223,224, 225,226,227,228,229,230,231,123,65,66,67,68,69,70,71,72, 73,232,233,234,235,236,237,125,74,75,76,77,78,79,80,81, 82,238,239,240,241,242,243,92,159,83,84,85,86,87,88,89, 90,244,245,246,247,248,249,48,49,50,51,52,53,54,55,56,57, 250,251,252,253,254,255) import string c.EBCDIC_TO_ASCII_MAP = string.maketrans( \ ''.join(map(chr, xrange(256))), ''.join(map(chr, emap))) return s.translate(c.EBCDIC_TO_ASCII_MAP)
def search(pairs, squares): hits = set() for Len in pairs: candis = pairs[Len] spaces = squares[Len] for w1, w2 in candis: for sqs in spaces: numsqs = int(sqs) if numsqs in hits: continue check = match(w1, sqs) if not check: continue table = maketrans(w1, sqs) trans = w2.translate(table) if trans in spaces: hits.add(numsqs) hits.add(int(trans)) return max(hits)
def normalize_jxns(chrom1, chrom2, pos1, pos2, strand1, strand2, repleft, repright, order): '''Choose one representation for DNA breakpoints''' flipstr = maketrans("-+", "+-") if order == 2: if strand1 == "-": new_pos1 = str(chrom1) + ":" + str(pos1 - int(repright)) + ":" + strand1.translate(flipstr) else: new_pos1 = str(chrom1) + ":" + str(pos1 + int(repright)) + ":" + strand1.translate(flipstr) if strand2 == "-": new_pos2 = str(chrom2) + ":" + str(pos2 - int(repright)) + ":" + strand2.translate(flipstr) else: new_pos2 = str(chrom2) + ":" + str(pos2 + int(repright)) + ":" + strand2.translate(flipstr) newid = new_pos2 + ":" + new_pos1 + ":" + str(repleft) + ":" + str(repright) elif order == 1: new_pos1 = str(chrom1) + ":" + str(pos1) + ":" + strand1 new_pos2 = str(chrom2) + ":" + str(pos2) + ":" + strand2 newid = new_pos1 + ":" + new_pos2 + ":" + str(repleft) + ":" + str(repright) return newid
def flip_jxn(jxn, gs1): '''Flip jxn orientation for RNA Fusion that is inverse according to strand info''' chrom1, pos1, str1, chrom2, pos2, str2, repleft, repright = re.split(':', jxn) chrom1 = str(chrom1) chrom2 = str(chrom2) pos1 = int(pos1) pos2 = int(pos2) if str1 != gs1[0]: flip = 1 flipstr = maketrans("-+", "+-") if str1 == "-": new_pos1 = chrom1 + ":" + str(pos1) + ":" + str1.translate(flipstr) else: new_pos1 = chrom1 + ":" + str(pos1) + ":" + str1.translate(flipstr) if str2 == "-": new_pos2 = chrom2 + ":" + str(pos2) + ":" + str2.translate(flipstr) else: new_pos2 = chrom2 + ":" + str(pos2) + ":" + str2.translate(flipstr) newid = new_pos2 + ":" + new_pos1 + ":" + str(repright) + ":" + str(repleft) else: newid = jxn flip = 0 return (newid, flip)
def isIsomorphic(self, s, t): if len(s) != len(t): return False for i in range(len(s)): if occurence(s, s[i]) != occurence(t, t[i]): return False return True # def isIsomorphic(self, s, t): # if len(s) != len(t): # return False # lens = len(s) # s_dict = '' # t_dict = '' # for idx, ele in enumerate(s): # if ele not in s_dict: # s_dict += ele # t_dict += t[idx] # translated = s.translate(maketrans(s_dict, t_dict)) # print translated, t # if translated == t: # return True # return False
def _ebcdic_to_ascii(s): global _ebcdic_to_ascii_map if not _ebcdic_to_ascii_map: emap = ( 0,1,2,3,156,9,134,127,151,141,142,11,12,13,14,15, 16,17,18,19,157,133,8,135,24,25,146,143,28,29,30,31, 128,129,130,131,132,10,23,27,136,137,138,139,140,5,6,7, 144,145,22,147,148,149,150,4,152,153,154,155,20,21,158,26, 32,160,161,162,163,164,165,166,167,168,91,46,60,40,43,33, 38,169,170,171,172,173,174,175,176,177,93,36,42,41,59,94, 45,47,178,179,180,181,182,183,184,185,124,44,37,95,62,63, 186,187,188,189,190,191,192,193,194,96,58,35,64,39,61,34, 195,97,98,99,100,101,102,103,104,105,196,197,198,199,200,201, 202,106,107,108,109,110,111,112,113,114,203,204,205,206,207,208, 209,126,115,116,117,118,119,120,121,122,210,211,212,213,214,215, 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231, 123,65,66,67,68,69,70,71,72,73,232,233,234,235,236,237, 125,74,75,76,77,78,79,80,81,82,238,239,240,241,242,243, 92,159,83,84,85,86,87,88,89,90,244,245,246,247,248,249, 48,49,50,51,52,53,54,55,56,57,250,251,252,253,254,255 ) import string _ebcdic_to_ascii_map = string.maketrans( \ ''.join(map(chr, range(256))), ''.join(map(chr, emap))) return s.translate(_ebcdic_to_ascii_map)
def test_trivial(self): # A couple trivial tests self.assertRaises(ValueError, urllib2.urlopen, 'bogus url') # XXX Name hacking to get this to work on Windows. fname = os.path.abspath(urllib2.__file__).replace('\\', '/') # And more hacking to get it to work on MacOS. This assumes # urllib.pathname2url works, unfortunately... if os.name == 'riscos': import string fname = os.expand(fname) fname = fname.translate(string.maketrans("/.", "./")) if os.name == 'nt': file_url = "file:///%s" % fname else: file_url = "file://%s" % fname f = urllib2.urlopen(file_url) buf = f.read() f.close()
def get_ext_fullpath(self, ext_name): """Returns the path of the filename for a given extension. The file is located in `build_lib` or directly in the package (inplace option). """ # makes sure the extension name is only using dots all_dots = string.maketrans('/'+os.sep, '..') ext_name = ext_name.translate(all_dots) fullname = self.get_ext_fullname(ext_name) modpath = fullname.split('.') filename = self.get_ext_filename(ext_name) filename = os.path.split(filename)[-1] if not self.inplace: # no further work needed # returning : # build_dir/package/path/filename filename = os.path.join(*modpath[:-1]+[filename]) return os.path.join(self.build_lib, filename) # the inplace option requires to find the package directory # using the build_py command for that package = '.'.join(modpath[0:-1]) build_py = self.get_finalized_command('build_py') package_dir = os.path.abspath(build_py.get_package_dir(package)) # returning # package_dir/filename return os.path.join(package_dir, filename)
def test(args=[]): if not args: args = [ '/etc/passwd', 'file:/etc/passwd', 'file://localhost/etc/passwd', 'ftp://ftp.gnu.org/pub/README', 'http://www.python.org/index.html', ] if hasattr(URLopener, "open_https"): args.append('https://synergy.as.cmu.edu/~geek/') try: for url in args: print '-'*10, url, '-'*10 fn, h = urlretrieve(url, None, reporthook) print fn if h: print '======' for k in h.keys(): print k + ':', h[k] print '======' with open(fn, 'rb') as fp: data = fp.read() if '\r' in data: table = string.maketrans("", "") data = data.translate(table, "\r") print data fn, h = None, None print '-'*40 finally: urlcleanup()
def __caesar(self, plaintext, shift): lower = string.ascii_lowercase lower_trans = lower[shift:] + lower[:shift] alphabet = lower + lower.upper() shifted = lower_trans + lower_trans.upper() return plaintext.translate(string.maketrans(alphabet, shifted))
def _ConvertToValidName(name): """Converts to name that we can use as a kubernetes job prefix. Args: name: benchmark name. Returns: Benchmark name that can be used as a kubernetes job prefix. """ return name.translate(maketrans('/:_', '---'))
def emit_text(self, text): """Emit straight text to the file""" if self.timeout is not None: if select.select([],[self.handle],[], self.timeout) == ([],[],[]): raise IOError("Timed out waiting for write") self.handle.write( "\x1e%s\n" % (text.translate(string.maketrans('', ''), "\n")) ) self.handle.flush()
def encode(plaintext, code): "Encodes text, using a code which is a permutation of the alphabet." from string import maketrans trans = maketrans(alphabet + alphabet.upper(), code + code.upper()) return plaintext.translate(trans)
def rev_comp(string): """Outputs reverse complement of a nucleotide sequence""" if sys.version_info.major == 2: import string as st complement = string.translate(st.maketrans('ACGT', 'TGCA')) else: complement = string.translate(str.maketrans('ACGT', 'TGCA')) return complement[::-1]
def _ebcdic_to_ascii(self, s): c = self.__class__ if not c.EBCDIC_TO_ASCII_MAP: emap = (0,1,2,3,156,9,134,127,151,141,142,11,12,13,14,15, 16,17,18,19,157,133,8,135,24,25,146,143,28,29,30,31, 128,129,130,131,132,10,23,27,136,137,138,139,140,5,6,7, 144,145,22,147,148,149,150,4,152,153,154,155,20,21,158,26, 32,160,161,162,163,164,165,166,167,168,91,46,60,40,43,33, 38,169,170,171,172,173,174,175,176,177,93,36,42,41,59,94, 45,47,178,179,180,181,182,183,184,185,124,44,37,95,62,63, 186,187,188,189,190,191,192,193,194,96,58,35,64,39,61,34, 195,97,98,99,100,101,102,103,104,105,196,197,198,199,200, 201,202,106,107,108,109,110,111,112,113,114,203,204,205, 206,207,208,209,126,115,116,117,118,119,120,121,122,210, 211,212,213,214,215,216,217,218,219,220,221,222,223,224, 225,226,227,228,229,230,231,123,65,66,67,68,69,70,71,72, 73,232,233,234,235,236,237,125,74,75,76,77,78,79,80,81, 82,238,239,240,241,242,243,92,159,83,84,85,86,87,88,89, 90,244,245,246,247,248,249,48,49,50,51,52,53,54,55,56,57, 250,251,252,253,254,255) import string c.EBCDIC_TO_ASCII_MAP = string.maketrans( ''.join(map(chr, list(range(256)))), ''.join(map(chr, emap))) return s.translate(c.EBCDIC_TO_ASCII_MAP) # A partial mapping of ISO-Latin-1 to HTML entities/XML numeric entities.
def fix_ugly_names(opts, uglydir): """Rename ugly file names to a beautified shell-correct names. Collect ugly file names, perform beautification, rename. """ # Shell-unfriendly characters made into a string. The user-provided # FROMCHARS and TOCHARS must be removed from the UGLYCHARS so that # they can be transformed instead of removed. delchars = opts.delchars + ''.join(set(UGLYCHARS) - set(opts.tochars+opts.fromchars)) # Table for later translation (removal of `delchars`). table = string.maketrans(opts.fromchars, opts.tochars) uglyroot = os.path.expanduser(uglydir) # Build list of name-change candidates. if opts.recurse: uglies = [f for root , _, _ in os.walk(uglyroot, topdown=False) for f in glob.glob(os.path.join(root, opts.limitglob))] else: uglies = glob.glob(os.path.join(uglyroot, opts.limitglob)) pretties = [beautify(ugly, table, delchars, opts.stringfunc) for ugly in uglies] # Do the renaming. for ugly, pretty in zip(uglies, pretties): if ugly != pretty: if not opts.silent: print ugly, '-->', pretty if not opts.dryrun: os.rename(ugly, pretty)
def __init__(self, frm='', to='', delete='', keep=None): if len(to) == 1: to = to * len(frm) self.trans = string.maketrans(frm, to) if keep is None: self.delete = delete else: self.delete = self.allchars.translate(self.allchars, keep.translate(self.allchars, delete))