content
stringlengths
42
6.51k
def toUnicode(input): """ Converts a series of bytes to unicode (UTF-16) bytes Arguments : input - the source bytes Return: the unicode expanded version of the input """ unicodebytes = "" # try/except, just in case .encode bails out try: unicodebytes = input.encode('UTF-16LE') except: inputlst = list...
def generate_html(fields, pidx, appdata): """Fun to be had here!""" html = "" for arg in appdata['arguments']: html += "type: %s" % (arg['type'], ) return html
def check_blank_before_after_class(class_docstring, context, is_script): """Class docstring should have 1 blank line around them. Insert a blank line before and after all docstrings (one-line or multi-line) that document a class -- generally speaking, the class's methods are separated from each other b...
def get_odd_predecessor(odd_int, index, k=3): """ This method calculates the odd predecessor for a certain odd number in a Collatz graph. For every odd number there are n predecessors. The variable index [0..n] specifies which predecessor is returned. The method is based on a deterministic algorithm. ...
def get_hemisphere(lat): """For a given latitude, return N or S.""" if lat < 0.0: return 'S' else: return 'N'
def make_str(ppddl_tree, level=0): """ Creates a string representation of a PPDDL tree. """ if not ppddl_tree: return '' # Make sure the resulting string has the correct indentation. indent = (' ' * (2*level)) ppddlstr = indent + '(' indent += ' ' # Appending subelements of the PPDDL tree. ...
def flip_ctrlpts2d(ctrlpts2d, size_u=0, size_v=0): """ Flips a list of surface 2-D control points in *[u][v]* order. The resulting control points list will be in *[v][u]* order. :param ctrlpts2d: 2-D control points :type ctrlpts2d: list, tuple :param size_u: size in U-direction (row length) :t...
def get_hgvs(gene_obj): """Analyse gene object Return: (hgvs_nucleotide, hgvs_protein)""" hgvs_nucleotide = "-" hgvs_protein = "" transcripts_list = gene_obj.get("transcripts") for transcript_obj in transcripts_list: if transcript_obj.get("is_canonical") is True: hgvs...
def to_modify_quota(input_quota, array_quota, array_include_overhead): """ :param input_quota: Threshold limits dictionary passed by the user. :param array_quota: Threshold limits dictionary got from the Isilon Array :param array_include_overhead: Whether Quota Include Overheads or not. :return: Tr...
def generate_clustered_data(stories, clusters): """ clusters is a dict with an index as key and values as story IDs as stored in MongoDB clustered is a list of dictionaries. Each dictionary is a cluster of stories. Internal dicts have as keys the storyid and as values the actual story content. ...
def get_transcripts_from_tree(chrom, start, stop, cds_tree): """Uses cds tree to btain transcript IDs from genomic coordinates chrom: (String) Specify chrom to use for transcript search. start: (Int) Specify start position to use for transcript search. stop: (Int) Specify ending position to use for tra...
def average(numbers): """ Return the average (arithmetic mean) of a sequence of numbers. """ return sum(numbers) / float(len(numbers))
def remove_error_clusters(all_clusters, node_paired_unpaired, case): """ The salient features of the function remove_error_clusters are: 1. Arguments: "all_clusters" = list of all clusters for the given case under study "node_paired_unpaired" = complete set of data on the paired instants, ...
def got_all_step_funcs(event, debug=False): """ Check if all step function arns found. Args: event (dict): Step functions arns are set as they are found. debug (bool): Print debug messages. Returns: (bool): True if all step functions found. """ if event['delete-sfn-arn'...
def eval_metric(results, params): """BLEU Evaluate """ crr_cnt, total_cnt = 0, 0 for result in results: total_cnt += 1 p = result['pred_answer'] g = result['gold_answer'] if p == g: crr_cnt += 1 return crr_cnt * 100. / total_cnt
def get_sort_params(value, default_key=None): """Parse a string into a list of sort_keys and a list of sort_dirs. :param value: A string that contains the sorting parameters. :param default_key: An optional key set as the default sorting key when no sorting option value is specified...
def _reverse_map(dictionary) -> dict: """ Inverts a map of single or iterable value types. { a: b, c: d, } will become { b: a, d: c, } { a: [b, c], d: [e, f], } will become { b: a, c: a, e: d, f: d, } etc... """ result = {} for k in dictionary.keys(): v = dictionary[k] ...
def strip_library(name): """ >>> strip_library("fuchsia.device/MAX_DEVICE_NAME_LEN") 'MAX_DEVICE_NAME_LEN' >>> strip_library("SomethingGreat") 'SomethingGreat' """ return name[name.rfind('/') + 1:]
def compute_all_relationships(scene_struct, eps=0.2): """ Computes relationships between all pairs of objects in the scene. Returns a dictionary mapping string relationship names to lists of lists of integers, where output[rel][i] gives a list of object indices that have the relationship rel with object i....
def _normalize(string): """Returns the canonical form of a color name. Removes non-ascii letters, then lowercases the whole thing. """ return ''.join(filter(str.isalpha, string)).lower()
def get_vlan_ranges(begin_range, end_range): """Expands vlan ranged from vlan pools""" vlans = [] for vlan in range(int(begin_range), int(end_range) + 1): vlans.append(str(vlan)) return vlans
def anySizeToBytes(size_string): """ Convert a string like '1 KB' to '1024' (bytes) """ # separate integer from unit try: size, unit = size_string.split() except Exception: try: size = size_string.strip() unit = ''.join([c for c in size if c.isalpha()]) ...
def remove_empty_columns(orig_cols): """Remove columns with <= 1 non-empty cells.""" cols = [] for col in orig_cols: non_empty = sum((bool(cell[1]) for cell in col), 0) if non_empty >= 2: cols.append(col) return cols
def safe_int_cast(val, default=0): """Safely casts a value to an int""" try: return int(val) except (ValueError, TypeError): return default
def stats(index_and_true_and_retrieved): """ Returns comma-separated list of accuracy stats Included are (relevant instances, retrieved instances, overlap, precision, recall, f-score) index_and_true_and_retrieved is composed of: index: used to track which sa...
def word_score(word, opt=None): """ Count up the score of a word. a=1, b=2, c=3 Args: word: the word to get the score of opt: if opt does not equal None z will be 1 and a will be 26 Returns: The score of the word Raises: KeyError: character is invalid """ ...
def merge(dest, source): """In-place, recursive merge of two dictionaries.""" for key in source: if key in dest: if isinstance(dest[key], dict) and isinstance(source[key], dict): merge(dest[key], source[key]) continue dest[key] = source[key] retu...
def to_list(obj): """Convert an object to a list if it is not already one""" if not isinstance(obj, (list, tuple)): obj = [obj, ] return obj
def euclideanDistance( a, b ): """Assumes inputs are tuples.""" return ( ( a[0] - b[0] )**2 + ( a[1] - b[1] )**2 )**.5
def filter_batch(batch, i): """check whether sample i should be included""" return batch["passed_quality_filter"][i] is True
def allow(perm): """ This function acts as a predicate for allowed permutations. """ # leaves us with 1108800/39916800 permutations order = {j: i for i, j in enumerate([str(_) for _ in perm])} if order['NigiriCard("egg")'] > order['NigiriCard("salmon")']: return False if order['Nigir...
def moveZerosToEnd(arr): """Moving zeros to the end using 2 pointers""" # set a pointer at the first 0 elem in the array zero = 0 # find the first 0 in the array while zero < len(arr) and arr[zero] != 0: zero += 1 # init a pointer for the next element (might be nonzero) nonzero = zer...
def are_equal(j1, j2): """Returns True if j1 and j2 are equal, False otherwise""" if j1 is None or j2 is None: return False sum_diffs_abs = sum(abs(a - b) for a, b in zip(j1, j2)) if sum_diffs_abs > 1e-3: return False return True
def _get_normal_name(orig_enc): """Imitates get_normal_name in tokenizer.c.""" # Only care about the first 12 characters. enc = orig_enc[:12].lower().replace("_", "-") if enc == "utf-8" or enc.startswith("utf-8-"): return "utf-8" if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or \ enc.startswith(("latin-1...
def ispart(ptha, pthb): """ the functions takes two paths a and b and retrns True if all a elements are part of b and in the same order """ j = 0 for elem in ptha: if elem in pthb: if pthb.index(elem) >= j: j = pthb.index(elem) else: ...
def simplifyinfixops(tree, targetnodes): """Flatten chained infix operations to reduce usage of Python stack >>> def f(tree): ... print prettyformat(simplifyinfixops(tree, ('or',)), ('symbol',)) >>> f(('or', ... ('or', ... ('symbol', '1'), ... ('symbol', '2')), ... ...
def _call_settimeout(a, t, e): """ Handler for setTimeout and setInterval. Should determine whether a[0] is a lambda function or a string. Strings are banned, lambda functions are ok. Since we can't do reliable type testing on other variables, we flag those, too. """ if a and a[0]["type"] !...
def _get_ipynb_code_cell_idx(ipynb_dict): """ Get the index of the code cell. Parameters ---------- ipynb_dict : dict Dictionary of notebook data. Returns ------- idx : int The index of the code cell. """ idx = 0 cells_list = ipynb_dict['cells...
def ensure_quotes(s: str) -> str: """Quote a string that isn't solely alphanumeric.""" return '"{}"'.format(s) if not s.isalnum() else s
def x_coord(col, dpi): """x-coordinate in mils""" return 1000.*col/dpi
def process_docids(string): """ Returns the docid as an integer :param string: :return: """ try: docid = int(string) except ValueError: # print("Error converting docid to integer:", string) docid = 0 return docid
def vis17(n): # DONE """ .O ..O ...O OOO ..O ...O OOOOO ...O OOOOOOO Number of Os: 4 7 10""" result = '' for i in range(n): result += '.' * n + 'O\n' result += 'O' * (n * 2 + 1) + '\n' return result
def get_roi_params(separation: str = "uplc", instrument: str = "qtof"): """ Creates a dictionary with recommended parameters for the make_roi function in different use cases. Parameters ---------- separation : {"uplc", "hplc"} Mode in which the data was acquired. Used to set minimum len...
def diversity(vec): """Calculate diversity. :param vec: kmer vec :return: Diversity(X) """ m_sum = sum(vec) from math import log return m_sum*log(m_sum, 2) - sum([e*log(e, 2) for e in vec if e != 0])
def sum_multiples_of_3_and_5(limit: int) -> int: """ Sums all multiples of 3 and 5 below the provided limit. :param limit: Limit for multiples to search, non-inclusive. :return: Sum of all multiples. """ # Sum the multiples of 3 and 5, but subtract the multiples of 15 which get counted # twi...
def gcd(a, b): """The Euclidean Algorithm """ a = abs(a) b = abs(b) while a: a, b = b % a, a return b
def ljust(value, length): """Format general alphanumeric fields.""" if value is None: value = '' else: value = str(value) value = value.ljust(length, ' ')[0:length] return value
def get_rb_data_attribute(xmldict, attr): """Get Attribute `attr` from dict `xmldict` Parameters ---------- xmldict : dict Blob Description Dictionary attr : string Attribute key Returns ------- sattr : int Attribute Values """ try: sattr = int(...
def _cut_if_too_long(text: str, max_length: int) -> str: """Cut a string down to the maximum length. Args: text: Text to check. max_length: The maximum length of the resulting string. Returns: Cut string with ... added at the end if it was too long. """ if len(text) <= max_...
def _find_valid_path(options): """Find valid path from *options*, which is a list of 2-tuple of (name, path). Return first pair where *path* is not None. If no valid path is found, return ('<unknown>', None) """ for by, data in options: if data is not None: return by, data e...
def compile_toc(entries, section_marker='='): """Compiles a list of sections with objects into sphinx formatted autosummary directives.""" toc = '' for section, objs in entries: toc += '\n\n%s\n%s\n\n' % (section, section_marker * len(section)) toc += '.. autosummary::\n\n' for...
def deletionDistance(str1,str2): """ finds the minimum number of characters you need to delete in the two strings in order to get the same strings. Params: str1 (String) - the first string THAT DOES NOT CONTAIN A REPEATING CHARACTER' str2 (String) - the second string THAT DOES NOT ...
def inner_product(vector1, vector2): """ Returns vector1 . vector2 """ result = 0 for i in range(len(vector1)): result = result + vector1[i]*vector2[i] return result
def comb(n: int, k: int) -> int: """Number of combination Args: n (int): [description] k (int): [description] Returns: int: [description] """ if k >= n or k == 0: return 1 return comb(n - 1, k - 1) + comb(n - 1, k)
def status_in_range(value: int, lower: int = 100, upper: int = 600) -> bool: """ Validates the status code of a HTTP call is within the given boundary, inclusive. """ return value in range(lower, upper+1)
def safe_sub(val1, val2): """ Safely subtracts two values. If either value is -1, then the result is -1 (unknown). """ return -1 if val1 == -1 or val2 == -1 else val1 - val2
def parse_session_cookie(cookie_to_cook): """ cookie_to_cook = http_header['cookie'] """ #print("cookie_to_cook: %s"%str(cookie_to_cook)) session_value = None tokens = cookie_to_cook.split(";") for tok in tokens: if 'remi_session=' in tok: #print("found session id: %s"%str(to...
def expand_to_string(config): """Return a string expanding the configuration""" if not isinstance(config, list): return '' sconf = '' for item in config: if isinstance(item, str): sconf += item + ' ' elif isinstance(item, dict): for key, value in item.it...
def xor(bytes1: bytes, bytes2: bytes) -> bytes: """Support function to perform Exclusive-OR operation on two bytes. :param bytes1: set of bytes 1 :param bytes2: set of bytes 2 :returns: XORed data """ if len(bytes1) == len(bytes2): return bytes(a ^ b for a, b in zip(bytes1, bytes2)) ...
def get_value(x, y, z, center_prob): """ Calculates the probability for the box at x, y, z (within a 3x3x3 box)""" center_count = (x % 2) + (y % 2) + (z % 2) # counting the number of "1" in our coordinates (centers) prob_unit = (1-center_prob)/14 # the probability per voxel/cube around center cube values = [(pr...
def humidity_formatter(value): """Return the state of the entity.""" if value is None: return None return round(float(value) / 100, 1)
def convert_to_d_h_m_s(days): """Return the tuple of days, hours, minutes and seconds from days (float)""" days, fraction = divmod(days, 1) hours, fraction = divmod(fraction * 24, 1) minutes, fraction = divmod(fraction * 60, 1) seconds = fraction * 60 return int(days), int(hours), int(minutes), int(seco...
def file_version_summary(list_of_files): """ Given the result of list-file-versions, returns a list of all file versions, with "+" for upload and "-" for hide, looking like this: ['+ photos/a.jpg', '- photos/b.jpg', '+ photos/c.jpg'] """ return [('+ ' if (f['action'] == 'upload') else '-...
def solution(arr, target): """Solution using a hashset.""" lookup = set() for num in arr: complement = target - num if complement in lookup: return True lookup.add(num) return False
def ParentId(tpe, id): """ A criterion used to search for records by their parent's id. For example * search for observables by case id * search for tasks by case id * search for logs by task id * search for jobs by observable id Arguments: tpe (str): class name of the parent: ...
def compute_area(poly): """compute the area of poly""" point = [] for i in range(0, len(poly) - 1, 2): point.append([poly[i], poly[i + 1]]) s = 0.0 point_num = len(point) if point_num < 3: return 0.0 for i in range(len(point)): s += point[i][1] * (point[i-1][0]-point[...
def load_dotted_name(name): """Load an object, giving its full dotted name. Currently this isn't very smart, e.g. it can't load attributes off an object. I'll improve it as I need it. """ try: (modnm,objnm) = name.rsplit(".",1) except ValueError: return __import__(name,fromlist...
def to_unicode_repr( _letter ): """ helpful in situations where browser/app may recognize Unicode encoding in the \u0b8e type syntax but not actual unicode glyph/code-point""" # Python 2-3 compatible return u"u'"+ u"".join( [ u"\\u%04x"%ord(l) for l in _letter ] ) + u"'"
def replace_special_characters(string: str): """Replace special characters in step id with -""" parts = [] for word in string.split(' '): part = '' for c in word: if c.isalnum(): part += c elif part: parts.append(part) p...
def survey_T(phrase, langDict): """ Function to translate a phrase using the dictionary passed in """ if phrase in langDict and langDict[phrase] != "": return langDict[phrase] else: return phrase
def get_name_from_link(link): """ returns name from link. """ name = link.split("/")[-1] return name
def data_cut_at_index(data_cuts, index): """given data_cuts, which is the return value of remove_saturation, and index, return True if index is in a region that has been cut due to saturation, false otherwise.""" for start,stop in data_cuts: if start <= index < stop: return True ...
def __get_service_names(scenario_config): """ Gets the list of services from the scenario config. If no services are given, an empty list is returned. :param scenario_config: The scenario config. :return: A list of services. [] if no service names are found. """ if 'services' in scenario_con...
def ros_advertise_service_cmd(_type, service): """ create a rosbridge advertise_service command object :param _type: type of the ROS service :param service: name of the service """ command = { "op": "advertise_service", "type": _type, "service": service } return...
def make_single(vdict): """ >>> d = {"xx": (1,)} >>> make_single(d) {'xx': 1} """ for k in vdict.keys(): if isinstance(vdict[k], tuple) and len(vdict[k]) == 1: vdict[k] = vdict[k][0] return vdict
def vol_prism(area_of_base: float, height: float) -> float: """ Calculate the Volume of a Prism. Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry) :return V = Bh >>> vol_prism(10, 2) 20.0 >>> vol_prism(11, 1) 11.0 """ return float(area_of_base * height)
def trim_time_column(line): """ trims the time column off the front of the given line from backup plan file. Useful when you're searching for a particular backup plan entry but don't care about the time. """ return " ".join(line.split("\t")[1:])
def pad_right(value, to_size, pad_with): """ Should be called to pad value to expected length """ pad_amount = to_size - len(value) if pad_amount > 0: return b"".join(( value, pad_with * pad_amount, )) else: return value
def _sort_destinations(destinations): """ Takes a list of destination tuples and returns the same list, sorted in order of the jumps. """ results = [] on_val = 0 for dest in destinations: if len(results) == 0: results.append(dest) else: while on_val <...
def sum_digits(n: int) -> int: """Returns the sum of the digits of non-negative integer n.""" assert not n < 0, "Non-negative integer n only." if n < 10: return n return n % 10 + sum_digits(n // 10)
def handle011AT(tokens): """ Processes the 011@ (https://www.gbv.de/bibliotheken/verbundbibliotheken/02Verbund/01Erschliessung/02Richtlinien/01KatRicht/1100.pdf) field. Currently, only subfield a is supported. Only the first year of publication is extracted. For details (in German), see: https://www.gbv.de...
def __num_two_factors(x): """Return how many times integer x can be evenly divided by 2. Returns 0 for non-positive integers. """ if x <= 0: return 0 num_twos = 0 while x % 2 == 0: num_twos += 1 x //= 2 return num_twos
def get_scales(dataset): """Returns NMS IOU, IOU and Prob Score for a particular dataset""" if dataset == 'DeepFruits': return 0.4, 1e-5, 0.2 elif dataset == 'mangoYOLO': return 0.5, 1e-5, 0.6 elif dataset == 'MultiFruit': return 0.3, 1e-5, 0.2 elif dataset == 'MinneAppl...
def GammaCorrection(color, gamma=2.8, max=255): """ Perfomes Gamma Correction on a color. :param color: The color to perform the gamma correction on. :param gamma: The gamma value. :param max: Specifies full scale output of the gamma correction. :return: Gamma corrected color. """ retu...
def memory2int(memory): """Internal helper function to convert Kubernetes memory amount to integers.""" multiplier = 1 if memory.endswith("Ki"): multiplier = 1024 elif memory.endswith("Mi"): multiplier = 1024 ** 2 elif memory.endswith("Gi"): multiplier = 1024 ** 3 return ...
def check_for_contents(string, string_dict): """Iterate through string dict to check contents in other string""" for snippet in string_dict: if snippet in string: return True return False
def is_overlapped(end1, start2): """Returns True if the two segments overlap Arguments --------- end1 : float End time of the first segment. start2 : float Start time of the second segment. """ if start2 > end1: return False else: return True
def getcoroutinelocals(coroutine): """ Get the mapping of coroutine local variables to their current values. A dict is returned, with the keys the local variable names and values the bound values.""" frame = getattr(coroutine, "cr_frame", None) if frame is not None: return frame.f_local...
def eigvector_uncoupled(par): """ Returns the flag for the correction factor to the eigenvectors for the linear guess of the unstable periodic orbit. Parameters ---------- parameters : float (list) model parameters Returns ------- correcx : 1 or 0 flag to set the x-com...
def get_did_by_foreign_key(did_foreign_key): """Return the DID referenced by a ForeignKey or OneToOneField to IdNamespace. Return None if ForeignKey or OneToOneField is NULL. This is used instead of "did_foreign_key.*.did" on ForeignKeys and OneToOneFields that allow NULL (null=True in the model). ...
def hours_to_minutes(hours: str) -> int: """Converts hours to minutes""" return int(hours) * 60
def subset(l, L): """ Takes two lists and returns True if the first one is contained in the second one. If the lists could be sorted, it would be more efficient. :param l: `list` instance. :param L: `list` instance. :return: `bool` instance. """ return all(x in L for x in l)
def trim_text(text): """Helper method to trim generated text.""" # Cut off generated output at the last ./?/! if there is one, # unless the text ends with hashtags and the last punc is before the hashtags. end_punc = '.!?' max_end_idx = -1 for end in end_punc: end_idx = text.rfind(end) if end_idx > max_end_id...
def min_(data): """Minimum of the values in the object""" minimum = data[0] for value in data: minimum = value if value < minimum else minimum return minimum
def stream2dict(stream_list): """Convert stream list into stream dict Parameters ---------- stream_list : list Stream in list form (list of dicts), as returned by Strava API v3 Returns ------- stream_dict : dict Stream in dict form, with key set to *stream name* and value s...
def get_support(cluster): """ Returns support >>> get_support({5: {'11111': ['ab', 'ac', 'df', 'bd', 'bc']}, ... 4: {'11101': ['ef', 'eg', 'de', 'fg'], '11011': ['cd']}, ... 3: {'11001': ['ad'], '10101': ['dg']}, ... 2: {'10010': ['dh', 'bh'], '11000'...
def wrap_node_data_lookup(uuid_to_devices): """given a uuid to devices map, returns dictionary like the following: {'parameter_defaults': {'NodeDataLookup': {'32e87b4c-c4a7-41be-865b-191684a6883b': {'devices': ['/dev/sdc']}}, {'ea6a84d6-cf89-4fe2-b7bd-869b3fe4dd6b': {'devices': ['/dev/sdc'...
def Counter64(al, br, delta): """64bit counter aggregator with wrapping """ if br < al: ch = 18446744073709551615 - al return (ch + br) / float(delta) return (br - al) / float(delta)
def generate_output_field(input_field, field_type): """ Grab an output field for a censys input field :param input_field: censys Field :return: """ output_field = input_field.replace('.', '__') return output_field
def create_data_structure(emojis): """Create data structure to store the emojis :param emojis: Dictionary of emoji :type emojis: dict :rtype: dict """ head = dict() for word in emojis.keys(): current = head for char in word: try: current = current...