content
stringlengths
42
6.51k
def check_classes_to_swap(source, target): """Check which item classes are better to use on a swap.""" classes = {} for class_, percentage in source['percentages'].items(): if class_ not in classes: classes[class_] = 0.0 classes[class_] = percentage - target['percentages'][class_...
def _nt_sum(cobj, prop, theta): """ Create sum expressions in n-t forms (sum(n[i]*theta**t[i])) Args: cobj: Component object that will contain the parameters prop: name of property parameters are associated with theta: expression or variable to use for theta in expression Retur...
def fix_links(links): """ The fix_links function removes multiple references to the same article allowing only one representant """ if links: result = [links[0]] for i in range(1,len(links)): test = [True for elem in result if elem in links[i]] if not test: ...
def digit(number: int, n: int) -> int: """Indexes integer without type conversion (e.g digit(253, 1) returns 5)\n Index of number is n """ return number // 10 ** n % 10
def ifnone(value, ifnone='~'): """ Pass a string other than "None" back if the value is None. Used in datestamp handling. """ if value is None: return ifnone return value
def split_extend(seq, sep, length): """ Splits on a character, but also output a list of defined length. Used the first field of the split result to pad out the requested length. eg >>> split_extend("A;B;C", ';', 3) ['A', 'B', 'C'] >>> split_extend("A", ';', 3) ['A', 'A', 'A'] >>...
def group(group_name): """Marks this field as belonging to a group""" tag = "group:%s" % group_name return tag
def _convert_valid_actions(valid_actions): """Convert provided valid_actions for gRPC. Args: valid_actions: Either None, a list of bools or a numpy boolean array. Returns: None if valid_actions is None. Otherwise, a list of bools. """ if valid_actions is None: return None return list(map(bool, ...
def links_to_graph(links): """ Changes links to undirected graph """ graph = {} # change in dictionary graph for u, v in links: # make undirected graph if u not in graph: graph[u] = [] if v not in graph: graph[v] = [] graph[u].append((1, v)) ...
def lookup_discrete(x, xs, ys): """ Intermediate values take on the value associated with the next lower x-coordinate (also called a step-wise function). The last two points of a discrete graphical function must have the same y value. Out-of-range values are the same as the closest endpoint (i.e, no extrapo...
def select_all_table(table: str) -> str: """ select_all_table is used for returning all the table data. :param table: return stock table data :return: """ sql_query = f"SELECT * FROM {table}" return sql_query
def pig_actions_d(state): """The legal actions from a state. Usually, ["roll", "hold"]. Exceptions: If double is "double", can only "accept" or "decline". Can't "hold" if pending is 0. If double is 1, can "double" (in addition to other moves). (If double > 1, cannot "double"). """ # state is...
def dar_troco(valor_a_pagar, valor_em_dinheiro): """ Calcule o troco numa lista com notas de 1,2,5,10,20,50 com sua quantidade de notas sem considerar centavos ex: 1 e 10 retorna troco_notas_quantidade = [5,2] quantidade_notas = [1,2]""" notas = (50, 20, 10, 5, 2, 1) valor_troco =valor_em_dinhei...
def is_structured(dt): """Check if the dtype is structured.""" if not hasattr(dt, "fields"): return False return dt.fields is not None
def _split_categories(value): """Splits the categories on comma. Returns a list of categories or if there is one category named 'none' returns an empty list.""" categories = [c.strip() for c in value.split(',')] if len(categories) == 1 and categories[0].lower() == 'none': return [] return ca...
def address_repr(buf, reverse: bool = True, delimit: str = "") -> str: """Convert a buffer into a hexlified string.""" order = range(len(buf) - 1, -1, -1) if reverse else range(len(buf)) return delimit.join(["%02X" % buf[byte] for byte in order])
def format_literal(raw): """Format a literal into a safe format. This is used to format the flont:Literal IRI. """ return "_" + raw.replace(" ", "_")
def isValidOs(sOs): """ Validates the OS name. """ if sOs in ('darwin', 'dos', 'dragonfly', 'freebsd', 'haiku', 'l4', 'linux', 'netbsd', 'nt', 'openbsd', \ 'os2', 'solaris', 'win', 'os-agnostic'): return True; return False;
def return_hashtags(hashtag_list: list) -> str: """Returns a string of formatted hashtags from a list.""" hashtag_string = [f"#{tag} " for tag in hashtag_list] print("\n".join(hashtag_string)) return "\n".join(hashtag_string)
def __get_edge_dict(uid, source, target, color, edge_type): """ Create dictionary for edges :param uid: :param source: :param target: :param color: :param edge_type: :return: """ return { 'id': uid, 'source': source, 'target': target, 'color': col...
def split_host_and_port(host): """ Splits a string into its host and port components :param host: a string matching the following pattern: <host name | ip address>[:port] :return: a Dictionary containing 'host' and 'port' entries for the input value """ if host is None: host_and_port = None else: ...
def get_chart_url(country_code, interval, date1, date2): """Gets a url with the specified parameters""" chart_url = f'https://spotifycharts.com/regional/{country_code}/{interval}/{date1}--{date2}' return chart_url
def make_adjacency_matrix(g): """ Make an adjacency matrix from a dict of node: [neighbors] pairs """ keys=sorted(g.keys()) size=len(keys) M = [ [0]*size for i in range(size) ] """ for a, row in g.items() iterates over the key:value entries in dictionary, and for b in row iterates over the ...
def get_provenance_record(attributes, obsname, ancestor_files): """Create a provenance record describing the diagnostic data and plot.""" if obsname != '': caption = ( "{long_name} bias for average between {start_year} and {end_year}". format(**attributes) + " against " + obsname...
def VersionString(versionTuple): """ (x,y,z .. n) -> 'x.y.z...n' """ return '.'.join(str(x) for x in versionTuple)
def solution_b(puzzle, stop=2020): """ This next solution is even faster, about 25%, thanks to Gravitar64. https://github.com/Gravitar64/Advent-of-Code-2020 Counting the turns from len(puzzle) instead of len(puzzle) + 1 makes everything so easy and nice! """ spoken = {last: turn for turn, la...
def unique_char(str): """Get index of first unique char.""" frequency = {} unique_chars = [] for index, char in enumerate(str): if char in frequency: frequency[char] += 1 unique_chars = [item for item in unique_chars if item[1] != char] else: frequency...
def icon_dpi(px, icon): """ Inkscape default: 90dpi == 1-to-1 for 100px. """ x1,y1,x2,y2 = [float(a) for a in icon['view_box'].split(" ")] width = x2-x1 height = y2-y1 # Fallback for invalid viewbox if width < 0: width = 100 if height < 0: height = 100 return 90. ...
def find_profile_lines(data, profile_name): """ Takes data as a list of lines and finds the range (startline, endline) where we found the profile, where startline is the line right after the profile name """ start, end = None, None for i, line in enumerate(data): if start is not None...
def choice(*args): """ Creates a choice argument type. The user will be able to choose one of given possibilities. Example: choice("quickly", "slowly") :param args: a list of string the user will be able to choose :return: a dict representing this argument type with the appropriate format t...
def append_cluster(cluster_a, cluster_b): """ Appends cluster_b to cluster_a :param cluster_a: array of shape [n_points, n_dimensions] :param cluster_b: array of shape [n_points, n_dimensions] """ for point in cluster_b: cluster_a.append(point) return cluster_a
def checkTypeRecursively(inObject): """ This method check the type of the inner object in the inObject. If inObject is an interable, this method returns the type of the first element @ In, inObject, object, a pyhon object @ Out, returnType, str, the type of the inner object """ returnType = type(...
def _is_multiline_string(value: str): """Determine if a string is multiline. .. note:: Inspired by http://stackoverflow.com/a/15423007/115478. :param value: The value to check :returns: A boolean indicating if the string is multiline """ for character in "\u000a\u000d\u001c\u001d\u001e...
def inputs(form_args): """ Creates list of input elements """ element = [] for name, value in form_args.items(): element.append( '<input type="hidden" name="{}" value="{}"/>'.format(name, value)) return "\n".join(element)
def arelle_parse_value(d): """Decodes an arelle string as a python type (float, int or str)""" if not isinstance(d, str): # already decoded. return d try: return int(d.replace(',', '')) except ValueError: pass try: return float(d.replace(",", "")) except ValueErr...
def map_codes_to_values(codes, values): """ Map the huffman code to the right value """ out = {} for i in range(len(codes)): out[codes[i]] = values[i] return out
def rfcspace(string): """ If the string is an RFC designation, and doesn't have a space between 'RFC' and the rfc-number, a space is added """ string = str(string) if string[:3].lower() == "rfc" and string[3] != " ": return string[:3].upper() + " " + string[3:] else: retu...
def get_severity(severity): """ Returns Severity as per DefectDojo standards. :param severity: :return: """ if severity == "high": return "High" elif severity == "medium": return "Medium" elif severity == "low": return "Low" elif severity == "informational...
def bytes_to_31_bit_int(as_bytes): """ Convert the 31 least-signficant bits to an integer, truncating any more significant bits. """ as_bytes = bytearray(as_bytes) if len(as_bytes) < 4: pad_len = 4 - len(as_bytes) as_bytes = bytearray(pad_len * [0]) + as_bytes as_int = (((as_...
def cut_include_start_end(some_text, start_text, end_text, maximum_lines_per_section=10000): """ from some_text (output from Network device session), returns a List of List(strings), sections of some_text containing the lines between StartText to EndText, INCLUDING StartText and EndText on the returning section...
def _get_leaf_list(tree): """ get all leaves of tree. """ if isinstance(tree, int): return list() leaves = list() for sub_tree in tree: if isinstance(sub_tree, int): leaves.append(sub_tree) else: leaves.extend( _get_leaf_list(sub_...
def prepare_update_request(rows_to_update, rows_to_append, users_to_verify_data): """Create the request's body used for batch update.""" value_ranges = [] for row_number, dumped_data in rows_to_update: # there is going to be updated just a single column within a specific row range value_ran...
def format_judge(submission): """ judge if the submission file's format is legal :param submission: submission file :return: False for illegal True for legal """ # submission: [sentenceID,antecedent_startid,antecedent_endid,consequent_startid,consequent_endid] if submission[1] ...
def human_bytes(num, suffix='B'): """ Convert a number to bytes to a human-readable form""" # taken from http://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']: if abs(num) < 1024.0: ...
def is_directory_automatically_created(folder: str): """ Verifies the name of the directory -> if it contains a month it returns True, otherwise False. """ months = [ "(01)Janvier", "(02)Fevrier", "(03)Mars", "(04)Avril", "(05)Mai", "(06)Juin", "(0...
def debug(txt): """ Print text to console.""" print(txt) return ''
def divide_ceiling(numerator, denominator): """ Determine the smallest number k such, that denominator * k >= numerator """ split_val = numerator // denominator rest = numerator % denominator if rest > 0: return split_val + 1 else: return split_val
def getMedian(alist): """get median of list""" tmp = list(alist) tmp.sort() alen = len(tmp) if (alen % 2) == 1: return tmp[alen // 2] else: return (tmp[alen // 2] + tmp[(alen // 2) - 1]) / 2
def dominates(a, b): """Return true if a Pareto dominates b (maximization)""" equals = True for i in range(len(a)): equals = equals and a[i] == b[i] if a[i] < b[i]: return False if equals: return False return True
def normalize_disp(dataset_name): """Function that specifies if disparity should be normalized""" return dataset_name in ["forward_facing"]
def vmul(vec1, vec2): """Return element wise multiplication""" return [v1*v2 for v1, v2 in zip(vec1, vec2)]
def list_to_string(ilist): """ Takes a list of instructions and combines them into a single string. This is a helper for compact_instructions().""" str = "" for s in ilist: if len(str) > 0: str += "\n" str += s return str
def cm_q(cm_i, l_t, mac): """ This calculates the damping in pitch coefficient Assumptions: None Source: J.H. Blakelock, "Automatic Control of Aircraft and Missiles" Wiley & Sons, Inc. New York, 1991, (pg 23) Inputs: cm_i [dimensionless] l_t ...
def filter_metrics_list(metrics_list, filters): """ Filter metrics list based on filters: * filters: space separated list of filtered strings, exclamation mark before word means negative filter """ if isinstance(filters, str): filters = filters.split() for _filter in fil...
def sum(sequence, start=0): """sum(sequence[, start]) -> value Returns the sum of a sequence of numbers (NOT strings) plus the value of parameter 'start' (which defaults to 0). When the sequence is empty, returns start.""" if isinstance(start, str): raise TypeError("sum() can't sum strings [use ''.joi...
def str_to_bool(value): """ convert the boolean inputs to actual bool objects""" if value.lower() in {'false', 'f', '0', 'no', 'n'}: return False elif value.lower() in {'true', 't', '1', 'yes', 'y'}: return True raise ValueError(f'{value} is not a valid boolean value')
def _is_passing_grade(course_grade): """ Check if the grade is a passing grade """ if course_grade: return course_grade.passed return False
def polynomial_power_combinations(degree): """ Combinations of powers for a 2D polynomial of a given degree. Produces the (i, j) pairs to evaluate the polynomial with ``x**i*y**j``. Parameters ---------- degree : int The degree of the 2D polynomial. Must be >= 1. Returns -----...
def rename_categories(old_to_new_map, data): """ Rename categories in task files Args: `old_to_new_map` should look like { "old category name 1": "new category name 1", "old category name 2": "new category name 2" } """ for i, category in enumera...
def is_keyword(v): """ Check if a value is of the format required to be a call to a header keyword Parameters ---------- v : str Value to be tested Returns ------- valid : bool True if 'v' has the correct format to be a header keyword, False otherwise. """ valid = True ...
def _iface_cell_value(arr, loc): """ Returns I face value for cell-centered data. """ i, j, k = loc # FIXME: built-in ghosts return 0.5 * (arr(i+1, j+1, k+1) + arr(i, j+1, k+1))
def split(n, m, rank=None): """ Return an iterator through the slices that partition a list of n elements in m almost same-size groups. If a rank is provided, only the slice for the rank is returned. Example ------- >>> split(1000, 2) (slice(0, 500, None), slice(500, 1000, None)) >>...
def build_profile(first, last, **user_info): """Build dict containing everything we know about a user""" profile = {} profile['first name'] = first profile['last name'] = last for key, value in user_info.items(): profile[key] = value return profile
def fiveplates_field_file(field): """ string representation of targets.txt file for field within fiveplates_field_files zip file. Parameters ---------- field : str identifier of field, e.g. 'GG_010' """ return f'{field}_targets.txt'
def calc_blinds_activation(radiation, g_gl, Rf_sh): """ This function calculates the blind operation according to ISO 13790. :param radiation: radiation in [W/m2] :param g_gl: window g value :param Rf_sh: shading factor """ # activate blinds when I =300 W/m2 if radiation > 300: # in w/...
def flatten_dict_values(d: dict) -> list: """Extract all values from a nested dictionary. Args: d: Nested dictionary from which to extract values from Returns: All values from the dictionary as a list """ if isinstance(d, dict): flattened = [] for k, v in d.items():...
def to_lower(input_text: str) -> str: """ Convert input text to lower case """ return input_text.lower()
def dectodms(decdegs): """Convert Declination in decimal degrees format to hours, minutes, seconds format. Keyword arguments: decdegs -- Dec. in degrees format Return value: dec -- list of 3 values, [degrees,minutes,seconds] """ sign = -1 if decdegs < 0 else 1 decdegs = abs(decde...
def re_primary_dirname(dirname): """ Tests if a dirname string, can be matched against just primary. Note that this is a subset of re_primary_filename(). """ if 'bin/' in dirname: return True if dirname.startswith('/etc/'): return True return False
def FormatDateString(dt, time=True): """Formats a DateTime object into a string for display. If dt is not a DateTime, then the empty string is returned. Parameters: * dt - An instance of DateTime.DateTime * time - If True, displays the time. If False, only the date is ...
def stations_by_river(stations): """For a list of MonitoringStation objects (stations), returns a dictionary that maps river names (key) to a list of MonitoringStation objects on a given river.""" # Dictionary containing river names and their corresponding stations rivers = {} for station in stati...
def print_path(path): """ path is a list of nodes """ result = [] for i in range(len(path)): result.append(str(path[i])) # if not last path if i != len(path) - 1: result.append('->') return ''.join(result)
def default_value(value, default): """ Returns a specified default value if the provided value is None. Args: value (object): The value. default (object): The default value to use if value is None. Returns: object: Either value, or the default. """ return value if value...
def _collect_lines(data): """Split lines from data into an array, trimming them """ matches = [] for line in data.splitlines(): match = line.strip() matches.append(match) return matches
def make_singular(word): """Relatively naive/imperfect function to make a word singular Parameters ---------- word : str The string to make singular (e.g. 'zebras'). Returns ------- str The string in singular form (e.e. 'zebra'). """ if not isinstance(word, str) or...
def c_to_f(temp): """Returns Celsius temperature as Fahrenheit""" return temp * (9/5) + 32
def formatPath(input_string): """ function to correct backslash issues in paths usage: strPath = ut.formatPath(strPath) """ lstReplace = [["\a","/a"], ["\b","/b"], ["\f","/f"], ["\n","/n"], ["\r","/r"], ...
def makegainstr(gainset): """Return a shorted string for the gainsetting""" if gainset.upper()=='FAINT': gnstr='FA' elif gainset.upper()=='BRIGHT': gnstr='BR' else: gnstr='' return gnstr
def nattrs(res): """ Return a color gradient based on the """ # Split list into if res: return dict(fillcolor='yellow') return dict(fillcolor='white')
def RPL_INFO(sender, receipient, message): """ Reply Code 371 """ return "<" + sender + ">: " + message
def sieveOfEratosthenes(n): """ Given a number n, returns a list of all primes up to n. """ values = [x for x in range(2, n+1)] primes = [] #Any encountered value not marked False is a prime for i,v in enumerate(values): if v is not False: primes.append(v) ...
def write_transient_conv_msg(nMax, totTime): """Return the convergence status message for writing to file.""" PrintMsg = f"\nSTATUS: SOLUTION OBTAINED AT\nTIME LEVEL=\ {totTime} s.\nTIME STEPS= {nMax}" print(PrintMsg) print() return PrintMsg
def pick(keys, dict): """Returns a partial copy of an object containing only the keys specified. If the key does not exist, the property is ignored""" picked_dict = {} for k in dict.keys(): if k in keys: picked_dict[k] = dict[k] return picked_dict
def generate_route53_records(properties): """Return list of AWS::Route53::RecordSet resources required""" records = [] if properties.get("VerificationToken"): records.append({ "Name": "_amazonses.{Domain}.".format(**properties), "Type": "TXT", "ResourceRecords": ...
def is_valid_medialive_channel_arn(mlive_channel_arn): """Determine if the ARN provided is a valid / complete MediaLive Channel ARN""" if mlive_channel_arn.startswith("arn:aws:medialive:") and "channel" in mlive_channel_arn: return True else: return False
def sort_dict(dictionary): """Sorts a dictionary with only Integer keys ascending by these keys. If the dictionary is not valid, i.e. does not exclusively contain Integer keys, an Error is raised. :param dictionary: The to be sorted dictionary :return: The sorted dictionary, ascending by Integer keys....
def _SubtractMemoryStats(end_memory_stats, start_memory_stats): """Computes the difference in memory usage stats. Each of the two stats arguments is a dict with the following format: {'Browser': {metric: value, ...}, 'Renderer': {metric: value, ...}, 'Gpu': {metric: value, ...}, 'Process...
def getDigits(num,baseNum,digitsList): """ Input: num, baseNum, and digitsList must all come from base(num,base) as is currently specified. Output: returns each digit in the output number of base(num,base). """ tmpList=[] for x in digitsList: if x*(baseNum)>num: tmpList....
def escape_tag(string: str): """ Escape `<` and `>` by replace it with `&lt;` and `&gt;` """ return string.replace("<", "&lt;").replace(">", "&gt;")
def is_report(post_site_id): """ Checks if a post is a report :param post_site_id: Report to check :return: Boolean stating if it is a report """ if post_site_id is None: return False return True
def remove_zeros(mylist): """ Creates a new list which is equal to the input list but without zeros. @param mylist: The array to delete zeros. @return: The list without zeros. """ myretlist = [] for elem in mylist: if elem!=0: myretlist.append(elem) return myretlist
def length_lt(value, arg): """Returns a boolean of whether the value's length is less than the argument. """ return len(value) < int(arg)
def rfl_to_mmh(z, a=256.0, b=1.42): """ wradlib.zr.z2r function """ return (z / a) ** (1.0 / b)
def parse_url(url): """Return the inclusion method and test_url part of a url. Example url: http://192.168.2.148:8001/apg/iframe/?url=https://192.168.2.148:44300/leaks/10643/noauth/&browser=chrome&version=90.0.4430.85&wait_time=500 Example result: ("iframe", "https://192.168.2.148:44300/leaks/10643/noauth/...
def sqrt(x): """ Calculate the square root of argument x. """ # Check that x is positive if x < 0: print("Error: negative value supplied") return -1 else: print("Here we go again...") # Initial guess for the square root. z = x / 2.0 # Continuously improve the guess. # Adapted from htt...
def detect_internals(pstart, pend, hit_start, hit_end): """[Local] Check for internal features (for 'genomic_location' column, independent of users key option) """ feat_in_peak = (pstart < hit_start and hit_end < pend) peak_in_feat = (hit_start < pstart and pend < hit_end) if feat_in_peak: ...
def is_condition_key_match(document_key, str): """ Given a documented condition key and one from a policy, determine if they match Examples: - s3:prefix and s3:prefix obviously match - s3:ExistingObjectTag/<key> and s3:ExistingObjectTag/backup match """ # Normalize both document_key = docum...
def is_not_nullprice(data): """ used by the filter to filter out the null entries :param data: input data :return: true if it's not null, false if null """ return data and data["MinimumPrice"] != None
def _extract_from_dict(dictionary, key, default=None): """Extract a given key from a dictionary. If the key doesn't exist and a default parameter has been set, return that. """ try: value = dictionary[key] del dictionary[key] except KeyError: value = default return ...
def replace_constants(fragments): """Replace ARG1 constants with referents introduced by constant clauses.""" constant_ref_map = { c[1]: c[2] for f in fragments for c in f if len(c) == 3 and c[1].startswith('"') and c[1].endswith('"') and c[1] not in ('"?"...