idx
int64
0
63k
question
stringlengths
61
4.03k
target
stringlengths
6
1.23k
21,000
def report_invalid_syntax ( self ) : ( exc_type , exc ) = sys . exc_info ( ) [ : 2 ] if len ( exc . args ) > 1 : offset = exc . args [ 1 ] if len ( offset ) > 2 : offset = offset [ 1 : 3 ] else : offset = ( 1 , 0 ) self . report_error ( offset [ 0 ] , offset [ 1 ] or 0 , 'E901 %s: %s' % ( exc_type . __name__ , exc . ar...
Check if the syntax is valid .
21,001
def run_check ( self , check , argument_names ) : arguments = [ ] for name in argument_names : arguments . append ( getattr ( self , name ) ) return check ( * arguments )
Run a check plugin .
21,002
def init_checker_state ( self , name , argument_names ) : if 'checker_state' in argument_names : self . checker_state = self . _checker_states . setdefault ( name , { } )
Prepare custom state for the specific checker plugin .
21,003
def check_ast ( self ) : try : tree = compile ( '' . join ( self . lines ) , '' , 'exec' , PyCF_ONLY_AST ) except ( ValueError , SyntaxError , TypeError ) : return self . report_invalid_syntax ( ) for name , cls , __ in self . _ast_checks : checker = cls ( tree , self . filename ) for lineno , offset , text , check in ...
Build the file s AST and run all AST checks .
21,004
def generate_tokens ( self ) : if self . _io_error : self . report_error ( 1 , 0 , 'E902 %s' % self . _io_error , readlines ) tokengen = tokenize . generate_tokens ( self . readline ) try : for token in tokengen : if token [ 2 ] [ 0 ] > self . total_lines : return self . noqa = token [ 4 ] and noqa ( token [ 4 ] ) self...
Tokenize the file run physical line checks and yield tokens .
21,005
def get_count ( self , prefix = '' ) : return sum ( [ self . counters [ key ] for key in self . messages if key . startswith ( prefix ) ] )
Return the total count of errors and warnings .
21,006
def get_file_results ( self ) : self . _deferred_print . sort ( ) for line_number , offset , code , text , doc in self . _deferred_print : print ( self . _fmt % { 'path' : self . filename , 'row' : self . line_offset + line_number , 'col' : offset + 1 , 'code' : code , 'text' : text , } ) if self . _show_source : if li...
Print the result and return the overall count for this file .
21,007
def init_report ( self , reporter = None ) : self . options . report = ( reporter or self . options . reporter ) ( self . options ) return self . options . report
Initialize the report instance .
21,008
def check_files ( self , paths = None ) : if paths is None : paths = self . paths report = self . options . report runner = self . runner report . start ( ) try : for path in paths : if os . path . isdir ( path ) : self . input_dir ( path ) elif not self . excluded ( path ) : runner ( path ) except KeyboardInterrupt : ...
Run all checks on the paths .
21,009
def input_dir ( self , dirname ) : dirname = dirname . rstrip ( '/' ) if self . excluded ( dirname ) : return 0 counters = self . options . report . counters verbose = self . options . verbose filepatterns = self . options . filename runner = self . runner for root , dirs , files in os . walk ( dirname ) : if verbose :...
Check all files in this directory and all subdirectories .
21,010
def ignore_code ( self , code ) : if len ( code ) < 4 and any ( s . startswith ( code ) for s in self . options . select ) : return False return ( code . startswith ( self . options . ignore ) and not code . startswith ( self . options . select ) )
Check if the error code should be ignored .
21,011
def get_checks ( self , argument_name ) : checks = [ ] for check , attrs in _checks [ argument_name ] . items ( ) : ( codes , args ) = attrs if any ( not ( code and self . ignore_code ( code ) ) for code in codes ) : checks . append ( ( check . __name__ , check , args ) ) return sorted ( checks )
Get all the checks for this category .
21,012
def loads ( data , use_datetime = 0 ) : p , u = getparser ( use_datetime = use_datetime ) p . feed ( data ) p . close ( ) return u . close ( ) , u . getmethodname ( )
data - > unmarshalled data method name
21,013
def transform_import ( self , node , results ) : import_mod = results . get ( "module" ) pref = import_mod . prefix names = [ ] for name in MAPPING [ import_mod . value ] [ : - 1 ] : names . extend ( [ Name ( name [ 0 ] , prefix = pref ) , Comma ( ) ] ) names . append ( Name ( MAPPING [ import_mod . value ] [ - 1 ] [ 0...
Transform for the basic import case . Replaces the old import name with a comma separated list of its replacements .
21,014
def transform_member ( self , node , results ) : mod_member = results . get ( "mod_member" ) pref = mod_member . prefix member = results . get ( "member" ) if member : if isinstance ( member , list ) : member = member [ 0 ] new_name = None for change in MAPPING [ mod_member . value ] : if member . value in change [ 1 ]...
Transform for imports of specific module elements . Replaces the module to be imported from with the appropriate new module .
21,015
def transform_dot ( self , node , results ) : module_dot = results . get ( "bare_with_attr" ) member = results . get ( "member" ) new_name = None if isinstance ( member , list ) : member = member [ 0 ] for change in MAPPING [ module_dot . value ] : if member . value in change [ 1 ] : new_name = change [ 0 ] break if ne...
Transform for calls to module members in code .
21,016
def call_only_once ( func ) : def new_func ( * args , ** kwargs ) : if not new_func . _called : new_func . _called = True return func ( * args , ** kwargs ) new_func . _called = False return new_func
To be used as a decorator
21,017
def get_all_fix_names ( fixer_pkg , remove_prefix = True ) : pkg = __import__ ( fixer_pkg , [ ] , [ ] , [ "*" ] ) fixer_dir = os . path . dirname ( pkg . __file__ ) fix_names = [ ] for name in sorted ( os . listdir ( fixer_dir ) ) : if name . startswith ( "fix_" ) and name . endswith ( ".py" ) : if remove_prefix : name...
Return a sorted list of all available fix names in the given package .
21,018
def _get_head_types ( pat ) : if isinstance ( pat , ( pytree . NodePattern , pytree . LeafPattern ) ) : if pat . type is None : raise _EveryNode return set ( [ pat . type ] ) if isinstance ( pat , pytree . NegatedPattern ) : if pat . content : return _get_head_types ( pat . content ) raise _EveryNode if isinstance ( pa...
Accepts a pytree Pattern Node and returns a set of the pattern types which will match first .
21,019
def get_fixers ( self ) : pre_order_fixers = [ ] post_order_fixers = [ ] for fix_mod_path in self . fixers : mod = __import__ ( fix_mod_path , { } , { } , [ "*" ] ) fix_name = fix_mod_path . rsplit ( "." , 1 ) [ - 1 ] if fix_name . startswith ( self . FILE_PREFIX ) : fix_name = fix_name [ len ( self . FILE_PREFIX ) : ]...
Inspects the options to load the requested patterns and handlers .
21,020
def log_message ( self , msg , * args ) : if args : msg = msg % args self . logger . info ( msg )
Hook to log a message .
21,021
def refactor ( self , items , write = False , doctests_only = False ) : for dir_or_file in items : if os . path . isdir ( dir_or_file ) : self . refactor_dir ( dir_or_file , write , doctests_only ) else : self . refactor_file ( dir_or_file , write , doctests_only )
Refactor a list of files and directories .
21,022
def refactor_dir ( self , dir_name , write = False , doctests_only = False ) : py_ext = os . extsep + "py" for dirpath , dirnames , filenames in os . walk ( dir_name ) : self . log_debug ( "Descending into %s" , dirpath ) dirnames . sort ( ) filenames . sort ( ) for name in filenames : if ( not name . startswith ( "." ...
Descends down a directory and refactor every Python file found .
21,023
def _read_python_source ( self , filename ) : try : f = open ( filename , "rb" ) except IOError as err : self . log_error ( "Can't open %s: %s" , filename , err ) return None , None try : encoding = tokenize . detect_encoding ( f . readline ) [ 0 ] finally : f . close ( ) with _open_with_encoding ( filename , "r" , enc...
Do our best to decode a Python source file correctly .
21,024
def refactor_file ( self , filename , write = False , doctests_only = False ) : input , encoding = self . _read_python_source ( filename ) if input is None : return input += u"\n" if doctests_only : self . log_debug ( "Refactoring doctests in %s" , filename ) output = self . refactor_docstring ( input , filename ) if s...
Refactors a file .
21,025
def refactor_string ( self , data , name ) : features = _detect_future_features ( data ) if "print_function" in features : self . driver . grammar = pygram . python_grammar_no_print_statement try : tree = self . driver . parse_string ( data ) except Exception as err : self . log_error ( "Can't parse %s: %s: %s" , name ...
Refactor a given input string .
21,026
def traverse_by ( self , fixers , traversal ) : if not fixers : return for node in traversal : for fixer in fixers [ node . type ] : results = fixer . match ( node ) if results : new = fixer . transform ( node , results ) if new is not None : node . replace ( new ) node = new
Traverse an AST applying a set of fixers to each node .
21,027
def processed_file ( self , new_text , filename , old_text = None , write = False , encoding = None ) : self . files . append ( filename ) if old_text is None : old_text = self . _read_python_source ( filename ) [ 0 ] if old_text is None : return equal = old_text == new_text self . print_output ( old_text , new_text , ...
Called when a file has been refactored and there may be changes .
21,028
def write_file ( self , new_text , filename , old_text , encoding = None ) : try : f = _open_with_encoding ( filename , "w" , encoding = encoding ) except os . error as err : self . log_error ( "Can't create %s: %s" , filename , err ) return try : f . write ( _to_system_newlines ( new_text ) ) except os . error as err ...
Writes a string to a file .
21,029
def refactor_docstring ( self , input , filename ) : result = [ ] block = None block_lineno = None indent = None lineno = 0 for line in input . splitlines ( True ) : lineno += 1 if line . lstrip ( ) . startswith ( self . PS1 ) : if block is not None : result . extend ( self . refactor_doctest ( block , block_lineno , i...
Refactors a docstring looking for doctests .
21,030
def parse_block ( self , block , lineno , indent ) : tree = self . driver . parse_tokens ( self . wrap_toks ( block , lineno , indent ) ) tree . future_features = frozenset ( ) return tree
Parses a block into a tree .
21,031
def gen_lines ( self , block , indent ) : prefix1 = indent + self . PS1 prefix2 = indent + self . PS2 prefix = prefix1 for line in block : if line . startswith ( prefix ) : yield line [ len ( prefix ) : ] elif line == prefix . rstrip ( ) + u"\n" : yield u"\n" else : raise AssertionError ( "line=%r, prefix=%r" % ( line ...
Generates lines as expected by tokenize from a list of lines .
21,032
def var_to_xml ( val , name , trim_if_too_big = True , additional_in_xml = '' , evaluate_full_value = True ) : type_name , type_qualifier , is_exception_on_eval , resolver , value = get_variable_details ( val , evaluate_full_value ) try : name = quote ( name , '/>_= ' ) except : pass xml = '<var name="%s" type="%s" ' %...
single variable or dictionary to xml representation
21,033
def do ( self , arg ) : ".exploitable - Determine the approximate exploitability rating" from winappdbg import Crash event = self . debug . lastEvent crash = Crash ( event ) crash . fetch_extra_data ( event ) status , rule , description = crash . isExploitable ( ) print "-" * 79 print "Exploitability: %s" % status prin...
. exploitable - Determine the approximate exploitability rating
21,034
def _cleanup ( self ) : self . device = None self . doc = None self . parser = None self . resmgr = None self . interpreter = None
Frees lots of non - textual information such as the fonts and images and the objects that were needed to parse the PDF .
21,035
def get_app_name ( app_name ) : type_ = locate ( app_name ) if inspect . isclass ( type_ ) : return type_ . name return app_name
Returns a app name from new app config if is a class or the same app name if is not a class .
21,036
def has_role ( user , roles ) : if user and user . is_superuser : return True if not isinstance ( roles , list ) : roles = [ roles ] normalized_roles = [ ] for role in roles : if not inspect . isclass ( role ) : role = RolesManager . retrieve_role ( role ) normalized_roles . append ( role ) user_roles = get_user_roles ...
Check if a user has any of the given roles .
21,037
def has_permission ( user , permission_name ) : if user and user . is_superuser : return True return permission_name in available_perm_names ( user )
Check if a user has a given permission .
21,038
def has_object_permission ( checker_name , user , obj ) : if user and user . is_superuser : return True checker = PermissionsManager . retrieve_checker ( checker_name ) user_roles = get_user_roles ( user ) if not user_roles : user_roles = [ None ] return any ( [ checker ( user_role , user , obj ) for user_role in user_...
Check if a user has permission to perform an action on an object .
21,039
def get_or_create_permission ( codename , name = camel_or_snake_to_title ) : user_ct = ContentType . objects . get_for_model ( get_user_model ( ) ) return Permission . objects . get_or_create ( content_type = user_ct , codename = codename , defaults = { 'name' : name ( codename ) if callable ( name ) else name } )
Get a Permission object from a permission name .
21,040
def get_user_roles ( user ) : if user : groups = user . groups . all ( ) roles = ( RolesManager . retrieve_role ( group . name ) for group in groups if group . name in RolesManager . get_roles_names ( ) ) return sorted ( roles , key = lambda r : r . get_name ( ) ) else : return [ ]
Get a list of a users s roles .
21,041
def clear_roles ( user ) : roles = get_user_roles ( user ) for role in roles : role . remove_role_from_user ( user ) return roles
Remove all roles from a user .
21,042
def available_perm_status ( user ) : roles = get_user_roles ( user ) permission_hash = { } for role in roles : permission_names = role . permission_names_list ( ) for permission_name in permission_names : permission_hash [ permission_name ] = get_permission ( permission_name ) in user . user_permissions . all ( ) retur...
Get a boolean map of the permissions available to a user based on that user s roles .
21,043
def grant_permission ( user , permission_name ) : roles = get_user_roles ( user ) for role in roles : if permission_name in role . permission_names_list ( ) : permission = get_permission ( permission_name ) user . user_permissions . add ( permission ) return raise RolePermissionScopeException ( "This permission isn't i...
Grant a user a specified permission .
21,044
def revoke_permission ( user , permission_name ) : roles = get_user_roles ( user ) for role in roles : if permission_name in role . permission_names_list ( ) : permission = get_permission ( permission_name ) user . user_permissions . remove ( permission ) return raise RolePermissionScopeException ( "This permission isn...
Revoke a specified permission from a user .
21,045
def model_saved ( sender , instance , created , raw , using , ** kwargs ) : opts = get_opts ( instance ) model = '.' . join ( [ opts . app_label , opts . object_name ] ) action = 'created' if created else 'updated' distill_model_event ( instance , model , action )
Automatically triggers created and updated actions .
21,046
def model_deleted ( sender , instance , using , ** kwargs ) : opts = get_opts ( instance ) model = '.' . join ( [ opts . app_label , opts . object_name ] ) distill_model_event ( instance , model , 'deleted' )
Automatically triggers deleted actions .
21,047
def raw_custom_event ( sender , event_name , payload , user , send_hook_meta = True , instance = None , ** kwargs ) : HookModel = get_hook_model ( ) hooks = HookModel . objects . filter ( user = user , event = event_name ) for hook in hooks : new_payload = payload if send_hook_meta : new_payload = { 'hook' : hook . dic...
Give a full payload
21,048
def clean ( self ) : if self . event not in HOOK_EVENTS . keys ( ) : raise ValidationError ( "Invalid hook event {evt}." . format ( evt = self . event ) )
Validation for events .
21,049
def get_module ( path ) : try : from importlib import import_module except ImportError as e : from django . utils . importlib import import_module try : mod_name , func_name = path . rsplit ( '.' , 1 ) mod = import_module ( mod_name ) except ImportError as e : raise ImportError ( 'Error importing alert function {0}: "{...
A modified duplicate from Django s built in backend retriever .
21,050
def get_hook_model ( ) : from rest_hooks . models import Hook HookModel = Hook if getattr ( settings , 'HOOK_CUSTOM_MODEL' , None ) : HookModel = get_module ( settings . HOOK_CUSTOM_MODEL ) return HookModel
Returns the Custom Hook model if defined in settings otherwise the default Hook model .
21,051
def find_and_fire_hook ( event_name , instance , user_override = None ) : try : from django . contrib . auth import get_user_model User = get_user_model ( ) except ImportError : from django . contrib . auth . models import User from rest_hooks . models import HOOK_EVENTS if not event_name in HOOK_EVENTS . keys ( ) : ra...
Look up Hooks that apply
21,052
def _get_plot_data ( data , ndim = None ) : out = data if isinstance ( data , PHATE ) : out = data . transform ( ) else : try : if isinstance ( data , anndata . AnnData ) : try : out = data . obsm [ 'X_phate' ] except KeyError : raise RuntimeError ( "data.obsm['X_phate'] not found. " "Please run `sc.tl.phate(adata)` be...
Get plot data out of an input object
21,053
def scatter ( x , y , z = None , c = None , cmap = None , s = None , discrete = None , ax = None , legend = None , figsize = None , xticks = False , yticks = False , zticks = False , xticklabels = True , yticklabels = True , zticklabels = True , label_prefix = "PHATE" , xlabel = None , ylabel = None , zlabel = None , t...
Create a scatter plot
21,054
def scatter2d ( data , ** kwargs ) : warnings . warn ( "`phate.plot.scatter2d` is deprecated. " "Use `scprep.plot.scatter2d` instead." , FutureWarning ) data = _get_plot_data ( data , ndim = 2 ) return scprep . plot . scatter2d ( data , ** kwargs )
Create a 2D scatter plot
21,055
def rotate_scatter3d ( data , filename = None , elev = 30 , rotation_speed = 30 , fps = 10 , ax = None , figsize = None , dpi = None , ipython_html = "jshtml" , ** kwargs ) : warnings . warn ( "`phate.plot.rotate_scatter3d` is deprecated. " "Use `scprep.plot.rotate_scatter3d` instead." , FutureWarning ) return scprep ....
Create a rotating 3D scatter plot
21,056
def kmeans ( phate_op , k = 8 , random_state = None ) : if phate_op . graph is not None : diff_potential = phate_op . calculate_potential ( ) if isinstance ( phate_op . graph , graphtools . graphs . LandmarkGraph ) : diff_potential = phate_op . graph . interpolate ( diff_potential ) return cluster . KMeans ( k , random...
KMeans on the PHATE potential
21,057
def cmdscale_fast ( D , ndim ) : tasklogger . log_debug ( "Performing classic MDS on {} of shape {}..." . format ( type ( D ) . __name__ , D . shape ) ) D = D ** 2 D = D - D . mean ( axis = 0 ) [ None , : ] D = D - D . mean ( axis = 1 ) [ : , None ] pca = PCA ( n_components = ndim , svd_solver = 'randomized' ) Y = pca ...
Fast CMDS using random SVD
21,058
def embed_MDS ( X , ndim = 2 , how = 'metric' , distance_metric = 'euclidean' , n_jobs = 1 , seed = None , verbose = 0 ) : if how not in [ 'classic' , 'metric' , 'nonmetric' ] : raise ValueError ( "Allowable 'how' values for MDS: 'classic', " "'metric', or 'nonmetric'. " "'{}' was passed." . format ( how ) ) X_dist = s...
Performs classic metric and non - metric MDS
21,059
def compute_von_neumann_entropy ( data , t_max = 100 ) : _ , eigenvalues , _ = svd ( data ) entropy = [ ] eigenvalues_t = np . copy ( eigenvalues ) for _ in range ( t_max ) : prob = eigenvalues_t / np . sum ( eigenvalues_t ) prob = prob + np . finfo ( float ) . eps entropy . append ( - np . sum ( prob * np . log ( prob...
Determines the Von Neumann entropy of data at varying matrix powers . The user should select a value of t around the knee of the entropy curve .
21,060
def check_positive ( ** params ) : for p in params : if not isinstance ( params [ p ] , numbers . Number ) or params [ p ] <= 0 : raise ValueError ( "Expected {} > 0, got {}" . format ( p , params [ p ] ) )
Check that parameters are positive as expected
21,061
def check_int ( ** params ) : for p in params : if not isinstance ( params [ p ] , numbers . Integral ) : raise ValueError ( "Expected {} integer, got {}" . format ( p , params [ p ] ) )
Check that parameters are integers as expected
21,062
def check_if_not ( x , * checks , ** params ) : for p in params : if params [ p ] is not x and params [ p ] != x : [ check ( ** { p : params [ p ] } ) for check in checks ]
Run checks only if parameters are not equal to a specified value
21,063
def check_in ( choices , ** params ) : for p in params : if params [ p ] not in choices : raise ValueError ( "{} value {} not recognized. Choose from {}" . format ( p , params [ p ] , choices ) )
Checks parameters are in a list of allowed parameters
21,064
def check_between ( v_min , v_max , ** params ) : for p in params : if params [ p ] < v_min or params [ p ] > v_max : raise ValueError ( "Expected {} between {} and {}, " "got {}" . format ( p , v_min , v_max , params [ p ] ) )
Checks parameters are in a specified range
21,065
def matrix_is_equivalent ( X , Y ) : return X is Y or ( isinstance ( X , Y . __class__ ) and X . shape == Y . shape and np . sum ( ( X != Y ) . sum ( ) ) == 0 )
Checks matrix equivalence with numpy scipy and pandas
21,066
def diff_op ( self ) : if self . graph is not None : if isinstance ( self . graph , graphtools . graphs . LandmarkGraph ) : diff_op = self . graph . landmark_op else : diff_op = self . graph . diff_op if sparse . issparse ( diff_op ) : diff_op = diff_op . toarray ( ) return diff_op else : raise NotFittedError ( "This P...
The diffusion operator calculated from the data
21,067
def _check_params ( self ) : utils . check_positive ( n_components = self . n_components , k = self . knn ) utils . check_int ( n_components = self . n_components , k = self . knn , n_jobs = self . n_jobs ) utils . check_between ( 0 , 1 , gamma = self . gamma ) utils . check_if_not ( None , utils . check_positive , a =...
Check PHATE parameters
21,068
def fit ( self , X ) : X , n_pca , precomputed , update_graph = self . _parse_input ( X ) if precomputed is None : tasklogger . log_info ( "Running PHATE on {} cells and {} genes." . format ( X . shape [ 0 ] , X . shape [ 1 ] ) ) else : tasklogger . log_info ( "Running PHATE on precomputed {} matrix with {} cells." . f...
Computes the diffusion operator
21,069
def transform ( self , X = None , t_max = 100 , plot_optimal_t = False , ax = None ) : if self . graph is None : raise NotFittedError ( "This PHATE instance is not fitted yet. Call " "'fit' with appropriate arguments before " "using this method." ) elif X is not None and not utils . matrix_is_equivalent ( X , self . X ...
Computes the position of the cells in the embedding space
21,070
def fit_transform ( self , X , ** kwargs ) : tasklogger . log_start ( 'PHATE' ) self . fit ( X ) embedding = self . transform ( ** kwargs ) tasklogger . log_complete ( 'PHATE' ) return embedding
Computes the diffusion operator and the position of the cells in the embedding space
21,071
def calculate_potential ( self , t = None , t_max = 100 , plot_optimal_t = False , ax = None ) : if t is None : t = self . t if self . diff_potential is None : if t == 'auto' : t = self . optimal_t ( t_max = t_max , plot = plot_optimal_t , ax = ax ) else : t = self . t tasklogger . log_start ( "diffusion potential" ) d...
Calculates the diffusion potential
21,072
def von_neumann_entropy ( self , t_max = 100 ) : t = np . arange ( t_max ) return t , vne . compute_von_neumann_entropy ( self . diff_op , t_max = t_max )
Calculate Von Neumann Entropy
21,073
def optimal_t ( self , t_max = 100 , plot = False , ax = None ) : tasklogger . log_start ( "optimal t" ) t , h = self . von_neumann_entropy ( t_max = t_max ) t_opt = vne . find_knee_point ( y = h , x = t ) tasklogger . log_info ( "Automatically selected t = {}" . format ( t_opt ) ) tasklogger . log_complete ( "optimal ...
Find the optimal value of t
21,074
def a2bits ( chars : str ) -> str : return bin ( reduce ( lambda x , y : ( x << 8 ) + y , ( ord ( c ) for c in chars ) , 1 ) ) [ 3 : ]
Converts a string to its bits representation as a string of 0 s and 1 s .
21,075
def a2bits_list ( chars : str , encoding : str = "UTF-8" ) -> List [ str ] : return [ bin ( ord ( x ) ) [ 2 : ] . rjust ( ENCODINGS [ encoding ] , "0" ) for x in chars ]
Convert a string to its bits representation as a list of 0 s and 1 s .
21,076
def bs ( s : int ) -> str : return str ( s ) if s <= 1 else bs ( s >> 1 ) + str ( s & 1 )
Converts an int to its bits representation as a string of 0 s and 1 s .
21,077
def n_at_a_time ( items : List [ int ] , n : int , fillvalue : str ) -> Iterator [ Tuple [ Union [ int , str ] ] ] : it = iter ( items ) return itertools . zip_longest ( * [ it ] * n , fillvalue = fillvalue )
Returns an iterator which groups n items at a time . Any final partial tuple will be padded with the fillvalue
21,078
def open_image ( fname_or_instance : Union [ str , IO [ bytes ] ] ) : if isinstance ( fname_or_instance , Image . Image ) : return fname_or_instance return Image . open ( fname_or_instance )
Opens a Image and returns it .
21,079
def log_gen ( ) -> Iterator [ int ] : y = 1 while True : adder = max ( 1 , math . pow ( 10 , int ( math . log10 ( y ) ) ) ) yield int ( y ) y = y + int ( adder )
Logarithmic generator .
21,080
def deprecated ( replacement = None , version = None ) : def outer ( oldfun ) : def inner ( * args , ** kwargs ) : msg = "%s is deprecated" % oldfun . __name__ if version is not None : msg += "will be removed in version %s;" % version if replacement is not None : msg += "; use %s instead" % ( replacement ) warnings . w...
A decorator which can be used to mark functions as deprecated . replacement is a callable that will be called with the same args as the decorated function . >>> import pytest >>>
21,081
def get_classname ( o ) : if inspect . isclass ( o ) : target = o elif callable ( o ) : target = o else : target = o . __class__ try : return target . __qualname__ except AttributeError : return target . __name__
Returns the classname of an object r a class
21,082
def _handler ( self , sender , setting , value , ** kwargs ) : if setting . startswith ( self . prefix ) : self . _set_attr ( setting , value )
handler for setting_changed signal .
21,083
def get_version ( model_instance , version ) : version_field = get_version_fieldname ( model_instance ) kwargs = { 'pk' : model_instance . pk , version_field : version } return model_instance . __class__ . objects . get ( ** kwargs )
try go load from the database one object with specific version
21,084
def conflict ( request , target = None , template_name = '409.html' ) : try : template = loader . get_template ( template_name ) except TemplateDoesNotExist : template = Template ( '<h1>Conflict</h1>' '<p>The request was unsuccessful due to a conflict. ' 'The object changed during the transaction.</p>' ) try : saved = ...
409 error handler .
21,085
def add_arguments ( self , parser ) : subparsers = parser . add_subparsers ( help = 'sub-command help' , dest = 'command' ) add_parser = partial ( _add_subparser , subparsers , parser ) add_parser ( 'list' , help = "list concurrency triggers" ) add_parser ( 'drop' , help = "drop concurrency triggers" ) add_parser ( 'c...
Entry point for subclassed commands to add custom arguments .
21,086
def action_checkbox ( self , obj ) : if self . check_concurrent_action : return helpers . checkbox . render ( helpers . ACTION_CHECKBOX_NAME , force_text ( "%s,%s" % ( obj . pk , get_revision_of_object ( obj ) ) ) ) else : return super ( ConcurrencyActionMixin , self ) . action_checkbox ( obj )
A list_display column containing a checkbox widget .
21,087
def _management_form ( self ) : if self . is_bound : form = ConcurrentManagementForm ( self . data , auto_id = self . auto_id , prefix = self . prefix ) if not form . is_valid ( ) : raise ValidationError ( 'ManagementForm data is missing or has been tampered with' ) else : form = ConcurrentManagementForm ( auto_id = se...
Returns the ManagementForm instance for this FormSet .
21,088
def cipherprefs ( self ) : if 'PreferredSymmetricAlgorithms' in self . _signature . subpackets : return next ( iter ( self . _signature . subpackets [ 'h_PreferredSymmetricAlgorithms' ] ) ) . flags return [ ]
A list of preferred symmetric algorithms specified in this signature if any . Otherwise an empty list .
21,089
def compprefs ( self ) : if 'PreferredCompressionAlgorithms' in self . _signature . subpackets : return next ( iter ( self . _signature . subpackets [ 'h_PreferredCompressionAlgorithms' ] ) ) . flags return [ ]
A list of preferred compression algorithms specified in this signature if any . Otherwise an empty list .
21,090
def exportable ( self ) : if 'ExportableCertification' in self . _signature . subpackets : return bool ( next ( iter ( self . _signature . subpackets [ 'ExportableCertification' ] ) ) ) return True
False if this signature is marked as being not exportable . Otherwise True .
21,091
def features ( self ) : if 'Features' in self . _signature . subpackets : return next ( iter ( self . _signature . subpackets [ 'Features' ] ) ) . flags return set ( )
A set of implementation features specified in this signature if any . Otherwise an empty set .
21,092
def hashprefs ( self ) : if 'PreferredHashAlgorithms' in self . _signature . subpackets : return next ( iter ( self . _signature . subpackets [ 'h_PreferredHashAlgorithms' ] ) ) . flags return [ ]
A list of preferred hash algorithms specified in this signature if any . Otherwise an empty list .
21,093
def is_expired ( self ) : expires_at = self . expires_at if expires_at is not None and expires_at != self . created : return expires_at < datetime . utcnow ( ) return False
True if the signature has an expiration date and is expired . Otherwise False
21,094
def keyserver ( self ) : if 'PreferredKeyServer' in self . _signature . subpackets : return next ( iter ( self . _signature . subpackets [ 'h_PreferredKeyServer' ] ) ) . uri return ''
The preferred key server specified in this signature if any . Otherwise an empty str .
21,095
def notation ( self ) : return dict ( ( nd . name , nd . value ) for nd in self . _signature . subpackets [ 'NotationData' ] )
A dict of notation data in this signature if any . Otherwise an empty dict .
21,096
def policy_uri ( self ) : if 'Policy' in self . _signature . subpackets : return next ( iter ( self . _signature . subpackets [ 'Policy' ] ) ) . uri return ''
The policy URI specified in this signature if any . Otherwise an empty str .
21,097
def revocable ( self ) : if 'Revocable' in self . _signature . subpackets : return bool ( next ( iter ( self . _signature . subpackets [ 'Revocable' ] ) ) ) return True
False if this signature is marked as being not revocable . Otherwise True .
21,098
def hashdata ( self , subject ) : _data = bytearray ( ) if isinstance ( subject , six . string_types ) : subject = subject . encode ( 'charmap' ) if self . type == SignatureType . BinaryDocument : if isinstance ( subject , ( SKEData , IntegrityProtectedSKEData ) ) : _data += subject . __bytearray__ ( ) else : _data += ...
All signatures are formed by producing a hash over the signature data and then using the resulting hash in the signature algorithm .
21,099
def image ( self ) : return self . _uid . image . image if isinstance ( self . _uid , UserAttribute ) else None
If this is a User Attribute this will be the stored image . If this is not a User Attribute this will be None .