本文整理汇总了Python中aspen.json.loads函数的典型用法代码示例。如果您正苦于以下问题:Python loads函数的具体用法?Python loads怎么用?Python loads使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了loads函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:ArmstrongJ,项目名称:aspen-python,代码行数:13,代码来源:message.py
示例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'
开发者ID:9cat,项目名称:templecoin-gratipay,代码行数:13,代码来源:test_membername_json.py
示例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
开发者ID:CryptArc,项目名称:liberapay.com,代码行数:7,代码来源:test_pages.py
示例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
开发者ID:Web5design,项目名称:www.gittip.com,代码行数:60,代码来源:github.py
示例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
开发者ID:prodigeni,项目名称:www.gittip.com,代码行数:34,代码来源:github.py
示例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']
开发者ID:Changaco,项目名称:liberapay.com,代码行数:7,代码来源:test_membername_json.py
示例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
开发者ID:DylanLacey,项目名称:www.gittip.com,代码行数:60,代码来源:twitter.py
示例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'
开发者ID:jaraco,项目名称:aspen,代码行数:26,代码来源:test_renderers.py
示例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,
}
开发者ID:HolaChica,项目名称:gratipay.com,代码行数:7,代码来源:test_privacy_json.py
示例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
开发者ID:BastianBerndsen,项目名称:www.gittip.com,代码行数:34,代码来源:bitbucket.py
示例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
开发者ID:imathis,项目名称:www.gittip.com,代码行数:33,代码来源:github.py
示例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'
开发者ID:9cat,项目名称:templecoin-gratipay,代码行数:9,代码来源:test_membername_json.py
示例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
开发者ID:ChimeraCoder,项目名称:www.gittip.com,代码行数:55,代码来源:twitter.py
示例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
开发者ID:PeterDaveHello,项目名称:img.shields.io-old,代码行数:15,代码来源:services.py
示例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
开发者ID:Logolo,项目名称:www.gittip.com,代码行数:46,代码来源:twitter.py
示例16: alias_and_track
def alias_and_track(cookie, gittip_user_id):
"""Given a cookie and a unicode, hit Mixpanel in a thread.
"""
typecheck(cookie, SimpleCookie, gittip_user_id, unicode)
# Pull distinct_id out of Mixpanel cookie. Yay undocumented internals!
# This is bound to fail some day. Since this is in a thread, it shouldn't
# affect the user experience, and we'll still get a record of the failure
# in Sentry.
mpcookie = [v for k,v in cookie.items() if k.endswith('_mixpanel')]
if mpcookie:
distinct_id = json.loads(unquote(mpcookie[0].value))['distinct_id']
distinct_id = distinct_id.decode("utf8")
alias(distinct_id, gittip_user_id)
track(gittip_user_id, u"Opt In")
开发者ID:ChimeraCoder,项目名称:www.gittip.com,代码行数:17,代码来源:mixpanel.py
示例17: travis_ci
def travis_ci(first, second, color):
first = "build"
url = 'https://api.travis-ci.org/repos/%s/branches/master' % quote(second)
fp = urlopen(url)
repo = json.loads(fp.read())
try:
second = repo['branch']['state']
except KeyError:
second = 'n/a'
color = { 'failed': RED
, 'passed': GREEN
, 'started': YELLOW
}.get(second, LIGHTGRAY)
return first, second, color
开发者ID:PeterDaveHello,项目名称:img.shields.io-old,代码行数:17,代码来源:services.py
示例18: __init__
def __init__(self, first, second, bg):
self.first = "build"
url = 'https://api.travis-ci.org/repos?slug=%s' % quote(second)
fp = urlopen(url)
repos = json.loads(fp.read())
if repos:
status = repos[0].get('last_build_status', 'n/a')
else:
status = 'n/a'
self.second = { 0: 'passing'
, 1: 'failing'
, None: 'pending'
, 'n/a': 'n/a'
}.get(status, 'n/a')
self.bg = { 'failing': RED
, 'passing': GREEN
, 'pending': YELLOW
}.get(self.second, LIGHT_GREY)
开发者ID:angleman,项目名称:badgr.co,代码行数:21,代码来源:services.py
示例19: get_user_info
def get_user_info(login):
"""Given a unicode, return a dict.
"""
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)
if user_info.status_code == 200:
user_info = json.loads(user_info.text)
else:
code = user_info.status_code
raise Response(500, "GitHub lookup failed with %d." % code)
return user_info
开发者ID:AbdAllah-Ahmed,项目名称:www.gittip.com,代码行数:22,代码来源:github.py
示例20: 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.")
if 'error' in qs:
raise Response(500, str(qs['error']))
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
开发者ID:CodeMontage,项目名称:www.gittip.com,代码行数:39,代码来源:github.py
注:本文中的aspen.json.loads函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论