content
stringlengths
42
6.51k
def pad_to(data, alignment, pad_character=b'\xFF'): """ Pad to the next alignment boundary """ pad_mod = len(data) % alignment if pad_mod != 0: data += pad_character * (alignment - pad_mod) return data
def pretty_tree(x, kids, show): """Return a pseudo-graphic tree representation of the object `x` similar to the `tree` command in Unix. Type: `(T, Callable[[T], List[T]], Callable[[T], str]) -> str` It applies the parameter `show` (which is a function of type `(T) -> str`) to get a textual represe...
def get_pad_tuple(padding, kernel): """Common code to get the pad option Parameters ---------- padding : int or str Padding size, or ['VALID', 'SAME'] kernel : tuple of int Conv kernel size Returns ------- pad_top : int Padding size on top pad_left : int ...
def parse_prefix(prefix, default_length=128): """ Splits the given IP prefix into a network address and a prefix length. If the prefix does not have a length (i.e., it is a simple IP address), it is presumed to have the given default length. :type prefix: string :param prefix: An IP mask. ...
def electricity_cost_sek(dist, sekpkwh=.85, kmpkwh=100): """Gets cost of commute via ebike in Swedish Krona. Inputs: dist: distance in kilometers (numeric) sekpkwh: Swedish Krona (SEK) per kilowatt-hour (kWh). Obtained from electricity_price() function. kmpkwh: Kilometers (km) per kilowa...
def _tpu_zone(index): """Chooses a GCP TPU zone based on the index.""" if index < 70: return 'europe-west4-a' elif index < 80: # 10 return 'us-central1-b' elif index < 90: # 10 return 'us-central1-c' elif index < 100: # 30 (seems like 10...) return 'asia-east1-c' elif index < 110: ret...
def apply_shear(c_ellip, c_gamma): """Compute complex ellipticity after shearing by complex shear `c_gamma`.""" return (c_ellip + c_gamma) / (1.0 + c_gamma.conjugate() * c_ellip)
def is_non_ascii(value): """ Check whether the string is ASCII only or not. """ return not all(ord(c) < 128 for c in value)
def mag2mom(mw): """Converts magnitude to moment - dyne-cm""" return 10 ** (3.0 / 2.0 * (mw + 10.7))
def sdf_supported_property(property_name): """ Check to verify that this property is support natively in SDF (without modification) :param property_name name to check :return: boolean true/false """ # these supported items, assume the input models are valid JSON, # and do no addition ...
def _copyPoints(points): """ Make a shallow copy of the points. """ copied = [point.copy() for point in points] return copied
def _make_equal_size(a: str, b: str): """ Make the strings a and b equal size, by right-padding with spaces the shortest string """ max_length = max(len(a), len(b)) a = a.ljust(max_length).upper() b = b.ljust(max_length).upper() return a, b
def find_folder(folder_list, folder_name): """ Check whether there is already a folder named 'folder_name' within the list 'folder_list'. Return its id. """ # Check whether a folder with the same name already exists for folder in folder_list: if folder['title'] == folder_name: ...
def mode(data=[]): """ returns the modal value in a list """ # gets the values represented once value_set = set( data ) # Initial values c = -1 v = [] for d in value_set: # Current count is higher that prev observed if ( c < data.count( d )): c = data.count( d ...
def prependash(arr): """Prepend dashes to non-empty list""" return arr and ('--',)+arr
def stripdesc(desc): """strip trailing whitespace and leading and trailing empty lines""" return b'\n'.join([l.rstrip() for l in desc.splitlines()]).strip(b'\n')
def stop_when_true(test_expr, result_expr, seq): """ feed elements into expr until it returns True, returning that element (None otherwise) :param test_expr: callable of 1 arg that returns True/False :param result_expr: callable of 1 arg; takes found element from test_expr and maps it to a fina...
def unbind(port: int) -> dict: """Request browser port unbinding. Parameters ---------- port: int Port number to unbind. """ return {"method": "Tethering.unbind", "params": {"port": port}}
def true_l2s(value: float) -> float: """Convert linear component to SRGB gamma corrected""" if value <= 0.0031308: return value * 12.92 return 1.055 * (value ** (1.0 / 2.4)) - 0.055
def merge(left, right): """Merge function used for merge sort""" lPoint = 0 rPoint = 0 result = [] # Use two pointer method to build sorted list while lPoint < len(left) and rPoint < len(right): if left[lPoint][0] > right[rPoint][0]: # Sort by min radius in descending order ...
def format_bytes(bytes): """Pretty-print a number of bytes.""" if bytes > 1e6: bytes = bytes / 1.0e6 return '%.1fm' % bytes if bytes > 1e3: bytes = bytes / 1.0e3 return '%.1fk' % bytes return str(bytes)
def fold(val, min, max): """ Transform values normalized between 0-1 back to their regular range. Parameters ---------- val : float value to be unfolded. min: float min of value range. max: float max of value range. """ fold_list = [] for i in val: ...
def count_sliding_increases(depths: list): """ :param depths: list of depths :return: number of times depth increased :rtype: int """ increases = 0 int_depths = [int(depth) for depth in depths] for position in range(len(int_depths)): if position >= 3 and \ int_d...
def nested_haskey(x, keys): """ For a nested dictionary 'x' and list of keys, checks if all keys exist in the nested key path. """ if len(keys) == 1: return (keys[0] in x) if keys[0] in x: return nested_haskey( x[keys[0]], keys[1:]) else: return False
def by_index(fg_color_index, bg_color_index = 0, attribute = 0): """ Return string with ANSI escape code for set text colors fg_color_index: color index from 0 to 255, applied to text bg_color_index: color index from 0 to 255, applied to background attribute: use Attribute class variables """ ...
def extract_explanation(exp_string): """ Convert raw string (eg "uid1|role1 uid2|role2" -> [uid1, uid2], [role1, role2]) """ if type(exp_string) != str: return [], [] uids = [] roles = [] for uid_and_role in exp_string.split(): uid, role = uid_and_role.split("|") uids.append(uid) roles.append(role) retu...
def merge_sort(array): """ Input : list of values Note : It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. Returns : sorted list of values """ def join_sorted_arrays(array1, array2): """ Input : 2 ...
def __assert_sorted(collection): """Check if collection is ascending sorted, if not - raises :py:class:`ValueError` :param collection: collection :return: True if collection is ascending sorted :raise: :py:class:`ValueError` if collection is not ascending sorted Examples: >>> __assert_sorted([0,...
def encodeCipherString(enctype, iv, ct, mac): """return bitwarden cipherstring""" ret = "{}.{}|{}".format(enctype, iv.decode('utf-8'), ct.decode('utf-8')) if mac: return ret + '|' + mac.decode('utf-8') return ret
def exists_in_prev_dialog_states(slot_value, converted_turns): """Whether slot value exists in the previous dialogue states.""" for user_turn in converted_turns[::2]: assert user_turn['speaker'] == 'USER' for frame in user_turn['frames']: if 'state' in frame and 'slot_values' in fram...
def convertStringToBool(nodeText): """ Convert string to bool @ In, nodeText, str, string from xml node text @ Out, val, bool, True or False """ stringsThatMeanTrue = list(['yes','y','true','t','on']) val = False if nodeText.lower() in stringsThatMeanTrue: val = True return val
def bytes(bits=None, kilobytes=None,megabytes=None,gigabytes=None,terabytes=None,petabytes=None): """Usage: Convert to bytes. Example: bytes(gigabytes=2)""" if bits is not None: return bits/8 elif kilobytes is not None: return kilobytes*1000 elif megabytes is not None: ret...
def _check_one_to_one(case_control_map: dict) -> bool: """Check if mapping dict is one-to-one (one control per case).""" return all([len(ctrls) == 1 for ctrls in case_control_map.values()])
def isclose(a, b, rel_tol=1e-4, abs_tol=1e-4): """ Returns True if a and b are close to each other (within absolute or relative tolerance). :param a: float :param b: float :param rel_tol: float :param abs_tol: float :return: bool """ return abs(a-b) <= max(rel_tol * max(abs(a), abs(...
def clamp(n, maxabs): """Clamp a number to be between -maxabs and maxabs""" return max(-maxabs, min(n, maxabs))
def solve(n, ar): """ Return the absolute sum of the nxn matrix defined by ar. """ # Initialize the sum of the diagonals 1 and 2. d1sum = 0 d2sum = 0 # Iterate through the list of n-sized lists, one row at a time and use # enumerate() to generate the current index value which we'll use ...
def flatten_documents_to_sentence_strings(docs): """Flattens the given documents in nested form to a string representation where each sentence is a new document (useful for sentence-wise cooccurrence measuring) [ #docs [ #document1 ['word1','word2','word3'], #sentence1 ['word1',...
def clean_cut(phases, cut): """ Ensure the validation set doesn't contain permutatations of the training vectors """ from itertools import permutations def perms(phase): return [list(i) for i in permutations(phase.split())] print("Searching the clean validation cut...") while phases[...
def try_key(d, key, val): """ d: dict key: str val: object the default value if the key does not exist in d """ if key in d: return d[key] return val
def coroutine_frames(all_frames): """Extract coroutine boilerplate frames from a frame list for better stack/traceback printing of coroutines """ useful_frames = [] for frame in all_frames: if frame[0] == '<string>' and frame[2] == 'raise_exc_info': continue # start out ...
def istype(obj, check): """like isinstance(obj, check), but strict This won't catch subclasses. """ if isinstance(check, tuple): for cls in check: if type(obj) is cls: return True return False else: return type(obj) is check
def get_relationships_by_type(rels, rel_type): """ Finds relationships by a relationship type Example:: # Import from cloudify import ctx from cloudify_ansible_tower import utils # Find specific relationships rels = utils.get_relationships_by_type( ...
def preprocess_keylist(keylist, **options): """Convert a list of keys to a comma-separated string.""" if isinstance(keylist, list): return ", ".join([str(key) for key in keylist]) return keylist
def binary_search(li, x): """ Given a sorted list li, return the index at which x appears using binary search i.e. in O(log(N)) time complexity where N is the length of li. If x is not in the list, return -1. :param li: list of sorted elements :param x: element to search for :return: "...
def scale_convert(x): """Transforms from 1:9 scale to -1:1""" return ((x - 1) / 8 - 0.5) * 2
def _check_monit_services_status(check_result, monit_services_status): """ @summary: Check whether each type of service which was monitored by Monit was in correct status or not. If a service was in "Not monitored" status, sanity check will skip it since this service was temporarily ...
def markdownify(span): """ Given a "span" element, returns a segment of markdown text Supports __bold__, _italic_, ![](image) and [link](url) elements. Bold and italic elements are trimmed to avoid markdown parse errors. """ raw_text = span.get("text", "") modifiers = span.get("mo...
def lr_schedule0(epoch): """Learning Rate Schedule Learning rate is scheduled to be reduced after 10, 20, 30, 40 epochs. Called automatically every epoch as part of callbacks during training. # Arguments epoch (int): The number of epochs # Returns lr (float32): learning rate """ ...
def iface_definition(iface, ssid, psk): """Returns the corresponding iface definition as a string, formatted for inclusion in /etc/network/interfaces.d/""" return \ """iface {} inet dhcp wpa-ssid "{}" wpa-psk {} """.format(iface, ssid, psk)
def count_fq(zz): """ x is a sorted list with repeats. returns a list of [count,value] where count is the number of times value is repeated in the list""" res = [] s = 0 z = list(zz) z.append(-100000) for i in range(len(z)-1): if not z[i] == z[i+1]: v = [s+1,z[i]] ...
def is_cell(keyword): """Check if cell keyword Args: Profiles keyword Returns: True if cell keyword """ if keyword[0] == 'L': z1 = keyword[1] else: z1 = keyword[0] return bool(z1 == 'B')
def get_cyclic_label(cycle_nr): """ Return string to be inserted in the SMILES string to indicate a cycle Input ---------- cycle_nr: int, number of the cycle to be closed Output ------- str, string to be inserted in the SMILES string to indicate a cycle """ if cycle_nr > 9: ...
def tag(tag_name: str, data: str): """ Generate tag include tag name and tag data """ return '<' + tag_name + '>' + data + '</' + tag_name + '>'
def boolean(value: str) -> bool: """Convert a "human" boolean (Y, Yes, True, ...) to a Python boolean, raising a ValueError if it can't be converted. """ if value.lower() not in {"y", "yes", "n", "no", "true", "false"}: raise ValueError('Please enter either "y" or "n".') return value.lower()...
def make_car(manufacturer, model, **user_info): """Build a dictionary containing everything we know about a car.""" car = {} # create empty dictionary # add elements to dictionary car['manufacturer'] = manufacturer car['model'] = model for key, value in user_info.items(): car[key] = val...
def get_agency_url(operator_code): """Get url for operators""" operator_urls = { 'OId_LUL': "https://tfl.gov.uk/maps/track/tube", 'OId_DLR': "https://tfl.gov.uk/modes/dlr/", 'OId_TRS': "https://www.thamesriverservices.co.uk/", 'OId_CCR': "https://www.citycruises.com/", 'O...
def weighted_average(numbers): """ Returns the weighted average of the numbers array weighted by the frequency of the numbers :param numbers:List[int] :return: float """ num_dict = {} for n in numbers: num_dict[n] = num_dict.get(n, 0) + 1 weighted_sum = 0 for n in num_dict: ...
def features(runenvs): """ Information about the capabilities of the cluster. show the user a list of available "features" and additional computational nodes. """ output = {} for runenv in runenvs: if runenv['runenv'] in output: output[runenv['runenv']].append(runenv['fea...
def build_model_data(model, name, is_public, tlp, tags, intelligence, description): """ Builds data dictionary that is used in Threat Model creation/update request. """ if model == 'tipreport': description_field_name = 'body' else: description_field_name = 'description' data ...
def UnixifyPath(file_path): """converts \ to /""" return file_path.replace("\\", "/")
def trim_docstring(docstring): """ Uniformly trim leading/trailing whitespace from docstrings. Based on https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation """ if not docstring or not docstring.strip(): return '' # Convert tabs to spaces and split into lines lin...
def igInt(i,length=7): """ returns a string denoting an integer of appropriate length """ return '%*i' % (length,i)
def lower_obj(x): """ Lower cases objects, including nested objects. Adapted from here: https://stackoverflow.com/a/4223871/5965685 :param x: input object :return: output object lower-cased """ ignored_fields = ['path', 'g-function-path', 'g_b-function-path'] if isinstance(x, list): ...
def FindBuildRecordFromLog(description, build_info): """Find the right build record in the build logs. Get the first build record from build log with a reason field that matches 'description'. ('description' is a special tag we created when we launched the buildbot, so we could find it at this point.) """ ...
def prunejoin(dict_, list_, sep=", "): """Remove non-values from list and join it using sep.""" return sep.join([dict_.get(i) for i in list_ if dict_.get(i)])
def hash_sdbm(name): """Calculate SDBM hash over a string.""" ret = 0 for ii in name: ret = (ret * 65599 + ord(ii)) & 0xFFFFFFFF return "0x%x" % (ret)
def _build_merge_vcf_command_str(raw_vcf_path_list): """Generate command string to merge vcf files into single multisample vcf.""" command = " ".join([ 'bcftools merge', " ".join(raw_vcf_path_list) ]) return command
def _next_smooth_int(n): """Find the next even integer with prime factors no larger than 5. Args: n: An `int`. Returns: The smallest `int` that is larger than or equal to `n`, even and with no prime factors larger than 5. """ if n <= 2: return 2 if n % 2 == 1: n += 1 # Even. n -= ...
def check_instance_tag(tag_key, tag_value, app): """ Check if instance tag given matches the application configuration :param tag_key: :param tag_value: :param app: :return: bool >>> my_app = {'_id': '123456789', 'name': 'webapp', 'env': 'dev', 'role': 'webfront'} >>> check_instance_tag...
def format_int(value): """Cast the given value to an integer (and then to a string).""" return "{}".format(int(value))
def proposal(proposal): """Specific mocked proposal for reparameterisation tests""" proposal.use_default_reparameterisations = False return proposal
def is_int(value): """returns Tre if a value can be typecasted as an int, else Falsee""" try: int(value) return True except ValueError: return False
def fit_linear(x, a, b): """Function model for calculating linear fit Args: x (float): Variable that we try to fit a (float): the directional factor b (float): free element Returns: float: value of linear fit """ return a * x + b
def cascaded_cmp_with_partial_constants_and_false_end(a, b, c): """ >>> cascaded_cmp_with_partial_constants_and_false_end(3, 6, 8) False >>> cascaded_cmp_with_partial_constants_and_false_end(1, 6, 8) False >>> cascaded_cmp_with_partial_constants_and_false_end(4, 6, 8) False >>> cascaded_...
def extendedEuclidean( a, b ): """Calculate the GCD and linear combination of two integers using the Extended Euclidean algorithm. Arguments should be integers. Returns (r,s,t) such that (r = a*s + b*t) - Based on pseudocode from https://en.wikipedia.org/wiki/Extended_Euclidean_algorith...
def ensure_replicates_kwarg_validity(replicate_kwarg): """ Ensures `replicate_kwarg` is either 'bootstrap' or 'jackknife'. Raises a helpful ValueError otherwise. """ if replicate_kwarg not in ['bootstrap', 'jackknife']: msg = "`replicates` MUST be either 'bootstrap' or 'jackknife'." ...
def check_uniqueness_in_rows(board: list): """ Check buildings of unique height in each row. Return True if buildings in a row have unique length, False otherwise. >>> check_uniqueness_in_rows(['***21**', '412453*',\ '423145*', '*543215', '*35214*', '*41532*', '*2*1***']) True >>> check_unique...
def ensure_three_decimal_points_for_milliseconds_and_replace_z( datetimestring: str, ) -> str: """ To convert SciHub Datetimes to Python Datetimes, we need them in ISO format SciHub Datetimes can have milliseconds of less than 3 digits therefore we pad them with zeros to the right to make 3 digits, ...
def Total(data): """Returns the float value of a number or the sum of a list.""" if type(data) == float: total = data elif type(data) == int: total = float(data) elif type(data) == list: total = float(sum(data)) else: raise TypeError return total
def score_to(ans, keys): """ Return score the ans get. Ans and keys are key-value dict. key - expert id values - [homepage,gender,position,pic,email,location] """ num = len(ans) goals = 0 for i in ans: goal = 0 x = ans[i] y = keys[i] for j in range(len...
def _to_string(val): """Convert to text.""" if isinstance(val, bytes): return val.decode('utf-8') assert isinstance(val, str) return val
def getOverlap(a,b): """ get the number of ms overlapped between 2 time windows """ return max(0,min(a[1],b[1]) - max(a[0],b[0]))
def make_box(poly): """Generate a bounding box from a polygon""" x = [] y = [] for p in poly: for point in p: x.append(point[0]) y.append(point[1]) return (min(x), min(y), max(x), max(y))
def format_imports(import_statements): """ ----- examples: @need from fastest.constants import TestBodies @end @let import_input = TestBodies.TEST_STACK_IMPORTS_INPUT output = TestBodies.TEST_STACK_IMPORTS_OUTPUT @end 1) format_imports(import_input) -> output ----- :...
def _get_external_id(account_info): """Get external id from account info.""" if all(k in account_info for k in ('external_id', 'external_method')): return dict(id=account_info['external_id'], method=account_info['external_method']) return None
def bio2_to_bioes(tags): """ Convert the BIO2 tag sequence into a BIOES sequence. Args: tags: a list of tags in BIO2 format Returns: new_tags: a list of tags in BIOES format """ new_tags = [] for i, tag in enumerate(tags): if tag == 'O': new_tags.append(...
def hub_quantile_prediction_dict_validator(target_group_dict, prediction_dict): """ Does hub prediction_dict validation as documented in `json_io_dict_from_quantile_csv_file()` """ error_messages = [] # return value. filled next valid_quantiles = target_group_dict['quantiles'] prediction_quanti...
def clean_note_content(content): """Removes unwanted characters from note content.""" return content.strip().replace('"', "'")
def join(*args): """ join url with paths and url queries :param args: :return: """ return '/'.join([arg.rstrip('/') for arg in args if len(arg)])
def _join(s, tail): """ Return the concatenation of s + ' ' + tail if s is a truthy string, or tail only otherwise. """ return " ".join((s, tail)) if s else tail
def isSolvable(state): """ Function to check if the solution exists from the current node configuration """ invCount = 0 size = len(state) for i in range(0, size-1): for j in range(i+1, size): if (int(state[j]) and int(state[i]) and state[i] > state[j]): ...
def seconds_to_duration(seconds): """Return a string representation of the duration in seconds""" h = seconds // 3600 m = seconds % 3600 // 60 s = seconds % 3600 % 60 return ( f"{h:.0f}h:{m:02.0f}m:{s:02.0f}s" if h > 0 else (f"{m:.0f}m:{s:02.0f}s" if m > 0 else f"{s:.0f}s") ...
def replace_opts(rep_doc, opts): """Replace flags with parameter names. This is a simple operation where we replace the command line flags with the attribute names. Parameters ---------- rep_doc : string Documentation string opts : dict Dictionary of option attributes and k...
def get_key_of_item(d: dict, i: str) -> str: """Returns the key of item (string) i in dict d, or None if i is not in d.""" for key, item in d.items(): if item == i: return key return ""
def remove_empty_leading_trailing(lines): """ Removes leading and trailing empty lines. A list of strings is passed as argument, some of which may be empty. This function removes from the start and end of the list a contiguous sequence of empty lines and returns the result. Embedded sequences of ...
def is_crud(sql): """Check that given sql is insert , update, delete or select :param sql: Sql string to check for is_crud :return: Boolean result """ crud = ['insert', 'update', 'delete', 'select'] if not isinstance(sql, str): raise TypeError('`sql` argument is not valid. `sql` must...
def escape_newline(s: str) -> str: """Replaces each new line character (\\n) in the input with \\\\n""" return s.replace('\n', '\\n')
def gather_ec_by_fc(toplist, ec_blast ,counts): """[format finnal ec by function counts] Args: toplist ([list]): [top 20 predicted EC] ec_blast ([string]): [blast results] counts ([int]): [function counts] Returns: [string]: [comma sepreated ec string] """ if counts...
def json_serialization(ytd_json_op): """ helping func for json serialize (filtering only required data) """ ytd_data_array = [] for r_obj in ytd_json_op: ytd_data_array.append( { 'link': 'https://www.youtube.com/watch?v='+ r_obj['id']['videoId'], 'title': r_ob...
def booleanise(b): """Normalise a 'stringified' Boolean to a proper Python Boolean. ElasticSearch has a habit of returning "true" and "false" in its JSON responses when it should be returning `true` and `false`. If `b` looks like a stringified Boolean true, return True. If `b` looks like a str...