content
stringlengths
42
6.51k
def all_black_colors(N): """ Generate all black colors """ black = [(0, 0, 0) for i in range(N)] return black
def utmify_email_url(url: str, campaign: str) -> str: """ Returns a versioned absolute asset URL (located within PostHog's static files). Example: {% utmify_email_url 'http://app.posthog.com' 'weekly_report' %} => "http://app.posthog.com?utm_source=posthog&utm_medium=email&utm_campaign=week...
def sign_extend(val, bits=32): """ Sign extension. High-order bit of val is left extended. :param val: VexValue """ sign_bit = 1 << (bits - 1) return (val & (sign_bit - 1)) - (val & sign_bit)
def unescaper(msg): """ unescape message this function undoes any escape sequences in a received message @param msg: the message to unescape @return: the unescaped message """ out = [] escape = False for x in msg: if x == 0x5c: escape = True continue ...
def weight_point_in_circle( point: tuple, center: tuple, radius: int, corner_threshold: float = 1.5 ): """ Function to decide whether a certain grid coordinate should be a full, half or empty tile. Arguments: point (tuple): x, y of the point to be tested ...
def lc_prefix(prefix, s): """return the longest common prefix of prefix and s""" if prefix is None: return s n = 0 for i, j in zip(prefix, s): if i != j: break n += 1 return prefix[:n]
def fill_tabs(string: str): """Replaces every occurence of \\t with four spaces""" return string.replace("\t", " ")
def count_keys(num_clbits): """Return ordered count keys.""" return [bin(j)[2:].zfill(num_clbits) for j in range(2 ** num_clbits)]
def custom_format(source, language, class_name, options, md, **kwargs): """Custom format.""" return '<div lang="%s" class_name="class-%s", option="%s">%s</div>' % (language, class_name, options['opt'], source)
def get_venv_name(dockenv_name): """ Helper function to split the user-defined virtual env name out of the full name that includes the dockenv tag :param dockenv_name: The full name to pull the virtual env name out of """ # Format of tage name if dockenv-**NAME**:latest...
def reducemap(func, sequence, initial=None, include_zeroth = False): """ A version of reduce that also returns the intermediate values. :param func: A function of the form x_i_plus_1 = f(x_i, params_i) Where: x_i is the value passed through the reduce. params_i is the i'th el...
def content_to_md(content, ref_count, faulty_pdfs): """ Returns the content (reference list for each PDF file) in a markdown formated string Keyword arguments: content -- a dict with each key being a PDF filepath and reference list (str) as value ref_count -- a dict with each key being a PDF filepath a...
def _FindLatestMinorVersion(debuggees): """Given a list of debuggees, find the one with the highest minor version. Args: debuggees: A list of Debuggee objects. Returns: If all debuggees have the same name, return the one with the highest integer value in its 'minorversion' label. If any member of the...
def recall_score(true_entities, pred_entities): """Compute the recall.""" nb_correct = len(true_entities & pred_entities) nb_true = len(true_entities) score = nb_correct / nb_true if nb_true > 0 else 0 return score
def map_clobs(columns): """ The map_clobs function will inject the following if there are clob data types. --map-column-java col6=String,col8=String This is needed as clob columns will not come into Hadoop unless we specifically map them. """ hasclobs = False clobs = "" for c in colu...
def search_for_pod_name(details: dict, operator_id: str): """Get operator pod name. Args: details (dict): workflow manifest from pipeline runtime operator_id (str): operator id Returns: dict: id and status of pod """ try: if 'nodes' in details['status']: ...
def iterate(iterator, n): """ Returns the nth element returned by the iterator if there are sufficient elements, or None if the iterator has been exhausted. :param iterator: The iterator to extract elements from :param n: The nth element returned by the iterator will be returned :return: An ele...
def blend(c, a): """blend semi-transparent color with white""" return 255 + (c - 255) * a
def merge(left, right): """Two lists are merged.""" results = [] while len(left) and len(right): if left[0] < right[0]: results.append(left.pop(0)) else: results.append(right.pop(0)) return results + left + right
def check_if_similarity_is_valid(similarity): """Raise error if not true""" similarity_error_message = "Invalid value, it should be between 0.0 and 1.0." try: similarity = float(similarity) except ValueError: raise ValueError(similarity_error_message) if not isinstance(similarity, f...
def normalize(position): """Accepts `position` of arbitrary precision and returns the block containing that position. Parameters ---------- position : tuple of len 3 Returns ------- block_position : tuple of ints of len 3 """ x, y, z = position x, y, z = (int(round(x)), int...
def clean_str( s: str, l: list, r: list, ) -> str: """Replace substrings within a given string Parameters ---------- s : str The string l : list The list of substrings r : list The list of replacement substrings Returns ------- str Th...
def _tf2idxf(tf_d, tidx_d): """ _tf2idxf(): Don't use it directly, use tf2idxf instead. This function is getting a TF dictionary representing the TF Vector, and a TF-Index. It returns a Index-Frequency dictionary where each term of the TF dictionary has been replaced with the Index number of...
def vni_row_name(network_type): """Determine name of row for VNI in allocations table. :param network_type: Network type to determine row name for. :type network_type: str :returns: Row name :rtype: str :raises: ValueError """ if network_type in ('gre',): return '{}_id'.format(n...
def Command(*_args, **_kw): """Fake Command""" return ["fake"]
def space_tokenize_with_eow(sentence): """Add </w> markers to ensure word-boundary alignment.""" return [t + "</w>" for t in sentence.split()]
def highest(x, a, b=0): """Calculate the highest sum of X.""" a, b = a + x, b + x return a if a > b else b
def _first_unvisited(graph, visited): """ Return first unvisited node. @type graph: graph @param graph: Graph. @type visited: list @param visited: List of nodes. @rtype: node @return: First unvisited node. """ for each in graph: if (each not in visited): ...
def unpack(dict_of_list, dict_): """ Unpack predictions into a dictionary. Dictionary keys are keys of *dict_* """ ret = {k: [None] * len(v) for k, v in dict_of_list.items()} for k, p in dict_.items(): ret[k[0]][k[1]] = p return ret
def add_word_continuation_tags(tags): """In place, add a continutation tag to each word: <cc/> -word continues current dialogue act and the next word will also continue it <ct/> -word continues current dialogue act and is the last word of it <tc/> -word starts this dialogue act tag and the ne...
def append_cmd_line_args_to(cmd): """Appends the syntax for command line arguments ("$*") to the cmd Args: cmd: A string representing the command. Returns: 'cmd' with the syntax for command line arguments ("$*") appended """ return cmd + " $*"
def parse_input(trace_doc, dbg_path_resolver): """Return a list of frame dicts from an object of {backtrace: list(), processInfo: dict()}.""" def make_base_addr_map(somap_list): """Return map from binary load address to description of library from the somap_list. The somap_list is a list of di...
def _num_32_bit_words_for_bit_fields(bit_fields): """ Gets the number of 32 bit unsigned ints needed store a list of bit fields. """ num_buckets, cur_bucket = 0, 0 for field in bit_fields: if field.size + cur_bucket > 32: num_buckets += 1 cur_bucket = 0 cur_bu...
def short(products, field): """Creates a bash completion list for data Parameters ---------- products: list list of products data field: str the field name that should be formated Returns ------- str formated string with unique fields and spces converted to unde...
def unique_list(obj, key=None): """ Remove same element from list :param obj: list :param key: key function :return: list """ checked = [] seen = set() for e in obj: _e = key(e) if key else e if _e not in seen: checked.append(e) seen.add(_e) ...
def get_cookie_name(request, name, usage, software='MOIN'): """ Determine the full cookie name for some software (usually 'MOIN') using it for some usage (e.g. 'SESSION') for some wiki (or group of wikis) determined by name. Note: ----- We do not use the path=... information in the cookie a...
def truncate_errors(errors, limit=float("inf")): """If limit was specified, truncate the list of errors. Give the total number of times that the error was found elsewhere. """ if len(errors) > limit: start1, end1, err1, msg1, replacements = errors[0] if len(errors) == limit + 1: ...
def journey_data_years(all_y = False): """Return available journey data years Either return all the years, if all_y = True, or just those that are relevant from the TTM viewpoint. """ if not (all_y): return (2012, 2014, 2015, 2016) else: return (2007, 2009, 2010, 2012, ...
def unpack_experiment_groups(experiment_groups): """ """ experiments = [] for experiment_group in experiment_groups: name = experiment_group["name"] for experiment_dir in experiment_group["dirs"]: experiments.append({ "name": name, "dir": exper...
def convert_boolean(value: bool) -> str: """Convert from python bool to postgresql BOOLEAN""" if value is True: return 'TRUE' elif value is False: return 'FALSE' else: return 'NULL'
def find_largest_digit(n): """ A recursive function for comparing the digits :param n: int, several digits :return: int, the biggest digit """ # Confirm that the parameter is a positive number if n < 0: num = n*-1 else: num = n # Base case, the first digit is compared if num//10 == 0: return num%10 # C...
def zbin(zval): """ return bin index for this track -- so that we take at most 1 track/bin/jet """ zmax = [0.1, 0.2, 0.3, 0.4, 0.5, 0.7, 1.0] for i,z in enumerate(zmax): if zval < z: return i+1
def create_context_element_attribute(name, type, value): """ Function to create a context attribute body :param name (string): Name of the attribute :param type (string): Type of the attribute :param value (string): Value of the attribute :return (dict): Contest attribute body. The attribute in ...
def producttype_to_sat(product_type: str) -> str: """Returns the satellite for the given product_type. Args: product_type: The type of the input satellite product (e.g. S2_ESA_L2A or L8_USGS_L1) Returns: The name of the satellite that matches the input product_type. """ ...
def game_over(current_board): """ Brain of the game. Goes through the classical rules of tic-tac-toe game. Inspects the current board and checks if the horizontal, vertical or diagonal elements are same or not. Also decides explicitly whether the game is draw or not. :param current_board: the cu...
def ilen(iterable): """Return the number of items in ``iterable``.""" return sum(1 for _ in iterable)
def flatten(d): """Return a dict as a list of lists. >>> flatten({"a": "b"}) [['a', 'b']] >>> flatten({"a": [1, 2, 3]}) [['a', [1, 2, 3]]] >>> flatten({"a": {"b": "c"}}) [['a', 'b', 'c']] >>> flatten({"a": {"b": {"c": "e"}}}) [['a', 'b', 'c', 'e']] >>> flatten({"a": {"b": "c", "...
def appenddir(path: str, dirname: str) -> str: """ Append a dirname to a path string :param str path: path where to append :param str dirname: path to be appended :return: path/dirname """ if path.endswith('/'): return '{}{}'.format(path, dirname) else: return '{}/{}'.fo...
def convert_device_name(framework, device_ids): """Convert device to either cpu or cuda.""" gpu_names = ["gpu", "cuda"] cpu_names = ["cpu"] if framework not in cpu_names + gpu_names: raise KeyError("the device should either " "be cuda or cpu but got {}".format(framework)) ...
def lorentz_berthelot_rule(sig_A, sig_B, eps_A, eps_B): """ Lorentz-Berthelot rules for combining LJ paramters. Parameters ----------- sig_A: float sig_B: float input sigma values eps_A: float eps_B: float input epsilon values Returns -------- float ...
def bytes_to_uint8_array(val, width=70): """ Formats a byte string for use as a uint8_t* literal in C/C++ """ if len(val) == 0: return '{}' lines = [] line = '{' + str(ord(val[0])) for x in val[1:]: token = str(ord(x)) if len(line) + len(token) > width: l...
def tags_equal(act, exp): """Check if each actual tag in act is equal to one or more expected tags in exp.""" return all(a == e if isinstance(e, str) else a in e for a, e in zip(act, exp))
def get_parse_context(i, deps, data): """ get the two elements from data at position 'i' linked through deps [left|right] (to last added edges) """ if i == -1 or not len(deps): return '', '' deps = deps[i] valency = len(deps) if valency == 1: return data[deps[-1]], '' els...
def check_point(point): """Validates a 2-d point with an intensity. Parameters ---------- point : array_like (x, y, [intensity]) Coordinates of the point to add. If optional `intensity` is not supplied, it will be set to `None`. Returns ...
def commaSplitNum(num : str) -> str: """Insert commas into every third position in a string. For example: "3" -> "3", "30000" -> "30,000", and "561928301" -> "561,928,301" :param str num: string to insert commas into. probably just containing digits :return: num, but split with commas at every third di...
def tower_builder(n_floors): """ Takes in an integer and builds a tower of * from the given int :param n_floors: the number of floors for the tower :return: a list with the number of floors represented with * :rtype: list """ return [("*" * (i * 2 - 1)).center(n_floors * 2 - 1) f...
def calculate_total_clones(haplotypes): """ # ======================================================================== CALCULATE TOTAL CLONES PURPOSE ------- Calculates the total number of clones accross multiple haplotypes. Note that there may be multiple clones associated with each hap...
def split_reads(read, d): """ :param read: :param d: :return: """ prev_c = 0 c = 0 reads = [] for c in range(d, len(read), d): # ToDo: Make generic logic for pairs first = read[prev_c: c] second = read[prev_c + 1: c + 1] third = read[prev_c + 2: c + 2...
def vacantTheSpot(parking_lot, posn): """Vacant that parking spot""" if posn in parking_lot.keys(): driver_age = parking_lot[posn]['driverAge'] vrn = parking_lot[posn]['VRN'] parking_lot[posn] = {} print(f"Slot number {posn} vacated, the car with vehicle registration number '{vr...
def is_equal2(a: int, b: int) -> bool: """Determine if a and b are equal numbers. Determine if both numbers by comparing. """ if type(a) == int and type(b) == int: return a == b return False
def cmake_cache_option(name, boolean_value, comment=""): """Generate a string for a cmake configuration option""" value = "ON" if boolean_value else "OFF" return 'set({0} {1} CACHE BOOL "{2}")\n'.format(name, value, comment)
def prepare_demographics_dict_from_row(row_dict, census_mapping): """ Extract demographic information from a row_dict using a census_mapping. Note that the census_mapping used here should be the same or a subset of the one used during extraction. """ return { category: { nam...
def f_to_c(temp_f): """Convert F to C.""" return (temp_f - 32) * 5 / 9
def getUserAndArea(user): """Factor out the magic user hack for use in other classes""" area = 'user' tokens = user.split('_') if tokens and len(tokens) > 1: user = tokens[0] area = tokens[1] return user, area
def extend(iterable, extension): """Extends the iterable using another iterable. :param iterable: collection of data to transform :type iterable: list :param extension: contains the additional iterable to extend the current one :param extension: iterable """ iterable.extend(extension) ...
def Babylonian(x, N=10): """ t=sqrt(x) """ t = (1 + x) / 2 for i in range(2, N + 1): t = (t + x / t) / 2 return t
def timepoints_by_route_direction(rating): """ Return a dictionary mapping (route ID, direction name) -> list of timepoints """ return { ( timepoint_pattern.route_id, timepoint_pattern.direction_name, ): timepoint_pattern.timepoints for timepoint_pattern i...
def _get_working_shape_and_iterations(requested_shape, max_power_of_two=13): """Returns the necessary size for a square grid which is usable in a DS algorithm. The Diamond Square algorithm requires a grid of size n x n where n = 2**x + 1, for any integer value of x greater than two. To accomodate a reques...
def suppress(action='ignore', **kwarg): """ Sets up the .suppress tuple properly, pass options to this method as you would the stdlib warnings.filterwarnings() So, to use this with a .suppress magic attribute you would do the following: >>> from twisted.trial import unittest, util >>> ...
def validate_phone(phone:str) -> bool: """ returns if the given phone number is valid """ phone = phone.replace("-", "").replace("(", "").replace(")", "") return phone.isdigit() and len(phone) == 10
def _r(noun): """Return a Nock-like repr() of the given noun. >>> _r((42, 0, 1)) '[42 0 1]' """ if isinstance(noun, int): return repr(noun) else: return '[%s]' % ' '.join(_r(i) for i in noun)
def integerBreak(n): """ :type n: int :rtype: int """ if n<=3: return n-1 rest=n%3 if rest==0: return 3**(n//3) elif rest==1: return 3**(n//3-1)*4 else: return 3**(n//3)*2
def sortable_version(version): """Converts '1.2.3' into '00001.00002.00003'""" # Version must be a string to split it. version = str(version) return '.'.join(bit.zfill(5) for bit in version.split('.'))
def get_support(itemsets, itemset, probability=False): """ Get the support of itemset in itemsets. Parameters ---------- itemsets : list All available itemsets. itemset : set The itemset of which the support is calculated. probability : bool Return the probability of...
def decimal_to_alpha(index): """Converts an int to an alphabetical index""" from string import ascii_lowercase alphanum = '' index += 1 # because alphabet hase no 0 and starts with 'a' while index: index -= 1 # 'a' needs to be used as next 'decimal' unit when reaching 'z': ..., 'y', 'z', ...
def sec_to_time(sec): """ Returns the formatted time H:MM:SS """ mins, sec = divmod(sec, 60) hrs, mins = divmod(mins, 60) return f"{hrs:d}:{mins:02d}:{sec:02d}"
def wavedec_keys(level): """Subband keys corresponding to a wavedec decomposition.""" approx = '' coeffs = {} for lev in range(level): for k in ['a', 'd']: coeffs[approx + k] = None approx = 'a' * (lev + 1) if lev < level - 1: coeffs.pop(approx) return...
def can_run_for_president(age, is_natural_born_citizen): """Can someone of the given age and citizenship status run for president in the US?""" # The US Constitution says you must be a natural born citizen *and* at least 35 years old return is_natural_born_citizen and (age >= 35)
def removeUnencodedText(text): """ Remove non UTF-8 characters Author: Anna Yannakopoulos (2020) Args: text (str): Input text Returns: str: Input text where non UTF-8 character removed """ return "".join([i if ord(i) < 128 else "" for i in text])
def cast_float(str): """a helper method for converting strings to their float value Args: str: a string containing a number Returns: the float value of the string given or None if not a float """ if str!="" and str!=None: v = float(str) else: v = None ...
def is_outerwall(line: str) -> bool: """Check if current line is the start of an outer wall section. Args: line (str): Gcode line Returns: bool: True if the line is the start of an outer wall section """ return line.startswith(";TYPE:WALL-OUTER")
def msecs_to_hours_mins_secs(msecs): """ Convert milliseconds to hours, minutes and seconds. msecs is an integer. Hours, minutes and seconds output is a string.""" secs = int(msecs / 1000) mins = int(secs / 60) remainder_secs = str(secs - mins * 60) if len(remainder_secs) == 1: remainde...
def parse(input_dict,nested_lists=None): """recursively parse a dictionary with nested lists into a flat dictionary""" out={} if dict not in [type(x) for x in input_dict.values()]: return input_dict else: for k,v in input_dict.items(): if type(v) in [str,int,float,bool]: ...
def check_order_of_symbols_and_freqs_list(symbols_and_freqs_list): """Checks whether provided symbols_freq_list is in decreasing order or not.""" assume_max_freq = symbols_and_freqs_list[0][1] for elem in symbols_and_freqs_list: if elem[1] > assume_max_freq: raise ValueError('Provided li...
def get(data,key): """ Returns the value corresponding to a key, if it exists. Otherwise it returns an empty string. """ try: value = data[key] except KeyError: value = '' return value
def size_of_varint_py(value): """ Number of bytes needed to encode an integer in variable-length format. """ value = (value << 1) ^ (value >> 63) if value <= 0x7f: return 1 if value <= 0x3fff: return 2 if value <= 0x1fffff: return 3 if value <= 0xfffffff: retu...
def is_prime_sieve(n): """ create a 0-indexed list where sieve[i] is true if i is prime, through n (size of list returned is n + 1) """ # True if prime is_prime = [True] * (n + 1) # 1 is definitionally False is_prime[0:2] = [False, False] for i in range(2, n): for add in ran...
def ini_elec_levels(spc_dct_i, spc_info): """ get initial elec levels """ if 'elec_levels' in spc_dct_i: elec_levels = spc_dct_i['elec_levels'] else: elec_levels = [[0., spc_info[2]]] return elec_levels
def gcd(num1, num2): """Find greater common divider. Find greater common divider for two integer numbers Args: num1 (int): number one num2 (int): number two Returns: int: Greater Common Divider """ while num1 != num2: if num1 > num2: num1 -= num...
def get_coverage_archive_name(benchmark): """Gets the archive name for |benchmark|.""" return 'coverage-build-%s.tar.gz' % benchmark
def make_splice_string(nodename, context): """Generates splice string from a list of context. E.g. make_splice_string("renorm4", [-4, 4]) returns "Append(Offset(renorm4, -4), Offset(renorm4, 4))" """ assert type(context) == list, "context argument must be a list" string = ["Offset({0}, {1})".fo...
def set0 (strlist,pos): """ removes elements of a list from pos to the end, save these as a separate list """ hold = [] while len(strlist) > pos: hold.append(strlist.pop(len(strlist)-1)) hold.reverse() return strlist,hold
def lags(fs): """Extract lags from a collection of features.""" return type(fs)(f.lag for f in fs)
def getLocation( min_point: tuple, max_point: tuple ) -> tuple: """Gets the bottom center location from the bounding box of an occupant.""" # Unpack the tuples into min/max values xmin, ymin = min_point xmax, ymax = max_point # Take midpoint of x-coordinate and ymax for bottom middle of box x_re...
def select_matching_signature(funcs, *args, **kwargs): """ Select and call the function that accepts ``*args, **kwargs``. *funcs* is a list of functions which should not raise any exception (other than `TypeError` if the arguments passed do not match their signature). `select_matching_signature` t...
def avg(window, *args, **kwargs): """ Returns window average :param window: :return: """ return float(sum(w[1] for w in window)) / len(window)
def remove_dup(a): """ remove duplicates using extra array """ res = [] count = 0 for i in range(0, len(a)-1): if a[i] != a[i+1]: res.append(a[i]) count = count + 1 res.append(a[len(a)-1]) print('Total count of unique elements: {}'.format(count + 1)) return ...
def remainder(x, y): """Difference between x and the closest integer multiple of y. Return x - n*y where n*y is the closest integer multiple of y. In the case where x is exactly halfway between two multiples of y, the nearest even value of n is used. The result is always exact.""" from math import...
def split_arguments(args): """ Split specified arguments to two list. This is used to distinguish the options of the program and execution command/arguments. Parameters ---------- args : list Command line arguments Returns ------- list : options, arguments opti...
def _parse_option(option_name, option_value): """Parse an option value and return the parsed value. Args: option_name: Name of the option. option_value: Value of the option. Returns: The parsed option value """ if option_name in ['test_package_names', 'permitted_emails']: option_value = (opt...