当前位置: 首页>>代码示例>>Python>>正文


Python escape.recursive_unicode函数代码示例

本文整理汇总了Python中tornado.escape.recursive_unicode函数的典型用法代码示例。如果您正苦于以下问题:Python recursive_unicode函数的具体用法?Python recursive_unicode怎么用?Python recursive_unicode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了recursive_unicode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _assertJsonStructuresEqualsRecursive

    def _assertJsonStructuresEqualsRecursive(self, a, b, path, msg):
        a_type = unicode if type(a) == str else type(a)
        b_type = unicode if type(b) == str else type(b)

        self.assertEqual(
            a_type, b_type,
            self._format_msg_and_path('Types are not equal: {} != {}'.format(type(a), type(b)), msg, path)
        )

        if isinstance(a, list):
            self.assertEqual(
                len(a), len(b),
                self._format_msg_and_path('Lists lengths are not equal: {} != {}'.format(len(a), len(b)), msg, path)
            )

            for i in xrange(len(a)):
                self._assertJsonStructuresEqualsRecursive(a[i], b[i], path + '[{}]'.format(i), msg)

        elif isinstance(a, dict):
            a_keys = sorted(a.keys())
            b_keys = sorted(b.keys())

            self.assertEqual(
                a_keys, b_keys,
                self._format_msg_and_path('Dict keys are not equal: {} != {}'.format(a_keys, b_keys), msg, path)
            )

            for key in a_keys:
                self._assertJsonStructuresEqualsRecursive(a[key], b[key], '.'.join(filter(None, (path, key))), msg)

        else:
            self.assertEqual(
                recursive_unicode(a), recursive_unicode(b),
                self._format_msg_and_path('Values are not equal: {!r} != {!r}'.format(a, b), msg, path)
            )
开发者ID:strogo,项目名称:frontik,代码行数:35,代码来源:json_asserts.py

示例2: post

	def post(self):
		habrachat_cookie = self.get_cookie("habrachat")
		if not habrachat_cookie:
			habrachat_cookie = _session_id()
			self.set_cookie("habrachat", habrachat_cookie)

		token = self.get_argument("token", None)
		if not token:
			log.warning("Not have Token")
			self.finish()
			return
		client = httpclient.AsyncHTTPClient()
		response = yield client.fetch(
			"http://u-login.com/token.php?token=%s&host=%s://%s" % (token, self.request.protocol, self.request.host),
			use_gzip=True
		)
		if response.code != 200:
			log.warning("Not have access to u-login")
			self.finish()
			return

		json_response = json_decode(response.body)
		if "error_type" in json_response:
			log.warning("Error auth: %s" % json_response["error_message"])
			self.finish()
			return

		json_response = json_decode(response.body)
		if "error" in json_response:
			log.warning("Error auth: %s" % json_response["error"])
			self.finish()
			return

		identity = json_response.get("identity")
		if not identity:
			log.error("Not have indentity! json: %s" % json_response)
		log.info("New user indetity: %s" % identity)
		user_id = hashlib.md5(utf8(identity)).hexdigest()
		new_user = {"id": user_id, "name": None}

		new_user_name = ""
		if "nickname" in json_response:
			new_user_name = json_response.get("nickname", "")
		if not new_user["name"] and "first_name" in json_response:
			new_user_name = json_response.get("first_name", "")

		new_user["name"] = new_user_name[:20].replace("[", "{").replace("]", "}").encode('UTF-8')

		new_user["avatar"] = json_response.get("photo")
		new_user["ismoderator"] = identity in options.moderators

		old_user_settings = yield tornado.gen.Task(self.redis.get, "setting_"+user_id)
		if not old_user_settings:
			new_user_settings = {
				"revert_chat_order": False,
				"send_message_enter": False
			}
			yield tornado.gen.Task(self.redis.set, "setting_"+user_id,  json_encode(recursive_unicode(new_user_settings)))
		yield tornado.gen.Task(self.redis.set, habrachat_cookie,  json_encode(recursive_unicode(new_user)))
		self.redirect("/")
