content stringlengths 42 6.51k |
|---|
def get_bottom_right_inner(sprites):
"""
:return: which sprite to use.
"""
return sprites["block"][0] |
def _clean_code(code):
"""
Cleans the received code (the parser does not like extra spaces not a VALUE
statement). Returns the cleaned code as a list of lines.
:param code: The COBOL code to clean
:return The list of code lines (cleaned)
"""
lines = []
# cleanup lines, the parser is ve... |
def get_float_list(range_max:int, div:int=100) -> list:
""" To get 0 -> 1, range_max must be same order of mag as div """
return [float(x)/div for x in range(int(range_max))] |
def extract_comp_reaction_ids(comp):
"""Extracts all reaction IDs from a MINE compound object."""
rxn_id_list = []
try:
rxn_id_list.extend(comp['Reactant_in'])
except KeyError:
pass
try:
rxn_id_list.extend(comp['Product_of'])
except KeyError:
pass
return rxn_i... |
def render_dashboard(category, tabs, prefix):
"""Renders a dashboard config string.
Follows this format:
{
name = 'dashboard_name'
dashboard_tab = [
tab('tab-name', 'test-group-name'),
...
]
}
"""
if '\'' in prefix:
raise ValueError(prefix)
... |
def build_signature(function_name, function_parameters):
"""
Given the function name and function parameters, returns the signature line
:param function_name: the name of the function
:param function_parameters: the parameters of the function (whatever would be
within the brackets)
"""
re... |
def remove_relationship(relationship_type: str, var: str = 'r') -> str:
"""
Returns a statement that removes a relationship.
Parameters
----------
relationship_type : str
The type of the relationship
var : str, optional
Name of the relationship when referred in another statement
Returns
-------
out: str
... |
def calculate_delta_x(a: float, b: float, n: int) -> float:
"""
Calculate delta_x = (b - a) / n
n = data points samples
a = min x value of integral
b = max x value of intergal
"""
return (b - a) / n |
def align8to32(bytes, width, mode):
"""
converts each scanline of data from 8 bit to 32 bit aligned
"""
bits_per_pixel = {"1": 1, "L": 8, "P": 8, "I;16": 16}[mode]
# calculate bytes per line and the extra padding if needed
bits_per_line = bits_per_pixel * width
full_bytes_per_line, remaini... |
def List_Combine_to_List(*listmember):
"""
Combine several "listmembers" to a list
"""
outlist=[]
for member in listmember:
outlist.append(member)
return outlist |
def new_seating_chart(size=22):
"""Create a new seating chart.
:param size: int - number if seats in the seating chart.
:return: dict - with number of seats specified, and placeholder values.
"""
return {number: None for number in range(1, size + 1)} |
def validate_float_or_None(s):
"""convert s to float, None or raise"""
# values directly from the rc file can only be strings,
# so we need to recognize the string "None" and convert
# it into the object. We will be case-sensitive here to
# avoid confusion between string values of 'none', which
... |
def comma_labels(x_list):
"""Change list of int to comma format."""
result = []
for x in x_list:
result.append(format(int(x), ','))
return(result) |
def istuple(n):
"""
Determines if it is a tuple or not.
Returns: Boolean
"""
return type(n) == tuple |
def get_indeces(a, b):
"""
Extracts the indices multiple items in a list.
Parameters:
a: list. where we want to get the index of the items.
b: list. the items we want to get index of.
#example:
x = ['SBS1', 'SBS2', 'SBS3', 'SBS5', 'SBS8', 'SBS13', 'SBS40']
y = ['SBS1... |
def kaldi_id_to_channel(example_id):
"""
>>> kaldi_id_to_channel('P28_S09_LIVING.R-0714562-0714764')
'R'
>>> kaldi_id_to_channel('P28_S09_LIVING.L-0714562-0714764')
'L'
>>> kaldi_id_to_channel('P05_S02_U02_KITCHEN.ENH-0007012-0007298')
'ENH'
>>> kaldi_id_to_channel('P09_S03_U01_NOLOCATIO... |
def create_list_versions(list_of_versions):
"""Create a list of package versions available in CodeArtifact repository"""
list_versions = [item['version'] for item in list_of_versions]
return list_versions |
def get_r( x, y = 0, z = 1):
"""Makes r (length between origin and point) for x, y, z
Returns r (scalar or array, depending on inputs)"""
r = ( x **2 + y **2 + z **2 ) **0.5
return r |
def capture_group(s):
"""
Places parentheses around s to form a capure group (a tagged piece of
a regular expression), unless it is already a capture group.
"""
return s if (s.startswith('(') and s.endswith(')')) else ('(%s)' % s) |
def metric_to_ips(d, min_depth, max_depth):
"""
Args:
d: metric depth [min_depth, max_depth]
min_dpeth: in meter
max_depth: in meter
Returns:
"""
# d = d.clamp(min_depth, max_depth)
return (max_depth * d - max_depth * min_depth) / ((max_depth - min_depth) * d) |
def _score_mers(mers: list, consensus: str) -> int:
"""
Calculate the score of a mer list
"""
result = 0
for mer in mers:
for i in range(len(mer)):
if mer[i] != consensus[i]:
result += 1
return result |
def is_testharness_output_passing(content_text):
"""Checks whether |content_text| is a passing testharness output.
Under a relatively loose/accepting definition of passing
testharness output, we consider any output with at least one
PASS result and no FAIL result (or TIMEOUT or NOTRUN).
"""
# L... |
def _get_ensemble_component_file_name(component_index: int) -> str:
"""Returns the file name for the checkpoint of the ith model in the ensemble."""
return f"ensemble_{component_index}" |
def get_header(header: str, sep: str) -> str:
"""
Gets a header in Sphinx rst format.
:param header: The text of the header
:param sep: The separator to use for creating the header
"""
line = header + "\n"
line += str(sep * len(header)) + "\n"
return line |
def drusen(drusize: int) -> int:
"""
DRSZWI: MAXIMUM DRUSEN W/I GRID
0=None
1=Quest
2=<C0 (63)
3=<C1 (125)
4=<C2 (250)
5=>=C2
8=CG
Returns:
0, 1, 2, 88
"""
drszwi = drusize
if 0 <= drszwi <= 2:
return 0
elif drszwi == ... |
def integer_to_bytestring(n):
"""Return the bytestring represented by an integer."""
bytes = []
while n > 0:
bytes.append(chr(n - ((n >> 8) << 8)))
n = n >> 8
return ''.join(bytes) |
def create_rules_dict(raw_rules_input: str) -> dict:
"""
Creates dict with rules from raw str input
:param raw_rules_input: String of written rules
:type raw_rules_input: str
:return: Dictionary with processed rules
:rtype: dict
"""
bag_rules: dict = {}
for rule_row in raw_rules_input.split(".\n"):
is_color... |
def strings_differ(string1: str, string2: str) -> bool:
"""Check whether two strings differ while avoiding timing attacks.
This function returns True if the given strings differ and False
if they are equal. It's careful not to leak information about *where*
they differ as a result of its running time,... |
def isBinary(inputString):
"""
Verify if a sequence is binary.
Args:
inputString: input string to be verified.
Returns:
boolean: true if the string is binary; false if it isn't.
Raises:
None
"""
binarydigits = ('01')
return all(char in binarydigits for char in... |
def partial(f, args):
"""
Arguments:
- `f`: a function of a value tuple to values
- `args`: a tuple of arguments for f, may contain None
elements
"""
if None in args:
return None
else:
return f(*args) |
def operation_mode(session, Type='Int32', RepCap='', AttrID=1250005, buffsize=0, action=['Get', '']):
"""[Operation Mode <int32>]
Sets/Gets how the function generator produces output. When set to Continuous mode, the waveform will be output continuously.
When set to Burst mode, the ConfigureBurstCount func... |
def is_image(file_path):
"""
Checks if the specified file is an image
:param file_path: Path to the candidate file
:return: Whether or not the file is an image
"""
return any(file_path.endswith(extension) for extension in [".png", ".jpg", ".jpeg"]) |
def get_data_file_names(config):
""" Used by the models to find the correct pickles for a specific fold. """
fold=config['fold']
return f'train_data_{fold}.pickle', f'pred_data_{fold}.pickle', f'raw_data_{fold}.pickle' |
def get_dlp_results_sql(project_id, dataset_id, table_id, min_count=0) -> str:
""" Generate sql to query the DLP results table: https://cloud.google.com/dlp/docs/querying-findings
and counts the number of finding in each field,info-type, likelihood grouping
:param project_id: Project Id
:param datas... |
def configureTrajectoryImage(dim, size=512, nTraj=100):
"""configureTrajectoryImage(dim, size=512, nTraj=100)
Configure trajectory images of the specified dim in meters and size in pixels."""
return { 'Trajectories' : dim, 'TrajSize' : 512, 'TrajCount':nTraj } |
def gen_type_help_txt(types: str, target: str = 'Input') -> str:
"""Generate a type help txt.
"""
return (f'Select type of {target} files from {types}'
'[Automatically detected by those extension]') |
def flattenlabel(label, order='C'):
"""
Flatten label in row-major order 'C' (default) or column-major order 'F'.
Code taken (but modified) from http://code.activestate.com/recipes/496807
"""
if order not in ('C', 'F'):
raise ValueError("order must be 'C' or 'F'")
label = list(... |
def print_dict(d, header=''):
"""
Formats a dictionary for printing.
:param d: Dictionary to print.
:return: String containing the formatted dictionary.
"""
obj_str = str(header) + '{ \n\t'
obj_str += "\n\t".join([str(key) + ": " + str(d[key]) for key in sorted(d.keys())])
obj_str += '\n... |
def check_uniqueness_in_rows(board: list):
"""
Check buildings of unique height in each row.
Return True if buildings in a row have unique length, False otherwise.
>>> check_uniqueness_in_rows(['***21**', '412453*', '423145*', '*543215',\
'*35214*', '*41532*', '*2*1***'])
True
>>> check_un... |
def w(a, cosmo):
"""
Effective equation of state parameter.
"""
return cosmo['w0'] + cosmo['wa'] * (1 - a) |
def BCD_calc(AM, POP):
"""
Calculate Bray-Curtis dissimilarity (BCD).
:param AM: Automatic/Manual
:type AM: int
:param POP: population or total number of samples
:type POP: int
:return: BCD as float
"""
try:
return abs(AM) / (2 * POP)
except (ZeroDivisionError, TypeError... |
def euler_step(u, t, f, dt):
"""
Returns the solution at the next time-step using Euler's
method.
Parameters
----------
u : array of float
solution at the previous time-step.
t : float
the time at the previous time-step.
f : function
function to compute the ... |
def StringLiteral(s):
"""Returns string literal for text."""
return '"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"' |
def filter_dupes(entries, id_comparator=None):
"""Partitions the list of entries into two lists: one containing uniques, and
one containing the duplicates.
entries = [(datetime(...), UUID()), (datetime(...), UUID())]
keys, dupes = filter_dupes(entries)
keys # => [UUID().bytes, UUID().byt... |
def default_hash_key_function(key, number_of_buckets, max_key_length=32):
"""Takes a key and returns a string value that represents the bucket identifier"""
raw_string_key = str(key)
string_key = raw_string_key[:min(max_key_length, len(raw_string_key))]
the_hash = 0
for character_index, character i... |
def optional_d(text):
"""Method for parsing an 'optional' integer."""
if text:
return int(text)
else:
return None |
def _anonymous_model_data(ops_data):
"""Returns a dict representing an anonymous model.
ops_data must be a dict representing the model operations. It will
be used unmodified for the model `operations` attribute.
"""
return {
"model": "",
"operations": ops_data
} |
def remove_non_deployable_operators(operators: list):
"""Removes operators that are not part of the deployment pipeline.
If the non-deployable operator is dependent on another operator, it will be
removed from that operator's dependency list.
Args:
operators (list): original pipeline operators.... |
def get_shortest_unique_filename(filename, filenames):
"""Get a representation of filename in a way that makes it look
unique compared to the other given filenames. The most unique part
of the path is used, and every directory in between that part and the
actual filename is represented with a slash.
... |
def _CompletionsFromArgs(fn_args):
"""Takes a list of fn args and returns a list of the fn's completion strings.
Args:
fn_args: A list of the args accepted by a function.
Returns:
A list of possible completion strings for that function.
"""
completions = []
for arg in fn_args:
arg = arg.replace... |
def check_json(data, name):
"""
Check if 'name' should be inhibited according to json 'data'
return bool True if the box should go down
"""
for force in data['force_desinhibit']:
if force in name:
return True
for inhib in data['inhibit']:
if inhib in name:
... |
def define_token(token):
"""Return the mandatory definition for a token to be used within cisp
where cisp expects a valid token.
For example, in order to use an identifier as a method name in a
cisp type, the definition generated by this function must be written
for that specific identi... |
def stringify(grid: dict, n: int) -> str:
"""Stringify with (0, 0) in the lower-left corner."""
rows = []
for y in reversed(range(n)):
row = []
for x in range(n):
value = grid.get((x, y), "-")
row.append(value)
rows.append(row)
return "\n".join("".join(r... |
def merge_dicts(a, b):
"""Return a new dict with items from two other dicts.
This function exists for backward compatibility to replace Python 3.9's a|b.
For performance reasons, there are no guarantees that a and b won't be modified.
"""
a.update(b)
return a |
def has_python_value(var, value):
"""Used during typing to check that variable var was resolved as Python type and has specific value"""
if not isinstance(var, type(value)):
return False
if value is None or isinstance(value, type(bool)):
return var is value
else:
return var == ... |
def is_assymetric_msg(msg_type: int) -> bool:
"""Check if an OpenFlow message type is assymetric."""
if msg_type in [1, 10, 11, 12]:
return True
return False |
def pad_extra_whitespace(string, pad):
"""Given a multiline string, add extra whitespaces to the front of every line."""
return '\n'.join(' ' * pad + line for line in string.split('\n')) |
def tl_to_string(exp):
""" Converts Python objects to a TL-readable string. """
if not isinstance(exp, list):
return str(exp)
return '(' + ' '.join(map(tl_to_string, exp)) + ')' |
def bmh_nic_names(bmh):
"""Returns the names of the interfaces of a BareMetalHost as a sorted
list."""
nics = bmh["status"]["hardware"]["nics"]
return sorted(set(nic["name"] for nic in nics)) |
def list_deployed_clusters(pipeline, actual_deployments):
"""Returns a list of clusters that a service is deployed to given
an input deploy pipeline and the actual deployments"""
deployed_clusters = []
# Get cluster.instance in the order in which they appear in deploy.yaml
for namespace in pipeline:... |
def _size_arg_converter(size_arg):
""" If it succeeds to interpret, then float is returned,
otherwise, ``None`` is returned.
"""
size_arg = size_arg.replace('"', '')
size_arg = size_arg.replace("'", '')
size_arg = size_arg.lower().replace("px", "")
size_arg = size_arg.lower().replace("pt", "... |
def string_interleave(s1, s2):
"""Interleave a character from a small string to a big string
Args:
s1 (str): input string
s2 (str): input string
Returns:
(str): the string that already interleave
Raises:
TypeError: if s1 or s2 is not a string
Examples:
>>> str... |
def split_operations_by_type(operations: list) -> tuple:
"""
Split input operations into sub-lists of each transformation type
the normalization operation is placed last to apply correctly the other operations
:param operations: list of pipeline operations
:return: tuple of lists of the operations ... |
def printSubsequence(Matrix, s1, s2, i, j, seq):
""" Returns the longest common subsequence of two strings
(2D-array, str, str, int, int, str) -> (str) """
if i == 0 or j == 0:
if seq == []: return None
return ''.join(seq[::-1])
# If inputs for s1, s2 are numbers uncomment below line... |
def create_buckets(lower_bound, upper_bound, bins):
"""
Create a dictionary with bins
:param lower_bound: low range
:param upper_bound: high range
:param bins: number of buckets
:return:
"""
range_value = (upper_bound - lower_bound) / bins
low = lower_bound
buckets = []
if ... |
def is_empty(elem):
"""This function is just a helper function for
check whether the passed elements (i.e. list)
is empty or not
Args:
elem (:obj:): Any structured data object (i.e. list).
Returns:
bool: True if element is empty or false if not.
"""
if not elem:
ret... |
def is_png(filename):
"""Checks whether the file is a .png.
Parameters:
-----------
filename:
string: The filename of the image.
Returns:
--------
Boolean:
"""
file_format = filename[::-1][:4:][::-1]
if file_format != ".png":
return False
return True |
def onroot_vd(t, y, ydot, solver):
"""
onroot function to just continue if time <28
"""
if t > 28:
return 1
return 0 |
def stringify_dict(datum):
"""
returns log data with all values as strings
"""
return dict((x, str(datum[x])) for x in datum) |
def is_binary_format(content, maxline=20):
"""
parse file header to judge the format is binary or not
:param content: file content in line list
:param maxline: maximum lines to parse
:return: binary format or not
"""
for lc in content[:maxline]:
if b'format' in lc:
... |
def de2hex(obj):
"""
Converts a decimal number into its hexadecimal representation
Parameters
----------
obj : int or str
A number in decimal format
Returns
-------
str
The hexadecimal representation of the input number
"""
return hex(int(obj))[2:].upper() |
def reflect(lower, current, modifier, upper):
"""
Reflects a projected position if it escapes the bounds.
"""
next, modified = current, modifier
if next + modified < lower:
modified *= -1
next = lower + (modified - next + lower)
elif upper < next + modified:
next = upper - (next + modified - up... |
def _find_match(needle: dict, haystack: list, keys: list):
"""Find a dictionary in a list of dictionary based on a set of keys"""
for item in haystack:
for key in keys:
if item.get(key) != needle[key]:
break
else:
return item
return None |
def _is_valid_glob(glob):
"""Check whether a glob pattern is valid.
It does so by making sure it has no dots (path separator) inside groups,
and that the grouping braces are not mismatched. This helps doing useless
(or worse, wrong) work on queries.
Args:
glob: Graphite glob pattern.
Re... |
def comment(*text):
"""
add comment
"""
return f"<!-- {''.join(text)} -->" |
def is_fin_st(id):
"""Used in p_one_line()
---
Checks if id begins with f or F or if or IF.
"""
return ( (id[0] in {'f','F'})
or ((len(id) > 1) and (id[0:2] == 'if' or id[0:2] == 'IF'))
) |
def _parse_url(time, inputnames, inputvalues, outputnames):
"""
Ensure that inputs has the right type
"""
data = {str(key):float(value)
for key, value in
zip(inputnames.split(','), inputvalues.split(','))}
data['time'] = float(time)
data['outputnames'] = outputnames.split... |
def applyF_filterG(L, f, g):
"""
Assumes L is a list of integers
Assume functions f and g are defined for you.
f takes in an integer, applies a function, returns another integer
g takes in an integer, applies a Boolean function,
returns either True or False
Mutates L such that, for ea... |
def solution(N):
"""
Write a function that returns an array containing the numbers from 1 to N,
where N is the parametered value. N will never be less than 1.
Replace certain values however if any of the following conditions are met:
- If the value is a multiple of 3: use the value 'Fizz' inste... |
def in_domain(x, y):
"""
Check if a point x, y is within the geometry domain
"""
l = 0.1
h = 0.04
xc = x - h
yc = y - h
check = True
if (x > l) or (x < 0.0) or (y > l) or (y < 0.0):
check = False
return check
if (xc > 0.0) & (yc > 0.0):
check = False
... |
def to_string(val):
""" Convert a value into a string, recursively for lists and tuples.
Args:
val: Value to convert value
Returns:
String representation of the value
"""
# Check tuple
if isinstance(val, tuple):
if len(val) == 1:
return "(" + to_string(val[0]... |
def one_or_all(mixed):
"""
Evaluate truth value of single bool or list of bools.
:param mixed: bool or list
:type mixed: bool or [bool]
:return: truth value
:rtype: bool
"""
if isinstance(mixed, bool):
return mixed
if isinstance(mixed, list):
return all(mixed) |
def camelcase_to_snake_case(_input):
""" Convert a camel case string to a snake case string: CamelCase -> camel_case
Args:
_input (str): The string to convert """
# https://codereview.stackexchange.com/a/185974
res = _input[0].lower()
for i, letter in enumerate(_input[1:], 1):
... |
def convert_list_elements_to_list(list_to_convert):
"""Converts list elements in list to sequence of elements:
Example: [1, 2, 3, [4, 5], 6, [7]] = [1, 2, 3, 4, 5, 6, 7]
"""
converted_list = []
for element in list_to_convert:
if isinstance(element, list):
converted_list.extend(element)
else:
... |
def bam_indexing(samtools, variable_name):
"""Will take a file variable name and a samtools path and will print
bash code that will create a bam index using samtools """
return ('# Indexing\n'
+ samtools + ' index $' + variable_name + ' >> $logFile 2>&1') |
def int_or_tuple_3d(value):
"""Converts `value` (int or tuple) to height, width for 3d ops.
This functions normalizes the input value by always returning a tuple.
Args:
value: A list of 3 ints, 5 ints, a single int or a tf.TensorShape.
Returns:
A list with 5 values.
Raises:
... |
def dt_controller(current_control_output_value, previous_control_output_value, derivative_gain_value):
"""Docstring here (what does the function do)"""
return (current_control_output_value - previous_control_output_value) * derivative_gain_value |
def get_tail(page, marker):
""" Returns page content after the marker """
if page is None: return None
if marker in page:
return page.split(marker,1)[1] |
def _ends_overlap(left, right):
"""Returns whether the left ends with one of the non-empty prefixes of the right"""
for i in range(1, min(len(left), len(right)) + 1):
if left.endswith(right[:i]):
return True
return False |
def bbox_horz_aligned(box1, box2):
"""
Returns true if the vertical center point of either span is within the
vertical range of the other
"""
if not (box1 and box2):
return False
# NEW: any overlap counts
# return box1.top <= box2.bottom and box2.top <= box1.bottom
box1_top = ... |
def get_link(text):
"""Turn a markdown headline in to a link, # Hello World -> hello-world"""
return ''.join(
i if i in '-_' or ('a' <= i <= 'z') else ''
for i in text.lower().replace(' ', '-')
) |
def get_crowd_selection_counts(input_id, task_runs_json_object):
"""
Figure out how many times the crowd selected each option
:param input_id: the id for a given task
:type input_id: int
:param task_runs_json_object: all of the input task_runs from json.load(open('task_run.json'))
:type task_r... |
def canonicalize(val):
"""
A helper function to convert all 'str' to 'bytes' in given value. The
values can either be a string or a list. We will recursively convert each
member of the list.
"""
if isinstance(val, list):
return [canonicalize(v) for v in val]
if isinstance(val, str):
... |
def forgiving_float(value):
"""Try to convert value to a float."""
try:
return float(value)
except (ValueError, TypeError):
return value |
def dual_cc_device_library_of(label):
"""
Given the label of a dual_cc_library, returns the label for the
on-device library. This library is private to the Bazel package that
defines it.
"""
return "{}_on_device_do_not_use_directly".format(label) |
def a_mystery_function_3(binary_string):
"""
binary_string is a string that is at least 4 characters long with 1s and 0s with the rightmost character representing the 0th bit
"""
binary_list=[int(i) for i in binary_string]
binary_list.reverse()
a,b,c=binary_list[0:3]
return (a or b or not c)... |
def zeroPad(data):
"""
ZeroPadding
"""
block_size = 8
while len(data) % block_size:
data += b"\0"
return data |
def pack(word, pattern):
"""Return a packed word given a spaced seed pattern.
>>> pack('actgac', [True, False, True, True, False, True])
'atgc'
"""
ret = []
for i, char in enumerate(word):
if pattern[i]:
ret.append(char)
return "".join(ret) |
def _str(item, encoding='utf-8', default=''):
"""_str
Args:
item (Any): item
encoding (str, optional): encoding
default (str, optional): default
Returns:
str: item in str type.
"""
if isinstance(item, bytes):
try:
result = item.decode(encoding)
... |
def kin2dyn(kin, density):
"""
Convert from kinematic to dynamic viscosity.
Parameters
----------
kin: ndarray, scalar
The kinematic viscosity of the lubricant.
density: ndarray, scalar
The density of the lubricant.
Returns
-------
dyn: ndarray, scalar
The ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.