本文整理汇总了Python中aspen.utils.typecheck函数的典型用法代码示例。如果您正苦于以下问题:Python typecheck函数的具体用法?Python typecheck怎么用?Python typecheck使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了typecheck函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: wrap
def wrap(u):
"""Given a unicode, return a unicode.
"""
typecheck(u, unicode)
u = linkify(u) # Do this first, because it calls xthml_escape.
u = u.replace(u"\r\n", u"<br />\r\n").replace(u"\n", u"<br />\n")
return u if u else "..."
开发者ID:kennethreitz-archive,项目名称:www.gittip.com,代码行数:7,代码来源:utils.py
示例2: __init__
def __init__(self, headers, fp, server_software):
"""Takes a str, a file-like object, and another str.
If the Mapping API is used (in/one/all/has), then the iterable will be
read and parsed as media of type application/x-www-form-urlencoded or
multipart/form-data, according to content_type.
"""
typecheck(headers, Headers, server_software, str)
raw_len = int(headers.get('Content-length', '') or '0')
self.raw = self._read_raw(server_software, fp, raw_len) # XXX lazy!
parsed = self._parse(headers, self.raw)
if parsed is None:
# There was no content-type. Use self.raw.
pass
else:
for k in parsed.keys():
v = parsed[k]
if isinstance(v, cgi.MiniFieldStorage):
v = v.value.decode("UTF-8") # XXX Really? Always UTF-8?
else:
assert isinstance(v, cgi.FieldStorage), v
if v.filename is None:
v = v.value.decode("UTF-8")
self[k] = v
开发者ID:BigBlueHat,项目名称:aspen-python,代码行数:25,代码来源:request.py
示例3: transfer
def transfer(self, tipper, tippee, amount, pachinko=False):
"""Given two unicodes, a Decimal, and a boolean, return a boolean.
If the tipper doesn't have enough in their Gittip account then we
return False. Otherwise we decrement tipper's balance and increment
tippee's *pending* balance by amount.
"""
typecheck( tipper, unicode
, tippee, unicode
, amount, Decimal
, pachinko, bool
)
with self.db.get_connection() as conn:
cursor = conn.cursor()
try:
self.debit_participant(cursor, tipper, amount)
except IntegrityError:
return False
self.credit_participant(cursor, tippee, amount)
self.record_transfer(cursor, tipper, tippee, amount)
if pachinko:
self.mark_pachinko(cursor, amount)
else:
self.mark_transfer(cursor, amount)
conn.commit()
return True
开发者ID:DylanLacey,项目名称:www.gittip.com,代码行数:30,代码来源:payday.py
示例4: __init__
def __init__(self, d):
"""Takes headers as a dict or str.
"""
typecheck(d, (dict, str))
if isinstance(d, str):
from aspen.exceptions import MalformedHeader
def genheaders():
for line in d.splitlines():
if b':' not in line:
# no colon separator in header
raise MalformedHeader(line)
k, v = line.split(b':', 1)
if k != k.strip():
# disallowed leading or trailing whitspace
# (per http://tools.ietf.org/html/rfc7230#section-3.2.4)
raise MalformedHeader(line)
yield k, v.strip()
else:
genheaders = d.iteritems
CaseInsensitiveMapping.__init__(self, genheaders)
# Cookie
# ======
self.cookie = SimpleCookie()
try:
self.cookie.load(self.get('Cookie', b''))
except CookieError:
pass # XXX really?
开发者ID:Acidburn0zzz,项目名称:aspen-python,代码行数:30,代码来源:baseheaders.py
示例5: get_balanced_account
def get_balanced_account(participant_id, balanced_account_uri):
"""Find or create a balanced.Account.
"""
typecheck( participant_id, unicode
, balanced_account_uri, (unicode, None)
)
# XXX Balanced requires an email address
# https://github.com/balanced/balanced-api/issues/20
email_address = '{}@gittip.com'.format(participant_id)
if balanced_account_uri is None:
try:
account = \
balanced.Account.query.filter(email_address=email_address).one()
except balanced.exc.NoResultFound:
account = balanced.Account(email_address=email_address).save()
BALANCED_ACCOUNT = """\
UPDATE participants
SET balanced_account_uri=%s
WHERE id=%s
"""
gittip.db.execute(BALANCED_ACCOUNT, (account.uri, participant_id))
account.meta['participant_id'] = participant_id
account.save() # HTTP call under here
else:
account = balanced.Account.find(balanced_account_uri)
return account
开发者ID:alexcouper,项目名称:www.gittip.com,代码行数:31,代码来源:__init__.py
示例6: 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
示例7: transfer
def transfer(self, tipper, tippee, amount, pachinko=False):
"""Given two unicodes, a Decimal, and a boolean, return a boolean.
If the tipper doesn't have enough in their Gittip account then we
return False. Otherwise we decrement tipper's balance and increment
tippee's *pending* balance by amount.
"""
typecheck( tipper, unicode
, tippee, unicode
, amount, Decimal
, pachinko, bool
)
with self.db.get_cursor() as cursor:
try:
self.debit_participant(cursor, tipper, amount)
except NegativeBalance:
return False
self.credit_participant(cursor, tippee, amount)
context = 'take' if pachinko else 'tip'
self.record_transfer(cursor, tipper, tippee, amount, context)
return True
开发者ID:Siecje,项目名称:www.gittip.com,代码行数:25,代码来源:payday.py
示例8: clear
def clear(participant_id, stripe_customer_id):
typecheck(participant_id, unicode, stripe_customer_id, unicode)
# "Unlike other objects, deleted customers can still be retrieved through
# the API, in order to be able to track the history of customers while
# still removing their credit card details and preventing any further
# operations to be performed" https://stripe.com/docs/api#delete_customer
#
# Hmm ... should we protect against that in associate (above)?
#
# What this means though is (I think?) that we'll continue to be able to
# search for customers in the Stripe management UI by participant_id (which
# is stored as description in associate) even after the association is lost
# in our own database. This should be helpful for customer support.
customer = stripe.Customer.retrieve(stripe_customer_id)
customer.delete()
CLEAR = """\
UPDATE participants
SET stripe_customer_id=NULL
, last_bill_result=NULL
WHERE id=%s
"""
db.execute(CLEAR, (participant_id,))
开发者ID:jonathanmarvens,项目名称:www.gittip.com,代码行数:27,代码来源:billing.py
示例9: get_user_info
def get_user_info(db, username, osm_api_url):
"""Get the given user's information from the DB or failing that, openstreetmap.
:param username:
A unicode string representing a username in OpenStreetMap.
:param osm_api_url:
URL of OpenStreetMap API.
:returns:
A dictionary containing OpenStreetMap specific information for the user.
"""
typecheck(username, (unicode, PathPart))
rec = db.one("""
SELECT user_info FROM elsewhere
WHERE platform='openstreetmap'
AND user_info->'username' = %s
""", (username,))
if rec is not None:
user_info = rec
else:
osm_user = requests.get("%s/user/%s" % (osm_api_url, username))
if osm_user.status_code == 200:
log("User %s found in OpenStreetMap but not in gittip." % username)
user_info = None
elif osm_user.status_code == 404:
raise Response(404,
"OpenStreetMap identity '{0}' not found.".format(username))
else:
log("OpenStreetMap api responded with {0}: {1}".format(status, content),
level=logging.WARNING)
raise Response(502, "OpenStreetMap lookup failed with %d." % status)
return user_info
开发者ID:BastianBerndsen,项目名称:www.gittip.com,代码行数:34,代码来源:openstreetmap.py
示例10: charge
def charge(self, participant, amount):
"""Given dict and Decimal, return None.
This is the only place where we actually charge credit cards. Amount
should be the nominal amount. We'll compute Gittip's fee below this
function and add it to amount to end up with charge_amount.
"""
typecheck(participant, Participant, amount, Decimal)
username = participant.username
balanced_customer_href = participant.balanced_customer_href
typecheck(username, unicode, balanced_customer_href, (unicode, None))
# Perform some last-minute checks.
# ================================
if balanced_customer_href is None:
self.mark_missing_funding()
return # Participant has no funding source.
if not is_whitelisted(participant):
return # Participant not trusted.
# Go to Balanced.
# ===============
things = self.charge_on_balanced(username, balanced_customer_href, amount)
charge_amount, fee, error = things
amount = charge_amount - fee # account for possible rounding under
# charge_on_*
self.record_charge(amount, charge_amount, fee, error, username)
开发者ID:kaverna,项目名称:www.gittip.com,代码行数:35,代码来源:payday.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, 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
示例12: authorize
def authorize(participant_id, pmt):
"""Given two unicodes, return a dict.
This function attempts to authorize the credit card details referenced by
pmt. If the attempt succeeds we cancel the transaction. If it fails we log
the failure. Even for failure we keep the payment_method_token, we don't
reset it to None/NULL. It's useful for loading the previous (bad) credit
card info from Samurai in order to prepopulate the form.
"""
typecheck(pmt, unicode, participant_id, unicode)
transaction = Processor.authorize(pmt, '1.00', custom=participant_id)
if transaction.errors:
last_bill_result = json.dumps(transaction.errors)
out = dict(transaction.errors)
else:
transaction.reverse()
last_bill_result = ''
out = {}
STANDING = """\
UPDATE participants
SET payment_method_token=%s
, last_bill_result=%s
WHERE id=%s
"""
db.execute(STANDING, (pmt, last_bill_result, participant_id))
return out
开发者ID:bglusman,项目名称:www.gittip.com,代码行数:30,代码来源:billing.py
示例13: transfer
def transfer(tipper, tippee, amount):
"""Given two unicodes and a Decimal, return a boolean indicating success.
If the tipper doesn't have enough in their Gittip account then we return
False. Otherwise we decrement tipper's balance and increment tippee's
*pending* balance by amount.
"""
typecheck(tipper, unicode, tippee, unicode, amount, decimal.Decimal)
with db.get_connection() as conn:
cursor = conn.cursor()
try:
debit_participant(cursor, tipper, amount)
except ValueError:
return False
credit_participant(cursor, tippee, amount)
record_transfer(cursor, tipper, tippee, amount)
increment_payday(cursor, amount)
# Success.
# ========
conn.commit()
return True
开发者ID:mwhite,项目名称:www.gittip.com,代码行数:26,代码来源:billing.py
示例14: capture_card_hold
def capture_card_hold(db, participant, amount, hold):
"""Capture the previously created hold on the participant's credit card.
"""
typecheck( hold, balanced.CardHold
, amount, Decimal
)
username = participant.username
assert participant.id == int(hold.meta['participant_id'])
route = ExchangeRoute.from_address(participant, 'balanced-cc', hold.card_href)
assert isinstance(route, ExchangeRoute)
cents, amount_str, charge_amount, fee = _prep_hit(amount)
amount = charge_amount - fee # account for possible rounding
e_id = record_exchange(db, route, amount, fee, participant, 'pre')
meta = dict(participant_id=participant.id, exchange_id=e_id)
try:
hold.capture(amount=cents, description=username, meta=meta)
record_exchange_result(db, e_id, 'succeeded', None, participant)
except Exception as e:
error = repr_exception(e)
record_exchange_result(db, e_id, 'failed', error, participant)
raise
hold.meta['state'] = 'captured'
hold.save()
log("Captured " + amount_str + " on Balanced for " + username)
开发者ID:augmen,项目名称:gratipay.com,代码行数:30,代码来源:exchanges.py
示例15: get_img_src
def get_img_src(self, size=128):
"""Return a value for <img src="..." />.
Until we have our own profile pics, delegate. XXX Is this an attack
vector? Can someone inject this value? Don't think so, but if you make
it happen, let me know, eh? Thanks. :)
https://www.gittip.com/security.txt
"""
typecheck(size, int)
src = '/assets/%s/avatar-default.gif' % os.environ['__VERSION__']
github, twitter, bitbucket = self.get_accounts_elsewhere()
if github is not None:
# GitHub -> Gravatar: http://en.gravatar.com/site/implement/images/
if 'gravatar_id' in github.user_info:
gravatar_hash = github.user_info['gravatar_id']
src = "https://www.gravatar.com/avatar/%s.jpg?s=%s"
src %= (gravatar_hash, size)
elif twitter is not None:
# https://dev.twitter.com/docs/api/1/get/users/profile_image/%3Ascreen_name
if 'profile_image_url_https' in twitter.user_info:
src = twitter.user_info['profile_image_url_https']
# For Twitter, we don't have good control over size. We don't
# want the original, cause that can be huge. The next option is
# 73px(?!).
src = src.replace('_normal.', '_bigger.')
return src
开发者ID:dahlia,项目名称:www.gittip.com,代码行数:33,代码来源:participant.py
示例16: hit_balanced
def hit_balanced(self, participant_id, balanced_account_uri, amount):
"""We have a purported balanced_account_uri. Try to use it.
"""
typecheck( participant_id, unicode
, balanced_account_uri, unicode
, amount, Decimal
)
try_charge_amount = (amount + FEE[0]) * FEE[1]
try_charge_amount = try_charge_amount.quantize( FEE[0]
, rounding=ROUND_UP
)
charge_amount = try_charge_amount
also_log = ''
if charge_amount < MINIMUM:
charge_amount = MINIMUM # per Balanced
also_log = ', rounded up to $%s' % charge_amount
fee = try_charge_amount - amount
cents = int(charge_amount * 100)
msg = "Charging %s %d cents ($%s + $%s fee = $%s%s) ... "
msg %= participant_id, cents, amount, fee, try_charge_amount, also_log
try:
customer = balanced.Account.find(balanced_account_uri)
customer.debit(cents, description=participant_id)
log(msg + "succeeded.")
except balanced.exc.HTTPError as err:
log(msg + "failed: %s" % err.message)
return charge_amount, fee, err.message
return charge_amount, fee, None
开发者ID:booo,项目名称:www.gittip.com,代码行数:33,代码来源:payday.py
示例17: get_balanced_account
def get_balanced_account(username, balanced_account_uri):
"""Find or create a balanced.Account.
"""
typecheck( username, unicode
, balanced_account_uri, (unicode, None)
)
# XXX Balanced requires an email address
# https://github.com/balanced/balanced-api/issues/20
# quote to work around https://github.com/gittip/www.gittip.com/issues/781
email_address = '{}@gittip.com'.format(quote(username))
if balanced_account_uri is None:
try:
account = \
balanced.Account.query.filter(email_address=email_address).one()
except balanced.exc.NoResultFound:
account = balanced.Account(email_address=email_address).save()
BALANCED_ACCOUNT = """\
UPDATE participants
SET balanced_account_uri=%s
WHERE username=%s
"""
gittip.db.run(BALANCED_ACCOUNT, (account.uri, username))
account.meta['username'] = username
account.save() # HTTP call under here
else:
account = balanced.Account.find(balanced_account_uri)
return account
开发者ID:bmispelon,项目名称:www.gittip.com,代码行数:32,代码来源:__init__.py
示例18: get_img_src
def get_img_src(self, size=128):
"""Return a value for <img src="..." />.
Until we have our own profile pics, delegate. XXX Is this an attack
vector? Can someone inject this value? Don't think so, but if you make
it happen, let me know, eh? Thanks. :)
https://www.gittip.com/security.txt
"""
typecheck(size, int)
src = '/assets/%s/avatar-default.gif' % os.environ['__VERSION__']
github, twitter, bitbucket, bountysource = self.get_accounts_elsewhere()
if github is not None:
# GitHub -> Gravatar: http://en.gravatar.com/site/implement/images/
if 'gravatar_id' in github.user_info:
gravatar_hash = github.user_info['gravatar_id']
src = "https://www.gravatar.com/avatar/%s.jpg?s=%s"
src %= (gravatar_hash, size)
elif twitter is not None:
# https://dev.twitter.com/docs/api/1.1/get/users/show
if 'profile_image_url_https' in twitter.user_info:
src = twitter.user_info['profile_image_url_https']
# For Twitter, we don't have good control over size. The
# biggest option is 73px(?!), but that's too small. Let's go
# with the original: even though it may be huge, that's
# preferrable to guaranteed blurriness. :-/
src = src.replace('_normal.', '.')
return src
开发者ID:DylanLacey,项目名称:www.gittip.com,代码行数:35,代码来源:participant.py
示例19: _get_renderer_factory
def _get_renderer_factory(self, media_type, renderer):
"""Given two bytestrings, return a renderer factory or None.
"""
typecheck(media_type, str, renderer, str)
if renderer_re.match(renderer) is None:
possible =', '.join(sorted(self.website.renderer_factories.keys()))
msg = ("Malformed renderer %s. It must match %s. Possible "
"renderers (might need third-party libs): %s.")
raise SyntaxError(msg % (renderer, renderer_re.pattern, possible))
renderer = renderer.decode('US-ASCII')
factories = self.website.renderer_factories
make_renderer = factories.get(renderer, None)
if isinstance(make_renderer, ImportError):
raise make_renderer
elif make_renderer is None:
possible = []
want_legend = False
for k, v in sorted(factories.iteritems()):
if isinstance(v, ImportError):
k = '*' + k
want_legend = True
possible.append(k)
possible = ', '.join(possible)
if want_legend:
legend = " (starred are missing third-party libraries)"
else:
legend = ''
raise ValueError("Unknown renderer for %s: %s. Possible "
"renderers%s: %s."
% (media_type, renderer, legend, possible))
return make_renderer
开发者ID:Web5design,项目名称:aspen-python,代码行数:33,代码来源:negotiated_resource.py
示例20: charge_on_balanced
def charge_on_balanced(self, username, balanced_customer_href, amount):
"""We have a purported balanced_customer_href. Try to use it.
"""
typecheck( username, unicode
, balanced_customer_href, unicode
, amount, Decimal
)
cents, msg, charge_amount, fee = self._prep_hit(amount)
msg = msg % (username, "Balanced")
try:
customer = balanced.Customer.fetch(balanced_customer_href)
customer.cards.one().debit(amount=cents, description=username)
log(msg + "succeeded.")
error = ""
except balanced.exc.HTTPError as err:
error = err.message.message
except:
error = repr(sys.exc_info()[1])
if error:
log(msg + "failed: %s" % error)
return charge_amount, fee, error
开发者ID:Siecje,项目名称:www.gittip.com,代码行数:25,代码来源:payday.py
注:本文中的aspen.utils.typecheck函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论