content
stringlengths
42
6.51k
def getCycleNodeStamp(cycle, node): """ Returns a CCCNNN stamp for this cycle and node. Useful for comparing the current cycle/node with requested snapshots in the settings See Also -------- isRequestedDetailPoint : compares a cycle,node to the dumpSnapshots list. extractCycleNodeFromStamp...
def factorial(number): """Function that finds the factorial number of the number introduced Args: number (int): Number that you want to find the factorial of Raises: ValueError: if the value of number is less than 0 it raises this error with a warning Returns: int: Factorial o...
def construct_names(paths): """ """ names = [] for p in paths: names.append("-".join([e["name"] for e in p])) return names
def score(G, results): """The score is the number of edges we got correctly, divided by the number of edges we guessed.""" points = 0 for src, (_timestamp, detected_node) in results.items(): if detected_node in G[src]: points += 1 return points / float(len(results))
def is_expression(tpn_field): """Return True IFF .tpn value `tpn_field` defines a header expression. Initially `tpn_field` is either a value from the TpnInfo "values" field or it is the value of the "presence" field. In both cases, an expression is signified by bracketing the value in parens. """...
def _convert_paths_to_flask(transmute_paths): """flask has it's own route syntax, so we convert it.""" paths = [] for p in transmute_paths: paths.append(p.replace("{", "<").replace("}", ">")) return paths
def from_submission_status_annotations(annotations): """ Convert back from submission status annotation format to a normal dictionary. Example:: submission_status.annotations = from_submission_status_annotations(submission_status.annotations) """ dictionary = {} for key, value in annot...
def is_decimal_amount(string: str) -> bool: """Checks if string is a decimal amount (e.g. 1/2, 1/4, etc..) Args: string: string to be checked. Returns: True if string is a decimal amount, False otherwise. """ if "/" not in string or len(string.split("/")) != 2: return False ...
def fibonacci(length=10): """Get fibonacci sequence given it length. Parameters ---------- length : int The length of the desired sequence. Returns ------- sequence : list of int The desired Fibonacci sequence """ if length < 1: raise ValueError("Sequence le...
def remove_directories(list_of_paths): """ Removes non-leafs from a list of directory paths """ found_dirs = set('/') for path in list_of_paths: dirs = path.strip().split('/') for i in range(2,len(dirs)): found_dirs.add( '/'.join(dirs[:i]) ) paths = [ path for path i...
def shape_to_string(shape): """ On Windows, shape tuples use long ints which results in formatted shapes such as (2L, 3L). This function ensures that the shape is always formatted without the Ls. """ return "({0})".format(", ".join(str(int(item)) for item in shape))
def wilight_to_opp_position(value): """Convert wilight position 1..255 to opp.format 0..100.""" return min(100, round((value * 100) / 255))
def format_value(value, fmt): """ Convert numerical value to string with a specific format Parameters ---------- value : int or float Numerical variable to convert. fmt : str String format used to apply the conversion Returns ------- string_value : str Strin...
def identity(*args): """ Always returns the same value that was used as its argument. Example: >>> identity(1) 1 >>> identity(1, 2) (1, 2) """ if len(args) == 1: return args[0] return args
def _object_get(entry, attr): """ simple getter over object entry Example:: class A(object): def __init__(self, attribute): self.attribute = attribute def foo(): return self.attribute a = A(10) #: calls foo as it's callable ...
def without_duplicates(duplicates, similar_songs): """Creates a list that only includes those strings which occur once in the similar_songs list. Args: duplicates: Usually the list that is the return list of get_duplicates. similar_songs: Usually the list that is the return list of ...
def point2str(point, precision=1): """ Format a point for printing, based on specified precision with trailing zeros. Uniform printing for vector-like data (tuple, numpy array, list). Args: point (vector-like): nD point with floating point coordinates. precision (int): Numbe...
def normalize_optical(optical_flow, min, max): """ Min-max normalizes optical flow given min and max values """ return ((optical_flow - min)/(max - min))
def parse_pair(src): """ parse '(field, "value")' definition to tuple (field, value) """ token = next(src) if token != '(': return None, None token = next(src) field = token token = next(src) if token == ')': return field, None elif token != ',': return No...
def redshiftFromScale(scale): """ Converts a scale factor to redshift. :param scale: scale factor :type scale: float or ndarray :return: redshift :rtype: float or ndarray """ return 1. / scale - 1.
def has_subpattern(string: str) -> bool: """ String subpattern recognition I In this kata you need to build a function to return either true/True or false/False if a string can be seen as the repetition of a simpler/shorter subpattern or not. Strings will never be empty and can be composed of ...
def next_id(arr: list) -> int: """ This function returns the smallest unused ID. """ reversed_arr = list(sorted(arr.copy())) if len(reversed_arr) == 0: return 0 for i in range(len(reversed_arr) - 1): if reversed_arr[0] != 0: return 0 if reversed_arr[i] - reversed_arr[...
def compare_locations_fc(fields, fcrow, id_vals, loc_fields): """Compares the values of each of a list of fields with the corresponding values in a dictionary. Compares values accross field types. Returns True if the values are different""" status = False for loc_field in loc_field...
def contains_any(seq, aset): """ Check whether sequence seq contains ANY of the items in aset. """ for c in seq: if c in aset: return True return False
def ascii(str): """Return a string with all non-ascii characters hex-encoded""" if type(str) != type(''): return map(ascii, str) rv = '' for c in str: if c in ('\t', '\n', '\r') or ' ' <= c < chr(0x7f): rv = rv + c else: rv = rv + '\\' + 'x%02.2x' % ord(c)...
def unique(lst): """ Returns a list made up of the unique values found in lst. i.e., it removes the redundant values in lst. """ lst = lst[:] unique_lst = [] # Cycle through the list and add each value to the unique list only once. for item in lst: if unique_lst.count(item) <= ...
def terminalise_image(output): """ joins lists :return:""" return "\n".join(["".join(row) for row in output])
def fl2int(x, deci=3): """ Convert floating point number to integer. """ return int(round(x * pow(10, deci)))
def get_language_file_path(language): """ :param language: string :return: string: path to where the language file lies """ return "{lang}/localization_{lang}.json".format(lang=language)
def edge_loops_from_edges(mesh, edges=None): """ Edge loops defined by edges Takes me.edges or a list of edges and returns the edge loops return a list of vertex indices. [ [1, 6, 7, 2], ...] closed loops have matching start and end values. """ line_polys = [] # Get edges not use...
def get_bbox(points2d_px: list, width: int, height: int, clip=True): """Get 2d bounding box in pixel for a normalized 2d point list. Args: points2d_px (list): List of normalized 2d points. width (int): Image width in pixels. height (int): Image heigh in pixels. clip (bool, optio...
def graph_labeling_to_list(t, keys): """Creates a list that contains the tree's labels (according to the pre-order traversal). Args: t: a dictionary that contains the nodes of a labeled tree keys: sorted keys of the given t Returns: list: contains the tree's labels """...
def guess_fused_orths(word, ud_forms): """The UD data 'fused tokens' don't necessarily expand to keys that match the form. We need orths that exact match the string. Here we make a best effort to divide up the word.""" if word == "".join(ud_forms): # Happy case: we get a perfect split, with each...
def sort_decls(sexp): """Sort all toplevel variable declarations in sexp. This is used to work around the fact that ir_reader::read_instructions reorders declarations. """ assert isinstance(sexp, list) decls = [] other_code = [] for s in sexp: if isinstance(s, list) and len(s) >...
def flatten_dict(dic, merge_symbol=':'): """ Flattens a tree like dictionary, merging root keys as the concatenation of all branch keys with the specified merge_symbol (str). """ flattened_dict = {} for key, val in dic.items(): if isinstance(val, dict): inner_dict = ...
def get_operand_string(mean, std_dev): """ Generate the Operand String to be used in workflow nodes to supply mean and std deviation to alff workflow nodes Parameters ---------- mean: string mean value in string format std_dev : string std deviation value in string f...
def gen_mock_key(func_name, key): # type: (str, str) -> str """ Concate function name and the key to store in the mock DB """ return '{0}-{1}'.format(func_name, key)
def to_list(obj): """ Checks if obj is a list and returns it. If not, returns an empty list. :param obj: :return: """ if isinstance(obj, list): return obj else: return []
def toList(given): """ This will take what is given and wrap it in a list if it is not already a list, otherwise it will simply return what it has been given. :return: list() """ if not isinstance(given, (tuple, list)): given = [given] return given
def s_round(self, places=2): """ Correctly round float to n decimal places. >>> s_round(4.055, 2) 4.06 >>> s_round(4.054, 2) 4.05 """ return round(self + 10**(-2*6), places)
def is_int(value): """Check if given value is integer Parameters ---------- value : variable Returns ------- bool """ if value is not None: try: int(value) return True except ValueError: return False else: return F...
def checkBucketName(bucketName): """ Checks to make sure bucket names input are valid according to S3 naming conventions :param bucketName: Name of bucket to check :return: Boolean - whether or not the name is valid """ if (len(bucketName) < 3) or (len(bucketName) > 63): # Bucket names can be 3-63...
def sum_geometric_progression( ist_pertama: int, rasio_umum: int, ist_bilangan: int ) -> float: """ mengembalikan jumlah n suku dalam deret geometric >>> sum_geometric_progression(1, 2, 10) 1023.0 >>> sum_geometric_progression(1, 10, 5) 11111.0 >>> sum_geometric_progression(0, 2, 10)...
def flatten(x): """Flattens nested list""" if isinstance(x, list): return [a for i in x for a in flatten(i)] else: return [x]
def collect_nodes(kind, collection, gen): """ Collects nodes with a given kind from a generator, and puts them in the provided list. Returns the first node from the generator that does not match the provided kind. """ tail = None for node in gen: if node.kind is not kind: ...
def get_field_val(data, gid, content_id, field_id, default = 'Undefined'): """ try to get a field value from the hash generated in field_hash """ if gid in data: if content_id in data[gid]: if field_id in data[gid][content_id]: return data[gid][content_id][field_id] return default
def get_distinct_by(key, source): """Finds distinct items for the key and returns the result in a list. :param key: Function computing a key value for each item :param source: Iterable collection of items :return: The list of distinct by the key value items """ seen_keys = set() return [it...
def hex_to_rgb(hex_color): """ Converts a 6 digit hex number to RGB. @param: hex_color - A 6 digit string with values in the range [a-fA-F0-9]. @return: a tuple containing 3 integers. """ if not isinstance(hex_color, str): raise TypeError("'hex_color' must be of type 'str'.") if le...
def subsequence(X): """Returns the Longest Increasing Subsequence in the Given List/Array""" N = len(X) P = [0] * N M = [0] * (N+1) L = 0 for i in range(N): lo = 1 hi = L while lo <= hi: mid = (lo+hi)//2 if (X[M[mid]] < X[i]): lo = mid+1 ...
def str_cannonize(str): """ Input string with spaces and/or mixed upper and lower characters will be converted to a cannonical form, i.e. all to lowercase and spaces replaced by a '_'. Return new cannonical string """ if not str: return None tmp = str.split() # intention is to...
def generator_next(g): """ Return next elements in generator. :param g|generator: :rtype generator: return None if stopping iteration. """ try: e = g.__next__() return e except StopIteration: return None
def checkcols(sudoku): """ Checks if each column contains each value only once """ size = len(sudoku) for col_num in range(size): numbercontained = [False for x in range(size)] for row_num in range(size): value = sudoku[row_num][col_num] # if placeholder, igno...
def is_hpp(file): """ Returns True if file looks like a C++ file (header of .cpp) """ return file.split(".")[-1] in ["hpp", "h"]
def fix_date_v2(date_str: str) -> str: """Fix v2 dates so they're ISO""" if date_str.endswith("+1000"): date_str = date_str[:-2] return date_str
def get_gender_from_label(gender): """ Method to get the passed gender in XNAT format :param gender: gender selected :return: value accepted by xnat: 'female' or 'male' """ if gender.lower() in ['female', 'f']: return 'female' elif gender.lower() in ['male', 'm']: return 'ma...
def convert_dict_into_tuple(py_dict): """ Convert dictionary object to tuple. """ if not isinstance(py_dict, dict): return py_tuple = tuple([x for x in py_dict.values()]) return py_tuple
def _get(d, keys): """ helper function to get a value from a nested dict with dotted string notation. Sould not be used from the api. :param d: :param keys: :return: """ if not isinstance(d, dict): return if "." in keys: key, rest = keys.split(".", 1) return ...
def lookup(value, key): """ Return a dictionary lookup of key in value """ try: return value[key] except KeyError: return ""
def encode(codes, sentence): """ join the codes together into one string """ try: if not codes: msg = "\nNo data available for encoding.\nDid you send a valid string?" raise ValueError(msg) except ValueError as ve: print(ve) return False output =...
def clean_up_instances(instances): """ Cleans the instance raw data up to send back to client """ cleansed_instances = {} for instance in instances: cleansed_instances.update({ instance["tags"]["DesktopId"]: { "instanceid": instance["instanceid"], "dns": instance["dns"], "lau...
def sales_force_efficiency(number_of_orders_from_visits, number_of_visits): """Returns the percentage of visits by the sales force that resulted in orders from customers. Args: number_of_orders_from_visits (int): Number of orders generated by sales force visits during the period. number_of_visi...
def getRealFactory(factory): """Get the real factory. Sometimes the original factory is masked by functions. If the function keeps track of the original factory, use it. """ # Remove all wrappers until none are found anymore. while hasattr(factory, 'factory'): factory = factory.factory ...
def split_arbitrary_thickness_section(key, value): """ >>> key = 'T(11)' >>> value = '[1.2,PT=(123,204)]' >>> index, out = split_arbitrary_thickness_section(key, value) >>> index 11 >>> out [1.2, [123, 204]] """ assert key.endswith(')'), 'key=%r' % key # T(3), CORE(3) key...
def normalize_dihedral(d): """ Normalize any number to the range (-180, 180], including 180 """ return d + (180-d)//360*360
def families_pulsed_magnets(): """Return pulsed magnet families.""" return ['InjSept']
def xy_to_bit(x: int, y: int) -> int: """Transform x/y coordinates into a bitboard bit number.""" return y * 8 + x
def get_path(node): """ Return a fake path if necessary. As an example Aliases use this as their target name in Ninja. """ if hasattr(node, "get_path"): return node.get_path() return str(node)
def import_stmt(imp_from, imp, imp_as): """Generate an import statement.""" return ( ("from " + imp_from + " " if imp_from is not None else "") + "import " + imp + (" as " + imp_as if imp_as is not None else "") )
def GetFeedItemIdsForCampaign(campaign_feed): """Gets the Feed Item Ids used by a campaign through a given Campaign Feed. Args: campaign_feed: the Campaign Feed we are retrieving Feed Item Ids from. Returns: A list of Feed Item IDs. """ feed_item_ids = set() try: lhs_operand = campaign_feed['...
def polyline(points,closed=False): """Path instructions for a polyline Args: points (list of 2-tuples): The vertices of the polyline closed (bool, optional): Close the polyline to a polygon. Defaults to False. Returns: string: Ipe path instructions """ instructions = [ str(...
def dict2list(dct, keylist): """ task 0.6.3 procedure taking a dictionary and a keylist output should be a list of the dict keylist values e.g. input dct={'a':'A', 'b':'B', 'c':'C'} keylist=['b', 'c', 'a'] output=['B', 'C', 'A'] """ return [dct[key] for key in keylist]
def vararg1(x, y): """ vararg1 """ z = x + y return z
def RPL_ENDOFEXCEPTLIST(sender, receipient, message): """ Reply Code 349 """ return "<" + sender + ">: " + message
def parse_element_string(elements_str, stoich=False): """ Parse element query string with macros. Has to parse braces too, and throw an error if brackets are unmatched. e.g. Parameters: '[VII][Fe,Ru,Os][I]' Returns: ['[VII]', '[Fe,Ru,Os]', '[I]'] e.g.2 Parameters: '[VII]2[Fe,Ru...
def matchLines(theLineS): """Given a list of lines return a list of objects: If the line is all digits then divide it by 2. If the line is all non-digits then make it lower case. Other lines are represented by None.""" result = [] for l in theLineS: # Your code goes here pass ...
def cons_function(kwargs): """Consumer function gets integers from the queue and stores them in a list passed throughout the calls. At the last call, the list plus the kwargs['add'] is returned.""" kwargs['_name'] is_last_call = kwargs['_last_call'] add = kwargs['add'] value = kwargs['_...
def extract(line): """Return a tuple (uin, nick) from 'O11111111 nickname'""" line = line.replace("\n", "") uin = line[1:line.find("\t")] # fix uin uin2 = "" for c in uin: if c.isdigit(): uin2 += c uin = uin2 nick = line[1+line.find("\t"):] nick = nick.replace("/", "_") nick = nick.replace(":", "_...
def _split_storage_url(storage_object_url): """ Returns a list containing the bucket id and the object id. """ return storage_object_url.split("/")[2:]
def summary(txt): """ Returns the first line of a string. """ lines = txt.split('\n') return lines[0]
def is_response_blank(response): """ Function: [is_response_blank] :param response: The [response] to check whether the response is blank. :return: [boolean] `true` if [response] is blank, `false` otherwise. """ return bool(response and response.strip())
def classify_bmi(bmi): """Function to classify BMI into categories""" if bmi <= 18.5: return "underweight" elif bmi <= 25: # Using elif after return is flagged by pylint. Check it out. return "normal" elif bmi <= 30: return "overweight" else: return "obese"
def peters_f(e): """ Compute f(e) from Peters and Mathews (1963) Eq.17 ### Parameters: e (`float/array`): Eccentricity ### Returns: f (`float/array`): Enhancement factor of gravitational radiation """ numerator = 1 + (73/24)*e**2 + (37/96)*e**4 denominato...
def calculate_age(seconds): """ Convert seconds to time period string e.g. 6000 -> 1 h 40 m """ m, s = divmod(seconds, 60) h, m = divmod(m, 60) return '%d h %02d m' % (h, m)
def check_imports(to_check): """ Function to check if a specific package is currently imported. Does not check if module components, or shortened module names eg: import pandas as pd from matplotlib.pyplot import plt Parameters ---------- module str | lst S...
def get_run_status(results_list): """ Returns overall run status either Pass or Fail. """ for tc in results_list: if tc["status"] == "Failed": return "FAILED" if tc["status"] == "Not Executed": return "SETUP-FAILURE" return "PASSED"
def readable_number(num, single_thing='', plura_thing='',): """ Convert numbers into human readable format e.g 1000000 to 1M :param num: the number :param single_thing: 1 apple :param plural_thing: 2 apples """ if num is None: return '' # Add a space in front of the thing if...
def mod_exp(base: int, exp: int, modulus: int) -> int: """Calling Python built-in method to implement fast modular exponentiation. Args: base: exp: modulus: Returns: """ return pow(base, exp, modulus)
def funded_by_grant(paper, grant_id): """Return True if the paper was funded by the specified grant. """ try: if grant_id in paper['funding']: return True except Exception as e: pass return False
def get_attr(obj, attrs): """ Recursively get a value from a nested set of objects Parameters ---------- obj : any The root object attrs : str or list Either a list of attribute names or a string of attribute names joined by dots. Returns ------- any or None ...
def get_median_change_tweets(trend_days): """ Function for calculating trend differences between days, based on tweets. @param trend_days: the data to calculate trend graph from. @return: list fo points for a graph. """ keys = sorted(trend_days.keys()) #print keys results = [] for i ...
def errors_to_msg(errors): """Convert a dictionary of errors in a well formatted message. """ err = '\n'.join(('- %s : %s' % (k, v) for k, v in errors.items())) return 'The following errors occured :\n' + err
def setbit(byte, offset, value): """ Set a bit in a byte to 1 if value is truthy, 0 if not. """ if value: return byte | (1 << offset) else: return byte & ~(1 << offset)
def parse_patch_output(output_line): """parses the output produced by patch and returns the filename""" pf = output_line[14:] if pf[0] == '`': pf = pf[1:-1] # Remove the quotes return pf
def get_delta(k, c=1.0874, s=1.0187): """ Estimate the approximate expected transit depth as a function of radius ratio. There might be a typo here. In the paper it uses c + s*k but in the public code, it is c - s*k: https://github.com/christopherburke/KeplerPORTs :param k: the dimensionles...
def read_xlsx(filename): """Read information from a XLSX file and return a list. Parameters ---------- filename : str Full path and name for the xlsx file. Returns ------- list """ try: import pandas as pd lines = pd.read_excel(filename) return...
def _new_units(first_unit, last_unit): """ Units for newly added device """ units= {} units['new_first_unit'] = first_unit units['new_last_unit'] = last_unit if units['new_first_unit'] > units['new_last_unit']: units['new_last_unit'] = first_unit units['new_first_unit'] = las...
def safeint(num): """converts to int, regardless """ try:v=int(num) except:v=0 return v
def tranform2bool(input_): """ transform the input parameter to boolean """ assert input_[0].lower() in ['y', 'n'], 'The input of Yes/No question should start with "y" or "n", please contact with the developer' if input_[0].lower() == 'y': return True elif input_[0].lower() == 'n': ...
def get_default_region_type(current_type): """If all shapes in current_type are of identical shape type, return this shape type, else "polygon" as lowest common denominator type. Parameters ---------- current_type : list of str list of current shape types Returns --------...
def bubblesort(a): """Bubble Sort algorithm for sorting a sequence container of values. """ l = len(a) swaps = 1 while swaps > 0: swaps = 0 for i,j in zip(range(0,l-1),range(1,l)): if a[i] > a[j]: t = a[i] a[i] = a[j] a[j] =...