content stringlengths 42 6.51k |
|---|
def get_treatments(ladders):
"""Creates Array of all tretments listed in ladders
Args:
ladders ([Array]): [ladders]
Returns:
[Array]: [treatments]
"""
treatments = []
#treatments are saved in the second column
for ladder in ladders:
treatments.append(ladder[1])
... |
def component_masses_to_symmetric_mass_ratio(mass_1, mass_2):
"""
Convert the component masses of a binary to its symmetric mass ratio.
Parameters
----------
mass_1: float
Mass of the heavier object
mass_2: float
Mass of the lighter object
Return
------
symmetric_ma... |
def hamming_distance(list1, list2):
""" Calculate the Hamming distance between two lists """
# Make sure we're working with lists
# Sorry, no other iterables are permitted
assert isinstance(list1, list)
assert isinstance(list2, list)
dist = 0
# 'zip' is a Python builtin, documented at
... |
def map_node2name(node_list, node_id):
""" find name from node-id """
for node in node_list:
if node_id == node.id:
return node.name
return "none" |
def getANSIfgarray_for_ANSIcolor(ANSIcolor):
"""Return array of color codes to be used in composing an SGR escape
sequence. Using array form lets us compose multiple color updates without
putting out additional escapes"""
# We are using "256 color mode" which is available in xterm but not
# necessar... |
def _compute_breadcrumbs(path, show_hidden=False):
"""Returns a list of breadcrumb objects for a path."""
breadcrumbs = []
breadcrumbs.append(('[root]', '/'))
path_parts = path.split('/')[1:-1]
full_path = '/'
for part in path_parts:
full_path += part + "/"
url_append = ""
... |
def SignExtend(val, nbits):
""" Returns a sign-extended value for the input value.
val - value to be sign-extended
nbits - number precision: 8 - byte, 16 - word etc.
"""
sign_bit = 1 << (nbits - 1)
return (val & (sign_bit - 1)) - (val & sign_bit) |
def get_source_detail_hr(sources):
"""
Iterate over source details from response.
make comma-separated string from sources.
:param sources: source details from response.
:return: String of multiple source names.
"""
return ', '.join([source.get('name', '') for source in sources]) |
def normalise_encoding_name(option_name, encoding):
"""
>>> normalise_encoding_name('c_string_encoding', 'ascii')
'ascii'
>>> normalise_encoding_name('c_string_encoding', 'AsCIi')
'ascii'
>>> normalise_encoding_name('c_string_encoding', 'us-ascii')
'ascii'
>>> normalise_encoding_name('c_... |
def complement(dnaStrand):
"""
Complements the input DNA strand using Base pairing rules
Input:
dnaStrand DNA strand to be complemented
Output:
comp Complement of the input DNA strand
"""
complement = {"A":"T", "T":"A", "C":"G", "G":"C"}
comp = ""
for i in dnaStrand:
comp += complement[i]
return com... |
def report_list_modes(inlist):
"""Report the mode of an input list
Parameters
----------
counts : list
List of values that can be counted
Returns
-------
list
List of mode values. If there is a tie then the list length > 1
"""
counts = {i: inlist.count(i) for i in s... |
def edge_args(start, dest, direction, label):
"""
compute argument ordering for Edge constructor based on direction flag
@param direction str: 'i', 'o', or 'b' (in/out/bidir) relative to \a start
@param start str: name of starting node
@param start dest: name of destination node
"""
edge_ar... |
def to_id(s):
"""Covert text to ids."""
if s == "+": return 11
if s == "*": return 12
return int(s) + 1 |
def parse_ticket_labels(labels):
"""Get a generator that returns names of labels passed if any."""
if not labels:
return []
return (label.get("name") for label in labels) |
def _splitrange(a, b):
"""Split range with bounds a and b into two ranges at 0 and return two
tuples of numbers for use as startdepth and stopdepth arguments of
revancestors and revdescendants.
>>> _splitrange(-10, -5) # [-10:-5]
((5, 11), (None, None))
>>> _splitrange(5, 10) # [5:10]... |
def is_remove(change):
""" Boolean check if current change references a stateful resource """
return change['Action'] == 'Remove' |
def get_bits(value, start_index, end_index):
"""
Returns a set of bit in position from start_index till end_index indexes(inclusively) from byte value
:param value: integer positive value
:param start_index: the number of the first bit which will be included to result;
most-significant bit is consid... |
def composition_approxDP_static_hetero_basic(distance_is):
"""apply composition on `distance_is`, a list of individual distances
:param distance_is: a list of (epsilon, delta), or ndarray of shape [k, 2]
"""
epsilon_is, delta_is = zip(*distance_is)
return sum(epsilon_is), sum(delta_is) |
def filter_paired(list):
"""
require that both pairs are mapped in the sam file in order to remove the reads
"""
pairs = {}
filtered = []
for id in list:
read = id.rsplit('/')[0]
if read not in pairs:
pairs[read] = []
pairs[read].append(id)
for read in pairs:
ids = pairs[read]
if len(ids) == 2:
f... |
def display_label(f_class, catalog):
"""
Predict the flower name with the topk most likely classes using a trained deep learning model.
Parameters:
image_path - path name of the flower image
model - our NN model
topk - the K most probable classes
Returns:
probability and id of the to... |
def getLength(intervals):
"""return sum of intervals lengths.
>>> getLength([(10,20), (30,40)])
20
"""
return sum([x[1] - x[0] for x in intervals]) |
def _esc_quote(contents: str) -> str:
"""Escape string contents for DLNA search quoted values.
See ContentDirectory:v4, section 4.1.2.
"""
return contents.replace("\\", "\\\\").replace('"', '\\"') |
def version_array_to_string(version_array):
"""Given an array of numbers representing a version, such as [1,2,3], returns
a string representation of the version, such as \"1.2.3\" """
return ".".join(str(x) for x in version_array) |
def row_empty_spaces(row, puzzle):
"""
Takes a row number, and a sudoku puzzle as parameter
Returns a list of empty spaces in that row on the puzzle
"""
valid_spaces = []
for col in range(0, 9):
if puzzle[row][col] == -1:
valid_spaces.append([row, col])
i = 1
print("\... |
def makeExpectedFTypes(expectedPrecision,foundFType,foundFTypes=['real']):
"""A very application-specific mapping to construct an fType list
for expected. Make sure that if we're looking at complex that we
do not replicate real-real comparisons."""
retTypes = ['makeExpectedFType::ERROR']
if 'logica... |
def map_components(notsplit_packages, components):
"""
Returns a list of packages to install based on component names
This is done by checking if a component is in notsplit_packages,
if it is, we know we need to install 'ceph' instead of the
raw component name. Essentially, this component hasn't b... |
def gcd(x, y):
"""returns the Greatest Common Divisor of x and y"""
while y != 0:
(x, y) = (y, x % y)
return x |
def parse_input_arg(input_arg: str) -> str:
"""Parse input URI as a valid JSON Schema ref.
This tool accepts bare base URIs, without the JSON Pointer,
so these should be converted to a root pointer.
"""
if "#" not in input_arg:
return input_arg + "#/"
return input_arg |
def subset(target, lst):
"""use it or lose it - determines whether or not it is possible to create
target sum using the values in the list. Values in list can be positive,
negative or zero"""
if target == 0:
return True
if lst == []:
return False
# use-it ... |
def opt_arg(spec, property, default_value=None):
"""Get an optional property, possibly with a default value.
@param spec: the JSON-like filter spec (a dict)(
@param property: the property name to retrieve
@param default_value: the default value to return if not found
@returns: the value if found, ot... |
def unify_1_md(bins, edges):
"""Unify 1- and multidimensional bins and edges.
Return a tuple of *(bins, edges)*.
Bins and multidimensional *edges* return unchanged,
while one-dimensional *edges* are inserted into a list.
"""
if hasattr(edges[0], '__iter__'):
# if isinstance(edges[0], (lis... |
def compute_roc_auc(xs, ys, positiveYValue):
"""
Compute area under the receiver operating curve...
INPUTS:
xs : predicted floating point values for each sample
ys : truth for each sample
positiveYValue : y value to treat as positive
OUTPUT: computed AUC
"""
def trapezoid... |
def gcd(p, q):
"""Returns the greatest common divisor of p and q
>>> gcd(48, 180)
12
"""
# Iterateive Version is faster and uses much less stack space
while q != 0:
if p < q: (p,q) = (q,p)
(p,q) = (q, p % q)
return p |
def ci(_tuple):
""" Combine indices """
return "-".join([str(i) for i in _tuple]) |
def calculate_pcpu(utime, stime, uptime, start_time, hertz):
"""
Implement ps' algorithm to calculate the percentage cpu utilisation for a
process.::
unsigned long long total_time; /* jiffies used by this process */
unsigned pcpu = 0; /* scaled %cpu, 99 means 99% */
unsigned lon... |
def pathjoin(a, *p):
"""
Join two or more pathname components, inserting '/' as needed.
If any component is an absolute path, all previous path components
will be discarded.
.. note:: This function is the same as :func:`os.path.join` on
POSIX systems but is reproduced here so that Nodular c... |
def string_boolean(value):
"""Determines the boolean value for a specified string"""
if value.lower() in ("false", "f", "0", ""):
return False
else:
return True |
def bare_msg_type(msg_type):
"""
Compute the bare data type, e.g. for arrays, get the underlying array item type
:param msg_type: ROS msg type (e.g. 'std_msgs/String'), ``str``
:returns: base type, ``str``
"""
if msg_type is None:
return None
if '[' in msg_type:
return m... |
def switch(*pairs):
"""Helper function for cond-logic in a Python lambda expression.
Part of a poor-man's functional programming suite used to define several pfainspector commands as one-liners.
:type pairs: callables
:param pairs: sequence of predicate1, consequent1, predicate2, consequent2, ..., alt... |
def normalize_commit_message(commit_message):
"""
Return a tuple of title and body from the commit message
"""
split_commit_message = commit_message.split("\n")
title = split_commit_message[0]
body = "\n".join(split_commit_message[1:])
return title, body.lstrip("\n") |
def grid_challenge(grid):
"""Hackerrank Problem: https://www.hackerrank.com/challenges/grid-challenge/problem
Given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically, ascending.
Determine if the columns are also in ascending alphabetical order, top to bottom... |
def input_format(line):
""" Converts a string into a list of vertices and a point
Receive an entry with this format with
this format '1 1, 3 2, 1 4, 3 4 | 3 3 '
Returns:
list = [(1,1), (3,2), (1,4), (3,4)] and a point = (3,3)
:param line:
:return: list, point
"""
line_strip = li... |
def reported_br_params(path1, path2):
"""Return (SUBBRANCH_RPATH, SUBBRANCH_FULLPATH).
Parameters are either (OUTER_BRANCH_FULLPATH, SUBBRANCH_RPATH) or for
a first-level branch (SUBBRANCH_RPATH, None). 'FULLPATH' means relpath
from the repo root; 'RPATH' means relpath from the outer branch.
"""
i... |
def mod_inverse(x, p):
"""Given 0 <= x < p, and p prime, returns y such that x * y % p == 1.
>>> mod_inverse(2, 5)
3
>>> mod_inverse(3, 5)
2
>>> mod_inverse(3, 65537)
21846
"""
return pow(x, p - 2, p) |
def _best_streak(year_of_commits):
"""
Return our longest streak in days, given a yeare of commits.
"""
best = 0
streak = 0
for commits in year_of_commits:
if commits > 0:
streak += 1
if streak > best:
best = streak
else:
streak... |
def reverse(x):
"""
Reverse a vector.
"""
return x[::-1] |
def get_sentences(text):
""" (str) -> list
The function takes as input a string. It returns a list of strings
each representing one of the sentences from the input string.
>>> t = "No animal must ever kill any other animal. All animals are equal."
>>> get_sentences(t)
['No anim... |
def handler(event: dict, context: object) -> dict:
"""
Handler of AWS Lambda function.
:param event: event data
:type event: dict
:param context: runtime information of the AWS Lambda function
:type context: LambdaContext object
"""
response = {
'isBase64Encoded': False,
... |
def unpack_problem(message):
"""Extract the problem incorporated in 'message'"""
problem = message['problem']
return problem |
def dot(i1, i2):
""" Dot iterables """
return sum( [i1[i]*i2[i] for i in range( len(i1) )] ) |
def se_to_varcope(se):
"""Convert standard error values to sampling variance.
.. versionadded:: 0.0.3
Parameters
----------
se : array_like
Standard error of the sample parameter
Returns
-------
varcope : array_like
Sampling variance of the parameter
Notes
---... |
def split(str, sep):
"""
Provides a filter to interface with the string.split() method
"""
return str.split(sep) |
def guess_n_initial_points(params):
"""Guess a good value for n_initial_points given params."""
return max(len(params), min(len(params) * 2, 10)) |
def get_routable_nodes_and_ways(nodes, highways):
"""
Extracts the routable nodes and ways from all nodes and highways.
:param nodes: All nodes as a dict
:param highways: All ways that have the 'highway' tag as list
:return: routable_nodes (dict), routable_ways (dict)
"""
useful_nodes = dict... |
def create_layer(name, description, domain, techniques, version):
"""create a Layer"""
min_mappings = min(map(lambda t: t["score"], techniques)) if len(techniques) > 0 else 0
max_mappings = max(map(lambda t: t["score"], techniques)) if len(techniques) > 0 else 100
gradient = ["#ACD0E6", "#08336E"]
#... |
def outer2D(v1, v2):
"""Calculates the magnitude of the outer product of two 2D vectors, v1 and v2"""
return v1[0]*v2[1] - v1[1]*v2[0] |
def reset_advanced(n, k, decay_factor, algorithm):
"""Callback method for resetting advanced settings."""
return [k, decay_factor, algorithm] |
def normalize_ped_delay_hours(study_hours):
"""
Normalize the given ped delay study hours, converting them to `StudyHours` enum
values (see `bdit_flashcrow` codebase, in particular `@/lib/Constants`).
"""
study_hours_norm = study_hours.lower()
if study_hours_norm == 'pedestrian':
return 'SCHOOL'
if st... |
def replicaset_statuses(members_document, module):
"""
Return a list of the statuses
"""
statuses = []
for member in members_document:
statuses.append(members_document[member])
return statuses |
def to_bottom_right_memo(grid_size=3, off_limits=()):
"""Memoized path to bottom right of grid
:param grid_size Height and width of the grid
:return Path to origin
"""
def to_bottom_right_helper(m, n, moves):
"""Helper function to return path to bottom right of grid
:par... |
def file2tuple(thing):
"""
Function that converts a filename to a tuple of the type: (filename,string containing file)
Input can be filename or tuple with filename and content in a string.
"""
if isinstance(thing,tuple): # it is a tuple with filename and content in a string
filename,st... |
def p2f(x):
"""Conver percentage to float"""
return float(x.strip('%'))/100 |
def modulo_div(arg1, arg2):
""" (float, float) -> float
Modulo division of two numbers
Returns (arg1 % arg2)
"""
try:
return arg1 % arg2
except TypeError:
return 'Unsupported operation: {0} % {1} '.format(type(arg1), type(arg2))
except ZeroDivisionError as zero... |
def ranges_intersect(pair1, pair2, buffer=0):
""" true if ranges entersect by more than buffer """
return (pair1[0] < pair2[1] - buffer) and (pair1[1] > pair2[0] + buffer) |
def percentage(part, total):
""" Helper function to calculate percentage """
if total == 0:
return None
return round(100 * part / float(total), 2) |
def stringify(obj):
"""Helper method which converts any given object into a string."""
if isinstance(obj, list):
return ''.join(obj)
else:
return str(obj) |
def roman_to_int(s):
"""
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
:param s: string
:return: integer
"""
_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
prev, cur, res = 0, 0, 0
for i in s[::-1]:
cur = _dict[i]
... |
def plural(count=0):
"""Return plural extension for provided count."""
ret = 's'
if count == 1:
ret = ''
return ret |
def flatten(L):
"""Flatten an iterable of iterables into a single list."""
return [i for x in L for i in x] |
def GetPatchJobUriPath(project, patch_job):
"""Returns the URI path of an osconfig patch job."""
return '/'.join(['projects', project, 'patchJobs', patch_job]) |
def visible_len(value: str) -> int:
"""The number of visible characters in a string.
Use this to get the length of strings that contain escape sequences.
"""
return int(len(value) - value.count('\x1b') * 4.5 + value.count('\x1b[1')) |
def average_tweets_per_user(tweets, users_with_freq):
"""
Return the average number of tweets per user from a list of tweets.
:param tweets: the list of tweets.
:param users_with_freq: a Counter of usernames with the number of tweets in 'tweets' from each user.
:return: float. average number ... |
def validate_choice_arg(arg: dict, value: str) -> bool:
"""
Checks if the given value matches constraints of choice argument (from api.arg_type).
It checks if the given value is in the list of choices defined in argument type
:param arg: the choice argument
:param value: the value to validate
:... |
def quadKey_to_Bing_URL(quadKey, api_key):
"""Create a URL linking to a Bing tile server"""
tile_url = ("http://t0.tiles.virtualearth.net/tiles/a{}.jpeg?"
"g=854&mkt=en-US&token={}".format(quadKey, api_key))
#print "\nThe tile URL is: {}".format(tile_url)
return tile_url |
def dsfr_summary(items: list) -> dict:
"""
Returns a summary item. Takes a list as parameter, with the following structure:
items = [{ "link": "item1", "label": "First item title"}, {...}]
**Tag name**::
dsfr_summary
**Usage**::
{% dsfr_summary items %}
"""
return {"self": ... |
def hex_to_ip(s):
"""
'7f000001' => '127.0.0.1'"""
try:
ip = map(lambda n: s[n:n+2], range(0, len(s), 2))
ip = map(lambda n: int(n, 16), ip)
return '.'.join(map(str, ip))
except:
return '' |
def mean(data):
"""Calculate the mean of a list of numbers
Parameters:
*data*
a list of numbers whose mean to calculate
"""
return float(sum(data))/len(data) |
def leapyear(year):
"""
Returns 1 if the provided year is a leap year, 0 if the provided
year is not a leap year.
"""
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return 1
else:
return 0
else:
return 1
... |
def is_hcj(character):
"""Test if a single character is a HCJ character.
HCJ is defined as the U+313x to U+318x block, sans two non-assigned code
points.
"""
return 0x3131 <= ord(character) <= 0x318E and ord(character) != 0x3164 |
def RGBToInteger(r, g, b):
"""Packs three color channels and returns a 24-bit color."""
return r << 16 | g << 8 | b |
def get_port_from_environment_vars(env_name, pod_envs):
"""Return port based on pod environment variables"""
def get_prefix(string):
return str.join('_', string.split('_')[:-1])
prefix = get_prefix(env_name)
for (key, value) in pod_envs.items():
if key.split('_')[-1:][0].lower() == 'port... |
def and_sum (phrase):
"""Returns TRUE iff every element in <phrase> is TRUE"""
if len(phrase) > 0:
total = set(phrase[0])
else:
total = set()
for x in phrase:
total = total.intersection(x)
return total |
def ecl_valid(passport):
""" Check that ecl is valid
ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
:param passport: passport
:return: boolean
"""
return any(color == passport['ecl'] for color in ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']) |
def unique_ordered(values):
"""return a list of unique values in an input list, without changing order
(list(set(.)) would change order randomly).
"""
seen = set()
def check(value):
if value in seen:
return False
seen.add(value)
return True
return [v for v in values if check(v)] |
def search_for_item(project):
"""Generate a string search key for a project"""
elements = []
elements.append(project['attributes']['title'])
return u' '.join(elements) |
def naive_ascii_encode(string_to_encode: str, string_length: int) -> str:
"""
Encodes an ASCII string with naive ascii encoding. Where each byte of
the string is encoded into the ASCII code point and then combined
together into a string representation of that integer.
:param string_to_encode: The input string.
:... |
def dna_to_rna(seq):
"""(str) -> changed string
simple function to replace all T with U
"""
seq = seq.upper()
seq = seq.replace("T", "U")
return seq |
def comment_out(text, comment="#"):
"""
Comment out some text, using the specified comment character(s) at the
start of each line.
"""
text = text.strip()
result = ""
for line in text.split("\n"):
if line: result += comment+" "+line+"\n"
else: result += comment+"\n"
ret... |
def parse_signing_keys(raw):
"""Parse a raw file into a dictionary of fingerprint and keys
:param raw: the raw file
:return: dictionary mapping the fingerprints to a RSA key in pem format"""
assert raw is not None
lines = raw.split('\n')
count = 0
keys = {}
next_fingerprint = None
... |
def sequence_to_one_hot(sequence: list) -> list:
"""Gets a sequence of size L and creates a matrix of LX4 of one hot encoding of each vector.
Args:
sequence (list): a list of letters out of the alphabet 'acgt'
Returns:
One hot encoding of the sequence, such that a = [1,0,0,0] ,c = [0,1,0,0]... |
def convert_RGB_to_BGR(color):
"""Converts a RGB color to a BGR color.
:param color: The RGB color.
:type color: Tuple[int, int, int]
:return: The corresponding BGR color.
:rtype: Tuple[int, int, int]
"""
return color[2], color[1], color[0] |
def find_flat_segment(polygon):
"""
Find beginning and end indexes of a horizontal section in the polygon.
This section can be made of several consecutive segments.
If the polygon contains several flat sections, this function only identify the first one.
The polygon is a list of 2D coordinates (x, y... |
def sum_divisors(n):
"""
Returns a sum of numbers proper divisors.
For exsample sum_divisors(28) = 1 + 2 + 4 + 7 + 14 = 28
"""
s = 1
limit = int(n ** 0.5)
if limit ** 2 == n:
s += limit
limit -= 1
for i in range(2, limit + 1):
if n % i == 0:
s += (i + ... |
def get_collection_link(db_id, collection_id):
"""Create and return collection link based on values passed in"""
# Return a link to the relevant CosmosDB Container/Document Collection
return "dbs/" + db_id + "/colls/" + collection_id |
def reddening_correction_sf11(extinction_r):
""" Compute the reddening values using the SF11 correction set.
Parameters
----------
extinction_r : array
The uncorrected extinction values in the r-band.
Returns
-------
A_u : array
The corrected... |
def is_char(obj):
"""return True if obj is a char (str with lenth<=1)"""
return isinstance(obj, str) and len(obj) <= 1 |
def out(coords, dims):
""" This function recurses through the two lists, adjusting output
if a coord is too big or too small (compared to corresponding dim).
Eg. For coords = (1,3,-2) and dims=(2,2,2), (2,3,4) is returned. """
new_coords = []
for coord, dim in zip(coords, dims):
if coord > ... |
def set_value_rec(branch, keys, value):
"""
Recursivelly traverse `branch` until the end of `keys` is
reached and set the value.
:Parameters:
- `branch`: dictionary
- `keys`: a list of keys that define path to the key to be set
- `value`: a value to store
"""
if len(keys) == ... |
def generate_fullname_for(o):
"""Produce a fully qualified class name for the specified instance.
:param o: The instance to generate information from.
:return: A string providing the package.module information for the
instance.
"""
if not o:
return 'None'
module = o.__class__.__mod... |
def find_category_subsets(subgraph_collection):
"""Finds subgraphs which are subsets of other subgraphs to remove redundancy, when specified.
:param subgraph_collection: A dictionary of subgraph objects (keys: subgraph name, values: subgraph object).
:return: A dictionary relating which subgraph objects ar... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.