开发者ID:ve1ikiy,项目名称:habrachat,代码行数:60,代码来源:habrachat.py

示例3: get

	def get(self):
		if self.get_argument('code', False):
			token = yield self.get_authenticated_user(
				redirect_uri='%s://%s/google_auth' % (self.request.protocol, self.request.host),
				code=self.get_argument('code'))

			client = httpclient.AsyncHTTPClient()
			response = yield client.fetch(
				"https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=%s" % (token["access_token"]),
				use_gzip=True
			)
			if response.code != 200:
				log.warning("Not have access to google")
				self.finish()
				return

			user = json_decode(response.body)

			habrachat_cookie = self.get_cookie("habrachat")
			if not habrachat_cookie:
				habrachat_cookie = _session_id()
				self.set_cookie("habrachat", habrachat_cookie)
			identity = user.get("link")
			if not identity:
				log.error("Not have indentity! json: %s" % user)
			log.info("New user indetity: %s" % identity)
			user_id = hashlib.md5(utf8(identity)).hexdigest()
			new_user = {"id": user_id, "name": None}
			if "username" in user:
				new_user["name"] = user.get("username").encode('UTF-8')
			if not new_user["name"] and "name" in user:
				new_user["name"] = user.get("name").encode('UTF-8')

			new_user["name"] = new_user["name"][:20].replace("[", "{").replace("]", "}")
			new_user["avatar"] = user.get("picture", "")
			new_user["ismoderator"] = identity in options.moderators

			redis_client = self.redis()
			old_user_settings = yield tornado.gen.Task(redis_client.get, "setting_"+user_id)
			if not old_user_settings:
				new_user_settings = {
					"revert_chat_order": False,
					"send_message_enter": False
				}
				yield tornado.gen.Task(redis_client.set, "setting_"+user_id,  json_encode(recursive_unicode(new_user_settings)))

			yield tornado.gen.Task(redis_client.set, habrachat_cookie,  json_encode(recursive_unicode(new_user)))
			self.redirect("/")
		else:
			yield self.authorize_redirect(
				redirect_uri='%s://%s/google_auth' % (self.request.protocol, self.request.host),
				client_id=self.settings['google_oauth']['key'],
				scope=['profile', 'email'],
				response_type='code',
				extra_params={'approval_prompt': 'auto'})
开发者ID:stalkerg,项目名称:habrachat,代码行数:55,代码来源:habrachat.py

示例4: test_recursive_unicode

 def test_recursive_unicode(self):
     tests = {
         "dict": {b"foo": b"bar"},
         "list": [b"foo", b"bar"],
         "tuple": (b"foo", b"bar"),
         "bytes": b"foo",
     }
     self.assertEqual(recursive_unicode(tests["dict"]), {u"foo": u"bar"})
     self.assertEqual(recursive_unicode(tests["list"]), [u"foo", u"bar"])
     self.assertEqual(recursive_unicode(tests["tuple"]), (u"foo", u"bar"))
     self.assertEqual(recursive_unicode(tests["bytes"]), u"foo")
开发者ID:bdarnell,项目名称:tornado,代码行数:11,代码来源:escape_test.py

示例5: test_recursive_unicode

 def test_recursive_unicode(self):
     tests = {
         'dict': {b"foo": b"bar"},
         'list': [b"foo", b"bar"],
         'tuple': (b"foo", b"bar"),
         'bytes': b"foo"
     }
     self.assertEqual(recursive_unicode(tests['dict']), {u"foo": u"bar"})
     self.assertEqual(recursive_unicode(tests['list']), [u"foo", u"bar"])
     self.assertEqual(recursive_unicode(tests['tuple']), (u"foo", u"bar"))
     self.assertEqual(recursive_unicode(tests['bytes']), u"foo")
开发者ID:leeclemens,项目名称:tornado,代码行数:11,代码来源:escape_test.py

示例6: get

 def get(self):
     global id
     p = user.userbase_reader.get_proxy(id)#.ice_ping()
     p.ice_ping()
     print os.getpid(), id
     id += 1
     self.set_header("Content-Type", "text/html; charset=UTF-8")
     self.write('%s' % recursive_unicode(self.request.arguments))
