content
stringlengths
42
6.51k
def asfloat(x): """try to convert value to float, or fail gracefully""" try: return float(x) except (ValueError, TypeError): return x
def invoke_if_valid(cb, value): """Returns the result of cb(value) if cb is callable, else -- just value""" if cb is None or not callable(cb): return value return cb(value)
def basename(p): # type: (str) -> str """Returns the final component of a pathname""" i = p.rfind('/') + 1 return p[i:]
def Scheme(endpoint): """Returns https scheme for all the endpoints except localhost.""" if endpoint.startswith('localhost:'): return 'http' else: return 'https'
def makename(package, module): """Join package and module with a dot.""" # Both package and module can be None/empty. if package: name = package if module: name += '.' + module else: name = module return name
def count_phrase_frequency( raw_list ): """ count the frequency that phrases and words appear in a text, when passed the list of phrases (which are kept as lists). It's a list of lists. """ frequencies = dict() for sub_list in raw_list: #for each sub list in the list items = [item for...
def scalar_mul(c, X): """Multiply vector by scalar.""" return [c*X[0], c*X[1]]
def determine_adjusted(adjusted): """Determines weather split adjusted closing price should be used.""" if adjusted == False: return 'close' elif adjusted == True: return 'adjClose'
def seq_decode(_string, symbols): """ Decode a number from a using encoded by a sequence of symbols. :param str _string: string to decode :param symbols: sequence key :type symbols: str or list[char] :returns: decoded value :rtype: int """ value = 0 base = len(symbols) for i...
def post_processing_aggregate_results(l_d_classification_results): """ Post processing results obtained from multi-processing cones prefixes. """ d_ases_cones_prefixes_merged = dict() # prepare three dictionaries as output for dict_result in l_d_classification_results: for k, v in dict...
def getval(obj, getter): """Gets a value from an object, using a getter which can be a callable, an attribute, or a dot-separated list of attributes""" if callable(getter): val = getter(obj) else: val = obj for attr in getter.split('.'): val = getattr(val, a...
def array_remove_duplicates(s): """removes any duplicated elements in a string array.""" found = dict() j = 0 for x in s: if x not in found.keys(): found[x] = True s[j] = x j = j + 1 return s[:j]
def average_word_error_rate(word_error_rate_scores, combined_ref_len): """ This function calculates average word error rate of the whole sentence. :param word_error_rate_scores: (float) Word error rate of each word. :param combined_ref_len: (int) Length of the reference sentence. :return: (float) Av...
def _get_rid_for_name(entries, name): """Get the rid of the entry with the given name.""" for entry in entries.values(): if entry['name'] == name: return entry['rid'] raise ValueError(u'No entry with name {} found in entries {}'.format(name, entries))
def stripped(userhost): """ return a stripped userhost (everything before the '/'). """ return userhost.split('/')[0]
def func_args_pq(*args, p="p", q="q"): """func. Parameters ---------- args: tuple p, q: str, optional Returns ------- args: tuple p, q: str """ return None, None, None, None, args, p, q, None
def _format_td(timedelt): """Format a timedelta object as hh:mm:ss""" if timedelt is None: return '' s = int(round(timedelt.total_seconds())) hours = s // 3600 minutes = (s % 3600) // 60 seconds = (s % 60) return '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)
def pa_array_to_dict(array): """ Convert a D-Bus named array into a Python dict. FIXME: This assumes all keys and values are meant to be strings. Dbus's arrays come out annoyingly, firstly they use the dbus version of strings/ints/etc, but the string values come out as a zero-terminated byte array....
def query_to_dict(results, descriptions): """ Replacement method for cursor.dictfetchall() as that method no longer exists in psycopg2, and I'm guessing in other backends too. Converts the results of a raw SQL query into a list of dictionaries, suitable for use in templates etc. """ output...
def tap(x, label=None): """Prints x and then returns it.""" if label: print('%s: %s' % (label, x)) else: print(x) return x
def group_course_contents(courses_contents): """ Group all the course contents by content type. :param courses_contents: List of dictionaries of course contents for all the courses in course_list. :type courses_contents: list(dict(str, int) :return: A dictionary with a list of each of the assessmen...
def calc_amount_function(obj_for_calc_amount): """ Calculates actual molecule amounts. Three types of amounts are calculated for a matched isotope chromatogram (MIC), maximum intensity, summed up intensity and area under curve. Additionally the score and the retention time at the maximum intensi...
def parse_item(item): """ Parse a CVE item from the NVD database """ parsed_items = {} cve = item.get('cve', {}) CVE_data_meta = cve.get('CVE_data_meta', {}) ID = CVE_data_meta.get('ID', '') parsed_items['id'] = ID affects = cve.get('affects', {}) vendor = affects...
def guess_format(text): """Guess YANG/YIN format If the first non-whitespace character is '<' then it is XML. Return 'yang' or 'yin'""" format = 'yang' i = 0 while i < len(text) and text[i].isspace(): i += 1 if i < len(text): if text[i] == '<': format = 'yin'...
def linear_map(src_value, src_domain, dst_range): """Maps source value (src_value) in the source domain (source_domain) onto the destination range (dest_range) using linear interpretation. Except that at the moment src_value is a bytes value that once converted to integer that it then is on the...
def myround(number, multiple=5): """ x is the number to round base is the multiple to round to, default 5 """ return multiple * round(number/multiple)
def calculate_ideal_amount_of_iterations(n, k=1): """ Calculates the ideal amount of Grover's iterations required for n qubits (assuming k solutions). See: https://en.wikipedia.org/wiki/Grover%27s_algorithm#Extension_to_space_with_multiple_targets If there are no solutions, no iterations are ne...
def count_spaces(line): """ Counting the spaces at the start of a line """ return len(line)-len(line.lstrip(' '))
def get_shape_type(name): """Translate the shape type (Polygon, Polyline, Point) into a, l, or p.""" types = {"Polygon": "a", "Polyline": "l", "Point": "p"} try: return types[name] except KeyError: # Key not found print("Unknown shape type") return -1
def parser_preferred_name_identifier_Descriptor(data,i,length,end): """\ parser_preferred_name_identifier_Descriptor(data,i,length,end) -> dict(parsed descriptor elements). This descriptor is not parsed at the moment. The dict returned is: { "type": "preferred_name_identifier", "contents" : unpa...
def answer(panel_array): """ Returns the maximum product of positive and (odd) negative numbers.""" print("panel_array=", panel_array) # Edge case I: no panels :] if (len(panel_array) == 0): return str(0) # Get zero panels. zero_panels = list(filter(lambda x: x == 0 , panel_arra...
def fix_filter_query(filter): """ fix filter query from user Args: filter: filter from users Returns: dict """ if filter: try: filter = { _.split("=")[0]: _.split("=")[1] for _ in list( set( str(filt...
def format_parsing_error(result): """ Output a formatted version of any parsing errors that Ansible runs into when looking at a playbook, if any are present. :param result: result to inspect :return: formatted output message """ if 'reason' in result: return 'Parsing the playboo...
def add_padding(data: bytes, block_n_bytes: int) -> bytes: """Adds 0x80... padding according to NIST 800-38A""" res = data + b'\x80' len_remainder = len(res) % block_n_bytes if len_remainder != 0: res += b'\0' * (block_n_bytes - len_remainder) return res
def longest_increasing_subsequence(seq): """Docstrings are lame figure out the type signature yourself """ cache = {} # where we will memoize our answer def helper(x): if x == 0: # base case return seq[:1] if x in cache: # checking if we've memoized a previous answ...
def _remove_empty_items(input_json): """Removing nulls and empty strings from input_json.""" row_output = dict((k, v) for k, v in input_json.items() if v) return row_output if row_output else None
def calc_tdf(docs): """Calculates the tdf scores based on the corpus""" terms = set() for doc in docs: for term in doc: terms.add(term) tdf = {} for term in terms: doc_count = 0 for doc in docs: doc_count += 1 if term in doc else 0 t...
def get_prov_transp(attr, ancestor_file, plotname): """Create a provenance record for the 1d meridional transports.""" caption = ("Thermodynamic Diagnostic Tool - Annual mean zonally averaged" " meridional {} transports" " for model {}.".format(attr[0], attr[1])) record = { ...
def parse(puzzle_input): """Parse input""" return [int(line) for line in puzzle_input.split()]
def alias_tags(tags_list, alias_map): """ update tags to new values Args: tags_list (list): alias_map (list): list of 2-tuples with regex, value Returns: list: updated tags """ def _alias_dict(tags): tags_ = [alias_map.get(t, t) for t in tags] return lis...
def __fix_ada_dictionary__(bayesDict): """ If convert AdaBoost's "base_estimator" parameter into string :param bayesDict: :return: """ for k, v in bayesDict.items(): # if k == 'params': # Param grid does not behave properly, # listDict = bayesDict[k] # new_params...
def reflectance(n1, n2): """Calculate the amplitude reflection coefficient due to moving from media with index n1 to media with index n2. Args: n1 (float:): Refractive index of media 1. n2 (float:): Refractive index of media 2. Returns: float: Reflection coefficient""" return (...
def _dict_conll_map_helper(values, empty, delim, av_separator, v_delimiter, formatter, av_key): """ Helper to map dicts to CoNLL-U format equivalents. Args: values: The value, dict, to map. empty: The empty CoNLL-U reprsentation for this value. delim: The ...
def get_base_by_color(base): """ Get color based on a base. - Uses different band of the same channel. :param base: :return: """ if base >= 250: return 'A' if base >= 180: return 'G' if base >= 100: return 'C' if base >= 30: return 'T' if base ...
def parse_packageset(packageset): """ Get packages and repositories data from PES event's package set """ return {p['name']: p['repository'].lower() for p in packageset.get('package', [])}
def Qualify(ns, name): """Makes a namespace-qualified name.""" return '{%s}%s' % (ns, name)
def ConstantAddition(s, t, scheme): """ ConstantAddition function from LowMC spec. :params s: state :params bs: round constants :params t: round :param scheme: lowmc parametrization + constants """ return [s[i] ^ scheme['b'][t-1][i] for i in range(scheme['blocksize'])]
def IoU(bbox_1, bbox_2): """ Get IoU value of two bboxes :param bbox_1: :param bbox_2: :return: IoU """ w_1 = bbox_1[2] - bbox_1[0] + 1 h_1 = bbox_1[3] - bbox_1[1] + 1 w_2 = bbox_2[2] - bbox_2[0] + 1 h_2 = bbox_2[3] - bbox_2[1] + 1 area_1 = w_1 * h_1 area_2 = w_2 * h_2 ...
def boolstr(value, true="true", false="false"): """ Convert a boolean value into a string. This function is intended to be used from within file templates to provide an easy way to take boolean values stored in Pillars or Grains, and write them out in the apprpriate syntax for a particular file ...
def check_cache(cache_obj: dict) -> tuple: """ Check if redis is used to cache images, and if image 'name' is cached. Return state of redis, if image in cache and image if it is in cache. :param name: :return: returns two values. First cache object, second Bokeh image as dict of 'script' and 'div' ...
def get_ending(fit_format: int) -> str: """Get the file extension for a given fit format. Args: fit_format (int): .csv (0) or .pkl (1). Raises: Exception: If an invalid fit format is provided. Returns: str: The extension for the provided fit format. """ if fit_format i...
def split(inp,n): """ Splits an input list into a list of lists. Length of each sub-list is n. :param inp: :param n: :return: """ if len(inp) % n != 0: raise ValueError i = j = 0 w = [] w2 = [] while i<len(inp): # print(i,j) if j==n: j ...
def splitNotaries(lines): """Segment the txt file into chunks of information for one notary. Args: lines (list): lines from the txt file Returns: list: list of lists, each with lines for one notary """ notaryLines = [] notaryInfo = [] for i in lines: if i ...
def greatest_common_divisor(a: int, b: int) -> int: """ Euclid's Lemma : d divides a and b, if and only if d divides a-b and b Euclid's Algorithm >>> greatest_common_divisor(7,5) 1 Note : In number theory, two integers a and b are said to be relatively prime, mutually prime, or co-pri...
def right_diagonal_value(coord_x, coord_y, grid): """Return the product of 4 right diagonally adjacent cells in a dictionary. """ try: product = (grid[(coord_x, coord_y)] * grid[(coord_x + 1, coord_y + 1)] * grid[(coord_x + 2, coord_y + 2)] * ...
def ntimes(string, char): """ Return number of times character 'char' occurs in string """ return string.count(char)
def make_generic_usage_message(doc): """Construct generic usage error :param doc: Usage documentation for program :type doc: str :returns: Generic usage error :rtype: str """ return 'Unknown option\n{}'.format(doc)
def anagram(string1, string2): """Determines whether two strings provided are anagrams""" # create a list of letters in string, then sort s1 = sorted(list(string1)) s2 = sorted(list(string2)) if len(s1) != len(s2): return False else: for i in range(len(s1)): if s...
def GetHumanReadableDiskSize(size): """Returns a human readable string representation of the disk size. Args: size: Disk size represented as number of bytes. Returns: A human readable string representation of the disk size. """ for unit in ['bytes', 'KB', 'MB', 'GB', 'TB']: if size < 1024.0: ...
def mutate(codon, alt, index): """ Replace (mutate) a base in a codon with an alternate base. Parameters ---------- codon : str three letter DNA sequence alt : str alternative base index : int index of the alt base in codon (0|1|2). ...
def filter_urcline(string, filler=''): """ filter undesirable characters out of urcline string """ for bad in '\r\x00': string = string.replace(bad, filler) return string
def city_state(city, state, population=0): """Return a string representing a city-state pair.""" output_string = city.title() + ", " + state.title() if population: output_string += ' - population ' + str(population) return output_string
def accuracy_evaluation_boolean_float(attribute, values_range): """ This function calculate the accuracy considering if each value is in the provided range. :param df: The attribute to evaluate :param values_range: The range of values admitted for the attribute in analysis e.g.: values_ran...
def _to_swarming_dimensions(dims): """Converts dimensions from buildbucket format to swarming format.""" return [ {'key': key, 'value': value} for key, value in (s.split(':', 1) for s in dims) ]
def stripdesc(desc): """strip trailing whitespace and leading and trailing empty lines""" return '\n'.join([l.rstrip() for l in desc.splitlines()]).strip('\n')
def nrrdvector(inp): """Parse a vector from a nrrd header, return a list.""" assert inp[0] == '(', "Vector should be enclosed by parenthesis." assert inp[-1] == ')', "Vector should be enclosed by parenthesis." return [float(x) for x in inp[1:-1].split(',')]
def check_entityname(entnamelist): """ Checks for non alpha-numeric characters in entity names. Replaces any with underscore. Directly modifies input list. Allows: -, (, ), empty space. Parameters ---------- entnamelist: list Returns ------- entnamelist: list """ ...
def _process(user_input, mapping, extra=[]): """Processes `user_input` string.""" target = user_input for symbol in mapping: target = target.replace(symbol[0], symbol[1]) for symbol in extra: target = target.replace(symbol[0], symbol[1]) # short_desc = target # Some magic... ...
def two_fer(name = 'you'): """ name: name returns: str """ return f"One for {name}, one for me."
def make_cup_links(cups): """Reformat list of cups into a dict with cup pointing to next cups.""" cup_links = dict() prev_cup = cups[0] for cup in cups[1:]: cup_links[prev_cup] = cup prev_cup = cup cup_links[prev_cup] = cups[0] return cup_links
def add_html_hoover(text, hoover_text, text_color='', url=''): """Add a "title" to a text, using an empty href link, in order to create a mouseover""" if text_color != '': text_color = '; color: ' + text_color return '<a href="'+url+'" style="text-decoration: none' + text_color + '" title="' + hoover_tex...
def get_normal_form(obj_name): """Transform object name to title form. Example: risk_assessments -> Risk Assessments """ return obj_name.replace("_", " ").title()
def filename(request): """Generate export filename with search query string.""" filename = 'contacts-export' # q = request.GET.get('q') # if q: # filename = '%s_%s' % (filename,q) return filename
def proto2methodprotofunc(proto): """Convert a prototype such as 'Ljava/lang/String;' into a string 'Ljava_lang_String" so we can append that to the 'METHOD_myMethod' if its export python name contains the prototype """ return proto.replace(' ', '').replace('(', '').replace('[', '').replace( ...
def grompp_em(job): """Run GROMACS grompp for the energy minimization step.""" em_mdp_path = "em.mdp" msg = f"gmx grompp -f {em_mdp_path} -o em.tpr -c init.gro -p init.top --maxwarn 1" return msg
def inc_key(text): """ increment the last letter of text by one. Used to replace key in SQL LIKE case with less than """ if len(text) > 0: return text[0:-1] + chr(ord(text[-1]) + 1) else: return text
def check_result(result): """ check result """ if isinstance(result, dict) and "error" in result.keys(): return True return False
def hamming_distance(s1, s2): """ Ripped straight from https://en.wikipedia.org/wiki/Hamming_distance#Algorithm_example """ if len(s1) != len(s2): raise ValueError("Undefined for sequences of unequal length") return sum(el1 != el2 for el1, el2 in zip(s1, s2))
def _err_msg(err, hostname, port): """Generate template error message. Generate a template error message with the hostname and the port: `ConnectionError. 192.168.1.50:55555`. Args: err (str): Error message to add. hostname (str): hostname. port (str|int): port. Returns: ...
def compute_top_k_accuracy(labels, predictions): """Compute the accuracy of the `predictions` using `labels` as ground truth. Args: labels: An iterable of ground truth labels. predictions: A matrix where each row contains k label predictions. The true label is in the correspo...
def fc_params(in_features: int, out_features: int, bias: bool = True): """ Return the number of parameters in a linear layer. Args: in_features: Size of input vector. out_features: Size of output vector. bias: If true count bias too. Returns: The number of parameters. ...
def get_road_shiftlog_url(season, game): """ Gets the url for a page containing shift information for specified game from HTML tables for road team. :param season: int, the season :param game: int, the game :return : str, e.g. http://www.nhl.com/scores/htmlreports/20072008/TV020001.HTM """ ...
def filter_dictionary(dictionary, field_list): """ Takes dictionary and list of elements and returns dictionary with just elements specified. Also decodes the items to unicode :param dictionary: the dictionary to filter :param field_list: list containing keys to keep :return: dictionary with ju...
def bleu(reference, candidate): """Calculates the BLEU score of candidate sentence. Candidate sentence is compared to reference sentence using a modified form of precision: BLEU = m / w Where m is the number of words in the candidate that were found in reference and w is the total number of w...
def _truncate_lines(infile, line, c): """Replace long sequences of single characters with a shorter version. """ if set(line) == set(c) and len(line) > 64: line = c * 64 return line
def linear_decay_learning_rate(step, total_train_steps, initial_lr=0.1, offset=0): """Linearly decay the learning rate to 0. Args: step: a tf.scalar representing the step we want the learning rate for. total_train...
def escapeXMLChars(text): """Controls characters that need to be escaped (to obtain a well-formed XML document) Args: text: The text that will be escaped (string) Returns: text: new text containing XML entities instead of characters (string) """ text = text.replace("&...
def type_to_node(type_name): """ Convert an Avro type name (with dots) to a GraphViz node identifier. """ # First double underscores type_name = type_name.replace("_", "__") # Then turn dots into underscores type_name = type_name.replace(".", "_") return type_name
def lum_double_power_law(lum, phi_star, lum_star, alpha, beta): """Evaluate a broken double power law luminosity function as a function of luminosity. :param lum: Luminosity :type lum: float or np.ndarray :param phi_star: Normalization of the broken power law at a value of lum_star :type p...
def names_match(rankings, param_names): """ This convert the results in partial order of the format index into Veneer_names. rankings: dict, partial sort results parameters: dataframe, contains names of parameters """ partial_names = {} for key, value in rankings.items(): partial_nam...
def tier_from_site_name(s): """ Splits site name by _ (underscore), and takes only the first part that represents tier. """ split = s.split('_') tier = str(split[0]) return tier
def _build_arguments(keyword_args): """ Builds a dictionary of function arguments appropriate to the index to be computed. :param dict keyword_args: :return: dictionary of arguments keyed with names expected by the corresponding index computation function """ function_arguments = { ...
def getSubsetTuples(dim_dict, subset_indices): """ returns a list of tuples that represents a subset of dim_dict defined by subset_indices Inputs: dim_dict (dict): a dictionary of (key, value) = (label for the level, list of indices for the level) Most often comes from ...
def paragraph_tokenizer(text, delimiter = '\n\n'): """Given a text, break it down into paragraphs Keyword arguments: text -- given text delimiter - type of delimiter to be used, default value is '\n\n' """ paragraphs = text.split(delimiter) return paragraphs
def str_to_bool(string: str = 'False') -> bool: """ util function for smart conversion from str to bool, returns bool conversion result, returns False if not given params raises Valuerror when string cant be converted """ true_values = ['true', 'True', '1', 'on', 'yes'] false_values = [...
def filter_long_docs(src, tgt, max_doc_len): """Filters too long documents together with their targets. Returns lists.""" new_src = [] new_tgt = [] for s, t in zip(src, tgt): if len(s) > max_doc_len: continue new_src.append(s) new_tgt.append(t) return new_src, new...
def binary_search(items, desired_item, start=0, end=None,): """Standard Binary search program takes Parameters: items= a sorted list desired_item = single looking for a match (groovy baby) start= int value representing the index position of search section end = end boundary of...
def form_metaquast_cmd_list(metaquast_fp, outdir, input_fasta): """format argument received to generate list to be used for metquast subprocess call Args: metaquast_fp(str): the string representing the path to metaquast executable outdir(str): the string representing the path to the output dire...
def remove_numbers(tokens_list:list) -> list: """Remove numbers from list of tokens. Args: tokens_list (list): list of tokens Returns: list: list of tokens with numbers removed """ return_lst = [] for lst in tokens_list: return_lst.append([s for s in lst if not s.isdig...
def get_geo_coordinates(tweet): """ Get the user's geo coordinates, if they are included in the payload (otherwise return None) Args: tweet (Tweet or dict): A Tweet object or dictionary Returns: dict: dictionary with the keys "latitude" and "longitude" or, if unavaiab...