signature stringlengths 29 44.1k | implementation stringlengths 0 85.2k |
|---|---|
def honeypot_equals ( val ) :
"""Default verifier used if HONEYPOT _ VERIFIER is not specified .
Ensures val = = HONEYPOT _ VALUE or HONEYPOT _ VALUE ( ) if it ' s a callable .""" | expected = getattr ( settings , 'HONEYPOT_VALUE' , '' )
if callable ( expected ) :
expected = expected ( )
return val == expected |
def get_lib_module_dict ( self ) :
"""Load the ' lib ' directory as a python module , so it can be used to provide functions
for rowpipe transforms . This only works filesystem packages""" | from importlib import import_module
if not self . ref :
return { }
u = parse_app_url ( self . ref )
if u . scheme == 'file' :
if not self . set_sys_path ( ) :
return { }
for module_name in self . lib_dir_names :
try :
m = import_module ( module_name )
return { k : v f... |
def has_main_target ( self , name ) :
"""Tells if a main target with the specified name exists .""" | assert isinstance ( name , basestring )
if not self . built_main_targets_ :
self . build_main_targets ( )
return name in self . main_target_ |
def _find_aux_coord_vars ( self , ds , refresh = False ) :
'''Returns a list of auxiliary coordinate variables
An auxiliary coordinate variable is any netCDF variable that contains
coordinate data , but is not a coordinate variable ( in the sense of the term
defined by CF ) .
: param netCDF4 . Dataset ds : ... | if self . _aux_coords . get ( ds , None ) and refresh is False :
return self . _aux_coords [ ds ]
self . _aux_coords [ ds ] = cfutil . get_auxiliary_coordinate_variables ( ds )
return self . _aux_coords [ ds ] |
def speech ( self ) -> str :
"""Report summary designed to be read by a text - to - speech program""" | if not self . data :
self . update ( )
return speech . metar ( self . data , self . units ) |
def get_sky_coords ( self ) :
"""Get the sky coordinates of all the pixels in this pixelization""" | if self . _ipix is None :
theta , phi = hp . pix2ang ( self . _nside , list ( range ( self . _npix ) ) , self . _nest )
else :
theta , phi = hp . pix2ang ( self . _nside , self . _ipix , self . _nest )
lat = np . degrees ( ( np . pi / 2 ) - theta )
lon = np . degrees ( phi )
return np . vstack ( [ lon , lat ] )... |
def preprocess ( self , source , name = None , filename = None ) :
"""Preprocesses the source with all extensions . This is automatically
called for all parsing and compiling methods but * not * for : meth : ` lex `
because there you usually only want the actual source tokenized .""" | return reduce ( lambda s , e : e . preprocess ( s , name , filename ) , self . iter_extensions ( ) , unicode ( source ) ) |
def iostat ( interval = 1 , count = 5 , disks = None ) :
'''Gather and return ( averaged ) IO stats .
. . versionadded : : 2016.3.0
. . versionchanged : : 2016.11.4
Added support for AIX
CLI Example :
. . code - block : : bash
salt ' * ' disk . iostat 1 5 disks = sda''' | if salt . utils . platform . is_linux ( ) :
return _iostat_linux ( interval , count , disks )
elif salt . utils . platform . is_freebsd ( ) :
return _iostat_fbsd ( interval , count , disks )
elif salt . utils . platform . is_aix ( ) :
return _iostat_aix ( interval , count , disks ) |
def KnowsFile ( self , filename ) :
"""Looks at extension and decides if it knows
how to manage this file""" | if self . _isMediaFile ( filename ) or self . _isConfigFile ( filename ) :
return True
return False |
def find_existing_split_discordants ( data ) :
"""Check for pre - calculated split reads and discordants done as part of alignment streaming .""" | in_bam = dd . get_align_bam ( data )
sr_file = "%s-sr.bam" % os . path . splitext ( in_bam ) [ 0 ]
disc_file = "%s-disc.bam" % os . path . splitext ( in_bam ) [ 0 ]
if utils . file_exists ( sr_file ) and utils . file_exists ( disc_file ) :
return sr_file , disc_file
else :
sr_file = dd . get_sr_bam ( data )
... |
def rpc_get_DID_record ( self , did , ** con_info ) :
"""Given a DID , return the name or subdomain it corresponds to""" | if not isinstance ( did , ( str , unicode ) ) :
return { 'error' : 'Invalid DID: not a string' , 'http_status' : 400 }
try :
did_info = parse_DID ( did )
except :
return { 'error' : 'Invalid DID' , 'http_status' : 400 }
res = None
if did_info [ 'name_type' ] == 'name' :
res = self . get_name_DID_record ... |
def setblocking ( self , blocking ) :
'''Set whether or not this message is blocking''' | for sock in self . socket ( ) :
sock . setblocking ( blocking )
self . _blocking = blocking |
def _job_completed ( self , job_name , success , message ) :
"""Internal method .
Called when a job of a long running task completes .""" | job = self . _objects [ job_name ] [ Interface [ 'Job' ] ]
action = self . _action_by_operation . get ( job [ 'Operation' ] )
if not action :
return
# We only handle events , which are associated to exactly one object :
object_path , = job [ 'Objects' ]
device = self [ object_path ]
if success : # It rarely happens... |
def prepare_denovo_input_narrowpeak ( inputfile , params , outdir ) :
"""Prepare a narrowPeak file for de novo motif prediction .
All regions to same size ; split in test and validation set ;
converted to FASTA .
Parameters
inputfile : str
BED file with input regions .
params : dict
Dictionary with pa... | bedfile = os . path . join ( outdir , "input.from.narrowpeak.bed" )
p = re . compile ( r'^(#|track|browser)' )
width = int ( params [ "width" ] )
logger . info ( "preparing input (narrowPeak to BED, width %s)" , width )
warn_no_summit = True
with open ( bedfile , "w" ) as f_out :
with open ( inputfile ) as f_in :
... |
def get_station_board ( self , crs , rows = 17 , include_departures = True , include_arrivals = False , destination_crs = None , origin_crs = None ) :
"""Query the darwin webservice to obtain a board for a particular station
and return a StationBoard instance
Positional arguments :
crs - - the three letter CR... | # Determine the darwn query we want to make
if include_departures and include_arrivals :
query_type = 'GetArrivalDepartureBoard'
elif include_departures :
query_type = 'GetDepartureBoard'
elif include_arrivals :
query_type = 'GetArrivalBoard'
else :
raise ValueError ( "get_station_board must have either... |
def mdot_t ( self , ifig = None , lims = [ 7.4 , 2.6 , - 8.5 , - 4.5 ] , label = None , colour = None , s2ms = False , dashes = None ) :
"""Plot mass loss history as a function of log - time - left
Parameters
ifig : integer or string
Figure label , if None the current figure is used
The default value is Non... | fsize = 18
params = { 'axes.labelsize' : fsize , # ' font . family ' : ' serif ' ,
'font.family' : 'Times New Roman' , 'figure.facecolor' : 'white' , 'text.fontsize' : fsize , 'legend.fontsize' : fsize , 'xtick.labelsize' : fsize * 0.8 , 'ytick.labelsize' : fsize * 0.8 , 'text.usetex' : False }
try :
pl . rcParams ... |
def spdhg_generic ( x , f , g , A , tau , sigma , niter , ** kwargs ) :
r"""Computes a saddle point with a stochastic PDHG .
This means , a solution ( x * , y * ) , y * = ( y * _ 1 , . . . , y * _ n ) such that
( x * , y * ) in arg min _ x max _ y sum _ i = 1 ^ n < y _ i , A _ i > - f * [ i ] ( y _ i ) + g ( x ... | # Callback object
callback = kwargs . pop ( 'callback' , None )
if callback is not None and not callable ( callback ) :
raise TypeError ( '`callback` {} is not callable' '' . format ( callback ) )
# Dual variable
y = kwargs . pop ( 'y' , None )
if y is None :
y = A . range . zero ( )
# Adjoint of dual variable
... |
def split_code_at_show ( text ) :
"""Split code at plt . show ( )""" | parts = [ ]
is_doctest = contains_doctest ( text )
part = [ ]
for line in text . split ( "\n" ) :
if ( not is_doctest and line . strip ( ) == 'plt.show()' ) or ( is_doctest and line . strip ( ) == '>>> plt.show()' ) :
part . append ( line )
parts . append ( "\n" . join ( part ) )
part = [ ]
... |
def corpus_page_generator ( corpus_files , tmp_dir , max_page_size_exp ) :
"""Generate pages from a list of . 7z encoded history dumps .
Args :
corpus _ files : a list of strings
tmp _ dir : a string
max _ page _ size _ exp : an integer
Yields :
strings""" | for remote_filepath in corpus_files :
filepath = maybe_copy_file_to_directory ( remote_filepath , tmp_dir )
tf . logging . info ( "Reading from " + filepath )
command = [ "7z" , "x" , "-so" , filepath ]
tf . logging . info ( "Running command: %s" , command )
p = subprocess . Popen ( command , stdout... |
def extract ( self , searches , tree = None , as_dict = True ) :
"""> > > foo = pdf . extract ( [ [ ' pages ' , ' LTPage ' ] ] )
> > > foo
{ ' pages ' : [ < LTPage > , < LTPage > ] }
> > > pdf . extract ( [ [ ' bar ' , ' : in _ bbox ( " 100,100,400,400 " ) ' ] ] , foo [ ' pages ' ] [ 0 ] )
{ ' bar ' : [ < L... | if self . tree is None or self . pq is None :
self . load ( )
if tree is None :
pq = self . pq
else :
pq = PyQuery ( tree , css_translator = PDFQueryTranslator ( ) )
results = [ ]
formatter = None
parent = pq
for search in searches :
if len ( search ) < 3 :
search = list ( search ) + [ formatter... |
def _partial ( self ) :
"""Callback for partial output .""" | raw_stdout = self . _process . readAllStandardOutput ( )
stdout = handle_qbytearray ( raw_stdout , self . _get_encoding ( ) )
if self . _partial_stdout is None :
self . _partial_stdout = stdout
else :
self . _partial_stdout += stdout
self . sig_partial . emit ( self , stdout , None ) |
def _hilink_decrypt ( self , encrypted_firmware ) :
'''This does the actual decryption .''' | cipher = DES . new ( self . DES_KEY , DES . MODE_ECB )
p1 = encrypted_firmware [ 0 : 3 ]
p2 = encrypted_firmware [ 3 : ]
p2 += b"\x00" * ( 8 - ( len ( p2 ) % 8 ) )
d1 = p1 + cipher . decrypt ( p2 )
d1 += b"\x00" * ( 8 - ( len ( d1 ) % 8 ) )
return cipher . decrypt ( d1 ) |
def on_channel_open ( self , channel ) :
"""This method is invoked by pika when the channel has been opened .
The channel object is passed in so we can make use of it .
Since the channel is now open , we ' ll declare the exchange to use .
: param pika . channel . Channel channel : The channel object""" | logger . info ( 'Channel opened' , channel = channel )
self . _channel = channel
self . add_on_channel_close_callback ( )
self . setup_exchange ( self . _exchange ) |
def send ( self , stack : Layers ) :
"""Add a message stack to the send list .""" | if not isinstance ( stack , Stack ) :
stack = Stack ( stack )
if not self . platform . accept ( stack ) :
raise UnacceptableStack ( 'The platform does not allow "{}"' . format ( stack . describe ( ) ) )
self . _stacks . append ( stack ) |
def unify ( self , matching = None , sources = None , fast_matching = False , no_strict_matching = False , interactive = False , recovery = False ) :
"""Merge unique identities using a matching algorithm .
This method looks for sets of similar identities , merging those
identities into one unique identity . To ... | matcher = None
if not matching :
matching = 'default'
strict = not no_strict_matching
self . recovery = recovery
try :
blacklist = api . blacklist ( self . db )
matcher = create_identity_matcher ( matching , blacklist , sources , strict )
except MatcherNotSupportedError as e :
self . error ( str ( e ) )... |
def get_duration ( self ) :
"""Get duration formatted
Format is : " HHh MMm SSs "
Example : " 10h 20m 40s "
: return : Formatted duration
: rtype : str""" | mins , secs = divmod ( self . duration_sec , 60 )
hours , mins = divmod ( mins , 60 )
return "%02dh %02dm %02ds" % ( hours , mins , secs ) |
def _apply_overrides ( settings , overrides , schema ) :
"""Get overrides config overlayed onto modules defaults .
: param modules : require stack modules config .
: returns : dictionary of modules config with user overrides applied .""" | if overrides :
for k , v in six . iteritems ( overrides ) :
if k in schema :
if schema [ k ] is None :
settings [ k ] = v
elif type ( schema [ k ] ) is dict :
settings [ k ] = _apply_overrides ( settings [ k ] , overrides [ k ] , schema [ k ] )
... |
def currentRepoTreeItemChanged ( self ) :
"""Called to update the GUI when a repo tree item has changed or a new one was selected .""" | # When the model is empty the current index may be invalid and the currentItem may be None .
currentItem , currentIndex = self . getCurrentItem ( )
hasCurrent = currentIndex . isValid ( )
assert hasCurrent == ( currentItem is not None ) , "If current idex is valid, currentIndex may not be None"
# sanity check
# Set the... |
def single_read ( self , register ) :
'''Reads data from desired register only once .''' | comm_reg = ( 0b00010 << 3 ) + register
if register == self . AD7730_STATUS_REG :
bytes_num = 1
elif register == self . AD7730_DATA_REG :
bytes_num = 3
elif register == self . AD7730_MODE_REG :
bytes_num = 2
elif register == self . AD7730_FILTER_REG :
bytes_num = 3
elif register == self . AD7730_DAC_REG ... |
def add_transition ( self , source : str , dest : str ) :
"""Adds a transition from one state to another .
Args :
source ( str ) : the name of the state from where the transition starts
dest ( str ) : the name of the state where the transition ends""" | self . _transitions [ source ] . append ( dest ) |
def create_alias ( self ) :
"""Create lambda alias with env name and points it to $ LATEST .""" | LOG . info ( 'Creating alias %s' , self . env )
try :
self . lambda_client . create_alias ( FunctionName = self . app_name , Name = self . env , FunctionVersion = '$LATEST' , Description = 'Alias for {}' . format ( self . env ) )
except boto3 . exceptions . botocore . exceptions . ClientError as error :
LOG . d... |
def quote ( self , s ) :
"""Return a shell - escaped version of the string s .""" | if six . PY2 :
from pipes import quote
else :
from shlex import quote
return quote ( s ) |
def list_dms ( archive , compression , cmd , verbosity , interactive ) :
"""List a DMS archive .""" | check_archive_ext ( archive )
return [ cmd , 'v' , archive ] |
def zoompan ( stream , ** kwargs ) :
"""Apply Zoom & Pan effect .
Args :
zoom : Set the zoom expression . Default is 1.
x : Set the x expression . Default is 0.
y : Set the y expression . Default is 0.
d : Set the duration expression in number of frames . This sets for how many number of frames effect wil... | return FilterNode ( stream , zoompan . __name__ , kwargs = kwargs ) . stream ( ) |
def get_yaps_by_name ( root , name , afun = lambda x : x , default = None ) :
"""From XML root , return value of node matching attribute ' name ' .
Arguments :
root ( Element ) Root XML node ( xml . etree . ElementTree Element ) .
This is the root of the entire XML document , not the YAPS
subtree .
name (... | node = root . find ( "ParamMap[@name='YAPS']/ParamLong[@name='%s']/value" % name )
if node is not None :
return ( afun ( node . text ) )
else :
return ( default ) |
def _get_diff_text ( old , new ) :
'''Returns the diff of two text blobs .''' | diff = difflib . unified_diff ( old . splitlines ( 1 ) , new . splitlines ( 1 ) )
return '' . join ( [ x . replace ( '\r' , '' ) for x in diff ] ) |
def _add_method_to_class ( target_cls , method_name , func , dataset_name , dataset , dataprovider , repeat_suffix , ) :
"""Add the described method to the given class .
: param target _ cls :
Test class to which to add a method .
: type target _ cls :
` class `
: param method _ name :
Base name of the ... | # pylint : disable = too - many - arguments
test_method_name_for_dataset = _build_final_method_name ( method_name , dataset_name , dataprovider . __name__ if dataprovider else None , repeat_suffix , )
test_method_for_dataset = _build_test_method ( func , dataset , dataprovider )
test_method_for_dataset = functools . up... |
def read_config ( self , correlation_id , parameters ) :
"""Reads configuration and parameterize it with given values .
: param correlation _ id : ( optional ) transaction id to trace execution through call chain .
: param parameters : values to parameters the configuration or null to skip parameterization .
... | value = self . _read_object ( correlation_id , parameters )
return ConfigParams . from_value ( value ) |
def _evaluate ( self ) :
"""Scan for orphaned records and retrieve any records that have not already been grabbed""" | retrieved_records = SortedDict ( )
for record_id , record in six . iteritems ( self . _elements ) :
if record is self . _field . _unset : # Record has not yet been retrieved , get it
try :
record = self . target_app . records . get ( id = record_id )
except SwimlaneHTTP400Error : # Recor... |
def create_ini ( self , board , project_dir = '' , sayyes = False ) :
"""Creates a new apio project file""" | project_dir = util . check_dir ( project_dir )
ini_path = util . safe_join ( project_dir , PROJECT_FILENAME )
# Check board
boards = Resources ( ) . boards
if board not in boards . keys ( ) :
click . secho ( 'Error: no such board \'{}\'' . format ( board ) , fg = 'red' )
sys . exit ( 1 )
if isfile ( ini_path ) ... |
def _is_leap_year ( year ) :
"""Determine if a year is leap year .
Parameters
year : numeric
Returns
isleap : array of bools""" | isleap = ( ( np . mod ( year , 4 ) == 0 ) & ( ( np . mod ( year , 100 ) != 0 ) | ( np . mod ( year , 400 ) == 0 ) ) )
return isleap |
def time_boxed ( func , iterable , time_budget , * args ) :
"""Apply a function to the items of an iterable within a given time budget .
Loop the given iterable , calling the given function on each item . The expended
time is compared to the given time budget after each iteration .""" | time_budget = time_budget / 1000
# budget in milliseconds
start = time . time ( )
for thing in iterable :
yield func ( thing , * args )
end = time . time ( ) - start
if end > time_budget : # Putting the condition at the end of the loop ensures that we
# always run it once , which is useful for testing
... |
def split_input ( cls , job_config ) :
"""Returns a list of input readers .
An equal number of input files are assigned to each shard ( + / - 1 ) . If there
are fewer files than shards , fewer than the requested number of shards will
be used . Input files are currently never split ( although for some formats ... | reader_params = job_config . input_reader_params
bucket = reader_params [ cls . BUCKET_NAME_PARAM ]
filenames = reader_params [ cls . OBJECT_NAMES_PARAM ]
delimiter = reader_params . get ( cls . DELIMITER_PARAM )
account_id = reader_params . get ( cls . _ACCOUNT_ID_PARAM )
buffer_size = reader_params . get ( cls . BUFF... |
def update_unnamed_class ( decls ) :
"""Adds name to class _ t declarations .
If CastXML is being used , the type definitions with an unnamed
class / struct are split across two nodes in the XML tree . For example ,
typedef struct { } cls ;
produces
< Struct id = " _ 7 " name = " " context = " _ 1 " . . .... | for decl in decls :
if isinstance ( decl , declarations . typedef_t ) :
referent = decl . decl_type
if isinstance ( referent , declarations . elaborated_t ) :
referent = referent . base
if not isinstance ( referent , declarations . declarated_t ) :
continue
re... |
def rank ( self , member ) :
"""Get the rank ( index of a member ) .""" | score = self . _members . get ( member )
if score is None :
return None
return bisect_left ( self . _scores , ( score , member ) ) |
def check_sizes ( size , width , height ) :
"""Check that these arguments , if supplied , are consistent .
Return a ( width , height ) pair .""" | if not size :
return width , height
if len ( size ) != 2 :
raise ProtocolError ( "size argument should be a pair (width, height)" )
if width is not None and width != size [ 0 ] :
raise ProtocolError ( "size[0] (%r) and width (%r) should match when both are used." % ( size [ 0 ] , width ) )
if height is not ... |
def get_countries_geo_zone_by_id ( cls , countries_geo_zone_id , ** kwargs ) :
"""Find CountriesGeoZone
Return single instance of CountriesGeoZone 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 _ ... | kwargs [ '_return_http_data_only' ] = True
if kwargs . get ( 'async' ) :
return cls . _get_countries_geo_zone_by_id_with_http_info ( countries_geo_zone_id , ** kwargs )
else :
( data ) = cls . _get_countries_geo_zone_by_id_with_http_info ( countries_geo_zone_id , ** kwargs )
return data |
def emit ( self , span_datas ) :
""": type span _ datas : list of : class :
` ~ opencensus . trace . span _ data . SpanData `
: param list of opencensus . trace . span _ data . SpanData span _ datas :
SpanData tuples to emit""" | project = 'projects/{}' . format ( self . project_id )
# Map each span data to it ' s corresponding trace id
trace_span_map = defaultdict ( list )
for sd in span_datas :
trace_span_map [ sd . context . trace_id ] += [ sd ]
stackdriver_spans = [ ]
# Write spans to Stackdriver
for _ , sds in trace_span_map . items ( ... |
def http_download ( url , target_path ) :
"""Download file to local
Args :
- url ( string ) : url request path
- target _ path ( string ) : download destination""" | r = requests . get ( url , stream = True )
with open ( target_path , 'wb' ) as f : # shutil . copyfileobj ( resp , f )
for chunk in r . iter_content ( chunk_size = 1024 ) :
if chunk :
f . write ( chunk )
return target_path |
def setStation ( self , number ) :
"""Select the given station number""" | # If we press up at the first station , we go to the last one
# and if we press down on the last one we go back to the first one .
if number < 0 :
number = len ( self . stations ) - 1
elif number >= len ( self . stations ) :
number = 0
self . selection = number
maxDisplayedItems = self . bodyMaxY - 2
if self . ... |
def clean_up ( self , table , verbose = False ) :
"""Removes exact duplicates , blank records or data without a * source _ id * from the specified * * table * * .
Then finds possible duplicates and prompts for conflict resolution .
Parameters
table : str
The name of the table to remove duplicates , blanks ,... | # Get the table info and all the records
metadata = self . query ( "PRAGMA table_info({})" . format ( table ) , fmt = 'table' )
columns , types , required = [ np . array ( metadata [ n ] ) for n in [ 'name' , 'type' , 'notnull' ] ]
# records = self . query ( " SELECT * FROM { } " . format ( table ) , fmt = ' table ' , ... |
def as_unicode ( s , encoding = 'utf-8' ) :
"""Force conversion of given string to unicode type .
Unicode is ` ` str ` ` type for Python 3 . x and ` ` unicode ` ` for Python 2 . x .
If the string is already in unicode , then no conversion is done and the same string is returned .
Parameters
s : str or bytes... | if isinstance ( s , six . text_type ) :
return s
elif isinstance ( s , six . binary_type ) :
return s . decode ( encoding )
else :
raise ValueError ( 'Can only convert types {0} and {1}' . format ( six . text_type , six . binary_type ) ) |
def attr_item_call_auto_cache ( func ) :
"""Decorator for a a single positional argument function to cache
its results and to make ` ` f ( " a " ) = = f [ " a " ] = = f . a ` ` .""" | def __missing__ ( self , key ) :
result = self [ key ] = func ( key )
return result
wrapper = type ( snake2ucamel ( func . __name__ ) , ( dict , ) , { "__missing__" : __missing__ , "__call__" : dict . __getitem__ , "__getattr__" : dict . __getitem__ , "__doc__" : func . __doc__ , # Class docstring can ' t be up... |
def get_exttype ( self , num = False ) :
"""Get the extension type
By default the result is a string that mirrors
the enumerated type names in cfitsio
' IMAGE _ HDU ' , ' ASCII _ TBL ' , ' BINARY _ TBL '
which have numeric values
0 1 2
send num = True to get the numbers . The values
fitsio . IMAGE _ H... | if num :
return self . _info [ 'hdutype' ]
else :
name = _hdu_type_map [ self . _info [ 'hdutype' ] ]
return name |
def set ( self , key , value ) :
"""Sets the value for a specific requirement .
: param key : Name of requirement to be set
: param value : Value to set for requirement key
: return : Nothing , modifies requirement""" | if key == "tags" :
self . _set_tag ( tags = value )
else :
if isinstance ( value , dict ) and key in self . _requirements and isinstance ( self . _requirements [ key ] , dict ) :
self . _requirements [ key ] = merge ( self . _requirements [ key ] , value )
else :
self . _requirements [ key ]... |
def seq_str ( self ) :
"""str : Get the sequence formatted as a string""" | if not self . seq :
return None
return ssbio . protein . sequence . utils . cast_to_str ( self . seq ) |
def col_to_cat ( df , col_name , dest = False ) :
"""Coerces a column in a DataFrame to categorical
Parameters :
df - DataFrame
DataFrame to operate on
col _ name - string
Name of column to coerce
dest - bool , default False
Whether to apply the result to the DataFrame or return it .
True is apply ,... | new_col = df [ col_name ] . astype ( 'category' )
if dest :
set_col ( df , col_name , new_col )
else :
return new_col |
def get_highest_version ( versions ) :
"""Returns highest available version for a package in a list of versions
Uses pkg _ resources to parse the versions
@ param versions : List of PyPI package versions
@ type versions : List of strings
@ returns : string of a PyPI package version""" | sorted_versions = [ ]
for ver in versions :
sorted_versions . append ( ( pkg_resources . parse_version ( ver ) , ver ) )
sorted_versions = sorted ( sorted_versions )
sorted_versions . reverse ( )
return sorted_versions [ 0 ] [ 1 ] |
def getAddPerson ( self ) :
"""Return an L { AddPersonFragment } which is a child of this fragment and
which will add a person to C { self . organizer } .""" | fragment = AddPersonFragment ( self . organizer )
fragment . setFragmentParent ( self )
return fragment |
def dump_table_as_insert_sql ( engine : Engine , table_name : str , fileobj : TextIO , wheredict : Dict [ str , Any ] = None , include_ddl : bool = False , multirow : bool = False ) -> None :
"""Reads a table from the database , and writes SQL to replicate the table ' s
data to the output ` ` fileobj ` ` .
Args... | # http : / / stackoverflow . com / questions / 5631078 / sqlalchemy - print - the - actual - query # noqa
# http : / / docs . sqlalchemy . org / en / latest / faq / sqlexpressions . html
# http : / / www . tylerlesmann . com / 2009 / apr / 27 / copying - databases - across - platforms - sqlalchemy / # noqa
# https : / ... |
def _is_proper_sequence ( seq ) :
"""Returns is seq is sequence and not string .""" | return ( isinstance ( seq , collections . abc . Sequence ) and not isinstance ( seq , str ) ) |
def monkey_patch_override_instance_method ( instance ) :
"""Override an instance method with a new version of the same name . The
original method implementation is made available within the override method
as ` _ original _ < METHOD _ NAME > ` .""" | def perform_override ( override_fn ) :
fn_name = override_fn . __name__
original_fn_name = '_original_' + fn_name
# Override instance method , if it hasn ' t already been done
if not hasattr ( instance , original_fn_name ) :
original_fn = getattr ( instance , fn_name )
setattr ( instance... |
def quote_selection ( self ) :
"""Quotes selected cells , marks content as changed""" | selection = self . get_selection ( )
current_table = self . grid . current_table
for row , col , tab in self . grid . code_array . dict_grid . keys ( ) :
if tab == current_table and ( row , col ) in selection :
self . grid . actions . quote_code ( ( row , col , tab ) )
self . grid . code_array . result_cach... |
def boxcox ( X ) :
"""Gaussianize X using the Box - Cox transformation : [ samples x phenotypes ]
- each phentoype is brought to a positive schale , by first subtracting the minimum value and adding 1.
- Then each phenotype transformed by the boxcox transformation""" | X_transformed = sp . zeros_like ( X )
maxlog = sp . zeros ( X . shape [ 1 ] )
for i in range ( X . shape [ 1 ] ) :
i_nan = sp . isnan ( X [ : , i ] )
values = X [ ~ i_nan , i ]
X_transformed [ i_nan , i ] = X [ i_nan , i ]
X_transformed [ ~ i_nan , i ] , maxlog [ i ] = st . boxcox ( values - values . mi... |
def base26 ( x , _alphabet = string . ascii_uppercase ) :
"""Return positive ` ` int ` ` ` ` x ` ` as string in bijective base26 notation .
> > > [ base26 ( i ) for i in [ 0 , 1 , 2 , 26 , 27 , 28 , 702 , 703 , 704 ] ]
[ ' ' , ' A ' , ' B ' , ' Z ' , ' AA ' , ' AB ' , ' ZZ ' , ' AAA ' , ' AAB ' ]
> > > base26... | result = [ ]
while x :
x , digit = divmod ( x , 26 )
if not digit :
x -= 1
digit = 26
result . append ( _alphabet [ digit - 1 ] )
return '' . join ( result [ : : - 1 ] ) |
def fire_event ( self , evt_name , * args , ** kwargs ) :
"""触发事件
: params evt _ name : 事件名称
: params args : 给事件接受者的参数
: params kwargs : 给事件接受者的参数""" | listeners = self . __get_listeners ( evt_name )
evt = self . generate_event ( evt_name )
for listener in listeners :
listener ( evt , * args , ** kwargs ) |
def get_role_mapping ( self , name = None , params = None ) :
"""` < https : / / www . elastic . co / guide / en / elasticsearch / reference / current / security - api - get - role - mapping . html > ` _
: arg name : Role - Mapping name""" | return self . transport . perform_request ( "GET" , _make_path ( "_security" , "role_mapping" , name ) , params = params ) |
def COOKIES ( self ) :
"""Cookies parsed into a dictionary . Signed cookies are NOT decoded
automatically . See : meth : ` get _ cookie ` for details .""" | raw_dict = SimpleCookie ( self . headers . get ( 'Cookie' , '' ) )
cookies = { }
for cookie in six . itervalues ( raw_dict ) :
cookies [ cookie . key ] = cookie . value
return cookies |
def plot_tree ( ax , tree , plane = 'xy' , diameter_scale = _DIAMETER_SCALE , linewidth = _LINEWIDTH , color = None , alpha = _ALPHA ) :
'''Plots a 2d figure of the tree ' s segments
Args :
ax ( matplotlib axes ) : on what to plot
tree ( neurom . core . Tree or neurom . core . Neurite ) : plotted tree
plane... | plane0 , plane1 = _plane2col ( plane )
segs = [ ( ( s [ 0 ] [ plane0 ] , s [ 0 ] [ plane1 ] ) , ( s [ 1 ] [ plane0 ] , s [ 1 ] [ plane1 ] ) ) for s in iter_segments ( tree ) ]
linewidth = _get_linewidth ( tree , diameter_scale = diameter_scale , linewidth = linewidth )
color = _get_color ( color , tree . type )
collect... |
def ReadSerializableArray ( self , class_name , max = sys . maxsize ) :
"""Deserialize a stream into the object specific by ` class _ name ` .
Args :
class _ name ( str ) : a full path to the class to be deserialized into . e . g . ' neo . Core . Block . Block '
max ( int ) : ( Optional ) maximum number of by... | module = '.' . join ( class_name . split ( '.' ) [ : - 1 ] )
klassname = class_name . split ( '.' ) [ - 1 ]
klass = getattr ( importlib . import_module ( module ) , klassname )
length = self . ReadVarInt ( max = max )
items = [ ]
# logger . info ( " READING ITEM % s % s " % ( length , class _ name ) )
try :
for i i... |
def min ( self ) :
"""Return the minimum of ` ` self ` ` .
See Also
numpy . amin
max""" | results = [ x . ufuncs . min ( ) for x in self . elem ]
return np . min ( results ) |
def sort_values ( self , by = None , axis = 0 , ascending = True , inplace = False , kind = 'quicksort' , na_position = 'last' ) :
"""Sort by the values along either axis .
Parameters
- - - - - % ( optional _ by ) s
axis : % ( axes _ single _ arg ) s , default 0
Axis to be sorted .
ascending : bool or lis... | raise NotImplementedError ( "sort_values has not been implemented " "on Panel or Panel4D objects." ) |
def cdf ( data , mode = 'continuous' , ** kwargs ) :
'''Return cumulative density .
: arguments :
* * data * * ( ` ` < numpy . ndarray > ` ` )
Input data , to plot the distribution for .
: returns :
* * P * * ( ` ` < numpy . ndarray > ` ` )
Cumulative probability .
* * x * * ( ` ` < numpy . ndarray > ... | return ( np . linspace ( 0.0 , 1.0 , len ( data ) ) , np . sort ( data ) ) |
def parse_osm_node ( response ) :
"""Parse points from OSM nodes .
Parameters
response : JSON
Nodes from OSM response .
Returns
Dict of vertex IDs and their lat , lon coordinates .""" | try :
point = Point ( response [ 'lon' ] , response [ 'lat' ] )
poi = { 'osmid' : response [ 'id' ] , 'geometry' : point }
if 'tags' in response :
for tag in response [ 'tags' ] :
poi [ tag ] = response [ 'tags' ] [ tag ]
except Exception :
log ( 'Point has invalid geometry: {}' . fo... |
def _attach_dummy_intf_rtr ( self , tenant_id , tenant_name , rtr_id ) :
"""Function to create a dummy router and interface .""" | serv_obj = self . get_service_obj ( tenant_id )
fw_dict = serv_obj . get_fw_dict ( )
fw_id = fw_dict . get ( 'fw_id' )
rtr_nwk = fw_id [ 0 : 4 ] + fw_const . DUMMY_SERVICE_NWK + ( fw_id [ len ( fw_id ) - 4 : ] )
net_id , subnet_id = self . os_helper . create_network ( rtr_nwk , tenant_id , self . servicedummy_ip_subnet... |
def show ( cls , report_name , data ) :
"""Shows a report by issuing a GET request to the / reports / report _ name
endpoint .
Args :
` report _ name ` : the name of the report to show
` data ` : the parameters for the report""" | conn = Qubole . agent ( )
return conn . get ( cls . element_path ( report_name ) , data ) |
def preprocess ( self , nb_man , resources , km = None ) :
"""Wraps the parent class process call slightly""" | with self . setup_preprocessor ( nb_man . nb , resources , km = km ) :
if self . log_output :
self . log . info ( "Executing notebook with kernel: {}" . format ( self . kernel_name ) )
nb , resources = self . papermill_process ( nb_man , resources )
info_msg = self . _wait_for_reply ( self . kc . ke... |
def get_placeholder_data ( self , request , obj ) :
"""Get placeholder data from layout .""" | if not obj or not getattr ( obj , 'layout' , None ) :
data = [ PlaceholderData ( slot = 'main' , role = 'm' , title = 'Main' ) ]
else :
data = obj . layout . get_placeholder_data ( )
return data |
def _config2von ( self , config : dict , access : str = None ) -> dict :
"""Given a configuration dict with indy and possibly more configuration values , return the
corresponding VON wallet configuration dict from current default and input values .
: param config : input configuration
: param access : access ... | rv = { k : config . get ( k , self . _defaults [ k ] ) for k in ( 'auto_create' , 'auto_remove' ) }
rv [ 'access' ] = access or self . default_access
for key in ( 'seed' , 'did' , 'link_secret_label' ) :
if key in config :
rv [ key ] = config [ key ]
return rv |
def _logmessage_transform ( cls , s , by = 2 ) :
"""Preprocess / cleanup a bzr log message before parsing
Args :
s ( str ) : log message string
by ( int ) : cutoff threshold for log message length
Returns :
str : preprocessed log message string""" | if len ( s ) >= by :
return s [ by : ] . strip ( '\n' )
return s . strip ( '\n' ) |
async def _wait ( self ) :
'''Wait on the other editatoms who are constructing nodes my new nodes refer to''' | for buid in self . otherbldgbuids :
nodeevnt = self . allbldgbuids . get ( buid )
if nodeevnt is None :
continue
await nodeevnt [ 1 ] . wait ( ) |
def word_frame_pos ( self , _id ) :
"""Get the position of words""" | left = int ( self . words [ _id ] [ 0 ] / 1000 )
right = max ( left + 1 , int ( self . words [ _id ] [ 1 ] / 1000 ) )
return ( left , right ) |
def _index ( self , row ) :
"""Add a row to the internal list of rows without writing it to disk .
This function should keep the data structure consistent so it ' s usable
for both adding new rows , and loading pre - existing histories .""" | self . rows . append ( row )
self . _keys . update ( row . keys ( ) )
self . _steps += 1 |
def get_config_file ( ) :
"""Return the loaded config file if one exists .""" | # config will be created here if we can ' t find one
new_config_path = os . path . expanduser ( '~/.dagobahd.yml' )
config_dirs = [ '/etc' , os . path . expanduser ( '~' ) ]
config_filenames = [ 'dagobahd.yml' , 'dagobahd.yaml' , '.dagobahd.yml' , '.dagobahd.yaml' ]
for directory in config_dirs :
for filename in co... |
def add_type ( self , new_name , orig_names ) :
"""Record the typedefd name for orig _ names . Resolve orig _ names
to their core names and save those .
: new _ name : TODO
: orig _ names : TODO
: returns : TODO""" | self . _dlog ( "adding a type '{}'" . format ( new_name ) )
# TODO do we allow clobbering of types ? ? ?
res = copy . copy ( orig_names )
resolved_names = self . _resolve_name ( res [ - 1 ] )
if resolved_names is not None :
res . pop ( )
res += resolved_names
self . _curr_scope [ "types" ] [ new_name ] = res |
def delete ( self , request , ** resources ) :
"""Default DELETE method . Allow bulk delete .
: return django . http . response : empty response""" | resource = resources . get ( self . _meta . name )
if not resource :
raise HttpError ( "Bad request" , status = status . HTTP_404_NOT_FOUND )
for o in as_tuple ( resource ) :
o . delete ( )
return HttpResponse ( "" ) |
def _capitalize_word ( text , pos ) :
"""Capitalize the current ( or following ) word .""" | while pos < len ( text ) and not text [ pos ] . isalnum ( ) :
pos += 1
if pos < len ( text ) :
text = text [ : pos ] + text [ pos ] . upper ( ) + text [ pos + 1 : ]
while pos < len ( text ) and text [ pos ] . isalnum ( ) :
pos += 1
return text , pos |
def generate ( self , number = None ) :
"""Generates vectors from the Gaussian .
@ param number : optional , if given , generates more than one vector .
@ returns : generated vector ( s ) , either as a one dimensional array
( shape ( d , ) ) if number is not set , or as a two dimensional array
( shape ( n ,... | if number is None :
return numpy . random . multivariate_normal ( self . mu , self . sigma )
else :
return numpy . random . multivariate_normal ( self . mu , self . sigma , number ) |
def all_input ( self ) :
"""Returns all input files as a dict of { filename : feffio object }""" | d = { "HEADER" : self . header ( ) , "PARAMETERS" : self . tags }
if "RECIPROCAL" not in self . tags :
d . update ( { "POTENTIALS" : self . potential , "ATOMS" : self . atoms } )
return d |
def dump ( self , dest_pattern = "{id}.jpg" , override = True , max_size = None , bits = 8 , contrast = None , gamma = None , colormap = None , inverse = None ) :
"""Download the image with optional image modifications .
Parameters
dest _ pattern : str , optional
Destination path for the downloaded image . " ... | if self . id is None :
raise ValueError ( "Cannot dump an annotation with no ID." )
pattern = re . compile ( "{(.*?)}" )
dest_pattern = re . sub ( pattern , lambda m : str ( getattr ( self , str ( m . group ( 0 ) ) [ 1 : - 1 ] , "_" ) ) , dest_pattern )
destination = os . path . dirname ( dest_pattern )
filename , ... |
def validate_statusline ( self , valid_statusline ) :
"""Check that the statusline is valid , eg . starts with a numeric
code . If not , replace with passed in valid _ statusline""" | code = self . get_statuscode ( )
try :
code = int ( code )
assert ( code > 0 )
return True
except ( ValueError , AssertionError ) :
self . statusline = valid_statusline
return False |
def hotp ( key , counter , format = 'dec6' , hash = hashlib . sha1 ) :
'''Compute a HOTP value as prescribed by RFC4226
: param key :
the HOTP secret key given as an hexadecimal string
: param counter :
the OTP generation counter
: param format :
the output format , can be :
- hex , for a variable len... | bin_hotp = __hotp ( key , counter , hash )
if format == 'dec4' :
return dec ( bin_hotp , 4 )
elif format == 'dec6' :
return dec ( bin_hotp , 6 )
elif format == 'dec7' :
return dec ( bin_hotp , 7 )
elif format == 'dec8' :
return dec ( bin_hotp , 8 )
elif format == 'hex' :
return '%x' % truncated_valu... |
def is_broker_action_done ( action , rid = None , unit = None ) :
"""Check whether broker action has completed yet .
@ param action : name of action to be performed
@ returns True if action complete otherwise False""" | rdata = relation_get ( rid , unit ) or { }
broker_rsp = rdata . get ( get_broker_rsp_key ( ) )
if not broker_rsp :
return False
rsp = CephBrokerRsp ( broker_rsp )
unit_name = local_unit ( ) . partition ( '/' ) [ 2 ]
key = "unit_{}_ceph_broker_action.{}" . format ( unit_name , action )
kvstore = kv ( )
val = kvstore... |
def get_random_giphy ( phrase ) :
"""Return the URL of a random GIF related to the phrase , if possible""" | with warnings . catch_warnings ( ) :
warnings . simplefilter ( 'ignore' )
giphy = giphypop . Giphy ( )
results = giphy . search_list ( phrase = phrase , limit = 100 )
if not results :
raise ValueError ( 'There were no results for that phrase' )
return random . choice ( results ) . media_url |
def remove_custom_field_setting ( self , project , params = { } , ** options ) :
"""Remove a custom field setting on the project .
Parameters
project : { Id } The project to associate the custom field with
[ data ] : { Object } Data for the request
- [ custom _ field ] : { Id } The id of the custom field to... | path = "/projects/%s/removeCustomFieldSetting" % ( project )
return self . client . post ( path , params , ** options ) |
def blend ( self , cycles = 1 ) :
"""Explands the existing Palette by inserting the blending colour
between all Colours already in the Palette .
Changes the Palette in - place .
args :
cycles ( int ) : number of * blend * cycles to apply . ( Default is 1)
Example usage :
. . code - block : : python
p1... | for j in range ( int ( cycles ) ) :
new_colours = [ ]
for i , c in enumerate ( self . _colours ) :
if i != 0 :
c2 = blend ( c , self . _colours [ i - 1 ] )
new_colours . append ( c2 )
new_colours . append ( c )
self . _colours = new_colours |
def add_permission_by_name ( self , code , save = False ) :
"""Adds a permission with given name .
Args :
code ( str ) : Code name of the permission .
save ( bool ) : If False , does nothing .""" | if not save :
return [ "%s | %s" % ( p . name , p . code ) for p in Permission . objects . filter ( code__contains = code ) ]
for p in Permission . objects . filter ( code__contains = code ) :
if p not in self . Permissions :
self . Permissions ( permission = p )
if p :
self . save ( ) |
def rebuildDay ( self , opt ) :
"""Rebuilds the scale for the day mode .
: param opt | < XGanttRenderOptions >""" | self . _labels = [ ]
self . _hlines = [ ]
self . _vlines = [ ]
self . _weekendRects = [ ]
self . _alternateRects = [ ]
self . _topLabels = [ ]
top_format = 'dddd MMMM dd'
label_format = 'ha'
increment = 60
# hour
# generate vertical lines
x = 0
i = 0
half = opt . header_height / 2.0
curr = QDateTime ( opt . start , QTi... |
def headgrid ( self , xg , yg , layers = None , printrow = False ) :
"""Grid of heads
Parameters
xg : array
x values of grid
yg : array
y values of grid
layers : integer , list or array , optional
layers for which grid is returned
printrow : boolean , optional
prints dot to screen for each row of ... | nx , ny = len ( xg ) , len ( yg )
if layers is None :
Nlayers = self . aq . find_aquifer_data ( xg [ 0 ] , yg [ 0 ] ) . naq
else :
Nlayers = len ( np . atleast_1d ( layers ) )
h = np . empty ( ( Nlayers , ny , nx ) )
for j in range ( ny ) :
if printrow :
print ( '.' , end = '' , flush = True )
f... |
def bgsize_rankspernode ( self ) :
"""Return ( bg _ size , ranks _ per _ node ) from mpi _ procs and omp _ threads .""" | bg_size = int ( math . ceil ( ( self . mpi_procs * self . omp_threads ) / self . hw . cores_per_node ) )
bg_size = max ( bg_size , 32 )
# TODO hardcoded
ranks_per_node = int ( math . ceil ( self . mpi_procs / bg_size ) )
return bg_size , ranks_per_node |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.