content stringlengths 42 6.51k |
|---|
def merge_dicts(*args):
"""
Creates a new dictionary that merges those passed as arguments.
"""
if not args:
return {}
d = args[0].copy()
for extra in args[1:]:
d.update(extra)
return d |
def get_campus(text):
"""
Check which campus is mentioned in the given text.
A campus can be denoted by its full name or by its three-letter acronym.
Args:
text: The text in which the occurrence of the campuses is checked.
Returns:
A list with acronyms for all UAntwerp campuses th... |
def transpose_shape(shape, target_format, spatial_axes):
"""Converts a tuple or a list to the correct `data_format`.
It does so by switching the positions of its elements.
# Arguments
shape: Tuple or list, often representing shape,
corresponding to `'channels_last'`.
target_forma... |
def parse_id(string):
"""Returns the UUID part of a string only."""
return string.split('/')[-1] |
def de_escape_string(*items)->list:
"""
Removes escape symbols from strings.
:param items: strings that have to be de-escaped
:return list: list of unescaped strings
"""
new_items = []
for item in items:
item_list = list(item)
new_string = ''
while item_list:
... |
def check_length(line, min=0, max=0):
"""Does a length check on the line
Params:
line (unicode)
min (int)
max (int)
Returns
true if length is ok
"""
status = True
if min and status:
status = len(line) >= min
if max and status:
status = len(lin... |
def quote_nan(value):
"""
Returns value in single quotes if value is not a number
>>> quote_nan('foo')
"'foo'"
>>> quote_nan('1')
'1'
"""
try:
float(value)
return value
except ValueError:
return "'{}'".format(value) |
def get_coordinate(coordinate, change, limit):
"""Get next coordinate, within 0 and limit."""
next_coordinate = coordinate + change
if next_coordinate < 0:
return 0
elif next_coordinate >= limit:
return limit - 1
return next_coordinate |
def S_VAR_ASSIGN(vardec, assign, data):
"""Evaluates an S_STATEMENT node"""
return vardec + "=" + data; |
def bio2ot_ts(ts_tag_sequence):
"""
perform bio-->ot for ts tag sequence
:param ts_tag_sequence:
:return:
"""
new_ts_sequence = []
n_tags = len(ts_tag_sequence)
for i in range(n_tags):
ts_tag = ts_tag_sequence[i]
if ts_tag == 'O':
new_ts_sequence.append('O')
... |
def find_duplicates(iterable):
"""Find duplicate elements in an iterable.
Parameters
----------
iterable : iterable
Iterable to be searched for duplicates (i.e., elements that are
repeated).
Returns
-------
set
Repeated elements in `iterable`.
"""
# modifie... |
def _get_color(color_tuple):
"""
Converts and returns the color in the following format:
In canse of a rgb tuple:
#%02x%02x%02x (i.e: #00FFCC)
In case of a rgba tuple:
rgba(color_tuple)
"""
if len(color_tuple) == 3:
return '#%02x%02x%02x' % color_tuple
return 'rgba%s'... |
def keyrange(start, end, duration=1000, **kwargs):
"""
Returns a list of frames sharing the same parameters
:param start: the first frame number for a range of frames used to
build a list of keyframes that share the given parameters
:param end: the last frame number for a range of frames used to
build a list o... |
def match(e, s):
"""Very naive NFA implementation (``*`` only, no parens or pipe)"""
e = e + '\0' # None is accept state
nfa = set([0])
i = 0
while nfa:
newnfa = set()
for j in nfa:
if e[j] == '\0':
return True
elif e[j] == '*':
... |
def decode(text):
"""
Decodes the text using run-length encoding
"""
lastIndex = len(text)
result = ""
i = 0
while i < len(text):
char = text[i]
if i == lastIndex - 1:
result += char
break
nextChar = text[i + 1]
if nextChar.isdi... |
def is_position_legal(position, board):
"""Return whether the given position is a legal position for given board."""
return 0 <= position[0] < len(board) and 0 <= position[1] < len(board) |
def rbndvi(b2, b4, b8):
"""
Red-Blue NDVI (Wang et al., 2007).
.. math:: RBNDVI = (b8 - (b4 + b2))/(b8 + (b4 + b2))
:param b2: Blue.
:type b2: numpy.ndarray or float
:param b4: Red.
:type b4: numpy.ndarray or float
:param b8: NIR.
:type b8: numpy.ndarray or float
:returns RBND... |
def init_loop_state(file_counter=0):
"""
Initialize the file row counter, the file counter,
and the list representing file data.
Janky, I know, but this needed to be done in 2 spots.
"""
file_row_counter = 0
file_counter += 1
file_data = []
file_data.append([
'Date',
... |
def check_methods(class_obj, *methods):
"""Check whether the listed methods are implemented in the provided class.
Args:
class_obj - the class object
methods - a list of method names
Returns:
either True (if the methods are implemented) or NotImplemented
"""
mro = class_obj... |
def _patient_graph_id(patient_id: str) -> str:
"""
Qualify a patient ID for use in the Graph API.
"""
if '-' not in patient_id:
patient_id = f"patient-{patient_id}"
if ',' not in patient_id:
patient_id = f"{patient_id},patient"
return patient_id |
def epa_nei_url_helper(build_url, config, args):
"""
Takes the basic url text and performs substitutions based on NEI year and version.
Returns the finished url.
"""
urls = []
url = build_url
url = url.replace('__year__', args['year'])
if args['year'] == '2017':
url = url.... |
def unzip_lst(lst):
"""
unzip a list of tuples/lists to multiple lists
"""
unzipped = list(zip(*lst))
unzipped_lsts = [list(tp) for tp in unzipped]
return unzipped_lsts |
def get_openmvg_image_path(kapture_image_name: str, flatten_path: bool = False):
""" the openmvg image sub path corresponding to the given kapture one. """
return kapture_image_name if not flatten_path else kapture_image_name.replace('/', '_') |
def indent(n: int) -> str:
"""Return `n` indents."""
return " " * n |
def evaluateInteriorInverseBarrier(
function, position,
inequalityConstraints=[],
equalityConstraints=[],
rp=1.0):
"""returns a float at the location selected with constraint penalties"""
objectiveValue = function(position)
ineq_constraint_penalty = 0
for constraint in inequalityConstr... |
def compute_basin(i, j, grid):
"""
Basin starts from the low point and includes any point up, down, left,
right as long as that point is lower than the point next to it. 9 is
automatically not included.
From each point that is in the basin, we have to recursively compute
the basin to the up, d... |
def get_fibonacci(n):
"""
A self-recursive function to obtain the final fibonacci result.
:type n: int
:rtype: int
"""
if n == 1 or n == 2:
return 1
n1 = n - 1
n2 = n - 2
fibr1 = get_fibonacci(n1)
fibr2 = get_fibonacci(n2)
res = fibr1 + fibr2
return res |
def paper_doll(text):
"""
Given a string, return a string where for every character in the original there are three characters
:param text:str
:return:str
paper_doll('Hello') --> 'HHHeeellllllooo'
paper_doll('Mississippi') --> 'MMMiiissssssiiippppppiii'
"""
new_text = ""
for ch in te... |
def unwords(strings):
"""
``unwords :: [String] -> String``
unwords is an inverse operation to words. It joins words with separating
spaces.
"""
return " ".join(strings) |
def srange(data):
"""
Accept data (list)
Return range of data.
"""
return max(data) - min(data) |
def _get_plot_title(target_name: str, last_observation_date: str,
eval_dataset_creation_date: str,
forecast_horizon: int) -> str:
"""Gets the title of the plot."""
return (
f"Comparison of metrics for predicting {target_name}. Forecast date: "
f"{last_observation_... |
def next_velocity(vel, acc, next_acc, h):
""" returns velocity at next time step for a particle. """
"""
parameters
----------
vel : array
velocity component of particle
acc : array
current force felt by particle
next_acc : array
accelaration acting on par... |
def split_strip(string, delimiter=u','):
"""
Splits ``string`` on ``delimiter``, stripping each resulting string
and returning a list of non-empty strings.
Ported from Jonathan Buchanan's `django-tagging
<http://django-tagging.googlecode.com/>`_
"""
if not string:
return []
wor... |
def sqrt(number):
"""
Calculate the floored square root of a number
Args:
number(int): Number to find the floored squared root
Returns:
int: Floored Square Root
"""
left = 0
right = number
while left <= right:
mid = (left + right)//2
if mid * m... |
def get_note_message(note):
"""return a message according to note
note is a float < 10 (10 is the highest note)
"""
assert note <= 10, "Note is %.2f. Either you cheated, or pylint's \
broken!" % note
if note < 0:
msg = 'You have to do something quick !'
elif note < 1:
msg = 'Hey... |
def compare_broken_content(broken_content_prod, broken_content_dev):
"""Compare output between 2 content_validation runs"""
unique_ids_prod = set([i["unique_id"] for i in broken_content_prod])
unique_ids_dev = set([i["unique_id"] for i in broken_content_dev])
new_broken_content_ids = unique_ids_dev.diff... |
def _meters_to_degrees(meters: int) -> float:
""" convert meters to approximate degrees """
# meters * 360 / (2 * PI * 6400000)
# multiply by (1/cos(lat) for longitude)
return meters * 1 / 111701 |
def _to_yaml(wrapped, instance, args, kwargs):
"""
New in v17
public decorator for yaml generator
"""
return wrapped(*args, **kwargs) |
def opt_pol_2(state) -> int:
"""'50 or 100 or bust' policy.
When capital is less than 50, implement a '50 or bust' policy; when
greater than 50, implement '100 or 50' policy; when exactly 50, go
all in.
"""
if state < 50:
return min(state, 50 - state)
elif state == 50:
retur... |
def get_excluded_hpo_ids_str(excluded_hpo_ids):
"""
Formats list of hpo_ids or None to add to bq script, adds empty hpo_id
:param excluded_hpo_ids: List output by args parser or None
:return: String of hpo_ids enclosed in single quotes along with empty hpo_ids
"""
if excluded_hpo_ids is None:
... |
def check_coordinates(loc_lat: float, loc_long: float, data_lat: float, data_long: float, margin=1.0) -> bool:
"""
Compares a pair of coordinates (latitude and longitude) to ensure proximity to a maximum value ('margin').
:param loc_lat: Latitude 1
:param loc_long: Longitude 1
:param... |
def add_none_match_to_list(combined_list: list, other_list: list) -> list:
"""Adds the none matched lists to the combined list
Args:
combined_list (list): combined list
other_list (list): [description]
Returns:
list: combined list of dictionaries
"""
headers = list(combined... |
def n_highest_grossing_actors(actors, n):
"""
Query helper to get the the n highest grossing ACTORS
:param actors: Dictionary (name of actor --> Actor node)
:param n: Number of highest grossing ACTORS to find
:return: List of n highest grossing ACTORS, sorted highest to lowest. If n > length of ACTO... |
def val_cm(val):
"""
arg val is tuple (2.0,'mm').
Return just the number in cm.
"""
if val[1] == 'cm':
return val[0]
if val[1] == 'mm':
return val[0]/10.0
if val[1] == 'm':
return val[0]*10
if val[1] == 'nm':
return val[0]/1E8 |
def extended_euclidean_algorithm(a, b):
"""
Returns a three-tuple (gcd, x, y) such that
a * x + b * y == gcd, where gcd is the greatest
common divisor of a and b.
This function implements the extended Euclidean
algorithm and runs in O(log b) in the worst case.
"""
s, old_s = 0, 1
t,... |
def _dhms2day( d, h, m, s ):
"""
The `_dhms2day` function converts hour, minute and second format for a given
day (decimal or integer) into a decimal day.
Returns a float.
"""
return d + h/24.0 + m/1440.0 + s/86400.0 |
def if_cond(condition, if_return, else_return):
"""
Evaluate a condition to select a return value
@param condition Statement that evaluates to a boolean
@param if_return Value that is returned if condition is true
@param else_return Value that is returned if condition is false
@return if_retur... |
def hello(name: str = "World") -> str:
"""Provide a name, return a greeting."""
return f"Hello, {name}!" |
def constrain (n, min, max):
"""This returns a number, n constrained to the min and max bounds. """
if n < min:
return min
if n > max:
return max
return n |
def strip_backslashes(input_string: str) -> str:
"""
>>> strip_backslashes(r'\\test\\\\')
'test'
"""
input_string = input_string.strip('\\')
return input_string |
def g_minority_1_dev(by_grps):
"""Passed if large group is fully consistent, and both small group items are different to each other and the large group letter
Examples:
Large - all S, small O,N -> TRUE
Large - all S, small S,N -> False
Large - all S, small N,N -> False
This behavior is beca... |
def _split_input_slice(batch_size, work_load_list):
"""Get input slice from the input shape.
Parameters
----------
batch_size : int
The number of samples in a mini-batch.
work_load_list : list of float or int, optional
The list of work load for different devices,
in the same... |
def build_select(table, to_select, where=None):
"""
Build a select request.
Parameters
----------
table : str
Table where query will be directed.
to_set: iterable
The list of columns to select.
where: iterable
The list of conditions to constrain the query.
Retur... |
def startswith(text: str, starts: str) -> bool:
"""
Template implementation of `str.startswith()`.
"""
if isinstance(text, str):
return text.startswith(starts)
return False |
def timezone_format(value):
""" Check the value of the timezeone offset and, if postive, add a plus sign"""
try:
if int(value) > 0:
value = '+' + str(value)
except:
value = ''
return value |
def make_qualified_schema_name(schema_name: str, table_name: str) -> str:
"""produces a qualified schema name given a schema and table
:param schema_name: the schema name
:param table_name: the table name
:return: a quoted dot separated qualified schema name
"""
return '"{s}"."{t}"'.format(s=sc... |
def eth_addr(a):
"""
Print Mac Address in the human format
Args:
a: string "6s"
Returns:
mac in the human format
"""
if isinstance(a, bytes):
a = a.decode("latin")
string = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"
mac = string % (ord(a[0]), ord(a[1]), ord(a[2]),
... |
def normalize_date(date):
"""Bring the date argument to a uniform format, which is YYYY-MM-DD."""
# Remove the time portion.
time_pos = date.find('T')
if time_pos >= 0:
date = date[:time_pos]
# Insert dashes.
if len(date) == 8:
date = date[:4] + '-' + date[4:6] + '-' + date[6:]
... |
def fullest_bank(banks):
"""
>>> fullest_bank([2, 4, 1, 2])
1
>>> fullest_bank([3, 1, 2, 3])
0
"""
return banks.index(max(banks)) |
def parse_s2bins(s2bins):
"""
parse ggKbase scaffold-to-bin mapping
- scaffolds-to-bins and bins-to-scaffolds
"""
s2b = {}
b2s = {}
for line in s2bins:
line = line.strip().split()
s, b = line[0], line[1]
if 'UNK' in b:
continue
if len(line) > 2... |
def mag2mom_nm(mw):
"""Converts magnitude to moment - newtonmetre"""
return 10 ** (9.05 + 1.5 * mw) |
def intents_to_string(intents, queryset=False):
"""
Args:
intents: [{"action": "/application/json/view"}, ...] OR
[models.Intent] (if queryset=True)
Returns:
['<intent.action', ...]
"""
if queryset:
new_intents = [i.action for i in intents]
else:
... |
def make_list(obj):
"""Convert an object to a list if it is not already"""
if isinstance(obj, list):
return obj
return [obj] |
def py_str(string):
"""Convert C string back to Python string"""
return string.decode('utf-8') |
def calc_more_stuff(result):
"""Do some more calculations"""
if type(result) is list:
result += [{"more": "stuff"}]
elif type(result) is dict:
result["more"] = "stuff"
return result |
def surface_for_tile(northing, easting, elevation):
"""Returns the points and faces that represent the tile boundary.
The provided elevation will be used and thus it will be uniform across the
tile. The corner points will not be based on the elevation of the corners
in the tiles nor will it be the avera... |
def _round_to_bit(value, power):
"""Rounds the given value to the next multiple of 2^power.
Args:
value: int to be rounded.
power: power of two which the value should be rounded up to.
Returns:
the result of value rounded to the next multiple 2^power.
"""
return (((value - 1) >> p... |
def interpret_line(line, splitter=','):
"""
Split text into arguments and parse each of them to an appropriate format (int, float or string)
Args:
line:
splitter:
Returns: list of arguments
"""
parsed = list()
elms = line.split(splitter)
for elm in elms:
try:
... |
def cipher(text, shift, encrypt=True):
"""
encrypting or decrypting a text with Caesar cipher.
It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet
Parameters
----------
text : string
text waitin... |
def repeat_inside(line: str) -> str:
"""
first the longest repeating substring
"""
ans = ''
for i in range(len(line)):
for j in range(len(line) - i):
s = line[i:i + j + 1]
for repeat in range(2, len(line) // len(s) + 1):
ls = s * repeat
... |
def get_inter_area(box_a, box_b):
"""
Args:
box_a: (xmin, ymin, xmax, ymax)
box_b: (xmin, ymin, xmax, ymax)
Returns:
intersection_area: intersection area between two points
"""
x_a = max(box_a[0], box_b[0])
y_a = max(box_a[1], box_b[1])
x_b = min(box_a[2], box_b[2])
... |
def seconds_readable(seconds: int) -> str:
"""Turn seconds as an int into 'DD:HH:MM:SS'."""
r: list[str] = []
days, seconds = divmod(seconds, 60 * 60 * 24)
if days:
r.append(f"{days:02d}")
hours, seconds = divmod(seconds, 60 * 60)
if hours:
r.append(f"{hours:02d}")
minutes... |
def str_to_bytes(obj) -> bytes:
"""Convenience method around encode"""
return obj.encode() |
def calculate_centroid(cluster):
"""
Calculates centroids of a single cluster
cluster is a 2-dimensional list of data points, each data point being a list of two integers.
Returns a centroid as a list of integers.
"""
num_of_points = len(cluster)
cent_x = sum(point[0] for point in cluster)... |
def formatTwoDecimalPoints(floatNum):
"""Return a string of the float number with only two decimal points."""
return "%.2f" % floatNum |
def OrganizeFindingsByFile(findings_list):
"""Creates dict mapping filepath to all findings in that file.
Args:
findings_list: a list of Finding objects.
Returns:
A dict. Keys are filepath strings and values are a list of findings
that are associated with that filepath.
"""
file_findings_map =... |
def is_svg(url) -> bool:
"""
Determine if it is an image of svg.
Parameters
----------
url : str
Target url.
Returns
-------
True or False: Return True if this url content is an image of svg else returns False.
"""
if url.endswith(".svg"):
return True
else:
... |
def product_vector3_3x3(vector,matrix):
"""mulipli le vector par une matrice care """
x = matrix[0][0]*vector[0] + matrix[0][1]*vector[1] + matrix[0][2]*vector[2]
y = matrix[1][0]*vector[0] + matrix[1][1]*vector[1] + matrix[1][2]*vector[2]
z = matrix[2][0]*vector[0] + matrix[2][1]*vector[1] + matrix[2][2]*vector[2]... |
def _is_a_url(input_element):
"""
-----
Brief
-----
Auxiliary function responsible for checking if the input is a string that contains an url.
-----------
Description
-----------
Some biosignalsnotebooks functions support a remote access to files. In this situation it is
importa... |
def card_html_id(card):
"""Return HTML id for element containing given card."""
return f'c{card:02d}' |
def next_power_of_two(x):
# type: (int) -> int
"""
Compute the next power of two that is greater than `x`:
>>> next_power_of_two(0)
1
>>> next_power_of_two(1)
2
>>> next_power_of_two(2)
4
>>> next_power_of_two(3)
4
>>> next_power_of_two... |
def get_valid_segment(text):
""" Returns None or the valid Loki-formatted urn segment for the given input string. """
if text == '':
return None
else:
# Return the converted text value with invalid characters removed.
valid_chars = ['.', '_', '-']
new_text = ''
for c... |
def by_rank(line):
"""
Adds the rankings of each program
:param line:
:return:
"""
sum = 0
cnt = 0
for i in range(0, len(line[:-1]), 3):
if not '--' in line[i]:
cnt += 1
sum += int(line[i])
return sum / cnt |
def fileNaming(names):
"""
Since two files cannot have equal names,
the one which comes later will have an addition
to its name in a form of (k), where k is the smallest
positive integer such that the obtained name is not
used yet. Return an array of names that will be given
to the file... |
def get_absolute_url(base_url: str, relative_url: str) -> str:
"""
Get absolute url for relative_url with given base_url.
:param base_url: base page url
:param relative_url: list of relative urls
:return: absolute url
"""
absolute_url = relative_url
if absolute_url.startswith('//'):
... |
def parse_gpu_ids(gpu_str):
"""Parse gpu ids from configuration.
Args:
gpu_str: string with gpu indexes
Return:
a list where each element is the gpu index as int
"""
gpus = []
if gpu_str:
parsed_gpus = gpu_str.split(",")
if len(gpus) != 1 or int(gpus[0]) >= 0... |
def extract_entities(input_data_tokens, entity_dict):
"""Extracts valid entities present in the input query.
Parses the tokenized input list to find valid entity values, based
on the given entity dataset.
Args:
input_data_tokens: A list of string tokens, without any punctuation,
ba... |
def get_port_detail(ports):
"""
Iterate over ports details from response and retrieve details of ports.
:param ports: list of ports details from response
:return: list of detailed element of ports
:rtype: list
"""
return [{
'ID': port.get('id', ''),
'Number': port.get('numbe... |
def nick_match_tellee(nick, tellee):
"""Tell if a ``nick`` matches a ``tellee``.
:param str nick: Nick seen by the bot
:param str tellee: Tellee name or pattern
The check between ``nick`` and ``tellee`` is case-insensitive::
>>> nick_match_tellee('Exirel', 'exirel')
True
>>> n... |
def set_and_true(key, _dict):
"""Is key in dict and value True?
Args:
key (str): Key to lookup in dictionary.
_dict (dict): The dictionary.
Returns:
bool: Is key in dict and value True?
"""
return key in _dict and _dict[key] is True |
def filter_by_id(result_list_to_filter, filtering_id_field, desired_id):
""" Given a list of results, returns only the ones that are tied to a given ID.
Args:
result_list_to_filter (list): list of dictionaries, containing data about entries.
filtering_id_field: The name of the field containin... |
def flatten(lst):
"""Returns a flattened version of lst.
>>> flatten([1, 2, 3]) # normal list
[1, 2, 3]
>>> x = [1, [2, 3], 4] # deep list
>>> flatten(x)
[1, 2, 3, 4]
>>> x = [[1, [1, 1]], 1, [1, 1]] # deep list
>>> flatten(x)
[1, 1, 1, 1, 1, 1]
"""
"*** YOUR CODE H... |
def viewitems(d):
"""Return either the items or viewitems method for a dictionary.
Args:
d (:obj:`dict`): A dictionary.
Returns:
view method: Either the items or viewitems method.
"""
func = getattr(d, "viewitems", None)
if func is None:
func ... |
def assign_min_points(num_points: int) -> int:
""" Improves runtime speed without impacting scores. set to 1 if willing to wait. """
return max((num_points // 1000), 1) |
def _get_md_relative_link(id_: str, title: str) -> str:
"""Returns a representation of a zettel that is a relative Markdown link.
Asterix at the beginning is a Markdown syntax for an unordered list, as links to
zettels are usually just used in references section of a zettel.
"""
return f"* [{id_}](... |
def divide2(a, b):
"""use ``try... except PossibleException... except`` clause.
List all possible exception you can imagine, and leave other unpredictable
exception to ``except clause``.
"""
try:
return a * 1.0 / b
except ZeroDivisionError:
raise ValueError("Zero division Error!... |
def decode(token):
"""Un-escape special characters in a token from a path representation.
:param str token: The token to decode
:return: The decoded string
:rtype: str
"""
return token.replace(r'\/', '/').replace('\\\\', '\\') |
def rearrange_digits(input_list):
"""
Rearrange Array Elements so as to form two number such that their sum is maximum.
Args:
input_list(list): Input List
Returns:
(int),(int): Two maximum sums
"""
if len(input_list) == 0 or type(input_list) != list:
print("INVALID INPUT"... |
def get_attribute_components(attribute, vars_only=True):
"""
Gets component names out of an attribute
>>> from pprint import pprint
>>> attr = ('a', ('sub1', '?c1'))
>>> get_attribute_components(attr)
{'?c1'}
>>> attr = '?c1'
>>> get_attribute_components(attr)
{'?c1'}
>>> attr... |
def _int_or_none(value):
"""Helper: return an integer or ``None``."""
if value is not None:
value = int(value)
return value |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.