content
stringlengths
42
6.51k
def try_except(var, d, text1, text2): """ Helps to deal with exceptions :param var: string, that needs to be converted to integer :param d: right border of possible values for integer :param text1: message that will appear if the task number is not integer :param text2: message that will appear...
def make_shell_logfile_data_url( host: str, shell_port: int, instance_id: int, offset: int, length: int, ) -> str: """ Make the url for log-file data in heron-shell from the info stored in stmgr. """ return ( f"http://{host}:{shell_port}" f"/filedata/log-files/{instance_id}.log...
def limitslist(limits): """ Translate a one dimensional vector to a list of pairs of consecutive elements. """ return [(mini, limits[i+1]) for i, mini in enumerate(limits[:-1])]
def catch_function_errors(fn, *args, **kwargs): """Returns function return value or None if there are errors.""" try: result = fn(*args, **kwargs) # Keyboard interrupts should stop server except KeyboardInterrupt: raise # Notify user that error occurred except Exception as err: ...
def cauchy(xs, h, y0, f, **derivatives): """Cauchy""" ys = [y0] for k in range(len(xs) - 1): subsidiary_y = ys[k] + f(xs[k], ys[k]) * h / 2 next_y = ys[k] + f(xs[k] + h / 2, subsidiary_y) * h ys.append(next_y) return ys
def fuse_events(events, fuse_duration=0.5, target_duration=5.0): """From list of (start, stop) times, create list with them merged """ sorted_events = sorted(events) result = [] for event in sorted_events: if len(result) == 0: result.append(list(event)) elif result[-1][1...
def rotation_cs(X, Y, c, s) : """For numpy arrays X and Y returns the numpy arrays of Xrot and Yrot for specified rotation angle cosine and sine values. """ Xrot = X*c - Y*s Yrot = Y*c + X*s return Xrot, Yrot
def resolve_alias(term: str) -> str: """ Resolves search term aliases (e.g., 'loc' for 'locations'). """ if term in ("loc", "location"): return "locations" elif term == "kw": return "keywords" elif term == "setting": return "setting" elif term == "character": ...
def iou(box_a, box_b): """ Calculates the IOU between two boxes. For example: >>> iou([0.5, 0.5], [1, 1]) 0.25 :param box_a: :param box_b: :return: """ c_w, c_h = box_b w, h = box_a if c_w >= w and c_h >= h: intersection, union = w * h, c_w * c_h elif c_w ...
def b128_encode(n): """ Performs the MSB base-128 encoding of a given value. Used to store variable integers (varints) in the LevelDB. The code is a port from the Bitcoin Core C++ source. Notice that the code is not exactly the same since the original one reads directly from the LevelDB. The encoding i...
def Euler003(n): """Solution of third Euler problem.""" pm = 2 while n != 1: if n % pm == 0: n = n / pm else: pm += 1 return pm
def fitness_function(binary_string): """OneMax - Returns number of ones in a string""" return binary_string.count('1')
def count_coll_com(s): """Count how many commenters are colleagues.""" auth = set([a for a in s["auth"] if a]) coms = [set(c.strip("-").split("-")) for c in s["coms"] if c and c not in ("?", "nan")] if not coms: return None return len([c for c in coms if len(c.intersection(auth))...
def _get_difference_locator(expected_image_locator, actual_image_locator): """Returns the locator string used to look up the diffs between expected_image and actual_image. Args: expected_image_locator: locator string pointing at expected image actual_image_locator: locator string pointing at actual image...
def clean_key(key): """ Clean the key words""" key = key.replace(',', '').replace('\n', ' ').replace('\t', ' ').replace(' ', ' ').strip() return key
def median(data_list): """ Finds the median in a list of numbers. :type data_list list """ data_list = list(map(float, data_list)) length = len(data_list) data_list.sort() # Test whether the length is odd if length & 1: # If is is, get the index simply by dividing it in half...
def to_std_dicts(value): """Convert nested ordered dicts to normal dicts for better comparison.""" if isinstance(value, dict): return {k: to_std_dicts(v) for k, v in value.items()} elif isinstance(value, list): return [to_std_dicts(v) for v in value] else: return value
def parse_cpu_info_tag_value(line): """parse the /proc/cpuinfo.""" elems = line.split(":") if len(elems) == 2: return (elems[0].strip(), elems[1].strip()) return (None, None)
def is_lower_camel(text): """ Check if a string is in lowerCamelCase format :param text: String to check :return: Whether string is in lower camel format """ if " " in text: return False return text[0].islower() and "_" not in text and not text.islower()
def compare_values(value1, value2, relative, absolute): """ Compare two values with respect to a relative and an absolute deviation. :param value1: First value :param value2: Second value :param relative: Relative deviation (0..1) :param absolute: Absolute deviation (e.g. 1, -5.7, 100) :retu...
def _dictionary_to_column_paths(dictionary, prefix=tuple()): """Convert a dictionary to the column paths within this dictionary For example, if the argument is { 1 : { 'a' : True, 'b' : False }, (10, 'blah') : SomeObject() } The result would be [ ...
def hex_to_rbg(hex_color: str) -> tuple: """ Convert hex color to rgb example: #FFFFFF to (255, 255, 255) :param hex_color: :return: """ r = int(hex_color[1:3], 16) g = int(hex_color[3:5], 16) b = int(hex_color[5:7], 16) return r, g, b
def normal_round(num, ndigits=0): """ Rounds a float to the specified number of decimal places. num: the value to round ndigits: the number of digits to round to """ if ndigits == 0: return int(num + 0.5) else: digit_value = 10 ** ndigits return int(num * digit_value ...
def BuildInitialList(LastNum): """ Builds an array of boolean values (0/1) filled with 1/3 of the provided LastNum, reduced by 1. First element is 0, all other elements are 1. """ return [0] + [1] * (int(LastNum / 3) - 1)
def sparse_search(arr, s): """ 10.5 Sparse Search: Given a sorted array of strings that is interspersed with empty strings, write a method to find the location of a given string. EXAMPLE: Input: find "ball" in {"at", "", "", "" , "ball", "", "", "car", "" , "" , "dad", ""} Output: 4 """ def ...
def hardlim(n): """ Hard Limit Transfer Function :param int n:Net Input of Neuron :return: Compute Heaviside step function :rtype: int """ if n<0: return 0 else: return 1
def f2measure(precision, recall): """Returns the f2measure (or F2-score)""" return (1.0 + (2 ** 2)) * ((precision * recall) / ((2 ** 2 * precision) + recall))
def check_novelty_objects(smiles,training_smiles, verbose=False): """Need to pass in valid smiles""" count_in_training=0 num_molecules_generated = len(smiles) if num_molecules_generated==0: return 0 else: training_smiles_set=set(training_smiles) num_molecules_training = len(t...
def dToB(n, numDigits): """requires: n is a natural number less than 2**numDigits returns a binary string of length numDigits representing the the decimal number n.""" assert type(n)==int and type(numDigits)==int and n >=0 and n < 2**numDigits bStr = '' while n > 0: bStr = st...
def _extended_euclidean(a, b): """Helper function that runs the extended Euclidean algorithm, which finds x and y st a * x + b * y = gcd(a, b). Used for gcd and modular division. Returns x, y, gcd(a, b)""" flip = b > a if flip: a, b = b, a if b == 0: return 1, 0, a x, y, gcd = _extended_eucl...
def verify_education(education_str: str) -> bool: """ A helper function to make verifying education strings easier Parameters ---------- education_str: str The single letter education string to be verified. Valid inputs are: 'b', 'm', 'd' Returns ---------- bool: Return...
def get_buffer_base_name(noise, size, data_type, ending): """Get the replay buffer name automatically from arguments. We make two versions (for now), one with .p for the pickle file of data, and another with .txt for logged data, to tell us behavioral policy performance. """ assert ('constant_' in ...
def get_clusters_size(n_samples, n_clusters): """Gets the number of members per cluster for equal groups kmeans""" return (n_samples + n_clusters - 1) // n_clusters
def sum_n_numbers(target, nums, n): """Find target sum from n numbers in nums.""" sums = {num: [num] for num in nums} for iteration in range(n - 1): for num in nums: updates = {} for cur_num, sum_from in sums.items(): # print(cur_num, sum_from) ...
def parse(input_list : list) -> dict: """Parse the flags of a written line into dictionary representing the flags The outputted dict is in the form of {FLAG:value, ...}, where value is Union[str, int, list]. Note that the preceeding '-' is removed from flag dict key. The default arguments (with no fla...
def interpolate(val, min, max): """ Interpolates values between 0 and 1 to values between the specified min and max. Used primarily map values generated by random.random() to a specified range. """ return val * (max - min) + min
def matching_ngram_completions(comparison_seq, hypothesis, n): """ Return the list of words that if appended to hypothesis, would create a n-gram that already exists in comparison_seq. For efficiency, this function represents words as integers not strings. Inputs: comparison_seq: list of in...
def contains(vector, space): """ :param vector: [0.26, 0.19] :param space: [ [0.5, 1.0], [0.5, 1.0] ] :return: True / False """ flag = True for ind, value in enumerate(vector): if value < space[ind][0] or value > space[ind][1]: flag = False break return...
def uniquify(l): """ Uniquify a list (skip duplicate items). """ result = [] for x in l: if x not in result: result.append(x) return result
def rfc3339strFromBeamBQStr(s): """Convert a string formatted BEAM_BQ_TIMESTAMP_FORMAT to RFC3339_TIMESTAMP_FORMAT""" return s[:-4].replace(' ', 'T')+'+00:00'
def extract_meta_instance_id(form): """Takes form json (as returned by xml2json)""" if form.get('Meta'): # bhoma, 0.9 commcare meta = form['Meta'] elif form.get('meta'): # commcare 1.0 meta = form['meta'] else: return None if meta.get('uid'): # bhoma ...
def knot_insertion_alpha(u, knotvector, span, idx, leg): """ Computes :math:`\\alpha` coefficient for knot insertion algorithm. :param u: knot :type u: float :param knotvector: knot vector :type knotvector: tuple :param span: knot span :type span: int :param idx: index value (degree-dep...
def _remove_long_seq(maxlen, seq, label): """Removes sequences that exceed the maximum length. # Arguments maxlen: Int, maximum length of the output sequences. seq: List of lists, where each sublist is a sequence. label: List where each element is an integer. # Returns new_...
def piglatin(word): """ IN this function changing word to a pig latin word""" vowels = "aeiou" if word[0] in vowels: new_word = word[1:] + word[0] + "way" else: new_word = word[1:] + word[0] + "ay" return new_word
def sum_numbers_from_lists(list1, list2,): """Sums two integer lists""" temp_list = [] for i in range(len(list1)): temp_num = list1[i] + list2[i] if temp_num > 26: temp_num -= 26 #temp_num = temp_num % 26 temp_list.append(temp_num) return temp_list
def make_shell_logfile_data_url(host, shell_port, instance_id, offset, length): """ Make the url for log-file data in heron-shell from the info stored in stmgr. """ return "http://%s:%d/filedata/log-files/%s.log.0?offset=%s&length=%s" % \ (host, shell_port, instance_id, offset, length)
def create_url_by_date(file_date): """ Returns the url of the gkg zip file to download from the Gdelt project. @Param file_date: Date of the file to download in the YYYYMMDDHHMMSS format (UTC). @Return url to the gkg zip file. """ # Url to the our data url_header = "http://data.gdeltproject....
def difference(array_a: list, array_b: list) -> list: """ Calculates the differences between two given values. :param array_a: list[int | float] :param array_b: list[int | float] :return: - differences - list[int | float] """ differences = [] if len(array_a) == len(array...
def _en_to_enth(energy, concs, A, B, C): """Converts an energy to an enthalpy. Converts energy to enthalpy using the following formula: Enthalpy = energy - (energy contribution from A) - (energy contribution from B) - (energy contribution from C) An absolute value is taken afterward for conveni...
def config_contains_tar(pipeline_config: dict, tags=None) -> bool: """ Check if the input file list contains a `.tar` archive. """ if tags is None: tags = ["archive", "inside_archive"] params = pipeline_config.get("params", {}) ffiles = params.get("fetch_files", []) for ff in ffile...
def closest_divisor(a, b): """ Returns the divisor of `a` that is closest to `b`. Parameters ---------- a, b : int Returns ------- int """ if b >= a or b == 0: return a # b=0 is special case. for test in range(b, 0, -1): if a % test == 0: return test assert(Fal...
def extract(s, start): """Given a string and index position to the first '(', extract a full group up to its ')'.""" level = 1 for idx in range(start+1, len(s)): ch = s[idx] if ch == '(': level += 1 if ch == ')': level -= 1 if level == 0: r...
def switch_encoding(phasing): """ >>> switch_encoding('0001011') '001110' """ assert isinstance(phasing, str) return ''.join(('0' if phasing[i-1] == phasing[i] else '1') for i in range(1, len(phasing)))
def partition(iterable, pivot, key): """Returns 3 lists: the lists of smaller, equal and larger elements, compared to pivot. Elements are compared by applying the key function and comparing the results""" pkey = key(pivot) less = [] equal = [] greater = [] for x in iterable: k = ke...
def guess_n_eigs(n_electron, n_eigs=None): """ Guess the number of eigenvalues (energies) to compute so that the smearing iteration converges. Passing n_eigs overrides the guess. """ if n_eigs is not None: return n_eigs if n_electron > 2: n_eigs = int(1.2 * ((0.5 * n_electron) + 5)) ...
def find_mimetype(filename): """In production, you don't need this, Static files should serve by web server, e.g. Nginx. """ if filename.endswith(('.jpg', '.jpep')): return 'image/jpeg' if filename.endswith('.png'): return 'image/png' if filename.endswith('.gif'): return ...
def get_device_status_sensortext(sensor_status): """ Get Text Information from Sensor Status int using Binary logic """ sensor_int = int(sensor_status) sensor_text = str(sensor_status) + ", " if sensor_int == 0: sensor_text = "OK, " elif sensor_int == 4: # "Lightning Disturber" sens...
def GetContentForTemplate(api_query): """Prepares and returns the template value for an API Query response. Args: api_query: The API Query for which to prepare the response content template value. Returns: A dict containing the template value to use for the Response content. """ conten...
def sequences_add_start_id(sequences, start_id=0, remove_last=False): """Add special start token(id) in the beginning of each sequence. Parameters ------------ sequences : list of list of int All sequences where each row is a sequence. start_id : int The start ID. remove_last : ...
def list2str(args): """ Convert list[str] into string. For example: ['x', 'y'] -> "['x', 'y']" """ if args is None: return '[]' assert isinstance(args, (list, tuple)) args = ["{}".format(arg) for arg in args] return repr(args)
def format_weighted_edges(retweeted): """Return a dictionary with the weight.""" elements = [] for key, value in retweeted.items(): source, destiny = key weight = value elements.append((source, destiny, weight)) return elements
def get_default_semantics(n_dim): """ Generate default semantics for n_dim dimensions. Parameters ---------- n_dim : int Number of dimensions. Indicating the number of variables of the model. Returns ------- semantics: dict Generated model description. """ sem...
def get_public_scoreboard(): """Gets the archived public scoreboard. Kind of a hack, tells the front end to look for a static page scoreboard rather than sending a 2000+ length array that the front end must parse. """ return {'path': '/staticscoreboard.html', 'group': 'Public'}
def dec2bit(value, bits=8): """ bits=8: 42 -> (False, True, False, True, False, True, False, False) """ v = value % 2**bits seq = tuple(True if c == '1' else False for c in bin(v)[2:].zfill(bits)[::-1]) if value - v > 0: seq = seq + dec2bit(value // 2**bits) return seq
def CalDistPointToFragment(x, fragment):#{{{ """ MMMMMMM K dist = 1 K dist = 0 R dist = 0 """ if x <= fragment[0]: dist = fragment[0]-x elif x >= fragment[1]: dist = x-fragment[1]+1 else: d1 = x - fragment[0] d2 = (fragmen...
def convert_value(var): """Convert the metric value from string into python type.""" if var["type"] == "number": return float(var["value"]) if var["type"] == "boolean": return var["value"] == "true" if var["type"] == "string": return var["value"] print("can't convert unknown...
def lcm(a: int, b: int) -> int: """ https://en.wikipedia.org/wiki/Least_common_multiple >>> lcm(4, 6) 12 >>> lcm(21, 6) 42 """ from math import gcd return (a // gcd(a, b)) * b
def sentence2id(sent, word2id): """ :param sent: :param word2id: :return: """ sentence_id = [] for word in sent: if word.isdigit(): word = '<NUM>' elif ('\u0041' <= word <= '\u005a') or ('\u0061' <= word <= '\u007a'): word = '<ENG>' if word not...
def eh_tabuleiro(tabuleiro): """ Function that receives any type of variable. Checks if it is a tabuleiro. """ if not isinstance(tabuleiro, tuple) or len(tabuleiro) != 3: return False if not all(isinstance(line, tuple) for line in tabuleiro): return False if (...
def reverse_digits(n: int) -> int: """Returns: The integer with all digits reversed. Example: 4297341 would return 1437924 """ negative = False if n < 0: negative = True n = -n result = 0 while n > 0: result = result * 10 + n % 10 n //= 10 if negative: return -result return re...
def _validate_text(text): """If text is not str or unicode, then try to convert it to str.""" if isinstance(text, str): return text.encode() else: return str(text).encode()
def _list_geojson(list_stops): """ Creates a list of stop data in GeoJSON format. :param list_stops: List of StopPoint objects. :returns: JSON-serializable dict. """ geojson = { "type": "FeatureCollection", "features": [s.to_geojson() for s in list_stops] } return g...
def import_obj(obj_path, hard=False): """ import_obj imports an object by uri, example:: >>> import_obj("module:main") <function main at x> :param obj_path: a string represents the object uri. :param hard: a boolean value indicates whether to raise an exception on impor...
def isGSM19(filename): """ Checks whether a file is GSM19 format. """ try: temp = open(filename, 'rt') #, encoding='utf-8', errors='ignore' except: return False try: li = temp.readline() except: return False while li.isspace(): li = temp.readline()...
def insert_sort(lst): """Insert sort.""" for i in range(len(lst)): k = lst[i] j = i while j > 0 and k < lst[j - 1]: lst[j] = lst[j - 1] j -= 1 lst[j] = k return lst
def column_fill(keyword): """``column-fill`` property validation.""" return keyword in ('auto', 'balance')
def oneHotEncode_3_evtypes(x, r_vals=None): """ This function one hot encodes the input for the event types cascade, tracks, doubel-bang """ # define universe of possible input values cascade = [1., 0., 0.] track = [0., 1., 0.] s_track = [0., 0., 1.] # map x to possible classes ...
def responseBuilder(responseDict): """ accept dictionary, build response string """ #Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF statusLine = responseDict['statusLine']['httpVersion'] + " " statusLine += responseDict['statusLine']['statusCode'] + " " statusLine += responseDi...
def get_photo_image_name_from_img_src(img_src, exclude = '_display'): """Parses img_src gotten from photologue's .get_display_url() (or thumbnail) (E.g. "to_del_photo_/media/photologue/photos/cache/ImageName_rndnumbers_display.jpg") and returns a substring of photo's image's name. (E.g. "ImageName_rndnumber...
def _headers(skey): """Returns the auth header for Splunk.""" return { 'Authorization': 'Splunk {0}'.format(skey), 'Content-Type': 'application/json', 'Accept': 'application/json', }
def smooth(list_, half_window=1): """Use box averaging smoothing.""" new_list = [] for i, _ in enumerate(list_): jjs = [j for j in range(i - half_window, i + half_window + 1) if 0 <= j < len(list_)] new_list.append(sum([list_[k] for k in jjs]) / len(jjs)) return new_list
def merge_and_count_inversions(left_tuple, right_tuple): """ Count the number of split inversions while merging the given results of the left and right subproblems and return a tuple containing the resulting merged sorted list and the total number of inversions. left_tuple -- a tuple containi...
def time_unit(val): """Time unit constraint finds values of the form: 10ns """ import re return re.search(r'^\d+(s|ms|us|ns|ps|fs)$', val) is not None
def add_sys_path(new_path): """ Adds a directory to Python's sys.path Does not add the directory if it does not exist or if it's already on sys.path. Returns 1 if OK, -1 if new_path does not exist, 0 if it was already on sys.path. Based on: https://www.oreilly.com/library/view/python-cookbook/05960...
def binary_search_iterative(lst, key, start=0, end=None): """ Performs binary search for the given key in iterable. Parameters ---------- lst : python iterable in which you want to search key key : value you want to search start : starting index end : ending index Returns -----...
def remove_overlap(ranges): """ Simplify a list of ranges; I got it from https://codereview.stackexchange.com/questions/21307/consolidate-list-of-ranges-that-overlap """ result = [] current_start = -1 current_stop = -1 for start, stop in sorted(ranges): if start > current_stop: # this segment starts after t...
def sub(proto, *args): """ Format string prototype I{proto} with I{args}. This really should be a built-in function. """ try: return proto.format(*args) except: raise ValueError("Proto '{}' couldn't apply args {}", proto, args)
def keep_faces(faces, indices): """ Remove surface mesh faces whose three vertices are not all in "indices". Parameters ---------- faces : list of lists of three integers the integers for each face are indices to vertices, starting from zero indices : list of integers indices to...
def is_power_of_two(n): """ Return True if n is a power of 2 """ return (n & (n - 1) == 0) and n != 0
def extract_name_from_file(filename): """ """ return filename.replace(".py", "")
def part2(input_data): """ >>> part2([1721,979,366,299,675,1456]) 241861950 """ for entry1 in input_data: for entry2 in input_data: for entry3 in input_data: if entry1 + entry2 + entry3 == 2020: return entry1 * entry2 * entry3 return None
def _create_eip712_market_join(chainId: int, verifyingContract: bytes, member: bytes, joined: int, marketId: bytes, actorType: int, meta: str) -> dict: """ :param chainId: :param verifyingContract: :param member: :param joined: :param marketId: :param actorTyp...
def run(location,option=None): """run function, needs definition""" print() print('He is following you, about 30 feet back.') print() return location
def recursive_unzip(dictionary, item_idx): """ "Unzip" dictionary """ ret = {} for key, item in dictionary.items(): if isinstance(item, dict): ret[key] = recursive_unzip(item, item_idx) else: ret[key] = item[item_idx] return ret
def split_year(title): """Returns (title base, year) Some categories have a year at the end. This detects that and returns a split. Example: >>> split_year('Foo') ('Foo', None) >>> split_year('PyCon 2013') ('PyCon', 2013) """ try: title = title.strip() return t...
def is_stringlike(item): """Is item string like?""" try: item + 'string' return True except TypeError: return False
def _chainCompareRecurse(a, b, k): """Compare implicit slices a[k:] and b[k:]""" if len(a) == k: return len(b) > k elif len(b) == k: return False elif a[k][0] < b[k][0]: return True elif b[k][0] < a[k][0]: return False elif a[k][1] == b[k][1]: return _chai...
def euclidan_distance_sqr(point1, point2): """ >>> euclidan_distance_sqr([1, 2], [2, 4]) 5 """ return (point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2
def getStr(bytes_arr): """Create string from ASCIIZ string buffer""" strng = "" for value in bytes_arr: if value != 0: strng = strng + chr(value) else: break return strng
def is_tableau_code(tabcode): """ Return True iff two character string is a valie tableau code. """ if ( (tabcode[0] not in ['L','R','P','O'] or tabcode[1] not in ['E','D','S','T']) and tabcode != 'HH' and tabcode != 'KK' ): return False else: return True