content
stringlengths
42
6.51k
def _func_star_single(func_item_args): """Equivalent to: func = func_item_args[0] item = func_item_args[1] args = func_item_args[2] kwargs = func_item_args[3] return func(item,args[0],args[1],..., **kwargs) """ return func_item_args[0](*[func_item_args[1]] + func_item_args...
def _rpm_split_filename(filename): """Taken from yum's rpmUtils.miscutils.py file Pass in a standard style rpm fullname Return a name, version, release, epoch, arch, e.g.:: foo-1.0-1.i386.rpm returns foo, 1.0, 1, i386 1:bar-9-123a.ia64.rpm returns bar, 9, 123a, 1, ia64 """ if filename[-4:] =...
def human_bytes(size): """Formats size, a number of bytes, in a human-readable way.""" suffices = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'HB'] for suffix in suffices: if size < 1024: return "%3.1f %s" % (size, suffix) size /= 1024.0 return "big"
def _calculate_downsampling_rate(initial_sampling_rate, maximum_f0): """ Determines downsampling rate to apply to the audio input passed for RAPT processing """ """ NOTE: Using Python 2.7 so division is integer division by default Different default behavior in in Python 3+. That said, keepi...
def split_volume_from_journal(citation_elements): """Split volume from journal title We need this because sometimes the volume is attached to the journal title instead of the volume. In those cases we move it here from the title to the volume """ for el in citation_elements: if el['type...
def get_unwise_image_url(ra, dec, npix, band, data_release, filetype="image"): """ Construct the UNWISE specific URL to download UNWISE cutouts. :param ra: float Right ascension of target :param dec: float Declination of target :param npix: float Cutout image size in pixels ...
def is_between(low, x, high): """Determine whether x is between X1 and X2""" return (low <= x) and (x <= high)
def unwrap_distributed(state_dict): """ Unwraps model from DistributedDataParallel. DDP wraps model in additional "module.", it needs to be removed for single GPU inference. :param state_dict: model's state dict """ new_state_dict = {} for key, value in state_dict.items(): new_ke...
def _sort_key_min_confidence_sd(sample, labels): """Samples sort key by the minimum confidence_sd.""" min_confidence_sd = float("+inf") for inference in sample["inferences"]: if labels and inference["label"] not in labels: continue confidence_sd = inference.get("confidence_sd", f...
def to_ascii(s): """ Force string to ascii :param s: :return: """ s = s.split(b'\x00', 1)[0] return s.decode('ascii', 'ignore').lower()
def get_j2k_parameters(codestream): """Return some of the JPEG 2000 component sample's parameters in `stream`. .. deprecated:: 1.2 Use :func:`~pydicom.pixel_data_handlers.utils.get_j2k_parameters` instead Parameters ---------- codestream : bytes The JPEG 2000 (ISO/IEC 1544...
def group_consecutives(vals, step=0): """Return list of consecutive lists of numbers from vals (number list).""" run = [] result = [run] expect = None for v in vals: if (v == expect) or (expect is None): run.append(v) else: run = [v] result.append(...
def get_offset(num, columns, spacing): """Return offset from prototype position. Positional arguments: num -- the number of the object, starting from 0 columns -- how many columns before wrapping spacing -- a tuple of (x,y), spacing between objects """ x_offset = (num % columns) * spacing[0]...
def first(a, b): """ Compare two iterable objects compares each element in 'a' with every element in 'b' (Elements in 'a' are prioritized) Returns None if there is no match """ for elem in a: if elem in b: return a return None
def city_country(city, country): """Try it yourself 8-6. City names.""" return city.title() + ", " + country.title()
def _is_valid_make_var(varname): """Check if the make variable name seems valid.""" if len(varname) == 0: return False # According to gnu make, any chars not whitespace, ':', '#', '=' are valid. invalid_chars = ":#= \t\n\r" for n in range(0, len(invalid_chars)): if invalid_chars[n] ...
def linear_func(x, a, b): """a * x + b""" return a * x + b
def replaceMultiple(mainString, toBeReplaces, newString): """ Replace a set of multiple sub strings with a new string """ # Iterate over the strings to be replaced for elem in toBeReplaces: # Check if string is in the main string if elem in mainString: # Replace the strin...
def CMakeStringEscape(a): """Escapes the string 'a' for use inside a CMake string. This means escaping '\' otherwise it may be seen as modifying the next character '"' otherwise it will end the string ';' otherwise the string becomes a list The following do not need to be escaped '#' when the lexer is i...
def apply_pred_id(x, labels): """Map Categories to Numeric Labels""" try: return int(labels[x]) except: return -1
def get_four_count_nums(candidates, disallowed_nums): """ if there are four of a given number in the rows and columns not shared by the square under test, and the number isn't already in the block, that number has to be in the square under test (I think) """ collector = {} for num in candidates: ...
def create_custom_var_from_popt(model_image, popt): """Creates variance map from the model image, given the 2nd poly fit parameters Introduced in 0.50 (PIPE2D-931) Parameters ---------- modelImg : `np.array` Model image popt : `np.array` 2d polyfit parameters Returns --...
def args_to_dict(args): """ Transforms the list of arguments received in the subcommand in a dictionary of option names and values (as a list, to cope with options with multiple values like --tag). """ full_args = [] for arg in args: if arg.startswith("--"): full_arg...
def guess_bytes(bstring): """ NOTE: Using `guess_bytes` is not the recommended way of using ftfy. ftfy is not designed to be an encoding detector. In the unfortunate situation that you have some bytes in an unknown encoding, ftfy can guess a reasonable strategy for decoding them, by trying a fe...
def format_match_string(string, fm_stopwords): """ function that converts to lower case and removes stop words """ string = string.lower().split() string = [word for word in string if word not in fm_stopwords] # remove stop words string = ' '.join(string) return string
def parse_commastr(str_comma): """Read comma-sperated string. """ if '' == str_comma: return None else: a, b = map(int, str_comma.split(',')) return [a, b]
def remove_block_hashtags(caption): """attempt to remove hidden hashtags at the bottom of captions""" caption = caption.split('\n', 1)[0] clean_caption = caption.split('\u2022', 1)[0] return clean_caption.strip()
def _cv_delta(x, eps=1.): """Returns the result of a regularised dirac function of the input value(s). """ return eps / (eps**2 + x**2)
def getUserDatabasePath(): """ Returns the path of the user database depending on whether or not this file is being run on reader.py or app.py. """ if __name__ == "__main__": database_path = "../../database/user_database.xlsx" else: database_path = "../database/user_database.xlsx" return dat...
def filter_one_letter_word(tweets): """remove one letter word""" for index in range(len(tweets)): tweets[index] = list( filter(lambda x: len(x) > 1, tweets[index])) return tweets
def _extract_version_number(bazel_version): """Extracts the semantic version number from a version string Args: bazel_version: the version string that begins with the semantic version e.g. "1.2.3rc1 abc1234" where "abc1234" is a commit hash. Returns: The semantic version string, like "1....
def get_happy_stack_name(deployment) -> str: """ Returns the name of the Happy stack for the specified deployment Note: This will only work with deployment={dev,stage,prod} and will not work with rdev! :param deployment: dev, stage or prod :return: """ return f"{deployment}-{deployment}stack...
def kelvin_to_rgb(K): """converts color temperature in Kelvin to RGB values according to http://www.vendian.org/mncharity/dir3/blackbody/UnstableURLs/bbr_color.html""" table = {4000: (1.0000, 0.6636, 0.3583), 5000: (1.0000, 0.7992, 0.6045), 6000: (1.0000, 0.9019, 0.8473), ...
def get_merged_gaps(gaps): """Get gaps merged across channels/streams Parameters ---------- gaps: dictionary contains channel/gap array pairs Returns ------- array_like an array of startime/endtime arrays representing gaps. Notes ----- Takes an dictionary of gap...
def printFinalSolutionToFile(resFileName,outputFileName, key='u_dof',component=0,meshLevel=0,verbose=0): """ write solution component at last (or only time step) on a given mesh level as simple text file """ import os if not os.path.exists(resFileName): prin...
def make_command_line_arguments(bam_file_name, bed_file_name, config_file_name, transcript_file_name, gui_output_file_name): """ Utility function to construct a list of command-line ar...
def scale3(a,c): """ 3 vector, vector ''a'' times scalar ``c``, `a * c`""" return [a[0]*c,a[1]*c,a[2]*c,1.0]
def time_formatter(milliseconds: int) -> str: """Time Formatter""" seconds, milliseconds = divmod(int(milliseconds), 1000) minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) tmp = ( ((str(days) + " day(s), ") if days else "") ...
def tuple_bool(x): """Implementation of `tuple_bool`.""" return len(x) != 0
def get_routing_keys(*args, **kwargs): """Get a list of routing keys for a plugin in order from least specific to most specific. Will return all possible routing keys to get a message to a particular system. args is used to specify routing words. The correct order is System, Version, Instance, Clon...
def isStrictPubKeyEncoding(pubKey): """ isStrictPubKeyEncoding returns whether or not the passed public key adheres to the strict encoding requirements. """ if len(pubKey) == 33 and (pubKey[0] == 0x02 or pubKey[0] == 0x03): # Compressed return True if len(pubKey) == 65 and pubKey...
def get_labels(data): """ Returns the list of labels for the given issue or PR data. """ return [edge["node"]["name"] for edge in data["node"]["labels"]["edges"]]
def get_user_attributes(cls, exclude_methods:bool=True)-> list: """ Get Attributes of a Class :param cls: Class Object :param exclude_methods: Exclude Methods :return: """ base_attrs = dir(type('dummy', (object,), {})) this_cls_attrs = dir(cls) res = [] for attr in this_cls_attrs: ...
def is_cyclic(x, y): """Are these four-digit numbers cyclic?""" # We can safely truncate to int as x and y come from the polygonal funcs. return str(int(x))[2:] == str(int(y))[:2]
def get_best_scaling(target_width, filter_width ): """ target_width: integer width for feature in face. For example width of bounding box for eyes. filter_width: integer width of filter """ # Scale width by 1.1 return 1.1 * (target_width / filter_width)
def email_parser(email): """[Email parsing function] Arguments: email {[str]} -- [email or list of email addresses] """ return [i.strip() for i in email.split(',')]
def byte_to_megabyte(byte): """Convert byte value to megabyte """ return (byte / 1048576)
def str2bool(string_, default='raise'): """ Convert a string to a bool. Parameters ---------- string_ : str default : {'raise', False} Default behaviour if none of the "true" strings is detected. Returns ------- boolean : bool Examples -------- >>> str2bool('True') True >>> str2bool...
def __map_scene_labels_biwi_crowds__(_labels=None): """ map labels from scenes in biwi and crowds dataset to a list of labels that are expected to coincide with the labels present in the paths to the data :param _labels: actual provided labels; if nothing is provided, a default list order is used; if an...
def queue_get_for(topic, host): """ Get a queue name for given topic and host """ return '%s.%s' %(topic, host) if host else topic
def aspcapStar_url(location_id, file_, url_header=None): """ aspcapStar url generator which in principle is able to generate file path Parameters ---------- location_id: int for 'apo1m', it's 1 for 'apo25m', it's like PLATE file_: string FILE url_header: string ...
def pluralize(num, singular): """Return the proper plural version. Examples: >>> pluralize(2, "meme") '2 memes' >>> pluralize(1, "thing") '1 thing' >>> pluralize(1, "class") '1 class' >>> pluralize(0, "class") '0 classes' """ if num == 1: return f"{num} {sing...
def _get_cols(fields, schema): """ Get column metadata for Google Charts based on field list and schema. """ typemap = { 'STRING': 'string', 'INT64': 'number', 'INTEGER': 'number', 'FLOAT': 'number', 'FLOAT64': 'number', 'BOOL': 'boolean', 'BOOLEAN': 'boolean', 'DATE': 'date', 'T...
def check_port(port): """Verifies port value given is valid. Args: port (int): port number to verify Raises: ValueError: if port number provided is invalid Returns: int: port number """ if port < 0 or port > 65535: raise ValueError("Port {} out of range".format(port)) return port
def show_explore_network_btn(enrichment_results): """Shows explore network button after enrichment is done.""" return {'display': 'inline-block'} if enrichment_results else {'display': 'none'}
def _drop_image_percentage(angle): """ We have alot of images that have small steering angles. This function drops angles that are low with a higher percentage, so we have more images with higher angles to train on """ absangle = abs(angle) percentage = 0.5 - 0.05 * absangle return max(0...
def _count_number_of_children_recursively(event): """Recursively steps down the children of an event to calculate the number of children. Args: event (json): Json representing the current event. Returns: The number of children of the current event. """ if len(event['children']) == 0: ...
def extract_comments(comments): """ Utility method for parsing JIRA comments represented as JSON Args: comments: A variable containing JIRA comments in JSON representation. Returns: A string containing all of the JIRA comments tied to an issue """ ...
def normalise_dict(d): """ Recursively convert dict-like object (eg OrderedDict) into plain dict. Sorts list values. """ out = {} for k, v in dict(d).items(): if hasattr(v, "iteritems"): out[k] = normalise_dict(v) elif isinstance(v, list): out[k] = [] ...
def calculate_multiple_choice_task_metrics(pred_dict, labels_dict): """Calculate accuracy for multiple choice tasks. Args: pred_dict: mapping subinstance ids to prediction, where subinstance id is like "0_0", which stands for the 0-th option for the 0-th instance labels_dict: mappin...
def get_urn_from_raw_update(raw_string): """ Return the URN of a raw group update Example: urn:li:fs_miniProfile:<id> Example: urn:li:fs_updateV2:(<urn>,GROUP_FEED,EMPTY,DEFAULT,false) """ return raw_string.split("(")[1].split(",")[0]
def fibonacci(v): """ Computes the Fibonacci sequence at point v. """ if v == 0: return 0 if v == 1: return 1 return fibonacci(v - 1) + fibonacci(v - 2)
def gray (px, *weights, cast = int): """Converts the pixel to grayscale using the given weights (or 1 by default) casting to int""" y = w = cast(0) for x in range(len(px)): z = cast(px[x]) try: y += z * weights[x] w += weights[x] except Exception: y += z w += 1 return y/w
def _format_unit_output(unit): """ Formats an unit to get outputed by the system (Format: "<name> (<function_name to enter>)") :param unit: :return: """ return unit["name"] + " (" + unit["_internal_function_"] + ")"
def test_rast(h, f): """Sun raster file""" if h.startswith(b'\x59\xA6\x6A\x95'): return 'rast'
def _buildstr(D, transpose=False, replace=None): """Construct a string suitable for a spreadsheet. D: scalar, 1d or 2d sequence For example a list or a list of lists. transpose: Bool Transpose the data if True. replace: tuple or None If tuple, it is two strings to pass to the ...
def enumerate_with_prefix(a_list, prefix='pre_'): """ given a list, return a list enumerated with prefix. """ num_digits = len(str(len(a_list))) # eg 5 -> 1, 15 -> 2, 150 -> 3 etc. enum_list = [prefix + str(idx).zfill(num_digits) for idx, el in enumerate(a_list)] return enum_li...
def remove_empty_line(text): """Remove empty line within a multiline string Args: text (str): Mutliline string to process Returns: str: String with empty lines removed """ res = list() for line in text.splitlines(): if line.strip(): res.append(line) retu...
def FormatThousands(value): """Format a numerical value, inserting commas as thousands separators. Args: value: An integer, float, or string representation thereof. If the argument is a float, it is converted to a string using '%.2f'. Returns: A string with groups of 3 digits before the decimal po...
def es_par(numero): """ (num) -> boolean Valida si un numero es par >>> es_par(10) True >>> es_par(20) True >>> es_par(189) False :param numero: el numero a evaluar :return: True si el numero es par, False de lo contrario """ return numero % 2 == 0
def match(s: str, substring: str) -> bool: """Return True if substring in str""" return s.find(substring) != -1
def weight_pp(perc): """Compute the weighted percentage. The gravity is near 0%.""" if perc > 75.0: return perc elif perc > 50.0: return perc * 0.75 else: return perc * 0.5
def IntToRgb(RGBint: int): # -> typing.Tuple[int,int,int]: """Converts a integer color value to a RGB tuple :param RGBint: :class:`int` The integer color value. :returns: :class:`tuple[int,int,int]` RGB tuple """ blue = RGBint & 255 green = (RGBint >> 8) & 255 red = (RGBin...
def steering_constraint(steering_angle, steering_velocity, s_min, s_max, sv_min, sv_max): """ Steering constraints, adjusts the steering velocity based on constraints Args: steering_angle (float): current steering_angle of the vehicle steering_velocity (float): unconstraint desi...
def stateIsChange(stateOld,stateNew): """ stateIsChange stateIsChange - check is state change or not """ if stateOld == None or stateNew == None: return False if stateOld != stateNew: stateOld = stateNew print('value is changed {}'.format(stateOld)) return Tru...
def get_if_exist(data, keys): """ Recursively get a value from a nested dictionary Parameters ---------- data : dict The (nested) dictionary keys : list The list of keys to fetch Returns ------- any or None The value at data[keys[0]][keys[1]] etc. or None if a k...
def get_pbf_url(region, subregion): """Returns the URL to the PBF for the region / subregion. Parameters ---------------------- region : str subregion : str Returns ---------------------- pbf_url : str """ base_url = 'https://download.geofabrik.de' if subregion is None: ...
def points_from_bbox(minx, miny, maxx, maxy): """Construct polygon coordinates in page representation from a numeric list representing a bounding box.""" return "%i,%i %i,%i %i,%i %i,%i" % ( minx, miny, maxx, miny, maxx, maxy, minx, maxy)
def as_list(x, length=1): """Return x if it is a list, else return x wrapped in a list.""" if not isinstance(x, list): x = length*[x] return x
def setup_java_class(content_to_add): """ returns an example java class with the given content_to_add contained within a method. """ template = """ public class Lambda { public static void main(String args[]) { %s } } """ return template % content_to_add
def dash_to_slash(datetime_str: str) -> str: """Convert d-m-y to y-m-d where original data recorded day in year format """ date, time = datetime_str.split() date_arr = date.split('-') if len(date_arr[0]) > 2: date_arr[0] = date_arr[0][-2:] date_str = '/'.join(date_arr) ret_string = d...
def unquote_header_value(value, is_filename=False): """Unquotes a header value. Reversal of :func:`quote_header_value`. This does not use the real un-quoting but what browsers are actually using for quoting. :param value: the header value to unquote. """ if value and value[0] == value[-1] == '...
def bin_dec(bin): """Conversion binary -> decimal. Needed to calculate decimal variable value from binary coded genome.""" dec=0.0 bin.reverse() for i in range(0, len(bin)): dec+=(bin[i]*(2**i)) return dec
def bfs(graph, start, goal): """ Breath first search on a given graph >>> bfs({'A': set(['B']), ... 'B': set(['C']), ... 'C': set()}, 'A', 'C') ['A', 'B', 'C'] """ queue = [(start, [start])] while queue: (vertex, path) = queue.pop(0) for next_node in graph[vertex] - s...
def align_on_left(txt: str) -> str: """ Remove all leading/trailing spaces for each line. """ txt_out = [] for curr_line in txt.split("\n"): curr_line = curr_line.rstrip(" ").lstrip(" ") txt_out.append(curr_line) res = "\n".join(txt_out) return res
def _get_jsonld_property(jsonld, property, default=None): """Return property value from expanded JSON-LD data.""" value = jsonld.get(property) if not value: return default if isinstance(value, list) and len(value) == 1 and isinstance(value[0], dict) and "@value" in value[0]: value = valu...
def get_min_max_words(input): """ returns the words with the least and maximum length. Use min and max and pass another function as argument """ return (min(input,key=len),max(input,key=len))# replace this calls to min and max #(sorted(input,key=len)[0],sorted(input,key=len)[len(input)-1])...
def _in_ranges(x, bins): """Function for pandas.apply() that assigs values into bins """ return [((x >= lower) & (x <= upper)) for lower, upper in bins]
def get_fuzzer_setting(fuzzer_settings, fuzzer_setting_name): """Read the fuzzer setting from the list of dict.""" for fuzzer_setting in fuzzer_settings: if "Name" in fuzzer_setting and fuzzer_setting["Name"] == fuzzer_setting_name: return fuzzer_setting return None
def mapLists(first, second): """ Make a dictionary from two lists with elements of the first as the keys and second as values. If there are more elements in the first list, they are assigned None values and if there are more in the second list, they're dropped. """ index = 0 dict = {} # Read through every index ...
def non_increasing(py_list): """ check if elements of a list are increasing monotonically. """ return all(x >= y for x, y in zip(py_list, py_list[1:]))
def expandCigar(cigar): """ Turns the abbreviated cigar into the full array 0 = M 1 = I 2 = D """ ret = [] for t,s in cigar: ret.extend([t]*s) return ret
def underscore_to_camel(string): """ Converts an undescored_name @string to UnderscoredName @string: #str object .. from vital.tools import underscore_to_camel underscore_to_camel("ted_koppel") # -> TedKoppel .. """ return "".join(s.capitalize()...
def bisect(slist, value): """ Use the bisection method to find the index of a word in a list. Precondition: list is sorted """ if not all(slist[i] <= slist[i+1] for i in range(len(slist)-1)): print('Please supply a sorted list.') return None start = 0 end = len(slist) mid...
def homogenize(xyz, w=1.0): """Homogenise a list of vectors. Parameters ---------- xyz : sequence[[float, float, float] | :class:`~compas.geometry.Point`] | sequence[[float, float, float] | :class:`~compas.geometry.Vector`] A list of points or vectors. w : float, optional Homogenisa...
def determine_issue_types(warnings): """ Get a list of issue types. :rtype: list """ issue_types = warnings["Report"]["IssueTypes"]["IssueType"] if not isinstance(issue_types, list): return [issue_types] return issue_types
def factorial_3(n, acc=1): """ Replace all recursive tail calls f(x=x1, y=y1, ...) with (x, y, ...) = (x1, y1, ...); continue """ while True: if n < 2: return 1 * acc (n, acc) = (n - 1, acc * n) continue break
def _CalcDelta(from_ts, to_ts): """ Calculates the delta between two timestamps. """ return to_ts[0] - from_ts[0] + (to_ts[1] - from_ts[1]) / 1000000.0
def str_to_list(string): """ Parameters ---------- string String representation of a list Returns A List ------- """ if "[" and "]" in string: string = string[1:-1] spt = string.split(",") lis = [] for f in spt: lis.append(floa...
def getsize(datadescriptor): """Get the size of a data descriptor tuple.""" if datadescriptor[0] == 'reg': size = datadescriptor[1][2] elif datadescriptor[0] == 'mem': size = datadescriptor[1][1] elif datadescriptor[0] == 'heap': size = datadescriptor[1][2] elif datadescriptor[0] == 'perp': size = datade...