content stringlengths 42 6.51k |
|---|
def sort_resources(resources):
"""Sort resources for inclusion on web page.
A number of rules are followed:
* resources are always grouped per renderer (.js, .css, etc)
* resources that depend on other resources are sorted later
* resources are grouped by library, if the dependencies allow it
... |
def carreau_model(shear_rate, mu_0, mu_inf, lambda_, n):
"""
Estimates the Carreau model for viscosity.
Used for fitting data using scipy.
Parameters
----------
shear_rate : array-like
The experimental shear rate data, with units of 1/s.
mu_0 : float
The estimated viscosity... |
def tts_ip_address(ip_address):
"""Convert an IP address to something the TTS will pronounce correctly.
Args:
ip_address (str): The IP address, e.g. '102.168.0.102'
Returns:
str: A pronounceable IP address, e.g. '192 dot 168 dot 0 dot 102'
"""
return ip_address.replace('.', ' dot '... |
def cut_tag_preserve(tags, tag):
"""
Cuts a tag from a list of tags without altering the original.
"""
tag_list = tags[:]
tag_list.remove(tag)
return ",".join(tag_list) |
def _split_data(data):
"""Split the keys of the data between 'properties' and 'data'
data: list (or None)
Result of a SQL query
Returns
-------
dict
"""
if data is None:
return data
data = data[0]
properties = ["iris", "city", "citycode", "label"]
result = {k: d... |
def hideCode(notebook):
"""
Finds the tags '#!--' and '#--! in each cell and removes
the lines in between.
Returns dict
"""
for i, cell in enumerate(notebook['cells']):
istart = 0
istop = -1
for idx, line in enumerate(cell['source']):
if '#!--' in line:
... |
def add_saturate(a, b, upper_bound, lower_bound):
"""
Returns the saturated result of an addition of two values a and b
Parameters
----------
a : Integer
First summand
b : Integer
Second summand
upper_bound : Integer
Upper bound fo... |
def _extract_items(json_dict: dict):
"""Return the items key if it exists, otherwise the top level."""
return json_dict.get("items", json_dict) |
def iterative(n):
"""Returns the n-th number of fib"""
a, b = 0, 1
if n == 0:
return a
else:
for _ in range(n):
a, b = b, a + b
return a |
def jaccard_similarity(str_a, str_b):
"""
Calculate jaccard similarity
Returns: jaccard score
"""
set1 = set(str_a.decode('utf8'))
set2 = set(str_b.decode('utf8'))
if len(set1) == 0 or len(set2) == 0:
return 0
return len(set1 & set2) / float(len(set1 | set2)) |
def sum_of_odd_integers(start, end):
"""
Counts the sum of all odd integers from start through end, inclusively.
Args:
start (int): starting number in range.
end (int): ending number in range.
Returns:
int: the sum of all odd integers from start through end, inclusively.
""... |
def backwards(inputString):
"""
Paramters
---------
inputString : string
Returns
-------
string
String with each space-separated word in inputString reversed
"""
wordList = inputString.split(" ") #list of space-separated words in inputString
backwardsList = [] #list ... |
def _text_file_to_string(reader, content_if_empty):
"""Load text file and convert it to string for display."""
if reader:
return reader.read().decode('utf-8')
else:
return content_if_empty |
def add_txt(string):
"""Ensures a filename ends in '.txt'."""
if not string.endswith(".txt"):
string += ".txt"
return string |
def not_the_same(user, other_user):
"""two users are not the same if they have different ids"""
return user["id"] != other_user["id"] |
def make_model_tuple(model):
"""
Take a model or a string of the form "app_label.ModelName" and return a
corresponding ("app_label", "modelname") tuple. If a tuple is passed in,
assume it's a valid model tuple already and return it unchanged.
"""
try:
if isinstance(model, tuple):
... |
def add_to_history(ll, lv):
"""Appends values from a list of values to a list from a list of lists.
Parameters
----------
ll : list
List of lists to which values will be appended.
lv : list
List of values to append to lists.
Returns
-------
list
List of lists t... |
def check_insertion(nwork, info, citation_var, citation_file, should_add, ref=""):
"""Check info validity after executing snowballing.create_info_code
Return dictionary of warning fields
Default: returns place1 if place is not defined and always returns the pdf name
"""
result = {}
result["pdf... |
def add_values_dict( d:dict ) ->float:
"""
Returns a float.
Add all the values inside a dict.
"""
total:float = 0.0
for k in d:
total += d[k]
return total |
def convertCIDRtoNetmask(cidr):
"""
:param cidr:
:return: Netmask length CIDR Notation. It's only "how many 1 there are"
"""
print("[ip - convertNetmaskToCIDR] Receive - ", cidr, "|")
bits = 0
for i in range(32-int(cidr), 32):
bits |= (1 << i)
return "%d.%d.%d.%d" % ((bits & ... |
def _iou(box1, box2, precision=1e-5):
"""
Calculate the Intersection over Union value for 2 bounding boxes
:param box1: array of 4 values
(top left and bottom right coords): [x0, y0, x1, x2]
:param box2: same as box1
:param precision: calculate precision for calculating
:return: IoU
"""... |
def adjustSize(imageSize, targetSize):
"""-> the size (x,y) the image must be scaled to in order to fit into the
target size, while keeping the aspect ratio.
"""
sx, sy = imageSize
tx, ty = targetSize
x = tx
y = sy * tx / sx
if y > ty:
y = ty
x = sx * ty / sy
return x... |
def miles_per_gallon(start_miles, end_miles, gallons):
"""Compute and return the average number of miles
that a vehicle traveled per gallon of fuel.
start_miles and end_miles are odometer readings in miles.
gallons is a fuel amount in U.S. gallons.
"""
mpg = abs(end_miles - start_miles) / gallon... |
def bitmap_is_set(bitmap: bytes, i: int) -> bool:
"""Returns True if bitmap is set at the i'th bit."""
assert (i >> 3) < len(bitmap), f"{i} out of range ({len(bitmap)})"
return bool(bitmap[i >> 3] & (0x80 >> (i & 7))) |
def _function7(value):
"""function7(value)"""
if value in [0, 2]:
out = 0
elif value in [1, 3]:
out = 1
else:
raise RuntimeError(value)
return out |
def solution2(n):
"""
This function returns only the largest prime factor.
"""
i = 2
while i * i <= n:
if n % i:
i += 1
else:
n //= i
return n |
def remove_duplicates(list_with_duplicates):
"""
Removes the duplicates and keeps the ordering of the original list.
For duplicates, the first occurrence is kept and the later occurrences are ignored.
Args:
list_with_duplicates: list that possibly contains duplicates
Returns:
A list ... |
def unify_lookup(lookup, results):
""" Create a unified serializable list from lookup and results. """
unified_list = []
for key in results.keys():
if key not in ('score', 'max_score'):
created_testbench = {'title': lookup[key][0],
'rule-id': key,
... |
def ShellEscape(s):
"""Escape a string to be passed to the shell.
Args:
s: String to escape.
Returns:
Escaped string.
"""
return "'" + s.replace("'", "'\\''") + "'" |
def get_cos_instances(resource_instances):
"""return available cos instances by name and id"""
storage_instances = []
for resource in resource_instances:
if 'cloud-object-storage' in resource['id']:
storage_instances.append({"name": resource['name'], "id": resource['id']})
return st... |
def valid_name(file_name):
"""determines that that the passed file_name
is valid, mainly making sure that it contains
no periods to prevent extension issues
"""
if '.' in file_name:
raise NameError("Name must not contain periods")
else:
return True |
def finds(itemlist, vec):
"""return the index of the first occurence of item in vec"""
idlist = []
for x in itemlist:
ix = -1
for i in range(len(vec)):
if x == vec[i]:
idlist.append(i)
ix = i
if ix == -1:
idlist.append(-1)
i... |
def build_transcript(transcript, build='37'):
"""Build a transcript object
These represents the transcripts that are parsed from the VCF, not
the transcript definitions that are collected from ensembl.
Args:
transcript(dict): Parsed transcript information
Returns:
... |
def get_first_aligned_bp_index(alignment_seq):
"""
Given an alignment string, return the index of the first aligned,
i.e. non-gap position (0-indexed!).
Args:
alignment_seq (string): String of aligned sequence, consisting of
gaps ('-') and non-gap characters, such as "HA-LO" or "---... |
def _config_info(mode, config):
"""Generate info about the config."""
return {
"mode": mode,
"resources": len(config.get("resources", [])),
"views": len(config.get("views", [])),
} |
def camel_case(text):
"""Camel-cases text.
Parameters
----------
text : str
Text to be camel-cased.
Returns
-------
str
A camel-cased string.
"""
return ''.join(text.title().split()) |
def stringify_sdg_number(sdg):
"""Converts integer to string and zero pads to 2 digits. Used for saving
and loading individual goal data.
Args:
sdg (int): Typically 1 - 17
Returns:
(str): e.g. '06'
"""
return str(sdg).zfill(2) |
def sizeof_fmt(num, suffix='B'):
"""Get bit number and return formatted size string"""
for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix) |
def process_param(args):#{{{
""" Parse command-line parameters
Some of them control the simulation, but all remaining will be passed to the model
"""
sim_param = { 'frequency_domain':False,
'frequency': None,
'MaxIter': 5000,
'... |
def get_dict_attrib(in_dict, key, default=None):
"""Attempt to retrieve attribute from dictionary"""
try:
return in_dict[key]
except KeyError:
return default |
def bool_to_bytes(val):
"""
Encode boolean to a 1 byte array
:param val: boolean value
:return: a bytes object
"""
if bool(val):
return b'\x01'
else:
return b'\x00' |
def __build_where_clause(where_columns_dictionary):
"""
Builds the where portion of the SQL query to account for None values
: :param where_columns_dictionary: the column name as a key and the value the column you are searching for in a
dictionary format (to select the specific row to update)
(i.e.... |
def minPathSum(grid):
"""
https://leetcode-cn.com/problems/minimum-path-sum/
:return:
"""
n = len(grid)
m = len(grid[0])
for i in range(n):
for j in range(m):
if i == 0 and j == 0:
continue
tp = 1e9
if i > 0:
tp = mi... |
def quality_to_factor(quality):
""" Calculate factor corresponding to quality
Args:
quality(float): Quality for jpeg compression.
Returns:
float: Compression factor.
"""
if quality < 50:
quality = 5000. / quality
else:
quality = 200. - quality * 2
return qua... |
def kwargs_to_tuple(d):
"""Convert expectation configuration kwargs to a canonical tuple."""
if isinstance(d, list):
return tuple(kwargs_to_tuple(v) for v in sorted(d))
elif isinstance(d, dict):
return tuple(
(k, kwargs_to_tuple(v))
for k, v in sorted(d.items())
... |
def get_xy_coord(n):
"""compute the xy coordinate given an index"""
if n < 1:
raise ValueError
elif n == 1:
return 0, 0
elif n == 2:
return 1, 0
else:
x, y = get_xy_coord(n-1)
if abs(x) > abs(y):
# spiral is going up the right side or down the left... |
def pruned_model_filename(sparsity, block_size):
"""Produces a human-readable name including sparsity parameters."""
return 'pruned_model_sparsity_%.2f_block_%s.tflite' % (
sparsity, '%dx%d' % block_size) |
def convert_facet_list_to_dict(facet_list: list, reverse: bool = False) -> dict:
"""
Solr returns search facet results in the form of an alternating list. Convert the list into a dictionary key
on the facet
:param facet_list: facet list returned by Solr
:param reverse: boolean flag indicating if the... |
def _get_dn_from_ldap_payload(payload):
"""
Returns the distinguished_name from the payload retrieved from rethinkDb
"""
return payload["distinguishedName"] |
def s3_fmt_range(range):
""" None -> None
(in, out) -> "bytes={in}-{out-1}"
"""
if range is None:
return None
_in, _out = range
return 'bytes={:d}-{:d}'.format(_in, _out-1) |
def find_in_list_by_value(search_value, search_list,
first_occurrence_only=True):
"""Find item(s) that match(es) the search value from the search list."""
valid = isinstance(search_list, list)
if not valid:
raise ValueError("Invalid argument was passed.")
if not search... |
def constraint_class(kind):
"""
The convention is that name of the class implementing the constraint
has a simple relationship to the constraint kind, namely that
a constraint whose kind is 'this_kind' is implemented by a class
called ThisKindConstraint.
So:
``min`` --> ``MinConst... |
def scale_to_internal(vec, scaling_factor, scaling_offset):
"""Scale a parameter vector from external scale to internal one.
Args:
vec (np.ndarray): Internal parameter vector with external scale.
scaling_factor (np.ndarray or None): If None, no scaling factor is used.
scaling_offset (np... |
def cr_validation(value):
"""
:param value: charge rate value given by the system
:return: Boolean
"""
max_value = 0.8
return True if (value < max_value) else False |
def blendcolors(rgb1, rgb2, prop):
"""
Blends two rgb colors so that prop comes from rgb2 and (1-prop) from rgb1,
where prop should be a proportion in the interval 0.0 - 1.0.
"""
return tuple(map(lambda c1, c2: int(round(c1 + (c2 - c1) * prop)), rgb1, rgb2)) |
def IsKillStep(chessboard: list, mv: list) -> bool:
"""
chessboard: current chessboard info [[x, y, class], [], []...]
mv: AI move info, [x_src, y_src, x_dst, y_dst]
return:
BOOL: true if this step is kill step, false if normal step.
"""
for pc in chessboard:
if mv[2] == pc[0] a... |
def is_librarys_flow_cell_owner(user, library):
"""Whether or not is owner of the given flow cell's library'"""
if not library or not library.flow_cell:
return False
else:
return library.flow_cell.owner == user |
def look_and_say(numbers):
"""
Performs a look'n'say iteration. Repeated digits are collapsed into one and preceeded by their amount.
Add 1 before each single digit. '111' -> '31'
:param numbers: string of digits
:return: look'n'say op over digits
"""
digit = ""
result = ""
count = 0... |
def create_graph(network):
"""Create a graph of train lines
Args:
network(list): A list of dictionaries of lines and stations in the line
Returns:
graph(dictionary): A graph of train stations
"""
graph = {}
line = 0
i = 0
while line < len(network):
while i <len... |
def _parse(filename):
"""Parse reference data filename to extract land cover and year."""
basename = filename.split('.')[0]
land_cover, year = basename.split('_')
year = int(year)
return land_cover, year |
def ptoc(width, height, x, y, zxoff, zyoff, zoom):
"""
Converts actual pixel coordinates to complex space coordinates
(zxoff, zyoff are always the complex offsets).
"""
zx_coord = zxoff + ((width / height) * (x - width / 2) / (zoom * width / 2))
zy_coord = zyoff + (-1 * (y - height / 2) / (zoom... |
def get_old_asg_instances(asg_object):
"""Gets instances from ASG that are not using the correct launch configuration"""
all_instances = []
instances = asg_object['AutoScalingGroups'][0]['Instances']
for instance in instances:
if 'LaunchConfigurationName' not in instance:
all_instanc... |
def cut_trailing_quotes(text: str) -> str:
"""Cut trailing quotes into one quote.
Args:
text: Input text.
Returns:
Text up to the dangling double quote.
"""
num_quotes = text.count('"')
if num_quotes == 1:
return text.replace('"', "")
elif num_quotes % 2 == 0:
... |
def combinations_construct(tree_config_path, path=["root"]):
"""
>>> combinations_construct({"root": ["target", "target teams"],
... "target": ["teams"],
... "target teams": [], "teams": []})
[['target'], ['target', 'teams'], ['target teams']]
""... |
def multi_split(s, split):
"""Splits on multiple given separators."""
for r in split:
s = s.replace(r, '|')
return [i for i in s.split('|') if len(i) > 0] |
def get_gitlab_scripts(data):
"""GitLab is nice, as far as I can tell its files have a
flat hierarchy with many small job entities"""
def flatten_nested_string_lists(data):
"""helper function"""
if isinstance(data, str):
return data
elif isinstance(data, list):
... |
def _mark_untranslated_strings(translation_dict):
"""Marks all untranslated keys as untranslated by surrounding them with
lte and gte symbols.
This function modifies the translation dictionary passed into it in-place
and then returns it.
"""
# This was a requirement when burton was written, but... |
def expand_qgrams(text, qsize, output):
"""Expands a text into a set of q-grams"""
n = len(text)
for start in range(n - qsize + 1):
output.append(text[start:start+qsize])
return output |
def erlangb(load, c):
"""
Return the the probability of loss in M/G/c/c system using recursive approach.
Much faster than direct computation via
scipy.stats.poisson.pmf(c, load) / scipy.stats.poisson.cdf(c, load)
Parameters
----------
load : float
average arrival rate * average ser... |
def _add_first_child_to_dic(base, dic, opt, tag):
"""Adds the first child element with a given tag to a dictionary.
Args:
base: The base node element. (lxml node)
dic: The dictionary to add the element to (dictionary)
opt: If false and id is not found in base, the element is added with ... |
def _add_path(root_path, relative_path):
"""Add another level to an LDAP path.
eg,
_add_path ('LDAP://DC=gb,DC=vo,DC=local', "cn=Users")
=> "LDAP://cn=users,DC=gb,DC=vo,DC=local"
"""
protocol = "LDAP://"
if relative_path.startswith(protocol):
return relative_path
if root_... |
def get_byte_size(row, delimiter, line_terminator):
"""Get the byte size of this row split by this delimiter."""
line = delimiter.join(row) + line_terminator
return len(line.encode("utf-8")) |
def descendants_query(node_id, node_label, edge_label="edge"):
"""Generate Cypher query finding descendant nodes starting at 'node_id'."""
return (
"MATCH path=(n:{} {{id: '{}'}})-[:{}*1..]->(m:{})\n".format(
node_label, node_id, edge_label, node_label) +
"RETURN m.id AS descendant, ... |
def contains(seq, value):
"""
Description
----------
Checks to see if a value is in the sequence or dictionary.
Parameters
----------
seq : (list or tuple or set or dict or string) - sequence/dictionary to search in\n
value : any - value to search for
Returns
----------
boo... |
def take_ingredients(request):
"""
We use this function to get ingredients from
our request when creating / editing recipes.
"""
ingredients = {}
for key in request:
if key.startswith('nameIngredient_'):
num = key[15:]
if num.isdecimal():
ingredien... |
def state_probabilities(endclasses):
"""
Tabulates the probabilities of different states in endclasses.
Parameters
----------
endclasses : dict
Dictionary of end-state classifications 'classification' and 'prob' attributes
Returns
-------
probabilities : dict
Dictionary... |
def break_down_filename(image):
"""
filename: g04i2c08.png
|| ||||
test feature (in this case gamma) ------+| ||||
parameter of test (here gamma-value) ----+ ||||
interlaced or non-interlaced --------------+|||
color-type ... |
def message_from_lax(data):
"""
format a message from a Lax response data
"""
return data.get("message") if data.get("message") else "(empty message)" |
def _validate_bool(s):
"""
A validation method to convert input s to boolean or raise error if it is not convertable
"""
try:
return bool(s)
except ValueError:
raise ValueError('Could not convert input to boolean') |
def extract_text(element):
"""
Return *text* attribute of an implied element if available.
"""
if element is not None:
return element.text
else:
return None |
def get_event_ends(T_part, n_repeats):
"""get the end points for a event sequence, with lenth T, and k repeats
- event ends need to be removed for prediction accuracy calculation, since
there is nothing to predict there
- event boundaries are defined by these values
Parameters
----------
T_... |
def wavelength(fringe_spacing, slits_distance, screen_distance):
"""
This function provides the wavelength of light
when the following parameters are provided.
===========================================
Parameters:
fringe_spacing : Fringe spacing
slits_distance : Distance between two sli... |
def is_indexed(value):
"""
Checks if `value` is integer indexed, i.e., ``list``, ``str`` or ``tuple``.
Args:
value (mixed): Value to check.
Returns:
bool: Whether `value` is integer indexed.
Example:
>>> is_indexed('')
True
>>> is_indexed([])
True
... |
def Probability(o):
"""Computes the probability corresponding to given odds.
Example: o=2 means 2:1 odds in favor, or 2/3 probability
o: float odds, strictly positive
Returns: float probability
"""
return o / (o + 1) |
def ParseIssueNames(names):
"""Because comics use the same upc for the whole series now parse out the series name from
the returned list
Noteable items that come after the series name: "issue #" and "#"
There is probably a better way to do this but it seems to work in most cases.
name... |
def is_full_section(section):
"""Is this section affected by "config.py full" and friends?"""
return section.endswith('support') or section.endswith('modules') |
def applianceLogsFileName(config):
"""
Returns the filename of the log zip file created by calling
syscfg.zipApplianceLogs()
"""
return config['converter.appliance_logs_name'] |
def _ascii_decode(ascii: bytes) -> str:
"""Decode bytes of ASCII charactors to string.
Args:
ascii (bytes): ASCII charactors
Returns:
str: Converted string
"""
return ascii.decode("ascii", "replace").replace("\x00", "") |
def _compute_char_shifts(tokens):
"""Compute the shifts in characters that occur when comparing the
tokens string with the string consisting of all tokens separated with a
space
For instance, if "hello?world" is tokenized in ["hello", "?", "world"],
then the character shifts between "hello?world" a... |
def get_cycle_stats(data_list):
"""
Calculates cycle statistics for test run.
Returns min, max, avg cycle count.
"""
cycles = [data[0] for data in data_list]
min_cycles = min(cycles)
max_cycles = max(cycles)
avg_cycles = sum(cycles) / len(cycles)
return min_cycles, max_cycles, avg_cy... |
def flatten(iterable):
"""Recursively denest iterable containers.
>>> flatten([1, 2, 3])
[1, 2, 3]
>>> flatten([1, 2, [3]])
[1, 2, 3]
>>> flatten([1, [2, 3], [4, 5]])
[1, 2, 3, 4, 5]
"""
result = []
for item in iterable:
if isinstance(item, list):
... |
def write_node(doc, parent, name, value, attr=None):
"""
:param doc: document DOM
:param parent: parent node
:param name: tag of the element
:param value: value of the child text node
:param attr: attribute dictionary
:return: True if something was appended, otherwise False
"""
if a... |
def max_length(choices):
"""
Returns the size of the longest choice.
:param: the available choice strings or tuples
:return: the maximum length
"""
def length(item):
if isinstance(item, str):
value = item
else:
value = item[0]
return len(value... |
def get_io_from_prov_json(prov_json):
"""Identify input and output files from provenance JSON
Parameters
----------
prov_json : OrderedDict
ordered dictionary generated from json prov file using python's json module
i.e. json.load(path_to_json_file, object_pairs_hook=Orde... |
def find_missing_int_using_sum(complete, incomplete):
"""
Problem: There are distinct integers in list `complete`. The same integers are in list `incomplete`, except for one.
Task: Find the one integer which is missing from the incomplete list.
Complexity: O(n) time, O(1) space
"""
s = 0
... |
def min_index(items):
"""Returns index of the smallest item.
items can be any sequence. If there is a tie, returns earliest item"""
return min([(item, index) for index, item in enumerate(items)])[1] |
def parse_http_protocol(s):
"""
Parse an HTTP protocol declaration. Returns a (major, minor) tuple, or
None.
"""
if not s.startswith(b"HTTP/"):
return None
_, version = s.split(b'/', 1)
if b"." not in version:
return None
major, minor = version.split(b'.', 1)
... |
def format_desc(desc="", available_values=[]):
"""
"""
output_str = ""
output_str += desc
if len(available_values) == 0:
return output_str
output_str += "\nExplain the meaning of the parameter:"
for a_value in available_values:
meaning, value = a_value
output_str... |
def extract_action_list_from_path(path_list):
"""
Converts a block idx path to action list.
Args
path_list: <list> list of block idx from source block to dest block.
Returns
action_list: <list> list of string discrete action commands (e.g. ['movesouth 1', 'movewest 1', ...]
... |
def author(context, request):
""" Handles author's profile """
return {'author': context} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.