content
stringlengths
42
6.51k
def crossProduct(p1, p2, p3): """ Cross product implementation: (P2 - P1) X (P3 - P2) :param p1: Point #1 :param p2: Point #2 :param p3: Point #3 :return: Cross product """ v1 = [p2[0] - p1[0], p2[1] - p1[1]] v2 = [p3[0] - p2[0], p3[1] - p2[1]] return v1[0] * v2[1] - v1[1] * v2[0...
def gatekeeper_add_to_list_display(serial=False): """ This adds fields to list_display for the Admin changelist page for the model. """ if serial: return ['show_publish_status', 'is_live', 'default_live'] return ['show_publish_status','available_to_public']
def lcg(x, length=16): """Linear congruential generator""" if x == 0: return bytes(length) out = bytearray(length) for i in range(length): x = 214013 * x + 2531011 & 2147483647 out[i] = x >> 16 & 255 return bytes(out)
def has_a_double_not_in_larger_group(s): """ >>> has_a_double_not_in_larger_group('1234') False >>> has_a_double_not_in_larger_group('111123') False >>> has_a_double_not_in_larger_group('135679') False >>> has_a_double_not_in_larger_group('223450') True >>> has_a_double_not_in_la...
def make_key(pattoo_agent_program, key): """Prepend the Agent program name to the key for uniqueness. Args: pattoo_agent_program: Program name key: Key Returns: result: Result """ # Return result = '{}_{}'.format(pattoo_agent_program, key) return result
def planetary_temp(S, A, L=1.0): """Calculate the planetary temperature. SL(1-A) = sT**4 Arguments --------- S : float Incident solar energy. A : float Planetary albedo. Keyword Arguments ----------------- L = 1.0 : float Normalised stellar luminosity. "...
def __neighcom(node, graph, status, weight_key): """ Compute the communities in the neighborhood of node in the graph given with the decomposition node2com """ weights = {} for neighbor, datas in graph[node].items(): if neighbor != node: edge_weight = datas.get(weight_key, 1)...
def validate_int(arg): """Guard against value errors when attempting to convert a null to int""" if len(arg) < 1: return 0 return int(arg)
def _exp_format(val, prec): """ [Docstring] """ # Convert val using string formatting: Always a leading space; # positive values with another leading space; negatives with the negative # sign; one digit in front of the decimal, 'dec' digits after. # Capital 'E' for the exponent. out = " {{...
def file_type(filename, stream=False): """ Detect potential compressed file Returns the gz, bz2 or zip if a compression is detected, else None. """ magic_dict = { "\x1f\x8b\x08": "gz", "\x42\x5a\x68": "bz2", "\x50\x4b\x03\x04": "zip" } max_len = max(len(x) for x in magic_dict) if not stream: ...
def cipher(map_from, map_to, code): """ map_from, map_to: strings where each contain N unique lowercase letters. code: string (assume it only contains letters also in map_from) Returns a tuple of (key_code, decoded). key_code is a dictionary with N keys mapping st...
def accuracy(y_true, y_pred): """Accuracy score function. Easy-to-use word tokenize function. Example: >>> from reason.metrics import accuracy >>> accuracy(y_true, y_pred) 0.9358 Args: y_true (list): Real labels. y_pred (list): Predicted labels returned by clas...
def _split(text, plan): """Recursive function to split the *text* into an n-deep list, according to the :py:class:`hl7._ParsePlan`. """ # Base condition, if we have used up all the plans if not plan: return text if not plan.applies(text): return plan.container([text]) # Par...
def _strip_tweet_hashtags(status_text: str) -> str: """Strip out words from tweet that are hashtags (ie. begin with a #).""" text_split = [word for word in status_text.split() if not word.startswith("#")] text = " ".join(text_split) return text
def check_config_ex_len(model, config_ex): """Get length for model config_ex.""" if model == "0001": if len(config_ex) == 6: return True elif model == "0002": if len(config_ex) == 3: return True elif model == "0100": if len(config_ex) == 8: ret...
def _list(values): """ >>> assert _list([1,2,[3,4,5,[6,7]],dict(a =[8,9], b=[10,[11,12]])]) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] >>> assert _list(1) == [1] >>> assert _list(dict(a=1, b=2)) == [1,2] """ if isinstance(values, list): return sum([_list(df) for df in values], ...
def make_initiator_target_all2all_map(initiator_wwpns, target_wwpns): """Build a simplistic all-to-all mapping.""" i_t_map = {} for i_wwpn in initiator_wwpns: i_t_map[str(i_wwpn)] = [] for t_wwpn in target_wwpns: i_t_map[i_wwpn].append(t_wwpn) return i_t_map
def period_at_end(token): """ Args: token (str): word being evaluated Returns: binary: True if last character is a period, false if not. """ if list(token).pop() is ".": return True else: return False
def linear_service_fee(principal, fee=0.0): """Calculate service fee proportional to the principal. If :math:`S` is the principal and :math:`g` is the fee aliquot, then the fee is given by :math:`gS`. """ return float(principal * fee)
def divide_grupos(alunos, grupos): """Function that gets the total of students and groups and does the division, in a optimized way. If it's a round division, returns the number of students per group If it's not a round division, the list presents: [quantity of groups of type 1 quantity of students per ...
def value_type(value, types): """ Check that the ``value`` type is one of ``types``. Parameters ---------- value: Any Variable to check its type. types: type or tuple or array Acceptable types. Could be one type, or a tuple or array of types. Raises ------ V...
def extend_list_series(nestlist): """Extend nested lists in lists""" series=[] for n in nestlist: series+=n return series
def round_down(x, n): # type: (int, int) -> int """Round down `x` to nearest `n`.""" return x // n * n
def calculate_grid_points(size, buffer, bars_per_line, lines_per_page): """ Calculates and returns two lists. The first list consists of x-coordinates of all bar lines. The second list consists of y-coordinates of all center staff lines. Parameters ---------- size : 2-tuple of ints ...
def pretty_ssh_key_hash(pubkey_fingerprint): """ Returns a pretty json from raw pubkey KEY_BITS KEY_HASH [JERK] (AUTH_TYPE) """ try: key_bits = int(pubkey_fingerprint.split(' ')[0]) except ValueError: key_bits = 0 except IndexError: key_bits = 0 try: key_...
def find_prime(n): """ Finds a prime greater than n. In this case, it finds the first prime greater than n. """ primes = [3] candidate = 5 while primes[-1] < n: is_prime = True for prime in primes: if candidate % prime == 0: is_prime = False ...
def lines_into_traces (lines): """Convert a list of split ASCII text lines into traces (a list of lists of floats)""" traces = [] num_of_traces = len(lines[0]) #work out how many traces from the no of columns ## make an empty list for i in range(num_of_traces): traces.append([]) ## transpose lin...
def ind_to_sub(n, ix): """Convert index from flattened upper triangular matrix to pair subindex. Parameters ---------- n : int Dimension size of square array. ix : int Index to convert. Returns ------- subix : tuple (i,j) """ k = 0 for i in range(n-...
def _rk4_(xy, f, t, dt, **kwargs): """Integrate one time step with RK4""" k1 = dt * f(t, xy, **kwargs) k2 = dt * f(t + 0.5*dt, xy + 0.5*k1, **kwargs) k3 = dt * f(t + 0.5*dt, xy + 0.5*k2, **kwargs) k4 = dt * f(t + 0.5*dt, xy + 0.5*k3, **kwargs) return xy + (k1 + k2 + k3 + k4)/6.
def tf(value): """ Wraps the value with Terraform interpolation syntax. Usage: "{{ 'module.example.arn' | tf }}" Output: "${module.example.arn}" """ return '${' + value + '}'
def get_matching_points(requested_file_names, all_file_names, object_points, image_points): """ Gets the object points and image points of a requested set of files :param requested_file_names: files to look through :param all_file_names: the list of file names :param object_points: the object p...
def is_watched_asn(parameters, asn): """Is this process responsible for the given AS ?""" if parameters["ases"] is not None: # if there is an ases file we check against it if asn in parameters["ases"]: return True else: # otherwise the AS are distributed between processe...
def update_config(config, update, merge=True): """ Update ``config`` directory keys from the ``update`` directory, if the same key is not present in ``config``. Else merge the value from two keys if ``merge`` key argument is set to ``True``. """ result = {} for key, value in config.item...
def build_cgi_environ(wsgi_environ, git_project_root, user=None): """Build a CGI environ from a WSGI environment: CONTENT_TYPE GIT_PROJECT_ROOT = directory containing bare repos PATH_INFO (if GIT_PROJECT_ROOT is set, otherwise PATH_TRANSLATED) QUERY_STRING REMOTE_USER REMOTE_ADDR REQUES...
def factorial(n): """Return factorial. Args: n (int): Argument (non-negative) Returns: Factorial of n """ assert type(n) == int and n >= 0 if n == 0: return 1 else: return n * factorial(n-1)
def str_to_microsec(str_1, str_2): """ Change the 2 provided numbers into a number of microseconds. Examples inputs: "100", "us" "2.76", "ms" "3", "s" """ if str_2 == "us": k = 1. elif str_2 == "ms": k = 1.e3 elif str_2 == "s": k = 1.e6 els...
def pad(coll, size, padding, left=True): """ Pad the collection `coll` with `padding` items until it reaches the length of `size`. By default do padding in the beginning (on the left side). """ padding_size = size - len(coll) if padding_size <= 0: return coll padding = [padding]...
def find_lcs(s1, s2): """find_lcs""" m = [[0 for i in range(len(s2) + 1)] for j in range(len(s1) + 1)] mmax = 0 p = 0 for i in range(len(s1)): for j in range(len(s2)): if s1[i] == s2[j]: m[i + 1][j + 1] = m[i][j] + 1 if m[i + 1][j + 1] > mmax: ...
def curate_list(input_list, words_list): """ :param input_list: :type input_list: :param words_list: :type words_list: :return: :rtype: """ final_list = [] for token in input_list: if len(token.strip()) == 0: continue if token.strip() in words_list: ...
def float_or_na(s): """ Convert string to float or NaN for "NA" for missing data Parameters: s - string representation of integer or "NA" Return value: integer value of s or NA_VALUE """ return float("NaN") if s == "NA" else float(s)
def list_duplicates_of(seq,item): """ Identifies the position of duplicated sequences """ start_at = -1 locs = [] while True: try: loc = seq.index(item,start_at+1) except ValueError: break else: locs.append(loc) start_at = l...
def get_average_score(tscores): """ Gets the average score for models cross validated using the cross_validate function. :param tscores: List. The scores to be averaged. Should be the result of a cross validation using the cross_validate function. """ score = 0 for i in range(0,len(tscores)): ...
def listAppend(list:list,appendObj): """Syntatic sugar. This appends the obj to the list and returns it.""" if appendObj: list.append(appendObj); return appendObj
def transfer_user_click(user_click): """ :param user_click:dict,key userid,value:[itemid1,itemid2] :return:dict,key itemid,value:[userid1,userid2] """ item_click_by_user = {} for user in user_click: item_list = user_click[user] for itemid in item_list: item_click_by_u...
def convertJsonToDictionary(data): """ :param data: this is a caching efficiency dictionary in a string format :return: the python dictionary format of the data string """ # this is the format of the output output = {"#Payloads": [], "#queries": [], "#uniqueQueries": [], "PayloadS...
def _build_archive_name(software, version, extension): """ Builds the name of an archive file for a software release. :param software: software to build archive file name for :type software: str :param version: release of software to build archive file name for :type version: str :param e...
def mean(arr): """Returns mean of arr""" return sum(arr) / len(arr) if arr else None
def number(string): """Helper function to convert strings to int or floats. Input: string Output: Int, Float or String if non convertable. """ try: return int(string) except TypeError: return float(string) except ValueError: return string
def rstrip_tuple(t: tuple): """Remove trailing zeroes in `t`.""" if not t or t[-1]: return t right = len(t) - 1 while right > 0 and t[right - 1] == 0: right -= 1 return t[:right]
def selective_title(str): """ Convert string to Title Case except for key initialisms Splits input string by space character, applies Title Case to each element except for ["NHS", "PCN", "CCG", "BNF", "std"], then joins elements back together with space Parameters ---------- str : str ...
def prob9(total=1000): """ A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a**2 + b**2 = c**2 For example, 3**2 + 4**2 = 9 + 16 = 25 = 5**2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. """ a = 1 whil...
def traverse_config_set(target, *args): """ >>> traverse_set({'level': {'one': 1}}, 'level', 'one', 42) {'level': {'one': 42}} """ # Seperate the path down from the value to set path, value = args[:-1], args[-1] current = target last = target for level in path: if not level i...
def first(targets, cat, kwargs): """A target chooser that simply picks the first from the given list This is the default, particularly for the case of only one element in the list """ targ = targets[0] if cat: s = cat[targ] if kwargs and targ in kwargs: s = kwargs.co...
def recup_centre(tuile_choisi, tuiles_zone_centre, tuile_permier_joueur): """ recuperer toute les tuiles de la zone centre de la meme couleur que la tuile choisie """ tuiles = [] nombre = tuiles_zone_centre.count(tuile_choisi) # corespond au nombre de tuile de la meme couleur que la t...
def bit_length(input): """ Return the bit length of input. EX: 7 (0b111) has length 3 EX: 8 (0b1000) has length 4 """ return len(bin(input)) - 2
def create_c1(dataset): """ Create a list of unique items in transaction data. Represent each item as a set of length 1. """ c = [] for data in dataset: for item in data: if not [item] in c: c.append([item]) c.sort() return list(map(frozense...
def bool_prop(name, node): """Boolean property""" try: return node.get(name) except KeyError: return None
def _init_weight(Dataset, d, method ="zero"): """initialization of weights Parameters : Dataset(Iterable):- data points with 1 at dth dimension and label at d+1th dimension d(int):- dimension of data points method(string):- method of initialisation with "zero" by default, "zero" giving z...
def inherit_fom_basemodel(model: dict): """Change the schema to inherit from _OpenAPIGenBaseModel.""" base = { 'allOf': [ { '$ref': '#/components/schemas/_OpenAPIGenBaseModel' }, { 'type': 'object', 'properties': {} } ] ...
def real_letter(character, key): """ Afla caracterul """ if character.isalpha(): character = ord(character)-key if character < ord('a'): character = ord('z') - abs(ord('a') - character) + 1 return chr(character) else: return character
def marriage_tag(context, source): """ Reformat your_marriage step Also show/hide optional questions """ show_all = False tags = [] first_column = '<tr><td width="75%" style="padding-right: 5%">' second_column = '</td><td width="25%">' end_tag = '</td></tr>' marriage_loc...
def flatten(l): """Flatten, like in ruby""" return flatten(l[0]) + (flatten(l[1:]) if len(l) > 1 else []) if type(l) is list else [l]
def t(a, b): """ @MG:reduce-on """ return a + b + 3
def mean_across_arrays(arrays): """ Computes elementwise mean across arrays. E.g. for input [[1, 2, 4], [5, 3, 6]] returns [3, 2.5, 5] :param arrays: list of arrays of the same length :return: elementwise average across arrays """ out_arr = [] n_arrays = len(arrays) # Iterate through...
def check_message_id_format(message_id): """Returns message id with < and > prepended and appended respectively Required format for exchangelib filter.""" message_id = message_id.strip() if not message_id.startswith("<"): message_id = f"<{message_id}" if not message_id.endswith(">"): ...
def ObjToString(obj, extra=' '): """ :param obj: :param extra: (Default value = ' ') """ if obj is None: return 'None' return str(obj.__class__) + '\n' + '\n'.join( (extra + (str(item) + ' = ' + (ObjToString(obj.__dict__[item], extra + ' '...
def primary_function(x1, y1, x2, y2): """ a = (y2- y1) / (x2 -x1) b = y1 - ax1 Return y = ax + b ---------- a: float b: float """ a = (y2 -y1) / ((x2 -x1)) b = y1 - a * x1 return [a, b]
def _median3(comparables, lo, mid, hi): """Sort the three elements of an array in ascending order in place and return the middle index Arguments: comparables -- an array of which the elements can be compared lo -- index 1 (inclusive) mid -- index 2 (inclusive) hi -- index 3 (inclus...
def convert_R_to_numpy_params(mu, theta): """ Convert mean/dispersion parameterization of a negative binomial to the ones numpy supports See https://en.wikipedia.org/wiki/Negative_binomial_distribution#Alternative_formulations From https://stackoverflow.com/a/47406400/2966723 """ r = the...
def stringToInt(message): """ Convert input string message into an integer """ string_to_binary = message.encode('utf8') return int.from_bytes(string_to_binary, byteorder='big', signed=False)
def group(pairs): """Given (key,value) pairs, return a table mapping each key to a list of all its values.""" table = {} for k, v in pairs: table.setdefault(k, []).append(v) return table
def cer(e): """ Canonicalize the representation of an undirected edge. Works by returning a sorted tuple. """ return tuple(sorted(e))
def get_E2K_subdict(the_dict, main_key, sub_key): """Returns the subdictionary specified by main_key and sub_key, returning an empty dictionary if any is missing. This is for use in the post-processing functions.""" return the_dict[main_key].get(sub_key, {}) \ if the_dict.get(main_key) else {}
def linear_search_recursive(array, item, index=0): """Time complexity: O(n) because you are returning the function continuously until index equals to nth-item """ if len(array) <= index: return index if array[index] == item: return index else: return linear_search_recursive...
def get_extra_couchdbs(config, couch_database_url, extra_db_names=()): """ Create a mapping from database prefix to database url :param config: list of database strings or tuples :param couch_database_url: main database url """ extra_dbs = {} postfixes = [] for row in conf...
def right_zero_pad(val,length=8): """ Right-zero-pad short-form angle strings with zeros. This reduces amount of error checking required, and makes decimal conversions more consistent This will also make the IOD/UK/RDE angle lengths uniform (UK/RDE adds one more digit of precision on the right) """ val = val.rst...
def get_non_lib(functions): """ Get all non-library functions @param functions: List of db_DataTypes.dbFunction objects @return: a subset list of db_DataTypes.dbFunction objects that are not library functions. """ return [f for f in functions if not f.is_lib_func]
def antiderE(V0,B0,B0pr,V): """ antiderivative of the Birch Murnaghan E(V) """ antider = (9*B0*V0*(-((-6 + B0pr)*V) - ((-4 + B0pr)*V0**2)/V + \ 3*(-14 + 3*B0pr)*V0*(V0/V)**(1/3) + \ 3*(-16 + 3*B0pr)*V*(V0/V)**(2/3)))/16 return antider
def Get_IOState_upstream(topo, begin_TM):#{{{ """ Get inside/outside state for the loop before the current TM helix Input: topo topology sequence of the protein begin_TM sequence position at the beginning of the TM helix (begin_TM, end_TM) defines the location o...
def _get_sensitive_attibutes(known_sensitive_features, features): """ Return sensitive attributes in appropriate format """ # Extract new names of sensitive attributes _sensitive_attributes = {} # it is a map because each entry contains all one-hot encoded variables for _column in features: ...
def _is_base_font(name): """ Used to filter out some special variants that we don't need """ MODIFIERS = ["Display", "Mono", "Slanted"] for m in MODIFIERS: if name.endswith(m): return False return True
def timestamp_to_day_timestamp(the_timestamp): """timestamp to day-timestamp Args: the_timestamp (int): the timestamp in sec Returns: int: day-timestamp """ the_block = the_timestamp // 86400 return the_block * 86400
def fib1(a,b,n): """Calculate the nth fibonacci number using the seeds a and b""" if n==1: return a elif n==2: return b else: return fib1(a,b,n-1)+fib1(a,b,n-2)
def split_host_port(host_port): """Return a tuple containing (host, port) of a string possibly containing both. If there is no port in host_port, the port will be None. Supports the following: - hostnames - ipv4 addresses - ipv6 addresses with or without ports. There is no validation ...
def is_sorted(items): """Return a boolean indicating whether given items are in sorted order. Running time: Worst case is O(n) Memory usage: O(1)""" for i in range(len(items)-1): if items[i] > items[i+1]: return False return True
def checke_do_reset(board_info): """.""" return board_info.get('upload.auto_reset', '') == 'true'
def params_to_lists(params): """Dictionaries are more convenient for storing and working with the parameters of the gaussians and lorentzians, but leastsq wants the initial parameters as a list (as far as I can tell...). This will take a list of dictionaries and convert it to a single list according to the ...
def calc_channel_current(E, sigma, A): """ Calculate channel current """ I = E * sigma * A return I
def match_nested_lists(l1, l2): """ Match nested lists term for term :param l1: first list :param l2: second list :return: True or False This differs from "match_lists_as_sets" in the sense that order is important. The lists in question can only contain other lists or objects for which == is a...
def sanitize_headers(headers): """Sanitize sensitive request headers for logging""" results = dict(headers) # Redact instead of remove Authorization header so that those # using Basic Auth can debug if needed if results.get('Authorization'): results['Authorization'] = '***redacted***' re...
def get_reversed_dictionary(dictionary, keys): """Return reveresed dictionary.""" return {dictionary.get(k): k for k in keys if dictionary.get(k)}
def string_compare_rule(mob_param_attributes, hmi_param_attributes): """Function checks presence of "minlength"="1" in HMI_API if "minlength" is omitted in MobileAPI. Should be checked only for "type"="String" """ attr = "minlength" if attr not in mob_param_attributes: if attr not in hmi...
def get_seq_middle(seq_length): """Returns relative index for the middle frame in sequence.""" half_offset = int((seq_length - 1) / 2) return seq_length - 1 - half_offset
def get_lgnd_labels(handles, labels, key): """Returns zipped handles and labels for which labels contains key.""" return [pair for pair in zip(handles, labels) if key in pair[1]]
def sum_node_list(node_list): """Custom sum function in order to avoid create redundant nodes in Python sum implementation.""" from operator import add from functools import reduce return reduce(add, node_list)
def scale(pot, scale_factor): """ Scale the potential by scaling factor :param pot: potential along a coordinate :type pot: dict[tuple(float)] = float :param scale_coeff: initial scaling coeffcient :type scael_coeff: float :param num_tors: number of torsions used in scaling ...
def no_disp_cpl(on=0): """Negar Acesso as Configuracoes de Video DESCRIPTION Esta opcao desabilita o icone no do Painel de Controle de Configuracao de Video, negando aos usuarios acesso a quaisquer configuracoes de video. COMPATIBILITY Todos. MODIFIED VALUES ...
def fib(n): """Fibonacci example function Args: n (int): integer Returns: int: n-th Fibonacci number """ assert n > 0 a, b = 1, 1 for i in range(n-1): a, b = b, a+b return a
def get_method_type(method): """ Returns either "graph_fn" OR "api" OR "other" depending on which method (and method name) is passed in. Args: method (callable): The actual method to analyze. Returns: Union[str,None]: "graph_fn", "api" or "other". None if method is not a callable. ...
def is_equivalent(seq1, seq2): """ Checks for existence of a bijection between input sequences seq1 and seq2. """ letters1 = set(seq1) letters2 = set(seq2) distinct_mappings = set(zip(seq1, seq2)) return (len(letters1) == len(letters2) == len(distinct_mappings) and len(seq1) == len(seq2))