content
stringlengths
42
6.51k
def dist(x, y): """Some distance""" return abs(x["par"] - y["par"])
def isprobablytitle(s): """Return True if string s looks like it could be a title; False otherwise. This is a heuristic function based on my Gutenberg Poetry corpus. Takes every word from string s with five or more characters and returns True if the resulting joined string is in title case. >>> is...
def is_binary(s: str) -> bool: """Checks if the given string is a binary operator. Parameters: s: string to check. Returns: ``True`` if the given string is a binary operator, ``False`` otherwise. """ return s == '&' or s == '|' or s == '->'
def _filter_by_attr(list_in, attr_name, attr_val, match_substring=False): """ Filter input list by the value of its elements' attributes. """ list_out = [] for val in list_in: if match_substring: if attr_val in getattr(val, attr_name): list_out.append(val) ...
def handle_extensions(extensions=('html',)): """ organizes multiple extensions that are separated with commas or passed by using --extension/-e multiple times. for example: running 'django-admin makemessages -e js,txt -e xhtml -a' would result in a extension list: ['.js', '.txt', '.xhtml'] >>>...
def float_vct_str ( vct , format = '%.5g' ) : """Convert vector of floating types to string""" try : return '[ ' + ', '.join ( [ format % v for v in vct ] ) + ' ]' except TypeError : pass return float_vct_str ( vct , format = '%.5g' )
def spline_grid_from_range(spline_size, range=2, round_to=1e-6): """ Compute spline grid spacing from desired one-side range and the number of activation coefficients. Args: round_to: round grid to this value """ spline_grid = ((range / (spline_size//2)) // round_to) * round_to return ...
def tag2dict(tags): """ Converts a list of AWS tag dicts to a single dict with corresponding keys and values """ return {tag.get('Key'): tag.get('Value') for tag in tags or {}}
def plural(attr): """ Simple function. Gives plural form with 's' at the end. """ return f"{attr.rstrip('s')}s"
def tag_predicate(p): """ Given a full URI predicate, return a tagged predicate. So, for example, given http://www.w3.org/1999/02/22-rdf-syntax-ns#type return rdf:type """ ns = { "http://www.w3.org/1999/02/22-rdf-syntax-ns#":"rdf:", "http://www.w3.org/2000/01/r...
def get_eccF(r1_norm, r2_norm, c_norm): """ Computes the eccentricity component along the chord. This value is kept constant for all the problem as long as the boundary conditons are not changed. Parameters ---------- r1_norm: float Norm of the initial vector position. r2_norm: ...
def minkowski_params(n_samples): """Generate minkowski benchmarking parameters. Parameters ---------- n_samples : int Number of samples to be used. Returns ------- _ : list. List of params. """ manifold = "Minkowski" manifold_args = [(3,), (5,)] module = "ge...
def _insert_intersphinx_mapping(c): """Insert the ``intersphinx_mapping``, ``intersphinx_timeout`` and ``intersphinx_cache_limit`` variableis into the configuration state. """ c["intersphinx_mapping"] = { "python": ("https://docs.python.org/3/", None), # FIXME add local object cache ...
def fetch_symbols_from_elements(elmnts): """Fetch the symbol entry in the elements dictionary in Aiida.""" new_dict = {} for key, value in elmnts.items(): new_dict[value['symbol']] = key return new_dict
def getblockrejectreason(sdb, propcode, blocks): """Get the reason for the block rejection""" for b in blocks: if propcode == b[2]: record = sdb.select('BlockRejectedReason_Id', 'BlockVisit', 'BlockVisit_Id=%i' % b[0])[0][0] return record return 0
def mock_config(_path): """ Load a mock config to override toml.load() Args: path (str): Normally, the path to the TOML is provided Returns: dict: The interpreted TOML file """ return {"project": {"name": "valid"}}
def is_gde_label(x): """Checks if x looks like a GDE label line.""" return x and x[0] in "%#"
def _find_node_by_name(nodes, name): """ Loop through a list of nodes and return the one which has the matching display name :param nodes: List of nodes :param name: Target name :return: Matching node, None otherwise """ # Nodes always have unique names, thus there's no need for duplicate ch...
def make_key(name: str, param: str) -> str: """ Formats a key in the API style. """ return "{}_{}".format(name, param)
def scaleFactor(line, sample): """ Parameters ---------- line : int sample : int Returns ------- int scalefactor - ratio of sample and minSample or line and minLine """ maxLine = 1000 maxSample = 1000 minLine = 50 minSample = 50 if sample < line: ...
def which(program): """ Mimic functionality of unix which command """ import sys,os def is_exe(fpath): return os.path.isfile(fpath) and os.access(fpath, os.X_OK) if sys.platform == "win32" and not program.endswith(".exe"): program += ".exe" fpath, fname = os.path.split(prog...
def getTypesWithName(types, names): """function returns all types that have a specific name Keyword arguments: types -- list of model.Type instances names -- list of strings with names """ typesWithName = [] for type in types: for name in names: if type.name == name: ...
def make_ordered_event_pairs(alphabet): """ Make an ordered collection of event pairs (ordered in the sense that the name of the first field of the pair comes lexicographic before the second field). @param alphabet: Alphabet to create combinations for. @type alphabet: C{set} of L{Event} @...
def autonomy_out_cb(outcome_map): """Returns transition""" if outcome_map['TOGGLE_LISTEN'] == 'invalid': return 'enter_teleop' elif outcome_map['AUTONOMY'] == 'aborted': return 'aborted' else: return 'stay'
def author_clean(author): """ Clean an author and return the formatted string """ replace = [".", ";", " ", ",", "_", "-"] author_split = author.strip().split(",") clean_author = "" if len(author_split) >= 2: last_name = author_split[0] first_name = author_split[1] for rep in...
def roi_pad(roi, pad, shape): """ Pad ROI on each side, with clamping. Returned ROI is guarnteed to be within ``(0,..) -> shape``. """ def pad_slice(s, n): return slice(max(0, s.start - pad), min(n, s.stop + pad)) if isinstance(roi, slice): return pad_slice(roi, shape) re...
def size (grid): """Returns the size of `grid` as `(nrows, ncols)`.""" return len(grid), len(grid[0])
def drop_command(message, command): """ Given a message text, drops the command prefix from the string. """ return message[len(command) + 1:]
def get_graph_mean(x, data): """function that actually calculates the average over x samples for \ different data""" ind = -1 new_data = [] for index, entry in enumerate(data): if index % x == 0: if ind >= 0: new_data[ind] /= x ind += 1 new...
def scalar_multiply(c: float, v) : """Multiplies every element by c""" return [c * v_i for v_i in v]
def unquote(s): """ Undo the effects of quote(). Based heavily on urllib.unquote(). """ mychr = chr myatoi = int list = s.split('_') res = [list[0]] myappend = res.append del list[0] for item in list: if item[1:2]: try: myappend(mychr(myatoi(it...
def __same_axes(x_axis, y_axis, xlim, ylim): """Check if two axes are the same, used to determine squared plots""" axes_same_and_not_none = (x_axis == y_axis) and (x_axis is not None) axes_same_lim = xlim == ylim return axes_same_and_not_none and axes_same_lim
def line_splitter(s): """Split a string at newlines. Parameters ---------- s : str The string to be split Returns -------- List[str] List of strings. Obtained by calling s.splitlines(). """ return s.splitlines()
def y_coord(row, rows, line_width): """ y-coordinate in mils""" return (rows - row - 1)*line_width
def validate_timestamp(val): """ Returns a boolean indication that a field is a valid timestamp - a floating point number representing the time in seconds since the Epoch (so called POSIX time, see https://en.wikipedia.org/wiki/Unix_time). """ return (isinstance(val, float) and val >= 0.0)
def parse_results(results): """ parses the results file into a dictionary of names and tuples """ # hold the results in a dictionary results_dict = {} # loop over each line (result) for result in results: # split the string based on spaces parts = result.split() # there shoul...
def and_join(strings): """Join the given ``strings`` by commas with last `' and '` conjuction. >>> and_join(['Korea', 'Japan', 'China', 'Taiwan']) 'Korea, Japan, China, and Taiwan' :param strings: a list of words to join :type string: :class:`collections.Sequence` :returns: a joined string ...
def _get_existing_wrapped_events(method): """ returns the current wrappers around methods """ ret = [] if hasattr(method, "__get_pycounters_event_names__"): ret = method.__get_pycounters_event_names__() return ret
def datavalidation_result_format_change(log): """ Change the format of result log ex:[{"id":1,"name":"hj","quantity":2}] Args: log: log of datavalidation Returns: Changed format of log ex:[["id","name","quantity"],[1,"hj",2]] """ result = [] keys = [] for key, value in ...
def generateSvalues(b=10, p=15): """ int x int -> list[int] returns a list of all the S quantities that are going to be tested afterwards """ # values: list[int] values = [] # i, i_max: int i = b i_max = pow(b, p) while i < i_max: values.extend(range(i, i*b, i)) i *=b values.append(i_max) return values
def mult(x, y): """Creates an SMTLIB multiplication statement formatted string Parameters ---------- x, y: float First and second numerical arguments to include in the expression """ return "(* " + x + " " + y + ")"
def check_string(input_string): """Updates string characters that may lead to errors when written.""" input_string = input_string.replace(" ", "_") input_string = input_string.replace(",", "_") input_string = input_string.replace(";", "_") input_string = input_string.replace(":", "_") input_stri...
def Iq(q, intercept, slope): """ :param q: Input q-value :param intercept: Intrecept in linear model :param slope: Slope in linear model :return: Calculated Intensity """ inten = intercept + slope*q return inten
def clean_names(a): """remove braces and tildes from names""" if a is None: return None a_new = [] for name in a: a_new.append(name.replace("{", "").replace("}", "").replace("~", " ")) return a_new
def without_styl(s): """Return string S without ".styl" extension.""" return s.replace(".styl", "")
def format_argument_version(arg_version): """ Replaces '.' with '-' throughout arg_version to match formatting requirements for log message Args: arg_version(str): Version tag to be formatted Returns: str: Formatted version tag """ return arg_version.replace(".", "-")
def parse_lookup_key(lookup_key): """Parse a lookup key.""" if not lookup_key: raise KeyError("No lookup key specified") # Parse the list of keys if isinstance(lookup_key, str): keys = lookup_key.split('.') elif isinstance(lookup_key, list): keys = lookup_key else: ...
def is_integer(num): """Check if given value is an integer.""" try: int(num) return True except ValueError: return False
def merge_two_dicts(x, y): """ merge to dictionaries :param x: :param y: :return: merged dict """ z = x.copy() # start with x's keys and values z.update(y) # modifies z with y's keys and values & returns None return z
def non_empty(value, question, keep=None): """Ask value while it is empty""" if value != "<ask>": if value: return value if keep is not None: return keep elif keep is None: return value while True: value = input("=> " + question + "\n") che...
def rgb_to_hex(color): """Convert an rgb color to hex.""" return "#%02x%02x%02x" % (*color,)
def milli_2_readadble(msecs): """Function: milli_2_readadble Description: Converts milliseconds into days, hours, minutes and seconds. Returns values with appropriate tags. Arguments: (input) msecs -> Milliseconds. """ data = msecs / 1000 seconds = data % 60 data /= 60...
def process_coords(coords, size, psize): """ centers object to be pasted on card :param coords: coords of the object to be pasted :param size: size of the we are pasting on :param psize: size of the object to be pasted :return: proper coords of the object to be pasted """ if coords[0] ==...
def get_stats(tp, fp, tn, fn): """ Computes performace measures using the correctness measures returned by check_preds() Parameters: ----------------- tp: number of true positive predictions returned by check_preds() fp: number of false positive predictions returned by check_preds() tn...
def intersect(u,v): """Compute the intersection of the two slice lists.""" if u is None: return v if v is None: return u return tuple([slice(max(u[i].start,v[i].start),min(u[i].stop,v[i].stop)) for i in range(len(u))])
def getKLDivergence( P, Q ): """Compute KL-divergence from P to Q""" import math divergence = 0 assert len(P) == len(Q) for i in range(len(P)): p = P[i] q = Q[i] assert p >= 0 assert q >= 0 if p > 0: divergence += p * math.log( p / q ) return d...
def calc_tfidf(p, idf): """Returns a dictionary of words present in p with their tfidf scores""" counts = {} for word in p: c = counts.setdefault(word, 0) c += 1 counts[word] = c tfidf = {} for k, c in counts.items(): tfidf[k] = (c / len(p)) * idf[k] return tfidf
def get_axe_names(image, ext_info): """Derive the name of all aXe products for a given image""" # make an empty dictionary axe_names = {} # get the root of the image name pos = image.rfind('.fits') root = image[:pos] # FILL the dictionary with names of aXe products # # the GOL: ...
def model_get_kwargs(feature): """ Get LandUseArea get() kwargs--useful for get_or_create()--for the feature. """ return { 'object_id': feature['attributes']['OBJECTID'], }
def all_in_charset(chars, string): """Returns true if string's characters are in chars, false otherwise :param chars: Set of characters to test characters in string against :param string: The string to test :return bool: Whether the characters in string are wholly within chars""" for c in string: ...
def soft_light(lower_rgb, upper_rgb): """Apply soft light blending mode of a layer on an image. """ return (1.0 - lower_rgb) * lower_rgb * upper_rgb \ + lower_rgb * (1.0 - (1.0 - lower_rgb) * (1.0 - upper_rgb))
def get_data(data, variable): """Perform transformations on raw data and int and converts them to supported types. :param data: Raw data dict object :param variable: key of dictionary to be transformed :returns transformed data (str) """ if isinstance(data.get(variable), list): return ...
def _dtype_equality(dtype1, dtype2): """ Check for numpy dtype equality References: https://stackoverflow.com/questions/26921836/correct-way-to-test-for-numpy-dtype Example: dtype1 = np.empty(0, dtype=np.uint8).dtype dtype2 = np.uint8 _dtype_equality(dtype1, dtype2) ...
def calculate_benefit_cost_ratio(region, country_parameters): """ Calculate the benefit cost ratio. """ cost = ( region['network_cost'] + region['spectrum_cost'] + region['tax'] + region['profit_margin'] ) revenue = region['total_revenue'] if revenue > 0 an...
def finditem(search_dict, field): """ Takes a dict with nested lists and dicts, and searches all dicts for a key of the field provided. """ fields_found = [] for key, value in search_dict.items(): if key == field: fields_found.append(value) elif isinstance(value, ...
def concat(list_a: list, list_b: list) -> list: """ Concatenates two lists together into a new list Example: >>> concat([1, 2, 3], [4, 5, 6]) ... [1, 2, 3, 4, 5 6] :param list_a: First list to concatenate :param list_b: Second list to concatenate :return: Concatenated list "...
def centerOfMass(positions, weights): """ Calculate the center of mass of a set of weighted positions. Args: positions: A list of (x,y,z) position tuples weights: A list of position weights Return: A tuple of floats representing the coordinates of the center of mass. """ tot_weight = sum(weigh...
def removeWhitespace(seqToTest): """ Checks sequence for non-permitted characters and removes them if necessary. Mainly for whitespace but I also put dashes and asterisks in here """ changed = False badChars = ['\t',' ','\n', '-', '*'] if any([x in badChars for x in seqToTest]): seqToTest = ''.join([x for x in...
def nonempty_region(r): """Return True if given region is nonempty. >>> nonempty_region([1,2,3,4,5,6]) True >>> nonempty_region([1,2,4,4,5,6]) False """ assert type(r) == list assert len(r) == 6 return (r[0] != r[1] and r[2] != r[3] and r[4] != r[5])
def get_hrefs(data): """Traversing the data recursively, return the list of values for all 'href' keys in the dictionary. If the value for an 'href' key is not a string, then ignore it. """ result = [] if isinstance(data, list): for value in data: result.append(get_hrefs(valu...
def to_camel_case(snake_str): """ Converts snake_string to camelCase :param str snake_str: :returns: str """ components = snake_str.split('_') return components[0] + "".join(x.title() for x in components[1:])
def scalar_product(v_1_i, v_2_i): """Scalar product of vectors.""" v_1_1, v_1_2, v_1_3 = v_1_i v_2_1, v_2_2, v_2_3 = v_2_i return v_1_1*v_2_1 + v_1_2*v_2_2 + v_1_3*v_2_3
def gap_digraph(edges): """Given a 0-indexed list of edges, return a 1-indexed list of edges of twice the length (i.e. a directed version) suitable for pasting into GAP.""" return [[a+1,b+1][::s] for (a, b) in edges for s in (1, -1)]
def diff_between_angles(angle_a, angle_b): """Calculates the difference between two angles angle_a and angle_b Args: angle_a (float): angle in degree angle_b (float): angle in degree Returns: float: difference between the two angles in degree. """ delta_mod = (angle_b - an...
def shorten_list(l, max_length=15): """ If list longer than max_length, returned shortened version""" length = len(l) if length > max_length: sl = l[0:max_length] sl.append("...%i total" % length) else: sl = l return sl
def DecodeUTF8(data): """Decodes data as UTF-8, replacing any bad characters. If data is not bytes type, returns as-is. """ if isinstance(data, bytes): return data.decode('utf-8', errors='replace') return data
def ldistinct(seq): """Iterates over sequence skipping duplicates""" seen = set() res = [] for item in seq: if item not in seen: seen.add(item) res.append(item) return res
def _collapse(xlink_pos, by_bc, group_by, multimax=1): """ Report number of cDNAs and reads in cross-link site on xlink_pos. Input parameter `by_bc` has te following structure: by_bc = { 'AAA': [(middle_pos, end_pos, read_len, num_mapped, cigar, second_start), # hit1 (middle_po...
def iroot(n, r): """Computes the integer root of order r of n""" u, s = n, n+1 while u < s: s = u t = (r-1) * s + n // pow(s, r-1) u = t // r return s
def on_storage(full_path): """ Check whether or not a file is located on Google Cloud Storage Args: ``full_path`` (string): file path Returns: ``flag`` (bool) """ return full_path.startswith("gs://")
def _get_best_indexes(logits, n_best_size): """Get the n-best logits from a list.""" index_and_score = sorted(enumerate(logits), key=lambda x: x[1], reverse=True) best_indexes = [] for i in range(len(index_and_score)): if i >= n_best_size: break best_indexes.append(index_and_score[i][0]) return best_indexe...
def split(word): """ Simple function to convert every string send it to him in to a list of single characters. """ list_word = [] for character in word: list_word.append(character) return list_word
def create_url(controller_ip, endpoint): """Create endpoint url to POST/PUT/GET/DELTE against.""" return 'https://%s:1280/%s' % (controller_ip, endpoint)
def _mel2hz(mel): """Convert Mel frequency to frequency. Arguments: mel:Mel frequency Returns: Frequency. """ return 700 * (10**(mel / 2595.0) - 1)
def F(N): """ Fibonacci Sequence. F(n + 1) = F(n) + F(n - 1) """ a = 0 b = 1 F = [a, b] for j in range(1, N + 1): c = a + b a = b b = c F.append(c) return F[N]
def _findfile(path, pattern): """ find file depending on pattern """ import os, fnmatch result = [] for root, dirs, files in os.walk(path): #DEBUG: print root, dirs, files for name in files: if fnmatch.fnmatch(name, pattern): result.append(os.path.join(r...
def factorial(num): """ return the factorial with recursion :param num: the number :return: factorial """ # use short-circuiting return int(num < 2) or num * factorial(num - 1)
def calc_offset_2(offset): """Part 2 offset is incremented by 1 after it's used unless it's decremented""" return offset - 1 if offset >= 3 else offset + 1
def anonymized(tumor_list, prefix="CS"): """Anonymizes a list of tumor sample names.""" result = {} alphabetical = sorted(tumor_list) for idx, elem in enumerate(alphabetical): result[elem] = elem return result
def fib(n): """ Recursively compute the fibonacci sequence """ if n<=1: return n else: return fib(n-1)+fib(n-2)
def get_odd_palindrome_at(s, index): """ (str, int) -> str Precondition: s contains only lowercase alphabetic characters. 0 <= index < len(s) Return the longest odd-length palindrome in s that is centered at index. >>> get_odd_palindrome_at('accccc', 2) 'ccc' >>> ge...
def ZoneNameToRegionName(zone_name): """Converts zone name to region name: 'us-central1-a' -> 'us-central1'.""" return zone_name.rsplit('-', 1)[0]
def list_substract(list1, list2): """OK [52] Returns list1 where each element is substracted by the corresponding list2 element. Returns None if the lenght of the lists is not the same. """ if not list1: return None result_list = [] if len(list1) != len(list2): #prin...
def get_link_meta_3_types(source, target, new2old, old2new): """ Return meta data of node pairs from original graph before transformation :param source: id of source node in transformed graph :param target: :param new2old: map of new ids to old (id, type) before transform :return: sour...
def is_float(value): """ Checks if `value` is a float. Args: value (mixed): Value to check. Returns: bool: Whether `value` is a float. Example: >>> is_float(1.0) True >>> is_float(1) False .. versionadded:: 2.0.0 """ return isinstance(...
def bubblesort(lst): """Bubble sorting algorithm.""" if isinstance(lst, list): for i in range(len(lst)): for j in range(len(lst) - 1, i, -1): if lst[j] < lst[j - 1]: lst[j], lst[j - 1] = lst[j - 1], lst[j] return lst return "BubbleSort takes on...
def call_me_maybe(obj): """See `here`_ for description. .. _here: https://www.youtube.com/watch?v=fWNaR-rxAic """ return obj() if hasattr(obj, "__call__") else obj
def ipstr(barray): """Print a string of ip digits""" return ".".join('{}'.format(x) for x in barray)
def apply_case(s, case): """Helper function that applies case to a string.""" if case.upper() == "UPPER": s = s.upper() elif case.upper() == "LOWER": s = s.lower() elif case.upper() == "CAPITALIZE": s = s.capitalize() elif case.upper() == "TITLE": s = " ".join([w.cap...
def sizeof_fmt(num, suffix='B'): """ Utility function to convert byte amounts to human readable format. Taken from http://stackoverflow.com/a/1094933/2528077 """ for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']: if abs(num) < 1024.0: return "%3.1f%s%s" % (num, unit, suffix) ...