idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
23,200
def import_attribute ( path ) : module_name , attribute_name = path . rsplit ( '.' , 1 ) module = import_module ( module_name ) attribute = getattr ( module , attribute_name ) return attribute
Import attribute by path like package . module . attribute
23,201
def extract_options ( options , names ) : result = { } for name , value in copy ( options ) . items ( ) : if name in names : result [ name ] = value del options [ name ] return result
Return options for names and remove it from given options in - place .
23,202
def stringify_value ( value ) : if value is None : return u'' isoformat = getattr ( value , 'isoformat' , None ) if isoformat is not None : value = isoformat ( ) return type ( u'' ) ( value )
Convert any value to string .
23,203
def reset ( self ) : if self . __row_number > self . __sample_size : self . __parser . reset ( ) self . __extract_sample ( ) self . __extract_headers ( ) self . __row_number = 0
Resets the stream pointer to the beginning of the file .
23,204
def sample ( self ) : sample = [ ] iterator = iter ( self . __sample_extended_rows ) iterator = self . __apply_processors ( iterator ) for row_number , headers , row in iterator : sample . append ( row ) return sample
Returns the stream s rows used as sample .
23,205
def iter ( self , keyed = False , extended = False ) : if self . closed : message = 'Stream is closed. Please call "stream.open()" first.' raise exceptions . TabulatorException ( message ) iterator = chain ( self . __sample_extended_rows , self . __parser . extended_rows ) iterator = self . __apply_processors ( iterato...
Iterate over the rows .
23,206
def save ( self , target , format = None , encoding = None , ** options ) : if encoding is None : encoding = config . DEFAULT_ENCODING if format is None : _ , format = helpers . detect_scheme_and_format ( target ) writer_class = self . __custom_writers . get ( format ) if writer_class is None : if format not in config ...
Save stream to the local filesystem .
23,207
def validate ( source , scheme = None , format = None ) : detected_scheme , detected_format = helpers . detect_scheme_and_format ( source ) scheme = scheme or detected_scheme format = format or detected_format if scheme is not None : if scheme not in config . LOADERS : raise exceptions . SchemeError ( 'Scheme "%s" is n...
Check if tabulator is able to load the source .
23,208
def require_axis ( f ) : @ wraps ( f ) def _wrapper ( self , * args , ** kwargs ) : if None in ( self . axis , self . sel_axis ) : raise ValueError ( '%(func_name) requires the node %(node)s ' 'to have an axis and a sel_axis function' % dict ( func_name = f . __name__ , node = repr ( self ) ) ) return f ( self , * args...
Check if the object of the function has axis and sel_axis members
23,209
def create ( point_list = None , dimensions = None , axis = 0 , sel_axis = None ) : if not point_list and not dimensions : raise ValueError ( 'either point_list or dimensions must be provided' ) elif point_list : dimensions = check_dimensionality ( point_list , dimensions ) sel_axis = sel_axis or ( lambda prev_axis : (...
Creates a kd - tree from a list of points
23,210
def level_order ( tree , include_all = False ) : q = deque ( ) q . append ( tree ) while q : node = q . popleft ( ) yield node if include_all or node . left : q . append ( node . left or node . __class__ ( ) ) if include_all or node . right : q . append ( node . right or node . __class__ ( ) )
Returns an iterator over the tree in level - order
23,211
def visualize ( tree , max_level = 100 , node_width = 10 , left_padding = 5 ) : height = min ( max_level , tree . height ( ) - 1 ) max_width = pow ( 2 , height ) per_level = 1 in_level = 0 level = 0 for node in level_order ( tree , include_all = True ) : if in_level == 0 : print ( ) print ( ) print ( ' ' * left_padding...
Prints the tree to stdout
23,212
def is_leaf ( self ) : return ( not self . data ) or ( all ( not bool ( c ) for c , p in self . children ) )
Returns True if a Node has no subnodes
23,213
def children ( self ) : if self . left and self . left . data is not None : yield self . left , 0 if self . right and self . right . data is not None : yield self . right , 1
Returns an iterator for the non - empty children of the Node
23,214
def set_child ( self , index , child ) : if index == 0 : self . left = child else : self . right = child
Sets one of the node s children
23,215
def get_child_pos ( self , child ) : for c , pos in self . children : if child == c : return pos
Returns the position if the given child
23,216
def add ( self , point ) : current = self while True : check_dimensionality ( [ point ] , dimensions = current . dimensions ) if current . data is None : current . data = point return current if point [ current . axis ] < current . data [ current . axis ] : if current . left is None : current . left = current . create_...
Adds a point to the current node or iteratively descends to one of its children .
23,217
def create_subnode ( self , data ) : return self . __class__ ( data , axis = self . sel_axis ( self . axis ) , sel_axis = self . sel_axis , dimensions = self . dimensions )
Creates a subnode for the current node
23,218
def find_replacement ( self ) : if self . right : child , parent = self . right . extreme_child ( min , self . axis ) else : child , parent = self . left . extreme_child ( max , self . axis ) return ( child , parent if parent is not None else self )
Finds a replacement for the current node
23,219
def remove ( self , point , node = None ) : if not self : return if self . should_remove ( point , node ) : return self . _remove ( point ) if self . left and self . left . should_remove ( point , node ) : self . left = self . left . _remove ( point ) elif self . right and self . right . should_remove ( point , node ) ...
Removes the node with the given point from the tree
23,220
def axis_dist ( self , point , axis ) : return math . pow ( self . data [ axis ] - point [ axis ] , 2 )
Squared distance at the given axis between the current Node and the given point
23,221
def dist ( self , point ) : r = range ( self . dimensions ) return sum ( [ self . axis_dist ( point , i ) for i in r ] )
Squared distance between the current Node and the given point
23,222
def search_knn ( self , point , k , dist = None ) : if k < 1 : raise ValueError ( "k must be greater than 0." ) if dist is None : get_dist = lambda n : n . dist ( point ) else : get_dist = lambda n : dist ( n . data , point ) results = [ ] self . _search_node ( point , k , results , get_dist , itertools . count ( ) ) r...
Return the k nearest neighbors of point and their distances
23,223
def search_nn ( self , point , dist = None ) : return next ( iter ( self . search_knn ( point , 1 , dist ) ) , None )
Search the nearest node of the given point
23,224
def search_nn_dist ( self , point , distance , best = None ) : results = [ ] get_dist = lambda n : n . dist ( point ) self . _search_nn_dist ( point , distance , results , get_dist ) return results
Search the n nearest nodes of the given point which are within given distance
23,225
def is_valid ( self ) : if not self : return True if self . left and self . data [ self . axis ] < self . left . data [ self . axis ] : return False if self . right and self . data [ self . axis ] > self . right . data [ self . axis ] : return False return all ( c . is_valid ( ) for c , _ in self . children ) or self ....
Checks recursively if the tree is valid
23,226
def extreme_child ( self , sel_func , axis ) : max_key = lambda child_parent : child_parent [ 0 ] . data [ axis ] me = [ ( self , None ) ] if self else [ ] child_max = [ c . extreme_child ( sel_func , axis ) for c , _ in self . children ] child_max = [ ( c , p if p is not None else self ) for c , p in child_max ] candi...
Returns a child of the subtree and its parent
23,227
def dump_raw ( self , text , stream = None ) : encrypted = self . vault . encrypt ( text ) if stream : stream . write ( encrypted ) else : return encrypted
Encrypt raw data and write to stream .
23,228
def dump ( self , data , stream = None ) : yaml_text = yaml . dump ( data , default_flow_style = False , allow_unicode = True ) return self . dump_raw ( yaml_text , stream = stream )
Encrypt data and print stdout or write to stream .
23,229
def add_item ( c , name , item ) : if isinstance ( item , MenuItem ) : if name not in c . items : c . items [ name ] = [ ] c . items [ name ] . append ( item ) c . sorted [ name ] = False
add_item adds MenuItems to the menu identified by name
23,230
def load_menus ( c ) : if c . loaded : return app_names = settings . INSTALLED_APPS if apps : app_names = [ app_config . name for app_config in apps . get_app_configs ( ) ] for app in app_names : if app . startswith ( "django." ) : continue menu_module = '%s.menus' % app try : __import__ ( menu_module , fromlist = [ "m...
load_menus loops through INSTALLED_APPS and loads the menu . py files from them .
23,231
def sort_menus ( c ) : for name in c . items : if not c . sorted [ name ] : c . items [ name ] . sort ( key = lambda x : x . weight ) c . sorted [ name ] = True
sort_menus goes through the items and sorts them based on their weight
23,232
def process ( c , request , name = None ) : c . load_menus ( ) c . sort_menus ( ) if name is None : items = { } for name in c . items : items [ name ] = c . process ( request , name ) return items if name not in c . items : return [ ] items = copy . deepcopy ( c . items [ name ] ) curitem = None for item in items : ite...
process uses the current request to determine which menus should be visible which are selected etc .
23,233
def check ( self , request ) : if callable ( self . check_func ) : self . visible = self . check_func ( request )
Evaluate if we should be visible for this request
23,234
def process ( self , request ) : self . check ( request ) if not self . visible : return if callable ( self . title ) : self . title = self . title ( request ) if self . slug is None : if sys . version_info > ( 3 , 0 ) : self . slug = slugify ( self . title ) else : self . slug = slugify ( unicode ( self . title ) ) if...
process determines if this item should visible if its selected etc ...
23,235
def match_url ( self , request ) : matched = False if self . exact_url : if re . match ( "%s$" % ( self . url , ) , request . path ) : matched = True elif re . match ( "%s" % self . url , request . path ) : matched = True return matched
match url determines if this is selected
23,236
def configure ( self , proto ) : if not proto : defer . returnValue ( None ) reply = yield self . __send_ismaster ( proto , timeout = self . initialDelay ) if len ( reply . documents ) != 1 : raise OperationFailure ( "TxMongo: invalid document length." ) config = reply . documents [ 0 ] . decode ( ) if not config . get...
Configures the protocol using the information gathered from the remote Mongo instance . Such information may contain the max BSON document size replica set configuration and the master status of the instance .
23,237
def notifyReady ( self ) : if self . instance : return defer . succeed ( self . instance ) def on_cancel ( d ) : self . __notify_ready . remove ( d ) df = defer . Deferred ( on_cancel ) self . __notify_ready . append ( df ) return df
Returns a deferred that will fire when the factory has created a protocol that can be used to communicate with a Mongo server .
23,238
def retryNextHost ( self , connector = None ) : if not self . continueTrying : msg = "TxMongo: Abandoning {0} on explicit request." . format ( connector ) log . msg ( msg ) return if connector is None : if self . connector is None : raise ValueError ( "TxMongo: No additional connector to retry." ) else : connector = se...
Have this connector connect again to the next host in the configured list of hosts .
23,239
def indexes_created ( self ) : d = defer . Deferred ( ) self . __indexes_created_defer . chainDeferred ( d ) return d
Returns a defer on the creation of this GridFS instance s indexes
23,240
def get_last_version ( self , filename ) : def ok ( doc ) : if doc is None : raise NoFile ( "TxMongo: no file in gridfs with filename {0}" . format ( repr ( filename ) ) ) return GridOut ( self . __collection , doc ) return self . __files . find_one ( { "filename" : filename } , filter = filter . sort ( DESCENDING ( "u...
Get a file from GridFS by filename .
23,241
def authenticate ( self , name , password , mechanism = "DEFAULT" ) : if not isinstance ( name , ( bytes , unicode ) ) : raise TypeError ( "TxMongo: name must be an instance of basestring." ) if not isinstance ( password , ( bytes , unicode ) ) : raise TypeError ( "TxMongo: password must be an instance of basestring." ...
Send an authentication command for this database . mostly stolen from pymongo
23,242
def timeout ( func ) : @ wraps ( func ) def _timeout ( * args , ** kwargs ) : now = time ( ) deadline = kwargs . pop ( "deadline" , None ) seconds = kwargs . pop ( "timeout" , None ) if deadline is None and seconds is not None : deadline = now + seconds if deadline is not None and deadline < now : raise TimeExceeded ( ...
Decorator to add timeout to Deferred calls
23,243
def writelines ( self , sequence ) : iterator = iter ( sequence ) def iterate ( _ = None ) : try : return self . write ( next ( iterator ) ) . addCallback ( iterate ) except StopIteration : return return defer . maybeDeferred ( iterate )
Write a sequence of strings to the file .
23,244
def get_new_requests ( self ) : content_type = self . __queue_item . response . headers . get ( 'content-type' ) scrapers = self . __get_all_scrapers ( ) new_requests = [ ] for scraper in scrapers : instance = scraper ( self . __options , self . __queue_item ) if self . __content_type_matches ( content_type , instance ...
Retrieve all the new request that were found in this request .
23,245
def __make_request ( self , url , method , data , auth , cookies , headers , proxies , timeout , verify ) : request_by_method = getattr ( requests , method ) return request_by_method ( url = url , data = data , auth = auth , cookies = cookies , headers = headers , proxies = proxies , timeout = timeout , verify = verify...
Execute a request with the given data .
23,246
def __get_all_scrapers ( self ) : modules_strings = self . __get_all_scrapers_modules ( ) modules = [ ] for module_string in modules_strings : module = importlib . import_module ( "nyawc.scrapers." + module_string ) modules . append ( getattr ( module , module_string ) ) return modules
Find all available scraper references .
23,247
def __get_all_scrapers_modules ( self ) : modules = [ ] file = os . path . realpath ( __file__ ) folder = os . path . dirname ( file ) for filename in os . listdir ( folder + "/../scrapers" ) : if filename . endswith ( "Scraper.py" ) and not filename . startswith ( "Base" ) : modules . append ( filename [ : - 3 ] ) ret...
Find all available scraper modules .
23,248
def __content_type_matches ( self , content_type , available_content_types ) : if content_type is None : return False if content_type in available_content_types : return True for available_content_type in available_content_types : if available_content_type in content_type : return True return False
Check if the given content type matches one of the available content types .
23,249
def increase_route_count ( self , crawled_request ) : for route in self . __routing_options . routes : if re . compile ( route ) . match ( crawled_request . url ) : count_key = str ( route ) + crawled_request . method if count_key in self . __routing_count . keys ( ) : self . __routing_count [ count_key ] += 1 else : s...
Increase the count that determines how many times a URL of a certain route has been crawled .
23,250
def is_treshold_reached ( self , scraped_request ) : for route in self . __routing_options . routes : if re . compile ( route ) . match ( scraped_request . url ) : count_key = str ( route ) + scraped_request . method if count_key in self . __routing_count . keys ( ) : return self . __routing_count [ count_key ] >= self...
Check if similar requests to the given requests have already been crawled X times . Where X is the minimum treshold amount from the options .
23,251
def add_request ( self , request ) : queue_item = QueueItem ( request , Response ( request . url ) ) self . add ( queue_item ) return queue_item
Add a request to the queue .
23,252
def has_request ( self , request ) : queue_item = QueueItem ( request , Response ( request . url ) ) key = queue_item . get_hash ( ) for status in QueueItem . STATUSES : if key in self . __get_var ( "items_" + status ) . keys ( ) : return True return False
Check if the given request already exists in the queue .
23,253
def get_first ( self , status ) : items = self . get_all ( status ) if items : return list ( items . items ( ) ) [ 0 ] [ 1 ] return None
Get the first item in the queue that has the given status .
23,254
def get_for_type ( input_type = "text" ) : if input_type in RandomInputHelper . cache : return RandomInputHelper . cache [ input_type ] types = { "text" : RandomInputHelper . get_random_value , "hidden" : RandomInputHelper . get_random_value , "search" : RandomInputHelper . get_random_value , "color" : RandomInputHelpe...
Get a random string for the given html input type
23,255
def get_random_value ( length = 10 , character_sets = [ string . ascii_uppercase , string . ascii_lowercase ] ) : return "" . join ( random . choice ( "" . join ( character_sets ) ) for i in range ( length ) )
Get a random string with the given length .
23,256
def get_random_email ( ltd = "com" ) : email = [ RandomInputHelper . get_random_value ( 6 , [ string . ascii_lowercase ] ) , "@" , RandomInputHelper . get_random_value ( 6 , [ string . ascii_lowercase ] ) , "." , ltd ] return "" . join ( email )
Get a random email address with the given ltd .
23,257
def get_random_password ( ) : password = [ ] password . append ( RandomInputHelper . get_random_value ( 4 , [ string . ascii_lowercase ] ) ) password . append ( RandomInputHelper . get_random_value ( 2 , [ string . digits ] ) ) password . append ( RandomInputHelper . get_random_value ( 2 , [ "$&*@!" ] ) ) password . ap...
Get a random password that complies with most of the requirements .
23,258
def get_random_url ( ltd = "com" ) : url = [ "https://" , RandomInputHelper . get_random_value ( 8 , [ string . ascii_lowercase ] ) , "." , ltd ] return "" . join ( url )
Get a random url with the given ltd .
23,259
def get_random_telephonenumber ( ) : phone = [ RandomInputHelper . get_random_value ( 3 , "123456789" ) , RandomInputHelper . get_random_value ( 3 , "12345678" ) , "" . join ( map ( str , random . sample ( range ( 10 ) , 4 ) ) ) ] return "-" . join ( phone )
Get a random 10 digit phone number that complies with most of the requirements .
23,260
def complies_with_scope ( queue_item , new_request , scope ) : if not URLHelper . is_parsable ( queue_item . request . url ) : return False if not URLHelper . is_parsable ( new_request . url ) : return False if scope . request_methods : if not queue_item . request . method in scope . request_methods : return False if s...
Check if the new request complies with the crawling scope .
23,261
def get_cookie_header ( queue_item ) : header = [ ] path = URLHelper . get_path ( queue_item . request . url ) for cookie in queue_item . request . cookies : root_path = cookie . path == "" or cookie . path == "/" if path . startswith ( cookie . path ) or root_path : header . append ( cookie . name + "=" + cookie . val...
Convert a requests cookie jar to a HTTP request cookie header value .
23,262
def get_soup_response ( self ) : if self . response is not None : if self . __response_soup is None : result = BeautifulSoup ( self . response . text , "lxml" ) if self . decomposed : return result else : self . __response_soup = BeautifulSoup ( self . response . text , "lxml" ) return self . __response_soup
Get the response as a cached BeautifulSoup container .
23,263
def get_hash ( self ) : if self . __index_hash : return self . __index_hash key = self . request . method key += URLHelper . get_protocol ( self . request . url ) key += URLHelper . get_subdomain ( self . request . url ) key += URLHelper . get_hostname ( self . request . url ) key += URLHelper . get_tld ( self . reques...
Generate and return the dict index hash of the given queue item .
23,264
def __get_request ( self , host , soup ) : url = URLHelper . make_absolute ( host , self . __trim_grave_accent ( soup [ "action" ] ) ) if soup . has_attr ( "action" ) else host method_original = soup [ "method" ] if soup . has_attr ( "method" ) else "get" method = "post" if method_original . lower ( ) == "post" else "g...
Build a request from the given soup form .
23,265
def __get_form_data ( self , soup ) : elements = self . __get_valid_form_data_elements ( soup ) form_data = self . __get_default_form_data_input ( elements ) callback = self . options . callbacks . form_before_autofill action = callback ( self . queue_item , elements , form_data ) if action == CrawlerActions . DO_AUTOF...
Build a form data dict from the given form .
23,266
def __get_valid_form_data_elements ( self , soup ) : elements = [ ] for element in soup . find_all ( [ "input" , "button" , "textarea" , "select" ] ) : if element . has_attr ( "name" ) : elements . append ( element ) return elements
Get all valid form input elements .
23,267
def __autofill_form_data ( self , form_data , elements ) : for element in elements : if not element [ "name" ] in form_data : continue if not len ( form_data [ element [ "name" ] ] ) is 0 : continue if element . name == "textarea" : form_data [ element [ "name" ] ] = RandomInputHelper . get_for_type ( "textarea" ) cont...
Autofill empty form data with random data .
23,268
def __get_default_value_from_element ( self , element ) : if element . name == "select" : options = element . find_all ( "option" ) is_multiple = element . has_attr ( "multiple" ) selected_options = [ option for option in options if option . has_attr ( "selected" ) ] if not selected_options and options : selected_optio...
Get the default value of a form element
23,269
def append_with_data ( url , data ) : if data is None : return url url_parts = list ( urlparse ( url ) ) query = OrderedDict ( parse_qsl ( url_parts [ 4 ] , keep_blank_values = True ) ) query . update ( data ) url_parts [ 4 ] = URLHelper . query_dict_to_string ( query ) return urlunparse ( url_parts )
Append the given URL with the given data OrderedDict .
23,270
def get_subdomain ( url ) : if url not in URLHelper . __cache : URLHelper . __cache [ url ] = urlparse ( url ) return "." . join ( URLHelper . __cache [ url ] . netloc . split ( "." ) [ : - 2 ] )
Get the subdomain of the given URL .
23,271
def get_hostname ( url ) : if url not in URLHelper . __cache : URLHelper . __cache [ url ] = urlparse ( url ) parts = URLHelper . __cache [ url ] . netloc . split ( "." ) if len ( parts ) == 1 : return parts [ 0 ] else : return "." . join ( parts [ - 2 : - 1 ] )
Get the hostname of the given URL .
23,272
def get_tld ( url ) : if url not in URLHelper . __cache : URLHelper . __cache [ url ] = urlparse ( url ) parts = URLHelper . __cache [ url ] . netloc . split ( "." ) if len ( parts ) == 1 : return "" else : return parts [ - 1 ]
Get the tld of the given URL .
23,273
def get_ordered_params ( url ) : if url not in URLHelper . __cache : URLHelper . __cache [ url ] = urlparse ( url ) params = URLHelper . query_string_to_dict ( URLHelper . __cache [ url ] . query ) return OrderedDict ( sorted ( params . items ( ) ) )
Get the query parameters of the given URL in alphabetical order .
23,274
def query_dict_to_string ( query ) : query_params = [ ] for key , value in query . items ( ) : query_params . append ( key + "=" + value ) return "&" . join ( query_params )
Convert an OrderedDict to a query string .
23,275
def query_string_to_dict ( query ) : query_params = { } for key_value in query . split ( "&" ) : key_value_pair = key_value . split ( "=" , 1 ) key = key_value_pair [ 0 ] if len ( key_value_pair ) >= 1 else "" value = key_value_pair [ 1 ] if len ( key_value_pair ) == 2 else "" query_params [ key ] = value return query_...
Convert a string to a query dict .
23,276
def get_version ( ) : if PackageHelper . __version : return PackageHelper . __version PackageHelper . __version = "Unknown" file = os . path . realpath ( __file__ ) folder = os . path . dirname ( file ) try : semver = open ( folder + "/../../.semver" , "r" ) PackageHelper . __version = semver . read ( ) . rstrip ( ) se...
Get the version number of this package .
23,277
def run ( self ) : try : self . __options . callbacks . request_in_thread_before_start ( self . __queue_item ) except Exception as e : print ( e ) new_requests = [ ] failed = False try : handler = Handler ( self . __options , self . __queue_item ) new_requests = handler . get_new_requests ( ) try : self . __queue_item ...
Executes the HTTP call .
23,278
def start_with ( self , request ) : HTTPRequestHelper . patch_with_options ( request , self . __options ) self . queue . add_request ( request ) self . __crawler_start ( )
Start the crawler using the given request .
23,279
def __spawn_new_requests ( self ) : self . __should_spawn_new_requests = False in_progress_count = len ( self . queue . get_all ( QueueItem . STATUS_IN_PROGRESS ) ) while in_progress_count < self . __options . performance . max_threads : if self . __spawn_new_request ( ) : in_progress_count += 1 else : break if in_prog...
Spawn new requests until the max threads option value is reached .
23,280
def __spawn_new_request ( self ) : first_in_line = self . queue . get_first ( QueueItem . STATUS_QUEUED ) if first_in_line is None : return False while self . routing . is_treshold_reached ( first_in_line . request ) : self . queue . move ( first_in_line , QueueItem . STATUS_CANCELLED ) first_in_line = self . queue . g...
Spawn the first queued request if there is one available .
23,281
def __crawler_start ( self ) : try : self . __options . callbacks . crawler_before_start ( ) except Exception as e : print ( e ) print ( traceback . format_exc ( ) ) self . __spawn_new_requests ( ) while not self . __stopped : if self . __should_stop : self . __crawler_stop ( ) if self . __should_spawn_new_requests : s...
Spawn the first X queued request where X is the max threads option .
23,282
def __crawler_stop ( self ) : if self . __stopping : return self . __stopping = True self . __wait_for_current_threads ( ) self . queue . move_bulk ( [ QueueItem . STATUS_QUEUED , QueueItem . STATUS_IN_PROGRESS ] , QueueItem . STATUS_CANCELLED ) self . __crawler_finish ( ) self . __stopped = True
Mark the crawler as stopped .
23,283
def __crawler_finish ( self ) : try : self . __options . callbacks . crawler_after_finish ( self . queue ) except Exception as e : print ( e ) print ( traceback . format_exc ( ) )
Called when the crawler is finished because there are no queued requests left or it was stopped .
23,284
def __request_start ( self , queue_item ) : try : action = self . __options . callbacks . request_before_start ( self . queue , queue_item ) except Exception as e : action = None print ( e ) print ( traceback . format_exc ( ) ) if action == CrawlerActions . DO_STOP_CRAWLING : self . __should_stop = True if action == Cr...
Execute the request in given queue item .
23,285
def __request_finish ( self , queue_item , new_requests , request_failed = False ) : if self . __stopping : return del self . __threads [ queue_item . get_hash ( ) ] if request_failed : new_queue_items = [ ] self . queue . move ( queue_item , QueueItem . STATUS_ERRORED ) else : self . routing . increase_route_count ( q...
Called when the crawler finished the given queue item .
23,286
def __add_scraped_requests_to_queue ( self , queue_item , scraped_requests ) : new_queue_items = [ ] for scraped_request in scraped_requests : HTTPRequestHelper . patch_with_options ( scraped_request , self . __options , queue_item ) if not HTTPRequestHelper . complies_with_scope ( queue_item , scraped_request , self ....
Convert the scraped requests to queue items return them and also add them to the queue .
23,287
def fit ( self , train_set , test_set ) : with tf . Graph ( ) . as_default ( ) , tf . Session ( ) as self . tf_session : self . build_model ( ) tf . global_variables_initializer ( ) . run ( ) third = self . num_epochs // 3 for i in range ( self . num_epochs ) : lr_decay = self . lr_decay ** max ( i - third , 0.0 ) self...
Fit the model to the given data .
23,288
def _run_train_step ( self , data , mode = 'train' ) : epoch_size = ( ( len ( data ) // self . batch_size ) - 1 ) // self . num_steps costs = 0.0 iters = 0 step = 0 state = self . _init_state . eval ( ) op = self . _train_op if mode == 'train' else tf . no_op ( ) for step , ( x , y ) in enumerate ( utilities . seq_data...
Run a single training step .
23,289
def build_model ( self ) : with tf . variable_scope ( "model" , reuse = None , initializer = self . initializer ) : self . _create_placeholders ( ) self . _create_rnn_cells ( ) self . _create_initstate_and_embeddings ( ) self . _create_rnn_architecture ( ) self . _create_optimizer_node ( )
Build the model s computational graph .
23,290
def _create_placeholders ( self ) : self . input_data = tf . placeholder ( tf . int32 , [ self . batch_size , self . num_steps ] ) self . input_labels = tf . placeholder ( tf . int32 , [ self . batch_size , self . num_steps ] )
Create the computational graph s placeholders .
23,291
def _create_rnn_cells ( self ) : lstm_cell = tf . nn . rnn_cell . LSTMCell ( self . num_hidden , forget_bias = 0.0 ) lstm_cell = tf . nn . rnn_cell . DropoutWrapper ( lstm_cell , output_keep_prob = self . dropout ) self . cell = tf . nn . rnn_cell . MultiRNNCell ( [ lstm_cell ] * self . num_layers )
Create the LSTM cells .
23,292
def _create_initstate_and_embeddings ( self ) : self . _init_state = self . cell . zero_state ( self . batch_size , tf . float32 ) embedding = tf . get_variable ( "embedding" , [ self . vocab_size , self . num_hidden ] ) inputs = tf . nn . embedding_lookup ( embedding , self . input_data ) self . inputs = tf . nn . dro...
Create the initial state for the cell and the data embeddings .
23,293
def _create_rnn_architecture ( self ) : self . inputs = [ tf . squeeze ( i , [ 1 ] ) for i in tf . split ( axis = 1 , num_or_size_splits = self . num_steps , value = self . inputs ) ] outputs , state = tf . nn . rnn ( self . cell , self . inputs , initial_state = self . _init_state ) output = tf . reshape ( tf . concat...
Create the training architecture and the last layer of the LSTM .
23,294
def _create_optimizer_node ( self ) : self . lr_var = tf . Variable ( 0.0 , trainable = False ) tvars = tf . trainable_variables ( ) grads , _ = tf . clip_by_global_norm ( tf . gradients ( self . cost , tvars ) , self . max_grad_norm ) optimizer = tf . train . GradientDescentOptimizer ( self . lr_var ) self . _train_op...
Create the optimizer node of the graph .
23,295
def _create_encoding_layers ( self ) : next_train = self . input_data self . layer_nodes = [ ] for l , layer in enumerate ( self . layers ) : with tf . name_scope ( "encode-{}" . format ( l ) ) : y_act = tf . add ( tf . matmul ( next_train , self . encoding_w_ [ l ] ) , self . encoding_b_ [ l ] ) if self . finetune_enc...
Create the encoding layers for supervised finetuning .
23,296
def _create_decoding_layers ( self ) : next_decode = self . encode for l , layer in reversed ( list ( enumerate ( self . layers ) ) ) : with tf . name_scope ( "decode-{}" . format ( l ) ) : if self . tied_weights : dec_w = tf . transpose ( self . encoding_w_ [ l ] ) else : dec_w = tf . Variable ( tf . transpose ( self ...
Create the decoding layers for reconstruction finetuning .
23,297
def load_mnist_dataset ( mode = 'supervised' , one_hot = True ) : mnist = input_data . read_data_sets ( "MNIST_data/" , one_hot = one_hot ) trX = mnist . train . images trY = mnist . train . labels vlX = mnist . validation . images vlY = mnist . validation . labels teX = mnist . test . images teY = mnist . test . label...
Load the MNIST handwritten digits dataset .
23,298
def load_cifar10_dataset ( cifar_dir , mode = 'supervised' ) : trX = None trY = np . array ( [ ] ) teX = np . array ( [ ] ) teY = np . array ( [ ] ) for fn in os . listdir ( cifar_dir ) : if not fn . startswith ( 'batches' ) and not fn . startswith ( 'readme' ) : fo = open ( os . path . join ( cifar_dir , fn ) , 'rb' )...
Load the cifar10 dataset .
23,299
def linear ( prev_layer , out_dim , name = "linear" ) : with tf . name_scope ( name ) : in_dim = prev_layer . get_shape ( ) [ 1 ] . value W = tf . Variable ( tf . truncated_normal ( [ in_dim , out_dim ] , stddev = 0.1 ) ) b = tf . Variable ( tf . constant ( 0.1 , shape = [ out_dim ] ) ) out = tf . add ( tf . matmul ( p...
Create a linear fully - connected layer .