<s> """<STR_LIT>""" <EOL> import functools <EOL> import logging <EOL> import sys <EOL> from dateutil import parser <EOL> from datafeed . exchange import * <EOL> from datafeed . quote import * <EOL> from datafeed . providers . http_fetcher import Fetcher <EOL> from tornado . escape import json_decode <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _EXCHANGES = { <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> class SinaSecurity ( Security ) : <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self . _abbr , self . symbol ) <EOL> @ property <EOL> def _abbr ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( self . exchange ) . lower ( ) <EOL> @ classmethod <EOL> def from_string ( cls , idstr ) : <EOL> abbr = idstr [ : <NUM_LIT:2> ] <EOL> symbol = idstr [ <NUM_LIT:2> : ] <EOL> exchange = cls . get_exchange_from_abbr ( abbr ) <EOL> return cls ( exchange , symbol ) <EOL> @ classmethod <EOL> def get_exchange_from_abbr ( cls , abbr ) : <EOL> """<STR_LIT>""" <EOL> klass = getattr ( sys . modules [ __name__ ] , abbr . upper ( ) ) <EOL> return klass ( ) <EOL> class SinaReport ( Report ) : <EOL> _DEFINITIONS = ( <EOL> ( "<STR_LIT:name>" , str ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> ( "<STR_LIT>" , float ) , <EOL> ( "<STR_LIT:date>" , lambda x : parser . parse ( x ) . date ( ) ) , <EOL> ( "<STR_LIT:time>" , lambda x : parser . parse ( x ) ) <EOL> ) <EOL> def __init__ ( self , security , raw_data ) : <EOL> assert len ( raw_data ) == <NUM_LIT:32> <EOL> data = { } <EOL> i = <NUM_LIT:0> <EOL> for conf in self . _DEFINITIONS : <EOL> key , callback = conf <EOL> data [ key ] = callback ( raw_data [ i ] ) <EOL> i += <NUM_LIT:1> <EOL> super ( SinaReport , self ) . __init__ ( security , data ) <EOL> @ staticmethod <EOL> def parse ( rawdata ) : <EOL> from cStringIO import StringIO <EOL> f = StringIO ( rawdata ) <EOL> return ( SinaReport . parse_line ( line ) for line in f ) <EOL> @ staticmethod <EOL> def parse_line ( line ) : <EOL> splited = line . split ( '<STR_LIT:">' ) <EOL> idstr = splited [ <NUM_LIT:0> ] . split ( '<STR_LIT:_>' ) . pop ( ) [ : - <NUM_LIT:1> ] <EOL> s = SinaSecurity . from_string ( idstr ) <EOL> return SinaReport ( s , splited [ <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) ) <EOL> class SinaReportFetcher ( Fetcher ) : <EOL> _MAX_REQUEST_SIZE = <NUM_LIT:100> <EOL> def __init__ ( self , base_url = '<STR_LIT>' , <EOL> time_out = <NUM_LIT:20> , max_clients = <NUM_LIT:10> , request_size = <NUM_LIT:100> ) : <EOL> assert request_size <= self . _MAX_REQUEST_SIZE <EOL> super ( SinaReportFetcher , self ) . __init__ ( base_url , time_out , max_clients ) <EOL> self . _request_size = request_size <EOL> def _fetching_urls ( self , * args , ** kwargs ) : <EOL> ids = ( str ( s ) for s in args ) <EOL> ids = self . _slice ( ids ) <EOL> return ( self . _make_url ( filter ( lambda x : x != None , i ) ) for i in ids ) <EOL> def _make_url ( self , ids ) : <EOL> return "<STR_LIT>" % ( self . _base_url , '<STR_LIT:U+002C>' . join ( ids ) ) <EOL> def _callback ( self , security , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> callback = kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> callback = None <EOL> return functools . partial ( self . _handle_request , callback ) <EOL> def _handle_request ( self , callback , response ) : <EOL> try : <EOL> self . queue_len = self . queue_len - <NUM_LIT:1> <EOL> if response . error : <EOL> logging . error ( response . error ) <EOL> else : <EOL> callback ( response . body ) <EOL> except StandardError : <EOL> logging . error ( "<STR_LIT>" ) <EOL> finally : <EOL> self . stop ( ) </s>
