signature
stringlengths
29
44.1k
implementation
stringlengths
0
85.2k
def add_file_argument ( self , * args , ** kwargs ) : """Add an argument that represents the location of a file : param args : : param kwargs : : return :"""
rval = self . add_argument ( * args , ** kwargs ) self . file_args . append ( rval ) return rval
def grad_local_log_likelihood ( self , x ) : """d / dx y ^ T Cx + y ^ T d - exp ( Cx + d ) = y ^ T C - exp ( Cx + d ) ^ T C = ( y - lmbda ) ^ T C"""
# Observation likelihoods lmbda = np . exp ( np . dot ( x , self . C . T ) + np . dot ( self . inputs , self . D . T ) ) return ( self . data - lmbda ) . dot ( self . C )
def parse_reqtype ( self ) : """Return the authentication body ."""
if self . job_args [ 'os_auth_version' ] == 'v1.0' : return dict ( ) else : setup = { 'username' : self . job_args . get ( 'os_user' ) } # Check if any prefix items are set . A prefix should be a # dictionary with keys matching the os _ * credential type . prefixes = self . job_args . get ( 'os_pref...
def send ( self , to , subject , body , reply_to = None , ** kwargs ) : """Send simple message"""
if self . provider == "SES" : self . mail . send ( to = to , subject = subject , body = body , reply_to = reply_to , ** kwargs ) elif self . provider == "FLASK-MAIL" : msg = flask_mail . Message ( recipients = to , subject = subject , body = body , reply_to = reply_to , sender = self . app . config . get ( "MAI...
def get_file_details ( filepath , hash_algorithms = [ 'sha256' ] ) : """< Purpose > To get file ' s length and hash information . The hash is computed using the sha256 algorithm . This function is used in the signerlib . py and updater . py modules . < Arguments > filepath : Absolute file path of a file...
# Making sure that the format of ' filepath ' is a path string . # ' securesystemslib . exceptions . FormatError ' is raised on incorrect format . securesystemslib . formats . PATH_SCHEMA . check_match ( filepath ) securesystemslib . formats . HASHALGORITHMS_SCHEMA . check_match ( hash_algorithms ) # The returned file ...
def dump ( self , validate = True ) : """Create bencoded : attr : ` metainfo ` ( i . e . the content of a torrent file ) : param bool validate : Whether to run : meth : ` validate ` first : return : : attr : ` metainfo ` as bencoded : class : ` bytes `"""
if validate : self . validate ( ) return bencode ( self . convert ( ) )
def split ( self , amt ) : """return 2 trades , 1 with specific amt and the other with self . quantity - amt"""
ratio = abs ( amt / self . qty ) t1 = Trade ( self . tid , self . ts , amt , self . px , fees = ratio * self . fees , ** self . kwargs ) t2 = Trade ( self . tid , self . ts , self . qty - amt , self . px , fees = ( 1. - ratio ) * self . fees , ** self . kwargs ) return [ t1 , t2 ]
def plot_elbo ( self , figsize = ( 15 , 7 ) ) : """Plots the ELBO progress ( if present )"""
import matplotlib . pyplot as plt plt . figure ( figsize = figsize ) plt . plot ( self . elbo_records ) plt . xlabel ( "Iterations" ) plt . ylabel ( "ELBO" ) plt . show ( )
def add ( self , path ) : """Adds a new resource with the given path to the resource set . Parameters : * * * path ( str , unicode ) : * * path of the resource to be protected Raises : TypeError when the path is not a string or a unicode string"""
if not isinstance ( path , str ) and not isinstance ( path , unicode ) : raise TypeError ( 'The value passed for parameter path is not a str' ' or unicode' ) resource = Resource ( path ) self . resources [ path ] = resource return resource
def get_view_root ( view_name : str ) -> XmlNode : '''Parses xml file and return root XmlNode'''
try : path = join ( deps . views_folder , '{0}.{1}' . format ( view_name , deps . view_ext ) ) parser = Parser ( ) if path not in _XML_CACHE : with open ( path , 'rb' ) as xml_file : _XML_CACHE [ path ] = parser . parse ( xml_file , view_name ) return _XML_CACHE [ path ] except FileN...
def standard_lstm_lm_200 ( dataset_name = None , vocab = None , pretrained = False , ctx = cpu ( ) , root = os . path . join ( get_home_dir ( ) , 'models' ) , ** kwargs ) : r"""Standard 2 - layer LSTM language model with tied embedding and output weights . Both embedding and hidden dimensions are 200. Parameter...
predefined_args = { 'embed_size' : 200 , 'hidden_size' : 200 , 'mode' : 'lstm' , 'num_layers' : 2 , 'tie_weights' : True , 'dropout' : 0.2 } mutable_args = [ 'dropout' ] assert all ( ( k not in kwargs or k in mutable_args ) for k in predefined_args ) , 'Cannot override predefined model settings.' predefined_args . upda...
def getuserurl ( iduser , * args , ** kwargs ) : """Request Users URL . If iduser is set , you ' ll get a response adequate for a MambuUser object . If not set , you ' ll get a response adequate for a MambuUsers object . See mambuuser module and pydoc for further information . Currently implemented filter p...
getparams = [ ] if kwargs : try : if kwargs [ "fullDetails" ] == True : getparams . append ( "fullDetails=true" ) else : getparams . append ( "fullDetails=false" ) except Exception as ex : pass try : getparams . append ( "branchId=%s" % kwargs [ "branc...
def _read_frame ( self ) : """Read a single frame from the trajectory"""
self . _secfile . get_next ( "Frame Number" ) frame = ATRJFrame ( ) # Read the time and energy energy_lines = self . _secfile . get_next ( "Time/Energy" ) energy_words = energy_lines [ 0 ] . split ( ) frame . time = float ( energy_words [ 0 ] ) * picosecond frame . step = int ( energy_words [ 1 ] ) frame . total_energy...
def find_cubes ( numbers ) : """This function computes the cube of each number in the input list using a lambda function . Examples : > > > find _ cubes ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ) [1 , 8 , 27 , 64 , 125 , 216 , 343 , 512 , 729 , 1000] > > > find _ cubes ( [ 10 , 20 , 30 ] ) [1000 , 800...
cube_values = list ( map ( lambda n : n ** 3 , numbers ) ) return cube_values
def console_load_xp ( con : tcod . console . Console , filename : str ) -> bool : """Update a console from a REXPaint ` . xp ` file ."""
return bool ( lib . TCOD_console_load_xp ( _console ( con ) , filename . encode ( "utf-8" ) ) )
def get_dataset ( self , dataset_id , ds_info , out = None ) : """Load a dataset ."""
var_path = ds_info . get ( 'file_key' , '{}' . format ( dataset_id . name ) ) dtype = ds_info . get ( 'dtype' , np . float32 ) if var_path + '/shape' not in self : # loading a scalar value shape = 1 else : shape = self [ var_path + '/shape' ] if shape [ 0 ] == 1 : # Remove the time dimenstion from dataset ...
def stationary_distribution ( P , C = None , mincount_connectivity = 0 ) : """Simple estimator for stationary distribution for multiple strongly connected sets"""
# can be replaced by msmtools . analysis . stationary _ distribution in next msmtools release from msmtools . analysis . dense . stationary_vector import stationary_distribution as msmstatdist if C is None : if is_connected ( P , strong = True ) : return msmstatdist ( P ) else : raise ValueError...
def get_similar ( self , limit = None ) : """Returns similar tracks for this track on the network , based on listening data ."""
params = self . _get_params ( ) if limit : params [ "limit" ] = limit doc = self . _request ( self . ws_prefix + ".getSimilar" , True , params ) seq = [ ] for node in doc . getElementsByTagName ( self . ws_prefix ) : title = _extract ( node , "name" ) artist = _extract ( node , "name" , 1 ) match = _num...
def set_xlsx_colwidths ( worksheet , fld2col_widths , fldnames ) : """Set xlsx column widths using fld2col _ widths ."""
for col_idx , fld in enumerate ( fldnames ) : col_width = fld2col_widths . get ( fld , None ) if col_width is not None : worksheet . set_column ( col_idx , col_idx , col_width )
def lpc ( blk , order = None ) : """Find the Linear Predictive Coding ( LPC ) coefficients as a ZFilter object , the analysis whitening filter . This implementation uses the autocorrelation method , using the Levinson - Durbin algorithm or Numpy pseudo - inverse for linear system solving , when needed . Par...
if order < 100 : return lpc . nautocor ( blk , order ) try : return lpc . kautocor ( blk , order ) except ParCorError : return lpc . nautocor ( blk , order )
def pack_balance_proof_update ( nonce : Nonce , balance_hash : BalanceHash , additional_hash : AdditionalHash , canonical_identifier : CanonicalIdentifier , partner_signature : Signature , ) -> bytes : """Packs balance proof data to be signed for updateNonClosingBalanceProof Packs the given arguments in a byte ar...
return pack_balance_proof ( nonce = nonce , balance_hash = balance_hash , additional_hash = additional_hash , canonical_identifier = canonical_identifier , msg_type = MessageTypeId . BALANCE_PROOF_UPDATE , ) + partner_signature
def flush ( self ) : """Flushes the current queue by by calling : func : ` sender ` ' s : func : ` send ` method ."""
local_sender = self . sender if not local_sender : return while True : # get at most send _ buffer _ size items and send them data = [ ] while len ( data ) < local_sender . send_buffer_size : item = self . get ( ) if not item : break data . append ( item ) if len ( da...
def generate_supremacy_circuit_google_v2_grid ( n_rows : int , n_cols : int , cz_depth : int , seed : int ) -> circuits . Circuit : """Generates Google Random Circuits v2 as in github . com / sboixo / GRCS cz _ v2. See also https : / / arxiv . org / abs / 1807.10749 Args : n _ rows : number of rows of a 2D la...
qubits = [ devices . GridQubit ( i , j ) for i in range ( n_rows ) for j in range ( n_cols ) ] return generate_supremacy_circuit_google_v2 ( qubits , cz_depth , seed )
def get_assessment_parts_by_search ( self , assessment_part_query , assessment_part_search ) : """Pass through to provider AssessmentPartSearchSession . get _ assessment _ parts _ by _ search"""
# Implemented from azosid template for - # osid . resource . ResourceSearchSession . get _ resources _ by _ search _ template if not self . _can ( 'search' ) : raise PermissionDenied ( ) return self . _provider_session . get_assessment_parts_by_search ( assessment_part_query , assessment_part_search )
def take_complement ( list_ , index_list ) : """Returns items in ` ` list _ ` ` not indexed by index _ list"""
mask = not_list ( index_to_boolmask ( index_list , len ( list_ ) ) ) return compress ( list_ , mask )
def d2ASbr_dV2 ( dSbr_dVa , dSbr_dVm , Sbr , Cbr , Ybr , V , lam ) : """Computes 2nd derivatives of | complex power flow | * * 2 w . r . t . V ."""
diaglam = spdiag ( lam ) diagSbr_conj = spdiag ( conj ( Sbr ) ) Saa , Sav , Sva , Svv = d2Sbr_dV2 ( Cbr , Ybr , V , diagSbr_conj * lam ) Haa = 2 * ( Saa + dSbr_dVa . T * diaglam * conj ( dSbr_dVa ) ) . real ( ) Hva = 2 * ( Sva + dSbr_dVm . T * diaglam * conj ( dSbr_dVa ) ) . real ( ) Hav = 2 * ( Sav + dSbr_dVa . T * di...
def autodetect_format ( content ) : """Return format identifier for given fragment or raise FormatAutodetectionError ."""
formats = set ( ) for impl in FORMAT_IDENTIFIER_TO_FORMAT_CLASS . values ( ) : guess = impl . guess_format ( content ) if guess is not None : formats . add ( guess ) if len ( formats ) == 1 : return formats . pop ( ) elif not formats : raise FormatAutodetectionError ( "No suitable formats" ) els...
def diff ( self ) : """The Difference between a PDA and a DFA"""
self . mmb . complement ( self . alphabet ) self . mmb . minimize ( ) print 'start intersection' self . mmc = self . _intesect ( ) print 'end intersection' return self . mmc
def _module_info_from_proto ( module_info_def , import_scope = None ) : """Deserializes ` module _ info _ def ` proto . Args : module _ info _ def : An instance of ` module _ pb2 . SonnetModule ` . import _ scope : Optional ` string ` . Name scope to use . Returns : An instance of ` ModuleInfo ` . Raise...
graph = tf . get_default_graph ( ) def prepend_name_scope ( name_scope ) : return ops . prepend_name_scope ( name_scope , import_scope ) def process_leafs ( name ) : return _path_to_graph_element ( prepend_name_scope ( name ) , graph ) connected_subgraphs = [ ] module_info = ModuleInfo ( module_name = module_in...
def msg ( self , target , msg ) : """Sends a message to an user or channel . : param target : user or channel to send to . : type target : str : param msg : message to send . : type msg : str"""
self . cmd ( u'PRIVMSG' , u'{0} :{1}' . format ( target , msg ) )
def spin_gen_op ( oper , gauge ) : """Generates the generic spin matrices for the system"""
slaves = len ( gauge ) oper [ 'O' ] = np . array ( [ spin_gen ( slaves , i , c ) for i , c in enumerate ( gauge ) ] ) oper [ 'O_d' ] = np . transpose ( oper [ 'O' ] , ( 0 , 2 , 1 ) ) oper [ 'O_dO' ] = np . einsum ( '...ij,...jk->...ik' , oper [ 'O_d' ] , oper [ 'O' ] ) oper [ 'Sfliphop' ] = spinflipandhop ( slaves )
def request ( self , method , uri , params = None , data = None , headers = None , auth = None , timeout = None , allow_redirects = False ) : """Makes an HTTP request to this domain . : param string method : The HTTP method . : param string uri : The HTTP uri . : param dict params : Query parameters . : par...
url = self . absolute_url ( uri ) return self . twilio . request ( method , url , params = params , data = data , headers = headers , auth = auth , timeout = timeout , allow_redirects = allow_redirects )
def generate ( data , algorithms = ( DEFAULT_ALOGRITHM , ) ) : """Yields subresource integrity Hash objects for the given data & algorithms > > > for ihash in generate ( b " alert ( ' Hello , world . ' ) ; " ) : . . . print ( ' % s % s ' % ( ihash . algorithm , ihash . b58digest ) ) sha384 H8BRh8j48O9oYatfu...
return ( Hash . fromresource ( data , algorithm ) for algorithm in algorithms )
def get_models ( self , model , page = None ) : """Get all the models from the server . Args : model ( string ) : The class as a string . page ( string , optional ) : The page number as a string Returns : list : A list of instances of the requested model ."""
if page is not None : return self . _store . find_all ( self . _get_model_class ( model ) , params = { 'page' : int ( page ) } ) else : return self . _store . find_all ( self . _get_model_class ( model ) )
def rect ( self , x : int , y : int , width : int , height : int , clear : bool , bg_blend : int = tcod . constants . BKGND_DEFAULT , ) -> None : """Draw a the background color on a rect optionally clearing the text . If ` clear ` is True the affected tiles are changed to space character . Args : x ( int ) : ...
self . __deprecate_defaults ( "draw_rect" , bg_blend , clear = bool ( clear ) ) lib . TCOD_console_rect ( self . console_c , x , y , width , height , clear , bg_blend )
def _set_bridge_domain_type ( self , v , load = False ) : """Setter method for bridge _ domain _ type , mapped from YANG variable / bridge _ domain / bridge _ domain _ type ( enumeration ) If this variable is read - only ( config : false ) in the source YANG file , then _ set _ bridge _ domain _ type is conside...
parent = getattr ( self , "_parent" , None ) if parent is not None and load is False : raise AttributeError ( "Cannot set keys directly when" + " within an instantiated list" ) if hasattr ( v , "_utype" ) : v = v . _utype ( v ) try : t = YANGDynClass ( v , base = RestrictedClassType ( base_type = unicode , ...
def get_example ( cls ) -> dict : """Returns an example value for the Dict type . If an example isn ' t a defined attribute on the class we return a dict of example values based on each property ' s annotation ."""
if cls . example is not None : return cls . example return { k : v . get_example ( ) for k , v in cls . properties . items ( ) }
def end_date ( self ) -> Optional [ datetime . date ] : """Returns the end date of the set of intervals , or ` ` None ` ` if empty ."""
if not self . intervals : return None return self . end_datetime ( ) . date ( )
def first ( self ) : """Return a ( value , source ) pair for the first object found for this view . This amounts to the first element returned by ` resolve ` . If no values are available , a NotFoundError is raised ."""
pairs = self . resolve ( ) try : return iter_first ( pairs ) except ValueError : raise NotFoundError ( u"{0} not found" . format ( self . name ) )
def rotoreflection ( axis , angle , origin = ( 0 , 0 , 0 ) ) : """Returns a roto - reflection symmetry operation Args : axis ( 3x1 array ) : Axis of rotation / mirror normal angle ( float ) : Angle in degrees origin ( 3x1 array ) : Point left invariant by roto - reflection . Defaults to ( 0 , 0 , 0 ) . ...
rot = SymmOp . from_origin_axis_angle ( origin , axis , angle ) refl = SymmOp . reflection ( axis , origin ) m = np . dot ( rot . affine_matrix , refl . affine_matrix ) return SymmOp ( m )
def read_string ( self , lpBaseAddress , nChars , fUnicode = False ) : """Reads an ASCII or Unicode string from the address space of the process . @ see : L { peek _ string } @ type lpBaseAddress : int @ param lpBaseAddress : Memory address to begin reading . @ type nChars : int @ param nChars : String ...
if fUnicode : nChars = nChars * 2 szString = self . read ( lpBaseAddress , nChars ) if fUnicode : szString = compat . unicode ( szString , 'U16' , 'ignore' ) return szString
def search_users ( self , query , n = 20 , maxUsers = 60 ) : """Method to perform the searchfy searches . : param query : Query to be performed . : param n : Number of results per query . : param maxUsers : Max . number of users to be recovered . : return : List of users"""
# Connecting to the API api = self . _connectToAPI ( ) # Verifying the limits of the API self . _rate_limit_status ( api = api , mode = "search_users" ) aux = [ ] page = 0 # print " Getting page % s of new users . . . " % page + 1 # Making the call to the API try : newUsers = api . search_users ( query , n , page )...
def _annotate_somatic ( data , retriever = None ) : """Annotate somatic calls if we have cosmic data installed ."""
if is_human ( data ) : paired = vcfutils . get_paired ( [ data ] ) if paired : r = dd . get_variation_resources ( data ) if r . get ( "cosmic" ) and objectstore . file_exists_or_remote ( r [ "cosmic" ] ) : return True return False
def get_braintree_gateway_by_id ( cls , braintree_gateway_id , ** kwargs ) : """Find BraintreeGateway Return single instance of BraintreeGateway by its ID . This method makes a synchronous HTTP request by default . To make an asynchronous HTTP request , please pass async = True > > > thread = api . get _ br...
kwargs [ '_return_http_data_only' ] = True if kwargs . get ( 'async' ) : return cls . _get_braintree_gateway_by_id_with_http_info ( braintree_gateway_id , ** kwargs ) else : ( data ) = cls . _get_braintree_gateway_by_id_with_http_info ( braintree_gateway_id , ** kwargs ) return data
def _create_slack_with_env_var ( env_var : EnvVar ) -> SlackClient : """Create a : obj : ` SlackClient ` with a token from an env var ."""
token = os . getenv ( env_var ) if token : return SlackClient ( token = token ) raise MissingToken ( f"Could not acquire token from {env_var}" )
def iterate ( self , scopes ) : """Yields ScopeInfo instances for the specified scopes , plus relevant related scopes . Relevant scopes are : - All tasks in a requested goal . - All subsystems tied to a request scope . Yields in a sensible order : Sorted by scope , but with subsystems tied to a request scop...
scope_infos = [ self . _scope_to_info [ s ] for s in self . _expand_tasks ( scopes ) ] if scope_infos : for scope_info in self . _expand_subsystems ( scope_infos ) : yield scope_info
def domain ( self , expparams ) : """Returns a list of ` ` Domain ` ` s , one for each input expparam . : param numpy . ndarray expparams : Array of experimental parameters . This array must be of dtype agreeing with the ` ` expparams _ dtype ` ` property , or , in the case where ` ` n _ outcomes _ constant `...
return [ IntegerDomain ( min = 0 , max = n_o - 1 ) for n_o in self . n_outcomes ( expparams ) ]
def get_config ( self , key , default = UndefinedKey ) : """Return tree config representation of value found at key : param key : key to use ( dot separated ) . E . g . , a . b . c : type key : basestring : param default : default value if key not found : type default : config : return : config value : ...
value = self . get ( key , default ) if isinstance ( value , dict ) : return value elif value is None : return None else : raise ConfigException ( u"{key} has type '{type}' rather than 'config'" . format ( key = key , type = type ( value ) . __name__ ) )
def http_method ( self , data ) : """The HTTP method for this request ."""
data = data . upper ( ) if data in [ 'DELETE' , 'GET' , 'POST' , 'PUT' ] : self . _http_method = data # set content type for commit methods ( best guess ) if self . _headers . get ( 'Content-Type' ) is None and data in [ 'POST' , 'PUT' ] : self . add_header ( 'Content-Type' , 'application/json' ) el...
def long_press ( self , on_element ) : """Long press on an element . : Args : - on _ element : The element to long press ."""
self . _actions . append ( lambda : self . _driver . execute ( Command . LONG_PRESS , { 'element' : on_element . id } ) ) return self
def imresize ( img , size , interpolate = "bilinear" , channel_first = False ) : """Resize image by pil module . Args : img ( numpy . ndarray ) : Image array to save . Image shape is considered as ( height , width , channel ) for RGB or ( height , width ) for gray - scale by default . size ( tupple of int )...
img = _imresize_before ( img , size , channel_first , interpolate , list ( interpolations_map . keys ( ) ) ) expand_flag = False if len ( img . shape ) == 3 and img . shape [ - 1 ] == 1 : # ( h , w , 1 ) can not be handled by pil . Image , temporally reshape to ( h , w ) img = img . reshape ( img . shape [ 0 ] , im...
def visitStart ( self , ctx : ShExDocParser . StartContext ) : """start : KW _ START ' = ' shapeExpression"""
shexpr = ShexShapeExpressionParser ( self . context , None ) shexpr . visit ( ctx . shapeExpression ( ) ) self . context . schema . start = shexpr . expr
def validate_unique ( self , * args , ** kwargs ) : """Checked whether more than one EighthSignup exists for a User on a given EighthBlock ."""
super ( EighthSignup , self ) . validate_unique ( * args , ** kwargs ) if self . has_conflict ( ) : raise ValidationError ( { NON_FIELD_ERRORS : ( "EighthSignup already exists for the User and the EighthScheduledActivity's block" , ) } )
def track ( * args ) : """Track additional files . It is often useful to use glob . glob here . For instance : track ( ' config . ini ' , glob . glob ( ' templates / * . pt ' ) , glob . glob ( ' db / * . db ' ) ) @ param args : A list where each element is either a filename or an iterable of filenames"""
for arg in args : if isinstance ( arg , str ) : arg = [ arg ] for filename in arg : realname , modified_time = _get_filename_and_modified ( filename ) if realname and realname not in _process_files : _process_files [ realname ] = modified_time
def build_schema_mof ( self , schema_classes ) : """Build a string that includes the ` ` # include pragma ` ` statements for the DMTF schema CIM classes defined in ` schema _ classes ` using the DMTF CIM schema defined by this object . The class names in this list can be just leaf classes . The pywbem MOF c...
if isinstance ( schema_classes , six . string_types ) : schema_classes = [ schema_classes ] schema_lines = [ ] with open ( self . schema_mof_file , 'r' ) as f : schema_lines = f . readlines ( ) output_lines = [ '#pragma locale ("en_US")\n' ] for cln in schema_classes : test_cln = '/{0}.mof' . format ( cln )...
def can_approve ( self , user , ** data ) : """Admins of repository service or sys admins can approve a repository : param user : a User : param data : data that the user wants to update"""
service_id = data . get ( 'service_id' , self . service_id ) try : service = yield Service . get ( service_id ) is_repo_admin = user . is_org_admin ( service . organisation_id ) is_reseller_preverifying = user . is_reseller ( ) and data . get ( 'pre_verified' , False ) raise Return ( is_repo_admin or is...
def register ( self , service , provider , singleton = False ) : """Registers a service provider for a given service . @ param service A key that identifies the service being registered . @ param provider This is either the service being registered , or a callable that will either instantiate it or return...
def get_singleton ( * args , ** kwargs ) : result = self . _get_singleton ( service ) if not result : instantiator = self . _get_instantiator ( provider ) result = instantiator ( * args , ** kwargs ) self . _set_singleton ( service , result ) return result # Providers are always regi...
def getenv ( key , value = None ) : """Like ` os . getenv ` but returns unicode under Windows + Python 2 Args : key ( pathlike ) : The env var to get value ( object ) : The value to return if the env var does not exist Returns : ` fsnative ` or ` object ` : The env var or the passed value if it doesn ' ...
key = path2fsn ( key ) if is_win and PY2 : return environ . get ( key , value ) return os . getenv ( key , value )
def get_module_data_path ( modname , relpath = None , attr_name = 'DATAPATH' ) : """Return module * modname * data path Note : relpath is ignored if module has an attribute named * attr _ name * Handles py2exe / cx _ Freeze distributions"""
datapath = getattr ( sys . modules [ modname ] , attr_name , '' ) if datapath : return datapath else : datapath = get_module_path ( modname ) parentdir = osp . join ( datapath , osp . pardir ) if osp . isfile ( parentdir ) : # Parent directory is not a directory but the ' library . zip ' file : # th...
def setCurrentSchemaColumn ( self , column ) : """Sets the current item based on the inputed column . : param column | < orb . Column > | | None"""
if column == self . _column : self . treeWidget ( ) . setCurrentItem ( self ) return True for c in range ( self . childCount ( ) ) : if self . child ( c ) . setCurrentSchemaColumn ( column ) : self . setExpanded ( True ) return True return None
def create_subscription ( self , client_id , client_secret , callback_url , object_type = model . Subscription . OBJECT_TYPE_ACTIVITY , aspect_type = model . Subscription . ASPECT_TYPE_CREATE , verify_token = model . Subscription . VERIFY_TOKEN_DEFAULT ) : """Creates a webhook event subscription . http : / / stra...
params = dict ( client_id = client_id , client_secret = client_secret , object_type = object_type , aspect_type = aspect_type , callback_url = callback_url , verify_token = verify_token ) raw = self . protocol . post ( '/push_subscriptions' , use_webhook_server = True , ** params ) return model . Subscription . deseria...
def add ( self , event_state , event_type , event_value , proc_list = None , proc_desc = "" , peak_time = 6 ) : """Add a new item to the logs list . If ' event ' is a ' new one ' , add it at the beginning of the list . If ' event ' is not a ' new one ' , update the list . If event < peak _ time then the alert...
proc_list = proc_list or glances_processes . getlist ( ) # Add or update the log event_index = self . __event_exist ( event_type ) if event_index < 0 : # Event did not exist , add it self . _create_event ( event_state , event_type , event_value , proc_list , proc_desc , peak_time ) else : # Event exist , update it ...
def pipupdate ( ) : """Update all currently installed pip packages"""
packages = [ d for d in pkg_resources . working_set ] subprocess . call ( 'pip install --upgrade ' + ' ' . join ( packages ) )
def equal_args ( * args , ** kwargs ) : """A memoized key factory that compares the equality ( ` = = ` ) of a stable sort of the parameters ."""
key = args if kwargs : key += _kwargs_separator + tuple ( sorted ( kwargs . items ( ) ) ) return key
def get_django_response ( proxy_response , strict_cookies = False ) : """This method is used to create an appropriate response based on the Content - Length of the proxy _ response . If the content is bigger than MIN _ STREAMING _ LENGTH , which is found on utils . py , than django . http . StreamingHttpRespo...
status = proxy_response . status headers = proxy_response . headers logger . debug ( 'Proxy response headers: %s' , headers ) content_type = headers . get ( 'Content-Type' ) logger . debug ( 'Content-Type: %s' , content_type ) if should_stream ( proxy_response ) : logger . info ( 'Content-Length is bigger than %s' ...
def get_token_info ( self ) : """Get information about the current access token ."""
response = requests . get ( self . _format_url ( OAUTH2_ROOT + 'token_info' , { 'token' : self . creds [ 'access_token' ] } ) ) data = response . json ( ) if response . status_code != 200 : raise _token_error_from_data ( data ) else : return data
def hash_file ( tar_filename ) : """hash the file"""
hasher = sha1 ( ) with open ( tar_filename , 'rb' ) as afile : buf = afile . read ( BLOCKSIZE ) while len ( buf ) > 0 : hasher . update ( buf ) buf = afile . read ( BLOCKSIZE ) data = HASHTMPL % ( hasher . hexdigest ( ) , os . path . basename ( tar_filename ) ) create_file ( '%s.sha1' % tar_file...
def mksls ( src , dst = None ) : '''Convert a kickstart file to an SLS file'''
mode = 'command' sls = { } ks_opts = { } with salt . utils . files . fopen ( src , 'r' ) as fh_ : for line in fh_ : if line . startswith ( '#' ) : continue if mode == 'command' : if line . startswith ( 'auth ' ) or line . startswith ( 'authconfig ' ) : ks_opts...
def visit_For ( self , node ) : """Handle iterator variable in for loops . Iterate variable may be the correct one at the end of the loop ."""
body = node . body if node . target . id in self . naming : body = [ ast . Assign ( targets = [ node . target ] , value = node . iter ) ] + body self . visit_any_conditionnal ( body , node . orelse ) else : iter_dep = self . visit ( node . iter ) self . naming [ node . target . id ] = iter_dep self ...
def connect ( self ) : '''Connects to the IRC server with the options defined in ` config `'''
self . _connect ( ) try : self . _listen ( ) except ( KeyboardInterrupt , SystemExit ) : pass finally : self . close ( )
def identical ( args ) : """% prog identical * . fasta Given multiple fasta files , find all the exactly identical records based on the computed md5 hexdigest or GCG checksum of each sequence . Output is an N + 1 column file ( where N = number of input fasta files ) . If there are duplicates within a given ...
from jcvi . utils . cbook import AutoVivification allowed_checksum = [ "MD5" , "GCG" ] p = OptionParser ( identical . __doc__ ) p . add_option ( "--ignore_case" , default = False , action = "store_true" , help = "ignore case when comparing sequences [default: %default]" ) p . add_option ( "--ignore_N" , default = False...
def from_array ( array ) : """Deserialize a new ForceReply from a given dictionary . : return : new ForceReply instance . : rtype : ForceReply"""
if array is None or not array : return None # end if assert_type_or_raise ( array , dict , parameter_name = "array" ) data = { } data [ 'force_reply' ] = bool ( array . get ( 'force_reply' ) ) data [ 'selective' ] = bool ( array . get ( 'selective' ) ) if array . get ( 'selective' ) is not None else None instance =...
def __send_api_file ( self , file_name ) : """Send apidoc files from the apidoc folder to the browser . This method replaces all absolute urls in the file by the current url . : param file _ name : the apidoc file ."""
file_name = join ( self . app . static_folder , file_name ) with codecs . open ( file_name , 'r' , 'utf-8' ) as file : data = file . read ( ) # replaces the hard coded url by the current url . api_project = self . __read_api_project ( ) old_url = api_project . get ( 'url' ) # replaces the project ' s url only if it...
def setcontext ( context , _local = local ) : """Set the current context to that given . Attributes provided by ` ` context ` ` override those in the current context . If ` ` context ` ` doesn ' t specify a particular attribute , the attribute from the current context shows through ."""
oldcontext = getcontext ( ) _local . __bigfloat_context__ = oldcontext + context
def find_version ( ) : """Only define version in one place"""
version_file = read_file ( '__init__.py' ) version_match = re . search ( r'^__version__ = ["\']([^"\']*)["\']' , version_file , re . M ) if version_match : return version_match . group ( 1 ) raise RuntimeError ( 'Unable to find version string.' )
def plot_histogram ( self , title_prefix = "" , title_override = "" , figsize = ( 8 , 6 ) ) : """Plots a histogram of the results after the Monte Carlo simulation is run . NOTE - This method must be called AFTER " roll _ mc " . : param title _ prefix : If desired , prefix the title ( such as " Alg 1 " ) : par...
# Check that roll _ mc has been called if not self . arr_res : raise ValueError ( "Call roll_mc before plotting the histogram." ) # Find a title using either the override or _ construct _ title method if title_override : title = title_override else : title = title_prefix + PBE . _construct_title ( self . nu...
def get_tcntobj ( go2obj , ** kws ) : """Return a TermCounts object if the user provides an annotation file , otherwise None ."""
# kws : gaf gene2go annots = read_annotations ( ** kws ) if annots : return TermCounts ( go2obj , annots )
def getElementById ( self , node , id , default = join ) : """Return the first child of node matching an id reference ."""
attrget = self . getAttr ELEMENT_NODE = node . ELEMENT_NODE for child in node . childNodes : if child . nodeType == ELEMENT_NODE : if attrget ( child , 'id' ) == id : return child if default is not join : return default raise KeyError , name
def _set_evpn_neighbor_ipv6 ( self , v , load = False ) : """Setter method for evpn _ neighbor _ ipv6 , mapped from YANG variable / rbridge _ id / router / router _ bgp / address _ family / l2vpn / evpn / neighbor / evpn _ neighbor _ ipv6 ( list ) If this variable is read - only ( config : false ) in the source...
if hasattr ( v , "_utype" ) : v = v . _utype ( v ) try : t = YANGDynClass ( v , base = YANGListType ( "evpn_neighbor_ipv6_address" , evpn_neighbor_ipv6 . evpn_neighbor_ipv6 , yang_name = "evpn-neighbor-ipv6" , rest_name = "evpn-neighbor-ipv6" , parent = self , is_container = 'list' , user_ordered = False , path...
def edit ( self , text ) : """Replace the body of the object with ` text ` . : returns : The updated object ."""
url = self . reddit_session . config [ 'edit' ] data = { 'thing_id' : self . fullname , 'text' : text } response = self . reddit_session . request_json ( url , data = data ) self . reddit_session . evict ( self . reddit_session . config [ 'user' ] ) return response [ 'data' ] [ 'things' ] [ 0 ]
def post ( self , path , body ) : """POST request ."""
return self . _make_request ( 'post' , self . _format_url ( API_ROOT + path ) , { 'json' : body } )
def toggle_input ( self ) : """Change behaviour of radio button based on input ."""
current_index = self . input . currentIndex ( ) # If current input is not a radio button enabler , disable radio button . if self . input . itemData ( current_index , Qt . UserRole ) != ( self . radio_button_enabler ) : self . disable_radio_button ( ) # Otherwise , enable radio button . else : self . enable_rad...
def main ( ) : """% prog database . fa query . fa [ options ] Wrapper for NCBI BLAST + ."""
p = OptionParser ( main . __doc__ ) p . add_option ( "--format" , default = " \'6 qseqid sseqid pident length " "mismatch gapopen qstart qend sstart send evalue bitscore\' " , help = "0-11, learn more with \"blastp -help\". [default: %default]" ) p . add_option ( "--path" , dest = "blast_path" , default = None , help =...
def skip ( stackframe = 1 ) : """Must be called from within ` _ _ enter _ _ ( ) ` . Performs some magic to have a # ContextSkipped exception be raised the moment the with context is entered . The # ContextSkipped must then be handled in ` _ _ exit _ _ ( ) ` to suppress the propagation of the exception . > I...
def trace ( frame , event , args ) : raise ContextSkipped sys . settrace ( lambda * args , ** kwargs : None ) frame = sys . _getframe ( stackframe + 1 ) frame . f_trace = trace
def parse_duration ( duration ) : """Attepmts to parse an ISO8601 formatted ` ` duration ` ` . Returns a ` ` datetime . timedelta ` ` object ."""
duration = str ( duration ) . upper ( ) . strip ( ) elements = ELEMENTS . copy ( ) for pattern in ( SIMPLE_DURATION , COMBINED_DURATION ) : if pattern . match ( duration ) : found = pattern . match ( duration ) . groupdict ( ) del found [ 'time' ] elements . update ( dict ( ( k , int ( v or ...
def create_object ( object_type , data , ** api_opts ) : '''Create raw infoblox object . This is a low level api call . CLI Example : . . code - block : : bash salt - call infoblox . update _ object object _ type = record : host data = { }'''
if '__opts__' in globals ( ) and __opts__ [ 'test' ] : return { 'Test' : 'Would attempt to create object: {0}' . format ( object_type ) } infoblox = _get_infoblox ( ** api_opts ) return infoblox . create_object ( object_type , data )
def hash_file ( path , hashobj , conn = None ) : '''Get the hexdigest hash value of a file'''
if os . path . isdir ( path ) : return '' with salt . utils . files . fopen ( path , 'r' ) as f : hashobj . update ( salt . utils . stringutils . to_bytes ( f . read ( ) ) ) return hashobj . hexdigest ( )
def find_elements_by_css_selector ( self , css_selector ) : """Finds elements by css selector . : Args : - css _ selector - CSS selector string , ex : ' a . nav # home ' : Returns : - list of WebElement - a list with elements if any was found . An empty list if not : Usage : elements = driver . find _...
return self . find_elements ( by = By . CSS_SELECTOR , value = css_selector )
def _resolve_model ( obj ) : """Resolve supplied ` obj ` to a Django model class . ` obj ` must be a Django model class itself , or a string representation of one . Useful in situations like GH # 1225 where Django may not have resolved a string - based reference to a model in another model ' s foreign key d...
if isinstance ( obj , six . string_types ) and len ( obj . split ( '.' ) ) == 2 : app_name , model_name = obj . split ( '.' ) resolved_model = apps . get_model ( app_name , model_name ) if resolved_model is None : msg = "Django did not return a model for {0}.{1}" raise ImproperlyConfigured (...
def check_author ( author , ** kwargs ) : """Check the presence of the author in the AUTHORS / THANKS files . Rules : - the author full name and email must appear in AUTHORS file : param authors : name of AUTHORS files : type authors : ` list ` : param path : path to the repository home : type path : st...
errors = [ ] authors = kwargs . get ( "authors" ) if not authors : errors . append ( '1:A100: ' + _author_codes [ 'A100' ] ) return errors exclude_author_names = kwargs . get ( "exclude_author_names" ) if exclude_author_names and author in exclude_author_names : return [ ] path = kwargs . get ( "path" ) if ...
def load_config_yaml ( self , flags , config_dict ) : """Load config dict and yaml dict and then override both with flags dict ."""
if config_dict is None : print ( 'Config File not specified. Using only input flags.' ) return flags try : config_yaml_dict = self . cfg_from_file ( flags [ 'YAML_FILE' ] , config_dict ) except KeyError : print ( 'Yaml File not specified. Using only input flags and config file.' ) return config_dict...
def cube2map ( data_cube , layout ) : r"""Cube to Map This method transforms the input data from a 3D cube to a 2D map with a specified layout Parameters data _ cube : np . ndarray Input data cube , 3D array of 2D images Layout : tuple 2D layout of 2D images Returns np . ndarray 2D map Raises ...
if data_cube . ndim != 3 : raise ValueError ( 'The input data must have 3 dimensions.' ) if data_cube . shape [ 0 ] != np . prod ( layout ) : raise ValueError ( 'The desired layout must match the number of input ' 'data layers.' ) return np . vstack ( [ np . hstack ( data_cube [ slice ( layout [ 1 ] * i , layou...
def merge_multipartobject ( upload_id , version_id = None ) : """Merge multipart object . : param upload _ id : The : class : ` invenio _ files _ rest . models . MultipartObject ` upload ID . : param version _ id : Optionally you can define which file version . ( Default : ` ` None ` ` ) : returns : The :...
mp = MultipartObject . query . filter_by ( upload_id = upload_id ) . one_or_none ( ) if not mp : raise RuntimeError ( 'Upload ID does not exists.' ) if not mp . completed : raise RuntimeError ( 'MultipartObject is not completed.' ) try : obj = mp . merge_parts ( version_id = version_id , progress_callback =...
def fully_expanded_path ( self ) : """Returns the absolutely absolute path . Calls os . ( normpath , normcase , expandvars and expanduser ) ."""
return os . path . abspath ( os . path . normpath ( os . path . normcase ( os . path . expandvars ( os . path . expanduser ( self . path ) ) ) ) )
def get_ldap_filter ( ldap_filter ) : # type : ( Any ) - > Optional [ Union [ LDAPFilter , LDAPCriteria ] ] """Retrieves the LDAP filter object corresponding to the given filter . Parses it the argument if it is an LDAPFilter instance : param ldap _ filter : An LDAP filter ( LDAPFilter or string ) : return : ...
if ldap_filter is None : return None if isinstance ( ldap_filter , ( LDAPFilter , LDAPCriteria ) ) : # No conversion needed return ldap_filter elif is_string ( ldap_filter ) : # Parse the filter return _parse_ldap ( ldap_filter ) # Unknown type raise TypeError ( "Unhandled filter type {0}" . format ( type (...
def fetchone ( self ) : """Fetch the next row"""
self . _check_executed ( ) if self . _rows is None or self . rownumber >= len ( self . _rows ) : return None result = self . _rows [ self . rownumber ] self . rownumber += 1 return result
def _permuted_dicts_of_specs ( specs ) : """Create { name : value } dict , one each for every permutation . Each permutation becomes a dictionary , with the keys being the attr names and the values being the corresponding value for that permutation . These dicts can then be directly passed to the Calc constru...
permuter = itertools . product ( * specs . values ( ) ) return [ dict ( zip ( specs . keys ( ) , perm ) ) for perm in permuter ]
def get_config ( k : str ) -> Any : """Returns a configuration variable ' s value or None if it is unset ."""
# If the config has been set explicitly , use it . if k in list ( CONFIG . keys ( ) ) : return CONFIG [ k ] # If there is a specific PULUMI _ CONFIG _ < k > environment variable , use it . env_key = get_config_env_key ( k ) if env_key in os . environ : return os . environ [ env_key ] # If the config hasn ' t be...
def convert_message_to_string ( self , message ) : """Convert message from list to string for GitHub API ."""
final_message = '' for submessage in message : final_message += '* {submessage}\n' . format ( submessage = submessage ) return final_message
def enable_result_transforms ( func ) : """Decorator that tries to use the object provided using a kwarg called ' electrode _ transformator ' to transform the return values of an import function . It is intended to be used to transform electrode numbers and locations , i . e . for use in roll - along - measur...
@ functools . wraps ( func ) def wrapper ( * args , ** kwargs ) : func_transformator = kwargs . pop ( 'electrode_transformator' , None ) data , electrodes , topography = func ( * args , ** kwargs ) if func_transformator is not None : data_transformed , electrodes_transformed , topography_transformed...