<s> from functools import wraps <EOL> from flask import request , current_app <EOL> import random , math <EOL> import re <EOL> import jinja2 <EOL> from time import sleep <EOL> import datetime <EOL> import stripe <EOL> import emailer <EOL> from unicode_helpers import to_unicode_or_bust <EOL> import unicodedata <EOL> from sqlalchemy . exc import IntegrityError , DataError , InvalidRequestError <EOL> from sqlalchemy . orm . exc import FlushError <EOL> import logging <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def slow ( f ) : <EOL> f . slow = True <EOL> return f <EOL> def remove_punctuation ( input_string ) : <EOL> no_punc = input_string <EOL> if input_string : <EOL> no_punc = "<STR_LIT>" . join ( e for e in input_string if ( e . isalnum ( ) or e . isspace ( ) ) ) <EOL> return no_punc <EOL> def remove_unneeded_characters ( input_string , encoding = '<STR_LIT:utf-8>' , char_classes_to_remove = [ "<STR_LIT:C>" , "<STR_LIT:M>" , "<STR_LIT:P>" , "<STR_LIT:S>" , "<STR_LIT>" ] ) : <EOL> input_was_unicode = True <EOL> if isinstance ( input_string , basestring ) : <EOL> if not isinstance ( input_string , unicode ) : <EOL> input_was_unicode = False <EOL> unicode_input = to_unicode_or_bust ( input_string ) <EOL> response = u'<STR_LIT>' . join ( c for c in unicode_input if unicodedata . category ( c ) [ <NUM_LIT:0> ] not in char_classes_to_remove ) <EOL> if not input_was_unicode : <EOL> response = response . encode ( encoding ) <EOL> return response <EOL> def jsonp ( f ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( f ) <EOL> def decorated_function ( * args , ** kwargs ) : <EOL> callback = request . args . get ( '<STR_LIT>' , False ) <EOL> if callback : <EOL> content = str ( callback ) + '<STR_LIT:(>' + str ( f ( * args , ** kwargs ) . data ) + '<STR_LIT:)>' <EOL> return current_app . response_class ( content , mimetype = '<STR_LIT>' ) <EOL> else : <EOL> return f ( * args , ** kwargs ) <EOL> return decorated_function <EOL> def jinja_render ( template_name , context ) : <EOL> templateLoader = jinja2 . FileSystemLoader ( searchpath = "<STR_LIT>" ) <EOL> templateEnv = jinja2 . Environment ( loader = templateLoader ) <EOL> html_template = templateEnv . get_template ( template_name ) <EOL> return html_template . render ( context ) <EOL> def pickWosItems ( num_total_results , num_subset_results ) : <EOL> num_in_final_sample = <NUM_LIT:100> <EOL> num_results_per_page = <NUM_LIT:50> <EOL> num_to_sample = int ( math . ceil ( <EOL> float ( num_subset_results * num_in_final_sample ) / float ( num_total_results ) <EOL> ) ) <EOL> subset_result_ids = range ( <NUM_LIT:1> , num_subset_results ) <EOL> ids_to_sample = sorted ( random . sample ( subset_result_ids , num_to_sample ) ) <EOL> pages_and_ids = [ ] <EOL> for result_id in ids_to_sample : <EOL> page = int ( math . floor ( result_id / num_results_per_page ) ) + <NUM_LIT:1> <EOL> pages_and_ids . append ( ( page , result_id ) ) <EOL> return pages_and_ids <EOL> _underscorer1 = re . compile ( r'<STR_LIT>' ) <EOL> _underscorer2 = re . compile ( '<STR_LIT>' ) <EOL> def camel_to_snake_case ( s ) : <EOL> """<STR_LIT>""" <EOL> subbed = _underscorer1 . sub ( r'<STR_LIT>' , s ) <EOL> return _underscorer2 . sub ( r'<STR_LIT>' , subbed ) . lower ( ) <EOL> def local_sleep ( interval , fuzz_proportion = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if fuzz_proportion : <EOL> max_fuzz_distance = interval * fuzz_proportion <EOL> interval += random . uniform ( - max_fuzz_distance , max_fuzz_distance ) <EOL> if "<STR_LIT>" in request . url_root : <EOL> logger . debug ( u"<STR_LIT>" . format ( <EOL> second = interval ) ) <EOL> sleep ( interval ) <EOL> def as_int_or_float_if_possible ( input_value ) : <EOL> value = input_value <EOL> try : <EOL> value = int ( input_value ) <EOL> except ( ValueError , TypeError ) : <EOL> try : <EOL> value = float ( input_value ) <EOL> except ( ValueError , TypeError ) : <EOL> pass <EOL> return ( value ) <EOL> def dict_from_dir ( obj , keys_to_ignore = None , keys_to_show = "<STR_LIT:all>" ) : <EOL> if keys_to_ignore is None : <EOL> keys_to_ignore = [ ] <EOL> elif isinstance ( keys_to_ignore , basestring ) : <EOL> keys_to_ignore = [ keys_to_ignore ] <EOL> ret = { } <EOL> if keys_to_show != "<STR_LIT:all>" : <EOL> for key in keys_to_show : <EOL> ret [ key ] = getattr ( obj , key ) <EOL> return ret <EOL> for k in dir ( obj ) : <EOL> pass <EOL> if k . startswith ( "<STR_LIT:_>" ) : <EOL> pass <EOL> elif k in keys_to_ignore : <EOL> pass <EOL> elif k in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> pass <EOL> else : <EOL> value = getattr ( obj , k ) <EOL> if not callable ( value ) : <EOL> ret [ k ] = value <EOL> return ret <EOL> def todict ( obj , classkey = None ) : <EOL> if isinstance ( obj , dict ) : <EOL> data = { } <EOL> for ( k , v ) in obj . items ( ) : <EOL> data [ k ] = todict ( v , classkey ) <EOL> return data <EOL> elif hasattr ( obj , "<STR_LIT>" ) : <EOL> data = dict ( [ <EOL> ( key , todict ( value , classkey ) ) <EOL> for key , value in obj . to_dict ( ) . iteritems ( ) <EOL> if key == "<STR_LIT>" or ( not callable ( value ) and not key . startswith ( '<STR_LIT:_>' ) ) <EOL> ] ) <EOL> if classkey is not None and hasattr ( obj , "<STR_LIT>" ) : <EOL> data [ classkey ] = obj . __class__ . __name__ <EOL> return data <EOL> elif hasattr ( obj , "<STR_LIT>" ) : <EOL> return todict ( obj . _ast ( ) ) <EOL> elif type ( obj ) is datetime . datetime : <EOL> return obj . isoformat ( ) <EOL> elif hasattr ( obj , "<STR_LIT>" ) : <EOL> return [ todict ( v , classkey ) for v in obj ] <EOL> elif hasattr ( obj , "<STR_LIT>" ) : <EOL> data = dict ( [ ( key , todict ( value , classkey ) ) <EOL> for key , value in obj . __dict__ . iteritems ( ) <EOL> if not callable ( value ) and not key . startswith ( '<STR_LIT:_>' ) ] ) <EOL> if classkey is not None and hasattr ( obj , "<STR_LIT>" ) : <EOL> data [ classkey ] = obj . __class__ . __name__ <EOL> return data <EOL> else : <EOL> return obj <EOL> class HTTPMethodOverrideMiddleware ( object ) : <EOL> allowed_methods = frozenset ( [ <EOL> '<STR_LIT:GET>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:POST>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> bodyless_methods = frozenset ( [ '<STR_LIT:GET>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> def __call__ ( self , environ , start_response ) : <EOL> method = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . upper ( ) <EOL> if method in self . allowed_methods : <EOL> method = method . encode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> environ [ '<STR_LIT>' ] = method <EOL> if method in self . bodyless_methods : <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT:0>' <EOL> return self . app ( environ , start_response ) <EOL> class Timer ( object ) : <EOL> def __init__ ( self ) : <EOL> self . start = datetime . datetime . now ( ) <EOL> self . last_check = self . start <EOL> def since_last_check ( self ) : <EOL> return self . elapsed ( since_last_check = True ) <EOL> def elapsed ( self , since_last_check = False ) : <EOL> if since_last_check : <EOL> window_start = self . last_check <EOL> else : <EOL> window_start = self . start <EOL> current_datetime = datetime . datetime . now ( ) <EOL> self . last_check = current_datetime <EOL> elapsed_seconds = ( current_datetime - window_start ) . total_seconds ( ) <EOL> return elapsed_seconds <EOL> def ordinal ( value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> value = int ( value ) <EOL> except ValueError : <EOL> return value <EOL> except TypeError : <EOL> return value <EOL> if value % <NUM_LIT:100> // <NUM_LIT:10> != <NUM_LIT:1> : <EOL> if value % <NUM_LIT:10> == <NUM_LIT:1> : <EOL> ordval = u"<STR_LIT>" % ( value , "<STR_LIT>" ) <EOL> elif value % <NUM_LIT:10> == <NUM_LIT:2> : <EOL> ordval = u"<STR_LIT>" % ( value , "<STR_LIT>" ) <EOL> elif value % <NUM_LIT:10> == <NUM_LIT:3> : <EOL> ordval = u"<STR_LIT>" % ( value , "<STR_LIT>" ) <EOL> else : <EOL> ordval = u"<STR_LIT>" % ( value , "<STR_LIT>" ) <EOL> else : <EOL> ordval = u"<STR_LIT>" % ( value , "<STR_LIT>" ) <EOL> return ordval <EOL> def cached_property ( property_name ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( property_name ) <EOL> def cached_propery_get ( self ) : <EOL> try : <EOL> return self . _cache [ property_name ] <EOL> except AttributeError : <EOL> self . _cache = { } <EOL> except KeyError : <EOL> pass <EOL> ret = self . _cache [ property_name ] = property_name ( self ) <EOL> return ret <EOL> return property ( cached_propery_get ) <EOL> def commit ( db ) : <EOL> try : <EOL> db . session . commit ( ) <EOL> except ( IntegrityError , FlushError ) as e : <EOL> db . session . rollback ( ) <EOL> logger . warning ( u"<STR_LIT>" . format ( <EOL> message = e . message ) ) <EOL> def random_alpha_str ( length = <NUM_LIT:5> ) : <EOL> return '<STR_LIT>' . join ( random . choice ( '<STR_LIT>' ) for i in range ( length ) ) <EOL> def mint_stripe_coupon ( stripe_token , email , cost , num_subscriptions ) : <EOL> coupon_code = "<STR_LIT>" + random_alpha_str ( ) <EOL> print "<STR_LIT>" , coupon_code <EOL> descr = "<STR_LIT>" . format ( <EOL> coupon_code = coupon_code , <EOL> num_subscriptions = num_subscriptions , <EOL> cost = cost <EOL> ) <EOL> print "<STR_LIT>" , coupon_code <EOL> coupon_resp = stripe . Coupon . create ( <EOL> id = coupon_code , <EOL> percent_off = <NUM_LIT:100> , <EOL> duration = "<STR_LIT>" , <EOL> duration_in_months = <NUM_LIT:12> , <EOL> max_redemptions = num_subscriptions , <EOL> metadata = { "<STR_LIT:email>" : email } <EOL> ) <EOL> charge_resp = stripe . Charge . create ( <EOL> amount = cost * <NUM_LIT:100> , <EOL> currency = "<STR_LIT>" , <EOL> card = stripe_token , <EOL> description = descr , <EOL> statement_description = "<STR_LIT>" , <EOL> receipt_email = email , <EOL> metadata = { <EOL> "<STR_LIT>" : coupon_code <EOL> } <EOL> ) <EOL> emailer . send ( <EOL> address = email , <EOL> subject = "<STR_LIT>" , <EOL> template_name = "<STR_LIT>" , <EOL> context = { <EOL> "<STR_LIT>" : num_subscriptions , <EOL> "<STR_LIT>" : coupon_code <EOL> } <EOL> ) </s>
