content
stringlengths
42
6.51k
def getMappingsBetweenUUIDsAndTraxels(model): """ From a dictionary encoded model, load the "traxelToUniqueId" mapping, create a reverse mapping, and return both. """ # create reverse mapping from json uuid to (timestep,ID) traxelIdPerTimestepToUniqueIdMap = model["traxelToUniqueId"] timest...
def to_var(field): """Converts a field name int a variable (snake_case) Args: string: a string to be converted to a var Returns: a string in lower snake case """ return field.replace(' ', '_').lower()
def derive_bt_mac(base_mac,offset): """ Derives the BT_MAC from the BASE_MAC and the BT_MAC LSB offset. """ base_mac_lsb=int(str(base_mac[-1]), base=16)+offset base_mac[-1]=format(base_mac_lsb, 'x') bt_mac='-'.join(base_mac) return bt_mac.strip()
def build_variant_sample_annotation_id(call_info, variant_uuid, file_accession): """ Helper function that builds a variant sample annotation ID from the required parts. """ return ':'.join([call_info, variant_uuid, file_accession])
def get_matches(geoms, tree_idx): """ Function to return the indici of the rtree that intersects with the input geometries Parameters ---------- geoms : list list of geometries to compare against the STRtree tree_idx: STRtree a STRtree indexing object Returns ------- ...
def split_box( fraction, x,y, w,h ): """Return set of two boxes where first is the fraction given""" if w >= h: new_w = int(w*fraction) if new_w: return (x,y,new_w,h),(x+new_w,y,w-new_w,h) else: return None,None else: new_h = int(h*fraction) if...
def valid_yes_or_no(user_input): """The purpose of this function is to receive user input and determine if the user input is a valid yes (Y) or no (N) response to the prompt. This function is called throughout the program to ensure errors do not occur. Once the user has entered a valid yes (Y) or no...
def is_ok_url(url: str): """ Check doc url is valid """ ng_list = [ "aws-sdk-php", "AWSAndroidSDK", "AWSiOSSDK", "AWSJavaScriptSDK", "AWSJavaSDK", "awssdkrubyrecord", "encryption-sdk", "mobile-sdk", "pythonsdk", "powershell"...
def get_deviation(number: int, circle: int) -> int: """Get distance to horizontal or vertical line from 1.""" # Special case for memory storage center if number == 1: return 0 # Side length derived from progression n'th child formula side = circle * 2 + 1 # Normalize number - bottom le...
def is_banned_asset_tag(text): """ Determines whether the text is a banned asset tag through various tests. :param text: Text to be checked against banned asset tags :type text: str :return: `True` if a banned asset tag else `False` :rtype: bool """ # Is asset tag in banned list? te...
def replace_valueCheck(value): """validate applyDQPlane.replace_value""" return (value in ('mean', 'median') or isinstance(value, float))
def quote_arg(s: str) -> str: """Return a quoted cmd arg, with all backslashes and double quotes escaped""" escaped = s.replace('\\', '\\\\').replace('"', '\\"') return f'"{escaped}"'
def trace_size_converter(value, params, key): """ Converts trace size from seconds to samples """ params = params[key] if value is None: return None if not value: return value if not params['frequency']: raise AttributeError('No frequency specified for trace-size argu...
def _sint(S): """ convert a string to an integer, treating an all-blank string as zero Parameter --------- S : str string that need to be converted as integer treating an all-blank strings as zero Returns ------- integer or zero """ if S.strip(): return i...
def s_to_b(s: str) -> bytes: """convert string to bytes :param s: input string :type s: str :return: output bytes :rtype: bytes """ b = s.encode('utf8') return b
def _py_if_stmt(cond, body, orelse): """Overload of if_stmt that executes a Python if statement.""" return body() if cond else orelse()
def inside_bounding_box(bb_minlat, bb_minlon, bb_maxlat, bb_maxlon, start_lat, start_lon, end_lat, end_lon): """ Check if two given sets of coordinates (start_lat, start_lon) and (end_lat, end_lon) are within a bounding box (bb_minlat, bb_minlon, bb_maxlat, bb_maxlon) Examples: >>> inside_bounding_...
def sort(word_p_lists): """ this method combine all the diction in word_p_list(word with its z_score) into totallist, with a mark to indicate which file the element(word with z_score) belongs to and then sort the totallist, to give user a clean output of which word in which file is the most abnormal ...
def distance1(point1, point2): """L1 distance""" return sum(abs(point1[i]-point2[i]) for i in range(len(point1)))
def check_overlap(peak1, peak2): """check_overlap checks if a peak overlaps, if they do, it returns 0, if not, it returns 1 if the first given peak is greater than the second, or -1 if otherwise Args: peak1 (tuple): start, end tuple which constitutes a peak peak2 (tuple): start, end tuple which...
def text_attribute(attribute, operator, value): """ Select an audience to send to based on an attribute object with a TEXT schema type, including predefined and device attributes. Please refer to https://docs.airship.com/api/ua/?http#schemas-textattribute for more information about using this selec...
def cat_matrices2D(mat1, mat2, axis=0): """ args: two matrices and axies return: matrix """ if axis == 0: if len(mat1[0]) is not len(mat2[0]): return None return [item.copy() for item in mat1] + [item.copy() for item in mat2] else: if len(mat1) is not len(mat2...
def ttr(text): """ Number of unique tokens :param str text: Input string of text :return float floatValue: Ratio of the unique/overall tokens """ if len(text.split(" ")) > 1 and len(text.split()) > 0: return len(set(text.split())) / len(text.split()) else: return 0
def _create_context_response(context_el, status_code): """ Function to build the context response model :param context_el: JSON including the context element attributes :param status_code: status code received from context manager :return (dict) Context response mode. The contextResponse in JSON wil...
def parse_attributes(attribute_str): """ Given the attribute part from a line record file, parse the attribute section. Returns a dict of numeric attribute values. Example: parse_attributes("0=0,2=0") => {0: 0, 2: 0} """ # Split into individual key/value pairs attrs = (s.strip()...
def is_human(user): """ checks that the user is not a bot exists for test mocking """ return user is not None and not user.is_bot
def check_against_gaps(regions, candidates): """Given a set of non-overlapping gaps and a list of candidate regions, return the candidates that do not overlap""" regions = sorted(regions, key=lambda l: l.start) candidates = sorted(candidates, key=lambda l: l.start) selected = [] r = 0 if not len...
def convert_extract_device(name): """ change Fusions UV to FusionsUV, etc """ n = "" if name: n = name.replace(" ", "") return n
def solution1(root): """ Inefficient solution by myself --- Runtime: 252ms --- :type root: TreeNode :rtype: list[int] """ if root is None: return [] queue = [(root, 0)] bfs_nodes = [] output = [] cur_depth = 0 while queue: bfs_nodes.append(queue...
def _valdiate_ValueEntries(strInput : str, acttyp) -> bool: """ Description ----------- Helper method used for validation of entry widgets with only one parameter Parameters ---------- `strInput` : string Input string which should be validated Return ------ `bValidateRe...
def find_repeat(source, elmt): """Helper function, find index of repreat elements in source.""" elmt_index = [] s_index = 0;e_index = len(source) while(s_index < e_index): try: temp = source.index(elmt, s_index, e_index) elmt_index.append(temp) s_index = temp...
def _format_params(cols, fields, where, crs, precision): """ Transform parameters into a query input for ESRIs feature service: The feature service allows users to query & edit feature geoms & attributes A breakdown of the ESRIs feature service: https://developers.arcgis.com/rest/services-refer...
def get_word_freq(word_list, normalize=True): """Returns a sorted list of (word,word count) tuples in descending order. The frequency is normalized to values between 0 and 1 by default.""" word_freq_dict = {} for w in word_list: if w in word_freq_dict: word_freq_dict[w] += 1 ...
def get_ngrams(text, n): """Returns all ngrams that are in the text. Inputs: text: string n: int Returns: list of strings (each is a ngram) """ if text == "": return [] tokens = text.split() return [" ".join(tokens[i:i + n]) for i in range(len(tokens) - (n - 1...
def bytes_to_block(block_size: int, i: int) -> slice: """ Given the block size and the desired block index, return the slice of bytes from 0 to the end of the given block. :param block_size: The block size. :param i: The block index. :return: slice of bytes from 0 to the end of the specified bl...
def is_currently_on_packet(freshman): """ Helper method for the search command Handles the filtering of results """ for _ in filter(lambda packet: packet["open"], freshman["packets"]): return True return False
def get_newline_str(do_escape_n=True, do_br_tag=True): """ Get the string to be used to indicate newlines in documents. Return a space if neither are specified. :param do_escape_n: :param do_br_tag: :return: """ if do_escape_n is False and do_br_tag is False: return " " newline_s...
def is_pos_int(number): """ Returns True if a number is a positive integer. """ return type(number) == int and number >= 0
def converged(losses, window=10, threshold=0.0001): """"Detect loss convergence in a window.""" try: if len(losses) < window: return False losses = losses[-window:] return max(losses) - min(losses) < threshold except: return False
def power(a, b): """ calculate the value of 'a' to the power 'b' :param a: :param b: :return: """ return pow(a, b)
def split_attribute(attribute): """Splits a list of attributes in head and remainder. """ return attribute[0], attribute[1:]
def fahrenheit_to_celsius(x): """Convert degrees in fahrenheit to celsius""" return (x - 32) * 0.5556
def get_source_region(global_cluster_response): """ Get writer region from 'describe_global_clusters' response. :param global_cluster_response: output of boto3 describe_global_clusters :return: aws region """ clusters = global_cluster_response["GlobalClusters"] for cluster in clusters: ...
def str_format_dict(jdict, **kwargs): """Pretty-format a dictionary into a nice looking string using the `json` package. Arguments --------- jdict : dict, Input dictionary to be formatted. Returns ------- jstr : str, Nicely formatted string. """ kwargs.setdefault('...
def sum_nums(nums): """Given list of numbers, return sum of those numbers. For example: sum_nums([1, 2, 3, 4]) Should return (not print): 10 """ # Python has a built-in function `sum()` for this, but we don't # want you to use it. Please write this by hand. # YOUR CODE HERE...
def show_navigator_boards(context): """Show navigator with for boards""" page = context['page'] index_begin = context['index_begin'] index_end = context['index_end'] index_total = context['index_total'] mindex_begin = context['mindex_begin'] mindex_end = context['mindex_end'] table = con...
def day_fraction(time): """Convert a 24-hour time to a fraction of a day. For example, midnight corresponds to 0.0, and noon to 0.5. :param time: Time in the form of 'HH:MM' (24-hour time) :type time: string :return: A day fraction :rtype: float :Examples: .. code-block:: python ...
def Series_info(series: list) -> dict: """[summary] Args: series (list): [Array Of Series] Attribute: third_term (flout): \n third_last_term (flout): \n sum_of_the_series (flout): \n Returns: dict: [Series Information] """ third_term = series[2] th...
def _is_authenticated_query_param(params, token): """Check if message is authentic. Args: params (dict): A dict of the HTTP request parameters. token (str): The predefined security token. Returns: bool: True if the auth param matches the token, False if not. """ return para...
def _QuoteValue(value): """Returns value quoted, with preference for "...".""" quoted = repr(value) if quoted.startswith('u'): quoted = quoted[1:] if quoted.startswith("'") and '"' not in value: quoted = '"' + quoted[1:-1] + '"' return quoted
def char_name(c): """ Return the name of a character. Specifically, returns a descriptive name instead of whitespace. No type checking is done. Parameters: c: a string containing the character """ cnames = { ' ': 'space', '\t': 'tab', '\n': 'newline', ...
def docsim_freetext(document, sess_id=None): """ use document similarity to recommend trials based on similarity to title & abstract text of review @param review_id: PMID of review @param sess_id: session ID if transitting progress via websocket """ if sess_id: socketio = SocketIO(messag...
def constr_leaf_novalue(xml_str, leafName): """onstruct the leaf update string""" xml_str += "<" + leafName + "></" + leafName + ">\r\n" return xml_str
def translate_signatures(signatures, rosetta, ignore_missing=False): """ Translate gene identifiers in a signature dictionary. Args: signatures (dict of list): signature dictionary rosetta (dict): translation table mapping one gene identifier to another ignore_missing (boolean): If ...
def rounded(minutes, base=5): """ Round the number of provided minutes based on the amount of minutes. :param minutes: Real number of minutes to apply round operation on. :type minutes: int :param base: The base number of minutes to use in rounding. :type base: int :return: Number of minute...
def sort_function(FF): """Remove all duplicate monomials in the feedback functions FF. """ FF_Sort = [] for i in range(len(FF)): # For each feedback function in FF if type(FF[i]) == list: tempx = FF[i] tempy = [] for sublist in tempx: if subl...
def sort_latency_keys(latency): """The FIO latency data has latency buckets and those are sorted ascending. The millisecond data has a >=2000 bucket which cannot be sorted in a 'normal' way, so it is just stuck on top. This function returns a list of sorted keys. """ placeholder = "" tmp = [] ...
def add_content(old_html, raw_html): """Add html content together""" old_html += raw_html return old_html
def is_bytes(data): """ Check if data is of type byte or bytearray. :param data: :return: """ try: data = data.decode() return False except AttributeError: return True
def get_items_from_index(x,y): """ decipher the values of items in a list from their indices. """ z = [] for i in y: try: z.append(x[i]) except: pass return z
def compute_score_interpretability_method(features_employed_by_explainer, features_employed_black_box): """ Compute the score of the explanation method based on the features employed for the explanation compared to the features truely used by the black box """ score = 0 for feature_employe in featur...
def str2bool(value: str) -> bool: """ Parse a string value and cast it into its boolean value :param value: :return: """ if value in ["y", "yes", "t", "true", "on", "1"]: return True if value in ["n", "no", "f", "false", "off", "0"]: return False raise ValueError("boolean value unrec...
def generate_structure_for_lgb(fetch,main_key_value,derived_col_names): """ It returns a List where the nodes of the model are in a structured format. Parameters ---------- fetch : dictionary Contains the nodes in dictionary format. main_key_value: List Empty list used to appen...
def make_inverse_map(lst): """ Make a map from Lin index to a state index. Parameters ---------- lst : list List containing Lin indices. Returns ------- rez : list List containing a map from Lin index to a state index. """ rez = [0]*len(lst) for j1 in range(...
def minmatch(userstr, mylist, case=0): #-------------------------------------------------------------- """ Purpose: Given a list 'mylist' with strings and a search string 'userstr', find a -minimal- match of this string in the list. Inputs: userstr- The string that we wan...
def process_results(unprocessed, P, R, G): """Process the results returned by the worker pool, sorting them by policy and run e.g. results[i][j][k] are the results from policy i on run j on graph k. Parameters: - unprocessed: Unprocessed results (as returned by the worker pool) - P: number of po...
def knapsack(capacity, items): """Return maximum value of the items with specified capacity.""" if not items or not capacity: return 0 item = items.pop() if (item.weight > capacity): return knapsack(capacity, items) capacity_with_item = capacity - item.weight with_item = item...
def identifier(s): """ Return s as a double-quoted string (good for psql identifiers) """ return u'"' + s.replace(u'"', u'""').replace(u'\0', '') + u'"'
def input_data_task_id(conf): # type: (dict) -> str """Retrieve input data task id :param dict conf: configuration object :rtype: str :return: task id """ return conf['task_id']
def occupied_squares_by_player(state, white_player): """ Returns the the x, y coordinates of the squares occupied by the given player. :param state: the given state :param white_player: True if the current player is white, False otherwise :return: the x, y coordinates of the squares occupied by the...
def is_seq_list(list_or_dict): """Checks to "seq" key is list or dictionary. If one seq is in the prefix-list, seq is a dictionary, if multiple seq, seq will be list of dictionaries. Convert to list if dictionary""" if isinstance(list_or_dict, list): make_list = list_or_dict else: ...
def link_fix(text, name_dict): """ Replace old file names with new in markdown links. """ new_text = text for old, new in name_dict.items(): new_text = new_text.replace(f']({old})', f']({new})') return new_text
def get_subnet_cidr_block(cidr_block_formatting, instance_index, subnet_suffix): """Get subnet cidr block Args: cidr_block_formatting (string): Cidr block formating instance_index (integer): Instance index subnet_suffix (string): subnet suffix Returns: string: Subnet cidr b...
def vm_mhz_to_percentage(vm_mhz_history, host_mhz_history, physical_cpu_mhz): """ Convert VM CPU utilization to the host's CPU utilization. :param vm_mhz_history: A list of CPU utilization histories of VMs in MHz. :type vm_mhz_history: list(list(int)) :param host_mhz_history: A history if the CPU usa...
def alcohol_by_volume(original_gravity: float, final_gravity: float): """ Calculate the Alcohol By Volume (ABV). """ return (original_gravity - final_gravity) * 131.25
def get_item_filename(omeka_item_dict, hardlinks={}): """ Get filename for a given item. Assumes one file. """ # FIXME need to allow for more than one filename, or how to figure out definitive file for object. filename = omeka_item_dict['filenames'][0] if filename in hardlinks: filename = hardlinks[filename...
def clz(v: int, bits: int) -> int: """ count leading zeroes """ mask = 1 << (bits - 1) count = 0 while (count < bits) and (v & mask) == 0: count += 1 v = v * 2 return count
def split_alnum(s): """ Split line to a sequence of iterating alpha and digit strings :param s: :type s: str :return: list :rtype: list >>> split_alnum("Fa 0/1") ['Fa ', 0, '/', 1] >>> split_alnum("Fa 0/1.15") ['Fa ', 0, '/', 1, '.', 15] >>> split_alnum("ge-1/0/1") ['ge...
def get_list_as_str(list_of_objs): """ Returns the list as a string. """ return '[' + ' '.join([str(x) for x in list_of_objs]) + ']'
def enc_vle(value): """takes an integer value, returns a bytearray""" if value >= 0xFFFFFFFF: print("writing unreasonably large VLE (0d{0})".format(value)) ret = bytearray() while value >= 0x80: value, octet = divmod(value, 0x80) ret.append(octet + 0x80) ret.append(value) ...
def maxloc(seq): """ Return the index of the (first) maximum in seq >>> assert maxloc([1,3,2,3]) == 1 """ return max(enumerate(seq), key=lambda s: s[1])[0]
def get_station_daily_path(station_id): """ Get path to a station daily file. :param station_id: :return: """ return "/pub/data/ghcn/daily/all/{0}.dly".format(station_id)
def nearest_25(num): """ Round the number to the nearest whole number dividable by 25. This will round up or down, to find the closest number Examples: --------- >>> nearest_25(5) 0 >>> nearest_25(25) 25 >>> nearest_25(40) 50 >>> nearest_25(8...
def check_small_primes(n): """ Returns True if n is divisible by a number in SMALL_PRIMES. Based on the MPL licensed https://github.com/letsencrypt/boulder/blob/58e27c0964a62772e7864e8a12e565ef8a975035/core/good_key.go """ small_primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, ...
def ordered(obj): """Return ordered version of the passed object Dictionaries are not ordered in all Python versions, and the implementation of sort_keys() in the the JSON library seems erratic in terms of effect """ if isinstance(obj, dict): return sorted((k, ordered(v)) for k, v in ob...
def get_peer_and_channel(peers, scid): """Look for the channel identified by {scid} in our list of {peers}""" for peer in peers: for channel in peer["channels"]: if channel.get("short_channel_id") == scid: return (peer, channel) return (None, None)
def find_point(coords, point_list): """ Coordinates represent either the source or destination [x,y] coordinates of a line. Given a list of unique points, map one to the given coordinates. """ for p in point_list: if p["coordinates"] == coords: return p["idx"] print("Couldn...
def polynomial1D(x, m, b): """Return the value of a line with slope m and offset b x: the independent variable m: the slope of the line b: the offset of the line """ return (m * x) + b
def straight(ranks): """Return True if the ordered ranks form a 5-card straight.""" return (max(ranks) - min(ranks) == 4) and len(set(ranks)) == 5
def init_model(N, y0): """Initialize the SEIR model. :param int y0: Infected rate at initial time step. :param int N: Population :returns: Model at initial time step :rtype: tuple(int, int, int) """ return N - y0, y0, 0, 0
def next_power_of_2(n): """ Return next power of 2 greater than or equal to n """ return 2**(n - 1).bit_length()
def gcd(a, b): """Returns the greatest common divisor of a and b. Should be implemented using recursion. >>> gcd(34, 19) 1 >>> gcd(39, 91) 13 >>> gcd(20, 30) 10 >>> gcd(40, 40) 40 """ "*** YOUR CODE HERE ***" min_val = min(a, b) # get max and min max_val = max(a...
def reverse_dotted_decimals(ipaddress): """ Reverse the order of the decimals in the specified IP-address. E.g. "192.168.10" would become "10.168.192" """ return '.'.join(ipaddress.split('.')[::-1])
def calc_f(params, strain): """Calculate the elastic free energy in the matrix """ return ( 0.5 * params["c11"] * (strain["e11"] ** 2 + strain["e22"] ** 2) + params["c12"] * strain["e11"] * strain["e22"] + 2 * params["c44"] * strain["e12"] ** 2 )
def extractDidParts(did, method="igo"): """ Parses and returns keystr from did raises ValueError if fails parsing """ try: # correct did format pre:method:keystr pre, meth, keystr = did.split(":") except ValueError as ex: raise ValueError("Malformed DID value") if pre != "...
def sunlight_duration(hour_angle_sunrise): """Returns the duration of Sunlight, in minutes, with Hour Angle in degrees, hour_angle.""" sunlight_durration = 8 * hour_angle_sunrise # this seems like the wrong output return sunlight_durration
def wavenumbers_to_nm(wavenumbers): """wavenumbers from given nm""" return 10**7/wavenumbers
def count_set_bits(n): """Number of '1' bits in binary expansion of a nonnnegative integer.""" return 1 + count_set_bits(n & n - 1) if n else 0
def median(numbers): """Return the median of the list of numbers. found at: http://mail.python.org/pipermail/python-list/2004-December/253517.html""" # Sort the list and take the middle element. n = len(numbers) copy = sorted(numbers[:]) # So that "numbers" keeps its original order if n & 1: ...
def num_decodings(code): """ Given an alphanumeric mapping and an encoded message, count the number of ways it can be decoded """ decodings = [] for idx in range(len(code)): # check one letter before if code[idx] == '0': # zero is special case that maps only to being...