开发者ID:pedia,项目名称:stuff,代码行数:8,代码来源:fork_test.py

示例7: unpack_bytes

 def unpack_bytes(self, obj_bytes, encoding=None):
     """Unpack a byte stream into a dictionary."""
     assert self.bytes_to_dict or self.string_to_dict
     encoding = encoding or self.default_encoding
     LOGGER.debug('%r decoding %d bytes with encoding of %s',
                  self, len(obj_bytes), encoding)
     if self.bytes_to_dict:
         return escape.recursive_unicode(self.bytes_to_dict(obj_bytes))
     return self.string_to_dict(obj_bytes.decode(encoding))
开发者ID:dave-shawley,项目名称:glinda,代码行数:9,代码来源:content.py

示例8: json_encode

def json_encode(value, **kwargs):
    """JSON-encodes the given Python object."""
    # JSON permits but does not require forward slashes to be escaped.
    # This is useful when json data is emitted in a <script> tag
    # in HTML, as it prevents </script> tags from prematurely terminating
    # the javscript.  Some json libraries do this escaping by default,
    # although python's standard library does not, so we do it here.
    # http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped
    return json.dumps(recursive_unicode(value), **kwargs).replace("</", "<\\/")
开发者ID:cloudorz,项目名称:apple,代码行数:9,代码来源:escape.py

示例9: CreateMessage

def CreateMessage(token, alert=None, badge=None, sound=None,
                  identifier=0, expiry=None, extra=None, allow_truncate=True):
  token = TokenToBinary(token)
  if len(token) != 32:
    raise ValueError, u'Token must be a 32-byte binary string.'
  if (alert is not None) and (not isinstance(alert, (basestring, dict))):
    raise ValueError, u'Alert message must be a string or a dictionary.'
  if expiry is None:
    expiry = long(time.time() + 365 * 86400)

  # Start by determining the length of the UTF-8 encoded JSON with no alert text. This allows us to
  # determine how much space is left for the message.
  # 'content-available': 1 is necessary to trigger iOS 7's background download processing.
  aps = { 'alert' : '', 'content-available': 1 }
  if badge is not None:
    aps['badge'] = badge
  if sound is not None:
    aps['sound'] = sound

  data = { 'aps' : aps }
  if extra is not None:
    data.update(extra)

  # Create compact JSON representation with no extra space and no escaping of non-ascii chars (i.e. use
  # direct UTF-8 representation rather than "\u1234" escaping). This maximizes the amount of space that's
  # left for the alert text.
  encoded = escape.utf8(json.dumps(escape.recursive_unicode(data), separators=(',', ':'), ensure_ascii=False))
  bytes_left = _MAX_PAYLOAD_BYTES - len(encoded)
  if allow_truncate and isinstance(alert, basestring):
    alert = _TruncateAlert(alert, bytes_left)
  elif alert and len(escape.utf8(alert)) > bytes_left:
    raise ValueError, u'max payload(%d) exceeded: %d' % (_MAX_PAYLOAD_BYTES, len(escape.utf8(alert)))

  # Now re-encode including the alert text.
  aps['alert'] = alert
  encoded = escape.utf8(json.dumps(escape.recursive_unicode(data), separators=(',', ':'), ensure_ascii=False))
  length = len(encoded)
  assert length <= _MAX_PAYLOAD_BYTES, (encoded, length)

  return struct.pack('!bIIH32sH%(length)ds' % { 'length' : length },
                     1, identifier, expiry,
                     32, token, length, encoded)
开发者ID:00zhengfu00,项目名称:viewfinder,代码行数:42,代码来源:apns_util.py

示例10: get

 def get(self, path):
     # Type checks: web.py interfaces convert argument values to
     # unicode strings (by default, but see also decode_argument).
     # In httpserver.py (i.e. self.request.arguments), they're left
     # as bytes.  Keys are always native strings.
     for key in self.request.arguments:
         assert type(key) == str, repr(key)
         for value in self.request.arguments[key]:
             assert type(value) == bytes_type, repr(value)
         for value in self.get_arguments(key):
             assert type(value) == str, repr(value)
     assert type(path) == str, repr(path)
     self.write(dict(path=path, args=recursive_unicode(self.request.arguments)))
