content
stringlengths
42
6.51k
def keyval_list_to_dict(l): """ Converts a list of key=value strings to a dictionary. :param l: the list :return: the dictionary """ stripped = lambda s: s.strip('"').strip("'") d = {} for e in l: keyval = e.split("=", 2) if len(keyval) == 2: d[stripped(keyval...
def example_distribution_config(ref): """Return a basic example distribution config for use in tests.""" return { "CallerReference": ref, "Origins": { "Quantity": 1, "Items": [ { "Id": "origin1", "DomainName": "asdf....
def str_xor(lhs, rhs): """Calculate xor for binary strings.""" if len(lhs) != len(rhs): raise RuntimeError("XOR strings '%s' and '%s' are not of same length") ret = "" for ii, jj in zip(lhs, rhs): if ii == jj: ret += "0" else: ret += "1" return ret
def get_string_or_none(text=None): """ Only return stripped content of text if text is not None and not empty Parameters ---------- text: str string to parse Returns ------- (str, None): content of text """ if text is not None and len(str(text).strip()) > 0: re...
def decodeSurrogatePair(hi, lo): """Returns a scalar value that corresponds to a surrogate pair""" return ((ord(hi) - 0xD800) * 0x400) + (ord(lo) - 0xDC00) + 0x10000
def view_url_to_swagger(url): """ Converts a view URL with `<>` arguments into Swagger-style `{}` arguments. """ return url.replace('<', '{').replace('>', '}')
def KK_RC26(w, Rs, R_values, t_values): """ Kramers-Kronig Function: -RC- Kristian B. Knudsen (kknu@berkeley.edu / kristianbknudsen@gmail.com) """ return ( Rs + (R_values[0] / (1 + w * 1j * t_values[0])) + (R_values[1] / (1 + w * 1j * t_values[1])) + (R_values[2] / (...
def _NamesNotIn(names, mapping): """Returns a list of the values in |names| that are not in |mapping|.""" return [name for name in names if name not in mapping]
def level_to_session(level): """ Converts study level to a year of study. Intended for use with the level descriptions that come out of the HE In Year Cohort web report, but applicable to other instances. Parameters ---------- level : str The text version of a level. Should begi...
def count_stations(list_of_stations): """Technical function to culculate the number of active meteostations. It is used to write data in the logfile Arguments: list_of_stations -- list of stations processed in this session """ list_of_ids = [] for item in list_of_stations: list_of_i...
def obs_to_string(observations): """ Convert observations (or states) to strings for transmission to server. Parameters ---------- observations: list of np.arrays [obs_1, ..., obs_n] which corresponds to the original observations (or states) each np.array obs_i has shape (batch_size) + ...
def extract_configuration_pair(line): """ Extract a configuration pair by splitting on spaces and taking the first couple of values. :param line: a line inside the configuration file :type line: str :return: a key-value pair :rtype: bool """ split = line.split(maxsplit=2) return spl...
def rank_results(results_dict, required_fields, rank_order): """First pick out the required fields from the results dict.""" # Choose the required items # Rank them and ensure no duplicates ranked_results = [] for field in rank_order: if field in required_fields: try: ...
def findmarkedvariables(str1, startmarker, endmarker, ignorelist=[]): """returns all the variables and locations in str1 marked with a given marker""" variables = [] currentpos = 0 while currentpos >= 0: variable = None currentpos = str1.find(startmarker, currentpos) if currentpo...
def clean_parenthesized_string(string): """Produce a clipped substring of `string` comprising all characters from the beginning of `string` through the closing paren that matches the first opening paren in `string` Parameters ---------- string: String A string that contains a parenthesized ...
def path_decoder(url): """Grab the last component of a url as the path.""" components = url.split('/') if components[-1]: return components[-1] else: return components[-2]
def inject_class(value, class_name): """Given raw html, attach the provided class to the first element. This method assumes a class does not already exist on the element. """ desired_index = 0 svg_lines = value.split("\n") for index, line in enumerate(svg_lines): if "<svg" in line: desired_index = index b...
def normalize(string): """Normalize numbers in URL query.""" if string.isnumeric(): return int(string) try: return float(string) except ValueError: return string
def flatten(sequence): """Flatten events in sequence elements to list of events""" return [event for element in sequence for event in element]
def get_bleeding_limbs(life): """Returns list of bleeding limbs.""" _bleeding = [] for limb in life['body']: if life['body'][limb]['bleeding']: _bleeding.append(limb) return _bleeding
def dectohex2(dec_str): """Alternative implementation using BIF""" dec = int(dec_str) return hex(dec)
def king(r, rc, rt, sigma_0, alpha=2): """ See http://iopscience.iop.org/1538-3881/139/6/2097/fulltext/ Parameters ---------- r: float radius rc: float core radius rt: float truncation radius sigma_0: float central density """ def z(x): r...
def right_bisect(sorted_list, element): """ Find an element in a list (from the right) """ idxLeft = 0 idxRight = len(sorted_list) - 1 while idxLeft < idxRight: idxMiddle = (idxLeft + idxRight) // 2 middle = sorted_list[idxMiddle] if middle <= element: idxLeft...
def get_polaris_version(self): """Retrieve deployment version from Polaris Returns: str: Polaris deployment version Raises: RequestException: If the query to Polaris returned an error """ try: query_name = "core_polaris_version" try: response = self._qu...
def convert_list_to_string(list_of_lists): """ Convert a list of lists of strings into a list of string This is a placeholder to use for firing patterns until I recode and rerun sims to use strings rather than lists of strings. This is because lists as data elements don't play nice with pandas. ...
def list_math_substraction_number(a, b): """! @brief Calculates subtraction between list and number. @details Each element from list 'a' is subtracted by number 'b'. @param[in] a (list): List of elements that supports mathematical subtraction. @param[in] b (list): Value that supports math...
def build_output_type_set(output_type_list, config): """Builds set of output types. Args: output_type_list: list, possible output segmentation coord types. config: dict, user passed parameters. Returns: Set of requested output segmenation coord types. """ output_types = set...
def to_dict(dictish): """ Given something that closely resembles a dictionary, we attempt to coerce it into a propery dictionary. """ if hasattr(dictish, "iterkeys"): m = dictish.iterkeys elif hasattr(dictish, "keys"): m = dictish.keys else: raise ValueError(dictish) ...
def create_look_up_table(vocab_list): """Create tables for encoding and decoding texts.""" vocab2int = {word: i for i, word in enumerate(vocab_list)} int2vocab = vocab_list return vocab2int, int2vocab
def flat_clfs(params) -> dict: """Flatten the classifiers for easier access.""" flat = {} for clfs in params["classifiers"].values(): flat.update(clfs) return flat
def _to_gj_multipoint(data): """ Dump a EsriJSON-like MultiPoint object to GeoJSON-dict. Input parameters and return value are the MULTIPOINT equivalent to :func:`_to_gj_point`. :returns: `dict` """ return {'type': 'Multipoint', 'coordinates': [pt for pt in data['points']]}
def selection(input_list): """ Sort a given list using selection sort algorithm and return a that list with the values ascending. """ input_list_length = len(input_list) for i in range(input_list_length): current_smallest_index = i try: for j in range(i, input_list_l...
def _prepend_argument(argument: str, to_prepend: str): """ Prepend as string to an argument :param argument: :param to_prepend: :return: """ return '--{0}-{1}'.format(to_prepend, argument[2:]) if argument[:2] == '--' \ else '-{0}-{1}'.format(to_prepend, argument[1:])
def C3(cls, *mro_lists): """Implementation of the Python's C3 Algorithm. Notes: * The order of items in an MRO should be preserved in all of its future subclasses """ import itertools # Make a copy so we don't change existing content mro_lists = [list(mro_list[:]) for mro_...
def isnumeric(inp): """Check a string is numeric.""" try: float(inp) return True except ValueError: return False
def arch_to_src_arch(arch): """ These conversion are based on Linux' main makefile. """ if arch == 'i386': return 'x86' if arch == 'x86_64': return 'x86' if arch == 'sparc32': return 'sparc' if arch == 'sparc64': return 'sparc' if arch == 'sh64': r...
def match_file(patterns, file): """ Matches the file to the patterns. *patterns* (:class:`~collections.abc.Iterable` of :class:`~pathspec.pattern.Pattern`) contains the patterns to use. *file* (:class:`str`) is the normalized file path to be matched against *patterns*. Returns :data:`True` if *file* matched; ...
def is_object(obj): """ A json object will be an object if it has a: - id - name (or label) *Remember, we're assuming the category from the endpoint used. If a json object contains keys that contain all three fields, then it will be chosen. We also want to extract these values (as ...
def remove_comma(in_str): """ Remove comma from given string """ return str(in_str).replace(",", " ").replace(" ", " ")
def get_annotation_of_violation(annotations, violation): """ Returns the annotation this violation violates. :param annotations: List of annotations. :param violation: The violation that violates the searched annotation. :return: The violated annotation. """ for annotation in annotations: ...
def define_regcontrol_object(regcontrol_name, action_type, regcontrol_info_series, general_property_list=None): """This function is used to create a command string to define an opendss regcontrol object at a given bus. A transformer should already exist at this bus. Regulator control will be placed on this tr...
def convert_file_timestamp(file_time: float, utc=False): """Takes a given file timestamp and converts it to a human-readable format. Examples: >>> import os\n >>> os.stat('.').st_mtime\n 1635892055.433207 >>> mtime = os.stat('.').st_mtime\n >>> convert_file_timestamp(mt...
def get_surrounding_text(text, sub_start, sub_end=None, distance=25): """ Looks for the substrings, 'sub_start' and 'sub_end' variables in 'text' and return a new substring with a number equal to 'distance' characters in the left of 'sub_start' position and in the right of 'sub_end' position if they exi...
def to_user_facing_code(code): """Returns a user-facing code given a raw code (e.g., abcdefghij).""" return '{}-{}-{}'.format(code[:3], code[3:6], code[6:])
def strip_nones(row): """ Remove all items with None for a value, because why include it if there's no value? """ return dict([(k,v) for k, v in row.items() if v is not None])
def concatenate_replacements(text, replacements): """Applies a rewrite to some text and returns a span to be replaced. Args: text: Text to be rewritten. replacements: An iterable of (new_text, start of replacement, end) Returns: A new replacement. """ joined = [] first_start = None last_end ...
def get_collection_id(collection): """Return id attribute of the object if it is collection, otherwise return given value.""" return collection.id if type(collection).__name__ == "Collection" else collection
def add_config(cmd, config): """ Adds config information to cmd either by appending to an existing --config/-C argument or appending to end of command. cmd (list of strings): passed snakemake commands. config (list of strings): additional values to add. """ config_flag = list(set(["--config"...
def bool_to_returncode(success): """Return 0 if |success|. Otherwise return 1.""" if success: print('Success.') return 0 print('Failed.') return 1
def solution(n): """Returns the difference between the sum of the squares of the first n natural numbers and the square of the sum. >>> solution(10) 2640 >>> solution(15) 13160 >>> solution(20) 41230 >>> solution(50) 1582700 """ suma = 0 sumb = 0 for i in range(1...
def get_docker_host_mount_location(cluster_name: str) -> str: """Return host path that Docker mounts attach to.""" docker_mount_prefix = "/tmp/cloudtik_tmp_mount/{cluster_name}" return docker_mount_prefix.format(cluster_name=cluster_name)
def caseinsensitive_sort(stringList): """case-insensitive string comparison sort doesn't do locale-specific compare though that would be a nice addition usage: stringList = caseinsensitive_sort(stringList)""" tupleList = [(x.lower(), x) for x in stringList] tupleList.sort() return [x[1] for...
def hex_to_rgb(h): """returns rgb as int 0-255""" r, g, b = tuple(int(h.lstrip('#')[2*i:2*i+2], 16) for i in range(3)) return r, g, b
def au_to_m(au): """ Converts the input distance (or velocity) of the input from atronomical units to meters. """ return au * 1.495978707 * 10**11
def isFloat(val): """Returns true if the passed value is a float value, otherwise false. NOTE: Returns true only if the value is a float type. See isNum() for a less restrictive test. **Parameters:** * val - value to test **Returns:** True if the passed value is a float, otherwise f...
def _merge_on_last_modified(l1, l2): """ Takes two iterables l1, l2 of objects with a `last_modified` attribute. Assumes the iterables are sorted (desc. order) on this attribute, and merges them into a single sorted list """ il1 = iter(l1) il2 = iter(l2) out = [] try: next_l1 = nex...
def udfize_lambda_string(expression: str): """Given an expression that uses 'input' as a parameter, return a lambda as a string.""" return "lambda input: ({})".format(expression)
def isruddynumeric(val): """ Checks if a value is numeric even if it is a floating point number. Parameters: val(str) Returns: bool """ try: float(val) return True except ValueError: return False
def _mounts_to_in_dict(mounts): """Convert docker-style mounts (external_dir):{docker_dir} into dictionary of external to docker. """ out = {} for m in mounts: external, docker = m.split(":") out[external] = docker return out
def check_not_finished_board(board: list): """ Check if skyscraper board is not finished, i.e., '?' present on the game board. Return True if finished, False otherwise. >>> check_not_finished_board(['***21**', '4?????*', '4?????*', '*?????5', \ '*?????*', '*?????*', '*2*1***']) False >...
def tobool(value): """Convert value to boolean or Not a Number if not possible""" from numpy import nan if value is None: value = nan else: value = bool(value) return value
def capitalize_title(title): """Convert the first letter of each word in the title to uppercase if needed. :param title: str - title string that needs title casing. :return: str - title string in title case (first letters capitalized). """ return title.title()
def bech32_hrp_expand(hrp): """Expand the HRP into values for checksum computation.""" return [ord(x) >> 5 for x in hrp] + [0] + [ord(x) & 31 for x in hrp]
def proba_fail(origin, next_ids, graph): """ Return the probability that the origin should be predicted before all the next_ids (supposing independency)""" prod = 1 for next_id in next_ids: if next_id in graph[origin]: prod *= graph[origin][next_id] else: prod *= 1-gr...
def ts_and_fract_to_float(ts_int,ts_fract): """ takes integral part and a fraction and converts to float :param ts: :return: """ return ts_int + (ts_fract / 1000.0)
def menus(): """ :return: [(action, menu_name)] """ return [ ('cancel_label', 'Cancel the label') ]
def htrc_get_titles(metadata, vol_id): """ Gets titles of the volume given the metadata from a json file and volume id. """ try: md = metadata[vol_id] return md[list(md.keys())[0]]['titles'] except KeyError: print('Volume ID not found:', vol_id) raise
def flag_name(status): """ Determine the name for a flag file of the status indicated. :param str status: Name of status for which to create flag file name. :return str: Name of flag file corresponding to given status. """ return status + ".flag"
def top_row(matrix): """ Return the first (top) row of a matrix. Returns a tuple (immutable). """ return tuple(matrix[0])
def automatic_nch(f_min, f_max, spacing): """How many channels are available in the spectrum :param f_min Lowest frequenecy [Hz] :param f_max Highest frequency [Hz] :param spacing Channel width [Hz] :return Number of uniform channels >>> automatic_nch(191.325e12, 196.125e12, 50e9) 96 >...
def extract_txt(head): """ dummy formatting function """ info = { 'data': { 'head': head, # retain tail for backwards compatibility with client 'tail': [] } } return '', info
def generate_z_array(string): """ This methods takes and string as input and returns Z-array for same :param string: :return: """ n = len(string) z_array = [0] * n left_index, right_index = 0, 0 # [L, R] index for i in range(1, n): if i > right_index: left_index...
def _primary_artist(artist): """ Utitlity function that tries to only get the main artist of a song. Example: "Tyler featuring Xyz" would just return "tyler" """ artist = artist.casefold() artist = artist.split('featuring')[0].strip() artist = artist.split('/')[0].strip() artist = a...
def skip_while(index, max_index, skipping_condition): """Increments |index| until |skipping_condition|(|index|) is False. Returns: A pair of an integer indicating a line number after skipped, and a boolean value which is True if found a line which skipping_condition is False for. """ while sk...
def parse_cstimer_text(text: str) -> list: """ Parses csTimer text. """ return [parse_time(line.split()[1]) for line in text.split("\n")[4:]]
def knot_hash(string): """Calculates the knot hash""" lengths = [ord(x) for x in string] lengths.extend([17, 31, 73, 47, 23]) rope = [x for x in range(0, 256)] rope_length = len(rope) current_position = skip_size = 0 for _ in range(64): for length in lengths: sub_list = [...
def calculate_value(mask, value): """ Calculates value with mask applied. :param mask: mask string :param value: int value :return: int value """ # get binary value as string bin_value = '{0:036b}'.format(value) # replace all values except X for i in range(len(mask)): if ma...
def div_round_up(n, d): """ ceil(n / d) """ return int((n + d - 1) / d)
def generate_slices( example, slice_functions, **kwargs): """Returns (slice_key, example) tuples based on provided slice functions. Args: example: An input example. slice_functions: An iterable of functions each of which takes as input an example (and zero or more kwargs) and returns a list o...
def get_text(prompt="", msg=None, input=input): """Prompt user to enter multiple lines of text.""" print((msg or "Enter text.") + " End with ^D or a '.' as first character.") lines = [] while True: try: line = input(prompt) except EOFError: break if line ...
def create_slices(start, stop, step=None, length=1): """ Generate slices of time indexes Parameters ---------- start : int Index where first slice should start. stop : int Index where last slice should maximally end. length : int Number of time sample included in a given...
def remove_prefix(str, prefix): """Removes a prefix from a string. Args: str (str): Any string to be separated from a prefix. prefix (str): Part to be stripped off the front. Returns: str: String with the prefix removed. If the string doesn't start with the specified pr...
def navbar_toggle(clicks, is_open): """Handle the collapsible function of the navbar Parameters ---------- clicks: int How many times the toggler has been clicked is_open: bool State of the collapsible unit Returns ---------- is_open: bool ...
def _jupyter_server_extension_paths(): """Register julynter server extension""" return [{ "module": "julynter" }]
def non_handling(sentence, mainword, **kwargs): #Done with testing """Delete any word that starts with 'non' or delete any word that comes immediately after the standalone word 'non'. Prevents the term search from making mistakes on words like noncalcified, nontuberculous, noninfectious, etc.""" if ...
def index(List, i): """Get list element via index.""" return List[int(i)]
def is_dataframe_like(df): """ Looks like a Pandas DataFrame """ typ = type(df) return ( all(hasattr(typ, name) for name in ("groupby", "head", "merge", "mean")) and all(hasattr(df, name) for name in ("dtypes", "columns")) and not any(hasattr(typ, name) for name in ("name", "dtype"))...
def base36encode(number: int) -> str: """Convert from Base10 to Base36.""" # Taken from https://en.wikipedia.org/wiki/Base36#Python_implementation chars = "0123456789abcdefghijklmnopqrstuvwxyz" sign = "-" if number < 0 else "" number = abs(number) result = "" while number > 0: numb...
def combine_dicts(dictionary, extras): """ Similar to {**dictionary, **extras} in Python 3 Args: dictionary (dict): A dictionary extras (dict): Another dictionary Returns: dict: A new dictionary with both key and value pairs """ ret = dict(dictionary) ret.update(ext...
def mean(target_value_list): """Calculate the average.""" average = sum(target_value_list) / len(target_value_list) return average
def escapeToXml(text, isattrib = 0): """ Escape text to proper XML form, per section 2.3 in the XML specification. @type text: C{str} @param text: Text to escape @type isattrib: C{bool} @param isattrib: Triggers escaping of characters necessary for use as attribute values ...
def set_size(width, fraction=1, subplots=(1, 1)): """Set figure dimensions to avoid scaling in LaTeX. Parameters ---------- width: float or string Document width in points, or string of predined document type fraction: float, optional Fraction of the width which you wish the...
def cdr(pair): """return the first element of a pair""" def createArray(a,b): array = [] array.append(a) array.append(b) return array #perform closure array = pair(createArray) return array[1]
def gen_output(matrix): """ Converts a 4x4 matrix into a 16-byte array represented as integers. """ return [int(b) for b in bytes(sum(matrix, []))]
def _check(isdsAppliance, serverID, action): """ Check if serverID one of these acceptable values: directoryserver directoryadminserver directorywat directoryintegrator directoryintegratorscimtarget scimservice Note: only directoryserver supports "startconfig...
def tokenize(str_:str, sep=[' ', '\n'], by=["([{", ")]}", "$`'\""], start=0, strip='', keep_empty=True): """ Split the string `str_` by elements in `sep`, but keep enclosed objects not split. Example: ---------- >>> tokenize("function(something inside), something else. ") ["function(somethi...
def bundle_or_dylib_filter(module): """ Return False if the module does not have a filetype attribute corresponding to a Mach-O bundle or dylib """ return getattr(module, 'filetype', None) in ('bundle', 'dylib')
def merge(L1: list, L2: list) -> list: """Merge sorted lists L1 and L2 into a new list and return that new list. >>> merge([1, 3, 4, 6], [1, 2, 5, 7]) [1, 1, 2, 3, 4, 5, 6, 7] """ newL = [] i1 = 0 i2 = 0 # For each pair of items L1[i1] and L2[i2], copy the smaller into newL. while not ...
def polyreduce(a, root): """ Given x = r is a root of n'th degree polynomial p(x) = (x-r)q(x), divide p(x) by linear factor (x-r) using the same algorithm as polynomial evaluation. Then, return the (n-1)'th degree quotient q(x) = polyreduce(a, r) = c[0] + c[1]x + c[2]x^2 +...+ c[n-2]x^{n-2} + c[n-1]x^{n-1} ...
def transform_objects(objects): """Transform objects.""" obj_list = [] for i, obj in objects.items(): data = dict(obj, instance_id=i) if data['destroyed'] is None: data['destroyed_x'] = None data['destroyed_y'] = None obj_list.append(data) return obj_list