def test_unicode_escape_decode_errors(self): from _codecs import unicode_escape_decode, raw_unicode_escape_decode for decode in [unicode_escape_decode, raw_unicode_escape_decode]: for c, d in ('u', 4), ('U', 4): for i in range(d): raises(UnicodeDecodeError, decode, "\\" + c + "0" * i) raises(UnicodeDecodeError, decode, "[\\" + c + "0" * i + "]") data = "[\\" + c + "0" * i + "]\\" + c + "0" * i assert decode(data, "ignore") == (u"[]", len(data)) assert decode(data, "replace") == (u"[\ufffd]\ufffd", len(data)) raises(UnicodeDecodeError, decode, r"\U00110000") assert decode(r"\U00110000", "ignore") == (u"", 10) assert decode(r"\U00110000", "replace") == (u"\ufffd", 10) exc = raises(UnicodeDecodeError, unicode_escape_decode, b"\u1z32z3", 'strict') assert str( exc.value ) == r"'unicodeescape' codec can't decode bytes in position 0-2: truncated \uXXXX escape" exc = raises(UnicodeDecodeError, raw_unicode_escape_decode, b"\u1z32z3", 'strict') assert str( exc.value ) == r"'rawunicodeescape' codec can't decode bytes in position 0-2: truncated \uXXXX" exc = raises(UnicodeDecodeError, raw_unicode_escape_decode, b"\U1z32z3", 'strict') assert str( exc.value ) == r"'rawunicodeescape' codec can't decode bytes in position 0-2: truncated \uXXXX"
def test_escape_decode_errors(self): from _codecs import escape_decode as decode raises(ValueError, decode, br"\x") raises(ValueError, decode, br"[\x]") assert decode(br"[\x]\x", "ignore") == (b"[]", 6) assert decode(br"[\x]\x", "replace") == (b"[?]?", 6) raises(ValueError, decode, br"\x0") raises(ValueError, decode, br"[\x0]") assert decode(br"[\x0]\x0", "ignore") == (b"[]", 8) assert decode(br"[\x0]\x0", "replace") == (b"[?]?", 8)
def int_to_bytes(n, length): # Helper function """ Int/long to byte string. Python 3.2+ has a built-in int.to_bytes() method that could be used instead, but the following works in earlier versions including 2.x. """ return decode('%%0%dx' % (length << 1) % n, 'hex')[-length:]
def process_related(self, request, user_profile, input_data, template_args, **kwargs): """ Displays and manages the item related_url behavior Her is the switch for external services integration """ # check if related-url is set # and switch depending on the service it represents # it's much more like an embed # # check for url node = kwargs['node'] if node.related_url: if node.related_url.startswith('https://twitter.com') and input_data.get('keyword'): wk = TwitterWorker(user_profile, kwargs['node'], input_data, kwargs) items = wk.prepare() template_args['nodes'] = items #return self.render(request, template_args, **kwargs) elif node.related_url.startswith('http://www.meetup.com'): # and #input_data.get('keyword'): wk = MeetupWorker(user_profile, kwargs['node'], input_data, kwargs) items = wk.prepare() template_args['nodes'] = items #return self.render(request, template_args, **kwargs) elif node.related_url.startswith('https://googlecalendar.com'): pass elif node.related_url.startswith('https://hackpad.com/'): hackpad_id = node.related_url.split('-')[-1] print 'importing hackpad' # import hackpad content HACKPAD_CLIENT_ID = 'vTuK1ArKv5m' HACKPAD_CLIENT_SECRET = '5FuDkwdgc8Mo0y2OuhMijuzFfQy3ni5T' hackpad = Hackpad(consumer_key=HACKPAD_CLIENT_ID, consumer_secret=HACKPAD_CLIENT_SECRET) #node.description = '' #node.description = hackpad.get_pad_content(hackpad_id, asUser='', response_format='md') hackpad_content = hackpad.get_pad_content(hackpad_id, asUser='', response_format='md') #node.description = unicode(decode(hackpad_content, 'latin1')) try: node.get_translation().content = markdown_deux.markdown( unicode(decode(hackpad_content, 'latin1')) ) node.save() except: pass #print node.content return self.manage_item_pipe(request, user_profile, input_data, template_args, **kwargs)
def test_unicode_escape_decode_errors(self): from _codecs import unicode_escape_decode, raw_unicode_escape_decode for decode in [unicode_escape_decode, raw_unicode_escape_decode]: for c, d in ('u', 4), ('U', 4): for i in range(d): raises(UnicodeDecodeError, decode, "\\" + c + "0"*i) raises(UnicodeDecodeError, decode, "[\\" + c + "0"*i + "]") data = "[\\" + c + "0"*i + "]\\" + c + "0"*i assert decode(data, "ignore") == (u"[]", len(data)) assert decode(data, "replace") == (u"[\ufffd]\ufffd", len(data)) raises(UnicodeDecodeError, decode, r"\U00110000") assert decode(r"\U00110000", "ignore") == (u"", 10) assert decode(r"\U00110000", "replace") == (u"\ufffd", 10) exc = raises(UnicodeDecodeError, unicode_escape_decode, b"\u1z32z3", 'strict') assert str(exc.value) == r"'unicodeescape' codec can't decode bytes in position 0-2: truncated \uXXXX escape" exc = raises(UnicodeDecodeError, raw_unicode_escape_decode, b"\u1z32z3", 'strict') assert str(exc.value) == r"'rawunicodeescape' codec can't decode bytes in position 0-2: truncated \uXXXX" exc = raises(UnicodeDecodeError, raw_unicode_escape_decode, b"\U1z32z3", 'strict') assert str(exc.value) == r"'rawunicodeescape' codec can't decode bytes in position 0-2: truncated \uXXXX"
def decode(self, encoding="utf-8", errors="strict"): """Decode the bytes using the codec registered for encoding. encoding The encoding with which to decode the bytes. errors The error handling scheme to use for the handling of decoding errors. The default is 'strict' meaning that decoding errors raise a UnicodeDecodeError. Other possible values are 'ignore' and 'replace' as well as any other name registered with codecs.register_error that can handle UnicodeDecodeErrors. """ return _codecs.decode(self, encoding=encoding, errors=errors)
def main(): if len(sys.argv) == 2: calculator = StringCalculator() decoded_input = decode(sys.argv[1], 'unicode_escape') result = calculator.add(decoded_input) print(result) else: print( "Usage: python main.py //[delimiter]\\n[delimiter separated numbers]" ) sys.exit(1)
def test_one_arg_encoder(self): import _codecs def search_function(encoding): def encode_one(u): return (b'foo', len(u)) def decode_one(u): return (u'foo', len(u)) if encoding == 'onearg': return (encode_one, decode_one, None, None) return None _codecs.register(search_function) assert u"hello".encode("onearg") == b'foo' assert b"hello".decode("onearg") == u'foo' assert _codecs.encode(u"hello", "onearg") == b'foo' assert _codecs.decode(b"hello", "onearg") == u'foo'
def shell_command(cmd: str, print_output=True): """ Run a shell command and prints its output to stdout :param cmd: the shell command :param print_output: if True this will print the output, if false this will yield the output """ process = Popen(cmd, stdout=PIPE, stderr=STDOUT, shell=True) lines = [] for line in process.stdout: res = decode(line) if print_output: print(res) else: lines.append(res) return lines
def decode(self, encoding="utf-8", errors="strict"): """Decode the bytes using the codec registered for encoding. encoding The encoding with which to decode the bytes. errors The error handling scheme to use for the handling of decoding errors. The default is 'strict' meaning that decoding errors raise a UnicodeDecodeError. Other possible values are 'ignore' and 'replace' as well as any other name registered with codecs.register_error that can handle UnicodeDecodeErrors. """ result = _codecs.decode(self, encoding=encoding, errors=errors) if not isinstance(result, str): raise TypeError("'%s' encoder returned '%s' instead of 'str'; use codecs.decode() to decode to arbitrary types" % (encoding, type(result).__name__)) return result
s.connect((host, port)) except: print("Servidor incorrecto no disponible.") exit() """Envío de petición""" s.send(bytes(request,"UTF-8")) print("Recibiendo respuesta...") """Se recibe respuesta""" lineas = [] linea = "" while True: try: c = str(decode(s.recv(1),"latin-1")) s.settimeout(0.3) if c == "\n": linea = linea.replace("\r", "") lineas.insert(len(lineas), linea) linea = "" else: linea = linea +c except: break """Se procesa y muestra respuesta""" print("\n----------Respuesta") try: print("Versión de http: "+lineas[0].split(" ")[0].replace("HTTP/",""))
print u exit() a = "aaaa'aaaaa" a = a.replace("'", "\'") print a exit() str1 = decode(u'\u5580\u5e03\u5c14\xa0\xa0') print str1 exit() dictstr = {'url': [u'http://place.qyer.com/kabul/'], 'en': [u'Kabul'], 'name': [u'\u5580\u5e03\u5c14\xa0\xa0', u'\r\n '], 'img': [u'http://pic1.qyer.com/album/user/331/55/QkpURx8AaA/index/cover']} print dictstr exit() country = 'http://place.qyer.com/afghanistan/' country = country.strip('/') + '/1000'
def test_escape_decode(self): from _codecs import unicode_escape_decode as decode assert decode('\\\x80') == (u'\\\x80', 2)
def test_decode(): ''' ''' #sanity new_str = codecs.decode("abc") AreEqual(new_str, u'abc')
try: s.connect((host, port)) except: print("Servidor incorrecto no disponible.") exit() """Envío de petición""" s.send(bytes(request, "UTF-8")) print("Recibiendo respuesta...") """Se recibe respuesta""" lineas = [] linea = "" while True: try: c = str(decode(s.recv(1), "latin-1")) s.settimeout(0.3) if c == "\n": linea = linea.replace("\r", "") lineas.insert(len(lineas), linea) linea = "" else: linea = linea + c except: break """Se procesa y muestra respuesta""" print("\n----------Respuesta") try: print("Versión de http: " + lineas[0].split(" ")[0].replace("HTTP/", "")) print("Código de respuesta: " + lineas[0].split(" ")[1])
def msgToJSON(self, message, done=False): self.logger.debug('Encoding msg to json: message={}'.format( vars(message))) topic = message.delivery_info['routing_key'] if message.body[0] == '[': # early v03 message to persist, (message.pubtime, message.baseurl, message.relpath, headers) = json.loads(message.body) notice = "%s %s %s" % (message.pubtime, message.baseurl, message.relpath) elif message.body[0] == '{': # late v03 message to persist, headers = json.loads(message.body) message.version = 'v03' message.pubtime = headers["pubTime"] message.baseurl = headers["baseUrl"] message.relpath = headers["relPath"] notice = "%s %s %s" % (message.pubtime, message.baseurl, message.relpath) if 'integrity' in headers.keys(): # v3 has no sum, must add it here sum_algo_map = { "a": "arbitrary", "d": "md5", "s": "sha512", "n": "md5name", "0": "random", "L": "link", "R": "remove", "z": "cod" } sum_algo_map = {v: k for k, v in sum_algo_map.items()} sumstr = sum_algo_map[headers['integrity']['method']] if sumstr == '0': sumstr = '{},{}'.format(sumstr, headers['integrity']['value']) elif sumstr == 'z': sumstr = '{},{}'.format( sumstr, sum_algo_map[headers['integrity']['value']]) else: decoded_value = encode( decode(headers['integrity']['value'].encode('utf-8'), 'base64'), 'hex').decode('utf-8').strip() sumstr = '{},{}'.format(sumstr, decoded_value) headers['sum'] = sumstr if sumstr == 'R': message.event = 'delete' elif sumstr == 'L': message.event = 'remove' else: message.event = 'modify' del headers['integrity'] if 'size' in headers.keys(): parts_map = {'inplace': 'i', 'partitioned': 'p'} if 'blocks' not in headers.keys(): partstr = "%s,%s,%s,%s,%s" % ('1', headers['size'], '1', '0', '0') else: partstr = "%s,%s,%s,%s,%s" % ( parts_map[headers['blocks']['method']], headers['blocks']['size'], headers['blocks']['count'], headers['blocks']['remainder'], headers['blocks']['number']) del headers['blocks'] del headers['size'] headers['parts'] = partstr else: headers = message.properties['application_headers'] if type(message.body) == bytes: notice = message.body.decode("utf-8") else: notice = message.body message.version = 'v02' if 'sum' in headers: sumstr = headers['sum'][0] if sumstr == 'R': message.event = 'delete' elif sumstr == 'L': message.event = 'remove' else: message.event = 'modify' if done: headers['_retry_tag_'] = 'done' return json.dumps([topic, headers, notice], sort_keys=True) + '\n'
payment = product_infos_list[2].find_all('p')[1].get_text() result["repayment"] = payment except: payment = "notFound" try : status = soup.find('div', {'class':'purchase-status-text'}).get_text() status = PageUtils.replaceString(status) tmp = status.split(' ') # status = tmp[-1].trim() status = tmp[-1].strip() result["state"] = status # 状态 print "status==>" , status TimeInfo = '' if decode(status, 'utf8') == unicode("已下架"): # 则不在取数据(暂时) # percentCompleted = '100%' tmpTag = soup.find('div', {'class': 'purchase-status-process-start purchase-status-chance-start'}) if tmpTag != None: a = PageUtils.replaceString(tmpTag.get_text()) result["startSellDate"] = PageUtils.clearStateDate(a) TimeInfo += a # # End Time tmpTag = soup.find('div', {'class': 'purchase-status-process-start purchase-status-saledone-end'}) if tmpTag != None: a = PageUtils.replaceString(tmpTag.get_text()) result["endSellDate"] = PageUtils.clearStateDate(a) TimeInfo += a else: percentCompleted = soup.find('div', {'id':'J_Purchase_Process_Value'}).get_text() percent_float = float(percentCompleted[:-1])