content
stringlengths
42
6.51k
def serialize_grades(grades): """ Returns an string with the representation of the grades in XML format. :param grades: grades to serialize :return: an string with the representation in the desired format """ result = '<scores>\n' for student_id, grade in grades.items(): result += ...
def exception_handler(main_graph_hover, selection): """ It handles the situation where no selection is made either from the map of the filtering option Parameters ---------- main_graph_hover : str The value as returned by hovering on the map selection: str The values as retur...
def get_configured_lease_duration(node_config): """ Just kidding. Lease duration is hard-coded. :return int: The number of seconds after which a newly acquired lease will be valid. """ # See lots of places in Tahoe-LAFS, eg src/allmydata/storage/server.py return 31 * 24 * 60 * 60
def shift(l, n): """ Shift a list grasshopper style. """ n = n % len(l) head = l[:n] l[:n] = [] l.extend(head) return l
def _get_partial_paths(paths, include_full=False): """Split flag paths into groups.""" partials = set() for path in paths: parts = path.split(".") limit = len(parts) if not include_full: limit -= 1 for i in range(limit): partials.add(".".join(parts[0 :...
def ismacro(o): """ Is the given object a macro? """ return hasattr(o, 'macroName')
def get_num_gophers(blades, remainders, M): """Find no. of gophers given no. of blades and remainders.""" for i in range(1, M + 1): congruences = all([i % b == r for b, r in zip(blades, remainders)]) if congruences: return i return None
def dedupe_matching(matching): """ Remove duplicates node pairs from the output of networkx.algorithms.max_weight_matching since we don't care about order. Args: matching (dict): output from networkx.algorithms.max_weight_matching. key is "from" node, value is "to" node. Returns: list...
def month_to_quarter(month_num): """Convert the month, provided as an int, to a yearly quarter, also an int. Args: month_num (int): The month number to convert (1-12) Returns: int: The quarter this month is in Examples: >>> month_to_quarter(1) 1 >>> month_to_qu...
def fact_while(n): """Returns factorial of the n using while loop.""" s = 1 while n > 1: s *= n n -= 1 return s
def to_similarity(distance, length): """Calculate a similarity measure from an edit distance. Parameters ---------- distance : int The edit distance between two strings. length : int The length of the longer of the two strings the edit distance is from. Returns ----...
def invert_dictionary(in_dict): """ For main_bag -> bags inside, invert to bag_name -> contained by bags """ inverted_dict = dict() for main_bag, contains_bags in in_dict.items(): if main_bag not in inverted_dict: inverted_dict[main_bag] = set() for _num, bag in c...
def minor_allele_frequency(locus, collection): """Suggests just 2 alleles per locus input: locus [integer]; collection of sequences [something iterable = string, list, tuple...] returns: frequency of the minor allele (MAF)""" snpList = [x[locus] for x in collection] maf = snpList.count(sn...
def list_comparator(o_list, t_list): """ Determine whether two lists contain the same elements :param o_list: :param t_list: :return: """ try: if sorted(o_list) == sorted(t_list): return True except Exception: return False else: return False
def _count_cross_inversions(P, Q): """ Counts the inversions across two sorted arrays. And combine the two arrays into one sorted array For all 1<= i<=len(P) and for all 1 <= j <= len(Q), if P[i] > Q[j], then (i, j) is a cross inversion Parameters ---------- P: array-like, sorted in non-...
def vocab(name): """ Given a property such as 'rel', get its fully-qualified URL in our JSON-LD vocabulary. """ return "http://api.conceptnet.io/ld/conceptnet5.7/context.ld.json#" + name
def frequency_to_band(freq): """ band code from frequency in GHz """ if freq < 1: return None elif freq >= 1 and freq < 2: return "L" elif freq >= 2 and freq < 4: return "S" elif freq >= 4 and freq < 8: return "C" elif freq >= 8 and freq < 12: return "X" elif fr...
def get_XA_mapping(tags, max_mm=None): """ Return a list of positions from the XA sam file data, these are additional mappings Arguments: - `tags`: The tags argument """ tlist = [] for tpair in tags: if tpair[0] == 'XA': for xadat in tpair[1].split(';')[:-...
def get_automatic_parallelization_options(max_num_machines=1, max_wallclock_seconds=1800): # pylint: disable=invalid-name """Return an instance of the automatic parallelization options dictionary. :param max_num_machines: set the number of nodes, default=1 :param max_wallclock_seconds: set the maximum num...
def valid_youtube_video(url): """ We don't want to process channels or user accounts, so we'll filter those out here. :param url: the YouTube URL we need to check. :return: True if it's a video; false if it's a channel, user, or playlist. """ banned_keywords = ['user', 'channel', 'playl...
def EnumValueCrossRefLabel(enum_value_name): """Enum value cross reference label.""" return 'envoy_api_enum_value_%s' % enum_value_name
def find_segments(node_list: list) -> str: """ Find segment ranges of node IDs on each rack """ node_list.sort() list_len = len(node_list) list_range = "" if node_list: list_range = str(node_list[0]) for i in range(list_len - 1): if node_list[i+1] != node_list[i] ...
def roundAllFloats(lista, l): """Round to 3 decimals""" nlista = [] for ii in lista: tt = ii[:l + 1] for jj in ii[l + 1:]: if jj > 100.0: jj = round(jj, -1) nn = round(jj, 3) tt.append(nn) nlista.append(tt) return nlista
def findBest(dictionary, comparator): """returns a list containing the best name and corresponding value-work tuple. The choice depends on the comparator function.""" keyList = [] keyIndex = 0 res = [] for key in dictionary: keyList.append(key) compareTemp = dictionary[keyList[0]] ...
def pid_from_context_or_data(value, context, **kwargs): """Get PID from marshmallow context.""" pid = (context or {}).get('pid') if pid is None: return value else: return pid.pid_value
def _reconstruct(x, y, r1, r2, ll, gamma, rho, sigma): """Reconstruct solution velocity vectors.""" V_r1 = gamma * ((ll * y - x) - rho * (ll * y + x)) / r1 V_r2 = -gamma * ((ll * y - x) + rho * (ll * y + x)) / r2 V_t1 = gamma * sigma * (y + ll * x) / r1 V_t2 = gamma * sigma * (y + ll * x) / r2 ...
def ordered_unique(ls): """Return the list with unique elements while keeping the elements ordered by first appearance""" seen = set() return [l for l in ls if not (l in seen or seen.add(l))]
def generate_time_series_exponential_growth_data(n, x0=1.565, b=1.1194): """ generate exponential growth SARS active case simulation data :param n: number of points :param x0: initial value of infected people :param b: growth rate :return: time series points to show exponential growth of a...
def set_stitch_num(stitch_num: int) -> str: """Set the stitch number. Part of the supported Knitout extensions.""" return 'x-stitch-number {}'.format(stitch_num)
def box_inside_box(inner,outer): """Return true if the inner bbox is inside or equal to the outer bbox. Parameters: inner (list): List of floats for inner bounding box of form [x0,y0,x1,y1] outer (list): List of floats for outer bounding box of form [x0,y0,x1,y1] Returns: bool: Whether inner is ...
def reverse_dictionary(title_to_freebase): """ Create a redirect to title dictionary. """ redirect_to_title = {} for link in title_to_freebase: for redirect in title_to_freebase[link]['alternate_titles']: redirect_to_title[redirect] = link return redirect_to_title
def get_hyperion_unique_id(server_id: str, instance: int, name: str) -> str: """Get a unique_id for a Hyperion instance.""" return f"{server_id}_{instance}_{name}"
def _CK_VERSION_to_tuple(data): """Convert CK_VERSION to tuple.""" return (data['major'], data['minor'])
def recursive_decode(integers, bits=16): """Turns a list of integers into a new list of integers where the values in the first are merged if it looks like a higher order integer split over two integers. (Code here adapted from the official python-mmtf package.) :param list integers: the integers t...
def atlas2aparc(atlas_name, hemi=None): """ Find freesurfer atlas aparc from atlas key. Valid keys: desikan, destrieux, dkt if `hemi` is specified, it a valid filename will be returned; otherwise a format string will be returned.""" if atlas_name == 'desikan': annot_file_template = '%s.ap...
def inject(*components): """Injects web components. >>> inject( >>> '<snap>a</snap>', >>> '<snap>b</snap>' >>> ) >>> <snap>a</snap><snap>b</snap> Args: components (WebComponents): The web components to inject. Returns: str: The string with injected web components...
def lazy_begin0(*bodys): """Racket-like begin0: run bodys in sequence, return the first return value. Lazy; each body must be a thunk (0-argument function), to delay its evaluation until begin0() runs. g = lambda x: lazy_begin0(lambda: 23*x, lambda: print("hi")) ...
def wrap_stringlist(strlist, width=75): """ Wraps the text lines of a list to width characters. >>> wrap_stringlist(['REM foo bar baz foo bar baz blah blub', 'REM 2foo bar baz foo bar baz blah blub'], 36) ['REM foo bar baz foo bar baz blah\\nREM blub\\n', 'REM 2foo bar baz foo bar baz blah\\nREM blub\\n...
def uniq(d): """Returns the unique value of a dictionary, else an empty dictionary.""" result = {} for k, v in d.items(): if result == {} or result == v: result = v return result # temp hack - ITM 3 ports have slight differences. else: print(f"WARNING: un...
def dest(current_command): """Return dest Mnemonic of current C-Command""" if "=" in current_command: #split string at = and return everything before the = (if = in string) command_list = current_command.split("=") return command_list[0] else: return ""
def time_delta(t_delta: float) -> str: """ Convert a timestamp into a human readable timestring. Args: t_delta (float): Difference between two timestamps of time.time() Returns: Human readable timestring. """ hours = round(t_delta // 3600) minutes = round(t_delta // 60 % 60)...
def longestCommonPrefix(strs): """ :type strs: List[str] :rtype: str """ i = 0 while i < min(len(strs[0]), len(strs[1])) and strs[0][i] == strs[1][i]: i += 1 if i == 0: return "" comstr = strs[0][:i] print(i,comstr) for str in strs: if str[:i] == comstr: ...
def multiseq_flops(V, D): """ Given the number of coarse clusters and the dimension of the data, compute the number of flops to required to rank each coarse vocabulary to the query. """ # (total coarse vocabulary) * (dims per coarse split) * (flops per squared distance) return (2 * V) * (D /...
def compare_range(a, b): """ >>> compare_range(1, "-10") True >>> compare_range(1, "10-") False >>> compare_range(20, "-10") False >>> compare_range(1, "10-20") False >>> compare_range(1.0, "0-1.0") True >>> compare_range(100, "-") True >>> compare_range("b", "a-z...
def fixDelex(filename, data, data2, idx, idx_acts): """Given system dialogue acts fix automatic delexicalization.""" try: turn = data2[filename.strip('.json')][str(idx_acts)] except: return data if not isinstance(turn, str): for k, act in turn.items(): if 'Attraction...
def get_discipline(discipline): """ helper guess the discipline from the competition name / description """ if 'boulder' in discipline.lower(): return 'Bouldern' elif 'lead' in discipline.lower(): return 'Lead' elif 'speed' in discipline.lower(): return 'Speed'...
def GetComment(plist, comments): """Get comment for a given property list.""" try: label = plist["Label"] except KeyError: return None if label in comments: return comments[label] return None
def _get_provider_type(row): """ groups provider by type """ if row['provider_category']: if 'npr' in row['provider_category'].lower(): return 'NPR' elif 'getty' in row['provider_category'].lower(): return 'Getty' elif 'istock' in row['provider_category']....
def _prepare(data: dict): """ Remove fields `_id` and `name` and return data. """ return {k: v for k, v in data.items() if k not in ('_id', 'name')}
def m_to_inches(value): """Converts distance in meters to inches Args: value: floating point representing the distance in meters Returns: distance in inches """ if value is None: return None return value / 39.37
def validate_allocation_strategy(allocation_strategy): """Validate allocation strategy :param allocation_strategy: Allocation strategy for ComputeResource :return: The provided value if valid Property: ComputeResources.AllocationStrategy """ valid_strategies = [ "BEST_FIT", "BES...
def _get_qualified_name(obj): """Return the Fully Qualified Name from an instance or class.""" module = obj.__module__ if hasattr(obj, '__name__'): obj_name = obj.__name__ else: obj_name = obj.__class__.__name__ return module + '.' + obj_name
def get_first_non_blacklisted(blacklist): """Return the first integer not in `blacklist`. """ i = 1 while i in blacklist: i += 1 return i
def init_parameters(parameter): """Auxiliary function to set the parameter dictionary Parameters ---------- parameter: dict See the above function initActivations for further information Returns ------- parameter: dict """ parameter['decay'] = 0.75 if 'decay' not in paramet...
def calculate_pos_deviation_square(error, sl): """ Calculate the square deviation between a given error value and a significance level if the deviation is positive (>0) Parameters ---------- error : error sl : significance level Returns ------- square deviation or 0 ...
def normalize_vendor(vendor): """ Return a canonical name for a type of database. """ if not vendor: return 'db' # should this ever happen? elif 'sqlite' in vendor: return 'sqlite' elif 'postgres' in vendor or vendor == 'psycopg2': return "postgres" else: return vend...
def clamp(value, min_value, max_value): """Return value constrained to be within defined limits Parameters ---------- value : int or float Original value min_value : int or float Allowed minimum value max_value : int or float Allowed maximum value Returns ------...
def isValidWit(text): """ Returns True if the text is related to the weather. Arguments: text -- user-input, typically transcribed speech """ return any(d.get(u'intent', u'') == u'need_umbrella' for d in text.get(u'outcomes', []))
def convex_hull(points): """Computes the convex hull of a set of 2D points. Input: an iterable sequence of (x, y) pairs representing the points. Output: a list of vertices of the convex hull in counter-clockwise order, starting from the vertex with the lexicographically smallest coordinates. Impl...
def validate_item_name(name, prefix, suffix): """Verifies that ``name`` starts with ``prefix`` and ends with ``suffix``. Returns ``True`` or ``False``""" return name[: len(prefix)] == prefix and name[-len(suffix) :] == suffix
def calculate_shape_keeping_aspect_ratio(height: int, width: int, min_size: int, max_size: int): """ The function changes spatial sizes of the image keeping aspect ratio to satisfy provided requirements. The behavior of this function is equivalent to the output shape calculation of the pre-processor block o...
def new_velocity(pos, vel): """Calculates the new velocity of the moon""" for j, moon in enumerate(pos): for pair_moon in pos: for i in range(3): if moon[i] > pair_moon[i]: vel[j][i] -= 1 elif moon[i] < pair_moon[i]: ...
def create_title_abstract_col(data): """Create new col from title and abstract cols of api response""" for dictionary in data: dictionary['patent_title_abstract'] = str([dictionary['patent_title'] + '. ' + dictionary['patent_abstract']][0]) return data
def preprocess_text(sentence): """Handle some weird edge cases in parsing, like 'i' needing to be capitalized to be correctly identified as a pronoun""" cleaned = [] words = sentence.split(' ') for w in words: w = w.lower() if w == 'i': w = 'I' if w == "i'm": ...
def is_segment(other): """Return true if this is a Segment. The purpose of this helper function is for testing if something is a segment without requiring the import of the class. """ return getattr(other, "is_segment", False)
def _color_to_tuple(value): """Converts a color from a 24-bit integer to a tuple. :param value: RGB LED desired value - can be a RGB tuple or a 24-bit integer. """ if isinstance(value, tuple): return value if isinstance(value, int): if value >> 24: raise ValueError("Only ...
def is_sha256(str): """Return True if str is a 64-byte hex string """ try: int(str, 16) return True except: return False
def parse_resource_type(resource_type): """Splits a resource type into it's components. :exc:`ValueError` is raised if the resource type is invalid. >>> parse_resource_type('AWS::ECS::Instance') ['AWS', 'ECS', 'Instance'] >>> parse_resource_type('AWS::ECS') Traceback (most recent call last): ...
def get_url_param(params, var, default=None, type_=None, *args, **kwargs): """ Wrapper for getting a variable from a url parameter. Necessary because url params can be registered as lists, so this will get a single value if it can be found """ def perform_get_url_param(params, var, *args, **kwargs...
def double_factorial(input_num): """ Calculate the double factorial of specified number >>>double_factorial(5) 15 >>>double_factorial(8) 384 >>>double_factorial(3) 3 >>>double_factorial(0) 1 >>>-1 Traceback (most recent call last): File "<string>", line 11, in <mo...
def ddm_ddd(a, sep=" "): """ convert degree, decimal minute string to decimal degrees : a - degree, decimal minute string : sep - usually a space, but check : Useage - ddm_ddd(!SourceField!, sep=" ") : python parser, sourcefield is the input string field, destination : field is type ...
def merge_sequences(list1, list2, **kwargs): """ Return a new list of model objects merging the lists ``list1`` and ``list2`` keeping the original ``list1`` order and discarding duplicates. """ list1 = list1 or [] list2 = list2 or [] merged = [] existing = set() for item in list1...
def get_degree_of(links: list, node_i: int) -> int: """ computes the degree of each node and maps the node id to its degree :param links: a list of tuples with [(src, dst), ...] :param node_i: index i of node :return: degree of node i """ deg_i = sum(1 if node_i == i else 0 for i, _ in link...
def edges_between_AB(dictP, A, B): """ Returns the list of edges (i,j) for which exactly one of i and j are in A and the other in B. """ edges = [edge for edge in dictP if (edge[0] in A and edge[1] in B) or (edge[0] in B and edge[1] in A)] return edges
def prepare_data(result): """ Prepares data to return """ header = [col['name'].strip("ga:") for col in result['columnHeaders']] data = [] for row in result.get('rows'): data.append(dict(zip(header, row))) return {'data': data}
def update_query(project,dataset,tablename,objects,condition): """ Function to process the query for update process """ if isinstance(tablename, str): pass else: raise ValueError("Tablename should be a String") if condition == None or isinstance(condition, str): pass ...
def check_output(command_args): """ Wrapper for subprocess.checkoutput with logging of the error of the failed command """ import subprocess import logging from subprocess import CalledProcessError try: output = subprocess.check_output(command_args, stderr=subprocess.STDOUT)[:-1] # [:-1] re...
def spreadingRate(Q, beta): """ Calculates the spreading rate on the planet. Inputs: Q - pore space heat flow relative to modern Earth [dimensionless] beta - scaling parameter [dimensionless] Returns: r_sr - the spreading rate relative to the modern Earth [dimensionless] ...
def get_vector(a, b): """Given two points (3D), Returns the common vector""" vector = ((b[0] - a[0]), (b[1] - a[1]), (b[2] - a[2])) return vector
def clamp(lower: int, value: int, upper: int) -> int: """ Clamp a value between (inclusive) lower and upper """ return min(max(value, lower), upper)
def counting_sort(elements): """ Use the simple counting sort algorithm to sort the :param elements. :param elements: a integer sequence in which the function __get_item__ and __len__ were implemented() :return: the sorted elements in increasing order """ length = len(elements) if not length...
def get_chrom_name_from_bin_fn(bin_fn): """ Assuming that the bin_fn is like this: genome_chr2.211_binary.txt.gz Chrom name that is returned in this function will be like thisL chr2.211 """ return bin_fn.split("_")[1]
def count_routes_graph(graph, source_node, dest_node): """ classic tree-like graph traversal """ if dest_node == source_node or dest_node - source_node == 1: return 1 else: routes = 0 for child in graph[source_node]: routes += count_routes_graph(graph, child, dest...
def mergelistmax(lst1,lst2): """returns the maximum value at each index comparing 2 lists""" try: return [max([lst1[i],lst2[i]]) for i in range(len(lst1))] except: print('incompatible lists')
def foldConditions(condition) : """Fold conditions to convert the condition of a priority element to the formatted string of the priority file matching the condition """ # If the condition is simple if 'comparison' in condition : return condition['type'] + '.' + condition['name'] + ' ' + con...
def filename_split (p): """ Split filename >>> print("('%s', '%s', '%s', '%s')"%filename_split("/toto/titi/tutu.tata")) ('', '/toto/titi', 'tutu', '.tata') """ from os.path import split as splitPath, splitdrive, splitext splt = splitPath (p) disk,dir_ = splitdrive(splt[0]) try: if disk[1] != ":": raise ...
def url_path_join(*pieces): """Join components of url into a relative url Use to prevent double slash when joining subpath. This will leave the initial and final / in place """ initial = pieces[0].startswith("/") final = pieces[-1].endswith("/") stripped = [s.strip("/") for s in piece...
def get_dim(min_mz, max_mz, bin_size): """ Compute the number of bins over the given mass range for the given bin size. Args: min_mz: The minimum mass in the mass range (inclusive). max_mz: The maximum mass in the mass range (inclusive). bin_size: The bin size (in Da). Retu...
def checksum (message): """grab the hashed checksum of ASCII text""" s = 0 # loop taking 2 characters at a time for i in range(0, len(message), 2): w = ord(message[i]) + (ord(message[i+1]) << 8 ) s = s + w # complement and mask to 4 byte short s = ~s & 0xffff retu...
def _convert_csv_numbers_to_list(value): """Converts a string containing CSV numbers to a list.""" if not value: return [] return [float(x) for x in value.split(',')]
def matmultiplication(A,B): """ Returns the product of the matrix A * B :param A: The first matrix - ORDER MATTERS! :param B: The second matrix :return: The product of the two matrices """ if(len(A[0])!=len(B)): return "Multiplication not possible" result = [[sum(a * ...
def policy_v3_1(probability=0.7, magnitude=5): """Randomly select two transformations from {color} transformations, and then randomly select two transformations from {shape} transformations.""" policy = { # color augment 0: [[('Mixup', probability, magnitude)], [('Gaussian_noise', probab...
def get_search_query(list2, t): """Create a query, that we will parse. __Attributes__ list2: List, which contain start and end date. t: if 1 change search query (look below) __Returns__ search_query: List, which contain search query. """ if t > 1: search_query = ("...
def calculate_signature_score(signature): """ Calculates the signature score for each signature as the sum of all the weights in it but ignoring the weights marked with "*". Input: A signature that contains tags of whether or not the weight should be included in calculating the signature. Output...
def _remove_duplicates(values): """ Removes duplicate values, useful in getting a concise list. (Requires re) """ output = [] seen = set() for value in values: # If value has not been encountered yet, # add it to both list and set. if value not in seen: output.ap...
def generate_user_input_table(ui): """Generate user input table in a format that allows for copy-paste from LaTeX pdf to python editor""" calculation_input = [] for key, value in ui.items(): if key != 'parameter_id': calculation_input.append(key) calculation_input = sorted(calcul...
def convert_to_uni(text): """convert bytes to text""" if isinstance(text, str): return text if isinstance(text, bytes): return text.decode('utf-8', 'ignore') raise Exception("The type %s does not convert!" % type(text))
def has_file_allowed_extension(filename, extensions): """ check if a file has an allowed extensio n. Args: filename (string): path to a file extensions (tuple of strings): extensions allowed (lowercase) Returns: bool: True if the file ends with one of the given extensions """ ...
def count_target_words(tokens): """ Utility function to count the total number of tokens in a dataset. Parameters ---------- tokens : list of lists List of sentences, where each sentence is a list of tokens. Usually returned from ``data.loader.load_data`` Returns ------- ...
def fibonacci_numbers_until_n_digits(n): """Return the list fibonacci numbers until the latest has at lest n digits :type n: The minimum amount of digits for the last fibonacci number in the list """ result = [] a, b = 0, 1 while len(str(b)) < n: result.append(b) a, b = b, a + b...