content stringlengths 42 6.51k |
|---|
def check_b4_adding(word, gpat, ypat, unused):
"""
Return True if:
gpat indicates that a letter is green and a letter does not match
ypat indicates that a letter is yellow and this letter matches or
this letter is not found in the word
the letter matches a letter known to be ... |
def flatten(d: dict, parent_key: str = '', sep: str = '_'):
"""
Args:
d:
parent_key:
sep:
Returns:
"""
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, dict):
items.extend(flatten(v, new_... |
def ual_grouper_base(basename: str) -> str:
"""
Returns a string to use in LDAP queries that provide the Grouper
ismemberof stem organization that UA Libraries use for patron
management
Note that this only returns a string, it is not RFC 4512
compatible. See :func:`requiam.ldap_query.ual_ldap_q... |
def rotate_left(num: int, num_size: int, shift_bits: int) -> int:
"""
Rotate a number num of num_size bits by shift_bits bits. See
https://en.wikipedia.org/wiki/Bitwise_operation#Rotate_no_carry for more information.
:param num: the number to rotate
:param num_size: the size of the number in bits
... |
def get_table_data(soup, text, default=None):
# type (BeautifulSoup.tag, str) -> str
"""Attempts to retrieve text data from a table based on the header value"""
if default is None:
default = ""
if soup is None:
return default
if text is None:
return default
head = soup.fi... |
def to_yellow(string):
""" Converts a string to yellow color (8bit)
Returns:
str: the string in yellow color
"""
return f"\u001b[33m{string}\u001b[0m" |
def sphinx_encode(string: str) -> str:
"""Replace tilde, hyphen and single quotes with their LaTeX commands."""
return (
string.replace("~", "\\textasciitilde{}")
.replace("-", "\\sphinxhyphen{}")
.replace("'", "\\textquotesingle{}")
) |
def make_url_absolute(url):
"""
Make sure a URL starts with '/'.
"""
if not url.startswith('/'):
url = "/" + url
return url |
def is_bool(x):
"""Tests if something is a boolean."""
return isinstance(x, bool) |
def _format_addresses(addresses):
"""Return a formatted string of networks and IP addresses
:param addresses: a dict of network IP addresses
:rtype: a string of semi-colon separated networks and IP addresses
"""
if not addresses:
return ""
output = []
for (network, addr_list) in a... |
def _edges_to_adjacency_list(edges):
"""Convert a list of edges into an adjacency list."""
adj = {}
for i, j in edges:
if i in adj: # pragma: no cover
ni = adj[i]
else:
ni = adj[i] = set()
if j in adj:
nj = adj[j]
else:
nj = ad... |
def median(data):
"""Finding the median from a list"""
# 1. Sort the list
new_data = sorted(data)
if len(new_data) == 1:
print(new_data[0])
return new_data[0]
else:
# Odd
if len(new_data) % 2 == 1:
mid_value = len(new_data) // 2
print(new_data... |
def unpack(a_dict,states):
"""
For implementational simplicity, V, RPE, etc, values for all RL models
implemented here are stored in a dict, keyed on each state. This
function uses states to unpack that dict into a list whose order
matches states.
Note: Null (0) states are silently dropped.
"""
from copy i... |
def unsigned_to_varint(num: int) -> bytes:
"""
convert an unsigned int to varint.
"""
if num < 0 or num > 0xffffffffffffffff:
raise OverflowError(f"can't convert {num} to varint")
if num <= 0xfc:
return num.to_bytes(1, 'little')
elif num <= 0xffff:
return b'\xfd' + num.to... |
def propeller_efficiency(thrust, shaft_power, velocity):
"""Efficiency of converting shaft power to thrust power."""
return thrust * velocity / shaft_power |
def diffuse_ratio(DIFF_data, ghi_data):
"""Function calculates the Diffuse ratio.
Parameters
----------
DIFF_data : array_like
Diffuse horizontal irradiation data
ghi_data : array_like
global horizontal irradiation data array
Returns
-------
K : float
... |
def env2human(action):
"""Needs because Gym env would only work with 0,1,2 as states
but this is confusing as a human.
We have:
-1 == sell == 0 in env
0 == hold == 1 in env
1 == buy == 2 in env
Parameters
----------
int
Action that the environment accepts
Returns
-... |
def generate_config_expected_result(config) -> dict:
"""Generate expected result for config"""
expected_result = {
'visible': not config['ui_options']['invisible'],
'editable': not config['read_only'],
"content": config['default'],
}
if config['required'] and not config['default'... |
def wang_ryzin_reg(h, Xi, x):
"""
A version for the Wang-Ryzin kernel for nonparametric regression.
Suggested by Li and Racine in [1] ch.4
"""
return h ** abs(Xi - x) |
def are_configurations_equal(configuration1, configuration2, keys):
"""
Compare two configurations. They are considered equal if they hold the same values for all keys.
:param configuration1: the first configuration in the comparison
:param configuration2: the second configuration in the comparison
... |
def format_training_confs(config):
"""
Set up configs for training/validation.
"""
t_config = {
'batch_size' : config['train']['batch_size'],
'num_per_class' : config['train']['num_per_class'][0],
'num_classes' : config['train']['num_classes'],
... |
def x_win_condition(board_list):
"""Condition for 'X' to win."""
return (
(board_list[1] == "X" and board_list[2] == "X" and board_list[3] == "X")
or (board_list[1] == "X" and board_list[4] == "X" and board_list[7] == "X")
or (board_list[1] == "X" and board_list[5] == "X" and board_list[... |
def character_correction(sequences_list, min_length, null_character='N'):
"""
Some perturbation based interpretability methods (e.g. lime)
might introduce null characters which are not viable input.
These are by default replaced with 'N' (for any character).
The sequence is padded to min_length cha... |
def zero_separation(n):
"""Zero separation in normalized r based on radial order n."""
return 1 / n ** 2 |
def get_last_revision_in_list(revision_list):
"""Gets the last revision in list."""
return revision_list[-1] |
def get_rect_size_from_quad(quad):
"""
Find the size of quad image.
:param quad:
:return:
"""
sz_x = ((abs(quad[1][0] - quad[0][0]) + abs(quad[3][0] - quad[2][0])) / 2.)
sz_y = ((abs(quad[2][1] - quad[0][1]) + abs(quad[3][1] - quad[1][1])) / 2.)
return sz_x, sz_y |
def is_true(arg) -> bool:
"""Return if the given argument is a True value"""
if isinstance(arg, str):
return arg.lower() in ('yes', 'true', 'on', '1')
if isinstance(arg, bool):
return arg
if isinstance(arg, int):
return arg != 0
raise Exception(f'Invalid boolean arg: {arg}') |
def count_receive(link_counters):
"""Find the receive count in the counters returned by a CommunicationLink."""
for processor in link_counters.values():
if 'receive' in processor:
return processor['receive'] |
def newton_sqrt(x):
"""uses the Newton method to return square root"""
val = x
while True:
last = val
val = (val + x /val) * 0.5
if abs(val - last) < 1e-9:
break
return val |
def apply_overlay(base, overlay, append=False):
"""
:param base: Dict of yaml to apply changes to. Gets mutated
:param overlay: Dict of changes. Identical structure to base
:param append: True to append, false to replace values
:return: base dict with changes applied. Mutation of base input dict
... |
def count_neighbours(grid, row, col):
"""
Input: Three arguments. A grid as a tuple of tuples with integers (1/0),
a row number and column number for a cell as integers.
Output: How many neighbouring cells have chips as an integer.
"""
# using the hint: form a list of neighbor ru... |
def get_rtol(rtol=None):
"""Get default numerical threshold for regression test."""
return 1e-3 if rtol is None else rtol |
def copy_dict(source_dict, diffs):
"""Returns a copy of source_dict, updated with the new key-value pairs in diffs."""
result = dict(source_dict)
result.update(diffs)
return result |
def isEmulator(title):
"""Returns True if 'emulator' is written in stream title, False otherwise."""
return 'emulator' in title |
def remove_comments(css):
"""Remove all CSS comment blocks."""
iemac = False
preserve = False
comment_start = css.find("/*")
while comment_start >= 0:
# Preserve comments that look like `/*!...*/`.
# Slicing is used to make sure we don"t get an IndexError.
preserve = css... |
def _int2coord(x, y, dim):
"""Convert x, y values in dim x dim-grid coordinate system into lng, lat values.
Parameters:
x: int x value of point [0, dim); corresponds to longitude
y: int y value of point [0, dim); corresponds to latitude
dim: int Number of coding point... |
def find_sample_name_mismatch(rows):
"""
if we have eg. paired sample file names file1_1.fastq.gz and file1_2.fastq.gz
then both of these files must have the same sample name (probably file1)
This function returns any mismatches
"""
ret = list()
for (index, subindex), row in rows.items():
... |
def _get_elem_neighbors(p, nelx, nely, nelz):
"""
Gets the elements which surround the element in point p1 (no diagonals).
"""
eset = {}
if nelz == 0:
e1 = (max(p[0]-1, 0), p[1])
e2 = (min(p[0]+1, nely-1), p[1])
e3 = (p[0], max(p[1]-1, 0))
e4 = (p[0], min(p[1]+1, nel... |
def RemoveIntervalsContained( intervals ):
"""
remove intervals that are fully contained in another.
[(10, 100), (20, 50), (70, 120), (130, 200), (10, 50), (140, 210), (150, 200)]
results:
[(10, 100), (70, 120), (130, 200), (140, 210)]
"""
if not intervals:
return []
n... |
def _acct(v):
"""Convert to accounting (float) for excel, but default to original value."""
try:
if v is None or v == '':
return ''
return float(v)
except ValueError:
return v |
def ToUpperCamelCase(s):
"""Converts a string to upper camel case.
Examples:
foo => Foo
foo_bar => FooBar
foo__bar => FooBar
foo_bar_baz => FooBarBaz
Args:
s: The string to convert to upper camel case.
Returns:
The upper camel cased string.
"""
return ''.join(fragment.capitalize()... |
def gTabIni(a,n):
"""Genere un tableau de a avec n elements"""
tab=[]
for i in range (0,n) :
tab.append(a)
return(tab) |
def getType(obj):
""" generates string representation of class of obj
discarding decoration """
return str(obj.__class__).split("'")[1].split(".")[-1] |
def plural_helper(count, word_stem):
"""returns <count> <word_stem>[s]"""
result = str(count) + ' ' + word_stem
if count == 1:
return result
return result + 's' |
def get_inline_views_from_fieldsets(fieldsets):
"""Returns a list of field names from an admin fieldsets structure."""
inline_views = []
for _, opts in fieldsets or ():
if 'fieldsets' in opts:
inline_views += get_inline_views_from_fieldsets(opts.get('fieldsets'))
elif 'inline_vie... |
def filterResultList(phoneName,resultsList):
""" Takes in a phone name, and a list of PriceAPI search result dicitonaries. Returns a filtered list with
blacklisted sites removed (ebay, phone carries)
- only correct phones
- unlocked phones
- Price in USD
- has a set price
- returns None if th... |
def fromstringtolist(strlist, map_function=int):
"""
read list written as string
map_function function to convert from string to suited element type
"""
if strlist in ("None", None):
return None
list_elems = strlist[1:-1].split(",")
toreturn = [map_function(elem) for elem in list_... |
def generate_color_brew(n):
"""
Generates an evenly distributed range
of hue values in the HSV colour scale.
Arguments:
n -- an int with the number of hue values
you want to obtain
Returns:
a python list with those hue values
"""
hue_step = 360 / float(n)
return [color... |
def find_annotations(list_filenames, all_annotations):
"""
Function that generates list of annotations according to the given filenames
Input:
list_filenames: List of filenames, that shall be found in all_annotations
all_annotations: List of dictionaries, containing annotations for all imag... |
def union_probability(p_a, p_b, p_intersection):
"""Compute the probability of P(A or B) given P(A), P(B) and P(A and B)"""
return p_a + p_b - p_intersection |
def my_specificity_score(labels, predictions):
"""
specificity scoring function
:param labels: actual class labels
:param predictions: predicted class labels
:return: specificity score
"""
tp = fp = tn = fn = 0
for x in range(len(labels)):
if (predictions[x] == labels[... |
def _is_weblog_entry(pagename):
"""Return True if the page is a weblog entry."""
if not pagename:
return
parts = pagename.split("/")
if not parts[0] == "articles":
return
if not len(parts) == 5:
return
if parts[-1] == "index":
return
return True |
def owner_ids_to_string_array(owner_ids):
"""
Converts a snowflake owner id to an array of strings.
Parameters
----------
owner_ids : `iterable` of `int`
Snowflakes.
Returns
-------
owner_id_array : `list` of `str`
"""
return [str(owner_id) for owner_id in owner... |
def sortie(laby):
"""
Cette fonction calcule l'indice de la ligne et de la conne
de la liste de listes laby qui contiennent un 3.
S'il n'y en a pas, elle retourne None et affiche un message d'erreur
"""
for i in range(len(laby)):
for j in range(len(laby[i])):
if laby[i][j] ==... |
def fix_spans_due_to_empty_words(action_dict, words):
"""Return modified (action_dict, words)"""
def reduce_span_vals_gte(d, i):
for k, v in d.items():
if type(v) == dict:
reduce_span_vals_gte(v, i)
continue
try:
a, b = v
... |
def IsPsuedoClass(cmd, line):
"""Check the line to see if its a link pseudo class
@param cmd: current command
@param line: line of the command
@return: bool
"""
if cmd.endswith(u':'):
token = line.split()[-1]
pieces = token.split(u":")
if pieces[0] == 'a' or pieces[0].st... |
def osc(last, type):
"""
R=[AG], Y=[CT], K=[GT], M=[AC], S=[GC], W=[AT], and the four-fold
degenerate character N=[ATCG]
"""
if type == "r":
if last == "a": return("g")
if last == "g": return("a")
return("a")
if type == "y":
if last == "c": return("t")
if ... |
def _get_hash_key(f, args, kwargs):
"""
Return a hash for a given function invocation.
Args:
f: the function
args: positional arguments (list)
kwargs: keyword arguments (dict)
Returns:
a hashed key (int), or None if any argument was unhashable
"""
try:
... |
def short_type_constraint(value):
"""Test short_type constraint."""
if value >= -32768 and value <= 32767:
return True
return False |
def calculate_bootstrap(bootstrap_size, length):
"""
Calculate the bootstrap size for the data of given length.
Parameters
----------
bootstrap_size : int, float, default=None
Bootstrap size for training. Must be one of:
- int : Use `bootstrap_size`.
- float : Use `bootstrap_size * n_samples`.
- None :... |
def get_json_value(json_input, keys_path):
"""Returns the value specified in keys_path (using dot notation) or an empty string if the key doesn't exist"""
if not isinstance(json_input, dict):
return ""
keys = keys_path.split(".")
try:
value = json_input[keys[0]]
for key in keys[1... |
def flip_square(square, direction):
"""Flip square in left or up direction."""
if direction == 'left':
return [
row[::-1] for row in square
]
elif direction == 'up':
return [
row[:]
for row in square[::-1]
] |
def _unfold_continuations(code_string):
"""Removes any backslash line continuations from the code."""
return code_string.replace('\\\n', '') |
def _merge_config(new, base):
"""
Internal function that merges config sections into a base config.
Returns the new config data structure.
"""
# Thanks, StackOverflow! http://stackoverflow.com/questions/823196/yaml-merge-in-python
# If we've got two dicts, merge them.
if isinstanc... |
def timeout_check(value):
"""Check Timeout Argument.
Checks timeout for validity.
Keyword Arguments:
value -- Time in seconds to wait before timing out request.
Return Value:
Floating point number representing the time (in seconds) that should be
used for the timeout.
... |
def remove_cations(SMILES):
"""
Removes periodic table group 1 and 7 counterions from the SMILES
strings.
Args:
-----
SMILES (str) -- the SMILES string representation of the
molecule.
Returns:
--------
SMILES (str) -- the string representation of the molecul... |
def avg_real(scores):
""" Calculates the average for accuracy or std error without 'none'
Args:
- scores: list of accuracy or std errors
Returns:
- Avg if list is nonempty and real
Exception:
None
"""
scores_no_none = [x for x in scores if x != None]
if l... |
def deepupdate(original, update):
"""
Recursively update a dict.
Subdict's won't be overwritten but also updated.
"""
for key, value in original.items():
if key not in update:
update[key] = value
elif isinstance(value, dict):
deepupdate(value, update[key])
... |
def content_lines(lines):
"""Return lines with content."""
return [line for line in lines if line and line[0] == "#"] |
def find_instances(string, character):
"""
The function returns all the indices of the
character present in the string.
A list is returned
"""
return [index for index, letter in enumerate(string) if letter == character] |
def get_hyperion_device_id(server_id: str, instance: int) -> str:
"""Get an id for a Hyperion device/instance."""
return f"{server_id}_{instance}" |
def isstring(token):
"""
Determine if the token is a string.
:param token: The token.
:return: Boolean representing whether the token is a string.
"""
return token[0] == '"' and token[-1] == '"' |
def round_channels(num_channels: int, width_coeff: float=1,
divisor: int=8, min_depth=None) -> int:
"""Round number of filters based on width coefficient."""
if width_coeff == 1:
return num_channels
min_depth = min_depth or divisor
new_num_channels = max(min_depth, int(num_ch... |
def getContour(semitones):
""" Given a list of integers defining the size and direction of a series of musical intervals in semitones,
this function encodes the contour of the melody with Parsons code for musical contour where u=up, d=down, r=repeat.
"""
contour = ''
for p in semitones:
... |
def candidate_matches(o, sm_current, list_matches):
"""
checks if an article shares authors with all other articles in a list
:param o: examined article
:param sm_current: list of articles that share authors
:param list_matches: list of all combinations of two articles that share authors
:return... |
def inline(text):
"""
Convert all newline characters to HTML entities:
This can be used to prevent Hypertag from indenting lines of `text` when rendering parent nodes,
and to safely insert `text` inside <pre>, <textarea>, or similar elements.
"""
return text.replace('\n', ' ') |
def uniform_but_one_dataset_no_weight(n, p):
"""Generates an unweighted dataset according to the AMS lower bound.
Args:
n: The size of the dataset
p: The frequency moment (used as a parameter in the lower bound)
Returns:
A list of keys, where all keys appear once, except for one key which
accoun... |
def repeat(x, num_times, assert_at_least_one_rep=False):
"""
Repeat x num_times times.
Parameters
----------
x : tuple or Circuit
the operation sequence to repeat
num_times : int
the number of times to repeat x
assert_at_least_one_rep : bool, optional
if True, asse... |
def coordinates_within_board(n: int, x: int, y: int) -> bool:
"""Are the given coordinates inside the board?"""
return x < n and y < n and x >= 0 and y >= 0 |
def command_result_processor_parameter_unexpected(parameter_value):
"""
Command result message processor if a received parameter is unexpected.
Parameters
----------
parameter_value : `str`
Received parameter.
Returns
-------
message : `str`
"""
message_parts = ... |
def _SecondsToMicroseconds(secs):
"""Converts seconds to microseconds."""
return int(secs * 1000000) |
def compare_tags(tag_list, other_tag_list):
"""
compare two tags lists with the same tag format:
(`tag_name`, `start_offset`, `end_offset`, `value`)
"""
tags_amount = len(tag_list)
if tags_amount != len(other_tag_list):
print(
"missmatch of amount of tags %d vs %d" % (tags_... |
def is_inplace(argument: str) -> bool:
"""Check that argument is in-place value.
:param str argument: argument string
:return: True if argument is a digit of decimal else False
:rtype: bool
"""
checks = [
str.isdigit,
str.isdecimal,
]
return any(check(argument) for che... |
def is_number(s):
"""
Validate string as a number
"""
try:
float(s)
return True
except ValueError:
return False |
def sexpr_print_sexpr(sexpr):
"""Prints a python S-expression as a string S-expression."""
if isinstance(sexpr, str):
return sexpr
elif isinstance(sexpr, int):
return str(sexpr)
elif isinstance(sexpr, tuple):
assert len(sexpr) > 1, sexpr
parts = map(sexpr_print_sexpr, sex... |
def newman_conway(num):
""" Returns a list of the Newman Conway numbers for the given value.
Time Complexity: O(n)
Space Complexity: ?
"""
if(num == 0):
raise ValueError
memo = []
if (num == 1):
memo = [0, 1]
elif (num == 2):
memo = [0, 1, 1]
... |
def merge_sort(collection):
"""Pure implementation of the merge sort algorithm in Python
:param collection: some mutable ordered collection with heterogeneous
comparable items inside
:return: the same collection ordered by ascending
Examples:
>>> merge_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]... |
def _powerlaw(x0, y0, slope, x):
"""
From point (x0, y0) and parameter slope, returns y = f(x) such that:
> f(x) = a * (x ** slope)
> f(x0) = y0
Parameters
----------
x0, y0, slope, x : float
Returns
-------
y = f(x) : float
"""
return y0 * ((x/x0) ** slope) |
def expected_count(af, effective_mutation_rate):
"""Calculate the expected number of somatic variants
greater than a given allele frequency given an effective mutation
rate, according to the model of Williams et al. Nature
Genetics 2016"""
return effective_mutation_rate * (1.0 / af - 1.0) |
def _bool_encode(d):
"""
Converts boolean values to lowercase strings
"""
for k, v in d.items():
if isinstance(v, bool):
d[k] = str(v).lower()
return d |
def c2str(rgb):
""" Represent a color tuple as a string
Args:
rgb (:obj:`tuple` of int): Color representation as (R, G, B), where
each color is an integer between 0-255.
Returns:
string: Comma separated string with values for each channel
"""
return '%d,%d,%d' % rgb |
def scale_text(figsize, text_size):
"""Scale text to figsize."""
if text_size is None and figsize is not None:
if figsize[0] <= 11:
return 12
else:
return figsize[0]
else:
return text_size |
def penn2morphy(penntag):
""" Converts Penn Treebank tags to WordNet. """
morphy_tag = {'NN':'n', 'JJ':'a',
'VB':'v', 'RB':'r'}
try:
return morphy_tag[penntag[:2]]
except:
return 'n' |
def get_first(queryset, default=None):
""" Return first element of queryset or default """
if queryset:
return queryset[0]
return default |
def find_second_maximum(lst):
"""
finding two highest numbers and storing into a sorted list
return tail
assumption: all numbers are positive
"""
highest_nums = [float('-inf'), float('-inf')]
for i in lst:
if i > highest_nums[1]:
if i > highest_nums[0]:
... |
def inclusion_check(n_timepoints, mean_fd, max_fd, n_spikes, fd_th):
"""
Checking if participant is recommended to be excluded from analysis
based on motion parameters and spikes regressors.
Inputs
-------
n_timepoints: number of timepoints
mean_fd: mean framewise_displacement (FD)
max... |
def to_product(product_tuple):
"""Parse a tuple into valid 'Product' object.
Args:
product_tuple: Tuple containing StockCode, Description, Quantity and UnitPrice.
Returns:
Product (dictionary).
"""
return {
'id': str(product_tuple[0]),
'description': str(product_tup... |
def extract_episode_details(season, episode_response):
"""Clean and extract episode details response.
Take an episode details response and cleans the data.
Extract the relevant fields needed to construct an
Episode object.
Args:
season: The season number
episode_response: An episod... |
def pre_hash(s):
"""
Prepends a string with its length.
EXAMPLES::
sage: from sage.doctest.parsing import pre_hash
sage: pre_hash("abc")
'3:abc'
"""
return "%s:%s" % (len(s), s) |
def mapset(fn, the_set):
"""map a function across a set as though it were a list"""
ls = list(the_set)
print(ls)
reslist = map(fn, ls)
print(reslist)
resset = set(reslist)
print(resset)
return resset |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.