content
stringlengths
42
6.51k
def find_new_news(excluding_seen_news, existing_news): """ Finds new news from the API that is not currently in the list """ # https://stackoverflow.com/questions/7271482/getting-a-list-of-values-from-a-list-of-dicts existing_news_titles = list(map(lambda d: d['title'], existing_news) ) return...
def consecutiveDistanceByProbability(r1,r2,p,xcontact=2): """ Upper bound distance constraints for consecutive domains return surface to surface distance. Parameters ----------- r1,r2 : int Radius for beads p : float Probability for contact xcontact : int scaling of ...
def _merge_clouds(old_dict, new_dict): """Like dict.update, except handling nested dicts.""" ret = old_dict.copy() for (k, v) in new_dict.items(): if isinstance(v, dict): if k in ret: ret[k] = _merge_clouds(ret[k], v) else: ret[k] = v.copy() ...
def cbrt(x): """Cube root.""" if 0 <= x: return x ** (1.0 / 3.0) return -(-x) ** (1.0 / 3.0)
def locales(resp_json, return_obj, options): """Locations in space and time at which data was collected.""" for rec in resp_json: data = dict() data.update(db='sead') data.update(locale_id='sead:loc:{0:d}'.format(rec.get('locale_id'))) if rec.get('doi'): ...
def find_maxima(x): """Find local maxima of x. Example: >>> x = [1, 2, 3, 2, 4, 3] >>> find_maxima(x) [2, 4] Input arguments: x -- 1D list of real numbers Output: idx -- list of indices of the local maxima in x """ if type(x) != type([]): message = 'Input argument...
def find_heads(own_snake, data): """ Finds coordinates of all the heads of enemy snakes :param own_snake: :param data: :return: list of coordinates of heads """ enemy_heads = [(snake['body'][0]['x'], snake['body'][0]['y']) for snake in data['board']['snakes'] if snake['id'...
def reject_bad_peaks(peaks): """ Go through the list of peaks and remove any that look really suspicious. I refer to this colloquially as "forensic accounting". Parameters ---------- peaks: sequence of 2-tuples: peak location and "strength" (e.g., SNR) of peaks Returns ------- ...
def get_tasks(file): """Heuristic method to find tasks within a yml/yaml file""" value_object_list = [] name_found_list = [] for element in file: if isinstance(element, dict): if 'name' in element: if 'tasks' in element: tasks_list = element['tasks...
def CreateStandbyPolicy(messages, initial_delay_sec): """Creates standby policy from args.""" # pylint: disable=g-explicit-bool-comparison if initial_delay_sec is None: return None return messages.InstanceGroupManagerStandbyPolicy( initialDelaySec=initial_delay_sec)
def generate_args(num): """ Returns questionmarks separated by commas as a string to be used by the insert command. """ args_str = '' for i in range(0, num): if i == num - 1: args_str += '?' else: args_str += '?,' return args_str
def nukenewlines(string): """Strip newlines and any trailing/following whitespace; rejoin with a single space where the newlines were. Bug: This routine will completely butcher any whitespace-formatted text.""" if not string: return '' lines = string.splitlines() return ' '.join( [...
def sort_reviews_by_last_updated(reviews): """Sort reviews in ascending order by last update date.""" return sorted(reviews, key=lambda review: review['lastUpdated'])
def replace_with_white(board: list): """ Replaces white boxes with '*' >>> board = [\ "*1** ****",\ "***1 ****",\ "** 3****",\ "* 4 1****",\ " 9 5 ",\ " 6 83 *",\ "3 1 **",\ " 8 2***",\ " 2 ****"] >>> replace_with_white(board)[0] '**** ****' """...
def dedupe_list(input): """Remove duplicates from the given list""" return list(set(input))
def make_filebody(number_of_lines: int) -> str: """ Return a string representing the contents of a file with a given number of lines. Only used for testing purposes. """ body = '' for i in range(0, number_of_lines): body += '{n}/{c}: line\n'.format(n=i, c=number_of_lines) return ...
def transform_suffix(filenames, suffix_old, suffix_new): """This function removes the suffix from every name in the set filenames and returns a set with the new file names inputs: filenames - a set with all the filenames suffix_old - the suffix to be removed from the filenames ...
def containsAny(string, char_set): """Check whether 'str' contains ANY of the chars in 'set'""" return 1 in [c in string for c in char_set]
def confopt_float(confstr, default=None): """Check and return a floating point number.""" ret = default try: ret = float(confstr) except Exception: pass return ret
def get_model_config(config, model_id): """ Return config of a single model """ for model_config in config["models"]: if model_config["model_id"] == model_id: return model_config
def maybe_add(d, exclude_false=False, **kws): """ Adds keywork argumnts to a dict if their values are not None. Parameters ---------- d: dict The dictionary to add to. exclude_false: bool Exclue keys whose values are false. kws: dict The keys to maybe add """ ...
def get_all_possible_mappings(orig, alt): """ Creates a list of lists that contains the mappings of the items in orig to all possible indexes from alt. :param orig: a list of items :param alt: a list of items :return: a list of lists, each element represents a list of indexes in the altered list that the origina...
def get_fields_with_datatype( dataset_ids: list, field_info: dict, data_format: str ) -> dict: """ Returns the column name and its data type by looking up stream and log_fields json file. If a mapping column name is not present in log_fields.json it returns the column name as `col<idx>` and its dat...
def mapping_linkedin_user_info_to_sso_user_info( cognito_id, cognito_email, linkedin_user_info ): """ Map the LinkedIn ID token info to the user info. """ sso_user_info = dict() sso_user_info["cognito_id"] = cognito_id sso_user_info["cognito_email"] = cognito_email sso_user_info["federat...
def rule_width(value: float, layer: str, angle_limit: float = 90) -> str: """Min feature size""" category = "width" error = f"{layer} {category} {value}um" return ( f"{layer}.{category}({value}, angle_limit({angle_limit}))" f".output('{error}', '{error}')" )
def human_bytes(byte: int, precision: int = 2) -> str: """Return a human readable version of the byte amount""" kilo_byte = 10 ** 3 mega_byte = 10 ** 6 giga_byte = 10 ** 9 tera_byte = 10 ** 12 if byte >= tera_byte: return f"{round(byte / tera_byte, precision)} TB" elif byte >= giga_b...
def simple_preproc(str_list, identifier, unifier): """Simple preprocessing applied only for metadata""" try: tokens = [token.strip() + identifier if not token == '' else '' for token in str_list.split(';')] re_unified = unifier.join(tokens) return re_unified except Exception: ...
def normalize_metadata(metadata_value): """Normalize the metadata received from the form.""" # Removing closing comments. if metadata_value is None: return None if '-->' in metadata_value: metadata_value = metadata_value.replace('-->', '') metadata_value = normalize_metadata(meta...
def replace_message_sig(message, sig_text): """Helper method to do a string replacement.""" return message.replace("SIG", sig_text)
def update_arg(original, new_val, name, log=False): """ Decide if update value or keep the original :param original: :param new_val: :param name: name of the variable :param log: decide if print or not :return: """ if new_val is None: out_val = original else: out_...
def text_to_sentences(text, sentence_sep="\n\n", word_sep="\n", label_sep="\t"): """ Converts text to sentences (a list of list of word) and labels. """ def word_label_condition(word_label): """ When words and labels are taken into account """ c1 = label_sep in word_label...
def get_locator(*values): """ Gets the first available locator. :rtype: :class:`aria.parser.reading.Locator` """ for v in values: if hasattr(v, '_locator'): locator = v._locator if locator is not None: return locator return None
def merge_dict_overwrite_first(dict1, dict2): """Desired result is a new dictionary with the values merged, and the second dict's values overwriting those from the first in pythonic syntax.""" return {**dict1, **dict2}
def set_num_precision(number, precision, mode='int'): """ Return the input number with N digits of precision. Args: number: Input value. precision: Number of digits of desired precision. Returns: Input value with 'precision' digits of precision. """ fmt = '{:.%ie}' % (p...
def gap_right(hist, x, th, W, K=10): """ Checks if this x-coordinate marks the end of a word/block. :param hist: distribution of pixels. :param x: x-coordinate. :param th: threshold value. :param K: number of columns of empty pixels to consider as new word/block. :return: whether this x-coor...
def abc_q2d(n, m): """A, B, C terms for 2D-Q polynomials. oe-20-3-2483 Eq. (A.3). Parameters ---------- n : `int` radial order m : `int` azimuthal order Returns ------- `float`, `float`, `float` A, B, C """ # D is used everywhere D = (4 * n ** 2 - ...
def dot_prod(a, b): """Returns the dot vector product of two lists of numbers""" if type(a) is not list: raise ValueError(f"Incorrect parameter type: {type(a)}") elif type(b) is not list: raise ValueError(f"Incorrect parameter type: {type(b)}") else: return sum([a[i] * b[i] for i...
def __compute_next(x: int, g: int, y: int, a: int, b: int, p: int, order: int): """ Computes x_(i+1), a_(i+1), b_(i+1) from x_i, a_i, b_i :param x: x_i in the random walk. Positive integer. :param g: Generator g of g^x = y (mod p). Positive integer. :param y: y of g^x = y (mod p). Positive integer....
def to_int32(f): """ Convert an image to an int32 image. img = to_int32(f) `to_int32` clips the input image between the values -2147483648 and 2147483647 and converts it to the signed 32-bit datatype. Parameters ---------- f : Any image Returns ------- img : The converted...
def B(A: dict) -> int: """Function that depends on A, but says it's a primitive type dict.""" return A['a'] + 1
def squaremeters_to_ha(value): """.""" tmp = value/10000. return float('{0:4.2f}'.format(tmp))
def _relative_likes(favorite_stories, inverted_favorites, fandom): """ .. versionadded:: 0.3.0 Returns how many stories a user likes in a fandom over all fandoms that they like. :param favorite_stories: List of story-ids liked by a user. :type favorite_stories: list. :param inverted_favori...
def prepare_mdtau(nrot, jobs): """ Returns what mdtau should be set to based on the number of hindered rotors and inserts MdTau into the joblist if need be """ mdtau = None if nrot > 0 and '1dTau' in jobs: mdtau = '1' if nrot > 1: mdtau = '2' if nrot > ...
def get_toll_path(toll, point, color): """ Get into to plot a path in mapbox from toll to point :param toll: :param point: :param color: :return: Dictionary info to plot in mapbox a line """ return { 'lat': [toll['lat'], point['lat']], 'lon': [toll['long'], point[...
def lat_lon_2_distance(lat1, lon1, lat2, lon2): """ return distance (in km) between two locations (lat1, lon1) and (lat2, lon2) parameter --------- lat1, lat2: latitude in degrees lon1, lon2: longitude in degrees return ------ distance in km """ from...
def _ShouldTreatDstUrlAsSingleton(src_url_names_container, have_multiple_srcs, have_existing_dest_subdir, dst_url, recursion_requested): """Checks that dst_url names a single file/object after wildcard expansion. It is possible that an object path...
def load(obj, cls, default_factory): """Create or load an object if necessary. Parameters ---------- obj : `object` or `dict` or `None` cls : `type` default_factory : `function` Returns ------- `object` """ if obj is None: return default_factory() if isinstance(...
def _create_metadata_row_from_string(element_label, element_string): """ create row of metadata to write """ row = {} row['Metadata_label'] = element_label row['Metadata_value'] = element_string return row
def to_bytes(text, encoding='utf-8'): """Make sure text is bytes type.""" if not text: return text if not isinstance(text, bytes): text = text.encode(encoding) return text
def first_in_parens(s): """ Returns: The substring of s that is inside the first pair of parentheses. The first pair of parenthesis consist of the first instance of character '(' and the first instance of ')' that follows it. Examples: first_in_parens('A (B) C') returns 'B' first_in_pa...
def massage_link(linkstring): """Don't allow html in the link string. Prepend http:// if there isn't already a protocol.""" for c in "<>'\"": linkstring = linkstring.replace(c, '') if linkstring and linkstring.find(':') == -1: linkstring = 'http://' + linkstring return linkstring
def change_ratio(data): """Calculates the ratio N+1/N for each element in data""" d = data[:] for idx, trace in enumerate(data): ratio = [] derivative = [a-b for a,b in zip(trace[1:], trace[:-1])] cumulative = trace[1:] for a, b in zip(derivative, cumulative): t...
def validate_subnets(value): """Raise exception if subnets fail to match length constraint.""" for subnet_id in [x["SubnetId"] for x in value]: if len(subnet_id) > 32: return "have length less than or equal to 32" return ""
def sort_coords(line): """ order set point of the line """ a, b, c, d = line if b > c: a, b, c, d = c, d, a, b return [a,b,c,d]
def disabled_payments_notice(context, addon=None): """ If payments are disabled, we show a friendly message urging the developer to make his/her app free. """ addon = context.get('addon', addon) return {'request': context.get('request'), 'addon': addon}
def xml_format(tag): """Ensures that tag is encapsulated inside angle brackets.""" if tag[0] != "<": tag = "<" + tag if tag[-1:] != ">": tag += ">" return tag
def make_expr_sig(args = None): """create experiment signature string from args and timestamp""" import time # print ("make_expr_sig", args) # infile = args.infile.replace("/", "_").replace(".wav", "") # expr_sig = "MN%s_MS%d_IF%s_IS%d_NE%d_EL%d_TS%s" % (args.mode, args.modelsize, infile, args.samp...
def remove_padding(data: bytes) -> bytes: """ Removes ISO/IEC 9797-1 Padding method 2 from data """ for idx, b in enumerate(reversed(data), start=1): if b == 0x80: return data[: len(data) - idx] return b""
def remove_tag_from_name(name, tag): """Removes a tag from a name given as a string.""" name = name.split("/")[-1].split(".")[0] if name == tag: return tag else: return name.replace(tag, "")
def snake_case_to_headless_camel_case(snake_string): """Convert snake_case to headlessCamelCase. Args: snake_string: The string to be converted. Returns: The input string converted to headlessCamelCase. """ return ''.join([snake_string.split('_')[0]] + list(sub_string.capitalize() ...
def test_closure(a): """This is the closure test in the paper.""" def x1(b): def x4(c): return b return x4 x2 = x1(a) x3 = x2(1) return x3
def ensure_trailing_slash(path): """Return path if path ends with a / or path + / if it doesn't.""" return path if path.endswith('/') else path + '/'
def stats(data): """ Assumes a matrix of data with variables on the columns and observations on the rows. Returns the mean, variance and standard error of the data. """ from numpy import average,sqrt mean = average(data) var = average((data-mean)**2) stderr = sqrt(var)/sqrt(len(dat...
def score_silence(item): """Compute a goodness score for a silent audio span (start, end). Used by `find_spoken_wordspans()` to find where to split phrases. """ start, end = item # We definitely want to split at the first and last detected silent span, # since these indicate the beginning and e...
def _ipVersionToLen(version): """Return number of bits in address for a certain IP version. >>> _ipVersionToLen(4) 32 >>> _ipVersionToLen(6) 128 >>> _ipVersionToLen(5) Traceback (most recent call last): File "<stdin>", line 1, in ? File "IPy.py", line 1076, in _ipVersionToLen ...
def game_result(player, players, value=1): """ Retorna un dict con todos los jugadores menos jugador con resultado -valor. El resultado de jugador se ajusta para que la suma de resultados sea cero. Por defecto (valor=1) marca a jugador como ganador. Para marcar a jugador como perde...
def to_lowercase(text): """ Transform all characters to lowercase :param text: input text :return: transformed text """ return text.lower()
def get_selected_action(body): """Get selected value from an action event""" if body["actions"] is not None and len(body["actions"]) > 0: return body["actions"][0]["selected_option"]["value"]
def default(data, dflt): """ Helper function for setting default values. Args: data: dflt: Returns: """ if data is None: return None elif isinstance(data, (list, tuple)): newdata = [] allnone = True for x in data: if x is N...
def set_required(field, render_kw=None, force=False): """ Returns *render_kw* with *required* set if the field is required. Sets the *required* key if the `required` flag is set for the field (this is mostly the case if it is set by validators). The `required` attribute is used by browsers to indic...
def isNumber(s): """ Checks to see if this is a JSON number """ try: float(s) return True except ValueError: return False
def remove_sub_strings(string, substrings): """Strips substrings from a given string.""" result = string for substring in substrings: result = result.replace(substring, '') return result
def _(arg): """If the lowercase string is 't' or 'true', return True else False.""" argument = arg.lower().strip() return argument == 'true' or argument == 't'
def find_largest_digit_helper(max_digit, current): """ This function will help the user find the largest digit in a certain number by continuously dividing 10 to get every digit within the number. :param max_digit: int, An initial value for comparison. :param current: int, Each digit found in a certain number. :r...
def compare_dicts(first, second, fine_if_wrong=None): """Compare two dictionaries, based on their key, value pairs.""" total_keys = len(set(first.keys()) | set(second.keys())) if fine_if_wrong is None: fine_if_wrong = set() resemblance = 0 differences = dict() for key, value in first.items(): ...
def multiply_tuple(row_obj): """Demonstrates an object formatter function""" return str(row_obj[2] * row_obj[3])
def get_linked_edges(verts, filter_edges): """ Find all the edges linked to verts that are also in filter edges """ linked_edges = [e for v in verts for e in v.link_edges] return list(filter(lambda e: e in filter_edges, linked_edges))
def computeSimpleInterest(principal, rateInPercent, timeInYears): """Computes and returns simple interest""" interest = 0 if timeInYears < 0 or rateInPercent < 0: return interest interest = principal * rateInPercent * timeInYears / 100 return interest
def shrink_to_hint(s: str): """ Shrink a string to hint. :param str s: Source string :return: str """ length = len(s) if length < 4: return '*' * length return '{}**{}'.format(s[0], s[length - 1])
def deep_update( original, new_dict, new_keys_allowed=False, whitelist=None, override_all_if_type_changes=None, ): """Updates original dict with values from new_dict recursively. If new key is introduced in new_dict, then if new_keys_allowed is not True, an error will be thrown. Further, for sub-dicts, ...
def wrap_360(valin): """ Wraps a value (float) to a 0--360 degree range. Parameters ---------- valin: float Input value in degrees Returns ------- valout : float Example ------- # e.g., 370 degrees is equivalent to 10 degrees. \n obs.wrap_360(370...
def remove_numbers(words): """Remove all numbers from word list. Unlike words, numbers without any context are not expected to provide any explanatory value for topic classification. Parameters ---------- words : string Original word-token list Returns ------- new_words : list of strings List with all...
def strictly_increasing(L): """Copied from accepted answer to this: https://stackoverflow.com/questions/4983258/python-how-to-check-list-monotonicity """ return all(x < y for x, y in zip(L, L[1:]))
def stats_tree(keys): """Given a bunch of keys, make a tree such that similarly prefixed stats are at the same level underneath eachother.""" keys = list(keys) def reduce_tree(tree): keys = list(sorted(tree.keys())) for k,v in tree.items(): if '.' not in k and not v: ...
def nraphson(f, df, x0, tol=1e-3, maxit=20000): """ Newto Raphson Equation Solving Compute Equation roots given its equation and derivate Xn = Xn-1 - f(x)/f'(x) :param f: Function f(x) handler :param df: Derivate of f'(x) handler :param x0: Inital guess :param maxit: ...
def get_starttag_string(name, attrs, save_attrs=True): """Returns string representation of start tag. Args: name: Name of tag. attrs: Attributes of tag -> ((attr1, value), ...). save_attrs: If it is True, attributes of tag will be save. """ tag_content = [name] if save_attr...
def wraplines(s, wrap=60): """ Wrap lines Args: s: String wrap: Number of characters per line Returns: str: String with newlines. """ return '\n'.join(s[i:(i+wrap)] for i in range(0, len(s), wrap))
def do_digest_auth(id): """ A function that performs Digest authentication by comparing the requested user name and password name with its own DB. """ id_list = {"admin": "admin", "testUser01": "testUser01"} if id in id_list: return id_list.get(id) return None
def _json_date(date=None): """Given a db datetime, return a steemd/json-friendly version.""" if not date: return '1969-12-31T23:59:59' return 'T'.join(str(date).split(' '))
def _filter_overlap(index): """Filter indexes in list if they overlap.""" if len(index) < 2: return index index_f = [] i = 0 j = i + 1 while j < len(index): if index[i][1] > index[j][0]: index[i] = (min(index[i][0], index[j][1]), max(index[i][0...
def plato_to_gravity(degrees_plato: float) -> float: """ Convert degrees plato to gravity. """ return 259.0 / (259.0 - degrees_plato)
def _get_ref(val: str, start: int) -> tuple: """Get the next reference starting from `start` position""" idx = start length = len(val) in_ref = False ref_name = '' while idx < length: ch = val[idx] if in_ref: # found end of ref, yeild if ch == '}': ...
def find_min_op(a: int, b: int, n: int): """ """ count = 0 while b <= n: a, b = max(a, b), b+a count += 1 return count
def apply_filters(genes, records, db_info, filters): """Apply predefined and additional filters. Args: genes: list of Gene objects records: list of records as Variant objects db_info: database configuration as Config object filters: list of filters as Filter objects """ ...
def get_station_type(station_name: str) -> str: """ Get the station type, one of 'intl', 'core' or 'remote' Args: station_name: Station name, e.g. "DE603LBA" or just "DE603" Returns: str: station type, one of 'intl', 'core' or 'remote' Example: >>> get_station_type("DE603"...
def cap(value: float, minimum: float, maximum: float) -> float: """Caps the value at given minimum and maximum. Arguments: value {float} -- The value being capped. minimum {float} -- Smallest value. maximum {float} -- Largest value. Returns: float -- The capped value or the...
def make_token_dict(sentences, pad_token=None, extra_tokens=None): """Extract tokens from tokenized *sentences*, remove all duplicates, sort the resulting set and map all tokens onto ther indices. :param sentences: tokenized sentences. :type sentences: list([list([str])]) :param pad_token: add a to...
def get_repo_content_path(data: dict): """ Return all content path :param data: :return: list of all paths: :rtype: list: """ paths = [] for key, files in data.items(): for file in files: if isinstance(file, dict): for k, v in file.items(): ...
def init_method_normalizer(name: str): """Normalizes the name of an initialization method.""" return name.lower().replace('_', '').replace('nodeembeddinginitializer', '')
def big_t_abt(s: int) -> int: """T_ABT from KPT21""" if s == 1: return 1 elif (s & (s - 1) == 0) and s != 0: """If s=2^i for some i""" return int(s / 2) else: return 0