content
stringlengths
42
6.51k
def validate_mask(mask): """Check if the netmask is valid return mask as string in the range [0, 32] or False if not valid """ if not mask: return False mask = mask.strip() if mask.isdigit() and int(mask) >= 0 and int(mask) <= 32: return mask return False
def Sphere(individual): """Sphere test objective function. F(x) = sum_{i=1}^d xi^2 d=1,2,3,... Range: [-100,100] Minima: 0 """ y=sum(x**2 for x in individual) return y
def get_config_list(ranking, ckpt_path2is_3class): """Assemble a model list for a specific task based on the ranking. In addition to bundling information about the ckpt_path and whether to model_uncertainty, the config_list also lists the value of the metric to aid debugging. Args: ranking...
def calculate_version_code(values_dict, shift): """ Version Code is calculated based on the four version integers. Major is for crosswalk's large update, and minor is for based chromium. Major and minor will always be increasing, so use the sum of them is enough. For each major and minor refresh, build wil...
def generate_baseline(_iterable, window_length): """Generate a sliding baseline of an iterable Creates a list of sliding baselines for the iterable. e.g. if you pass in a list of len==5 with a baseline length of 2, we will generate: [ [elem0 (first element), elem1, elem2], [elem1, elem2...
def _tryint_(s): """ Tries to turn input into an integer. If that fails, returns input without conversion. Source: http://nedbatchelder.com/blog/200712/human_sorting.html """ try: return int(s) except ValueError: return s
def compare_bases_and_generate_key(tx_bases, rx_bases, measure): """Compares TX and RX bases and return the selected bits.""" if not (len(tx_bases) == len(rx_bases) == len(measure)): return None, "tx_bases(%d), rx_bases(%d) and measure(%d) must have the same length." % (len(tx_bases), len(rx_bases), len(measure...
def num_jobs(num_workers): """How many jobs to run in stress tests.""" return min(100, num_workers * 10)
def format_email_pass_id(submit_id): """Return tuple of the formatted email and password IDs based on the base submit_id value. Args: submit_id: id used to create unique element IDs Returns: tuple: formatted IDs: `(email_id, pass_id)` """ return [f'{submit_id}-{key}' for key in ['...
def octal_to_int(oct_str): """Convert an octal string to an int, respecting None""" return int(oct_str, 8) if oct_str else None
def sorted_dict(d, key=None, reverse=False): """ Return dictionary sorted using key. If no key provided sorted by dict keys. """ if key is None: return dict(sorted(d.items(), key=lambda e: e[0], reverse=reverse)) return dict(sorted(d.items(), key=key, reverse=reverse))
def _runtime_maybe(ctx, variants): """Check predicates at runtime and return matched value or None.""" for value, predicate in variants: if callable(predicate): if predicate(ctx): return value elif predicate: return value
def find_element_by_key(obj, key): """ Recursively finds element or elements in dict. """ path = key.split(".", 1) if len(path) == 1: if isinstance(obj, list): return [i.get(path[0]) for i in obj] elif isinstance(obj, dict): return obj.get(path[0]) el...
def unary_superposition_mop_r_3(ar_1: tuple, mop: tuple): """substitution for unary multioperations. :param tuple f: multioperation which. :param tuple g: multioperation for :rtype: tuple """ dic_mop = { 1: (0,), 2: (1,), 3: (0, 1), 4: (2,), 5: (0, 2), ...
def test_rar (archive, compression, cmd, verbosity, interactive): """Test a RAR archive.""" cmdlist = [cmd, 't'] if not interactive: cmdlist.extend(['-p-', '-y']) cmdlist.extend(['--', archive]) return cmdlist
def count_ed_mgs(db): """ Just return how many times Ed answered. """ try: count = db.get('ed_info')['qty_answed_message'] except: count = 1 return count
def swap(heights_list, index01, index02): """swap two positions in a list at given indexes Args: heights_list (list): iterable in which swapping occurs index01 (int): index of first element index02 (int): index of second element Returns: list: list with element positions s...
def gather_types(input_step, varname): """ Given and input step, return a SPARQL fragment to gather the types for the step :param input_step: :return: SPARQL fragment as string """ if not input_step['object']['literal']: return ' ?' + input_step['object']['name'] + ' a ?' + varname + ' ....
def get_recurrence_rule(doc): """Recurring Event not implemeted.""" # until = datetime.strptime(doc.repeat_till, '%Y-%m-%d').strftime("%Y%m%dT%H%M%SZ") # if doc.repeat_on == "Every Day": return ["RRULE:FREQ=DAILY;UNTIL=%s;BYDAY=%s"%(until,get_by_day_string(doc))] # elif doc.repeat_on == "Every Week": return ["RRUL...
def dict_max(*ds): """Take the maximum of dictionaries. Args: *ds (dict): Dictionaries. Returns: dict: `max(*ds)`. """ if not all([set(d.keys()) == set(ds[0].keys()) for d in ds[1:]]): raise ValueError("Dictionaries have different keys.") return {k: max([d[k] for d in d...
def add_arrays(digits1, digits2): """Adds numbers represented by arrays of digits.""" if len(digits1) > len(digits2): longer = digits1[::-1] shorter = digits2[::-1] else: longer = digits2[::-1] shorter = digits1[::-1] sum_digits = [] carry = 0 for i, d in enumerat...
def is_multiclass(classes): """ Returns True if values in classes are anything but 1, 0, True, or False, otherwise returns False. """ try: return len(set(int(i) for i in classes) - {0, 1}) != 0 except ValueError: return True
def dedupe(duped_data, encoding: str = 'utf-8'): """ Removes duplicates from a given data structure """ # Import libraries from hashlib import md5 codes = set() deduped_data = [] for item in duped_data: hash_digest = md5(str(item).encode(encoding)).hexdigest() if hash...
def indexed_tags(tags): """Returns a list of tags that must be indexed. The order of returned tags is from more selective to less selective. """ if not tags: return [] return sorted( set(t for t in tags if t.startswith(('buildset:', 'build_address:'))) )
def fibonacci_recursive(number): """Returns the nth Fibonacci number in the Fibonacci sequence. The Fibonacci numbers are the numbers in the Fibonacci sequence, defined by the equation and seed values: F(n) = F(n - 1) + F(n - 2) F(0) = 0 F(1) = 1 * O(2^n) time complexity * O(n) space complexity A...
def longuest_sub_array(arr): """ This function caculates the longuest sub array in a list A sub array is an array which doesn't contain any 0 It returns the index of the last element which composes the sub array and the length of the sub array """ sub_arrays = [] last_index = 0 len...
def _faster_comp(candidates, pairs): """ helper function for run(), evaluates winner of pairs, but faster (by about two orders of magnitude) than _graph() (now deprecated) """ # This computation doesn't create the whole graph, but relies on the idea # that the winner will never have an edge pointing to it edges ...
def getColPermutations(possible_columns, max_num_of_perms = 100): """ Get all possible combinations given a list of column names :return: Given Input = [a,b,c] Then, Output= [ [a], [b], [c], [a,b], [a,c], [b,c] ] """ permutations = {col: 1 for col in possible_columns} for perm_...
def get_hover_string(n_commits, dt): """ """ if n_commits == 0: n_txt = 'No commits on ' elif n_commits == 1: n_txt = '1 commit on ' else: n_txt = f'{n_commits} commits on ' if dt == '': return '' else: return n_txt + dt.strftime('%b %d, %Y')
def isStructureCompatible(lp1, lp2, bp): """ Checks, if the region within lp1 and lp2 is structurally balanced """ x = lp1 + 1 while x < lp2: if bp[x] <= lp1 or bp[x] > lp2: return False if x == bp[x]: x += 1 else: x = bp[x] + 1 return ...
def dic_partiel(n): """retourne un dictionnaire partiel de mot de taille n correctement parenthese""" if n <= 0: return [""] elif n == 1: return ["()"] sub_list = dic_partiel(n-1) new_list = [] for word in sub_list: new_list.append(word + "()") new_list.append("(...
def shorten_file_by_removing_comments(content: str) -> str: """ Removes all lines with line comments or block comments from the file content, so that the ast parser does not get confused. """ lines = content.splitlines(keepends=True) delete = False for line_num, line in enumerate(lines): ...
def gtex_location_to_gwas_location_(gtex_location: str) -> str: """Converts variant locations in GWAS catalog format to GTEx format. i.e. given 'chr1_64764_C_T_b38', returns '4:79296443'. """ parts = gtex_location.split("_") chr = parts[0][3] return f"{chr}:{parts[1]}"
def error(endpoint, reason, advice=None): """Generate error packet. `endpoint` Optional endpoint name `reason` Error reason `advice` Error advice """ return u'7::%s:%s+%s' % (endpoint or '', (reason or ''), (advic...
def get_process_identifier(args): """by looking at arguments we try to generate a proper identifier >>> get_process_identifier(['python', 'echo.py', '1']) 'python_echo.py_1' """ return '_'.join(args)
def changes_existing_nodes(nodes_exist, alpha, dict_proj, dict_proj_changed_exist): """ For nodes that are in both sequential snapshots, update their embedding by their current embedding and new calculated embedding. Do it only for those that their neighbours are changed. :param nodes_exist: Nodes that ...
def verify_expected_list(list_expected_strs, list_actual_strs): """ Return True if each str in list_expected_strs is in list_actual_strs """ return all(elem in list_actual_strs for elem in list_expected_strs)
def xpath_class_check(cls: str) -> str: """ Parameters ---------- cls : str The CSS class name to check Returns ------- xpath: str An xpath expression for finding an element with class `cls` """ return f"contains(concat(' ',normalize-space(@class),' '),' {cls} ')"
def DL_ignore(answers): """Return False if any dictionary-learning method was selected. Arguments --------- answers: dict Previous questions answers. Returns ------- bool True if DL verbosity question should be ignored. """ expected = ['ITKrMM', 'wKSVD', 'BPFA'] ...
def in_any_list(list1, list2): """ Check if any items in list1 are in list2 :param list1: list :param list2: list :return: """ list1 = list1.split(" ") if isinstance(list1, str) else list1 list2 = list2.split(" ") if isinstance(list2, str) else list2 return any(i in list2 for i in li...
def path_to_startdate(path): """ Given a path of the form fall2018/coursename, returns corresponding start date. >>> path_to_startdate('fall2018/coursename') '2018-09-01' >>> path_to_startdate(u'fall2018') '2018-09-01' >>> path_to_startdate('spring2018/foo/bar/baz') ...
def complement(sequence, reverse=False): """ Return complement of sequence """ complements = {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'} sequence = reversed(sequence) if reverse else sequence return ''.join(complements.get(base, 'N') for base in sequence)
def jsonapi_errors(jsonapi_errors): """Construct api error according to jsonapi 1.0 :param iterable jsonapi_errors: an iterable of jsonapi error :return dict: a dict of errors according to jsonapi 1.0 """ return {'errors': [jsonapi_error for jsonapi_error in jsonapi_errors], 'jsonapi': ...
def map_quads(coord): """ Map a quadrant number to Moran's I designation HH=1, LH=2, LL=3, HL=4 Args: coord (int): quadrant of a specific measurement Returns: classification (one of 'HH', 'LH', 'LL', or 'HL') """ if coord == 1: return 'HH' elif coord == 2: ret...
def group_sequential(a): """ Purpose: Group the numerical list or 1D array by sequential blocks. Return: a list of list. Example: >>> a=[2,3,4,67,78,79,102] >>> print mathex.group_sequential(a) >>> a=[2,3,4,67,78,79,102,103] >>> print mathex.group_sequential(a) >>...
def publish_file_in_ipfs(ipfs_client, filepath, wrap_with_directory=True): """ push a file to ipfs given its path """ try: with open(filepath, 'r+b') as file: result = ipfs_client.add( file, pin=True, wrap_with_directory=wrap_with_directory) if wrap_wi...
def solution(A, B, M, X, Y): # write your code in Python 2.7 """ floors = [0 to M ] elevator: max capacity of X people weight limit of Y lbs N people gathered in floor[0] standing in a queue() k: a kth numbered person A[k] = weight, B[k] = target floor or dst ex)A[0] and B[0]...
def climb_stairs(k): """ Return the number of ways that `k` stairs can be climbed given that you can climb either 1 or 2 stairs at a time """ num_ways = [1] for idx in range(1, k+1): one_step = num_ways[idx-1] two_step = num_ways[idx-2] if idx > 1 else 0 num_ways.append(...
def get_location_list(state_alert_list): """Get lat, lon list from state alert list""" locations = [] for item in state_alert_list: locations.append([item["lat"], item["lon"]]) return locations
def calc_tot_orbits(n_orbit_dict): """Calculate the total number of direct and indirect orbits in n_orbit_dict. Parameters ---------- n_orbit_dict : dictionary A dictionary where each key is an object in the system and each value is a tuple of 2 values. The first represents the numb...
def ee_bands_rgb(collection): """ Earth Engine band names """ dic = { 'Sentinel2': ['B4','B3','B2'], 'Landsat7': ['B3','B2','B1'], 'CroplandDataLayers': ['cropland'] } return dic[collection]
def get_atom_neighbours(atom_idx: int, molecule_covalent_nbrs: list): """Get all neighbours for atom_idx. Extract all covalent bonding partner (alpha), all nearest neighbours (beta), and all nearest-nearest neighbours (gamma) for atom with index 'atom_idx'. Args: atom_idx: index of atom to ext...
def get_bcl2fastq_read_type_map(read_info, sample_index_read=None, dual_indexed=False, ignore_dual_index=False): """ Get a mapping between ReadInfo read name (R1,I1,I2,R2) and bcl2fastq output file naming (R1/R2/R3/R4/I1) The guarantee here is that the 10X sample index will always be on I1, if gene...
def circular_shift_key_one_left(key): """The function does a cyclic shift left of the whole key. To be able to shift the whole 40 bytes left in a cyclic manner, the function shifts the bits between two adjacent bytes each time""" l = len(key) return [ ((key[i] << 1) & 0xff) | ((key[(i + 1) % l] & 0...
def get_include_role(validation): """Returns Included Role""" try: if 'tasks' in validation: return validation['tasks'][0]['include_role']['name'] return validation['roles'][0] except KeyError: return list()
def meta_name(file_name): """Generate the name of the meta file""" return "{}.json".format(file_name)
def to_list(tup: tuple): """Convert from tuple to packed list Allows us to call function with arguments in a loop Parameters ---------- tup: tuple tuple of objects to convert to packed list Raises ------ ValueError If passed uneven number of arguments without a list. P...
def mock_glob_method(path): """ Given a path input, returns a list of candidates """ if ".x86" in path: return ["linux"] if ".app" in path: return ["darwin"] if ".exe" in path: return ["win32"] if "*" in path: return "Any" return []
def GetFunctionToGLVersionsMap(gl_versions): """Construct map from a function names to GL versions which define the function. Args: extensions: Map of gl versions => functions. Returns: Map of function name => gl versions. """ function_to_gl_versions = {} for gl_version, functions in gl_versions....
def capitalized_comp_name(context): """return the component name in capitalized format Args: context (dict): complete package and component transformation Returns: str: component name in capitalized format, underscore being removed. Examples: >>> context = {componentName="anot...
def combineRulesWithOperator(listOfRules, operator): """ Takes a list of rules and makes an overall rule that ties them together with the AND or the OR operator Parameters ---------- listOfRules: list A list of string representation of rules operator: str Should be either AN...
def _sign(number): """ Get sign of the given number :param number: :type number: int :return: -1, 0, 1 :rtype: int """ if number > 0: return 1 if number < 0: return -1 return 0
def _failsafe_values_atom(schema, values, errors, source, kw): """Map all erroneous inputs to a single value.""" for key in errors: kw[key] = values return kw
def _is_complex_type(item_type: str) -> bool: """ Checks to see if the type is a complex type (as primitives are decoded differently) Args: item_type: A string that has the type of the object Returns: True if complex, False if primitive """ if item_type == 'Boolean' or i...
def jossann_reclassifier(original_classifier, probability): """ Reclassify: note that if probabilities are (0, 1) or (1, 0) then we override the original classifier. """ if sum(probability) > 1: probability = tuple([i / sum(probability) for i in probability]) if probability in [(1, 0), ...
def map_gen(size): """Generates map""" map_ = [] for _ in range(size): map_.append([" ~"] * size) return map_
def asbool(obj): """ Interprets an object as a boolean value. :rtype: bool """ if isinstance(obj, str): obj = obj.strip().lower() if obj in ('true', 'yes', 'on', 'y', 't', '1'): return True if obj in ('false', 'no', 'off', 'n', 'f', '0'): return False...
def head_number_poly(cappa): """Calculate head number for a given pump's typical number. The polynomial has been calculated applaying the curve fitting at nodes cappa .2 .3 .4 .5 .6 .7 .8 .9 1.0 1.1 1.2 psi .583 .575 .560 .535 .515 .489 .465 .441 .415 .395 .380 weights ones(cappa)...
def get_service_id(path): """Get the service ID (e.g. octue.services.<uuid>) from a topic or subscription path (e.g. projects/<project-name>/topics/octue.services.<uuid>) :param str path: :return str: """ return path.split("/")[-1]
def calculate(x: int, y: int = 1, *, subtract: bool = False) -> int: """Calculates the sum (or difference) of two numbers. Parameters: `x` : int, required The first number `y` : int, optional The second number (default is 1) `subtraction`: bool, optional Whether to perform s...
def exp_opts(expansion): """Return the options of an expansion. If options are not defined, return {}""" if isinstance(expansion, str): return {} return expansion[1]
def avg_brightness(image_list): """ A list of grey scale images """ brightness_per_block=[] for image in image_list: img_shape = image.shape img_Size = image.size total=0 for i in range(0,img_shape[0]): for j in range(0,img_shape[1]): total...
def volCuboid(length: float, breadth: float, height: float) -> float: """Finds volume of a cuboid""" volume: float = length * breadth * height return volume
def toString(obj): """ Convert the diffmask object to string. If it has a `toString' method, use it. Otherwise, hope it'll converted implicitly. This is a cheap wrapper to avoid Python2/3 string incompatibility. """ if hasattr(obj, 'toString'): return obj.toString() else: return obj
def shellsplit(text): """Very simple shell-like line splitting. :param text: Text to split. :return: List with parts of the line as strings. """ ret = list() inquotes = False current = "" for c in text: if c == "\"": inquotes = not inquotes elif c in ("\t", "...
def _has_sectors(tax_name: str, ignore_sectors: bool) -> bool: """Determine whether we are doing a sector-based forecast.""" return tax_name in ["birt", "sales", "wage", "rtt"] and not ignore_sectors
def fast_exp(a, b): """Returns a^b in O(lg n) time complexity, where a and b are both integers.""" if not isinstance(a, int) == isinstance(b, int) == True: raise TypeError('a and b should both be integers.') return None if b < 0: return 0 result = 1 while (b != 0): ...
def redis_stream_id_add_one(message_id): """Add one to the message ID This is useful when we need to xrange() events exclusive of the given ID, rather than inclusive of the given ID (which is the sensible default). There is no chance of missing events with this method. """ milliseconds, n = map...
def tokenize(s): """ tokenize """ #s = re.sub('\d+', NUM, s).lower() # tokens = nltk.RegexpTokenizer(r'\w+|<sil>|[^\w\s]+').tokenize(s) tokens = s.split(' ') return tokens
def escape_ansi(line): """It removes the ansi codes from a string.""" import re ansi_escape=re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]') return ansi_escape.sub('',line)
def _split_member(member): """Splits an IAM member into type and optional value. Args: member (str): The IAM member to split. Returns: tuple: The member type and optionally member value. """ # 'allUsers' and 'allAuthenticatedUsers' member do not contain ':' so they # need to be...
def _is_float(value): """Checks if the value is float. Which means it contains two non empty integer parts separated by . Args: value (str): The value. Returns: bool: True if the value represents a float. """ if "." not in value: return False a, b = value.split(".", 1) ...
def dna_to_rna(seq): """ Convert a DNA sequence to RNA. """ # Determine if original sequence was uppercase seq_upper = seq.isupper() # Convert to lowercase seq = seq.lower() # Swap out 't' for 'u' seq = seq.replace('t', 'u') # Return upper or lower case RNA sequence if seq...
def is_list_of_dicts(li: list) -> bool: """Return true if input is a list of dicts.""" return all(isinstance(elem, dict) for elem in li)
def tolist(item): """ tolist wraps a single value in a list, or converts the item to a list """ return item if type(item) == list else list(item)
def yesno_to_bool(s): """Convert yes/no answer to True/False""" if s == "y" or s == "yes": return True return False
def generic_type_template(tipo: str, name: str, behaviour: str, result0: str, result1: str) -> str: """Template for feature behaviour reason generated from DICE Returns: str: generic behaviour based on the type """ dict_type = { "category": (f"{name} {behaviour} from {result0} to {resu...
def from_c_string(addr): """utf-8 decode a C string into a python string""" if addr is None: return None if type(addr) == str: return addr return addr.decode('utf-8')
def rotate_right(arr): """ Rotate a copy of given 2D list clockwise by 90 degrees and return a new list. :param arr: A 2D-list of arbitrary dimensions. :return: A list that is "arr" rotated 90 degree clockwise by its center. """ n = len(arr) m = len(arr[0]) res = [[Non...
def _dict_to_ebuild(dictionary): """Helper to format a dictionary into an ebuild file.""" output = [] for key in sorted(dictionary.keys()): output.append('%s="%s"' % (key, dictionary[key])) output.append('\n') return '\n'.join(output)
def present_to_future_value(ir, t): """ Returns a coefficient to convert money from one point in time to t years in the future, with an annual interest rate of ir. This is the inverse of future_to_present_value if calculated with the same rate and number of years. Example: >>> round(present_...
def basic_backquote_string_evaluator(val): """The basic evaluator for backquoted strings""" return eval(val,globals(),locals())
def calculate_center(shape: tuple): """ Calculate and return the center point of ``shape``. :param shape: A tuple (width, height) of odd numbers :return: A ``tuple`` (x, y) containing the center points coordinates """ if any(d%2 == 0 for d in shape): raise ValueError("width and height ...
def flatten(x): """ Flatten lists of lists of any depth. Preserves tuples. """ result = [] for el in x: if hasattr(el, "__iter__") and not isinstance(el, str) and type(el)!=tuple and not issubclass(type(el), tuple): result.extend(flatten(el)) else: result.appe...
def cleanup_double_endline(text: str) -> str: """Removes unnecessary empty lines in text""" return text.replace('\n\n\n', '\n')
def _ContinuationAlignStyleStringConverter(s): """Option value converter for a continuation align style string.""" accepted_styles = ('SPACE', 'FIXED', 'VALIGN-RIGHT') if s: r = s.strip('"\'').replace('_', '-').upper() if r not in accepted_styles: raise ValueError('unknown continuation align style: ...
def dispatch_strategy(declaration): """How are we going to call the underlying implementation of a declaration? There are two strategies: - use_derived: we want to call the implementation on CPUDoubleType (or a similar, derived Type instance). Because these derived instances deal ...
def sanitize_code(code): """ Make a canonical text version of a code - a 5 digit, 0-padded string """ try: code = "%05i"%code except TypeError: code=str(code) return code
def cmpversion(a, b): """Compare versions the way chrome does.""" def split_version(v): """Get major/minor of version.""" if '.' in v: return v.split('.', 1) if '_' in v: return v.split('_', 1) return (v, '0') a_maj, a_min = split_version(a) b_maj, b_min = split_version(b) if a_maj...
def sizeof_fmt(num): """ Convert file size to human readable format. """ for x in ['b', 'K', 'M', 'G', 'T']: if num < 1024.0: return "{0:.2f}{1}".format(num, x) num /= 1024.0