content stringlengths 42 6.51k |
|---|
def iso2sec(iso: str) -> int:
"""
:param iso: e.g. 1:01:02
:return: sec in int
"""
arr = iso.split(':')
len_arr = len(arr)
if len_arr <= 3:
arr = ['0'] * (3 - len_arr) + arr
else:
raise Exception('len_arr > 3, arr: {}'.format(arr))
return int(arr[0]) * 60 * 60 + int(... |
def is_prime(n):
"""https://stackoverflow.com/questions/15285534/isprime-function-for-python-language"""
if n == 2 or n == 3: return True
if n < 2 or n%2 == 0: return False
if n < 9: return True
if n%3 == 0: return False
r = int(n**0.5)
f = 5
while f <= r:
if n%f == 0: return Fal... |
def createRasterPreset(steps, step_size, step_rest):
"""Creates a raster for one axis each
Parameters:
steps (int): amount of parts the axis shall be split into
steps_size (int): average pixel size for each step
step_rest (int): amount of pixels which are added in last iteration
Returns:
l... |
def COLOR1(obj):
"""Format an object into string of highlight color 1 (blue) in console.
Args:
obj: the object to be formatted.
Returns:
None
"""
return '\x1b[1;34m' + str(obj) + '\x1b[0m' |
def country(country_code):
"""Extract the country from the given variable."""
return country_code[0:country_code.find('-')] |
def email_is_valid(email):
"""
Check if email is a valid one
"""
at_pos = email.find("@")
dot_pos = email.find(".")
if at_pos == -1 or dot_pos == -1 or dot_pos == len(email) - 1 or dot_pos == at_pos + 1: #Various email format checks
return False
return True |
def check_config_inputs(arg):
"""
Checks that all the data that should be numerical from that config
can be represented as a float.
Parameters
__________
arg: unknown
any argument can be passed.
Returns
_______
is_number: Boolean
Value is True if the arg is a number... |
def ShortBranchName(branch):
"""Convert a name like 'refs/heads/foo' to just 'foo'."""
return branch.replace('refs/heads/', '') |
def sample_lines():
"""Get some sample lines."""
return [
"This is some lines.",
"yeah quite a few.",
"In real life this would be real messages.",
"From real discord users.",
"But for privacy reasons.",
"And testing purposes.",
"We must instead provide som... |
def drop_mid_name(name):
"""Get only first and last name of a student."""
names = name.split(" ")
if len(names) == 3:
return names[0] + ' ' + names[2]
else:
return names[0] + ' ' + names[1] |
def capwords(s, sep=None):
"""capwords(s [,sep]) -> string
Split the argument into words using split, capitalize each
word using capitalize, and join the capitalized words using
join. If the optional second argument sep is absent or None,
runs of whitespace characters are replaced by a single space... |
def RPL_ENDOFINFO(sender, receipient, message):
""" Reply Code 374 """
return "<" + sender + ">: " + message |
def calculate_config_dist(tpa, tpb):
"""Trivially calculate the distance of two configs"""
ca, cb = tpa[0], tpb[0]
num_diff_field = 0
for k in ca:
if ca[k] != cb[k]:
num_diff_field += 1
return num_diff_field |
def is_list_of_str(value):
"""
Check if an object is a list of strings
:param value:
:return:
"""
return bool(value) and isinstance(value, list) and all(isinstance(elem, str) for elem in value) |
def parse_row(row, fields):
"""Returns a well-formed JSON row containing interesting fields."""
output = {}
for index, field_name in fields.items():
output[field_name] = row[index]
return output |
def numericrange_to_tuple(r):
"""Helper method to normalize NumericRange into a tuple."""
if r is None:
return (None, None)
lower = r.lower
upper = r.upper
if lower and not r.lower_inc:
lower += 1
if upper and not r.upper_inc:
upper -= 1
return lower, upper |
def doubleclick(x, y, bombs, cal, flags):
"""
Shortcut for double click
:param x: horizontal position of the click
:param y: vertical position of the click
:param bombs: list of bomb positions
:param cal: number of the position
:param flags: list of flags
:return: status after the double... |
def sort_012(input_arr):
"""
Given an input array consisting on only 0, 1, and 2, sort the array in a single traversal.
Time Complexity O(n)
Space Complexity O(n)
Where n is the array size.
Args:
input_arr(array): Array to be sorted
Returns:
sorted_arr(array): ... |
def parse_service_uri(uri):
"""Parses a service uri into separate provider, service, and catalog_id strings.
Examples:
usgs-nwis:dv/0800345522
gebco-bathymetry
usgs-ned:1-arc-second
Args:
uri (string): A string that is a uri.
Returns:
Three strings are returned... |
def get_underlying_address_objects(address_group_name, name_to_address_groups, name_to_addresses):
"""
address_group_entry: An AddressGroup object
name_to_address_groups: Dict of name -> AddressGroup objects
name_to_addresses: Dict of name -> Address objects
Return: A list of Address objects
"""... |
def drop_empty_lists(X):
"""
:param X: a data matrix: a list wrapping a list of strings, with each sublist being a sentence.
:return:
>>> drop_empty_lists([['Catullus'], [], [], ['I.', 'ad', 'Cornelium'], ['Cui', 'dono', 'lepidum', 'novum', 'libellum', 'arida', 'modo', 'pumice', 'expolitum', '?']])
... |
def flatten(lst):
"""
Flatterns a list of lists one level.
:param lst: list of lists
:return: list
"""
return [subelem for elem in lst for subelem in elem] |
def find_b(p, a):
"""Calculates b for p and a combo using derived formula. False if b not
int"""
num = p*p/2 - a*p
den = p - a
if num % den:
return False
return num // den |
def as_request_params(**kwargs):
"""Coerce kwargs into a tuple of param=value strings."""
return tuple('{0}={1}'.format(k, v) for k, v in kwargs.items()) |
def parse_response_browse_node(browse_nodes_response_list):
"""
The function parses Browse Nodes Response and creates a dict of BrowseNodeID to AmazonBrowseNode object
params
*browse_nodes_response_list*
List of BrowseNodes in GetBrowseNodes response
return
Dict of BrowseNo... |
def syncsafe(num, size):
""" Given a number, sync safe it """
result = ''
for i in range(0,size):
x = (num >> (i*7)) & 0x7f
result = chr(x) + result
return result |
def dedup_and_title_case_names(names):
"""Should return a list of names, each name appears only once"""
return list(set(name.title() for name in names)) |
def token_accuracy(references, hypotheses, level="word"):
"""
Compute the accuracy of hypothesis tokens: correct tokens / all tokens
Tokens are correct if they appear in the same position in the reference.
:param hypotheses: list of hypotheses (strings)
:param references: list of references (string... |
def linha_para_inteiro(l):
"""
Devolve um inteiro correspondente a linha da posicao inserida.
:param l: string, linha da posicao.
:return: int, valor correspondente da linha.
"""
l_int = {'1': 0, '2': 1, '3': 2}
return l_int[l] |
def gmof(res, sigma):
"""
Geman-McClure error function
- residual
- sigma scaling factor
"""
x_squared = res ** 2
sigma_squared = sigma ** 2
return (sigma_squared * x_squared) / (sigma_squared + x_squared) |
def get_by_path(dic, path):
"""
Gets a key (can be specified via dot notation) `path` in a nested `dic`
"""
parts = path.split('.')
loc = dic
for part in parts:
if part not in loc:
raise KeyError(path)
loc = loc[part]
return loc |
def is_country(feat_code: str):
"""Test a feature code"""
if not feat_code: return False
return feat_code.startswith("PCL") |
def get_page_filename(page_path: str) -> str:
"""Generate a filename for a rendered page."""
return f'{page_path}.j2' |
def get_first_key(dictionary, value):
"""Get first key in a dict for a given value.
:param dict dictionary:
:param string value:
"""
for key, val in dictionary.items():
if val == value:
return key
return None |
def elapsed_time(seconds, suffixes=['y','w','d','h','m','s'], add_s=False, separator=' '):
"""
Takes an amount of seconds and turns it into a human-readable amount of time.
"""
# the formatted time string to be returned
time = []
# the pieces of time to iterate over (days, hours, minutes, etc)
... |
def f(z):
"""
A function of metallicity Z, which we will set as the yield of Sr from
core-collapse supernovae.
"""
return 3.5e-8 * (z / 0.014) |
def calc_D_lin(c, D_params):
"""
Estimates diffusivity [m^2/s] as a function of the concentration c [kg/m^3]
assuming a linear model (parameters obtained from lin_fit_D_c()).
"""
a, b = D_params
return a*c + b |
def encrypt_this(text: str) -> str:
"""
Encrypts each word in the message using the following rules:
* The first letter needs to be converted to its ASCII code.
* The second letter needs to be switched with the last letter
Keepin' it simple: There are no special characters in input.
:param t... |
def _fmt(key, value, template='s'):
""" Helper SQL formatting to feed parametrized queries
Examples:
>>> _fmt('name', 'yogi')
"name = %(name)s"
>>> _fmt('id >=', 10)
"id >= %(id)s"
"""
ctypes = {tuple: 'in'}
if ' ' in key:
key, compare = key.split()
else:... |
def valid_book_definition(name, author, year, pages):
"""Does some simple checks on whether the provided arguments are sufficient to defien a book"""
if len(name) <= 1:
return False
if len(author) <= 1:
return False
try:
int(pages)
int(year)
except ValueError:
... |
def _parseTCP(factory, port, interface="", backlog=50):
"""
Internal parser function for L{_parseServer} to convert the string
arguments for a TCP(IPv4) stream endpoint into the structured arguments.
@param factory: the protocol factory being parsed, or C{None}. (This was a
leftover argument f... |
def valid_if_true(value, should_raise=True):
"""Validator that passes if the input is True."""
if value is True:
return True
if should_raise:
raise TypeError
return False |
def fib_recursion(n):
"""Many duplicated calculation"""
if n < 3:
return 1
else:
print("{0} = {1} + {2}".format(n, n - 1, n - 2))
return fib_recursion(n - 1) + fib_recursion(n - 2) |
def underscored2camel_case(v):
"""Converts ott_id to ottId."""
vlist = v.split('_')
c = []
for n, el in enumerate(vlist):
if el:
if n == 0:
c.append(el)
else:
c.extend([el[0].upper(), el[1:]])
return ''.join(c) |
def fspath(path):
"""Return the path representation of a path-like object.
If str or bytes is passed in, it is returned unchanged. Otherwise the
os.PathLike interface is used to get the path representation. If the
path representation is not str or bytes, TypeError is raised. If the
provided path is... |
def poly3(x,C0,C1,C2,C3):
"""
Calculate a polynomial function of degree 3 with a single variable 'x'.
Parameters
----------
x : numeric
Input variable.
C0, C1, C2, C3 : numeric
Polynomial coefficients
Returns
-------
numeric
Result of the polynomial func... |
def foldr(function, list, initial):
"""
given a function, a list, and an initial accumulator,
fold (reduce) each item into the accumulator from the right using function(item, accumulator)
"""
for item in list[::-1]:
initial = function(item, initial)
return initial |
def create_mapnik_config(layer_name, file_path, layer_srs):
""" Creates a mapnik config file
file_path is the absolute path to
the geotiff file """
return """
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Map[]>
<Map srs="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.... |
def flipXYZ(oldXYZ): # This is an example of a nice Modular function.
"""Flip XY coords of an XYZ coordinate string
This function inverts the xy coordinates generated by afni's
3dclust command. It is presently acting as a sort of Helper
function to getClusterStats function defined below.
Params:... |
def friendly_display(port,begin=8000):
"""for wehn you want some 'web' ports"""
ret= port-begin
if ret < 0 or port<0:
raise ValueError('neg values')
return ret |
def getcomplementaryUnitsPairs(refined):
"""This function ensures that two units do not share the
same functionality and planarity. At the next stage,
further complementarity based on complementarity of
building units and directionality needs to be encoded."""
complementarypairs = []
for i, uni... |
def _ring_port(ring_path, node):
"""Determine correct port from relation settings for a given ring file."""
for name in ['account', 'object', 'container']:
if name in ring_path:
return node[('{}_port'.format(name))] |
def __get_average_kor__(qars: list, department):
"""
Get the average of kor in the department passed as parameter in the benin republic area
"""
_all = list(filter(lambda c: c.department == department, qars))
total = 0
count = len(_all)
if count == 0:
count = 1
for i, x in enume... |
def wrapLines ( docin, maxlen = 100 ):
"""
Break line length to a max while retaining the leading spaces...
"""
import textwrap as textwrap
import string
docout = []
for line in docin:
prefix=0
for c in line:
if c in string.whitespace:
prefix += 1
... |
def feature_vectorizer(features, term_voc):
"""
Produces vector of features
term_voc : core.TermVocabulary
returns: dict
vector {index1: value1, ..., indexN: valueN}
"""
vector = {}
for feature_name in features.keys():
if not term_voc.contains(feature_name):
term... |
def mergeDictionaries(inputDict, separator=';'):
"""This function merges two or more dictionaries whereas values from different sources for the same key are combined by indicating the provenance.
For example sourceA = {'a': 'val1'} and sourceB = {'a': 'val2'} will be merged into {'a': 'val1 (sourceA)\nval2 (sour... |
def _apply_directives(stream, directives, ctxt, vars):
"""Apply the given directives to the stream.
:param stream: the stream the directives should be applied to
:param directives: the list of directives to apply
:param ctxt: the `Context`
:param vars: additional variables that should be availa... |
def cp(container, destination, tarname):
"""
The Docker people said they would not implement "cp" at the API level.
So, we live with what we have.
"""
try:
with open(tarname, 'rb') as tarhandle:
container.put_archive(destination, tarhandle)
return(True)
except Excepti... |
def params_for(prefix, kwargs):
"""Extract parameters that belong to a given sklearn module prefix from
``kwargs``. This is useful to obtain parameters that belong to a
submodule.
Example usage
-------------
>>> kwargs = {'encoder__a': 3, 'encoder__b': 4, 'decoder__a': 5}
>>> params_for('en... |
def symop_string_from_symop_matrix_tr(matrix, tr=(0, 0, 0), eps=0):
"""
Construct a CIF representation of symmetry operator plus translation.
See International Tables for Crystallography Vol. A. (2002) for
definition.
:param matrix: 3x3 matrix, representing the symmetry operator
:param tr: tran... |
def isCCW(ring):
"""
Determines if a LinearRing is oriented counter-clockwise or not
"""
area = 0.0
for i in range(0,len(ring)-1):
p1 = ring[i]
p2 = ring[i+1]
area += (p1[1] * p2[0]) - (p1[0] * p2[1])
if area > 0:
return False
else:
retur... |
def is_int(INTEGER_MAYBE):
""" Checks if a string could be expressed as an integer. '"""
try:
int(INTEGER_MAYBE)
return True
except ValueError:
return False |
def convert_bytes(tot_bytes):
"""Convert bytes to human-readable GB, MB, KB.
Parameters
----------
tot_bytes : int
Total number of bytes.
Returns
-------
[GB, MB, KB, rem] : list
Bytes divided into Giga, Mega, Kilo bytes.
"""
GB, rem = divmod(tot_bytes, 1024 * 1024 ... |
def _f(fpath, dir_):
"""Helper function for contains_parent_dir function."""
return fpath.startswith(dir_) |
def process_results_datasud(results, nb_results):
"""
Input: results: results from datasud
nb_results: number of results to use
Output: List of results formatted to reranking API
"""
formatted_results = []
results = results["result"]["results"][0:nb_results]
for result in re... |
def vanderpol(y,t,mu):
""" Return the derivative vector for the van der Pol equations."""
y1= y[0]
y2= y[1]
dy1=y2
dy2=mu*(1-y1**2)*y2-y1
return [dy1, dy2] |
def color565(r, g, b):
"""Convert red, green, blue components to a 16-bit 565 RGB value. Components
should be values 0 to 255.
"""
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3) |
def _evaluate_params(params_name: list, params_index: list, config_params_available: bool):
"""
This ensures that (1) if parameter names are provided in the config file, one and only one of
the arguments, `params_name` or `params_index`, might be given, and (2) if parameter names are
NOT provided in the... |
def _to_shape(shape):
"""convert shape into tuple."""
return tuple(int(sh) for sh in shape) |
def quadratic_easeout(pos):
"""
Easing function for animations: Quadratic Ease Out.
"""
return -(pos * (pos - 2)) |
def mkdown_blockquote(text):
"""
Generates the markdown syntax for a blockquote.
"""
return '\n'.join([f'> {line.strip()}' for line in text.splitlines()]) |
def bin2hex(bin_bytes):
"""
Converts a binary string to a string of space-separated hexadecimal bytes.
"""
return ' '.join('%02x' % ord(c) for c in bin_bytes) |
def pad_sentences(sentences, padding_word="<PAD/>", maxlen=0):
"""
Pads all the sentences to the same length. The length is defined by the longest sentence.
Returns padded sentences.
"""
if maxlen > 0:
sequence_length = maxlen
else:
sequence_length = max(len(s) for s in sentenc... |
def dump_version(command):
"""
Dump version for GCC/Clang compilers
Parameters
----------
command : str
Compiler command.
Returns
-------
float or None: version if found else None
"""
from subprocess import run, PIPE, CalledProcessError
try:
if (
... |
def is_ssl(socket):
"""True if socket is an active SSLSocket."""
return bool(getattr(socket, '_sslobj', False)) |
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
"""Test if a and b are close with given relative and absolute precision."""
return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) |
def oct2bin(x):
"""
Convert octal string to binary string.
For instance: '11' -> '1001'
"""
return bin(int(x, 8))[2:] |
def EnvsKwargs(task,env_kwargs):
"""
create several environments kwargs
:param tasks: the task we need the omnirobot to perform
:param env_kwargs: the original env_kwargs from previous pre-trained odel
:return: a list of env_kwargs that has the same length as tasks
"""
t=task
tmp=env_kwa... |
def play_recursive(p1, p2):
"""Get the winning deck of the recursive combat game."""
states_seen = set() # List of all states seen. If repeaded p1 wins
while p1 and p2:
# Check for duplicate play state
state = str(p1) + str(p2)
if state in states_seen:
return 1, p1
... |
def normalize_grid_frame(grid_frame):
"""
Given a range of acceptible abbreviations and spellings returns the exact frame name that we need.
Parameters
----------
grid_frame : str
The name of the grid frame that we're trying to match to.
Returns
-------
str - normalize... |
def filter_article(event):
"""
Optionally filters original article text from response.
Expects "original_text" and optional "filter_article"
fields in event.
"""
if event.get("filter_article", True):
del event["original_text"]
return event |
def RemoveIntervallsContained(intervalls):
"""
remove intervalls 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 intervalls:
return []
new... |
def list_of_dicts_to_dict_of_lists(list_of_dicts, key_order):
"""
Transform a list of dictionaries into a dictionary of lists.
"""
keys = list(set().union(*(list(d.keys()) for d in list_of_dicts)))
columns = {}
for key in keys:
columns[key] = [d[key] for d in list_of_dicts if key in d]
... |
def _code(function):
"""Return the byte code for a function.
If this Python interpreter does not supply the byte code for functions,
then this function returns NaN so that all functions compare unequal.
"""
return (function.__code__.co_code
if hasattr(function, '__code__') else float('n... |
def create_positional_array(cigar_tuples):
"""
# ========================================================================
CREATE POSITIONAL ARRAY
PURPOSE
-------
Create a positional array that maps positions in a
CIGAR tuple to a list.
Ex. CIGAR tuple is: [(0, 4), (2, 1) (1, 2)]
... |
def kill_kth_bit(n, k):
"""
use bitwise AND
0 & 0 = 0
1 & 0 = 0
to clear the bit: perform a bitwise AND of the number with a reset bit
n = n & ~(1 << k)
OR
n &= ~(1 << k)
"""
return n & ~(1 << k - 1) |
def month_by_name(name):
""" Return the number of a month by (locale-independently) English name """
ENGLISH_NAMES = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
try:
return ENGLISH_NAMES.index(name) + 1
... |
def _depgrep_node_label_use_action(_s, _l, tokens):
"""
Returns the node label used to begin a tgrep_expr_labeled. See
`_depgrep_segmented_pattern_action`.
Called for expressions like (`tgrep_node_label_use`)::
=s
when they appear as the first element of a `tgrep_expr_labeled`
expres... |
def intToStr(i):
"""
What we know.
1. Only have to look at the loop as there are no function calls
2. Within while loop, constant number of steps
3. How many times through loop?
i. how many times can one divide i by 10
ii. O(log(i)), log base 10 for the size of i
Nugget: It... |
def munge_time(t):
"""Take a time from nvprof and convert it into a chrome://tracing time."""
# For strict correctness, divide by 1000, but this reduces accuracy.
return t |
def _replace_from_end(s, a, b, times=None):
"""replace from end"""
return s[::-1].replace(a, b, times)[::-1] |
def count_non_zero_length(aaa):
"""
aaa = [[116, 105, 114, 97, 115],
[ 97, 110, 110, 0, 0],
[101, 108, 0, 0, 0]]
ans -> [5, 3, 2]
"""
bbb = []
for item in aaa:
counting = 0
for element in item:
if element != 0:
counting += ... |
def arrangeFlatsByExpId(exposureList, exposureIdList):
"""Arrange exposures by exposure ID.
There is no guarantee that this will properly group exposures, but
allows a sequence of flats that have different illumination
(despite having the same exposure time) to be processed.
Parameters
---------... |
def convert_seq_to_codons(seq):
"""Convert a string (sequence) into a list of 3-letter strings (triplets)."""
seq_codons = [seq[i : i + 3] for i in range(0, len(seq), 3)]
return seq_codons |
def value_right(self, right):
"""
Returns the value of the type instance calling an to use in an
operator method, namely when the method's instance is on the
right side of the expression.
"""
return self if isinstance(right, self.__class__) else self.value |
def is_sequence(arg):
"""
Checks if arg is a sequence
"""
if isinstance(arg, str):
return False
elif hasattr(arg, '__iter__'):
return True
elif not hasattr(arg, 'strip') and hasattr(arg, '__getitem__'):
return True
else:
return False |
def count_digit(n, digit):
"""Return how many times digit appears in n.
>>> count_digit(55055, 5)
4
"""
if n == 0:
return 0
else:
if n%10 == digit:
return count_digit(n//10, digit) + 1
else:
return count_digit(n//10, digit) |
def enough_objects(dictionary):
"""
Checks whether the json (converted to a dict) has enough objects (Monday-Sunday)
Returns True if it does, False otherwise.
"""
keys = list(dictionary.keys())
# still have no idea why there's 8 keys instead of 7
reference = ["0", "1", "2", "3", "4", "5", "6... |
def test_accuracy(reference_src, reference_tar, aligned_src, aligned_tar, penalty_points=None):
"""
Tests aligned lists of strings against reference lists, typically hand aligned.
Args:
reference_src: list of reference source strings
reference_tar: list of reference target strings
al... |
def nextIter(it, default=None):
"""
Returns the next element of the iterator,
returning the default value if it's empty,
rather than throwing an error.
"""
try:
return next(iter(it))
except StopIteration:
return default |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.