idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
52,000
def merge_pages ( self , replacements ) : warnings . warn ( "merge_pages has been deprecated in favour of merge_templates" , category = DeprecationWarning , stacklevel = 2 ) self . merge_templates ( replacements , "page_break" )
Deprecated method .
52,001
def importpath ( path , error_text = None ) : result = None attrs = [ ] parts = path . split ( '.' ) exception = None while parts : try : result = __import__ ( '.' . join ( parts ) , { } , { } , [ '' ] ) except ImportError as e : if exception is None : exception = e attrs = parts [ - 1 : ] + attrs parts = parts [ : - 1...
Import value by specified path . Value can represent module class object attribute or method . If error_text is not None and import will raise ImproperlyConfigured with user friendly text .
52,002
def get_booking ( request ) : booking = None if request . user . is_authenticated ( ) : try : booking = Booking . objects . get ( user = request . user , booking_status__slug = 'inprogress' ) except Booking . DoesNotExist : pass else : session = Session . objects . get ( session_key = request . session . session_key ) ...
Returns the booking that is in progress for the current user or None
52,003
def persist_booking ( booking , user ) : if booking is not None : existing_bookings = Booking . objects . filter ( user = user , booking_status__slug = 'inprogress' ) . exclude ( pk = booking . pk ) existing_bookings . delete ( ) booking . session = None booking . user = user booking . save ( )
Ties an in - progress booking from a session to a user when the user logs in .
52,004
def compare_config ( self , target , init = True , indent_level = 0 ) : if init : fwd = self . full_path_fwd bwd = self . full_path_bwd else : fwd = self . rel_path_fwd bwd = self . rel_path_bwd indent = 4 * indent_level * ' ' if indent_level == 0 and self . vdom is not None : if self . vdom == 'global' : pre = 'conf g...
This method will return all the necessary commands to get from the config we are in to the target config .
52,005
def to_text ( self , relative = False , indent_level = 0 , clean_empty_block = False ) : if relative : fwd = self . rel_path_fwd bwd = self . rel_path_bwd else : fwd = self . full_path_fwd bwd = self . full_path_bwd indent = 4 * indent_level * ' ' pre = '%s%s' % ( indent , fwd ) post = '%s%s' % ( indent , bwd ) text = ...
This method returns the object model in text format . You should be able to copy&paste this text into any device running a supported version of FortiOS .
52,006
def open ( self ) : logger . debug ( 'Connecting to device %s, vdom %s' % ( self . hostname , self . vdom ) ) self . ssh = paramiko . SSHClient ( ) self . ssh . set_missing_host_key_policy ( paramiko . AutoAddPolicy ( ) ) cfg = { 'hostname' : self . hostname , 'timeout' : self . timeout , 'username' : self . username ,...
Opens the ssh session with the device .
52,007
def _read_wrapper ( data ) : if isinstance ( data , int ) : data = chr ( data ) return py23_compat . text_type ( data )
Ensure unicode always returned on read .
52,008
def _parse_batch_lastlog ( last_log ) : regexp = re . compile ( '(-?[0-9]\d*):\W+(.*)' ) wrong_commands = list ( ) for line in last_log : result = regexp . match ( line ) if result is not None : status_code = result . group ( 1 ) command = result . group ( 2 ) if int ( status_code ) < 0 : wrong_commands . append ( ( st...
This static method will help reading the result of the commit command by command .
52,009
def _reload_config ( self , reload_original_config ) : if reload_original_config : self . original_config = self . running_config self . original_config . set_name ( 'original' ) paths = self . running_config . get_paths ( ) self . running_config = FortiConfig ( 'running' , vdom = self . vdom ) for path in paths : self...
This command will update the running config from the live device .
52,010
def generate_states ( self , initial_condition = None , with_noise = True , stateseq = None ) : from pybasicbayes . util . stats import sample_discrete T , K , n = self . T , self . num_states , self . D_latent A = self . trans_matrix dss = - 1 * np . ones ( T , dtype = np . int32 ) if stateseq is None else stateseq . ...
Jointly sample the discrete and continuous states
52,011
def heldout_log_likelihood ( self , test_mask = None ) : if test_mask is None : if self . mask is None : return 0 else : test_mask = ~ self . mask xs = np . hstack ( ( self . gaussian_states , self . inputs ) ) if self . single_emission : return self . emission_distns [ 0 ] . log_likelihood ( ( xs , self . data ) , mas...
Compute the log likelihood of the masked data given the latent discrete and continuous states .
52,012
def empirical_rate ( data , sigma = 3.0 ) : from scipy . ndimage . filters import gaussian_filter1d return 0.001 + gaussian_filter1d ( data . astype ( np . float ) , sigma , axis = 0 )
Smooth count data to get an empirical rate
52,013
def get_empirical_ar_params ( train_datas , params ) : assert isinstance ( train_datas , list ) and len ( train_datas ) > 0 datadimension = train_datas [ 0 ] . shape [ 1 ] assert params [ "nu_0" ] > datadimension + 1 obs_params = dict ( nu_0 = params [ "nu_0" ] , S_0 = params [ 'S_0' ] , M_0 = params [ 'M_0' ] , K_0 = ...
Estimate the parameters of an AR observation model by fitting a single AR model to the entire dataset .
52,014
def capture ( target_url , user_agent = "savepagenow (https://github.com/pastpages/savepagenow)" , accept_cache = False ) : domain = "https://web.archive.org" save_url = urljoin ( domain , "/save/" ) request_url = save_url + target_url headers = { 'User-Agent' : user_agent , } response = requests . get ( request_url , ...
Archives the provided URL using archive . org s Wayback Machine .
52,015
def capture_or_cache ( target_url , user_agent = "savepagenow (https://github.com/pastpages/savepagenow)" ) : try : return capture ( target_url , user_agent = user_agent , accept_cache = False ) , True except CachedPage : return capture ( target_url , user_agent = user_agent , accept_cache = True ) , False
Archives the provided URL using archive . org s Wayback Machine unless the page has been recently captured .
52,016
def get_unique_id ( element ) : this_id = make_id ( element ) dup = True while dup : if this_id not in ids : dup = False ids . append ( this_id ) else : this_id = make_id ( element ) return ids [ - 1 ]
Returns a unique id for a given element
52,017
def get_xml_type ( val ) : if type ( val ) . __name__ in ( 'str' , 'unicode' ) : return 'str' if type ( val ) . __name__ in ( 'int' , 'long' ) : return 'int' if type ( val ) . __name__ == 'float' : return 'float' if type ( val ) . __name__ == 'bool' : return 'bool' if isinstance ( val , numbers . Number ) : return 'num...
Returns the data type for the xml type attribute
52,018
def make_attrstring ( attr ) : attrstring = ' ' . join ( [ '%s="%s"' % ( k , v ) for k , v in attr . items ( ) ] ) return '%s%s' % ( ' ' if attrstring != '' else '' , attrstring )
Returns an attribute string in the form key = val
52,019
def key_is_valid_xml ( key ) : LOG . info ( 'Inside key_is_valid_xml(). Testing "%s"' % ( unicode_me ( key ) ) ) test_xml = '<?xml version="1.0" encoding="UTF-8" ?><%s>foo</%s>' % ( key , key ) try : parseString ( test_xml ) return True except Exception : return False
Checks that a key is a valid XML name
52,020
def make_valid_xml_name ( key , attr ) : LOG . info ( 'Inside make_valid_xml_name(). Testing key "%s" with attr "%s"' % ( unicode_me ( key ) , unicode_me ( attr ) ) ) key = escape_xml ( key ) attr = escape_xml ( attr ) if key_is_valid_xml ( key ) : return key , attr if key . isdigit ( ) : return 'n%s' % ( key ) , attr ...
Tests an XML name and fixes it if invalid
52,021
def convert ( obj , ids , attr_type , item_func , cdata , parent = 'root' ) : LOG . info ( 'Inside convert(). obj type is: "%s", obj="%s"' % ( type ( obj ) . __name__ , unicode_me ( obj ) ) ) item_name = item_func ( parent ) if isinstance ( obj , numbers . Number ) or type ( obj ) in ( str , unicode ) : return convert_...
Routes the elements of an object to the right function to convert them based on their data type
52,022
def convert_dict ( obj , ids , parent , attr_type , item_func , cdata ) : LOG . info ( 'Inside convert_dict(): obj type is: "%s", obj="%s"' % ( type ( obj ) . __name__ , unicode_me ( obj ) ) ) output = [ ] addline = output . append item_name = item_func ( parent ) for key , val in obj . items ( ) : LOG . info ( 'Loopin...
Converts a dict into an XML string .
52,023
def convert_list ( items , ids , parent , attr_type , item_func , cdata ) : LOG . info ( 'Inside convert_list()' ) output = [ ] addline = output . append item_name = item_func ( parent ) if ids : this_id = get_unique_id ( parent ) for i , item in enumerate ( items ) : LOG . info ( 'Looping inside convert_list(): item="...
Converts a list into an XML string .
52,024
def convert_kv ( key , val , attr_type , attr = { } , cdata = False ) : LOG . info ( 'Inside convert_kv(): key="%s", val="%s", type(val) is: "%s"' % ( unicode_me ( key ) , unicode_me ( val ) , type ( val ) . __name__ ) ) key , attr = make_valid_xml_name ( key , attr ) if attr_type : attr [ 'type' ] = get_xml_type ( val...
Converts a number or string into an XML element
52,025
def convert_bool ( key , val , attr_type , attr = { } , cdata = False ) : LOG . info ( 'Inside convert_bool(): key="%s", val="%s", type(val) is: "%s"' % ( unicode_me ( key ) , unicode_me ( val ) , type ( val ) . __name__ ) ) key , attr = make_valid_xml_name ( key , attr ) if attr_type : attr [ 'type' ] = get_xml_type (...
Converts a boolean into an XML element
52,026
def convert_none ( key , val , attr_type , attr = { } , cdata = False ) : LOG . info ( 'Inside convert_none(): key="%s"' % ( unicode_me ( key ) ) ) key , attr = make_valid_xml_name ( key , attr ) if attr_type : attr [ 'type' ] = get_xml_type ( val ) attrstring = make_attrstring ( attr ) return '<%s%s></%s>' % ( key , a...
Converts a null value into an XML element
52,027
def getCallSourceLines ( callFrame , icNames , icMethod ) : code = callFrame . f_code try : if code . co_name == '<module>' : parentBlockStartLine = 1 lines = inspect . findsource ( code ) [ 0 ] parentBlockSource = '' . join ( lines ) else : parentBlockStartLine = code . co_firstlineno parentBlockSource = inspect . get...
Raises NoSourceAvailableError .
52,028
def init_websocket ( self , event_handler , websocket_cls = Websocket ) : self . websocket = websocket_cls ( self . options , self . client . token ) loop = asyncio . get_event_loop ( ) loop . run_until_complete ( self . websocket . connect ( event_handler ) ) return loop
Will initialize the websocket connection to the mattermost server .
52,029
def login ( self ) : if self . options [ 'token' ] : self . client . token = self . options [ 'token' ] result = self . users . get_user ( 'me' ) else : response = self . users . login_user ( { 'login_id' : self . options [ 'login_id' ] , 'password' : self . options [ 'password' ] , 'token' : self . options [ 'mfa_toke...
Logs the user in .
52,030
def connect ( self , event_handler ) : context = ssl . create_default_context ( purpose = ssl . Purpose . CLIENT_AUTH ) if not self . options [ 'verify' ] : context . verify_mode = ssl . CERT_NONE scheme = 'wss://' if self . options [ 'scheme' ] != 'https' : scheme = 'ws://' context = None url = '{scheme:s}{url:s}:{por...
Connect to the websocket and authenticate it . When the authentication has finished start the loop listening for messages sending a ping to the server to keep the connection alive .
52,031
def _authenticate_websocket ( self , websocket , event_handler ) : log . debug ( 'Authenticating websocket' ) json_data = json . dumps ( { "seq" : 1 , "action" : "authentication_challenge" , "data" : { "token" : self . _token } } ) . encode ( 'utf8' ) yield from websocket . send ( json_data ) while True : message = yie...
Sends a authentication challenge over a websocket . This is not needed when we just send the cookie we got on login when connecting to the websocket .
52,032
def run_cell ( self , cell ) : logging . info ( 'Running cell:\n%s\n' , cell . input ) self . kc . execute ( cell . input ) reply = self . kc . get_shell_msg ( ) status = reply [ 'content' ] [ 'status' ] traceback_text = '' if status == 'error' : traceback_text = 'Cell raised uncaught exception: \n' + '\n' . join ( rep...
Run a notebook cell and update the output of that cell in - place .
52,033
def iter_code_cells ( self ) : for ws in self . nb . worksheets : for cell in ws . cells : if cell . cell_type == 'code' : yield cell
Iterate over the notebook cells containing code .
52,034
def run_notebook ( self , skip_exceptions = False , progress_callback = None ) : for i , cell in enumerate ( self . iter_code_cells ( ) ) : try : self . run_cell ( cell ) except NotebookError : if not skip_exceptions : raise if progress_callback : progress_callback ( i )
Run all the notebook cells in order and update the outputs in - place .
52,035
def config ( filename ) : Config = collections . namedtuple ( 'Config' , [ 'git' , 'lock_file' , 'version' , 'name' , 'src' , 'dst' , 'files' , 'post_commands' , ] ) return [ Config ( ** d ) for d in _get_config_generator ( filename ) ]
Construct Config object and return a list .
52,036
def _get_files_config ( src_dir , files_list ) : FilesConfig = collections . namedtuple ( 'FilesConfig' , [ 'src' , 'dst' , 'post_commands' ] ) return [ FilesConfig ( ** d ) for d in _get_files_generator ( src_dir , files_list ) ]
Construct FileConfig object and return a list .
52,037
def _get_config ( filename ) : i = interpolation . Interpolator ( interpolation . TemplateWithDefaults , os . environ ) with open ( filename , 'r' ) as stream : try : interpolated_config = i . interpolate ( stream . read ( ) ) return yaml . safe_load ( interpolated_config ) except yaml . parser . ParserError as e : msg...
Parse the provided YAML file and return a dict .
52,038
def _get_dst_dir ( dst_dir ) : wd = os . getcwd ( ) _makedirs ( dst_dir ) return os . path . join ( wd , dst_dir )
Prefix the provided string with working directory and return a str .
52,039
def _makedirs ( path ) : dirname , _ = os . path . split ( path ) try : os . makedirs ( dirname ) except OSError as exc : if exc . errno == errno . EEXIST : pass else : raise
Create a base directory of the provided path and return None .
52,040
def main ( ctx , config , debug ) : ctx . obj = { } ctx . obj [ 'args' ] = { } ctx . obj [ 'args' ] [ 'debug' ] = debug ctx . obj [ 'args' ] [ 'config' ] = config
gilt - A GIT layering tool .
52,041
def overlay ( ctx ) : args = ctx . obj . get ( 'args' ) filename = args . get ( 'config' ) debug = args . get ( 'debug' ) _setup ( filename ) for c in config . config ( filename ) : with fasteners . InterProcessLock ( c . lock_file ) : util . print_info ( '{}:' . format ( c . name ) ) if not os . path . exists ( c . sr...
Install gilt dependencies
52,042
def clone ( name , repository , destination , debug = False ) : msg = ' - cloning {} to {}' . format ( name , destination ) util . print_info ( msg ) cmd = sh . git . bake ( 'clone' , repository , destination ) util . run_command ( cmd , debug = debug )
Clone the specified repository into a temporary directory and return None .
52,043
def _get_version ( version , debug = False ) : if not any ( ( _has_branch ( version , debug ) , _has_tag ( version , debug ) , _has_commit ( version , debug ) ) ) : cmd = sh . git . bake ( 'fetch' ) util . run_command ( cmd , debug = debug ) cmd = sh . git . bake ( 'checkout' , version ) util . run_command ( cmd , debu...
Handle switching to the specified version and return None .
52,044
def _has_commit ( version , debug = False ) : if _has_tag ( version , debug ) or _has_branch ( version , debug ) : return False cmd = sh . git . bake ( 'cat-file' , '-e' , version ) try : util . run_command ( cmd , debug = debug ) return True except sh . ErrorReturnCode : return False
Determine a version is a local git commit sha or not .
52,045
def _has_tag ( version , debug = False ) : cmd = sh . git . bake ( 'show-ref' , '--verify' , '--quiet' , "refs/tags/{}" . format ( version ) ) try : util . run_command ( cmd , debug = debug ) return True except sh . ErrorReturnCode : return False
Determine a version is a local git tag name or not .
52,046
def run_command ( cmd , debug = False ) : if debug : msg = ' PWD: {}' . format ( os . getcwd ( ) ) print_warn ( msg ) msg = ' COMMAND: {}' . format ( cmd ) print_warn ( msg ) cmd ( )
Execute the given command and return None .
52,047
def build_sh_cmd ( cmd , cwd = None ) : args = cmd . split ( ) return getattr ( sh , args [ 0 ] ) . bake ( _cwd = cwd , * args [ 1 : ] )
Build a sh . Command from a string .
52,048
def copy ( src , dst ) : try : shutil . copytree ( src , dst ) except OSError as exc : if exc . errno == errno . ENOTDIR : shutil . copy ( src , dst ) else : raise
Handle the copying of a file or directory .
52,049
def to_dict ( self ) : return { 'schema' : self . schema , 'name' : self . name , 'columns' : [ col . to_dict ( ) for col in self . _columns ] , 'foreign_keys' : self . foreign_keys . to_dict ( ) , 'ref_keys' : self . ref_keys . to_dict ( ) }
Serialize representation of the table for local caching .
52,050
def list_profiles ( ) : profiles = { } user = os . path . expanduser ( "~" ) for f in os . listdir ( user ) : if f . startswith ( ".db.py_" ) : profile = load_from_json ( os . path . join ( user , f ) ) tables = profile . pop ( 'tables' , None ) if tables : profile [ 'metadata' ] = True else : profile [ 'metadata' ] = ...
Lists all of the database profiles available
52,051
def remove_profile ( name , s3 = False ) : user = os . path . expanduser ( "~" ) if s3 : f = os . path . join ( user , S3_PROFILE_ID + name ) else : f = os . path . join ( user , DBPY_PROFILE_ID + name ) try : try : open ( f ) except : raise Exception ( "Profile '{0}' does not exist. Could not find file {1}" . format (...
Removes a profile from your config
52,052
def tables ( self ) : if len ( self . _tables ) == 0 : self . refresh_schema ( self . _exclude_system_tables , self . _use_cache ) return self . _tables
A lazy loaded reference to the table metadata for the DB .
52,053
def save_credentials ( self , profile = "default" ) : f = profile_path ( DBPY_PROFILE_ID , profile ) dump_to_json ( f , self . credentials )
Save your database credentials so you don t have to save them in script .
52,054
def save_metadata ( self , profile = "default" ) : if len ( self . tables ) > 0 : f = profile_path ( DBPY_PROFILE_ID , profile ) dump_to_json ( f , self . to_dict ( ) )
Save the database credentials plus the database properties to your db . py profile .
52,055
def credentials ( self ) : if self . filename : db_filename = os . path . join ( os . getcwd ( ) , self . filename ) else : db_filename = None return { "username" : self . username , "password" : self . password , "hostname" : self . hostname , "port" : self . port , "filename" : db_filename , "dbname" : self . dbname ...
Dict representation of all credentials for the database .
52,056
def find_table ( self , search ) : tables = [ ] for table in self . tables : if glob . fnmatch . fnmatch ( table . name , search ) : tables . append ( table ) return TableSet ( tables )
Aggresively search through your database s schema for a table .
52,057
def find_column ( self , search , data_type = None ) : if isinstance ( data_type , str ) : data_type = [ data_type ] cols = [ ] for table in self . tables : for col in vars ( table ) : if glob . fnmatch . fnmatch ( col , search ) : if data_type and isinstance ( getattr ( table , col ) , Column ) and getattr ( table , c...
Aggresively search through your database s schema for a column .
52,058
def query ( self , q , data = None , union = True , limit = None ) : if data : q = self . _apply_handlebars ( q , data , union ) if limit : q = self . _assign_limit ( q , limit ) return pd . read_sql ( q , self . con )
Query your database with a raw string .
52,059
def query_from_file ( self , filename , data = None , union = True , limit = None ) : with open ( filename ) as fp : q = fp . read ( ) return self . query ( q , data = data , union = union , limit = limit )
Query your database from a file .
52,060
def refresh_schema ( self , exclude_system_tables = True , use_cache = False ) : col_meta , table_meta = self . _get_db_metadata ( exclude_system_tables , use_cache ) tables = self . _gen_tables_from_col_tuples ( col_meta ) if use_cache : self . _tables = TableSet ( [ Table ( self . con , self . _query_templates , tabl...
Pulls your database s schema again and looks for any new tables and columns .
52,061
def to_dict ( self ) : db_dict = self . credentials db_dict . update ( self . tables . to_dict ( ) ) return db_dict
Dict representation of the database as credentials plus tables dict representation .
52,062
def profile_path ( profile_id , profile ) : user = os . path . expanduser ( "~" ) return os . path . join ( user , profile_id + profile )
Create full path to given provide for the current user .
52,063
def load_from_json ( file_path ) : if os . path . exists ( file_path ) : raw_data = open ( file_path , 'rb' ) . read ( ) return json . loads ( base64 . decodestring ( raw_data ) . decode ( 'utf-8' ) )
Load the stored data from json and return as a dict .
52,064
def save_credentials ( self , profile ) : filename = profile_path ( S3_PROFILE_ID , profile ) creds = { "access_key" : self . access_key , "secret_key" : self . secret_key } dump_to_json ( filename , creds )
Saves credentials to a dotfile so you can open them grab them later .
52,065
def to_dict ( self ) : return { 'schema' : self . schema , 'table' : self . table , 'name' : self . name , 'type' : self . type }
Serialize representation of the column for local caching .
52,066
def rate_limit ( self , rate_limit ) : self . _rate_limit = bool ( rate_limit ) self . _rate_limit_last_call = None self . clear_memoized ( )
Turn on or off rate limiting
52,067
def language ( self , lang ) : lang = lang . lower ( ) if self . _lang == lang : return url = self . _api_url tmp = url . replace ( "/{0}." . format ( self . _lang ) , "/{0}." . format ( lang ) ) self . _api_url = tmp self . _lang = lang self . clear_memoized ( )
Set the language to use ; attempts to change the API URL
52,068
def refresh_interval ( self , refresh_interval ) : if isinstance ( refresh_interval , int ) and refresh_interval > 0 : self . _refresh_interval = refresh_interval else : self . _refresh_interval = None
Set the new cache refresh interval
52,069
def login ( self , username , password , strict = True ) : params = { "action" : "query" , "meta" : "tokens" , "type" : "login" , "format" : "json" , } token_res = self . _get_response ( params ) if "query" in token_res and "tokens" in token_res [ "query" ] : token = token_res [ "query" ] [ "tokens" ] [ "logintoken" ] ...
Login as specified user
52,070
def set_api_url ( self , api_url = "https://{lang}.wikipedia.org/w/api.php" , lang = "en" ) : old_api_url = self . _api_url old_lang = self . _lang self . _lang = lang . lower ( ) self . _api_url = api_url . format ( lang = self . _lang ) try : self . _get_site_info ( ) self . __supported_languages = None except MediaW...
Set the API URL and language
52,071
def _reset_session ( self ) : headers = { "User-Agent" : self . _user_agent } self . _session = requests . Session ( ) self . _session . headers . update ( headers ) self . _is_logged_in = False
Set session information
52,072
def random ( self , pages = 1 ) : if pages is None or pages < 1 : raise ValueError ( "Number of pages must be greater than 0" ) query_params = { "list" : "random" , "rnnamespace" : 0 , "rnlimit" : pages } request = self . wiki_request ( query_params ) titles = [ page [ "title" ] for page in request [ "query" ] [ "rando...
Request a random page title or list of random titles
52,073
def search ( self , query , results = 10 , suggestion = False ) : self . _check_query ( query , "Query must be specified" ) search_params = { "list" : "search" , "srprop" : "" , "srlimit" : results , "srsearch" : query , } if suggestion : search_params [ "srinfo" ] = "suggestion" raw_results = self . wiki_request ( sea...
Search for similar titles
52,074
def suggest ( self , query ) : res , suggest = self . search ( query , results = 1 , suggestion = True ) try : title = suggest or res [ 0 ] except IndexError : title = None return title
Gather suggestions based on the provided title or None if no suggestions found
52,075
def geosearch ( self , latitude = None , longitude = None , radius = 1000 , title = None , auto_suggest = True , results = 10 , ) : def test_lat_long ( val ) : if not isinstance ( val , Decimal ) : error = ( "Latitude and Longitude must be specified either as " "a Decimal or in formats that can be coerced into " "a Dec...
Search for pages that relate to the provided geocoords or near the page
52,076
def opensearch ( self , query , results = 10 , redirect = True ) : self . _check_query ( query , "Query must be specified" ) query_params = { "action" : "opensearch" , "search" : query , "limit" : ( 100 if results > 100 else results ) , "redirects" : ( "resolve" if redirect else "return" ) , "warningsaserror" : True , ...
Execute a MediaWiki opensearch request similar to search box suggestions and conforming to the OpenSearch specification
52,077
def prefixsearch ( self , prefix , results = 10 ) : self . _check_query ( prefix , "Prefix must be specified" ) query_params = { "list" : "prefixsearch" , "pssearch" : prefix , "pslimit" : ( "max" if results > 500 else results ) , "psnamespace" : 0 , "psoffset" : 0 , } raw_results = self . wiki_request ( query_params )...
Perform a prefix search using the provided prefix string
52,078
def summary ( self , title , sentences = 0 , chars = 0 , auto_suggest = True , redirect = True ) : page_info = self . page ( title , auto_suggest = auto_suggest , redirect = redirect ) return page_info . summarize ( sentences , chars )
Get the summary for the title in question
52,079
def categorytree ( self , category , depth = 5 ) : def __cat_tree_rec ( cat , depth , tree , level , categories , links ) : tree [ cat ] = dict ( ) tree [ cat ] [ "depth" ] = level tree [ cat ] [ "sub-categories" ] = dict ( ) tree [ cat ] [ "links" ] = list ( ) tree [ cat ] [ "parent-categories" ] = list ( ) parent_cat...
Generate the Category Tree for the given categories
52,080
def page ( self , title = None , pageid = None , auto_suggest = True , redirect = True , preload = False ) : if ( title is None or title . strip ( ) == "" ) and pageid is None : raise ValueError ( "Either a title or a pageid must be specified" ) elif title : if auto_suggest : temp_title = self . suggest ( title ) if te...
Get MediaWiki page based on the provided title or pageid
52,081
def wiki_request ( self , params ) : params [ "format" ] = "json" if "action" not in params : params [ "action" ] = "query" limit = self . _rate_limit last_call = self . _rate_limit_last_call if limit and last_call and last_call + self . _min_wait > datetime . now ( ) : wait_time = ( last_call + self . _min_wait ) - da...
Make a request to the MediaWiki API using the given search parameters
52,082
def _get_site_info ( self ) : response = self . wiki_request ( { "meta" : "siteinfo" , "siprop" : "extensions|general" } ) query = response . get ( "query" , None ) if query is None or query . get ( "general" , None ) is None : raise MediaWikiException ( "Missing query in response" ) gen = query . get ( "general" , Non...
Parse out the Wikimedia site information including API Version and Extensions
52,083
def _check_error_response ( response , query ) : if "error" in response : http_error = [ "HTTP request timed out." , "Pool queue is full" ] geo_error = [ "Page coordinates unknown." , "One of the parameters gscoord, gspage, gsbbox is required" , "Invalid coordinate provided" , ] err = response [ "error" ] [ "info" ] if...
check for default error messages and throw correct exception
52,084
def _get_response ( self , params ) : return self . _session . get ( self . _api_url , params = params , timeout = self . _timeout ) . json ( encoding = "utf8" )
wrap the call to the requests package
52,085
def _post_response ( self , params ) : return self . _session . post ( self . _api_url , data = params , timeout = self . _timeout ) . json ( encoding = "utf8" )
wrap a post call to the requests package
52,086
def parse_all_arguments ( func ) : args = dict ( ) if sys . version_info < ( 3 , 0 ) : func_args = inspect . getargspec ( func ) if func_args . defaults is not None : val = len ( func_args . defaults ) for i , itm in enumerate ( func_args . args [ - val : ] ) : args [ itm ] = func_args . defaults [ i ] else : func_args...
determine all positional and named arguments as a dict
52,087
def str_or_unicode ( text ) : encoding = sys . stdout . encoding if sys . version_info > ( 3 , 0 ) : return text . encode ( encoding ) . decode ( encoding ) return text . encode ( encoding )
handle python 3 unicode and python 2 . 7 byte strings
52,088
def is_relative_url ( url ) : if url . startswith ( "#" ) : return None if url . find ( "://" ) > 0 or url . startswith ( "//" ) : return False return True
simple method to determine if a url is relative or absolute
52,089
def read_file ( filepath ) : with io . open ( filepath , "r" ) as filepointer : res = filepointer . read ( ) return res
read the file
52,090
def _pull_content_revision_parent ( self ) : if self . _revision_id is None : query_params = { "prop" : "extracts|revisions" , "explaintext" : "" , "rvprop" : "ids" , } query_params . update ( self . __title_query_param ( ) ) request = self . mediawiki . wiki_request ( query_params ) page_info = request [ "query" ] [ "...
combine the pulling of these three properties
52,091
def section ( self , section_title ) : section = "== {0} ==" . format ( section_title ) try : content = self . content index = content . index ( section ) + len ( section ) while True : if content [ index + 1 ] == "=" : index += 1 else : break except ValueError : return None except IndexError : pass try : next_index = ...
Plain text section content
52,092
def parse_section_links ( self , section_title ) : soup = BeautifulSoup ( self . html , "html.parser" ) headlines = soup . find_all ( "span" , { "class" : "mw-headline" } ) tmp_soup = BeautifulSoup ( section_title , "html.parser" ) tmp_sec_title = tmp_soup . get_text ( ) . lower ( ) id_tag = None for headline in headli...
Parse all links within a section
52,093
def __load ( self , redirect = True , preload = False ) : query_params = { "prop" : "info|pageprops" , "inprop" : "url" , "ppprop" : "disambiguation" , "redirects" : "" , } query_params . update ( self . __title_query_param ( ) ) request = self . mediawiki . wiki_request ( query_params ) query = request [ "query" ] pag...
load the basic page information
52,094
def _raise_page_error ( self ) : if hasattr ( self , "title" ) : raise PageError ( title = self . title ) else : raise PageError ( pageid = self . pageid )
raise the correct type of page error
52,095
def _raise_disambiguation_error ( self , page , pageid ) : query_params = { "prop" : "revisions" , "rvprop" : "content" , "rvparse" : "" , "rvlimit" : 1 , } query_params . update ( self . __title_query_param ( ) ) request = self . mediawiki . wiki_request ( query_params ) html = request [ "query" ] [ "pages" ] [ pageid...
parse and throw a disambiguation error
52,096
def _parse_section_links ( self , id_tag ) : soup = BeautifulSoup ( self . html , "html.parser" ) info = soup . find ( "span" , { "id" : id_tag } ) all_links = list ( ) if info is None : return all_links for node in soup . find ( id = id_tag ) . parent . next_siblings : if not isinstance ( node , Tag ) : continue elif ...
given a section id parse the links in the unordered list
52,097
def _parse_sections ( self ) : def _list_to_dict ( _dict , path , sec ) : tmp = _dict for elm in path [ : - 1 ] : tmp = tmp [ elm ] tmp [ sec ] = OrderedDict ( ) self . _sections = list ( ) section_regexp = r"\n==* .* ==*\n" found_obj = re . findall ( section_regexp , self . content ) res = OrderedDict ( ) path = list ...
parse sections and TOC
52,098
def __pull_combined_properties ( self ) : query_params = { "titles" : self . title , "prop" : "extracts|redirects|links|coordinates|categories|extlinks" , "continue" : dict ( ) , "explaintext" : "" , "exintro" : "" , "rdprop" : "title" , "rdlimit" : "max" , "plnamespace" : 0 , "pllimit" : "max" , "colimit" : "max" , "c...
something here ...
52,099
def is_armed ( self ) : alarm_code = self . get_armed_status ( ) if alarm_code == YALE_STATE_ARM_FULL : return True if alarm_code == YALE_STATE_ARM_PARTIAL : return True return False
Return True or False if the system is armed in any way