content stringlengths 42 6.51k |
|---|
def get_manual_finding_report_detail(manual_finding_reports):
"""
Iterate over manual finding report detail from response.
:param manual_finding_reports: manual finding report detail from the response
:return: List of manual finding report elements.
"""
return [{
'ID': manual_finding_re... |
def find_root_domain(soa):
"""
It is nessicary to know which domain is at the top of a zone. This
function returns that domain.
:param soa: A zone's :class:`SOA` object.
:type soa: :class:`SOA`
The following code is an example of how to call this function using
a Domain as ``domain``.
... |
def ints(int_list):
"""coerce a list of strings that represent integers into a list of integers"""
return [ int(number) for number in int_list ] |
def jaro_similarity(s1, s2):
"""
Computes the Jaro similarity between 2 sequences from:
Matthew A. Jaro (1989). Advances in record linkage methodology
as applied to the 1985 census of Tampa Florida. Journal of the
American Statistical Association. 84 (406): 414-20.
The Jaro distance... |
def merge_nodes(a, b):
"""Recursively and non-destructively merges two nodes. Returns the newly
created node.
"""
if a is None:
return b
if b is None:
return a
if a[0] > b[0]:
a, b = b, a
return a[0], merge_nodes(b, a[2]), a[1] |
def reverse_loop(value):
"""Reverse string using a loop."""
result = ""
for char in value:
result = char + result
return result |
def perm(n, k):
"""Return P(n, k), the number of permutations of length k drawn from n
choices.
"""
result = 1
assert k > 0
while k:
result *= n
n -= 1
k -= 1
return result |
def checklen(astring):
"""
(str) -> Boolean
Returns true if length of astring is at least 5 characters long, else False
>>> checklen('', 1)
False
>>> checklen('four', 5)
False
>>> checklen('check', 5)
True
>>> checklen('check6', 6)
True
"""
return len(astring) >= 5 |
def dummy_address_check( address ):
"""
Determines if values contain dummy addresses
Args:
address: Dictionary, list, or string containing addresses
Returns:
True if any of the data contains a dummy address; False otherwise
"""
dummy_addresses = [ "", "0.0.0.0", "::" ]
if... |
def process_builder_convert(data, test_name):
"""Converts 'test_name' to run on Swarming in 'data'.
Returns True if 'test_name' was found.
"""
result = False
for test in data['gtest_tests']:
if test['test'] != test_name:
continue
test.setdefault('swarming', {})
if not test['swarming'].get('... |
def hosoya(height, width):
""" Calculates the hosoya triangle
height -- height of the triangle
"""
if (width == 0) and (height in (0,1)):
return 1
if (width == 1) and (height in (1,2)):
return 1
if height > width:
return hosoya(height - 1, width) + hosoya(height - 2, wid... |
def get_domains(resolution, frequency, variable_fixed, no_frequencies):
"""Get the domains for the arguments provided.
Args:
resolution (int): Resolution
frequency (str): Frequency
variable_fixed (bool): Is the variable fixed?
no_frequencies (bool): True if no frequencies were p... |
def filter_keyphrases_brat(raw_ann):
"""Receive raw content in brat format and return keyphrases"""
filter_keyphrases = map(lambda t: t.split("\t"),
filter(lambda t: t[:1] == "T",
raw_ann.split("\n")))
keyphrases = {}
for keyphrase in filter... |
def formatter(n):
"""formatter for venn diagram, so it can be easily turned off."""
#if you want it to be there
return f"{n:.02f}" |
def ctext(text, colour="green"):
"""Colour some terminal output"""
# colours
c = {
"off": "\033[0m",
# High Intensity
"black": "\033[0;90m",
"bl": "\033[0;90m",
"red": "\033[0;91m",
"r": "\033[0;91m",
"green": "\033[0;92m",
"g": "\033[0;92m",... |
def deleteFirstRow(array):
"""Deletes the first row of a 2D array.
It returns a copy of the new array"""
array = array[1::]
return array |
def user_name_for(name):
""" Returns a "user-friendly" name for a specified trait.
"""
name = name.replace('_', ' ')
name = name[:1].upper() + name[1:]
result = ''
last_lower = 0
for c in name:
if c.isupper() and last_lower:
result += ' '
last_lower = c.islower()
... |
def _verify_type(val, default, the_type, name, instance):
"""
Validate that the input is an instance of the provided type.
Parameters
----------
val
The prospective value.
default : None|float
The default value.
the_type : Type
The desired type for the value.
nam... |
def pointInPolygon(pt, poly, bbox=None):
"""Returns `True` if the point is inside the polygon.
If `bbox` is passed in (as ``(x0,y0,x1,y1)``), that's used for a quick check first.
Main code adapted from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
"""
x, y = pt
if bbox:
... |
def deleteRules(parentRule,ruleName):
"""
Function to fetch all delete rules matching a ruleName
Parameters
----------
ruleName : <List>
Default parent rule represented as a list
Returns
-------
parentRule : Updated Rule tree
"""
if parentRule[0]['name'] == ruleName:
... |
def get_row_col(num_pic: int):
"""
get figure row and column number
"""
sqr = num_pic ** 0.5
row = round(sqr)
col = row + 1 if sqr - row > 0 else row
return row, col |
def safe_division_d(number, divisor, **kwargs):
"""
safe_division_d
:param number:
:param divisor:
:param kwargs:
:return:
"""
# ignore_overflow = kwargs.pop('ignore_overflow', False)
# ignore_zero_div = kwargs.pop('ignore_zero_division', False)
if kwargs:
raise TypeError... |
def _parse_ref_dict(reference_dict, strict=True):
"""Parse the referenced dict into a tuple (TYPE, ID).
The ``strict`` parameter controls if the number of keys in the
reference dict is checked strictly or not.
"""
keys = list(reference_dict.keys())
if strict and len(keys) != 1:
raise V... |
def clean_texmath(txt):
"""
clean tex math string, preserving control sequences
(incluing \n, so also '\nu') inside $ $, while allowing
\n and \t to be meaningful in the text string
"""
s = "%s " % txt
out = []
i = 0
while i < len(s)-1:
if s[i] == '\\' and s[i+1] in ('n', 't'... |
def collect_all_methods(cls, method_name):
"""Return list of all `method_name` methods for cls and its superclass chain.
List is in MRO order, with no duplicates. Methods are unbound.
(This is used to simplify mixins and subclasses that contribute to a method set,
without requiring superclass chaining... |
def cobs_encode(data):
"""COBS-Encode bytes.
:param data: input bytes
:return: cobs-encoded bytearray
"""
out = bytearray(len(data) + 1 + (len(data) // 254))
ci = ri = 0
c = wi = 1
while ri < len(data):
if not data[ri]:
out[ci] = c
c = 1
ci = ... |
def _stats_source(year):
"""Returns the path to the stats source file of the given year."""
return f'stats/stats.{year}.txt' |
def replace_in_list(my_list, idx, element):
"""
Replaces an element in a list at given index
"""
list_len = len(my_list)
if list_len <= idx or idx < 0:
return (my_list)
my_list[idx] = element
return (my_list) |
def rake_to_mech(rake):
"""
Convert rake to mechanism.
Args:
rake (float): Rake angle in degrees.
Returns:
str: Mechanism.
"""
mech = 'ALL'
if rake is not None:
if (rake >= -180 and rake <= -150) or \
(rake >= -30 and rake <= 30) or \
(rake >= ... |
def sub_binary_search(sorted_array, test_value, low, high):
""" run through sorted_array and look for test_value.
if test_value is in sorted_array, return index for test_value
in sorted_array. If test_value is not in sorted_array, return
-1 """
if low > high:
False
else:
mid = (l... |
def fisbHexErrsToStr(hexErrs):
"""
Given an list containing error entries for each FIS-B block, return a
string representing the errors. This will appear as a comment in either
the result string, or the failed error message.
Args:
hexErrs (list): List of 6 items, one for each FIS-B block. Each entry
... |
def remove_comment(line, marker="##"):
"""Return the given line, without the part which follows the comment
marker ## (and without the marker itself)."""
i = line.find(marker)
if i < 0:
return line
else:
return line[:i] |
def config_from_defaults(struct: tuple) -> dict:
"""Return dict from defaults."""
return {x: y for x, _, y in struct} |
def getOverlapSetSim(concepts_1: set, concepts_2: set):
""" Returns Overlap Set Similarity for the given concept sets """
intersection = len(concepts_1.intersection(concepts_2))
return intersection/min(len(concepts_1),len(concepts_2)) |
def time_convert(input_time):
"""
Convert input time from sec to MM,SS format.
:param input_time: input time in sec
:type input_time: float
:return: converted time as str
"""
sec = float(input_time)
_days, sec = divmod(sec, 24 * 3600)
_hours, sec = divmod(sec, 3600)
minutes, sec... |
def keep_aa(attentions):
""" Last minute change: transfer over the network is very slow. Need to drop keys from the JSON to make rendering faster """
aa = attentions['aa']
out = {'aa': aa}
return out |
def welcome(location):
"""Takes the input string welcome and returns a string of the form
'Welcome to the location'
"""
return "Welcome to the " + location |
def package_name(package):
# type: (str) -> str
"""
Returns the package name of the given module name
"""
if not package:
return ""
lastdot = package.rfind(".")
if lastdot == -1:
return package
return package[:lastdot] |
def concatenate_title_and_text(title, text):
"""Concatenates title and content of an article in the same string.
The two parts are separated by a blank space.
:param title: The tile of an article
:type title: str
:param text: The text content of an article
:type text: str
:return: The strin... |
def _format_kwargs(kwargs):
"""Returns a dictionary as key value pairs to be used in tags
Usage:
>>> _format_kwargs({a:1, b:"2"})
a="1" b="2"
"""
element_as_str = []
for key, value in kwargs.items():
element_as_str.append('{}="{}"'.format(key, value))
return ' '... |
def __makenumber(value):
"""
Helper function to change the poorly formatted numbers to floats
Examples:
> value is an integer / float data type -> float type returned
> value = '1,000', then the float value is 1000
> value = '1 000 000.00' then the float value is 1000000
... |
def get_content_length(environ):
"""Returns the content length from the WSGI environment as
integer. If it's not available or chunked transfer encoding is used,
``None`` is returned.
.. versionadded:: 0.9
:param environ: the WSGI environ to fetch the content length from.
"""
if environ.get... |
def get_prop(obj, prop, mytype='str'):
"""Get a property of a dict, for example device['uptime'], and handle None-values."""
if mytype == 'str':
if prop in obj:
if obj[prop] is not None:
return obj[prop].encode('utf-8')
return ''
else:
if prop in obj:
... |
def closeEnough(tolerance, length, string):
"""
tolerance: the tolerance of the string length
length: the target length
string: the string to evaluate
"""
if(abs(len(string) - length) <= tolerance):
return True
else:
return False |
def _requires_dist_to_pip_requirement(requires_dist):
"""Parse "Foo (v); python_version == '2.x'" from Requires-Dist
Returns pip-style appropriate for requirements.txt.
"""
env_mark = ''
if ';' in requires_dist:
name_version, env_mark = requires_dist.split(';', 1)
else:
name_ver... |
def binary_search(start, end, intervals):
"""Performs a binary search"""
start_search = 0
end_search = len(intervals)
while start_search < (end_search - 1):
mid = start_search + (end_search - start_search) // 2
(interval_start, interval_end) = intervals[mid]
if interval_end <= s... |
def enum_name(name):
"""Shorten an enumeration name."""
assert name.startswith('GL_')
return name[3:] |
def _replace_oov(original_vocab, line):
"""Replace out-of-vocab words with "<UNK>".
This maintains compatibility with published results.
Args:
original_vocab: a set of strings (The standard vocabulary for the dataset)
line: a unicode string - a space-delimited sequence of words.
Returns:... |
def speed_func(t_n):
"""
Returns the normalised velocity U(t)/U0.
"""
return min(1,t_n) |
def return_min(list_of_dims):
"""
Returns the dimensions that produce the minimum area.
In the event of a tie, will return the first match.
:param list_of_dims: A list of dimensions.
:return: The dimensions with the minimum area.
"""
return min(list_of_dims, key=lambda dim: dim[... |
def html_decode(s):
"""
Returns the ASCII decoded version of the given HTML string. This does
NOT remove normal HTML tags like <p>.
"""
htmlCodes = (
("'", '''),
('"', '"'),
('>', '>'),
('<', '<'),
('&', '&')
)
fo... |
def _normalize_encoding(encoding):
"""returns normalized name for <encoding>
see dist/src/Parser/tokenizer.c 'get_normal_name()'
for implementation details / reference
NOTE: for now, parser.suite() raises a MemoryError when
a bad encoding is used. (SF bug #979739)
"""
if encoding is ... |
def _true(*args):
"""
Default rerun filter function that always returns True.
"""
# pylint:disable=unused-argument
return True |
def get_calmag(inimag, distance, ebv_val, magext_val):
""" Apply the absorption and distance modulus
color = colsub * 3.07 * EBV
inimag -- magnitude from star star[chip[FILTER] (it should be = star[chip[BAND]]
distance -- Distance Modulus used to calculate the calibrated magnitude
ebv_va... |
def detokenize_text(src):
""" Join all tokens corresponding to single characters for creating the resulting text.
This function is reverse for the function `tokenize_text`.
:param src: source token list.
:return: the resulting text.
"""
new_text = u''
for cur_token in src.split():
... |
def _is_no_rec_name(info_name):
"""
helper method to see if we should not provide any recommendation
"""
if info_name == "last_boot_time":
return True |
def prod(x):
"""
Computes the product of the elements of an iterable
:param x: iterable
:type x: iterable
:return: product of the elements of x
"""
ret = 1
for item in x:
ret = item * ret
return ret |
def temporal_filter(start_date, end_date=None):
"""TemporalFilter data model.
Parameters
----------
start_date : str
ISO 8601 formatted date.
end_date : str, optional
ISO 8601 formatted date.
Returns
-------
temporal_filter : dict
TemporalFilter data model a... |
def inflate_dict(dct, sep=".", deep=-1):
"""Inflates a flattened dict.
Will look in simple dict of string key with string values to
create a dict containing sub dicts as values.
Samples are better than explanation:
>>> from pprint import pprint as pp
>>> pp(inflate_dict({'a.x': 3, 'a.... |
def config_shim(args):
"""Make new argument parsing method backwards compatible."""
if len(args) == 2 and args[1][0] != '-':
return ['--config-file', args[1]] |
def unpad(string):
"""
From: https://github.com/CharlesBlonde/libpurecoollink
Copyright 2017 Charles Blonde
Licensed under the Apache License
Un pad string."""
return string[:-ord(string[len(string) - 1:])] |
def pydiff(text1, text2, text1_name='text1', text2_name='text2',
prefix_diff_files='tmp_diff', n=3):
"""
Use Python's ``difflib`` module to compute the difference
between strings `text1` and `text2`.
Produce text and html diff in files with `prefix_diff_files`
as prefix. The `text1_name` ... |
def str2tuple(s, sep=',', converter=None, *, maxsplit=-1):
"""Convert a string to a tuple.
If ``converter`` is given and not ``None``, it must be a callable that
takes a string parameter and returns an object of the required type,
or else a tuple with string elements will be returned.
>>> str2tupl... |
def downcase(string):
"""Returns a copy of `string` with all the alphabetic characters converted
to lowercase.
:param string: string to downcase.
"""
return string.lower() |
def minmax(min_value, value, max_value):
"""
Restrict value to [min_value; max_value]
>>> minmax(-2, -3, 10)
-2
>>> minmax(-2, 27, 10)
10
>>> minmax(-2, 0, 10)
0
"""
return min(max(min_value, value), max_value) |
def makeSafeString( someString:str ) -> str:
"""
Replaces potentially unsafe characters in a string to make it safe for display.
"""
#return someString.replace('&','&').replace('<','<').replace('>','>')
return someString.replace('<','_LT_').replace('>','_GT_') |
def M_TO_N(m, n, e):
"""
match from m to n occurences of e
:param:
- `m`: the minimum required number of matches
- `n`: the maximum number of matches
- `e`: the expression to match
"""
return "{e}{{{m},{n}}}".format(m=m, n=n, e=e) |
def _convert_obj_ids_to_strings(data):
"""Convert ObjectIds to hexadecimal strings.
Takes a dictionary or a list of dictionaries of MongoDB BSON
documents. Transforms their ObjectIds into strings so the documents
are JSON serializable and the doc ids are easily accessed.
"""
if isinstance(data,... |
def flatten_meal_item(meal_item):
"""
Input: <meal_items> is in the form:
[
[category,title,extra],
...
]
Output: dictionaries, each one containing
a list of items w/extra text that fall into the
category.
{... |
def sumDigits(s):
"""
:Assumes s is a string:
:Returns the sum of the decimal digits in s:
:For example, if s is 'a2b3c' it returns 5:
"""
digits = []
for char in s:
try:
digits.append(int(char))
except ValueError:
pass
return sum(digits) |
def lr_schedule_adam(epoch):
"""Learning Rate Schedule
# Arguments
epoch (int): The number of epochs
# Returns
lr (float32): learning rate
"""
# for Adam Optimizer
lr = 1e-3
if epoch > 350:
lr = 1e-5
elif epoch > 300:
lr = 1e-4
elif epoch > 200:
... |
def index_name(i, j=None, k=None, l=None):
"""
Provides formatting for a name, given title and up to 3 indices.
Parameters:
title: string
i: string
j: string
k: string
l: string
Returns: string
"""
if i is not None and j is None and k is None and l is No... |
def calc_avg(varlist):
"""
Collecting the statistics for descriptives, including number of elements, number of functions,
number of variables, number of constants
:param varlist: list of dict of the variables
:return: total variables, average elements per equation, number of functions and average, ... |
def unpad(bytestring, k=16):
"""
Remove the PKCS#7 padding from a text bytestring.
"""
val = bytestring[-1]
if val > k:
raise ValueError("Input is not padded or padding is corrupt")
l = len(bytestring) - val
return bytestring[:l] |
def find_min_loc(L):
"""find min loc uses a loop to return the minimum of L
and the location (index or day) of that minimum.
Argument L: a nonempty list of numbers.
Results: the smallest value in L, its location (index)
"""
minval = L[0]
minloc = 0
for i in lis... |
def _format_tracestate(tracestate):
"""Parse a w3c tracestate header into a TraceState.
Args:
tracestate: the tracestate header to write
Returns:
A string that adheres to the w3c tracestate
header format.
"""
return ','.join(key + '=' + value for key, value in tracestate.it... |
def _running_locally(coreapi_url, jobs_api_url):
"""Check if tests are running locally."""
return not (coreapi_url and jobs_api_url) |
def merge_dicts(*args, **kwargs):
""" Merge dict into one dict """
final = {}
for element in args:
for key in element:
final[key] = element[key]
return final |
def empty_cache(max_T, labeling_with_blanks):
"""Create empty cache."""
return [[None for _ in range(len(labeling_with_blanks))] for _ in range(max_T)] |
def _score(estimator, x, y, scorers):
"""Return a dict of scores"""
scores = {}
for name, scorer in scorers.items():
score = scorer(estimator, x, y)
scores[name] = score
return scores |
def gcd(a, b):
"""
>>> gcd(3,6)
3
>>> gcd(10,15)
5
"""
if a == 0: return abs(b)
if b == 0: return abs(a)
if a < 0: a = -a
if b < 0: b = -b
while b:
c = a % b
a = b
b = c
return a |
def permute(string):
"""permute(str) -> str
Outputs a list of all possible permutations a string.
Note: If a character is repeated, each occurence as distinct.
>>> permute('abc')
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
>>> permute('dog')
['dog', 'dgo', 'odg', 'o... |
def reverse(s):
""" (str) -> str
Return a reversed version of s.
>>> reverse('hello')
'olleh'
>>> reverse('a')
'a'
"""
rev = ''
# For each character in s, add that char to the beginning of rev.
for ch in s:
rev = ch + rev
return rev |
def create_grid(locked_positions={}):
"""Creates the playfield's gridfield."""
grid = [[(0, 0, 0) for _ in range(10)] for _ in range(20)]
for i in range(len(grid)):
for j in range(len(grid[i])):
if (j, i) in locked_positions:
c = locked_positions[(j, i)]
... |
def int_or_none(x):
"""Given a value x it cast as int or None
:param x: The value to transform and return
:returns: Either None or x cast to an int
"""
if x is None:
return None
return int(x) |
def lambda_handler(event, context):
"""Lambda function which does no operation
Args:
event (dict): Parameter to pass in event data to the handler.
context (bootstrap.LambdaContext): Parameter to provide runtime information to the handler.
Returns:
json: A simple json object with tw... |
def get_role_name(account_id, role):
"""Shortcut to insert the `account_id` and `role` into the iam string."""
return "arn:aws:iam::{0}:role/{1}".format(account_id, role) |
def has_pythonX_package(pkg_name, name_by_version, version):
"""Given the package name, check if python<version>-<pkg_name>
or <pkg_name>-python<version> exists in name_by_version.
Return: (bool) True if such package name exists, False otherwise
"""
return (
'python{}-{}'.format(version, pk... |
def get_primes(start_value, end_value) -> list:
"""
:param start_value: interval start_value
:param end_value: interval end_value
:return: List of primes in the given range
"""
primes_list = []
for value in range(start_value, end_value + 1):
if value > 1:
for n in range... |
def constrain(x, lower, upper):
"""Limits the incoming value to
the given lower and upper limit."""
y = 0
if x > upper:
y = upper
elif x < lower:
y = lower
if x > 6500:
y = 0
else:
y = x
return y |
def gen_I(n):
"""Returns an nxn identity matrix."""
return [[min(x // y, y // x) for x in range(1, n + 1)] for y in range(1, n + 1)] |
def padr(text, n, c):
"""
padr - right pad of text with character c
"""
text = str(text)
return text + str(c) * (n - len(text)) |
def myfun(x, binary=True):
"""
fonction de seuillage
si > theta : 1
si < theta : min
sinon theta
"""
if binary:
_min, _theta = 0, .5
else:
_min, _theta = -1, 0
if x > _theta: return 1
if x == _theta: return _theta
return _min |
def convert_list_items(old_list: list, convert_type: type):
"""
Info:
Converts each list item to the type specified
Paramaters:
old_list: list - List to convert
convert_type: type - The type to convert to.
Usage:
convert_list_items(old_list, convert_type)
Retur... |
def find_column(header_row, pattern="rs"):
"""Find the first column in a row that matches a pattern."""
snp_columns = (index for index, column in enumerate(header_row) if column.startswith(pattern))
# return the first index
return next(snp_columns) |
def invalid_route(path):
"""Catches all invalid routes."""
response = {"success": False, "error": {"type": "RouteNotFoundError",
"message": "No such route"}}
return response, 404 |
def merge_outfiles(filelist, outfile_name):
""" merge the output from multiple BLAST runs of type 6 output
(no headers)
"""
# only grab .tab files, ie, the blast output
with open(outfile_name, "a") as outf:
for idx, f in enumerate(filelist):
with open(f, "r") as inf:
... |
def skip_mul(n):
"""Return the product of n * (n - 2) * (n - 4) * ...
>>> skip_mul(5) # 5 * 3 * 1
15
>>> skip_mul(8) # 8 * 6 * 4 * 2
384
"""
if n == 2:
return 2
elif n == 1:
return 1
else:
return n * skip_mul(n - 2) |
def _bool2str(b):
"""
Convert boolean to string
Used by XML serialization
"""
return "true" if b else "false" |
def count_empty_fields(cur_elem):
"""
Loop through the whole dict and count the number of fields that contain empty values,
such as empty lists or empty dicts
"""
empty_elems = 0
if type(cur_elem) is dict:
if len(cur_elem.items()) == 0:
empty_elems += 1
for k, v in c... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.