signature stringlengths 29 44.1k | implementation stringlengths 0 85.2k |
|---|---|
def announcement_view ( request , announcement_pk ) :
'''The view of a single manager announcement .''' | announce = get_object_or_404 ( Announcement , pk = announcement_pk )
page_name = "View Announcement"
profile = UserProfile . objects . get ( user = request . user )
pin_form = PinForm ( request . POST if "pin" in request . POST else None , instance = announce , )
can_edit = announce . incumbent == profile or request . ... |
def to_global ( s ) :
"""Format a global variable name .""" | if s . startswith ( 'GPSTime' ) :
s = 'Gps' + s [ 3 : ]
if '_' in s :
s = "" . join ( [ i . capitalize ( ) for i in s . split ( "_" ) ] )
return s [ 0 ] . lower ( ) + s [ 1 : ] |
def kwargs_to_string ( kwargs ) :
"""Given a set of kwargs , turns them into a string which can then be passed to a command .
: param kwargs : kwargs from a function call .
: return : outstr : A string , which is ' ' if no kwargs were given , and the kwargs in string format otherwise .""" | outstr = ''
for arg in kwargs :
outstr += ' -{} {}' . format ( arg , kwargs [ arg ] )
return outstr |
def rst_blocks ( script_blocks , output_blocks , file_conf , gallery_conf ) :
"""Generates the rst string containing the script prose , code and output
Parameters
script _ blocks : list
( label , content , line _ number )
List where each element is a tuple with the label ( ' text ' or ' code ' ) ,
the cor... | # A simple example has two blocks : one for the
# example introduction / explanation and one for the code
is_example_notebook_like = len ( script_blocks ) > 2
example_rst = u""
# there can be unicode content
for ( blabel , bcontent , lineno ) , code_output in zip ( script_blocks , output_blocks ) :
if blabel == 'co... |
def get_rna_counts ( rna_file , transcript_name ) :
"""Get coverage for a given RNA BAM file , return read counts .""" | # check if the RNA file exists
if not os . path . exists ( rna_file ) :
msg = 'RNA-Seq BAM file "{}" does not exist' . format ( rna_file )
logging . error ( msg )
raise OSError ( msg )
rna_counts = { }
cov_file = tempfile . NamedTemporaryFile ( delete = False )
try :
subprocess . check_call ( [ 'bedtool... |
def disable_hidden_api_blacklist ( self ) :
"""If necessary and possible , disables hidden api blacklist .""" | version_codename = self . _ad . adb . getprop ( 'ro.build.version.codename' )
sdk_version = int ( self . _ad . adb . getprop ( 'ro.build.version.sdk' ) )
# we check version _ codename in addition to sdk _ version because P builds
# in development report sdk _ version 27 , but still enforce the blacklist .
if self . _ad... |
def rebuild_token_map ( self , partitioner , token_map ) :
"""Rebuild our view of the topology from fresh rows from the
system topology tables .
For internal use only .""" | self . partitioner = partitioner
if partitioner . endswith ( 'RandomPartitioner' ) :
token_class = MD5Token
elif partitioner . endswith ( 'Murmur3Partitioner' ) :
token_class = Murmur3Token
elif partitioner . endswith ( 'ByteOrderedPartitioner' ) :
token_class = BytesToken
else :
self . token_map = None... |
def check ( ctx , repository , config ) :
"""Check commits .""" | ctx . obj = Repo ( repository = repository , config = config ) |
def as_list ( func ) :
"""A decorator used to return a JSON response of a list of model
objects . It expects the decorated function to return a list
of model instances . It then converts the instances to dicts
and serializes them into a json response
Examples :
> > > @ app . route ( ' / api ' )
. . . @ ... | @ wraps ( func )
def wrapper ( * args , ** kwargs ) :
response = func ( * args , ** kwargs )
if isinstance ( response , Response ) :
return response
return as_json_list ( response , ** _serializable_params ( request . args , check_groupby = True ) )
return wrapper |
def log ( level , msg , * args , ** kwargs ) :
"""Logs ' msg % args ' at absl logging level ' level ' .
If no args are given just print msg , ignoring any interpolation specifiers .
Args :
level : int , the absl logging level at which to log the message
( logging . DEBUG | INFO | WARNING | ERROR | FATAL ) .... | if level > converter . ABSL_DEBUG : # Even though this function supports level that is greater than 1 , users
# should use logging . vlog instead for such cases .
# Treat this as vlog , 1 is equivalent to DEBUG .
standard_level = converter . STANDARD_DEBUG - ( level - 1 )
else :
if level < converter . ABSL_FATA... |
def envs ( backend = None , sources = False ) :
'''Return the available fileserver environments . If no backend is provided ,
then the environments for all configured backends will be returned .
backend
Narrow fileserver backends to a subset of the enabled ones .
. . versionchanged : : 2015.5.0
If all pas... | fileserver = salt . fileserver . Fileserver ( __opts__ )
return sorted ( fileserver . envs ( back = backend , sources = sources ) ) |
def all ( self ) :
"""Get all collaborators .
Returns :
List [ str ] : Collaborators .""" | return [ email for email , action in self . _collaborators . items ( ) if action in [ RoleValue . Owner , RoleValue . User , ShareRequestValue . Add ] ] |
def to_dict ( mapreduce_yaml ) :
"""Converts a MapReduceYaml file into a JSON - encodable dictionary .
For use in user - visible UI and internal methods for interfacing with
user code ( like param validation ) . as a list
Args :
mapreduce _ yaml : The Pyton representation of the mapreduce . yaml document . ... | all_configs = [ ]
for config in mapreduce_yaml . mapreduce :
out = { "name" : config . name , "mapper_input_reader" : config . mapper . input_reader , "mapper_handler" : config . mapper . handler , }
if config . mapper . params_validator :
out [ "mapper_params_validator" ] = config . mapper . params_val... |
def create_function_from_response_pdu ( resp_pdu , req_pdu = None ) :
"""Parse response PDU and return instance of : class : ` ModbusFunction ` or
raise error .
: param resp _ pdu : PDU of response .
: param req _ pdu : Request PDU , some functions require more info than in
response PDU in order to create i... | function_code = pdu_to_function_code_or_raise_error ( resp_pdu )
function = function_code_to_function_map [ function_code ]
if req_pdu is not None and 'req_pdu' in inspect . getargspec ( function . create_from_response_pdu ) . args : # NOQA
return function . create_from_response_pdu ( resp_pdu , req_pdu )
return fu... |
def get_storage_key ( self , * args ) :
"""Return the redis key where to store the index for the given " value " ( ` args ` )
For this index , we store all PKs having for a field in the same sorted - set .
Key has this form :
model - name : field - name : sub - field - name : index - key - name
The ' : sub ... | args = list ( args )
args . pop ( )
# final value , not needed for the storage key
parts = [ self . model . _name , self . field . name , ] + args
if self . prefix :
parts . append ( self . prefix )
if self . key :
parts . append ( self . key )
return self . field . make_key ( * parts ) |
def normalize_array ( value ) :
"""5.1.1.4 - c . 5:
Arrays shall be rendered by :
1 . generating the normalized values of its elements ,
2 . joining these elements with single underscores ( _ ) into a single
character sequence , and
3 . using the first eight characters of the md5 checksum of this
charac... | norm_elements = [ ]
for element in value :
norm_elements . append ( normalize ( element ) )
norm_str = "_" . join ( norm_elements )
md5 = hashlib . md5 ( norm_str . encode ( 'utf-8' ) ) . hexdigest ( )
return md5 [ : 8 ] |
def remove_year ( name ) :
"""Removes year from input
: param name : path to edit
: return : inputs with no years""" | for i in range ( len ( name ) - 3 ) : # last index is length - 3 - 1 = length - 4
if name [ i : i + 4 ] . isdigit ( ) :
name = name [ : i ] + name [ i + 4 : ]
return remove_year ( name )
# if there is a removal , start again
return name |
def set_const ( const , val ) :
'''Convenience wrapper to reliably set the value of a constant from
outside of package scope''' | try :
cur = getattr ( _c , const )
except AttributeError :
raise FSQEnvError ( errno . ENOENT , u'no such constant:' u' {0}' . format ( const ) )
except TypeError :
raise TypeError ( errno . EINVAL , u'const name must be a string or' u' unicode object, not:' u' {0}' . format ( const . __class__ . __name__ )... |
def read_dist_egginfo_json ( dist , filename = DEFAULT_JSON ) :
"""Safely get a json within an egginfo from a distribution .""" | # use the given package ' s distribution to acquire the json file .
if not dist . has_metadata ( filename ) :
logger . debug ( "no '%s' for '%s'" , filename , dist )
return
try :
result = dist . get_metadata ( filename )
except IOError :
logger . error ( "I/O error on reading of '%s' for '%s'." , filena... |
def calc_qigz2_v1 ( self ) :
"""Aggregate the amount of the second interflow component released
by all HRUs .
Required control parameters :
| NHRU |
| FHRU |
Required flux sequence :
| QIB2 |
Calculated state sequence :
| QIGZ2 |
Basic equation :
: math : ` QIGZ2 = \\ Sigma ( FHRU \\ cdot QIB2 )... | con = self . parameters . control . fastaccess
flu = self . sequences . fluxes . fastaccess
sta = self . sequences . states . fastaccess
sta . qigz2 = 0.
for k in range ( con . nhru ) :
sta . qigz2 += con . fhru [ k ] * flu . qib2 [ k ] |
def _filter_satisfied ( self , update_setd = False ) :
"""This method extracts a model provided by the previous call to a SAT
oracle and iterates over all soft clauses checking if each of is
satisfied by the model . Satisfied clauses are marked accordingly
while the literals of the unsatisfied clauses are kep... | model = self . oracle . get_model ( )
setd = set ( )
for i , cl in enumerate ( self . soft ) :
if not self . satc [ i ] :
if self . _satisfied ( cl , model ) :
self . satc [ i ] = True
self . ss_assumps . append ( self . sels [ i ] )
else :
setd = setd . union ( s... |
def make_hashcode ( uuid , filepath , file_event ) :
"""Generate a SHA1 based on the given arguments .
: param uuid : perceval uuid of the item
: param filepath : path of the corresponding file
: param file _ event : commit file event
: returns : a SHA1 hash code""" | content = ':' . join ( [ uuid , filepath , file_event ] )
hashcode = hashlib . sha1 ( content . encode ( 'utf-8' ) )
return hashcode . hexdigest ( ) |
def get_fields_from_model ( self , model , fields ) :
"""Iterate over given < field > names ( in " orm query " notation ) and find
the actual field given the initial < model > .
If < field > is a tuple of the format ( ' field _ name ' , ' Verbose name ' ) ,
overwrite the field ' s verbose name with the given ... | model_fields = { }
for field in fields :
if isinstance ( field , tuple ) and len ( field ) == 2 :
field , verbose_name = field [ 0 ] , field [ 1 ]
else :
try :
model_field = get_fields_from_path ( model , field ) [ - 1 ]
verbose_name = model_field . verbose_name
e... |
def pivot ( self , speed , durationS = - 1.0 ) :
"""pivot ( ) controls the pivot speed of the RedBot . The values of the pivot function inputs
range from - 255:255 , with - 255 indicating a full speed counter - clockwise rotation .
255 indicates a full speed clockwise rotation""" | if speed < 0 :
self . left_fwd ( min ( abs ( speed ) , 255 ) )
self . right_rev ( min ( abs ( speed ) , 255 ) )
else :
self . left_rev ( min ( abs ( speed ) , 255 ) )
self . right_fwd ( min ( abs ( speed ) , 255 ) )
if durationS > 0 :
self . board . sleep ( durationS )
self . left_stop ( )
s... |
def get_orientation_degrees ( self ) :
"""Returns a dictionary object to represent the current orientation
in degrees , 0 to 360 , using the aircraft principal axes of
pitch , roll and yaw""" | orientation = self . get_orientation_radians ( )
for key , val in orientation . items ( ) :
deg = math . degrees ( val )
# Result is - 180 to + 180
orientation [ key ] = deg + 360 if deg < 0 else deg
return orientation |
def _set_port_channel ( self , v , load = False ) :
"""Setter method for port _ channel , mapped from YANG variable / interface / port _ channel ( list )
If this variable is read - only ( config : false ) in the
source YANG file , then _ set _ port _ channel is considered as a private
method . Backends lookin... | if hasattr ( v , "_utype" ) :
v = v . _utype ( v )
try :
t = YANGDynClass ( v , base = YANGListType ( "name" , port_channel . port_channel , yang_name = "port-channel" , rest_name = "Port-channel" , parent = self , is_container = 'list' , user_ordered = True , path_helper = self . _path_helper , yang_keys = 'na... |
def get_requests_session ( ) :
"""Set connection pool maxsize and block value to avoid ` connection pool full ` warnings .
: return : requests session""" | session = requests . sessions . Session ( )
session . mount ( 'http://' , HTTPAdapter ( pool_connections = 25 , pool_maxsize = 25 , pool_block = True ) )
session . mount ( 'https://' , HTTPAdapter ( pool_connections = 25 , pool_maxsize = 25 , pool_block = True ) )
return session |
def fburl ( parser , token ) :
"""Returns an absolute URL matching given view with its parameters .
This is a way to define links that aren ' t tied to a particular URL
configuration : :
{ % url path . to . some _ view arg1 , arg2 , name1 = value1 % }
The first argument is a path to a view . It can be an ab... | bits = token . contents . split ( ' ' )
if len ( bits ) < 2 :
raise template . TemplateSyntaxError ( "'%s' takes at least one argument" " (path to a view)" % bits [ 0 ] )
viewname = bits [ 1 ]
args = [ ]
kwargs = { }
asvar = None
if len ( bits ) > 2 :
bits = iter ( bits [ 2 : ] )
for bit in bits :
i... |
def get_default_value ( self ) :
"""Return the default value for the parameter . If here is no default value , return None""" | if ( 'default_value' in self . attributes and bool ( self . attributes [ 'default_value' ] . strip ( ) ) ) :
return self . attributes [ 'default_value' ]
else :
return None |
def are_in_interval ( s , l , r , border = 'included' ) :
"""Checks whether all number in the sequence s lie inside the interval formed by
l and r .""" | return numpy . all ( [ IntensityRangeStandardization . is_in_interval ( x , l , r , border ) for x in s ] ) |
def run ( configObj , wcsmap = None ) :
"""Run the blot task based on parameters provided interactively by the user .""" | # Insure all output filenames specified have . fits extensions
if configObj [ 'outdata' ] [ - 5 : ] != '.fits' :
configObj [ 'outdata' ] += '.fits'
scale_pars = configObj [ 'Data Scaling Parameters' ]
user_wcs_pars = configObj [ 'User WCS Parameters' ]
# PyFITS can be used here as it will always operate on
# output... |
def move_to ( x , y ) :
"""Moves the brush to a particular position .
Arguments :
x - a number between - 250 and 250.
y - a number between - 180 and 180.""" | _make_cnc_request ( "coord/{0}/{1}" . format ( x , y ) )
state [ 'turtle' ] . goto ( x , y ) |
def make_logger ( self , level = "INFO" ) :
"""Convenience function which creates a logger for the module .
INPUTS :
level ( default = " INFO " ) : Minimum log level for logged / streamed messages .
OUTPUTS :
logger Logger for the function . NOTE : Must be bound to variable named logger .""" | level = getattr ( logging , level . upper ( ) )
logger = logging . getLogger ( __name__ )
logger . setLevel ( logging . DEBUG )
fh = logging . FileHandler ( 'PyMORESANE.log' , mode = 'w' )
fh . setLevel ( level )
ch = logging . StreamHandler ( )
ch . setLevel ( level )
formatter = logging . Formatter ( '%(asctime)s [%(... |
def to_netcdf ( self , filename , compress = True ) :
"""Write InferenceData to file using netcdf4.
Parameters
filename : str
Location to write to
compress : bool
Whether to compress result . Note this saves disk space , but may make
saving and loading somewhat slower ( default : True ) .
Returns
st... | mode = "w"
# overwrite first , then append
if self . _groups : # check ' s whether a group is present or not .
for group in self . _groups :
data = getattr ( self , group )
kwargs = { }
if compress :
kwargs [ "encoding" ] = { var_name : { "zlib" : True } for var_name in data . va... |
def compute_csets_dTRAM ( connectivity , count_matrices , nn = None , callback = None ) :
r"""Computes the largest connected sets for dTRAM data .
Parameters
connectivity : string
one ' reversible _ pathways ' , ' neighbors ' , ' summed _ count _ matrix ' or None .
Selects the algorithm for measuring overla... | if connectivity == 'post_hoc_RE' or connectivity == 'BAR_variance' :
raise Exception ( 'Connectivity type %s not supported for dTRAM data.' % connectivity )
state_counts = _np . maximum ( count_matrices . sum ( axis = 1 ) , count_matrices . sum ( axis = 2 ) )
return _compute_csets ( connectivity , state_counts , co... |
def tracked_array ( array , dtype = None ) :
"""Properly subclass a numpy ndarray to track changes .
Avoids some pitfalls of subclassing by forcing contiguous
arrays , and does a view into a TrackedArray .
Parameters
array : array - like object
To be turned into a TrackedArray
dtype : np . dtype
Which... | # if someone passed us None , just create an empty array
if array is None :
array = [ ]
# make sure it is contiguous then view it as our subclass
tracked = np . ascontiguousarray ( array , dtype = dtype ) . view ( TrackedArray )
# should always be contiguous here
assert tracked . flags [ 'C_CONTIGUOUS' ]
return tra... |
def glob_wildcards ( pattern : str , files : Optional [ List [ str ] ] = None ) -> Dict [ str , Union [ List [ Any ] , List [ str ] ] ] :
"""Glob the values of the wildcards by matching the given pattern to the filesystem .
Returns a named tuple with a list of values for each wildcard .""" | pattern = os . path . normpath ( pattern )
if sys . platform == 'win32' : # we perform path matching with / slash only
pattern = pattern . replace ( '\\' , '/' )
first_wildcard = re . search ( "{[^{]" , pattern )
dirname = os . path . dirname ( pattern [ : first_wildcard . start ( ) ] ) if first_wildcard else os . ... |
def _recv_robust ( self , sock , size ) :
"""Receive size from sock , and retry if the recv ( ) call was interrupted .
( this is only required for python2 compatability )""" | while True :
try :
return sock . recv ( size )
except socket . error as e :
if e . errno != errno . EINTR :
raise |
def get_register_context ( self ) :
"""return hexadecimal dump of registers as expected by GDB""" | logging . debug ( "GDB getting register context" )
resp = b''
reg_num_list = [ reg . reg_num for reg in self . _register_list ]
vals = self . _context . read_core_registers_raw ( reg_num_list )
# print ( " Vals : % s " % vals )
for reg , regValue in zip ( self . _register_list , vals ) :
if reg . bitsize == 64 :
... |
def drop_message ( self , message ) :
"""Drop the given message object from the appropriate message list .
Returns True if the message was found , otherwise False .""" | for type_ in 'errors' , 'warnings' , 'notices' :
list_ = getattr ( self , type_ )
if message in list_ :
list_ . remove ( message )
if 'signing_severity' in message :
self . signing_summary [ message [ 'signing_severity' ] ] -= 1
return True
return False |
def get_xml_root ( xml_path ) :
"""Load and parse an xml by given xml _ path and return its root .
: param xml _ path : URL to a xml file
: type xml _ path : str
: return : xml root""" | r = requests . get ( xml_path )
root = ET . fromstring ( r . content )
return root |
def wcs_to_coords ( w , shape ) :
"""Generate an N x D list of pixel center coordinates where N is
the number of pixels and D is the dimensionality of the map .""" | if w . naxis == 2 :
y , x = wcs_to_axes ( w , shape )
elif w . naxis == 3 :
z , y , x = wcs_to_axes ( w , shape )
else :
raise Exception ( "Wrong number of WCS axes %i" % w . naxis )
x = 0.5 * ( x [ 1 : ] + x [ : - 1 ] )
y = 0.5 * ( y [ 1 : ] + y [ : - 1 ] )
if w . naxis == 2 :
x = np . ravel ( np . one... |
def build_catalog ( site , datasets , format = None ) :
'''Build the DCAT catalog for this site''' | site_url = url_for ( 'site.home_redirect' , _external = True )
catalog_url = url_for ( 'site.rdf_catalog' , _external = True )
graph = Graph ( namespace_manager = namespace_manager )
catalog = graph . resource ( URIRef ( catalog_url ) )
catalog . set ( RDF . type , DCAT . Catalog )
catalog . set ( DCT . title , Literal... |
def can_import ( self , file_uris , current_doc = None ) :
"""Check that the specified file looks like an image supported by PIL""" | if len ( file_uris ) <= 0 :
return False
for file_uri in file_uris :
file_uri = self . fs . safe ( file_uri )
if not self . check_file_type ( file_uri ) :
return False
return True |
def add_instruction ( self , target , data ) :
"""Add an instruction node to this element .
: param string text : text content to add as an instruction .""" | self . _add_instruction ( self . impl_node , target , data ) |
def com_google_fonts_check_name_description_max_length ( ttFont ) :
"""Description strings in the name table must not exceed 200 characters .""" | failed = False
for name in ttFont [ 'name' ] . names :
if ( name . nameID == NameID . DESCRIPTION and len ( name . string . decode ( name . getEncoding ( ) ) ) > 200 ) :
failed = True
break
if failed :
yield WARN , ( "A few name table entries with ID={} (NameID.DESCRIPTION)" " are longer than 20... |
def read_sbml ( filename ) :
"""Read the model from a SBML file .
: param filename : SBML filename to read the model from
: return : A tuple , consisting of : class : ` ~ means . core . model . Model ` instance ,
set of parameter values , and set of initial conditions variables .""" | import libsbml
if not os . path . exists ( filename ) :
raise IOError ( 'File {0!r} does not exist' . format ( filename ) )
reader = libsbml . SBMLReader ( )
document = reader . readSBML ( filename )
sbml_model = document . getModel ( )
if not sbml_model :
raise ValueError ( 'Cannot parse SBML model from {0!r}'... |
def _split ( rule ) :
"""Splits a rule whose len ( rhs ) > 2 into shorter rules .""" | rule_str = str ( rule . lhs ) + '__' + '_' . join ( str ( x ) for x in rule . rhs )
rule_name = '__SP_%s' % ( rule_str ) + '_%d'
yield Rule ( rule . lhs , [ rule . rhs [ 0 ] , NT ( rule_name % 1 ) ] , weight = rule . weight , alias = rule . alias )
for i in xrange ( 1 , len ( rule . rhs ) - 2 ) :
yield Rule ( NT ( ... |
def goBack ( self ) :
"""Goes up one level if possible and returns the url at the current level .
If it cannot go up , then a blank string will be returned .
: return < str >""" | if not self . canGoBack ( ) :
return ''
self . _blockStack = True
self . _index -= 1
self . emitCurrentChanged ( )
self . _blockStack = False
return self . currentUrl ( ) |
def delete_dcnm_out_part ( self , tenant_id , fw_dict , is_fw_virt = False ) :
"""Delete the DCNM OUT partition and update the result .""" | res = fw_const . DCNM_OUT_PART_DEL_SUCCESS
tenant_name = fw_dict . get ( 'tenant_name' )
ret = True
try :
self . _delete_partition ( tenant_id , tenant_name )
except Exception as exc :
LOG . error ( "deletion of Out Partition failed for tenant " "%(tenant)s, Exception %(exc)s" , { 'tenant' : tenant_id , 'exc' :... |
def _ack ( self , sender , uid , level , payload = None ) :
"""Replies to a message""" | content = { 'reply-to' : uid , 'reply-level' : level , 'payload' : payload }
self . __client . send_message ( sender , json . dumps ( content ) ) |
def DbPutAttributeAlias ( self , argin ) :
"""Define an alias for an attribute
: param argin : Str [ 0 ] = attribute name
Str [ 1 ] = attribute alias
: type : tango . DevVarStringArray
: return :
: rtype : tango . DevVoid""" | self . _log . debug ( "In DbPutAttributeAlias()" )
if len ( argin ) < 2 :
self . warn_stream ( "DataBase::DbPutAttributeAlias(): insufficient number of arguments " )
th_exc ( DB_IncorrectArguments , "insufficient number of arguments to put attribute alias" , "DataBase::DbPutAttributeAlias()" )
attribute_name = ... |
def visitEqualityExpression ( self , ctx ) :
"""expression : expression ( EQ | NEQ ) expression""" | arg1 , arg2 = conversions . to_same ( self . visit ( ctx . expression ( 0 ) ) , self . visit ( ctx . expression ( 1 ) ) , self . _eval_context )
if isinstance ( arg1 , str ) : # string equality is case - insensitive
equal = arg1 . lower ( ) == arg2 . lower ( )
else :
equal = arg1 == arg2
return equal if ctx . E... |
def _node_type ( st ) :
"""return a string indicating the type of special node represented by
the stat buffer st ( block , character , fifo , socket ) .""" | _types = [ ( stat . S_ISBLK , "block device" ) , ( stat . S_ISCHR , "character device" ) , ( stat . S_ISFIFO , "named pipe" ) , ( stat . S_ISSOCK , "socket" ) ]
for t in _types :
if t [ 0 ] ( st . st_mode ) :
return t [ 1 ] |
def is_dir ( self , follow_symlinks = True ) :
"""Return True if this entry is a directory or a symbolic link pointing to
a directory ; return False if the entry is or points to any other kind
of file , or if it doesn ’ t exist anymore .
The result is cached on the os . DirEntry object .
Args :
follow _ s... | try :
return ( self . _system . isdir ( path = self . _path , client_kwargs = self . _client_kwargs , virtual_dir = False ) or # Some directories only exists virtually in object path and
# don ' t have headers .
bool ( S_ISDIR ( self . stat ( ) . st_mode ) ) )
except ObjectPermissionError : # The directory ... |
def version ( self ) :
'''adb version''' | match = re . search ( r"(\d+)\.(\d+)\.(\d+)" , self . raw_cmd ( "version" ) . communicate ( ) [ 0 ] . decode ( "utf-8" ) )
return [ match . group ( i ) for i in range ( 4 ) ] |
def get_center ( self , element ) :
"""Get center coordinates of an element
: param element : either a WebElement , PageElement or element locator as a tuple ( locator _ type , locator _ value )
: returns : dict with center coordinates""" | web_element = self . get_web_element ( element )
location = web_element . location
size = web_element . size
return { 'x' : location [ 'x' ] + ( size [ 'width' ] / 2 ) , 'y' : location [ 'y' ] + ( size [ 'height' ] / 2 ) } |
def set_file ( path , saltenv = 'base' , ** kwargs ) :
'''Set answers to debconf questions from a file .
CLI Example :
. . code - block : : bash
salt ' * ' debconf . set _ file salt : / / pathto / pkg . selections''' | if '__env__' in kwargs : # " env " is not supported ; Use " saltenv " .
kwargs . pop ( '__env__' )
path = __salt__ [ 'cp.cache_file' ] ( path , saltenv )
if path :
_set_file ( path )
return True
return False |
def update_tag ( self , tag_name , description = None , custom_properties = None , ** kwargs ) :
"""update a tag by name
Args :
tag _ name ( string ) : name of tag to update
description ( optional [ string ] ) : a description
custom _ properties ( optional [ dict ] ) : dictionary of custom properties""" | data = { 'description' : description or '' , 'customProperties' : custom_properties or { } }
resp = self . _put ( self . _u ( self . _TAG_ENDPOINT_SUFFIX , tag_name ) , data = data , ** kwargs )
resp . raise_for_status ( )
return resp . json ( ) |
def create_time_subscription ( self , instance , on_data = None , timeout = 60 ) :
"""Create a new subscription for receiving time updates of an instance .
Time updates are emitted at 1Hz .
This method returns a future , then returns immediately . Stop the
subscription by canceling the future .
: param str ... | manager = WebSocketSubscriptionManager ( self , resource = 'time' )
# Represent subscription as a future
subscription = TimeSubscription ( manager )
wrapped_callback = functools . partial ( _wrap_callback_parse_time_info , subscription , on_data )
manager . open ( wrapped_callback , instance )
# Wait until a reply or e... |
def make_search_body ( index , query , params = None ) :
"""Generates a dictionary suitable for encoding as the search body
: param index : The index name to query
: param query : The query itself
: param params : Modifiers for the query
: type params : : class : ` couchbase . fulltext . Params `
: return... | dd = { }
if not isinstance ( query , Query ) :
query = QueryStringQuery ( query )
dd [ 'query' ] = query . encodable
if params :
dd . update ( params . as_encodable ( index ) )
dd [ 'indexName' ] = index
return dd |
def read_connections ( self , connections ) :
"""Reads connections from configuration parameters .
Each section represents an individual Connectionparams
: param connections : configuration parameters to be read""" | del self . _items [ : ]
for key in connections . get_key_names ( ) :
item = DiscoveryItem ( )
item . key = key
value = connections . get_as_nullable_string ( key )
item . connection = ConnectionParams . from_string ( value )
self . _items . append ( item ) |
def validate ( self ) :
"""validate : Makes sure question is valid
Args : None
Returns : boolean indicating if question is valid""" | assert self . id is not None , "Assumption Failed: Question must have an id"
assert isinstance ( self . question , str ) or self . question is None , "Assumption Failed: Question must be a string"
assert isinstance ( self . question_type , str ) , "Assumption Failed: Question type must be a string"
assert isinstance ( ... |
def on_notify_load_status ( self , webkitView , * args , ** kwargs ) :
"""Callback function when the page was loaded completely
FYI , this function will be called after $ ( document ) . ready ( )
in jQuery""" | status = webkitView . get_load_status ( )
if status == status . FINISHED :
if self . debug is True :
print 'Load finished' |
def _internal_verify_cas ( ticket , service , suffix ) :
"""Verifies CAS 2.0 and 3.0 XML - based authentication ticket .
Returns username on success and None on failure .""" | params = { 'ticket' : ticket , 'service' : service }
if settings . CAS_PROXY_CALLBACK :
params [ 'pgtUrl' ] = settings . CAS_PROXY_CALLBACK
url = ( urljoin ( settings . CAS_SERVER_URL , suffix ) + '?' + urlencode ( params ) )
page = urlopen ( url )
username = None
try :
response = page . read ( )
tree = Ele... |
def add_note ( self , player , text , label = None , update = None ) :
"""Add a note to the xml . If update param is None , it will be the current time .""" | if label is not None and ( label not in self . label_names ) :
raise LabelNotFoundError ( 'Invalid label: {}' . format ( label ) )
if update is None :
update = datetime . utcnow ( )
# converted to timestamp , rounded to ones
update = update . strftime ( '%s' )
label_id = self . _get_label_id ( label )
new_note ... |
def parse_plays_stream ( self ) :
"""Generate and yield a stream of parsed plays . Useful for per play processing .""" | lx_doc = self . html_doc ( )
if lx_doc is not None :
parser = PlayParser ( self . game_key . season , self . game_key . game_type )
plays = lx_doc . xpath ( '//tr[@class = "evenColor"]' )
for p in plays :
p_obj = parser . build_play ( p )
self . plays . append ( p_obj )
yield p_obj |
def _cancel ( self , msgId ) :
"""Cancel outstanding REQ , drop reply silently .
@ param msgId : message ID to cancel
@ type msgId : C { str }""" | _ , canceller = self . _requests . pop ( msgId , ( None , None ) )
if canceller is not None and canceller . active ( ) :
canceller . cancel ( ) |
def _seek_to_extent ( self , extent ) : # type : ( int ) - > None
'''An internal method to seek to a particular extent on the input ISO .
Parameters :
extent - The extent to seek to .
Returns :
Nothing .''' | self . _cdfp . seek ( extent * self . pvd . logical_block_size ( ) ) |
def _symlink_or_copy_grabix ( in_file , out_file , data ) :
"""We cannot symlink in CWL , but may be able to use inputs or copy""" | if cwlutils . is_cwl_run ( data ) : # Has grabix indexes , we ' re okay to go
if utils . file_exists ( in_file + ".gbi" ) :
out_file = in_file
else :
utils . copy_plus ( in_file , out_file )
else :
utils . symlink_plus ( in_file , out_file )
return out_file |
def generate_chunks ( data , chunk_size = DEFAULT_CHUNK_SIZE ) :
"""Yield ' chunk _ size ' items from ' data ' at a time .""" | iterator = iter ( repeated . getvalues ( data ) )
while True :
chunk = list ( itertools . islice ( iterator , chunk_size ) )
if not chunk :
return
yield chunk |
def get_string ( ) :
"""A better str ( _ get _ keycodes ( ) ) method""" | keycodes = _get_keycodes ( )
initial_code , codes = keycodes [ 0 ] , keycodes [ 1 : ]
initial_char = chr ( initial_code )
if initial_code == 27 :
initial_char = '\\e'
elif not ascii . isgraph ( initial_char ) :
initial_char = '\\x%x' % initial_code
chars = '' . join ( [ chr ( c ) for c in codes ] )
return '' . ... |
def _post ( self , url , ** kw ) :
'''Makes a POST request , setting Authorization
header by default''' | headers = kw . pop ( 'headers' , { } )
headers . setdefault ( 'Authorization' , self . AUTHORIZATION_HEADER )
kw [ 'headers' ] = headers
resp = self . session . post ( url , ** kw )
self . _raise_for_status ( resp )
return resp |
def get_password ( self , service , username ) :
"""Get password of the username for the service""" | items = self . _find_passwords ( service , username )
if not items :
return None
secret = items [ 0 ] . secret
return ( secret if isinstance ( secret , six . text_type ) else secret . decode ( 'utf-8' ) ) |
def show_header ( ** header ) :
"Display a HTTP - style header on the command - line ." | print ( '%s: %s' % ( 'Now' , header [ 'now' ] ) )
print ( '%s: %s' % ( 'Stop-Name' , header [ 'name' ] ) )
print ( '%s: %s' % ( 'Stop-ID' , header . get ( 'id' , None ) ) )
print ( '' ) |
def handle_set_command ( self , line : str , position : int , tokens : ParseResults ) -> ParseResults :
"""Handle a ` ` SET X = " Y " ` ` statement .""" | key , value = tokens [ 'key' ] , tokens [ 'value' ]
self . raise_for_invalid_annotation_value ( line , position , key , value )
self . annotations [ key ] = value
return tokens |
def onContinue ( self , event , grid , next_dia = None ) : # , age _ data _ type = ' site ' ) :
"""Save grid data in the data object""" | # deselect column , including remove ' EDIT ALL ' label
if self . grid_frame . drop_down_menu :
self . grid_frame . drop_down_menu . clean_up ( )
# remove ' * * ' and ' ^ ^ ' from col names
# self . remove _ starred _ labels ( grid )
grid . remove_starred_labels ( )
grid . SaveEditControlValue ( )
# locks in value ... |
def _compose_range ( pattern , rule , fill = 2 ) :
"""oc . _ compose _ range ( ' Week ' , ' Week04 - Week09 ' , fill = 2 ) - hash a range .
This takes apart a range of times and returns a dictionary of
all intervening values appropriately set . The fill value is
used to format the time numbers .""" | keys = [ ]
mask = len ( pattern )
for rule in str . split ( rule , "," ) :
if not '-' in rule :
if rule [ : mask ] == pattern :
keys . append ( rule [ mask : ] )
else :
keys . append ( rule )
else :
( start , end ) = str . split ( rule , '-' )
if rule [ : ... |
def find_tags_from_xml ( self , xml_name , tag_name , ** attribute_filter ) :
"""Return a list of all the matched tags in a specific xml
: param str xml _ name : specify from which xml to pick the tag from
: param str tag _ name : specify the tag name""" | xml = self . xml [ xml_name ]
if xml is None :
return [ ]
if xml . tag == tag_name :
if self . is_tag_matched ( xml . tag , ** attribute_filter ) :
return [ xml ]
return [ ]
tags = xml . findall ( ".//" + tag_name )
return [ tag for tag in tags if self . is_tag_matched ( tag , ** attribute_filter ) ... |
def set_metadata ( self , obj , metadata , clear = False , prefix = None ) :
"""Accepts a dictionary of metadata key / value pairs and updates the
specified object metadata with them .
If ' clear ' is True , any existing metadata is deleted and only the
passed metadata is retained . Otherwise , the values pas... | # Add the metadata prefix , if needed .
if prefix is None :
prefix = OBJECT_META_PREFIX
massaged = _massage_metakeys ( metadata , prefix )
cname = utils . get_name ( self . container )
oname = utils . get_name ( obj )
new_meta = { }
# Note that the API for object POST is the opposite of that for
# container POST : ... |
def dump ( self , indentation = 0 ) :
"""Returns a string representation of the structure .""" | dump = [ ]
dump . append ( '[{0}]' . format ( self . name ) )
printable_bytes = [ ord ( i ) for i in string . printable if i not in string . whitespace ]
# Refer to the _ _ set _ format _ _ method for an explanation
# of the following construct .
for keys in self . __keys__ :
for key in keys :
val = getattr... |
def respond ( self , result ) :
"""Create a response to this request .
When processing the request completed successfully this method can be used to
create a response object .
: param result : The result of the invoked method .
: type result : Anything that can be encoded by JSON .
: returns : A response ... | if self . one_way or self . unique_id is None :
return None
response = JSONRPCSuccessResponse ( )
response . result = result
response . unique_id = self . unique_id
return response |
def wrap ( query , container , namespace ) :
"""NORMALIZE QUERY SO IT CAN STILL BE JSON""" | if is_op ( query , QueryOp ) or query == None :
return query
query = wrap ( query )
table = container . get_table ( query [ 'from' ] )
schema = table . schema
output = QueryOp ( frum = table , format = query . format , limit = mo_math . min ( MAX_LIMIT , coalesce ( query . limit , DEFAULT_LIMIT ) ) )
if query . sel... |
def merge ( d1 , d2 ) :
"""Merge two raw datasets into one .
Parameters
d1 : dict
d2 : dict
Returns
dict""" | if d1 [ 'formula_id2latex' ] is None :
formula_id2latex = { }
else :
formula_id2latex = d1 [ 'formula_id2latex' ] . copy ( )
formula_id2latex . update ( d2 [ 'formula_id2latex' ] )
handwriting_datasets = d1 [ 'handwriting_datasets' ]
for dataset in d2 [ 'handwriting_datasets' ] :
handwriting_datasets . appe... |
def base_uri ( relative_path = '' ) :
"""Get absolute path to resource , works for dev and for PyInstaller""" | try : # PyInstaller creates a temp folder and stores path in _ MEIPASS
base_path = sys . _MEIPASS
except Exception :
if 'pytest' in sys . modules :
for arg in reversed ( sys . argv ) :
path = os . path . realpath ( arg )
if os . path . exists ( path ) :
base_path ... |
def gauss_fltr_opencv ( dem , size = 3 , sigma = 1 ) :
"""OpenCV Gaussian filter
Still propagates NaN values""" | import cv2
dem = malib . checkma ( dem )
dem_cv = cv2 . GaussianBlur ( dem . filled ( np . nan ) , ( size , size ) , sigma )
out = np . ma . fix_invalid ( dem_cv )
out . set_fill_value ( dem . fill_value )
return out |
def load_layer ( name , globals_dict = None , symb_list = None ) :
"""Loads a Scapy layer module to make variables , objects and functions
available globally .""" | _load ( "scapy.layers." + LAYER_ALIASES . get ( name , name ) , globals_dict = globals_dict , symb_list = symb_list ) |
def get_og_image_url ( self ) :
""": return : URL of the image to use in OG shares""" | li = self . get_list_image ( )
if li :
from easy_thumbnails . files import get_thumbnailer
thumb_url = get_thumbnailer ( li ) [ 'og_image' ] . url
# TODO : looks like this may fail if SITE _ DOMAIN = " acmi . lvh . me "
return urljoin ( settings . SITE_DOMAIN , thumb_url ) |
def read ( stream ) :
'''Read PLY data from a readable file - like object or filename .''' | ( must_close , stream ) = _open_stream ( stream , 'read' )
try :
data = PlyData . _parse_header ( stream )
for elt in data :
elt . _read ( stream , data . text , data . byte_order )
finally :
if must_close :
stream . close ( )
return data |
def run ( self ) :
"""runner""" | subprocess . check_call ( "python setup.py sdist" , shell = True )
subprocess . check_call ( "python setup.py bdist_wheel" , shell = True )
# OLD way :
# os . system ( " python setup . py sdist bdist _ wheel upload " )
# NEW way :
# Ref : https : / / packaging . python . org / distributing /
subprocess . check_call ( "... |
def label_by_time ( self , time_signals , label_names = [ ] , time_units = 'ms' , time_dimension = 0 , copy = True , backup_original_spike_times_to = None , ** kwargs ) :
"""creates a labeled spike data structure
` time _ signals ` is list of lists ( or matrix ) , containing a timestamp in the
first column ( or... | if self . data_format == 'empty' :
return SpikeContainer ( None , units = self . units , copy_from = self )
spike_times = self . spike_times . get_converted ( time_dimension , units = time_units ) [ 1 ] . copy ( )
# this is read only
time_signals = np . array ( time_signals )
re_zeroed_spike_times = spike_times . c... |
def remove_acl ( path ) :
"""Remove the ACL of the file or folder located on the given path .
Also remove the ACL of any file and folder below the given one ,
recursively .
Args :
path ( str ) : Path to the file or folder to remove the ACL for ,
recursively .""" | # Some files have ACLs , let ' s remove them recursively
if ( platform . system ( ) == constants . PLATFORM_DARWIN and os . path . isfile ( '/bin/chmod' ) ) :
subprocess . call ( [ '/bin/chmod' , '-R' , '-N' , path ] )
elif ( ( platform . system ( ) == constants . PLATFORM_LINUX ) and os . path . isfile ( '/bin/set... |
def _get_buffer ( self , index ) :
"""Shared bounds checking and buffer creation .""" | if not 0 <= index < self . count :
raise IndexError ( )
size = struct . calcsize ( self . format )
# We create the buffer every time instead of keeping the buffer ( which is 32 bytes at least )
# around forever .
buf = bytearray ( size + 1 )
buf [ 0 ] = self . first_register + size * index
return buf |
def create_repo_from_pip_url ( pip_url , ** kwargs ) :
r"""Return a object representation of a VCS repository via pip - style url .
: returns : instance of a repository object
: rtype : : class : ` libvcs . svn . SubversionRepo ` , : class : ` libvcs . git . GitRepo ` or
: class : ` libvcs . hg . MercurialRep... | if pip_url . startswith ( 'git+' ) :
return GitRepo . from_pip_url ( pip_url , ** kwargs )
elif pip_url . startswith ( 'hg+' ) :
return MercurialRepo . from_pip_url ( pip_url , ** kwargs )
elif pip_url . startswith ( 'svn+' ) :
return SubversionRepo . from_pip_url ( pip_url , ** kwargs )
else :
raise In... |
def post ( interface , method , version = 1 , apihost = DEFAULT_PARAMS [ 'apihost' ] , https = DEFAULT_PARAMS [ 'https' ] , caller = None , session = None , params = None ) :
"""Send POST request to an API endpoint
. . versionadded : : 0.8.3
: param interface : interface name
: type interface : str
: param ... | url = "%s://%s/%s/%s/v%s/" % ( 'https' if https else 'http' , apihost , interface , method , version )
return webapi_request ( url , 'POST' , caller = caller , session = session , params = params ) |
def get_inventory ( self , context ) :
"""Will locate vm in vcenter and fill its uuid
: type context : cloudshell . shell . core . context . ResourceCommandContext""" | vcenter_vm_name = context . resource . attributes [ 'vCenter VM' ]
vcenter_vm_name = vcenter_vm_name . replace ( '\\' , '/' )
vcenter_name = context . resource . attributes [ 'vCenter Name' ]
self . logger . info ( 'start autoloading vm_path: {0} on vcenter: {1}' . format ( vcenter_vm_name , vcenter_name ) )
with Cloud... |
def plot_energy ( data , kind = "kde" , bfmi = True , figsize = None , legend = True , fill_alpha = ( 1 , 0.75 ) , fill_color = ( "C0" , "C5" ) , bw = 4.5 , textsize = None , fill_kwargs = None , plot_kwargs = None , ax = None , ) :
"""Plot energy transition distribution and marginal energy distribution in HMC algo... | energy = convert_to_dataset ( data , group = "sample_stats" ) . energy . values
if ax is None :
_ , ax = plt . subplots ( figsize = figsize , constrained_layout = True )
if fill_kwargs is None :
fill_kwargs = { }
if plot_kwargs is None :
plot_kwargs = { }
figsize , _ , _ , xt_labelsize , linewidth , _ = _sc... |
def readline ( self ) :
"""Get the next line from the input buffer .""" | self . line_number += 1
if self . line_number > len ( self . lines ) :
return ''
return self . lines [ self . line_number - 1 ] |
def _create_environment ( config , outdir ) :
"""Constructor for an instance of the environment .
Args :
config : Object providing configurations via attributes .
outdir : Directory to store videos in .
Raises :
NotImplementedError : For action spaces other than Box and Discrete .
Returns :
Wrapped Op... | if isinstance ( config . env , str ) :
env = gym . make ( config . env )
else :
env = config . env ( )
# Ensure that the environment has the specification attribute set as expected
# by the monitor wrapper .
if not hasattr ( env , 'spec' ) :
setattr ( env , 'spec' , getattr ( env , 'spec' , None ) )
if conf... |
def two_way_difference ( self , b , extra_add = ( ) , extra_remove = ( ) ) :
"""Return ( self - b , b - self )""" | if self is b :
return ( ( ) , ( ) )
if isinstance ( b , DiffRef_ ) :
extra_remove = extra_remove + b . add
b = b . origin
if extra_add == extra_remove :
extra_add = extra_remove = ( )
if isinstance ( b , Diff_ ) :
if self . base is b . base :
first = self . add + b . remove
second = ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.