<s> import re <EOL> import decorator <EOL> import numpy as np <EOL> import pandas as pd <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> def _memoize ( func , * args , ** kw ) : <EOL> refresh = False <EOL> refresh_kw = func . mrefresh_keyword <EOL> if refresh_kw in func . __code__ . co_varnames : <EOL> if args [ func . __code__ . co_varnames . index ( refresh_kw ) ] : <EOL> refresh = True <EOL> if not refresh and refresh_kw in kw : <EOL> if kw [ refresh_kw ] : <EOL> refresh = True <EOL> key = pickle . dumps ( args , <NUM_LIT:1> ) + pickle . dumps ( kw , <NUM_LIT:1> ) <EOL> cache = func . mcache <EOL> if not refresh and key in cache : <EOL> return cache [ key ] <EOL> else : <EOL> cache [ key ] = result = func ( * args , ** kw ) <EOL> return result <EOL> def memoize ( f , refresh_keyword = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> f . mcache = { } <EOL> f . mrefresh_keyword = refresh_keyword <EOL> return decorator . decorator ( _memoize , f ) <EOL> def parse_arg ( arg ) : <EOL> """<STR_LIT>""" <EOL> if type ( arg ) == str : <EOL> arg = arg . strip ( ) <EOL> if '<STR_LIT:U+002C>' in arg : <EOL> arg = arg . split ( '<STR_LIT:U+002C>' ) <EOL> arg = [ x . strip ( ) for x in arg ] <EOL> else : <EOL> arg = [ arg ] <EOL> return arg <EOL> def clean_ticker ( ticker ) : <EOL> """<STR_LIT>""" <EOL> pattern = re . compile ( '<STR_LIT>' ) <EOL> res = pattern . sub ( '<STR_LIT>' , ticker . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:0> ] ) <EOL> return res . lower ( ) <EOL> def clean_tickers ( tickers ) : <EOL> """<STR_LIT>""" <EOL> return [ clean_ticker ( x ) for x in tickers ] <EOL> def fmtp ( number ) : <EOL> """<STR_LIT>""" <EOL> if np . isnan ( number ) : <EOL> return '<STR_LIT:->' <EOL> return format ( number , '<STR_LIT>' ) <EOL> def fmtpn ( number ) : <EOL> """<STR_LIT>""" <EOL> if np . isnan ( number ) : <EOL> return '<STR_LIT:->' <EOL> return format ( number * <NUM_LIT:100> , '<STR_LIT>' ) <EOL> def fmtn ( number ) : <EOL> """<STR_LIT>""" <EOL> if np . isnan ( number ) : <EOL> return '<STR_LIT:->' <EOL> return format ( number , '<STR_LIT>' ) <EOL> def get_period_name ( period ) : <EOL> period = period . upper ( ) <EOL> periods = { <EOL> '<STR_LIT:B>' : '<STR_LIT>' , <EOL> '<STR_LIT:C>' : '<STR_LIT>' , <EOL> '<STR_LIT:D>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:M>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:Y>' : '<STR_LIT>' , <EOL> '<STR_LIT:A>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:H>' : '<STR_LIT>' , <EOL> '<STR_LIT:T>' : '<STR_LIT>' , <EOL> '<STR_LIT:S>' : '<STR_LIT>' , <EOL> '<STR_LIT:L>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> if period in periods : <EOL> return periods [ period ] <EOL> else : <EOL> return None <EOL> def scale ( val , src , dst ) : <EOL> """<STR_LIT>""" <EOL> if val < src [ <NUM_LIT:0> ] : <EOL> return dst [ <NUM_LIT:0> ] <EOL> if val > src [ <NUM_LIT:1> ] : <EOL> return dst [ <NUM_LIT:1> ] <EOL> return ( ( val - src [ <NUM_LIT:0> ] ) / ( src [ <NUM_LIT:1> ] - src [ <NUM_LIT:0> ] ) ) * ( dst [ <NUM_LIT:1> ] - dst [ <NUM_LIT:0> ] ) + dst [ <NUM_LIT:0> ] <EOL> def as_percent ( self , digits = <NUM_LIT:2> ) : <EOL> return as_format ( self , '<STR_LIT>' % digits ) <EOL> def as_format ( item , format_str = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( item , pd . Series ) : <EOL> return item . map ( lambda x : format ( x , format_str ) ) <EOL> elif isinstance ( item , pd . DataFrame ) : <EOL> return item . applymap ( lambda x : format ( x , format_str ) ) </s>