开发者ID:e1ven,项目名称:Waymoot,代码行数:13,代码来源:web_test.py

示例11: get

 def get(self, *path_args):
     # Type checks: web.py interfaces convert argument values to
     # unicode strings (by default, but see also decode_argument).
     # In httpserver.py (i.e. self.request.arguments), they're left
     # as bytes.  Keys are always native strings.
     for key in self.request.arguments:
         if type(key) != str:
             raise Exception("incorrect type for key: %r" % type(key))
         for value in self.request.arguments[key]:
             if type(value) != bytes_type:
                 raise Exception("incorrect type for value: %r" % type(value))
         for value in self.get_arguments(key):
             if type(value) != unicode:
                 raise Exception("incorrect type for value: %r" % type(value))
     for arg in path_args:
         if type(arg) != unicode:
             raise Exception("incorrect type for path arg: %r" % type(arg))
     self.write(dict(path=self.request.path, path_args=path_args, args=recursive_unicode(self.request.arguments)))
开发者ID:nickwong,项目名称:tornado,代码行数:18,代码来源:web_test.py

示例12: _TruncateAlert

def _TruncateAlert(alert, max_bytes):
  """Converts the alert text to UTF-8 encoded JSON format, which is how
  the alert will be stored in the APNS payload. If the number of
  resulting bytes exceeds "max_bytes", then truncates the alert text
  at a Unicode character boundary, taking care not to split JSON
  escape sequences. Returns the truncated UTF-8 encoded alert text,
  including a trailing ellipsis character.
  """
  alert_json = escape.utf8(json.dumps(escape.recursive_unicode(alert), ensure_ascii=False))

  # Strip quotes added by JSON.
  alert_json = alert_json[1:-1]

  # Check if alert fits with no truncation.
  if len(alert_json) <= max_bytes:
    return escape.utf8(alert)

  # Make room for an appended ellipsis.
  assert max_bytes >= len(_ELLIPSIS_BYTES), 'max_bytes must be at least %d' % len(_ELLIPSIS_BYTES)
  max_bytes -= len(_ELLIPSIS_BYTES)

  # Truncate the JSON UTF8 string at a Unicode character boundary.
  truncated = alert_json[:max_bytes].decode('utf-8', errors='ignore')

  # If JSON escape sequences were split, then the truncated string may not be valid JSON. Keep
  # chopping trailing characters until the truncated string is valid JSON. It may take several
  # tries, such as in the case where a "\u1234" sequence has been split.
  while True:
    try:
      alert = json.loads(u'"%s"' % truncated)
      break
    except Exception:
      truncated = truncated[:-1]

  # Return the UTF-8 encoding of the alert with the ellipsis appended to it.
  return escape.utf8(alert) + _ELLIPSIS_BYTES
开发者ID:00zhengfu00,项目名称:viewfinder,代码行数:36,代码来源:apns_util.py

示例13: post

 def post(self):
     self.write(recursive_unicode(self.request.arguments))
开发者ID:alexdxy,项目名称:tornado,代码行数:2,代码来源:httpserver_test.py

示例14: _TestTruncate

 def _TestTruncate(alert, max_bytes, expected):
   truncated = _TruncateAlert(alert, max_bytes)
   truncated_json = escape.utf8(json.dumps(escape.recursive_unicode(truncated), ensure_ascii=False)[1:-1])
   self.assertEqual(truncated_json, expected)
   self.assertTrue(len(truncated_json) <= max_bytes)
开发者ID:00zhengfu00,项目名称:viewfinder,代码行数:5,代码来源:apns_test.py

示例15: json_encode

def json_encode(value):
    return json.dumps(recursive_unicode(value), cls=JSONDateTimeEncoder).replace("</", "<\\/")
开发者ID:cloudorz,项目名称:apple2,代码行数:2,代码来源:escape.py


注:本文中的tornado.escape.recursive_unicode函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。