content stringlengths 42 6.51k |
|---|
def reparsing_namespaces(namespace_map):
"""compile a string representation of the namespaces"""
namespace_string = ""
for prefix, uri in namespace_map.items():
namespace_string += 'xmlns:%s="%s" ' % (prefix, uri)
return namespace_string.rstrip() |
def list_to_number(column):
"""
Turns a columns of 0s and 1s to an integer
Args:
column: List of 0s and 1s to turn into a number
"""
# Cast column integers to strings
column = [str(cell) for cell in column]
# Turn to an integer with base2
return int(''.join(column), 2) |
def reverse_config_section(section):
"""
creates an inverted lookup from a config section. Useful to find LEDs and PWM.
"""
return {v: k for k, v in section.items()} |
def define_browsers(browsers, remote_browsers, default_browsers, custom_browsers):
"""Generate the definitions for the browsers.
A defined browser contains the following attributes:
'name': real name
'capabilities': capabilities defined by remote_browsers setting
"""
browsers_defini... |
def _get_resource_tree(req_path):
"""
Given a :requested_path split this path into array that represents the
tree to that resource in server's uploaded-resources-folder (uploads).
e.g.: venv_a/etl_archive_a/subdir_1/etl_script.py =>
[venv_a, etl_archive_a, subdir_1, etl_script.py]
... |
def calculate_cell_power_level(x, y, serial):
"""
Calculate the convoluted power level of a cell based on the formula provided.
:param x: the x coordinate
:param y: the y coordinate
:param serial: the serial number of the device
:return: the power level
>>> calculate_cell_power_level(3, 5, ... |
def gf_quote(str):
"""
Changes the underscore for a space and quotes the string.
"""
return '"' + str.replace("_", " ").replace('"', '\\"') + '"' |
def replace(_, result, original, substitute):
""" Replace substrings within content. """
return result.replace(original, substitute) |
def transpose(matrix):
"""
Transpose e.g. [[1,2,3], [4,5,6]] to [[1,4], [2,5], [3,6]]
"""
return list(map(list, zip(*matrix))) |
def tf_format(string: str, clip_start: int, clip_end: int) -> str:
"""
formats a string with clip information, returns result
clip_start: int
clip start in seconds
clip_end: int
clip end in seconds
"""
def ts_format(ts: int) -> str:
"""nested function represent `ts: int... |
def get_lambda_zip_name(domain):
"""Get name of zip file containing lambda.
This must match the name created in the makedomainenv script that runs on
the lambda build server.
Args:
domain (string): The VPC's domain name such as integration.boss.
Returns:
(string)
"""
retur... |
def extract_years_filter(config):
"""
Extract min and max years to filter data from "years_filter" dictionary value the query
configuration. The years will be splited by the "-" character.
years_filter: 1780-1918
:param config: config
:type config: dict
:return: min_year, max_year
... |
def parse_coordinates(result):
"""
Function purpose: parse coordinates from console result into a list
Input: string
Output: list
"""
bboxStr = result[result.find("[") + 1:result.find("]")]
bboxList = [float(i) for i in bboxStr.split(',')]
return bboxList |
def least_missing(colors):
"""The smallest integer not in 'colors'."""
colors.sort()
for color in colors:
if color + 1 not in colors:
return color + 1 |
def upper_bound(arr, value, first, last):
"""Find the upper bound of the value in the array
upper bound: the first element in arr that is larger than value
Args:
arr : input array
value : target value
first : starting point of the search, inclusive
last ... |
def with_end_char(text, char):
"""
Description: Append an after character to a text
:param text: raw text
:param char: character to put
:return: Appended Text (e.g. with_end_char("Accounts", ":")-> "Accounts:")
"""
return str("").join([text, char]) |
def generate_ucc_amplitudes(n_electrons, n_spin_orbitals):
"""
Create lists of amplidues to generate UCC operators.
This function does not enforce spin-conservation in the
excitation operators.
Args:
n_electrons (int): Number of electrons.
n_spin_orbitals (int): Number of spin-orbita... |
def clean_file_extensions(filename):
"""
For solving the error: Unknown image file format. One of JPEG, PNG, GIF, BMP required.
"""
return filename
# return filename.replace("jpg", "jpeg")
# if '.webp' in filename:
# image = Image.open('new-format-image-from-png.webp')
# image =... |
def _is_equal(x, y):
"""special comparison used in get_all_doc_starts"""
return x[0] == y |
def get_parent_hierarchy_object_id_str(elt):
"""Get the elt path from the 1st parent with an objectId / ElectionReport."""
elt_hierarchy = []
current_elt = elt
while current_elt is not None:
if current_elt.get("objectId"):
elt_hierarchy.append(current_elt.tag + ":" + current_elt.get("objectId"))
... |
def _pointer_str(obj):
"""
Get the memory address of *obj* as used in :meth:`object.__repr__`.
This is equivalent to ``sprintf("%p", id(obj))``, but python does not
support ``%p``.
"""
full_repr = object.__repr__(obj) # gives "<{type} object at {address}>"
return full_repr.rsplit(" ", 1)[1... |
def TSA_rn_v( rnet, vegetation_fraction):
"""
//Chen et al., 2005. IJRS 26(8):1755-1762.
//Estimation of daily evapotranspiration using a two-layer remote sensing model.
Vegetation net radiation
TSA_rn_v( rnet, vegetation_fraction)
"""
result = vegetation_fraction * rnet
return result |
def convert_to_unsigned_integer(value, size):
"""
:param int size: number of bits containing this integer
"""
upper_bound = 2 ** size
if not (-upper_bound // 2 <= value < upper_bound):
msg = '{} is out of range of {} bits'.format(value, size)
raise ValueError(msg)
all_f_mask = up... |
def IsSorted(arr):
""" Returns True if the array is Sorted in either ascending or descending order;
False If Not
"""
ascend, descend = True, False
for i in range(len(arr)-1):
if (arr[i] < arr[i+1]) and not descend:
ascend, descend = True, False
elif (arr[i] > ar... |
def _plan(D, W):
"""The naming scheme for a ResNet is 'cifar_resnet_N[_W]'.
The ResNet is structured as an initial convolutional layer followed by three "segments"
and a linear output layer. Each segment consists of D blocks. Each block is two
convolutional layers surrounded by a residual connection. E... |
def bit_reversed(x, n):
"""
Bit-reversal operation.
Parameters
----------
x: ndarray<int>, int
a vector of indices
n: int
number of bits per index in ``x``
Returns
----------
ndarray<int>, int
bit-reversed version of x
"""
result = 0
for i in r... |
def depth(expr):
"""
depth(expr) finds the depth of the mathematical expr formatted as a list.
depth is defined as the deepest level of nested operations within the expression.
"""
if not isinstance(expr, list):
raise TypeError("depth() : expr must be of type list")
else:
exprlen... |
def pyramid_sum(lower, upper, margin = 0):
"""Returns the sum of the numbers from lower to upper,
and outputs a trace of the arguments and return values
on each call."""
blanks = " " * margin
print(blanks, lower, upper) # Print the arguments
if lower > upper:
print(blanks, 0) # Print th... |
def countSlopeChange(slopesBetweenJumps):
"""
Counts the number of slope changes and corresponds that to sides
slopesBetweenJumps: list of slopes
Returns: side count
"""
currSlope = 10000
sides = 0
for x in slopesBetweenJumps:
if(abs(x - currSlope) > 10):
sides +=1
... |
def find_largest_digit(n):
"""
:param n: (int) An integer.
:return: (int) Single digit integer which is the bigger than any other digits integer.
"""
if n < 0:
n = - n # Make each integer a positive integer
# Base case
if n < 10:
return n
# Recursive case
else:
remainder_one = n % 10 # Store the last n... |
def _splitTag(tag):
"""Split the namespace and tag name"""
return [part.strip('{') for part in tag.split('}')] |
def parse_slice_inv(text):
"""Parse a string into a slice notation.
This function inverts the result from 'parse_slice'.
:param str text: the input string.
:return str: the slice notation.
:raise ValueError
Examples:
parse_slice_inv('[None, None]') == ":"
parse_slice_inv('[1, 2]') ... |
def cumdiff(yin):
""" compute the cumulative mean normalized difference """
W = len(yin)
yin[0] = 1.
cumsum = 0.
for tau in range(1, W):
cumsum += yin[tau]
if cumsum != 0:
yin[tau] *= tau/cumsum
else:
yin[tau] = 1
return yin |
def get_median_two_sorted_arrays_merge_sort(arr1, arr2):
"""
Time complexity: O(m+n)
Space complexity: O(n)
Args:
arr1:
arr2:
Returns:
"""
new_arr = []
i = 0
j = 0
while i < len(arr1) and j < len(arr2):
if arr1[i] < arr2[j]:
new_arr.append(a... |
def alpha_blend(img1,img2,alpha):
""" Blend two images with weights as in alpha. """
return (1 - alpha)*img1 + alpha * img2 |
def get_var_val(key, ii, varDict):
"""Gets an input in the likes of ${var} and returns the corresponding var value from the dict
Parameters
----------
key: string
unparsed key of var
ii: int
current iteration idx
varDict: dict
variable dictionary
Returns... |
def compare(c1, c2):
"""Compares two configuration dictionaries
Returns:
< 0 if c1 is bigger than c2
0 if they're equivalent sizes
> 0 if c2 is bigger than c1
"""
result = len(c2.keys()) - len(c1.keys())
while result > -1:
for k, v in c1.items():
... |
def get_next_available_key(iterable, key, midfix="", suffix="", is_underscore=True, start_from_null=False):
"""Get the next available key that does not collide with the keys in the dictionary."""
if start_from_null and key + suffix not in iterable:
return key + suffix
else:
i = 0
und... |
def tail(iterable):
"""Returns all elements excluding the first out of an iterable.
:param iterable:
Iterable sequence.
:returns:
All elements of the iterable sequence excluding the first.
"""
return iterable[1:] |
def extract_method_header(headers):
"""
Extracts the request method from the headers list.
"""
for k, v in headers:
if k in (b':method', u':method'):
if not isinstance(v, bytes):
return v.encode('utf-8')
else:
return v |
def index_to_bytes(i):
""" Map the WHATWG index back to the original ShiftJIS bytes.
"""
lead = i // 188;
lead_offset = 0x81 if lead < 0x1F else 0xC1
trail = i % 188
trail_offset = 0x40 if trail < 0x3F else 0x41
return (lead + lead_offset, trail + trail_offset) |
def convert_units(val, fromUnit, toUnit):
""" Convert flowrate units. Possible volume values: ml, ul, pl; possible time values: hor, min, sec
:param fromUnit: unit to convert from
:param toUnit: unit to convert to
:type fromUnit: str
:type toUnit: str
:return: float
"""
time_fact... |
def f(LL):
"""list[list[int]] -> int"""
(LL + [])[0].append(1)
return 0 |
def _get_object_id_from_url(url):
"""
Extract object ID from Ralph API url.
"""
return url.rstrip('/').rpartition('/')[2] |
def clean_tag(tag):
"""clean up tag."""
if tag is None:
return None
t = tag
if isinstance(t, list):
t = t[0]
if isinstance(t, tuple):
t = t[0]
if t.startswith('#'):
t = t[1:]
t = t.strip()
t = t.upper()
t = t.replace('O', '0')
t = t.replace('B', '8... |
def has_even_parity(message: int) -> bool:
""" Return true if message has even parity."""
parity_is_even: bool = True
while message:
parity_is_even = not parity_is_even
message = message & (message - 1)
return parity_is_even |
def pattern_string_generator(patterns):
"""
Creates a list of viable pattern strings that are easier to read
Input:
patterns --- a list of lists of individual characters e.g. [["A","B","B","A"],["B","A","B","A"]]
Output:
pattern_strings --- a list of lists of strings e.g. [["ABBA"],["BA... |
def build_error_report(results):
"""Build an user-friendly error report for a failed job.
Args:
results (dict): result section of the job response.
Returns:
str: the error report.
"""
error_list = []
for index, result in enumerate(results):
if not result['success']:
... |
def label_connected_components(num_nodes, edges):
"""Given a graph described by a list of undirected edges, find all connected
components and return labels for each node indicating which component they belong to."""
leader = list(range(num_nodes))
def head(k):
if leader[k] == k:
return k
else:
leader[k] ... |
def dict_key_apply(iterable, str_fkt):
"""
Applys a string modifiying function to all keys of a nested dict.
"""
if type(iterable) is dict:
for key in list(iterable.keys()):
new_key = str_fkt(key)
iterable[new_key] = iterable.pop(key)
if type(iterable[new_key]... |
def addNewCards(old_dict, comparison_dict, new_dict):
"""Compare old_dict and comparison_dict, add only those keys not in
old_dict to the new_dict."""
if old_dict:
for k, v in comparison_dict.items():
if k not in old_dict:
new_dict[k] = comparison_dict[k]
retu... |
def diff(list_1, list_2):
"""
get difference of two lists
:param list_1: list
:param list_2: list
:return: list
"""
return list(set(list_1) - set(list_2)) |
def sec_from_hms(start, *times):
""" Returns a list of times based on adding each offset tuple in times
to the start time (which should be in seconds). Offset tuples can be
in any of the forms: (hours), (hours,minutes), or (hours,minutes,seconds).
"""
ret = []
for t in times:
cur... |
def check_7(oe_repos, srcoe_repos):
"""
All repositories' name must follow the gitee requirements
"""
print("All repositories' name must follow the gitee requirements")
errors_found = 0
error_msg = "Repo name allows only letters, numbers, or an underscore (_), dash (-),"\
" and ... |
def sum_of_n_natual_numbers(n):
"""
Returns sum of first n natural numbers
"""
try:
n+1
except TypeError: # invlid input hence return early
return
if n < 1: # invlid input hence return early
return
return n*(n+1) // 2 |
def is_interesting_transaction(txdata, all_addresses):
"""Check if the transaction contains any deposits to our addresses."""
return any([(detail["category"] == "receive" and detail["address"] in all_addresses) for detail in txdata["details"]]) |
def remove_trailing_s(token):
"""Remove trailing s from a string."""
if token.endswith('s'):
return token[:-1]
else:
return token |
def one(iterable, too_short=None, too_long=None):
"""Return the first item from *iterable*, which is expected to contain only
that item. Raise an exception if *iterable* is empty or has more than one
item.
:func:`one` is useful for ensuring that an iterable contains only one item.
For example, it c... |
def dice_coefficient(a, b, case_insens=True):
"""
:type a: str
:type b: str
:type case_insens: bool
dice coefficient 2nt/na + nb.
https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Dice%27s_coefficient#Python
"""
if case_insens:
a = a.lower()
b = b.lower()
... |
def reducefn(x, y):
"""Combine counts arrays.
e.g. [12, 7, ...], [3, 1, ...] -> [15, 8, ...]
"""
for i, v in enumerate(y):
x[i] += v
return x |
def half_adder(a,b):
"""Single bit addition"""
return (a+b)%2 |
def convert_array(arr):
"""
In-place algorithm without using extra space
"""
size = len(arr) // 3
for idx in range(len(arr)):
swap_index = (idx % 3) * size + (idx // 3)
while swap_index < idx:
swap_index = (swap_index % 3) * size + (swap_index // 3)
arr[idx], arr[... |
def int_dpid(dpid):
"""Convert a str dpid to an int."""
dpid = int(dpid.replace(":", ""), 16)
return dpid |
def clean_bibtex_authors(author_str):
"""Convert author names to `firstname(s) lastname` format."""
authors = []
for s in author_str:
s = s.strip()
if len(s) < 1:
continue
if ',' in s:
split_names = s.split(',', 1)
last_name = split_names[0].strip(... |
def get_simbench_code_from_parameters(sb_code_parameters):
""" Converts flag parameters, describing a SimBench grid selection, into the unique regarding
SimBench Code. """
switch_param = "no_sw" if not sb_code_parameters[6] else "sw"
sb_code = str(sb_code_parameters[0])+"-"+sb_code_parameters[1]+sb_code... |
def lr_scheduler(optimizer, epoch, lr_decay=0.3, lr_decay_epoch=2, number_of_decay=5):
"""
lr_scheduler method is written for decay learning rate by a factor of lr_decay
every lr_decay_epoch epochs
:param optimizer: input optimizer
:param epoch: epoch number
:param lr_decay: the rate of reductio... |
def create_tron_config(*args) -> str:
""" Convert a list of parameters into a serialized tron grid string.
Parameters
----------
args
All of the arguments into tron grid as star args.
Returns
-------
str
Serialized string
"""
raw_string = "{};" * len(args)
return... |
def mod(a: int, b: int) -> int:
"""
Return a mod b, account for positive/negative numbers
"""
return (a % b + b) % b |
def is_channel(code, length):
"""Method to check if given axis code belongs to channel dimension.
Parameters
----------
code :
Returns
-------
bool
"""
return code == "C" and not length > 8 |
def get_version(version: tuple) -> str:
"""
Gets the version of the package based on a :class:`tuple` and returns a :class:`str`.
This method is based on ``django-extensions`` get_version method.
"""
str_version = ''
for idx, n in enumerate(version):
try:
str_version += '%d'... |
def build_dict(*param_dicts, **param_dict):
"""
Create a merged dictionary from the supplied dictionaries and keyword parameters.
"""
merged_param_dict = param_dict.copy()
for d in param_dicts:
if d is not None:
# log.info("param_dicts %r"%(d,))
merged_param_dict.upda... |
def is_leap_year(year):
""" Return boolean flag indicating whether the given year is a leap year
or not.
"""
if year > 1582:
return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
else:
return year % 4 == 0 |
def find_top(char_locs, pt):
"""Finds the 'top' coord of a word that a character belongs to.
:param char_locs: All character locations on the grid.
:param pt: The coord of the required character.
:return: The 'top' coord.
"""
if pt not in char_locs:
return []
l = list(pt)
... |
def get_pixel_brightness(pixel):
"""rgb pixel to brightness value"""
return(max((pixel[0], pixel[1], pixel[2])) / 255 * 100) |
def count_change(amount):
"""Return the number of ways to make change for amount.
>>> count_change(7)
6
>>> count_change(10)
14
>>> count_change(20)
60
>>> count_change(100)
9828
"""
"*** YOUR CODE HERE ***"
def max_m(num):
i = 0
while pow(2, i) < num:
... |
def scale(input_value, input_min, input_max, out_min, out_max):
""" scale a value from one range to another
"""
# Figure out how 'wide' each range is
input_span = input_max - input_min
output_span = out_max - out_min
# Convert the left range into a 0-1 range (float)
valuescaled = float(input... |
def lerp(x, x0, x1, y0, y1):
"""
Interpolates to get the value of y for x by linearly interpolating
between points (x0, y0) and (x1, y)
:return:
"""
t = (x-x0)/(x1 - x0)
return (1 - t) * y0 + t * y1 |
def second_to_day(seconds):
"""
:param seconds:
:return:
"""
day = int(seconds)/86400
assert day < 58, 'Too many seconds, reached day %s' % day
return day |
def get_violations_per_category(guideline_violations, guidelines):
""" Get the number of violations per category """
violations_per_category = {}
if any(guidelines):
# initialize the violations to 0 for all categories
violations_per_category = {"Required": 0, "Advisory": 0, "Mandatory": 0}... |
def func_linear(data, a_factor, y_int):
"""A linear function with y-intercept"""
return a_factor * data + y_int |
def recompute_fuel( positions ):
"""
Move from 16 to 5: 66 fuel : 11 = 1+2+3+4+5+6+7+8+9+10+11 = 66
Move from 1 to 5: 10 fuel : 4 = 1+2+3+4= 10
"""
fuel_per_target = dict()
for target in range( max(positions)+1 ):
fuel_per_submarine = [ sum( range(1,abs(target-position)+1) ) for positio... |
def default_pop(dictobj, key, default={}):
"""Pop the key from the dict-like object. If the key doesn't exist, return a default.
Args:
dictobj (dict-like): A dict-like object to modify.
key (hashable): A key to pop from the dict-like object.
default: Any value to be returned as defa... |
def n_xx_n_mod_k(n, k):
""" Compute n ** n mod k. """
return pow(n, n, k) |
def _year_matches_code(year, code):
"""
Returns whether `year` falls in the range specified by `code`
"""
if isinstance(code, int): # explicitly specified year as an int
return year == code
if not code or code.lower() == "default": # empty indicates default case
return True
cod... |
def paths_prob_to_edges_flux(paths_prob):
"""Chops a list of paths into its edges, and calculate the probability
of that edge across all paths.
Parameters
----------
paths: list of tuples
list of the paths.
Returns
-------
edge_flux: dictionary
Edge tuples as keys, and ... |
def convert_to_kw(value: float, precision: int = 1) -> float:
"""Converts watt to kilowatt and rounds to precision"""
# Don't round if precision is -1
if precision == -1:
return value / 1000
else:
return round(value / 1000, precision) |
def _ensure_mfdataset_filenames(fname):
"""Checks if grib or nemsio data
Parameters
----------
fname : string or list of strings
Description of parameter `fname`.
Returns
-------
type
Description of returned object.
"""
from glob import glob
from numpy import s... |
def _rreplace(s, old, new, occurrence=1):
"""Simple function to replace the last 'occurrence' values of 'old'
with 'new' in the string 's'
Thanks to https://stackoverflow.com/questions/2556108/
rreplace-how-to-replace-the-last-occurrence-
of-an-expression-in-a-string... |
def keywords_encode(d):
""" Takes a dictionary of keywords and encodes them into a somewhat
readable url query format.
For example:
{
'color': ['blue', 'red'],
'weight': ['normal']
}
Results in
'+color:blue+color:red+weight:normal'
Instead of a di... |
def check_args(allowed, arg):
"""Raise name error if argument doesn't match 'allowed' list/set of values"""
assert type(allowed) in {list, set}, "First argument must be 'allowed' list/set of args values"
if arg not in allowed:
raise NameError("Unexpected arg {0}: allowed args are {1}".format(arg, al... |
def get_correct_url(html, vid_id):
"""get the url that's the exact video we want from a link with multiple results"""
try:
url_portion = html.split('" title="' + vid_id + ' ')[0].split('><a href=".')[1]
return "http://www.javlibrary.com/en" + url_portion
except:
return None |
def make_wheel_filename_generic(wheel):
""" Wheel filenames contain the python version and the python ABI version
for the wheel. https://www.python.org/dev/peps/pep-0427/#file-name-convention
Since we're distributing a rust binary this doesn't matter for us ... """
name, version, python, abi, platform =... |
def concatenate_codelines(codelines):
""" Compresses a list of strings into one string. """
codeline = ""
for l in codelines:
codeline = codeline + l
return codeline |
def is_letter(char_code):
"""Return True if char_code is a letter character code from the ASCII
table. Otherwise return False.
"""
if isinstance(char_code, str) or isinstance(char_code, bytes):
char_code = ord(char_code)
if char_code >= 65 and char_code <= 90: # uppercase letters
... |
def reduce_names(l):
"""Reduce the names in the list to acronyms, if possible.
Args:
l (list(str)): list of names to convert.
Returns:
(list(str)): list of converted names.
"""
for i, item in enumerate(l):
if item == 'QuadraticDiscriminantAnalysis':
l[i] = 'QDA'... |
def cropRegion(coord, topCrop=0, bottomCrop=0, leftCrop=0, rightCrop=0):
"""crops a region defined by two coordinates"""
w = coord[1][0]-coord[0][0] # x2 - x1
h = coord[1][1]-coord[0][1] # y2 - y1
y1 = coord[0][1] + topCrop * h # y1 = y1 + topCrop * h
y2 = coord[1][1] - bottomCrop * h # y2 = y... |
def intensity_perpendicular_from_monomer(M, A_1, A_2, b, c_b_1):
"""
Calculate perpendicular intesity from monomer fraction, monomer and dimer
anisotropy, brightness relation and monomer brightness.
"""
return (1/3)*(((1-A_1) - (1-A_2)*b) *M + (1-A_2)*b)*c_b_1 |
def slash_esc(string):
"""
:type string: str
:rtype: str
"""
return string.replace("/", r"\/") |
def format_list(list1, fmt = '%16s', delimiter = ","):
"""
format list of numbers to string.
delimiter defaults = ','
"""
string1 = delimiter.join(fmt % h for h in list1) + '\n'
return string1 |
def restricang(thtg):
"""
0 to 2pi range restriction of angle
"""
while(thtg<0):
if thtg<0:
thtg = 6.28+thtg
else:
break
while(thtg>6.28):
if thtg>6.28:
thtg = thtg - 6.28
else:
break
return thtg |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.