def test_charmapencode(self): if test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=303938"): return # For charmap encodings the replacement string will be # mapped through the encoding again. This means, that # to be able to use e.g. the "replace" handler, the # charmap has to have a mapping for "?". charmap = dict([ (ord(c), 2*c.upper()) for c in "abcdefgh"]) sin = u"abc" sout = "AABBCC" self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout) sin = u"abcA" self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap) charmap[ord("?")] = "XYZ" sin = u"abcDEF" sout = "AABBCCXYZXYZXYZ" self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout) charmap[ord("?")] = u"XYZ" self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap) charmap[ord("?")] = u"XYZ" self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
def test_charmap_decode_1(self): import codecs assert codecs.charmap_encode(u'xxx') == ('xxx', 3) assert codecs.charmap_encode(u'xxx', 'strict', {ord('x'): 'XX'}) == ('XXXXXX', 3) res = codecs.charmap_decode("\x00\x01\x02", "replace", u"ab") assert res == (u"ab\ufffd", 3) res = codecs.charmap_decode("\x00\x01\x02", "replace", u"ab\ufffe") assert res == (u'ab\ufffd', 3)
def test_charmap_decode_1(self): import codecs assert codecs.charmap_encode(u"xxx") == ("xxx", 3) assert codecs.charmap_encode(u"xxx", "strict", {ord("x"): "XX"}) == ("XXXXXX", 3) res = codecs.charmap_decode("\x00\x01\x02", "replace", u"ab") assert res == (u"ab\ufffd", 3) res = codecs.charmap_decode("\x00\x01\x02", "replace", u"ab\ufffe") assert res == (u"ab\ufffd", 3)
def getPayload( self ): print self.challenge hash = password( self.passwd, self.challenge ) packet = "0" + DELIM1 + self.nick + DELIM1 + "6" + DELIM1 + codecs.charmap_encode(hash[ 0 ])[0] + DELIM1 + "96" + \ DELIM1 + codecs.charmap_encode(hash[ 1 ])[0] + DELIM1 + "2" + DELIM1 + "1" + DELIM1 + "1" + DELIM1 + \ self.nick + DELIM1 printPacket( packet ) return packet
def test_charmap_encode_replace(self): charmap = dict([(c, bytes([c, c]).upper()) for c in b"abcdefgh"]) charmap[ord("?")] = b"XYZ" import codecs sin = "abcDEF" sout = codecs.charmap_encode(sin, "replace", charmap)[0] assert sout == b"AABBCCXYZXYZXYZ"
def doExecute( self, sock, words ): # where is the song file? server = self.config.getString( "playing", "server" ) filename = self.config.getString( "playing", "file" ) # get the file from the server conn = httplib.HTTPConnection( server ) conn.request( "GET", filename ) response = conn.getresponse() if response.status != 200: raise Command.CommandError( "Could not access song file" ) file = response.read() # parse the XML dom = xml.dom.minidom.parseString(file) node = dom.getElementsByTagName( "media" )[0] artist = node.getElementsByTagName("artist")[0].firstChild.nodeValue.strip() song = node.getElementsByTagName( "title" )[0].firstChild.nodeValue.strip() album = node.getElementsByTagName( "album" )[0].firstChild.nodeValue.strip() year = node.getElementsByTagName( "year" )[0].firstChild.nodeValue.strip() # show it msg = "%(artist)s: %(song)s [%(album)s, %(year)s]" % locals() msg = codecs.charmap_encode(msg)[0] self.sendMessage( sock, msg )
def test_charmap_encode_replace(self): charmap = dict([ (ord(c), 2*c.upper()) for c in "abcdefgh"]) charmap[ord("?")] = "XYZ" import codecs sin = u"abcDEF" sout = codecs.charmap_encode(sin, "replace", charmap)[0] assert sout == "AABBCCXYZXYZXYZ"
def test_charmap_encode(self): #Sanity self.assertEqual(codecs.charmap_encode("abc"), ('abc', 3)) self.assertEqual(codecs.charmap_encode("abc", "strict"), ('abc', 3)) self.assertEqual(codecs.charmap_encode("", "strict", {}), ('', 0)) charmap = dict([ (ord(c), c.upper()) for c in "abcdefgh"]) self.assertEqual(codecs.charmap_encode(u"abc", "strict", charmap), ('ABC', 3)) #Sanity Negative self.assertRaises(UnicodeEncodeError, codecs.charmap_encode, "abc", "strict", {})
def test_charmapencode(self): # For charmap encodings the replacement string will be # mapped through the encoding again. This means, that # to be able to use e.g. the "replace" handler, the # charmap has to have a mapping for "?". charmap = dict((ord(c), bytes(2*c.upper(), 'ascii')) for c in "abcdefgh") sin = "abc" sout = b"AABBCC" self.assertEquals(codecs.charmap_encode(sin, "strict", charmap)[0], sout) sin = "abcA" self.assertRaises(UnicodeError, codecs.charmap_encode, sin, "strict", charmap) charmap[ord("?")] = b"XYZ" sin = "abcDEF" sout = b"AABBCCXYZXYZXYZ" self.assertEquals(codecs.charmap_encode(sin, "replace", charmap)[0], sout) charmap[ord("?")] = "XYZ" # wrong type in mapping self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
def _encode(input,errors='strict'): # split to see if we have any 'extended' characters runs=unicode_splitter.split(input) # now iterate through handling any 'multibyte' ourselves out_str=list() consumed=0 extended=extended_encode_map.keys() for run in runs: if len(run)==1 and run[0] in extended: out_str.append(extended_indicator+extended_encode_map[run]) consumed+=1 else: # pass it to the standard encoder out,cons=codecs.charmap_encode(run,errors,encoding_table) out_str.append(out) consumed+=cons return (''.join(out_str),consumed)
def encode(self, input, errors="strict"): return codecs.charmap_encode(input, errors, encoding_table)
def encode(self, char, final=False): return codecs.charmap_encode(char, self.errors, ENCODING_MAP)[0]
""" Python Character Mapping Codec generated from 'CP852.TXT' with gencodec.py.
def encode(self, input, final=False): data, nbytes = codecs.charmap_encode(input, self.errors, encoding_map) return data
def encode(self, input, final = False): return codecs.charmap_encode(input, self.errors, self.mapping)[0]
def encode(self, input, final=False): return codecs.charmap_encode(input, self.errors, self.mapping)[0]
""" Python Character Mapping Codec generated from 'CP860.TXT' with gencodec.py.
def encode(self, input, final = False): return codecs.charmap_encode(input, self.errors, encoding_table)[0]
#!/usr/bin/env python3 # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Error handling with character map encodings. """ #end_pymotw_header import codecs from codecs_invertcaps_charmap import encoding_map text = 'pi: \u03c0' for error in ['ignore', 'replace', 'strict']: try: encoded = codecs.charmap_encode(text, error, encoding_map) except UnicodeEncodeError as err: encoded = str(err) print('{:7}: {}'.format(error, encoded))
""" Python Character Mapping Codec generated from 'CP857.TXT' with gencodec.py.
def encode(self,input,errors='strict'): return codecs.charmap_encode(input,errors,encoding_table)
def encode(self, input, final=False): return codecs.charmap_encode(input,self.errors,encmap_hd44780_a02)[0]
def encode_a02(self,input,errors='strict'): return codecs.charmap_encode(input,errors,encmap_hd44780_a02)
def test_charmap_build(self): decodemap = ''.join([unichr(i).upper() if chr(i).islower() else unichr(i).lower() for i in xrange(256)]) encodemap = codecs.charmap_build(decodemap) self.assertEqual(codecs.charmap_decode(u'Hello World', 'strict', decodemap), ('hELLO wORLD', 11)) self.assertEqual(codecs.charmap_encode(u'Hello World', 'strict', encodemap), ('hELLO wORLD', 11))
""" Python Character Mapping Codec generated from 'CP500.TXT' with gencodec.py.
""" Python Character Mapping Codec generated from 'CP1257.TXT' with gencodec.py.
def encode(self, _input, errors='strict'): return codecs.charmap_encode(_input, errors, encoding_map)
""" Python Character Mapping Codec generated from 'KOI8-R.TXT' with gencodec.py.
""" Python Character Mapping Codec generated from 'CP1006.TXT' with gencodec.py.
# # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Character mapping encoder """ #end_pymotw_header import codecs import string # Map every character to itself decoding_map = codecs.make_identity_dict(range(256)) # Make a list of pairs of ordinal values for the lower and upper case # letters pairs = zip([ ord(c) for c in string.ascii_lowercase], [ ord(c) for c in string.ascii_uppercase]) # Modify the mapping to convert upper to lower and lower to upper. decoding_map.update( dict( (upper, lower) for (lower, upper) in pairs) ) decoding_map.update( dict( (lower, upper) for (lower, upper) in pairs) ) # Create a separate encoding map. encoding_map = codecs.make_encoding_map(decoding_map) if __name__ == '__main__': print codecs.charmap_encode('abc.DEF', 'strict', encoding_map) print codecs.charmap_decode('abc.DEF', 'strict', decoding_map) print encoding_map == decoding_map
""" Python Character Mapping Codec generated from 'CP424.TXT' with gencodec.py.
def encode(self, input, errors="strict"): return codecs.charmap_encode(input, errors, encoding_map)
""" Python Character Mapping Codec generated from 'CP1256.TXT' with gencodec.py.
#!/usr/bin/python # -*- coding: utf-8 -*- # # Copyright (c) 2008 Doug Hellmann All rights reserved. # import codecs import string # Map every character to itself decoding_map = codecs.make_identity_dict(range(256)) # Make a list of pairs of ordinal values for the lower and upper case # letters pairs = zip([ord(c) for c in string.ascii_lowercase], [ord(c) for c in string.ascii_uppercase]) # Modify the mapping to convert upper to lower and lower to upper. decoding_map.update(dict((upper, lower) for (lower, upper) in pairs)) decoding_map.update(dict((lower, upper) for (lower, upper) in pairs)) # Create a separate encoding map. encoding_map = codecs.make_encoding_map(decoding_map) if __name__ == '__main__': print codecs.charmap_encode('abc.DEF', 'strict', encoding_map) print codecs.charmap_decode('abc.DEF', 'strict', decoding_map) print encoding_map == decoding_map
""" Python Character Mapping Codec generated from '8859-9.TXT' with gencodec.py.
""" Python Character Mapping Codec generated from '8859-10.TXT' with gencodec.py.
def encode(self, _input, final=False): return codecs.charmap_encode(_input, self.errors, encoding_map)[0]
def encode(self, char, errors='strict'): return codecs.charmap_encode(char, errors, ENCODING_MAP)
def encode(self, input, errors=error_handling): return codecs.charmap_encode(input, errors, encoding_map)
# Map every character to itself decoding_map = codecs.make_identity_dict( xrange(256) ) # Make a list of pairs of ordinal values for all lcase and ucase letters pairs = zip( [ord(c) for c in string.ascii_lowercase], [ord(c) for c in string.ascii_uppercase] ) # modify the mapping to convert upper to lower and lower to upper decoding_map.update( dict( (upper, lower) for (lower, upper) in pairs ) ) decoding_map.update( dict( (lower, upper) for (lower, upper) in pairs ) ) # Create a separate encoding map encoding_map = codecs.make_encoding_map( decoding_map ) print (codecs.charmap_encode('abc.DEF', error_handling, encoding_map)) print (codecs.charmap_decode('abc.DEF', error_handling, decoding_map)) print encoding_map == decoding_map print # by default, char map encoders and decoders support the standard error methods # since this charmap only includes [a-zA-Z], the u"pi: π" from earlier fails for error in ['ignore', 'replace', 'strict']: try: encoded = codecs.charmap_encode(data, error, encoding_map) except UnicodeEncodeError, err: encoded = str(err) print '{:7} {}'.format(error, encoded) print # After defining a en/decoding maps, a few additonal classes have to be set up
""" Python Character Mapping Codec generated from 'ICELAND.TXT' with gencodec.py.
#!/usr/bin/env python # -*- coding: utf-8 -*- import codecs from codecs_invertcaps_charmap import encoding_map text = u'pi: π' for error in [ 'ignore', 'replace', 'strict' ]: try: encoded = codecs.charmap_encode(text, error, encoding_map) except UnicodeEncodeError, err: encoded = str(err) print '{:7}: {}'.format(error, encoded)
""" Python Character Mapping Codec generated from 'CP875.TXT' with gencodec.py.
""" Python Character Mapping Codec generated from '8859-8.TXT' with gencodec.py.
def encode(self,input,errors='strict'): return codecs.charmap_encode(input,errors,encoding_map)
""" Python Character Mapping Codec generated from 'LATIN2.TXT' with gencodec.py.