content stringlengths 42 6.51k |
|---|
def command_parser(command):
"""Parse the input command and return type, and any accompanying arguments.
:param command: str, command input by the user.
:return: (str, tuple), command type, tuple of accompanying arguments.
"""
command = command.strip()
if command == "":
return 'EMPTY',... |
def author_name_check(agr_data, value):
"""
check a database reference has all authors with names, because <CollectiveName> in PubMed XML is not standard author pattern
:param agr_data:
:param value:
:return:
"""
if 'authors' not in agr_data:
return 'Failure: No authors found in da... |
def read_md_file(filepath):
"""Reads an md file and returns the string."""
with open(filepath, "r") as md_file:
md_file_str = md_file.read()
return md_file_str |
def linky_to_json(linky_dict, occurence):
"""
:param dictionnary
:return: json array
"""
json_body = [
{
"measurement": "Index_HC",
"tags": {
"Location": "Linky",
"Occurence": occurence
},
"fields": {
... |
def join_words(words, language="en"):
"""
Joins a list of words with a language specific separator. Because not all
languages use the standard English white-space between words.
:param words: List of words
:type words: List[str]
:param language: Language code
:type language: str
:retu... |
def exclude_from_weight_decay(name):
"""exclude_from_weight_decay
"""
if not isinstance(name, str):
name = name.name
if name.find("layer_norm") > -1:
return True
bias_suffix = ["_bias", "_b", ".b_0"]
for suffix in bias_suffix:
if name.endswith(suffix):
return ... |
def get_list_years(start_date=1985, end_date=2019):
"""
picks a random year
"""
return [i for i in range(start_date, end_date)] |
def get_major_domain(data, cutoff):
"""
Find the prevalent domain (for example, Eukaryota):
{'Eukaryota': 100.0, 'Other': 0.0, 'Viruses': 0.0, 'unclassified sequences': 0.0, 'Viroids': 0.0, 'Archaea': 0.0, 'Bacteria': 0.0}
"""
major_domain = 'Mixed'
maximum = max(data, key=data.get)
if data... |
def hex_to_RGB(hex):
"""'#FFFFFF' -> [255,255,255]"""
# Pass 16 to the integer function for change of base
return [int(hex[i:i+2], 16) for i in range(1,6,2)] |
def is_callable(x):
"""
this function identifies variables that are callable
"""
return hasattr(x, '__call__') |
def first(iterable, default=None):
""" Returns the first element of an iterable """
return next(iter(iterable), default) |
def process_wifi(strength: int) -> str:
"""Process wifi signal strength and return string for display."""
if strength >= 86:
return "Low"
if strength >= 71:
return "Medium"
if strength >= 56:
return "High"
return "Full" |
def self_multiply(tensor_tuple: tuple):
"""
:param tensor_tuple:
:return:
"""
tensor_list = list(tensor_tuple)
if None in tensor_list:
tensor_list.remove(None)
if len(tensor_list) == 0:
return 0
s = tensor_list[0]
for i in range(1, len(tensor_list)):
s *= ten... |
def top_stream(cache, user_id):
"""
Peek at the top of the stack in the cache.
:param cache: werkzeug BasicCache-like object
:param user_id: id of user, used as key in cache
:return: top item in user's cached stack, otherwise None
"""
if not user_id:
return None
stack = ca... |
def calc_PLF(PLR, PLFs):
"""
takes the part load ratio as an input and outputs the part load factor
coefficients taken from https://comnet.org/index.php/382-chillers and only includes water source electrical chillers
:param np.array PLR: part load ratio for each chiller
:return np.array PLF: part lo... |
def get_T_value(conf_level: float = 0.05, dof: int = 10):
"""
get value of T
two tail = 0.95:
:param conf_level:
:param dof:
:return:
"""
print(conf_level)
T_value = [
12.71, 4.303, 3.182, 2.776, 2.571, 2.447, 2.365, 2.306, 2.262, 2.228,
2.201, 2.179, 2.160, 2.145, ... |
def jaccard_similarity(a, b): # noqa
"""
Examples:
>>> jaccard_similarity('abc', 'cba')
1.0
>>> jaccard_similarity('abc', 'bcd')
0.5
Args:
a:
b:
"""
set_a = set(a)
set_b = set(b)
if not (set_a and set_b):
return 0.0
return 1.0 * ... |
def get_float(string):
"""Get float function"""
return float(string.replace('(', '').replace(')', '').replace(',', '')) |
def _chord(x, y0, y1):
"""
Compute the area of a triangle defined by the origin and two
points, (x,y0) and (x,y1). This is a signed area. If y1 > y0
then the area will be positive, otherwise it will be negative.
"""
return 0.5 * x * (y1 - y0) |
def convert_int_to_ip(int_ip):
"""Converts an integer type IP to its string form.
Args:
int_ip (int): IP to be converted.
"""
drive_ip1 = (int_ip >> 24) & 0x000000FF
drive_ip2 = (int_ip >> 16) & 0x000000FF
drive_ip3 = (int_ip >> 8) & 0x000000FF
drive_ip4 = int_ip & 0x000000FF
r... |
def _get_node_url(node_type: str, node_name: str) -> str:
"""Constructs the URL to documentation of the specified node.
Args:
node_type (str): One of input, model, dabble, draw or output.
node_name (str): Name of the node.
Returns:
(str): Full URL to the documentation of the specif... |
def filter_nan_str(x):
"""Filter 'nan' values as string from a list of strings"""
if not isinstance(x, list):
return x
else:
return [y for y in x if y.lower() != 'nan'] |
def parse_post_age(text):
""" map 'posted 10 days ago' => '10' """
if 'hours' in text:
return '1'
return ''.join(list(filter(lambda c: c.isdigit(), text))) |
def fontInfoPostscriptWindowsCharacterSetValidator(value):
"""
Version 2+.
"""
validValues = list(range(1, 21))
if value not in validValues:
return False
return True |
def default_format(x: float) -> str:
"""General format used for loss, hyper params, etc."""
return str(round(x, 6)) |
def retrieve_nuts3(url: str) -> str:
"""Prepare nuts3 code from url."""
return url.split('/')[-2] |
def add_outputs_from_dict(
api_current_dict: dict,
fields_to_keep: list
) -> dict:
"""
Filters a dict and keeps only the keys that appears in the given list
:param api_current_dict: the origin dict
:param fields_to_keep: the list which contains the wanted keys
:return: a dict based o... |
def axis_str2idx(a):
"""return index (0, 1, 2) for given axis designation (x, y, z)"""
d = {'x': 0, 'y': 1, 'z': 2}
return d[a.lower()[0]] |
def create_levels(levels, base_factory):
"""Create a multidimensional array
:param levels: An Iterable if ints representing the size of each level
:param base_factory: A nullary function that returns a new instance of the basic unit of the array
:return: Nested lists
"""
def loop(current, rest... |
def join(chars=()):
"""
Join a sequence of characters into a string.
:param bytes chars: list of chars
:rtype: str
"""
return b''.join(chars).decode() |
def part1(data):
"""Return the value at position 0 after running the program.
Put 12 and 2 in positions 1 and 2 before running. Instructions are:
1, x, y, r - add the values at positions x and y and put result at r
2, x, y, r - multiply instead of adding
99 - halt the execution
"""
... |
def counting_sort(input_array):
"""
input_array : the array that we want to sorted
working_storage : it is an array, and it will be used as a temporary working storage
sorted_array : it is a sorted array of the input_array in ascending order
"""
sorted_array = [0 for items in range(len(inp... |
def filter_nulls(field):
"""Filter out null field values, float conversion otherwise."""
if field is None or field == "<null>":
return 0
else:
try:
return float(field)
except ValueError:
return str(field) |
def check_for_output_match(output, test_suite):
"""Return bool list with a True item for each output matching expected output.
Return None if the functions suspects user tried to print something when
they should not have."""
output_lines = output.splitlines()
if len(output_lines) != len(test_s... |
def _avoid_duplicate_arrays(types):
"""Collapse arrays when we have multiple types.
"""
arrays = [t for t in types if isinstance(t, dict) and t["type"] == "array"]
others = [t for t in types if not (isinstance(t, dict) and t["type"] == "array")]
if arrays:
items = set([])
for t in ar... |
def link(url, name):
"""Print clickabl link on supporting terminals."""
return "\033]8;;{}\033\\{}\033]8;;\033\\".format(url, name) |
def exhaustCall(v):
"""
Get an uncallable value at the end of a call chain or `v` itself
if `v` is not callable.
"""
while callable(v):
v = v()
return v |
def clean_up_spacing(sentence):
"""Trim any leading or trailing whitespace from the sentence.
:param sentence: str - a sentence to clean of leading and trailing space characters.
:return: str - a sentence that has been cleaned of leading and trailing space characters.
"""
clean_sentence = sentence... |
def expand_suggested_responses(instrument, lookups, *responses):
"""
Maps any {'_suggested_response': pk} values to the SuggestedResponse by that id, as long as it
is present in the ``lookups`` dict.
"""
values = []
for response in responses:
data = response # Assume raw passthrough by... |
def listify(value, return_empty_list_if_none=True, convert_tuple_to_list=True):
"""
Ensures that the value is a list.
If it is not a list, it creates a new list with `value` as an item.
# Arguments
value: A list or something else.
empty_list_if_none: A boolean. When True (default), None... |
def flatten(items):
"""flatten(sequence) -> list
Returns a single, flat list which contains all elements retrieved
from the sequence and all recursively contained sub-sequences
(iterables).
Examples:
>>> [1, 2, [3,4], (5,6)]
[1, 2, [3, 4], (5, 6)]
>>> flatten([[[1,2,3], (42,None)], [4,... |
def find_adjacent_segment_type(segments, time):
"""Find boundary type on left and right (NONSPEECH or SPEECH)"""
# find previous segment type
segments[0].append("NS")
prev = segments[0]
for i in range(1, len(segments)):
if (segments[i][0] - time) > prev[2]:
segments[i].append("NS... |
def calculate_TB(salt, ver="Uppstrom"):
""" Calculate Total Borate """
if "upp" in ver.lower():
return 0.0004157 * salt / 35 # in mol/kg-SW
else:
# Lee, Kim, Byrne, Millero, Feely, Yong-Ming Liu. 2010.
# Geochimica Et Cosmochimica Acta 74 (6): 1801?1811.
return 0.0004326 * salt / ... |
def result_fixture(controller_state, uuid4):
"""Return a server result message."""
return {
"type": "result",
"success": True,
"result": {"state": controller_state},
"messageId": uuid4,
} |
def buildConnectionString(params):
"""Build a connection string from a dictionary
Returns string.
"""
return ";".join(["%s=%s" % (k, v) for k, v in params.items()]) |
def equalize_array(array):
""" Expand all rows of 2D array to the same lenght """
if len(array) == 0:
return array
max_length = max(map(len, array))
for row in array:
diff = max_length - len(row)
row.extend([""] * diff)
return array |
def remove_string_float(string):
"""Pandas dataframe returns integers sometimes as floats. This function
takes a string and removes the unnecessary .0 if the next character is
a tab or new line.
Args:
string: tsv file in string format
Return:
string: string with float removed
... |
def deserialize(node):
""" Returns an ast instance from an expanded dict. """
if isinstance(node, tuple):
klass, kws = node
return klass(**deserialize(kws))
elif isinstance(node, dict):
d = {}
for k, v in node.items():
d[k] = deserialize(v)
return d
el... |
def relative_class_counts(data):
"""input: a dict mapping class keys to their absolute counts
output: a dict mapping class keys to their relative counts"""
counts_items = sum(data.values())
return {k: 1.0 * v / counts_items for k, v in data.items()} |
def cut_text(value):
""" returns a cut string if its length is greater than 50 chars """
return value if len(value) <= 50 else f"{value[:50]}..." |
def split_id(image_id):
"""
Split Earth Engine image ID into collection and index components.
Parameters
----------
image_id: str
Earth engine image ID.
Returns
-------
tuple
A tuple of strings: (collection name, image index).
"""
index = image_id.split("/... |
def transaction_data_list_create(in_trans_list):
""" Returns tuple of transaction data
Parameters:
in_trans_list (List<str>): List of strings describing transactions
Returns:
(List<tuple>): List of tuples containing transaction data in the
following fo... |
def mergesort(unsorted_list):
"""Sort by dividing up halves and re-merging."""
def _merge(left_half, right_half):
"""Re-merge."""
merged = []
left_index, right_index = 0, 0
while left_index < len(left_half) and right_index < len(right_half):
if left_half[left_index] <... |
def hash_string(s: str) -> bool:
"""
Verify the manifest hash string for a given artifact.
:param s: str
:return: bool
"""
# ToDo: verify hash pattern
return type(s) is str |
def update_dict(d, e, copy=True):
"""
Returns a new dictionary updated by another dictionary.
Examples
--------
Consider a dictionary `d` which we want to update:
>>> d = {0: 'a', 1: 'b'}
Now consider the dictionary for update:
>>> e = {1: 'c', 2: 'd'}
We can update the `d` as follows:... |
def tcl_escape(s):
"""Escape the given string as appropriate for using in a Tcl string
and regex.
"""
return s.replace("[", "\\[").replace("]", "\\]") \
.replace("$", "\\$") \
.replace("?", "\\?") \
.replace("(", "\\(").replace(")", "\\)") |
def get_base_to_base_mapping_from_aligned_pairs(reftuple, qLen, strand):
"""
Returns: dict of 0-based position --> 0-based ref position
"""
cur_genome_loc = reftuple[0][1]
mapping = {}
for qpos, rpos in reftuple:
if qpos is not None and rpos is not None:
mapping[qpos] = (rpo... |
def _parse_resource_name(name):
"""Extracts project id, location, dataset id etc from the resource name."""
parts = name.split('/')
return parts[1], parts[3], parts[5], parts[7], \
"%s/%s" % (parts[9], parts[10]) |
def modinv(a, n):
"""Extended Euclid's algorithm.
See http://www.finetune.co.jp/~lyuka/technote/mod_calc/exeuclid.html
"""
s = a
t = n
u = 1
v = 0
while s > 0:
q = t // s
w = t - q*s
t = s
s = w
w = v - q*u
v = u
u = w
return ... |
def generation(x, g):
"""return how many generations have passed"""
return int(x/g) |
def _control_input(devices, control_inputs, idx):
"""Returns the `idx`-th item in control_inputs to be used in ops.control_dependencies.
This is a helper function for building collective ops.
Args:
devices: a list of device strings the collective run on.
control_inputs: a list or None.
idx: the inde... |
def paginated_list(full_list, sort_key, max_results, next_token):
"""
Returns a tuple containing a slice of the full list starting at next_token and ending with at most the max_results
number of elements, and the new next_token which can be passed back in for the next segment of the full list.
"""
i... |
def hasFunction(object_, methodname):
""" Test if class of ``object`` has a method called ``methodname``. """
method = getattr(object_, methodname, None)
return callable(method) |
def _is_freezeable(obj):
"""Determine if obj has the same freezing interface as `PostgreSQLTestUtil`.
For some reason isinstance doesn't work properly with fixtures, so checking
``isinstance(obj, PostgreSQLTestDB)`` will always fail. Instead, we check to
see if obj.time.freeze()/unfreeze() are present,... |
def isstring(args, quoted=False):
"""Checks if value is a (quoted) string."""
isquoted = lambda c: c[0]==c[-1] and c[0] in ['"', "'"]
if quoted:
check = lambda c: isinstance(c, str) and isquoted(c)
else:
check = lambda c: isinstance(c, str)
if isinstance(args, list):
return... |
def delete(node):
"""
Simply copy the data from the next node over to the current node.
"""
if not node or not node.next:
return False
node.cargo = node.next.cargo
node.next = node.next.next
return True |
def has_data(d, fullname):
"""Test if any of the `keys` of the `d` dictionary starts with `fullname`.
"""
fullname = r'%s-' % (fullname, )
for k in d:
if not k.startswith(fullname):
continue
return True
return False |
def _rescale_score_by_abs(score: float, max_score: float,
min_score: float) -> float:
"""
Normalizes an attribution score to the range [0., 1.], where a score
score of 0. is mapped to 0.5.
:param score: An attribution score
:param max_score: The maximum possible attributio... |
def merge_dicts(*dict_args):
"""Given any number of dicts, shallow copy and merge into a new dict,
precedence goes to key value pairs in latter dicts.
Source: Aaron Hall, https://stackoverflow.com/questions/38987/how-to-merge-two-dictionaries-in-a-single-expression
"""
result = {}
for dictionary... |
def parse_params(func, params, models):
"""
get spec for (query, headers, cookies)
"""
if hasattr(func, 'query'):
query = models[func.query]
for name, schema in query['properties'].items():
params.append({
'name': name,
'in': 'query',
... |
def sec_to_hms(duration):
"""
Return hours, minutes and seconds for given duration.
>>> sec_to_hms('80')
(0, 1, 20)
:param int|str duration: Duration in seconds. Can be int or string.
:return: tuple (hours, minutes, seconds)
:rtype: (int, int, int)
"""
s = int(duration)
h = s /... |
def compute_ngrams(tokens, max_len = None, min_len = 1):
"""
tokens : iterable of string
a single sentence of tokens. Assumes start and stop tokens omitted
max_len : int
maximum ngram length
min_len : int
minimum ngram length
"""
... |
def lj_potential(rdist, eps, sig):
""" Calculate potential energy value of two interacting bodies
assuming a 12-6 Lennard-Jones potential.
:param rdist: distance between two interacting bodies (Bohr)
:type rdist: float
:param eps: Lennard-Jones epsilon parameter for interaction (_)
... |
def deault_parse_function(data: bytes,intro: str) -> bytes:
"""Default parse function. Print the data and who sent them and return it unchanged."""
print("[Received from {intro}]: {data}\n\n".format(intro=intro,data=str(data)))
return data |
def tostr(value):
"""
to use in for python 3 replacing python 2 str
:param value:
:return:
"""
try:
encoded = value.encode('utf-8').decode('utf-8')
except ValueError:
encoded = ""
return encoded |
def calc_raid_partition_sectors(psize, start):
"""Calculates end sector and converts start and end sectors including
the unit of measure, compatible with parted.
:param psize: size of the raid partition
:param start: start sector of the raid partion in integer format
:return: start and end sector i... |
def neighbors(X, Y, x, y):
"""
returns a list of tuples containing all neighbor
of the vertex (x, y)
Arguments:
X {int} -- number of vertices in X direction
Y {int} -- number of vertices in Y direction
x {int} -- x position of vertex 0 <= x <= X
y {int} -- y pos... |
def find(x, condition, n=1):
""" Return the index of the nth element that fulfills the condition.
"""
search_n = 1
for i in range(len(x)):
if condition(x[i]):
if search_n == n:
return i
search_n += 1
return -1 |
def PreOpL2(op, items):
"""
Uses algorithm from SecureSCM WP9 deliverable.
op must be a binary function that outputs a new register
"""
k = len(items)
half = k // 2
output = list(items)
if k == 0:
return []
u = [op(items[2 * i], items[2 * i + 1]) for i in range(ha... |
def get_bounding_box(points, padding=0):
"""Returns the bounding box of a list of points with the given padding"""
x_coords = list(map(lambda x: x[0], points))
y_coords = list(map(lambda x: x[1], points))
return (
min(x_coords) - padding,
min(y_coords) - padding,
max(x_coords) + ... |
def and_(*args):
"""Build AND filters."""
return {
'operator': 'and',
'criteria': list(args)
} |
def b_overlap_a(a, b, alab='a', blab='b', verb=False):
"""Check if there is any value in `b` inside the range of the values in `a`.
Only checks if there is overlap, but not how (e.g. do not tell if all values of b inside a, or just from one side).
Examples
--------
a: ' |---| '
b: '|--... |
def respuesta(res):
""" Funcion para formatear la respuesta. """
return "iguales" if res else "diferentes" |
def _radix_length(num: int) -> int:
"""Finds the number of digits for a number"""
if num == 0:
return 1
digits: int = 0
while num != 0:
digits += 1
num = int(num / 10)
return digits |
def size_node(node):
"""
Check the size of this node
:param node: the node to check
:return: 0 if the node is None, the size of the node otherwise
"""
if node is None:
return 0
return node.size |
def find_mode(iid, modes):
""" Called by get_display_info() below """
for mode in modes:
if iid == mode.id:
return "{}x{}".format(mode.width, mode.height) |
def connected_nodes(node, neighbor_func, visited=None):
""" Recursively build up a set of nodes connected to `node` by following
neighbors as given by `neighbor_func(node)`, i.e. "flood fill."
>>> def neighbor_func(node):
... return {-node, min(node+1, 5)}
>>> connected_nodes(0, neighbor_func)... |
def evaluate_ids(new_data, old_data, id_field_key):
"""Summary
Args:
new_data (TYPE): Description
old_data (TYPE): Description
id_field_key (TYPE): Description
Returns:
TYPE: Description
"""
# each key in existing_ids is a record id in the admin table
# ... |
def chr_dict(d):
"""
Input dictionary generated by file2dict to make a new dict:
chromosome as key name, positions as value list.
"""
new_dict = {}
for k in d:
chromosome, pos = tuple(k.split('_'))
if chromosome in new_dict:
new_dict[chromosome].append(int(pos))
... |
def nested(path, query, *args, **kwargs):
"""
Creates a nested query for use with nested documents
Keyword arguments such as score_mode and others can be added.
"""
nested = {
"path": path,
"query": query
}
nested.update(kwargs)
return {
"nested": nested
} |
def parse_csv_import(data_string):
"""Parse CSV data into an array.
Parameters
----------
data_string : str
entries should be delimited by \n, values by comma
Returns
-------
data_array : list
rows are taken from \n delimiters, columns from commas
"""
data_array = d... |
def run(dfs: dict, settings: dict) -> dict:
"""list(df)"""
print()
print("\n".join(list(dfs[settings['df']])))
print()
return dfs |
def mit_bubble(L):
"""Bubble sort
Parameters:
L (list): Unsorted (eventually sorted) list.
swap (boolean): Whether list's elements have been swapped before, serves as an indicator on whether we're done sorting.
Returns:
L (list): Sorted list.
"""
swap = False
while not swap... |
def _odd_vertices_of_MST(M, number_of_nodes):
"""
Returns the vertices having Odd degree in the Minimum Spanning Tree(MST).
"""
odd_vertices = [0 for i in range(number_of_nodes)]
for u, v, _ in M:
odd_vertices[u] = odd_vertices[u] + 1
odd_vertices[v] = odd_vertices[v] + 1
odd_ver... |
def homogenize(vectors, w=1.0):
"""Homogenise a list of vectors.
Parameters
----------
vectors : list
A list of vectors.
w : float, optional
Homogenisation parameter.
Defaults to ``1.0``.
Returns
-------
list
Homogenised vectors.
Notes
-----
... |
def PNT2TidalOcto_Pv16(XA,beta0PNT=0,beta1PNT=0):
""" TaylorT2 1PN Octopolar Tidal Coefficient, v^16 Phasing Term.
XA = mass fraction of object
beta0PNT = 0PN Octopole Tidal Flux coefficient
beta1PNT = 1PN Octopole Tidal Flux coefficient """
XATo2nd = XA*XA
XATo3rd = XATo2nd*XA
XATo4th = XATo3rd*XA
... |
def _try_lookup(table, value, default = ""):
""" try to get a string from the lookup table, return "" instead of key
error
"""
try:
string = table[ value ]
except KeyError:
string = default
return string |
def render_if_string_starts_with_a_number(string):
"""
add "_" to the start if name starts with a digit
:type string: str
:rtype: str
"""
if string[0].isdigit():
string = "num" + string
return string |
def taputil_create_sorted_dict_key(dictionaryObject):
"""Searches for the specified keyword
Parameters
----------
dictionaryObject : dictionary object, mandatory
Dictionary
Returns
-------
A keyword based on a sorted dictionary key+value items
"""
if dictionaryObject is Non... |
def relative_config_file_path(file_name):
"""Return the config file path based on the environment.
Args:
file_name (str): The file name to be loaded.
Returns:
str: The config file path.
"""
return f"/configs/{file_name}" |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.