本文整理汇总了Python中aspen.json.loads函数的典型用法代码示例。如果您正苦于以下问题:Python loads函数的具体用法?Python loads怎么用?Python loads使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了loads函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _set_data
def _set_data(self, data):
if self.type == 4: # json
data = json.loads(data)
elif self.type == 5: # event
data = json.loads(data)
if 'name' not in data:
raise ValueError("An event message must have a 'name' key.")
if 'args' not in data:
raise ValueError("An event message must have an 'args' key.")
if data['name'] in RESERVED_EVENTS:
msg = "That event name is reserved: %s." % data['name']
raise ValueError(msg)
self.__data = data
示例2: test_post_can_remove_member_from_team
def test_post_can_remove_member_from_team(self):
response = self.client.POST('/team/members/alice.json', {'take': '0.01'}, auth_as='team')
data = json.loads(response.body)
assert len(data) == 2
for rec in data:
assert rec['username'] in ('team', 'alice'), rec['username']
response = self.client.POST('/team/members/alice.json', {'take': '0.00'}, auth_as='team')
data = json.loads(response.body)
assert len(data) == 1
assert data[0]['username'] == 'team'
示例3: test_paydays_json_gives_paydays
def test_paydays_json_gives_paydays(self):
Payday.start()
self.make_participant("alice")
response = self.client.GET("/about/paydays.json")
paydays = json.loads(response.body)
assert paydays[0]['ntippers'] == 0
示例4: get_user_info
def get_user_info(login):
"""Get the given user's information from the DB or failing that, github.
:param login:
A unicode string representing a username in github.
:returns:
A dictionary containing github specific information for the user.
"""
typecheck(login, unicode)
rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
"WHERE platform='github' "
"AND user_info->'login' = %s"
, (login,)
)
if rec is not None:
user_info = rec['user_info']
else:
url = "https://api.github.com/users/%s"
user_info = requests.get(url % login, params={
'client_id': os.environ.get('GITHUB_CLIENT_ID'),
'client_secret': os.environ.get('GITHUB_CLIENT_SECRET')
})
status = user_info.status_code
content = user_info.text
# Calculate how much of our ratelimit we have consumed
remaining = int(user_info.headers['x-ratelimit-remaining'])
limit = int(user_info.headers['x-ratelimit-limit'])
# thanks to from __future__ import division this is a float
percent_remaining = remaining/limit
log_msg = ''
log_lvl = None
# We want anything 50% or over
if 0.5 <= percent_remaining:
log_msg = ("{0}% of GitHub's ratelimit has been consumed. {1}"
" requests remaining.").format(percent_remaining * 100,
remaining)
if 0.5 <= percent_remaining < 0.8:
log_lvl = logging.WARNING
elif 0.8 <= percent_remaining < 0.95:
log_lvl = logging.ERROR
elif 0.95 <= percent_remaining:
log_lvl = logging.CRITICAL
if log_msg and log_lvl:
log(log_msg, log_lvl)
if status == 200:
user_info = json.loads(content)
elif status == 404:
raise Response(404,
"GitHub identity '{0}' not found.".format(login))
else:
log("Github api responded with {0}: {1}".format(status, content),
level=logging.WARNING)
raise Response(502, "GitHub lookup failed with %d." % status)
return user_info
示例5: oauth_dance
def oauth_dance(website, qs):
"""Given a querystring, return a dict of user_info.
The querystring should be the querystring that we get from GitHub when
we send the user to the return value of oauth_url above.
See also:
http://developer.github.com/v3/oauth/
"""
log("Doing an OAuth dance with Github.")
data = {
"code": qs["code"].encode("US-ASCII"),
"client_id": website.github_client_id,
"client_secret": website.github_client_secret,
}
r = requests.post("https://github.com/login/oauth/access_token", data=data)
assert r.status_code == 200, (r.status_code, r.text)
back = dict([pair.split("=") for pair in r.text.split("&")]) # XXX
if "error" in back:
raise Response(400, back["error"].encode("utf-8"))
assert back.get("token_type", "") == "bearer", back
access_token = back["access_token"]
r = requests.get("https://api.github.com/user", headers={"Authorization": "token %s" % access_token})
assert r.status_code == 200, (r.status_code, r.text)
user_info = json.loads(r.text)
log("Done with OAuth dance with Github for %s (%s)." % (user_info["login"], user_info["id"]))
return user_info
示例6: test_post_can_add_member_to_team
def test_post_can_add_member_to_team(self):
response = self.client.POST('/team/members/alice.json', {'take': '0.01'}, auth_as=self.team)
data = json.loads(response.body)['members']
assert len(data) == 2
for rec in data:
assert rec['username'] in ('team', 'alice'), rec['username']
示例7: get_user_info
def get_user_info(screen_name):
"""Given a unicode, return a dict.
"""
typecheck(screen_name, unicode)
try:
rec = gittip.db.one( "SELECT user_info FROM elsewhere "
"WHERE platform='twitter' "
"AND user_info->'screen_name' = %s"
, (screen_name,)
)
except TooFew:
rec = None
if rec is not None:
user_info = rec['user_info']
else:
# Updated using Twython as a point of reference:
# https://github.com/ryanmcgrath/twython/blob/master/twython/twython.py#L76
oauth = OAuth1(
# we do not have access to the website obj,
# so let's grab the details from the env
environ['TWITTER_CONSUMER_KEY'],
environ['TWITTER_CONSUMER_SECRET'],
environ['TWITTER_ACCESS_TOKEN'],
environ['TWITTER_ACCESS_TOKEN_SECRET'],
)
url = "https://api.twitter.com/1.1/users/show.json?screen_name=%s"
user_info = requests.get(url % screen_name, auth=oauth)
# Keep an eye on our Twitter usage.
# =================================
rate_limit = user_info.headers['X-Rate-Limit-Limit']
rate_limit_remaining = user_info.headers['X-Rate-Limit-Remaining']
rate_limit_reset = user_info.headers['X-Rate-Limit-Reset']
try:
rate_limit = int(rate_limit)
rate_limit_remaining = int(rate_limit_remaining)
rate_limit_reset = int(rate_limit_reset)
except (TypeError, ValueError):
log( "Got weird rate headers from Twitter: %s %s %s"
% (rate_limit, rate_limit_remaining, rate_limit_reset)
)
else:
reset = datetime.datetime.fromtimestamp(rate_limit_reset, tz=utc)
reset = to_age(reset)
log( "Twitter API calls used: %d / %d. Resets %s."
% (rate_limit - rate_limit_remaining, rate_limit, reset)
)
if user_info.status_code == 200:
user_info = json.loads(user_info.text)
else:
log("Twitter lookup failed with %d." % user_info.status_code)
raise Response(404)
return user_info
示例8: test_a_custom_renderer
def test_a_custom_renderer(harness):
class TestRenderer(Renderer):
def compile(self, *a):
return self.raw.upper()
def render_content(self, context):
d = dict((k, v) for k, v in self.__dict__.items() if k[0] != '_')
return json.dumps(d)
class TestFactory(Factory):
Renderer = TestRenderer
def compile_meta(self, configuration):
return 'foobar'
website = harness.client.website
website.renderer_factories['lorem'] = TestFactory(website)
r = harness.simple("[---]\n[---] text/html via lorem\nLorem ipsum")
d = json.loads(r.body)
assert d['meta'] == 'foobar'
assert d['raw'] == 'Lorem ipsum'
assert d['media_type'] == 'text/html'
assert d['offset'] == 2
assert d['compiled'] == 'LOREM IPSUM'
示例9: test_participant_can_get_their_privacy_settings
def test_participant_can_get_their_privacy_settings(self):
response = self.hit_privacy('GET')
actual = json.loads(response.body)
assert actual == {
'is_searchable': True,
'anonymous_giving': False,
}
示例10: get_user_info
def get_user_info(db, username):
"""Get the given user's information from the DB or failing that, bitbucket.
:param username:
A unicode string representing a username in bitbucket.
:returns:
A dictionary containing bitbucket specific information for the user.
"""
typecheck(username, (unicode, PathPart))
rec = db.one(
"""
SELECT user_info FROM elsewhere
WHERE platform='bitbucket'
AND user_info->'username' = %s
""",
(username,),
)
if rec is not None:
user_info = rec
else:
url = "%s/users/%s?pagelen=100"
user_info = requests.get(url % (BASE_API_URL, username))
status = user_info.status_code
content = user_info.content
if status == 200:
user_info = json.loads(content)["user"]
elif status == 404:
raise Response(404, "Bitbucket identity '{0}' not found.".format(username))
else:
log("Bitbucket api responded with {0}: {1}".format(status, content), level=logging.WARNING)
raise Response(502, "Bitbucket lookup failed with %d." % status)
return user_info
示例11: get_user_info
def get_user_info(login):
"""Get the given user's information from the DB or failing that, github.
:param login:
A unicode string representing a username in github.
:returns:
A dictionary containing github specific information for the user.
"""
typecheck(login, unicode)
rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
"WHERE platform='github' "
"AND user_info->'login' = %s"
, (login,)
)
if rec is not None:
user_info = rec['user_info']
else:
url = "https://api.github.com/users/%s"
user_info = requests.get(url % login)
status = user_info.status_code
content = user_info.text
if status == 200:
user_info = json.loads(content)
elif status == 404:
raise Response(404,
"GitHub identity '{0}' not found.".format(login))
else:
log("Github api responded with {0}: {1}".format(status, content),
level=logging.WARNING)
raise Response(502, "GitHub lookup failed with %d." % status)
return user_info
示例12: test_get_team_members_returns_take_when_member
def test_get_team_members_returns_take_when_member(self):
response = self.client.POST('/team/members/alice.json', {'take': '0.01'}, auth_as='team')
assert response.code == 200
response = self.client.GET('/team/members/alice.json', auth_as='team')
data = json.loads(response.body)
assert response.code == 200
assert data['username'] == 'alice'
assert data['take'] == '0.01'
示例13: get_user_info
def get_user_info(screen_name):
"""Given a unicode, return a dict.
"""
typecheck(screen_name, unicode)
rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
"WHERE platform='twitter' "
"AND user_info->'screen_name' = %s"
, (screen_name,)
)
if rec is not None:
user_info = rec['user_info']
else:
oauth = OAuthHook(
# we haven't got access to the website obj,
# so let's grab the details from the env
access_token=environ['TWITTER_ACCESS_TOKEN'],
access_token_secret=environ['TWITTER_ACCESS_TOKEN_SECRET'],
consumer_key=environ['TWITTER_CONSUMER_KEY'],
consumer_secret=environ['TWITTER_CONSUMER_SECRET'],
header_auth=True
)
url = "https://api.twitter.com/1.1/users/show.json?screen_name=%s"
user_info = requests.get(url % screen_name, hooks={'pre_request': oauth})
# Keep an eye on our Twitter usage.
# =================================
rate_limit = user_info.headers['X-RateLimit-Limit']
rate_limit_remaining = user_info.headers['X-RateLimit-Remaining']
rate_limit_reset = user_info.headers['X-RateLimit-Reset']
try:
rate_limit = int(rate_limit)
rate_limit_remaining = int(rate_limit_remaining)
rate_limit_reset = int(rate_limit_reset)
except (TypeError, ValueError):
log( "Got weird rate headers from Twitter: %s %s %s"
% (rate_limit, rate_limit_remaining, rate_limit_reset)
)
else:
reset = datetime.datetime.fromtimestamp(rate_limit_reset, tz=utc)
reset = to_age(reset)
log( "Twitter API calls used: %d / %d. Resets %s."
% (rate_limit - rate_limit_remaining, rate_limit, reset)
)
if user_info.status_code == 200:
user_info = json.loads(user_info.text)
else:
log("Twitter lookup failed with %d." % user_info.status_code)
raise Response(404)
return user_info
示例14: gittip
def gittip(first, second, color):
first = "tips"
fp = urlopen("https://www.gittip.com/%s/public.json" % second)
receiving = float(json.loads(fp.read())['receiving'])
second = "$%d / week" % receiving
if receiving == 0:
color = RED
elif receiving < 10:
color = YELLOW
elif receiving < 100:
color = YELLOWGREEN
else:
color = GREEN
return first, second, color
示例15: get_user_info
def get_user_info(screen_name):
"""Given a unicode, return a dict.
"""
typecheck(screen_name, unicode)
rec = gittip.db.fetchone( "SELECT user_info FROM elsewhere "
"WHERE platform='twitter' "
"AND user_info->'screen_name' = %s"
, (screen_name,)
)
if rec is not None:
user_info = rec['user_info']
else:
url = "https://api.twitter.com/1/users/show.json?screen_name=%s"
user_info = requests.get(url % screen_name)
# Keep an eye on our Twitter usage.
# =================================
rate_limit = user_info.headers['X-RateLimit-Limit']
rate_limit_remaining = user_info.headers['X-RateLimit-Remaining']
rate_limit_reset = user_info.headers['X-RateLimit-Reset']
try:
rate_limit = int(rate_limit)
rate_limit_remaining = int(rate_limit_remaining)
rate_limit_reset = int(rate_limit_reset)
except (TypeError, ValueError):
log( "Got weird rate headers from Twitter: %s %s %s"
% (rate_limit, rate_limit_remaining, rate_limit_reset)
)
else:
reset = datetime.datetime.fromtimestamp(rate_limit_reset, tz=utc)
reset = to_age(reset)
log( "Twitter API calls used: %d / %d. Resets %s."
% (rate_limit - rate_limit_remaining, rate_limit, reset)
)
if user_info.status_code == 200:
user_info = json.loads(user_info.text)
else:
log("Twitter lookup failed with %d." % user_info.status_code)
raise Response(404)
return user_info