Python string.maketrans() Examples
The following are 30
code examples of string.maketrans().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
string
, or try the search function
.
Example #1
Source File: CTMagic.py From CapTipper with GNU General Public License v3.0 | 6 votes |
def istext(self,cont): # Based on http://code.activestate.com/recipes/173220/ import string text_characters = "".join(map(chr, range(32, 127)) + list("\n\r\t\b")) _null_trans = string.maketrans("", "") if not cont: # Empty files are considered text return True if "\0" in cont: # Files with null bytes are likely binary return False # Get the non-text characters (maps a character to itself then # use the 'remove' option to get rid of the text characters.) t = cont.translate(_null_trans, text_characters) # If more than 30% non-text characters, then # this is considered a binary file if float(len(t))/float(len(cont)) > 0.30: return False return True
Example #2
Source File: base64.py From ironpython2 with Apache License 2.0 | 6 votes |
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)
Example #3
Source File: classical.py From featherduster with BSD 3-Clause "New" or "Revised" License | 6 votes |
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()])
Example #4
Source File: strings_utils.py From locality-sensitive-hashing with MIT License | 6 votes |
def normalize(str): """ Normalizes the string making string all lower case and removes all punctuation. :param str: string to be normalized :return: normalized string, if str is None or empty it returns the original string """ if str: if isinstance(str, unicode): not_letters_or_digits = u'!"#%\'()*+,-./:;<=>?@[\]^_`{|}~' translate_to = u'' translate_table = dict((ord(char), translate_to) for char in not_letters_or_digits) return str.translate(translate_table) else: return str.lower().translate(string.maketrans("",""), string.punctuation) else: return str
Example #5
Source File: _bsoup.py From faces with GNU General Public License v2.0 | 6 votes |
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)
Example #6
Source File: text.py From lambda-packs with MIT License | 6 votes |
def text_to_word_sequence(text, filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n', lower=True, split=' '): """Converts a text to a sequence of word indices. Arguments: text: Input text (string). filters: Sequence of characters to filter out. lower: Whether to convert the input to lowercase. split: Sentence split marker (string). Returns: A list of integer word indices. """ if lower: text = text.lower() text = text.translate(maketrans(filters, split * len(filters))) seq = text.split(split) return [i for i in seq if i]
Example #7
Source File: __init__.py From arnold-usd with Apache License 2.0 | 6 votes |
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)
Example #8
Source File: baseDriver.py From BioQueue with Apache License 2.0 | 6 votes |
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
Example #9
Source File: misc.py From pixelworld with MIT License | 6 votes |
def is_text(filename): s=open(filename).read(512) text_characters = "".join(map(chr, range(32, 127)) + list("\n\r\t\b")) _null_trans = string.maketrans("", "") if not s: # Empty files are considered text return True if "\0" in s: # Files with null bytes are likely binary return False # Get the non-text characters (maps a character to itself then # use the 'remove' option to get rid of the text characters.) t = s.translate(_null_trans, text_characters) # If more than 30% non-text characters, then # this is considered a binary file if float(len(t))/float(len(s)) > 0.30: return False return True
Example #10
Source File: helpers.py From featherduster with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #11
Source File: BeautifulSoup.py From nightmare with GNU General Public License v2.0 | 6 votes |
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)
Example #12
Source File: BeautifulSoup.py From xbmc-betaseries with GNU General Public License v2.0 | 6 votes |
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)
Example #13
Source File: sport365.py From bugatsinho.github.io with GNU General Public License v3.0 | 6 votes |
def derot(xset, hset, src): xset = eval(xset) hset = eval(hset) import string o = '' u = '' il = 0 for first in hset: u += first o += xset[il] il += 1 rot13 = string.maketrans(o, u) link = string.translate(src, rot13) xbmc.log('@#@DEROT-LINK: %s' % link, xbmc.LOGNOTICE) return link
Example #14
Source File: BeautifulSoup.py From xbmc-betaseries with GNU General Public License v2.0 | 6 votes |
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)
Example #15
Source File: test_urllib2.py From ironpython2 with Apache License 2.0 | 6 votes |
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()
Example #16
Source File: test_urllib2.py From BinderFilter with MIT License | 6 votes |
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()
Example #17
Source File: newbytes.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def maketrans(cls, frm, to): """ B.maketrans(frm, to) -> translation table Return a translation table (a bytes object of length 256) suitable for use in the bytes or bytearray translate method where each byte in frm is mapped to the byte at the same position in to. The bytes objects frm and to must be of the same length. """ return newbytes(string.maketrans(frm, to))
Example #18
Source File: classical.py From featherduster with BSD 3-Clause "New" or "Revised" License | 5 votes |
def morse_encode(text, dot='.', dash='-', space=' '): ''' Encodes text into Morse code. ''' dot_dash_trans = string.maketrans('.-', dot+dash) translated_morse_table = map(lambda (x,y): (x, string.translate(y, dot_dash_trans)), morse_table.items()) translated_morse_table = dict(translated_morse_table) output = [] for char in text.lower(): if char in string.lowercase + string.digits: output.append(translated_morse_table[char]) return space.join(output)
Example #19
Source File: text.py From aima with MIT License | 5 votes |
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)
Example #20
Source File: vgslspecs.py From Gun-Detector with Apache License 2.0 | 5 votes |
def __init__(self, widths, heights, is_training): """Constructs a VGSLSpecs. Args: widths: Tensor of size batch_size of the widths of the inputs. heights: Tensor of size batch_size of the heights of the inputs. is_training: True if the graph should be build for training. """ # The string that was used to build this model. self.model_str = None # True if we are training self.is_training = is_training # Tensor for the size of the images, of size batch_size. self.widths = widths self.heights = heights # Overall reduction factors of this model so far for each dimension. # TODO(rays) consider building a graph from widths and heights instead of # computing a scale factor. self.reduction_factors = [1.0, 1.0, 1.0, 1.0] # List of Op parsers. # TODO(rays) add more Op types as needed. self.valid_ops = [self.AddSeries, self.AddParallel, self.AddConvLayer, self.AddMaxPool, self.AddDropout, self.AddReShape, self.AddFCLayer, self.AddLSTMLayer] # Translation table to convert unacceptable characters that may occur # in op strings that cannot be used as names. self.transtab = maketrans('(,)', '___')
Example #21
Source File: text.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def text_to_word_sequence(text, filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n', lower=True, split=" "): """Converts a text to a sequence of words (or tokens). # Arguments text: Input text (string). filters: list (or concatenation) of characters to filter out, such as punctuation. Default: ``!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n``, includes basic punctuation, tabs, and newlines. lower: boolean. Whether to convert the input to lowercase. split: str. Separator for word splitting. # Returns A list of words (or tokens). """ if lower: text = text.lower() if sys.version_info < (3,): if isinstance(text, unicode): translate_map = dict((ord(c), unicode(split)) for c in filters) text = text.translate(translate_map) elif len(split) == 1: translate_map = maketrans(filters, split * len(filters)) text = text.translate(translate_map) else: for c in filters: text = text.replace(c, split) else: translate_dict = dict((c, split) for c in filters) translate_map = maketrans(translate_dict) text = text.translate(translate_map) seq = text.split(split) return [i for i in seq if i]
Example #22
Source File: text.py From robocup-soccer with MIT License | 5 votes |
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)
Example #23
Source File: preprocessing.py From deepWordBug with Apache License 2.0 | 5 votes |
def text_to_word_sequence(text, filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n', lower=True, split=" "): """Converts a text to a sequence of words (or tokens). # Arguments text: Input text (string). filters: list (or concatenation) of characters to filter out, such as punctuation. Default: ``!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\\t\\n``, includes basic punctuation, tabs, and newlines. lower: boolean. Whether to convert the input to lowercase. split: str. Separator for word splitting. # Returns A list of words (or tokens). """ if lower: text = text.lower() if sys.version_info < (3,): if isinstance(text, unicode): translate_map = dict((ord(c), unicode(split)) for c in filters) text = text.translate(translate_map) elif len(split) == 1: translate_map = maketrans(filters, split * len(filters)) text = text.translate(translate_map) else: for c in filters: text = text.replace(c, split) else: translate_dict = dict((c, split) for c in filters) translate_map = maketrans(translate_dict) text = text.translate(translate_map) seq = text.split(split) return [i for i in seq if i]
Example #24
Source File: dammit.py From crunchy-xml-decoder with GNU General Public License v2.0 | 5 votes |
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.
Example #25
Source File: test_string.py From oss-ftp with MIT License | 5 votes |
def test_maketrans(self): transtable = '\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377' self.assertEqual(string.maketrans('abc', 'xyz'), transtable) self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzq')
Example #26
Source File: string_tests.py From oss-ftp with MIT License | 5 votes |
def test_translate(self): table = string.maketrans('abc', 'xyz') self.checkequal('xyzxyz', 'xyzabcdef', 'translate', table, 'def') table = string.maketrans('a', 'A') self.checkequal('Abc', 'abc', 'translate', table) self.checkequal('xyz', 'xyz', 'translate', table) self.checkequal('yz', 'xyz', 'translate', table, 'x') self.checkequal('yx', 'zyzzx', 'translate', None, 'z') self.checkequal('zyzzx', 'zyzzx', 'translate', None, '') self.checkequal('zyzzx', 'zyzzx', 'translate', None) self.checkraises(ValueError, 'xyz', 'translate', 'too short', 'strip') self.checkraises(ValueError, 'xyz', 'translate', 'too short')
Example #27
Source File: string_tests.py From oss-ftp with MIT License | 5 votes |
def test_maketrans(self): self.assertEqual( ''.join(map(chr, xrange(256))).replace('abc', 'xyz'), string.maketrans('abc', 'xyz') ) self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzw')
Example #28
Source File: test_bigmem.py From oss-ftp with MIT License | 5 votes |
def test_translate(self, size): trans = string.maketrans('.aZ', '-!$') SUBSTR = 'aZz.z.Aaz.' sublen = len(SUBSTR) repeats = size // sublen + 2 s = SUBSTR * repeats s = s.translate(trans) self.assertEqual(len(s), repeats * sublen) self.assertEqual(s[:sublen], SUBSTR.translate(trans)) self.assertEqual(s[-sublen:], SUBSTR.translate(trans)) self.assertEqual(s.count('.'), 0) self.assertEqual(s.count('!'), repeats * 2) self.assertEqual(s.count('z'), repeats * 3)
Example #29
Source File: build_ext.py From oss-ftp with MIT License | 5 votes |
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)
Example #30
Source File: build_ext.py From Computable with MIT License | 5 votes |
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)