content
stringlengths
42
6.51k
def _iframe(src, width=650, height=365, content=None, link=None): """Create an iframe html snippet.""" html = ( '<iframe width="%s" height="%s" src="%s" ' 'frameborder="0" allowfullscreen></iframe>' ) % (width, height, src) if not content: return html if link: content...
def linecount(doc: str, end: int, start: int = 0): """Returns the number of lines (by counting the number of newline characters \\n, with the first line being line number one) in the string *doc* between the positions *start* and *end*. """ return (doc.count('\n', start, end) + 1)
def __discount_FP(i, n): """ Future worth factor (compound amount f actor) Factor: (F/P, i, N) Formula: F=P(1+i^N :param i: :param n: :return: Cash Flow: F | | -------------- | P """ return (1 + i) ** n
def _TransformOperationState(metadata): """Extract operation state from metadata.""" if 'status' in metadata: return metadata['status']['state'] elif 'state' in metadata: return metadata['state'] return ''
def _invoke_member(obj, membername, *args, **kwargs): """Retrieve a member of an object, then call it with the provided arguments. Args: obj: The object to operate on. membername: The name of the member to retrieve from ojb. args: Positional arguments to pass to the method. kwar...
def col_num_to_string(n): """ Converts a column number (e.g. 3) into an excel-like column name (e.g. C) """ string = "" while n > 0: n, remainder = divmod(n - 1, 26) string = chr(65 + remainder) + string return string
def checksumStr(data): """Take a NMEA 0183 string and compute the checksum. @param data: NMEA message. Leading ?/! and training checksum are optional @type data: str @return: hexadecimal value @rtype: str Checksum is calculated by xor'ing everything between ? or ! and the * >>> checksumSt...
def order_of_execution(count: int, k: int): """ This one is linear time. :param count: :param k: :return: """ actions = 0 r = [*range(1, 1 + count, 1)] removed = [] pos = 0 while len(r) != 0: pos = (pos + (k - 1)) % len(r) removed.append(r.pop(pos)) ac...
def parse_input(event): """Parses all input required from step function.""" input_request = event["input"] return { "batch_id": input_request["transformation_step_output"]["batch_id"], "output_sns_arn": input_request.get("destinationSnsArn"), "execution_id": event["execution_id"], ...
def average(a, b): """ Given two numbers a and b, return their average value. Parameters ---------- a : number A number b : number Another number Returns ------- res : number The average of a and b, computed using 0.5*(a + b) Example ------- >>> avera...
def insertion_sort(A): """Sort list of comparable elements into nondecreasing order.""" for k in range(1, len(A)): # from 1 to n-1 cur = A[k] # current element to be inserted j = k # find correct index j for current ...
def find_dependency_in_spec(spec, ref): """Utility to return the dict corresponding to the given ref in a dependency build spec document fragment """ for item in spec: if item['ref'] == ref: return item
def jaccard_similarity(x, y): """ set implementation of jaccard similarity """ intersection_cardinality = len(set.intersection(*[set(x), set(y)])) union_cardinality = len(set.union(*[set(x), set(y)])) return intersection_cardinality / float(union_cardinality)
def preprocess(arr): """ Args: data is a list of token positions wher a position is a form of list [d_model, idx1, idx2, idx3 ,...] in which d_model means the length of the position list, "idx1", "idx2", "idx3" are the the indexes that correpsonding value is 1. R...
def col_num_equal(header, second_line): """ Check if the two strings of the same number of lines :param header: the header of one of the simulation results files in the directory. This contains the names of the parameters and summary statistics. :param second_line: 2nd line of results file containin...
def get_parent_path(thepath="."): """Returns the parent directory as an absolute path. Examples: >>> import os >>> os.getcwd() '/home/pengwin/Temp/pyplay/IMPORT_functions/my_py_funcs/worksheet_dir' >>> >>> get_parent_path() PosixPath('/home/pengwin/Temp/pyplay/IM...
def count_noun(number, noun, plural=None, pad_number=False, pad_noun=False): """ EXAMPLES:: sage: from sage.doctest.util import count_noun sage: count_noun(1, "apple") '1 apple' sage: count_noun(1, "apple", pad_noun=True) '1 apple ' sage: count_noun(1, "apple", p...
def data_split(x_dataset, y_dataset, ratio=0.8): """ Split input data to test data and train data by ratio. Return splited data. """ x_dataset_size = len(x_dataset) y_dataset_size = len(y_dataset) x_dataset_train = x_dataset[:int(x_dataset_size * ratio)] y_dataset_train = y_dataset[:int(...
def get_samples_correlation_chains( mixin, trade_data, chain_length, sample_epoch): """ To get list of correlation chains based on sample epoch and distance """ chain = [] chains = [] for m in mixin: if m[1] == sample_epoch and len(chain) == 0: chain = [m] ...
def uint_to_int(uint, bits): """ Assume an int was read from binary as an unsigned int, decode it as a two's compliment signed integer :param uint: :param bits: :return: """ if (uint & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255 uint = uint - (...
def calculate_weight(i, j, patterns): """Calculate the weight between the given neurons""" num_patterns = len(patterns) s = 0.0 for mu in range(num_patterns): s += patterns[mu][i] * patterns[mu][j] w = (1.0 / float(num_patterns)) * s return w
def get_keys(obj): """ Returns an iterator or iterable containing keys of given obj. """ if hasattr(obj, "keys"): return obj.keys() else: return range(len(obj))
def processor(document: dict) -> dict: """ The default document processor for job documents. Transforms projected job documents to a structure that can be dispatches to clients. 1. Removes the ``status`` and ``args`` fields from the job document. 2. Adds a ``username`` field. 3. Adds a ``create...
def map_chars_one_to_one(text_1: str, text_2: str) -> bool: """ in case the mapping is direct """ char_dict = {} for c1, c2 in zip(text_1, text_2): if c1 not in char_dict: char_dict[c1] = c2 elif char_dict[c1] != c2: return False return True
def check_double_quote(inpstring): """ Check if some strings needs of a double quote (if some space are inside the string, it will need to be inside two double quote). E.g.: --sfmt="TIFF (unstitched, 3D)" Input: inpstring: input string or array of strings Output: newstring = new string (or...
def is_integer(num_str: str) -> bool: """ :param num_str: :return: """ if len(num_str) == 0: return False for ch in num_str: if not ch.isdigit() and ch != "_": return False return True
def __find_microhomology(seq_before, seq, seq_after): """ Finds the length of microhomology before or after a mutation. :param seq_before: the genomic sequence before the mutation (str) :param seq: the genomic sequence of the mutation (str) :param seq_after: the sequence after the mutation (str) ...
def extract_media_url(media): """extracts the url of a media item: an image post, or an image within a gallary I extract the data from the first items in "display_resources" instead of the standard "display_url" because this is the lowest resolution available """ return media["display_resources"][0...
def urljoin(*paths): """Join delimited path using specified delimiter. >>> assert urljoin('') == '' >>> assert urljoin('/') == '/' >>> assert urljoin('', '/a') == '/a' >>> assert urljoin('a', '/') == 'a/' >>> assert urljoin('', '/a', '', '', 'b') == '/a/b' >>> ret = '/a/b/c/d/e/' >>> as...
def truncate_seq_pair(tokens_a, tokens_b, max_num_tokens, rng): """Truncates a pair of sequences to a maximum sequence length.""" while True: total_length = len(tokens_a) + len(tokens_b) if total_length <= max_num_tokens: break trunc_tokens = tokens_a if len(tokens_a) > len(tokens_b) else tokens_...
def first_come_first_served(evs, iface): """ Sort EVs by arrival time in increasing order. Args: evs (List[EV]): List of EVs to be sorted. iface (Interface): Interface object. (not used in this case) Returns: List[EV]: List of EVs sorted by arrival time in increasing order. """...
def in2pt(inval=1): """1in -> 72pt""" return float(inval) * 72.0
def Factor_obs2int(a_lam): """ Calculate the factor to correct the observed flux or luminosity to the intrinsic one. Parameters ---------- a_lam : float The extinction in magnitude, A_lambda. Returns ------- f : float The correction factor to convert the observed va...
def find_dict(mdicts, key, val): """ Given a list of mult-dicts, return the multi-dict that contains the given key/value pair. """ def found(d): return key in d and val in d[key] try: return [d for d in mdicts if found(d)][0] except IndexError: raise LookupError("Di...
def _handle_sort_key(model_name, sort_key=None): """Generate sort keys according to the passed in sort key from user. :param model_name: Database model name be query.(alarm, meter, etc.) :param sort_key: sort key passed from user. return: sort keys list """ sort_keys_extra = {'alarm': ['name', ...
def capm(rf, beta, rm): """ calculate the minimum rate of return using CAPM (Capital Asset Pricing Model). parameters: ----------- rf: The risk free rate of retun. beta: The company's beta factor which measures the sensitivity of an investment return to market movement rm: mark...
def is_unicode_safe(stream): """returns true if the stream supports UTF-8""" if not hasattr(stream, "encoding"): return False return stream.encoding in ["UTF_8", "UTF-8"]
def _get_dict_from_list(dict_key, list_of_dicts): """Retrieve a specific dict from a list of dicts. Parameters ---------- dict_key : str The (single) key of the dict to be retrieved from the list. list_of_dicts : list The list of dicts to search for the specific dict. Returns ...
def closest_multiple(n, k, ceil=True): """Return closest greater multiple of `k` to `n`.""" if n == 0: return 0 if n < k: return k if n % k == 0: return n return k * (n / k + (1 if ceil else 0))
def _center_strip_right(text: str, width: int) -> str: """Returns a string with sufficient leading whitespace such that `text` would be centered within the specified `width` plus a trailing newline.""" space = (width - len(text)) // 2 return space * " " + text + "\n"
def first(func, iterable): """Returns the first element in iterable for which func(elem) is true. Equivalent to next(ifilter(func, iterable)). """ for elem in iterable: if func(elem): return elem
def cmc_get_data(jso, cmc_id, pair_symbol='USD'): """ Pull relevant data from a response object """ if not jso: return None data = jso.get('data', {}) specific_data = data.get(str(cmc_id), {}) quote = specific_data.get('quote', {}) symbol_data = quote.get(pair_symbol, {}) return { ...
def skip_doc(cls): """Returns True if we should skip cls in docstring extraction.""" return cls.__name__.endswith("Box") or (hasattr(cls, "no_doc") and cls.no_doc)
def fragment_to_keys(fragment): """Split a fragment, eg. #/components/headers/Foo in a list of keys ("components", "headers", "Foo") """ return fragment.strip("#").strip("/").split("/")
def _sanitize_features_name(explanations, features_name): """ Helper that provide generic features name (with the feature index) if features name is None. """ if features_name is None: single_explanation = len(explanations.shape)==1 if single_explanation: features_name = ...
def calc_total_crab_fuel_complex(input_positions: list) -> int: """ Determine the fuel required for each crab to move to each position and return the fuel for the minimum consumption position Args: input_positions: list of input crab submarine positions Returns: total fuel required ...
def generate_ncr_sets(n, r): """ This is exactly same as the dp formula to compute nCr. C(n,r) = (n-1, r-1)[Choose the nth element] + C(n-1, r)[Dont choose] """ if r <= 0 or n <= 0 or r > n: return [[]] if r == n: # CAREFUL: Dont forget this base case! return [[i for i in...
def find_discrete_state(x0, part): """Return index identifying the discrete state to which the continuous state x0 belongs to. Notes ===== 1. If there are overlapping partitions (i.e., x0 belongs to more than one discrete state), then return the first discrete state ID @param x...
def findLargestAlphebetizedSubstr(inStr): """Finds the largest substring of inStr that is in alphebetical order.""" if(len(inStr) == 0): return "" curSubstr = inStr[0] curLargestSubstr = "" for i in inStr: if i.lower() >= curSubstr[-1].lower(): #Still alphabetical... ...
def distance2(x1, y1, x2, y2): """Retourner la distance euclidienne entre (x1, y1) et (x2, y2).""" return ((x1-x2)**2 + (y1-y2)**2)**0.5
def raw_name_to_display(raw_name): """ Converts the given raw command or it's parameter's name to it's display name. Parameters ---------- raw_name : `str` The name to convert. Returns ------- display_name : `str` The converted name. """ return '-'.join(...
def find_pivot(input_list): """Find the pivot point of the sorted yet "shifted" list. A simple divide and conquer strategy to find the pivot point of the list. Time complexity O(log n) :param input_list: a sorted and pivoted list (i.e. "shifted") :type input_list: list :return: an index of th...
def append_path(filepath, path_list, path): """Append appropriate paths for testcase/suite/project in test folder """ temp_list = [] for file_name in path_list: file_name = path + file_name temp_list.append(file_name) if temp_list: filepath.extend(temp_list) return filepa...
def divide_lists(lst_numer, lst_denom): """ Divides each element of the nested list 'lst_numer' by 'lst_denom'. The division is done by taking each element of 'lst_numer' and for each index of that element, dividing the item at that index by the item at the same index of 'lst_denom'. See example be...
def union(a, b): """ Returns the union of sets a and b. In plain english: Returns all the items of a and b combined with duplications removed. """ return a.union(b)
def intersection(l1, l2): """Return intersection of two lists as a new list:: >>> intersection([1, 2, 3], [2, 3, 4]) [2, 3] >>> intersection([1, 2, 3], [1, 2, 3, 4]) [1, 2, 3] >>> intersection([1, 2, 3], [3, 4]) [3] >>> intersec...
def get_radius_attribute(p, attribute_name): """ Utility function to get an attribute from a tuple """ try: return next(value for name, value in p if name == attribute_name) except StopIteration: return None
def load_fdist_from_str(line): """ Load a frequency dictionary from a line of identifier, frequecy pairs, e.g. S 10 NP 5 VP 5 ... """ fdist = {} tokens = line.strip().split() nPair = int(len(tokens)/2) assert len(tokens) % 2 == 0 for i in range(nPair): identifier = tokens[i*2...
def verify_on_off(flag): """ :type: str :param flag: On/Off parameter to check :rtype: str :return: Fix flag format :raises: ValueError: invalid form """ if flag is None: return None flag = str(flag).lower() if flag not in ['on', 'off']: raise ValueError('"{}" i...
def normalisation_constant(unnormalised: float, normalised: float) -> int: """ Returns the constant used to normalise a value. :param unnormalised: The unnormalised value. :param normalised: The normalised value. :return: The normalisation constant. """ return round(u...
def bioconductor_tarball_url(package, pkg_version, bioc_version): """ Constructs a url for a package tarball Parameters ---------- package : str Case-sensitive Bioconductor package name pkg_version : str Bioconductor package version bioc_version : str Bioconductor ...
def convert_subscripts(old_sub, symbol_map): """Convert user custom subscripts list to subscript string according to `symbol_map`. Examples -------- >>> oe.parser.convert_subscripts(['abc', 'def'], {'abc':'a', 'def':'b'}) 'ab' >>> oe.parser.convert_subscripts([Ellipsis, object], {object:'a'}) ...
def base36_to_int(s: str): """ Convert a base 36 string to an int. Raise ValueError if the input won't fit into an int. """ # To prevent overconsumption of server resources, reject any # base36 string that is longer than 13 base36 digits (13 digits # is sufficient to base36-encode any 64-bit...
def _get_entity_type_sg_name_field(entity_type): """ Return the Shotgun name field to use for the specified entity type. This is needed as not all entity types are consistent! :param entity_type: The entity type to get the name field for :returns: The name field for the specified...
def generate_list(iterable_a, iterable_b): """Return a list of a+b combinations""" result = [] for iter_a in iterable_a: for iter_b in iterable_b: result.append(iter_a+iter_b) return result
def get_user_pool_domain(prefix, region): """Return a user pool domain name based on the prefix received and region. Args: prefix (str): The domain prefix for the domain region (str): The region in which the pool resides """ return '%s.auth.%s.amazoncognito.com' % (prefix, region)
def focal_length_calculator(width_in_image, distance_in_image, real_width_of_object): """ This function is used to calculate the focal length for the distnace estimation using triangle similarity :param1 (width_in_image): The width of the object in the reference image :param2 (distance_in_image): T...
def limit_rgb(in_v, limiter): """ Takes a value and it's maximum potential and maps it to 0-255 :param in_v: input value :param limiter: value's maximum potential :return: int """ out_v = int((in_v/limiter) * 255) return out_v
def _package_root(name): """Convert treadmill.logging.xxx => treadmill """ return name.split('.', 1)[0]
def _cleanUpAllIssues(results, all_issues): """ Helper function for _getAllIssues(). Clean up issues and strip out cursor info. GIVEN: results (dict) -- initial query response with metadata all_issues (list) -- nested list of issues with comments and metadata RETURN: results (dict) -...
def encrypt_file(filepath): """Encrypt file contents to base64. :param filepath: the path of the file. """ try: # if it starts with ~ # os.path.expanduser with open(filepath) as inf: file_contents = inf.read() return file_contents.encode('base64') exce...
def human_readable_patch(address, data, comp = None): """Returns a human readable string describing the patching action. """ s = "[0x%04X] returns 0x%02X" % (address, data); if comp is not None: s += " if read as 0x%02X" % comp; return s;
def is_valid(row, col): """ Returns a boolean based on whether on not a (row, col) pair is a valid board coordinate """ return ((row >= 0) and (row <= 9) and (col >= 0) and (col <= 9))
def get_multi_insert_str(columns_to_insert,insert_values_dict_lst): """ get_multi_insert_str creates the placeholder string for multiple insertion values. :param columns_to_insert: :param insert_values_dict_lst: :return: """ dict_lst= [] placeholder_str = ["%s"]*len(columns_to_insert) ...
def get_jyutping_from_mor(mor): """ Extract jyutping string from *mor*. """ jyutping, _, _ = mor.partition("=") jyutping, _, _ = jyutping.partition("-") jyutping, _, _ = jyutping.partition("&") return jyutping
def number_valid(value): """Test for valid number >>> number_valid(0) True >>> number_valid(-1) True >>> number_valid(1.12039123) True >>> number_valid('1.12039123') True >>> number_valid('x1.12039123') False >>> number_valid('t') False >>> number_valid('') T...
def is_data(data): """ Check if a packet is a data packet. """ return len(data) > 26 and ord(data[25]) == 0x08 and ord(data[26]) in [0x42, 0x62]
def where_point_in_poly(poly_points): """ a where bool, where geom is contained in the polygon made of the inputted points :input: an array of tuples [(lon, lat), (lon, lat)...]. THE LAST TUPLE SHOULD BE THE SAME AS THE FIRST :output: a where clause string """ poly_points_string = map("{...
def union_dict(dict1, dict2): """combine two dicts Args: dict1(dict): only allow dict which value is int dict2(dict): only allow dict which value is int Returns: dict2: combined dict Examples: >>> d = Dict() >>> d.union_dict({"a": 1}, {"b": 2}) {'a': 1, ...
def is_iterable(testee): """Check if a value is iterable. This does no type comparisons. Note that strings are iterables too! >>> is_iterable([]) True >>> is_iterable("Hello World!") True >>> is_iterable(False) False """ try: iter(testee) return True exce...
def cached_pow(number: float, power: float) -> float: """ Raises number to power. Caches the previous results so that if a same set of arguments is passed, it will fetch from the cache. """ return pow(number, power)
def GetKRStateFraction(alignedSeqList):#{{{ """return (cnt_K, cnt_R, per_K, per_R)""" lengthAlignment=len(alignedSeqList[0]) numSeq = len(alignedSeqList) cnt_K = [0]*lengthAlignment cnt_R = [0]*lengthAlignment for i in range(numSeq): alignedSeq = alignedSeqList[i] for j in range...
def filter_toks(tokens, min_y, max_y_dist): """Currently just filters by max y distance between rows""" row_intervals = sorted([[t['top'], t['top'] + t['height']] for t in tokens if t['conf'] > .2]) row_intervals.sort(key=lambda interval: interval[0]) row_ranges = row_intervals for current in row_i...
def append_ordinal(number): """Add an ordinal string to an integer. :param number: The number to be converted to an ordinal. :type number: int :rtype: str """ suffixes = dict() for i, v in enumerate(['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']): suffixes[str(i)] = ...
def linear_rampup(current, rampup_length): """Linear rampup""" assert current >= 0 and rampup_length >= 0 if current >= rampup_length: return 1.0 else: return current / rampup_length
def _lisp_pad_before(surrounding_text): """Helper for lisps that determines whether leading padding is needed.""" return not ( surrounding_text and surrounding_text.char_before in [*"([{@,\n\r\t ", "", None] )
def region_domains(board): """ make list of tuples for each region to construct domains return dictionary of region : set of coord pairs """ regionCoords={} rowIndex=0 for row in board: colIndex=0 for colVal in row: if colVal in regionCoords: r...
def key(name): """Extracts the filename key.""" dot = name.rfind(".") if -1 != dot: return name[0:dot] return ""
def ext_euclid(x, y): """ Returns (g, a, b) such that g = gcd(x, y) = ax + by """ if y == 0: # gcd = x and gcd = x = (1)x + (0)y return (x, 1, 0) else: # Recursively, g = a1 * (y) + b1 * (x % y) (g, a1, b1) = ext_euclid(y, x % y) # a1 * (y) + b1 * (x % y) = b1...
def sum_series(n, a = 0, b = 1): """ The function will determine which sequence to run The function with have one required parameter and two optional ones The required parameter will determine the element in the series to print The optional parameters will default to 0 and 1. The optional parameters will de...
def talk(text, is_yelling=False): """ Prints text is_yelling capitalizes text returns transformed text """ if is_yelling: text = text.upper() print(text) return text
def generate_docstring(operation_spec): """Generate a docstring for an operation defined in operation_spec (swagger)""" # Description of the operation docs = operation_spec.get("description", "No Description") docs += "\n\n" # Parameters of the operation parameters = operation_spec.get("paramet...
def get_job_type(name): """Returns job type based on its name.""" if 'phase1' in name: return 'phase1' elif 'phase2' in name: return 'phase2' elif 'dfg' in name: return 'dfg' else: return 'other'
def flag_true(argument): """ Check for a valid flag option (no argument) and return ``True``. (Directive option conversion function.) Raise ``ValueError`` if an argument is found. """ if argument and argument.strip(): raise ValueError('no argument is allowed; "%s" supplied' % argument) ...
def syntax_highlighter(input: str) -> str: """.repo-metadata.json language field to syntax highlighter name.""" if input == "nodejs": return "javascript" return input
def yellow_bold(msg: str) -> str: """ Given an 'str' object, wraps it between ANSI yellow & bold escape characters. :param msg: Message to be wrapped. :return: The same message, which will be displayed as yellow & bold by the terminal. """ return '\u001b[1;33m%s\u001b[0m' % msg
def heuristic(i0, j0, i1, j1): """ Squared eucledian distance. At the moment it's much faster without sqrt. (4000x4000 grid ~8s vs ~60s) If the script is not accurate use with math.sqrt. """ return ((i0 - i1) ** 2) + ((j0 - j1) ** 2)
def resistivity(rho,T,factor,T0=293): """ calculates temperature dependent resistivity """ return rho*(1 + (T-T0)*factor)
def health(): """ A simple check to see if the application is running """ return {"message": "Healthy"}
def check_namespace(name=""): """ replaces the colon with a hyphen :param name: :return: <str> name. """ if ':' in name: return '-'.join(name.split(':')) return name