signature
stringlengths
29
44.1k
implementation
stringlengths
0
85.2k
def _duplicateLayer ( self , layerName , newLayerName ) : """This is the environment implementation of : meth : ` BaseFont . duplicateLayer ` . * * layerName * * will be a : ref : ` type - string ` representing a valid layer name . The value will have been normalized with : func : ` normalizers . normalizeLayer...
newLayer = self . getLayer ( layerName ) . copy ( ) return self . insertLayer ( newLayer , newLayerName )
def parse_yaml ( self , y ) : '''Parse a YAML specification of a location into this object .'''
self . x = int ( y [ 'x' ] ) self . y = int ( y [ 'y' ] ) self . height = int ( y [ 'height' ] ) self . width = int ( y [ 'width' ] ) self . direction = dir . from_string ( y [ 'direction' ] ) return self
def getPrecision ( self , result = None ) : """Returns the precision for the Analysis . - If ManualUncertainty is set , calculates the precision of the result in accordance with the manual uncertainty set . - If Calculate Precision from Uncertainty is set in Analysis Service , calculates the precision in ac...
allow_manual = self . getAllowManualUncertainty ( ) precision_unc = self . getPrecisionFromUncertainty ( ) if allow_manual or precision_unc : uncertainty = self . getUncertainty ( result ) if uncertainty is None : return self . getField ( 'Precision' ) . get ( self ) if uncertainty == 0 and result i...
def get_analysis ( self , current_class ) : """Returns the : class : ` ~ androguard . core . analysis . analysis . Analysis ` object which contains the ` current _ class ` . : param current _ class : The class to search for : type current _ class : androguard . core . bytecodes . dvm . ClassDefItem : rtype ...
for digest in self . analyzed_vms : dx = self . analyzed_vms [ digest ] if dx . is_class_present ( current_class . get_name ( ) ) : return dx return None
def do_copy_file ( self , args ) : """Determine if file should be copied or not and handle exceptions ."""
path , prefixed_path , source_storage = args reset_connection ( self . storage ) if self . collectfast_enabled and not self . dry_run : try : if not should_copy_file ( self . storage , path , prefixed_path , source_storage ) : return False except Exception as e : if settings . debug ...
def add_wf ( self , wf_obj ) : """Adds a token to the text layer @ type wf _ obj : L { Cwf } @ param wf _ obj : the token object"""
if self . text_layer is None : self . text_layer = Ctext ( type = self . type ) self . root . append ( self . text_layer . get_node ( ) ) self . text_layer . add_wf ( wf_obj )
def assign_from_subscribed ( self , assignments ) : """Update the assignment to the specified partitions This method is called by the coordinator to dynamically assign partitions based on the consumer ' s topic subscription . This is different from assign _ from _ user ( ) which directly sets the assignment f...
if not self . partitions_auto_assigned ( ) : raise IllegalStateError ( self . _SUBSCRIPTION_EXCEPTION_MESSAGE ) for tp in assignments : if tp . topic not in self . subscription : raise ValueError ( "Assigned partition %s for non-subscribed topic." % ( tp , ) ) # after rebalancing , we always reinitializ...
def _assert_path_is_dir ( self , dir_path ) : """Args : dir _ path :"""
if not os . path . isdir ( dir_path ) : raise django . core . management . base . CommandError ( 'Invalid dir path. path="{}"' . format ( dir_path ) )
def calmar_ratio ( returns , period = DAILY , annualization = None ) : """Determines the Calmar ratio , or drawdown ratio , of a strategy . Parameters returns : pd . Series or np . ndarray Daily returns of the strategy , noncumulative . - See full explanation in : func : ` ~ empyrical . stats . cum _ return...
max_dd = max_drawdown ( returns = returns ) if max_dd < 0 : temp = annual_return ( returns = returns , period = period , annualization = annualization ) / abs ( max_dd ) else : return np . nan if np . isinf ( temp ) : return np . nan return temp
def db_user_exists ( username ) : """Return True if the DB user already exists ."""
qry = u"""SELECT COUNT(*) FROM pg_roles where rolname = \'{username}\';""" output = StringIO ( ) excute_query ( qry . format ( username = username ) , flags = "-Aqt" , use_sudo = True , stdout = output ) # FIXME : is there a way to get fabric to not clutter the output # with " [ 127.0.0.1 ] out : " on each line ? lines...
def binary_size ( self ) : '''Return the number of bytes to store this group and its parameters .'''
return ( 1 + # group _ id 1 + len ( self . name . encode ( 'utf-8' ) ) + # size of name and name bytes 2 + # next offset marker 1 + len ( self . desc . encode ( 'utf-8' ) ) + # size of desc and desc bytes sum ( p . binary_size ( ) for p in self . params . values ( ) ) )
def parse_seq ( tokens , options ) : """seq : : = ( atom [ ' . . . ' ] ) * ;"""
result = [ ] while tokens . current ( ) not in [ None , ']' , ')' , '|' ] : atom = parse_atom ( tokens , options ) if tokens . current ( ) == '...' : atom = [ OneOrMore ( * atom ) ] tokens . move ( ) result += atom return result
def fader ( self , value : int ) : """Move the fader to a new position in the range 0 to 1023."""
self . _fader = int ( value ) if 0 < value < 1024 else 0 self . outport . send ( mido . Message ( 'control_change' , control = 0 , value = self . _fader >> 7 ) ) self . outport . send ( mido . Message ( 'control_change' , control = 32 , value = self . _fader & 0x7F ) )
def _create_dns_list ( self , dns ) : """: param dns : : return :"""
if not dns : return None dns_list = [ ] if isinstance ( dns , six . string_types ) : if is_valid_ip ( dns ) : dns_list . append ( dns ) else : raise ValueError ( "dns is required to be a valid ip adress. {0} was passed." . format ( dns ) ) elif isinstance ( dns , list ) : for dns_entry i...
def workdays ( first_day = None ) : """Returns a list of workday names . Arguments first _ day : str , default None The first day of the five - day work week . If not given , ' Monday ' is used . Returns list A list of workday names ."""
if first_day is None : first_day = 'Monday' ix = _lower_weekdays ( ) . index ( first_day . lower ( ) ) return _double_weekdays ( ) [ ix : ix + 5 ]
def process_content ( self , content , filename = None , content_type = None ) : """Standard implementation of : meth : ` . DepotFileInfo . process _ content ` This is the standard depot implementation of files upload , it will store the file on the default depot and will provide the standard attributes . S...
file_path , file_id = self . store_content ( content , filename , content_type ) self [ 'file_id' ] = file_id self [ 'path' ] = file_path saved_file = self . file self [ 'filename' ] = saved_file . filename self [ 'content_type' ] = saved_file . content_type self [ 'uploaded_at' ] = saved_file . last_modified . strftim...
def print_model ( self , include_unsigned_edges = False ) : """Return a SIF string of the assembled model . Parameters include _ unsigned _ edges : bool If True , includes edges with an unknown activating / inactivating relationship ( e . g . , most PTMs ) . Default is False ."""
sif_str = '' for edge in self . graph . edges ( data = True ) : n1 = edge [ 0 ] n2 = edge [ 1 ] data = edge [ 2 ] polarity = data . get ( 'polarity' ) if polarity == 'negative' : rel = '-1' elif polarity == 'positive' : rel = '1' elif include_unsigned_edges : rel = '0...
def _compute_callables_count ( self , iters : Dict [ str , Any ] ) : """Iterate over all issues and count the number of times each callable is seen ."""
count = dict . fromkeys ( [ issue [ "callable" ] for issue in iters [ "issues" ] ] , 0 ) for issue in iters [ "issues" ] : count [ issue [ "callable" ] ] += 1 return count
def pose_to_list ( pose ) : """Convert a Pose or PoseStamped in Python list ( ( position ) , ( quaternion ) ) : param pose : geometry _ msgs . msg . PoseStamped or geometry _ msgs . msg . Pose : return : the equivalent in list ( ( position ) , ( quaternion ) )"""
if type ( pose ) == geometry_msgs . msg . PoseStamped : return [ [ pose . pose . position . x , pose . pose . position . y , pose . pose . position . z ] , [ pose . pose . orientation . x , pose . pose . orientation . y , pose . pose . orientation . z , pose . pose . orientation . w ] ] elif type ( pose ) == geomet...
def canonicalize ( message ) : """Function to convert an email Message to standard format string : param message : email . Message to be converted to standard string : return : the standard representation of the email message in bytes"""
if message . is_multipart ( ) or message . get ( 'Content-Transfer-Encoding' ) != 'binary' : return mime_to_bytes ( message , 0 ) . replace ( b'\r\n' , b'\n' ) . replace ( b'\r' , b'\n' ) . replace ( b'\n' , b'\r\n' ) else : message_header = '' message_body = message . get_payload ( decode = True ) for ...
def read_requirements_file ( path ) : """reads requirements . txt file and handles PyPI index URLs : param path : ( str ) path to requirements . txt file : return : ( tuple of lists )"""
last_pypi_url = None with open ( path ) as f : requires = [ ] pypi_urls = [ ] for line in f . readlines ( ) : if not line : continue if '--' in line : match = re . match ( r'--index-url\s+([\w\d:/.-]+)\s' , line ) if match : last_pypi_url =...
def file_rights ( filepath , mode = None , uid = None , gid = None ) : '''Change file rights'''
file_handle = os . open ( filepath , os . O_RDONLY ) if mode : os . fchmod ( file_handle , mode ) if uid : if not gid : gid = 0 os . fchown ( file_handle , uid , gid ) os . close ( file_handle )
def linestrings_intersect ( line1 , line2 ) : """To valid whether linestrings from geojson are intersected with each other . reference : http : / / www . kevlindev . com / gui / math / intersection / Intersection . js Keyword arguments : line1 - - first line geojson object line2 - - second line geojson obje...
intersects = [ ] for i in range ( 0 , len ( line1 [ 'coordinates' ] ) - 1 ) : for j in range ( 0 , len ( line2 [ 'coordinates' ] ) - 1 ) : a1_x = line1 [ 'coordinates' ] [ i ] [ 1 ] a1_y = line1 [ 'coordinates' ] [ i ] [ 0 ] a2_x = line1 [ 'coordinates' ] [ i + 1 ] [ 1 ] a2_y = line1...
def save ( self ) : """Save the row back to the spreadsheet"""
if self . _sheet . readonly : raise ReadOnlyException if not self . _changed : # nothing to save return gd_client = self . _sheet . client assert gd_client is not None try : entry = gd_client . UpdateRow ( self . _entry , self . _data ) except RequestError as e : error_data = e . args [ 0 ] if error...
def load_data_and_build ( self , filename , delimiter = "," ) : """Convenience function for directly working with a data file . This opens a file and reads the data into an array , sets the data as an nparray and list of dimnames @ In , filename , string representing the data file"""
data = np . genfromtxt ( filename , dtype = float , delimiter = delimiter , names = True ) data = data . view ( np . float64 ) . reshape ( data . shape + ( - 1 , ) ) X = data [ : , 0 : - 1 ] Y = data [ : , - 1 ] self . build ( X = X , Y = Y )
def returner ( ret ) : '''Send an PushOver message with the data'''
_options = _get_options ( ret ) user = _options . get ( 'user' ) device = _options . get ( 'device' ) token = _options . get ( 'token' ) title = _options . get ( 'title' ) priority = _options . get ( 'priority' ) expire = _options . get ( 'expire' ) retry = _options . get ( 'retry' ) sound = _options . get ( 'sound' ) ...
def launch ( ) : """Ensure that python is running from the Lore virtualenv past this point ."""
if launched ( ) : check_version ( ) os . chdir ( ROOT ) return if not os . path . exists ( BIN_LORE ) : missing = ' %s virtualenv is missing.' % APP if '--launched' in sys . argv : sys . exit ( ansi . error ( ) + missing + ' Please check for errors during:\n $ lore install\n' ) else : ...
def decrease_posts_count_after_post_unaproval ( sender , instance , ** kwargs ) : """Decreases the member ' s post count after a post unaproval . This receiver handles the unaproval of a forum post : the posts count associated with the post ' s author is decreased ."""
if not instance . pk : # Do not consider posts being created . return profile , dummy = ForumProfile . objects . get_or_create ( user = instance . poster ) try : old_instance = instance . __class__ . _default_manager . get ( pk = instance . pk ) except ObjectDoesNotExist : # pragma : no cover # This should neve...
def module_broadcast ( m , broadcast_fn , * args , ** kwargs ) : """Call given function in all submodules with given parameters"""
apply_leaf ( m , lambda x : module_apply_broadcast ( x , broadcast_fn , args , kwargs ) )
def build_or_reuse_placeholder ( tensor_spec ) : """Build a tf . placeholder from the metadata in the given tensor spec , or return an existing one . Args : tensor _ spec ( tf . TensorSpec ) : Returns : tf . Tensor :"""
g = tfv1 . get_default_graph ( ) name = tensor_spec . name try : tensor = g . get_tensor_by_name ( name + ':0' ) assert "Placeholder" in tensor . op . type , "Tensor {} exists but is not a placeholder!" . format ( name ) assert tensor_spec . is_compatible_with ( tensor ) , "Tensor {} exists but is not compa...
def set_figure_params ( self , scanpy = True , dpi = 80 , dpi_save = 150 , frameon = True , vector_friendly = True , fontsize = 14 , color_map = None , format = "pdf" , transparent = False , ipython_format = "png2x" , ) : """Set resolution / size , styling and format of figures . Parameters scanpy : ` bool ` , ...
try : import IPython IPython . core . display . set_matplotlib_formats ( ipython_format ) except : pass from matplotlib import rcParams self . _vector_friendly = vector_friendly self . file_format_figs = format if dpi is not None : rcParams [ "figure.dpi" ] = dpi if dpi_save is not None : rcParams [...
def reverse_default ( self , subid , ipaddr , params = None ) : '''/ v1 / server / reverse _ default _ ipv4 POST - account Set a reverse DNS entry for an IPv4 address of a virtual machine to the original setting . Upon success , DNS changes may take 6-12 hours to become active . Link : https : / / www . v...
params = update_params ( params , { 'SUBID' : subid , 'ip' : ipaddr } ) return self . request ( '/v1/server/reverse_default_ipv4' , params , 'POST' )
def unregister_namespace ( self , namespace ) : """Unregister a namespace . : param namespace : Namespace tag . : type namespace : str"""
if namespace not in self . _namespaces : raise NoRegisteredError ( "Namespace '{0}' is not registered on loader." . format ( namespace ) ) del self . _namespaces [ namespace ]
def mtotal_from_tau0_tau3 ( tau0 , tau3 , f_lower , in_seconds = False ) : r"""Returns total mass from : math : ` \ tau _ 0 , \ tau _ 3 ` ."""
mtotal = ( tau3 / _a3 ( f_lower ) ) / ( tau0 / _a0 ( f_lower ) ) if not in_seconds : # convert back to solar mass units mtotal /= lal . MTSUN_SI return mtotal
def add_point_labels ( self , points , labels , italic = False , bold = True , font_size = None , text_color = 'k' , font_family = None , shadow = False , show_points = True , point_color = 'k' , point_size = 5 , name = None ) : """Creates a point actor with one label from list labels assigned to each point . P...
if font_family is None : font_family = rcParams [ 'font' ] [ 'family' ] if font_size is None : font_size = rcParams [ 'font' ] [ 'size' ] if len ( points ) != len ( labels ) : raise Exception ( 'There must be one label for each point' ) vtkpoints = vtki . PolyData ( points ) vtklabels = vtk . vtkStringArray...
def DirectoryStimuliFactory ( loader ) : """Takes an input path to the images folder of an experiment and generates automatically the category - filenumber list needed to construct an appropriate _ categories object . Parameters : loader : Loader object which contains impath : string path to the input ,...
impath = loader . impath ftrpath = loader . ftrpath # checks whether user has reading permission for the path assert os . access ( impath , os . R_OK ) assert os . access ( ftrpath , os . R_OK ) # EXTRACTING IMAGE NAMES img_per_cat = { } # extract only directories in the given folder subfolders = [ name for name in os ...
def PureMultiHeadedAttention ( x , params , num_heads = 8 , dropout = 0.0 , mode = 'train' , ** kwargs ) : """Pure transformer - style multi - headed attention . Args : x : inputs ( ( q , k , v ) , mask ) params : parameters ( none ) num _ heads : int : number of attention heads dropout : float : dropout ...
del params rng = kwargs . get ( 'rng' , None ) ( q , k , v ) , mask = x feature_depth = q . shape [ - 1 ] assert feature_depth % num_heads == 0 head_depth = feature_depth // num_heads nbatch = np . shape ( q ) [ 0 ] # nbatch , seqlen , feature _ depth - - > nbatch , num _ heads , seqlen , head _ depth def SplitHeads ( ...
def t_IDENTIFIER ( t ) : r"[ A - Z _ a - z ] [ 0-9A - Z _ a - z ] *"
if t . value in keywords : t . type = t . value return t
def parse_networks_output ( out ) : """Parses the output of the Docker CLI ' docker network ls ' and returns it in the format similar to the Docker API . : param out : CLI output . : type out : unicode | str : return : Parsed result . : rtype : list [ dict ]"""
if not out : return [ ] line_iter = islice ( out . splitlines ( ) , 1 , None ) # Skip header return list ( map ( _network_info , line_iter ) )
def add_to_current_action ( self , controller ) : """Add a controller to the current action ."""
item = self . current_item self . _history [ self . _index ] = item + ( controller , )
def as_dict ( self ) -> Dict [ str , Any ] : """The names and values of all options . . . versionadded : : 3.1"""
return dict ( ( opt . name , opt . value ( ) ) for name , opt in self . _options . items ( ) )
def connect ( self , peer_address ) : """Client - side UDP connection establishment This method connects this object ' s underlying socket . It subsequently performs a handshake if do _ handshake _ on _ connect was set during initialization . Arguments : peer _ address - address tuple of server peer"""
self . _sock . connect ( peer_address ) peer_address = self . _sock . getpeername ( ) # substituted host addrinfo BIO_dgram_set_connected ( self . _wbio . value , peer_address ) assert self . _wbio is self . _rbio if self . _do_handshake_on_connect : self . do_handshake ( )
def get_file ( self , target_path , host_path , note = None , loglevel = logging . DEBUG ) : """Copy a file from the target machine to the host machine @ param target _ path : path to file in the target @ param host _ path : path to file on the host machine ( e . g . copy test ) @ param note : See send ( ) ...
shutit_global . shutit_global_object . yield_to_draw ( ) self . handle_note ( note ) # Only handle for docker initially , return false in case we care if self . build [ 'delivery' ] != 'docker' : return False # on the host , run : # Usage : docker cp [ OPTIONS ] CONTAINER : PATH LOCALPATH | - # Need : host env , co...
def save ( self , backref_obj = None ) : """Save the cleaned data to the initial object or creating a new one ( if a ` model _ class ` was provided ) ."""
if not self . validated : assert self . is_valid ( ) if self . _model and not self . _obj : obj = self . _save_new_object ( backref_obj ) else : obj = self . save_to ( self . _obj ) for key , subform in self . _forms . items ( ) : data = subform . save ( obj ) if self . _model and not data : ...
def open ( self , baudrate = None , no_reader_thread = False ) : """Opens the device . If the device cannot be opened , an exception is thrown . In that case , open ( ) can be called repeatedly to try and open the connection . : param baudrate : baudrate used for the device . Defaults to the lower - level d...
self . _wire_events ( ) try : self . _device . open ( baudrate = baudrate , no_reader_thread = no_reader_thread ) except : self . _unwire_events raise return self
def _scan_pth_files ( dir_paths ) : """Given an iterable of directory paths , yield paths to all . pth files within ."""
for dir_path in dir_paths : if not os . path . exists ( dir_path ) : continue pth_filenames = ( f for f in os . listdir ( dir_path ) if f . endswith ( '.pth' ) ) for pth_filename in pth_filenames : yield os . path . join ( dir_path , pth_filename )
def _onDecorator ( self , name , line , pos , absPosition ) : "Memorizes a function or a class decorator"
# A class or a function must be on the top of the stack self . objectsStack [ - 1 ] . decorators . append ( Decorator ( name , line , pos , absPosition ) ) return
def get_plan_from_dual ( alpha , beta , C , regul ) : """Retrieve optimal transportation plan from optimal dual potentials . Parameters alpha : array , shape = len ( a ) beta : array , shape = len ( b ) Optimal dual potentials . C : array , shape = len ( a ) x len ( b ) Ground cost matrix . regul : Re...
X = alpha [ : , np . newaxis ] + beta - C return regul . delta_Omega ( X ) [ 1 ]
def show_vcs_output_total_nodes_in_cluster ( self , ** kwargs ) : """Auto Generated Code"""
config = ET . Element ( "config" ) show_vcs = ET . Element ( "show_vcs" ) config = show_vcs output = ET . SubElement ( show_vcs , "output" ) total_nodes_in_cluster = ET . SubElement ( output , "total-nodes-in-cluster" ) total_nodes_in_cluster . text = kwargs . pop ( 'total_nodes_in_cluster' ) callback = kwargs . pop ( ...
def _get_config ( ** kwargs ) : '''Return configuration'''
config = { 'box_type' : 'sealedbox' , 'sk' : None , 'sk_file' : os . path . join ( kwargs [ 'opts' ] . get ( 'pki_dir' ) , 'master/nacl' ) , 'pk' : None , 'pk_file' : os . path . join ( kwargs [ 'opts' ] . get ( 'pki_dir' ) , 'master/nacl.pub' ) , } config_key = '{0}.config' . format ( __virtualname__ ) try : confi...
def encodeStringForB64Header ( s ) : """HTTP Headers can ' t have new lines in them , let ' s"""
if isinstance ( s , six . text_type ) : s = s . encode ( ) return base64 . encodestring ( s ) . strip ( ) . replace ( b'\n' , b'' )
def delete_item ( key ) : """Delete cached file if present"""
CACHED_KEY_FILE = os . path . join ( CURRENT_DIR , key ) if os . path . isfile ( CACHED_KEY_FILE ) : os . remove ( CACHED_KEY_FILE )
def parseExtensionArgs ( self , args , is_openid1 , strict = False ) : """Set the state of this request to be that expressed in these PAPE arguments @ param args : The PAPE arguments without a namespace @ param strict : Whether to raise an exception if the input is out of spec or otherwise malformed . If st...
# preferred _ auth _ policies is a space - separated list of policy URIs self . preferred_auth_policies = [ ] policies_str = args . get ( 'preferred_auth_policies' ) if policies_str : for uri in policies_str . split ( ' ' ) : if uri not in self . preferred_auth_policies : self . preferred_auth_p...
def pink ( N , depth = 80 ) : """N - length vector with ( approximate ) pink noise pink noise has 1 / f PSD"""
a = [ ] s = iterpink ( depth ) for n in range ( N ) : a . append ( next ( s ) ) return a
def get_xmlns_string ( self , ns_uris = None , sort = False , preferred_prefixes_only = True , delim = "\n" ) : """Generates XML namespace declarations for namespaces in this set . It must be suitable for use in an actual XML document , so an exception is raised if this can ' t be done , e . g . if it would h...
if ns_uris is None : ns_uris = self . namespace_uris if sort : ns_uris = sorted ( ns_uris ) have_default = False # Flag for default xmlns entry . xmlns_entries = [ ] # Stores all the xmlns : prefix = uri entries . for ns_uri in ns_uris : ni = self . __lookup_uri ( ns_uri ) if preferred_prefixes_only : ...
def _start_services_on_ads ( ads ) : """Starts long running services on multiple AndroidDevice objects . If any one AndroidDevice object fails to start services , cleans up all existing AndroidDevice objects and their services . Args : ads : A list of AndroidDevice objects whose services to start ."""
running_ads = [ ] for ad in ads : running_ads . append ( ad ) start_logcat = not getattr ( ad , KEY_SKIP_LOGCAT , DEFAULT_VALUE_SKIP_LOGCAT ) try : ad . services . register ( SERVICE_NAME_LOGCAT , logcat . Logcat , start_service = start_logcat ) except Exception : is_required = getattr (...
def run ( self , module , post_check ) : '''Execute the configured source code in a module and run any post checks . Args : module ( Module ) : a module to execute the configured code in . post _ check ( callable ) : a function that can raise an exception if expected post - conditions are not met after co...
try : # Simulate the sys . path behaviour decribed here : # https : / / docs . python . org / 2 / library / sys . html # sys . path _cwd = os . getcwd ( ) _sys_path = list ( sys . path ) _sys_argv = list ( sys . argv ) sys . path . insert ( 0 , os . path . dirname ( self . _path ) ) sys . argv = [ o...
def _update_known_atts ( self , ** kwargs ) : """Updates instance attributes with supplied keyword arguments ."""
for k , v in kwargs . items ( ) : if k not in ATTR_KEYS : # Warn if passed in unknown kwargs raise SyntaxWarning ( 'Unknown argument: {}' . format ( k ) ) elif not v : # Delete attributes with falsey values delattr ( self , k ) else : setattr ( self , k , v )
def save ( file_name , content ) : """Save content to a file"""
with open ( file_name , "w" , encoding = "utf-8" ) as output_file : output_file . write ( content ) return output_file . name
def update_invoice_item ( self , invoice_item_id , invoice_item_dict ) : """Updates an invoice item : param invoice _ item _ id : the invoice item id : param invoice _ item _ dict : dict : return : dict"""
return self . _create_put_request ( resource = INVOICE_ITEMS , billomat_id = invoice_item_id , send_data = invoice_item_dict )
def note_to_chord ( notes ) : """Convert note list to chord list : param list [ str ] notes : list of note arranged from lower note . ex ) [ " C " , " Eb " , " G " ] : rtype : list [ pychord . Chord ] : return : list of chord"""
if not notes : raise ValueError ( "Please specify notes which consist a chord." ) root = notes [ 0 ] root_and_positions = [ ] for rotated_notes in get_all_rotated_notes ( notes ) : rotated_root = rotated_notes [ 0 ] root_and_positions . append ( [ rotated_root , notes_to_positions ( rotated_notes , rotated_...
def _get_image ( self ) : """Get a PIL Image instance of this file . The image is cached to avoid the file needing to be read again if the function is called again ."""
if not hasattr ( self , '_image_cache' ) : from easy_thumbnails . source_generators import pil_image self . image = pil_image ( self ) return self . _image_cache
async def dump_tuple ( self , elem , elem_type , params = None , obj = None ) : """Dumps tuple of elements to the writer . : param elem : : param elem _ type : : param params : : param obj : : return :"""
if len ( elem ) != len ( elem_type . f_specs ( ) ) : raise ValueError ( 'Fixed size tuple has not defined size: %s' % len ( elem_type . f_specs ( ) ) ) elem_fields = params [ 0 ] if params else None if elem_fields is None : elem_fields = elem_type . f_specs ( ) obj = [ ] if obj is None else x . get_elem ( obj )...
def get_device ( self , device_id ) : """Return specified device . Returns a Command ."""
def process_result ( result ) : return Device ( result ) return Command ( 'get' , [ ROOT_DEVICES , device_id ] , process_result = process_result )
def log_relative_gauss ( z ) : """log _ relative _ gauss"""
if z < - 6 : return 1 , - 1.0e12 , - 1 if z > 6 : return 0 , 0 , 1 else : logphi = - 0.5 * ( z * z + l2p ) logPhi = np . log ( .5 * special . erfc ( - z / sq2 ) ) e = np . exp ( logphi - logPhi ) return e , logPhi , 0
def spawn ( cls , argv , cwd = None , env = None , dimensions = ( 24 , 80 ) ) : """Start the given command in a child process in a pseudo terminal . This does all the setting up the pty , and returns an instance of PtyProcess . Dimensions of the psuedoterminal used for the subprocess can be specified as a t...
if isinstance ( argv , str ) : argv = shlex . split ( argv , posix = False ) if not isinstance ( argv , ( list , tuple ) ) : raise TypeError ( "Expected a list or tuple for argv, got %r" % argv ) # Shallow copy of argv so we can modify it argv = argv [ : ] command = argv [ 0 ] env = env or os . environ path = e...
def get_rule_option ( self , rule_name_or_id , option_name ) : """Returns the value of a given option for a given rule . LintConfigErrors will be raised if the rule or option don ' t exist ."""
option = self . _get_option ( rule_name_or_id , option_name ) return option . value
def to_lower ( self , string ) : "Helper function to transform strings to lower case"
value = None try : value = string . lower ( ) except AttributeError : value = "" finally : return value
def save ( self , * args , ** kwargs ) : """Set the description field on save ."""
if self . gen_description : self . description = strip_tags ( self . description_from_content ( ) ) super ( MetaData , self ) . save ( * args , ** kwargs )
def frames_iter ( socket , tty ) : """Return a generator of frames read from socket . A frame is a tuple where the first item is the stream number and the second item is a chunk of data . If the tty setting is enabled , the streams are multiplexed into the stdout stream ."""
if tty : return ( ( STDOUT , frame ) for frame in frames_iter_tty ( socket ) ) else : return frames_iter_no_tty ( socket )
def _call ( callable_obj , arg_names , namespace ) : """Actually calls the callable with the namespace parsed from the command line . Args : callable _ obj : a callable object arg _ names : name of the function arguments namespace : the namespace object parsed from the command line"""
arguments = { arg_name : getattr ( namespace , arg_name ) for arg_name in arg_names } return callable_obj ( ** arguments )
def format_string ( string , context ) : """String - template format a string : > > > format _ string ( ' $ foo and $ { foo } s ' , dict ( foo = 42 ) ) '42 and 42s ' This does not do any attribute lookup etc . For more advanced string formattings have a look at the ` werkzeug . template ` module . : param...
def lookup_arg ( match ) : x = context [ match . group ( 1 ) or match . group ( 2 ) ] if not isinstance ( x , string_types ) : x = type ( string ) ( x ) return x return _format_re . sub ( lookup_arg , string )
def list ( self , request , * args , ** kwargs ) : """Filter services by type It is possible to filter services by their types . Example : / api / services / ? service _ type = DigitalOcean & service _ type = OpenStack"""
return super ( ServicesViewSet , self ) . list ( request , * args , ** kwargs )
def __delete_all_bgedges_between_two_vertices ( self , vertex1 , vertex2 ) : """Deletes all edges between two supplied vertices : param vertex1 : a first out of two vertices edges between which are to be deleted : type vertex1 : any python hashable object . : class : ` bg . vertex . BGVertex ` is expected : p...
edges_to_be_deleted_with_keys = [ ( key , data ) for v1 , v2 , key , data in self . bg . edges ( nbunch = vertex1 , keys = True , data = True ) if v2 == vertex2 ] for key , data in edges_to_be_deleted_with_keys : self . __delete_bgedge ( BGEdge ( vertex1 = vertex1 , vertex2 = vertex2 , multicolor = data [ "attr_dic...
def heightmap_dig_bezier ( hm : np . ndarray , px : Tuple [ int , int , int , int ] , py : Tuple [ int , int , int , int ] , startRadius : float , startDepth : float , endRadius : float , endDepth : float , ) -> None : """Carve a path along a cubic Bezier curve . Both radius and depth can vary linearly along the ...
lib . TCOD_heightmap_dig_bezier ( _heightmap_cdata ( hm ) , px , py , startRadius , startDepth , endRadius , endDepth , )
def process_response ( self , response ) : """Store response headers ."""
self . response_headers = [ ( k , v ) for k , v in sorted ( response . headers . items ( ) ) ]
def Put ( self , key , obj ) : """Add the object to the cache ."""
# Remove the old entry if it is there . node = self . _hash . pop ( key , None ) if node : self . _age . Unlink ( node ) # Make a new node and insert it . node = Node ( key = key , data = obj ) self . _hash [ key ] = node self . _age . AppendNode ( node ) self . Expire ( ) return key
def set_node_text ( self , node , text ) : """Set text value as sole Text child node of element ; any existing Text nodes are removed"""
# Remove any existing Text node children for child in self . get_node_children ( node ) : if child . nodeType == xml . dom . Node . TEXT_NODE : self . remove_node_child ( node , child , True ) if text is not None : text_node = self . new_impl_text ( text ) self . add_node_child ( node , text_node )
def csvpretty ( csvfile : csvfile = sys . stdin ) : """Pretty print a CSV file ."""
shellish . tabulate ( csv . reader ( csvfile ) )
def _prepare_request_data ( self , eopatch , bbox , time_interval ) : """Collects all parameters used for DataRequest , each one is taken either from initialization parameters or from EOPatch"""
service_type = ServiceType ( self . _get_parameter ( 'service_type' , eopatch ) ) if time_interval is None : time_interval = self . _get_parameter ( 'time_interval' , eopatch ) if service_type is ServiceType . WMS : size_x_name , size_y_name = 'width' , 'height' else : size_x_name , size_y_name = 'resx' , '...
def get_group_partition ( group , partition_count ) : """Given a group name , return the partition number of the consumer offset topic containing the data associated to that group ."""
def java_string_hashcode ( s ) : h = 0 for c in s : h = ( 31 * h + ord ( c ) ) & 0xFFFFFFFF return ( ( h + 0x80000000 ) & 0xFFFFFFFF ) - 0x80000000 return abs ( java_string_hashcode ( group ) ) % partition_count
def check ( text ) : """Suggest the preferred forms ."""
err = "redundancy.wallace" msg = "Redundancy. Use '{}' instead of '{}'." redundancies = [ [ "rectangular" , [ "rectangular in shape" ] ] , [ "audible" , [ "audible to the ear" ] ] , ] return preferred_forms_check ( text , redundancies , err , msg )
def compose ( * parameter_functions ) : """Composes multiple modification functions in order . Args : * parameter _ functions : The functions to compose . Returns : A parameter modification function that consists of applying all the provided functions ."""
def composed_fn ( var_name , variable , phase ) : for fn in parameter_functions : variable = fn ( var_name , variable , phase ) return variable return composed_fn
def post ( self , endpoint , json = None , params = None , ** kwargs ) : """POST to DHIS2 : param endpoint : DHIS2 API endpoint : param json : HTTP payload : param params : HTTP parameters : return : requests . Response object"""
json = kwargs [ 'data' ] if 'data' in kwargs else json return self . _make_request ( 'post' , endpoint , data = json , params = params )
def get_filenames ( cls , mapreduce_state ) : """See parent class ."""
shards = mapreduce_state . mapreduce_spec . mapper . shard_count filenames = [ ] for _ in range ( shards ) : filenames . append ( [ None ] * shards ) shard_states = model . ShardState . find_all_by_mapreduce_state ( mapreduce_state ) for x , shard_state in enumerate ( shard_states ) : shard_filenames = shard_st...
def setChoice ( key , * args ) : '''check choice'''
return And ( lambda n : n in args , error = SCHEMA_RANGE_ERROR % ( key , str ( args ) ) )
def iter_methods ( self ) : """Iterate over stored functions and instance methods Yields : Instance methods or function objects"""
for wrkey , obj in self . iter_instances ( ) : f , obj_id = wrkey if f == 'function' : yield self [ wrkey ] else : yield getattr ( obj , f . __name__ )
def create_weapon_layer ( weapon , hashcode , isSecond = False ) : """Creates the layer for weapons ."""
return pgnreader . parse_pagan_file ( ( '%s%spgn%s' % ( PACKAGE_DIR , os . sep , os . sep ) ) + weapon + '.pgn' , hashcode , sym = False , invert = isSecond )
def has_code ( state , text , incorrect_msg = "Check the {ast_path}. The checker expected to find {text}." , fixed = False , ) : """Test whether the student code contains text . Args : state : State instance describing student and solution code . Can be omitted if used with Ex ( ) . text : text that student c...
stu_ast = state . student_ast stu_code = state . student_code # fallback on using complete student code if no ast ParseError = state . ast_dispatcher . ParseError def get_text ( ast , code ) : if isinstance ( ast , ParseError ) : return code try : return ast . get_text ( code ) except : ...
def creation_dates ( self , sort = True ) : """Return a list of ( file _ path , creation _ date ) tuples created from list of walked paths . : param sort : Bool , sorts file _ paths on created _ date from newest to oldest . : return : List of ( file _ path , created _ date ) tuples ."""
if not sort : return pool_creation_date ( self . filepaths ) else : pcd = pool_creation_date ( self . filepaths ) pcd . sort ( key = itemgetter ( 1 ) , reverse = True ) return pcd
def sort_string_by_pairs ( strings ) : """Group a list of strings by pairs , by matching those with only one character difference between each other together ."""
assert len ( strings ) % 2 == 0 pairs = [ ] strings = list ( strings ) # This shallow copies the list while strings : template = strings . pop ( ) for i , candidate in enumerate ( strings ) : if count_string_diff ( template , candidate ) == 1 : pair = [ template , strings . pop ( i ) ] ...
def register_states_of_state_machine ( self , state_machine ) : """This functions registers all states of state machine . : param state _ machine : the state machine to register all states of : return :"""
root = state_machine . root_state root . add_observer ( self , "state_execution_status" , notify_after_function = self . on_state_execution_status_changed_after ) self . recursively_register_child_states ( root )
def add_snmp_host ( self , ** kwargs ) : """Add SNMP host to NOS device . Args : host _ info ( tuple ( str , str ) ) : Tuple of host IP and port . community ( str ) : Community string to be added to device . callback ( function ) : A function executed upon completion of the method . The only parameter pas...
host_info = kwargs . pop ( 'host_info' ) community = kwargs . pop ( 'community' ) callback = kwargs . pop ( 'callback' , self . _callback ) config = ET . Element ( 'config' ) snmp_server = ET . SubElement ( config , 'snmp-server' , xmlns = ( "urn:brocade.com:mgmt:" "brocade-snmp" ) ) host = ET . SubElement ( snmp_serve...
def share ( self , name , item ) : '''Share an object via the telepath protocol . Args : name ( str ) : Name of the shared object item ( object ) : The object to share over telepath .'''
try : if isinstance ( item , s_telepath . Aware ) : item . onTeleShare ( self , name ) self . shared [ name ] = item except Exception : logger . exception ( f'onTeleShare() error for: {name}' )
def find_loci ( self , cluster_size , maxgap , locusview = False , colordict = None ) : '''Finds the loci of a given cluster size & maximum gap between cluster members . Args cluster _ size ( int ) : minimum number of genes in the cluster . maxgap ( int ) : max basepair gap between genes in the cluster . Kw...
if colordict != None : self . search . protein_arrow_color_dict = colordict for organism in self . organisms : print 'finding loci for' , organism . name # reset loci if there is something in there already organism . loci = [ ] orghits = [ ] for protein in organism . proteins : if len ( ...
def hyphen_range ( string ) : '''Expands a string of numbers separated by commas and hyphens into a list of integers . For example : 2-3,5-7,20-21,23,100-200'''
list_numbers = list ( ) temporary_list = string . split ( ',' ) for element in temporary_list : sub_element = element . split ( '-' ) if len ( sub_element ) == 1 : list_numbers . append ( int ( sub_element [ 0 ] ) ) elif len ( sub_element ) == 2 : for x in range ( int ( sub_element [ 0 ] ) ,...
def sparse_GP_regression_1D ( num_samples = 400 , num_inducing = 5 , max_iters = 100 , optimize = True , plot = True , checkgrad = False ) : """Run a 1D example of a sparse GP regression ."""
# sample inputs and outputs X = np . random . uniform ( - 3. , 3. , ( num_samples , 1 ) ) Y = np . sin ( X ) + np . random . randn ( num_samples , 1 ) * 0.05 # construct kernel rbf = GPy . kern . RBF ( 1 ) # create simple GP Model m = GPy . models . SparseGPRegression ( X , Y , kernel = rbf , num_inducing = num_inducin...
def exit_and_fail ( self , msg = None , out = None ) : """Exits the runtime with a nonzero exit code , indicating failure . : param msg : A string message to print to stderr or another custom file desciptor before exiting . ( Optional ) : param out : The file descriptor to emit ` msg ` to . ( Optional )"""
self . exit ( result = PANTS_FAILED_EXIT_CODE , msg = msg , out = out )
def _import_plugins ( self ) -> None : """Import and register plugin in the plugin manager . The pluggy library is used as plugin manager ."""
logger . debug ( 'Importing plugins' ) self . _pm = pluggy . PluginManager ( 'sirbot' ) self . _pm . add_hookspecs ( hookspecs ) for plugin in self . config [ 'sirbot' ] [ 'plugins' ] : try : p = importlib . import_module ( plugin ) except ( ModuleNotFoundError , ) : if os . getcwd ( ) not in sy...
def save ( self , output_file = None , convert_to = None , runscript = "/bin/bash" , force = False ) : '''save will convert a recipe to a specified format ( defaults to the opposite of the recipe type originally loaded , ( e . g . , docker - - > singularity and singularity - - > docker ) and write to an output ...
converted = self . convert ( convert_to , runscript , force ) if output_file is None : output_file = self . _get_conversion_outfile ( convert_to = None ) bot . info ( 'Saving to %s' % output_file ) write_file ( output_file , converted )