content stringlengths 42 6.51k |
|---|
def to_lists(scraped_data):
"""
Translate elements of scraped data (a list) from dicts to lists and return it. Convert 'research_fields' and 'research_objects' lists to ' :: '-delimited strings.
"""
delimiter = " :: "
return [[lab["number"], lab["certdate"], lab["org_name"], lab["org_address"], lab[... |
def w_def(typ: str) -> dict:
"""
Make definition reference
"""
return {'#ref': f'#/definitions/{typ}'} |
def _get_team_together(postgame, de_data):
"""Get team together flag."""
if de_data is not None:
return not de_data.random_positions
if postgame is not None:
return not postgame.random_positions
return None |
def compliment_name(name, flag):
""" if the alignment is a reverse, add _comp to the end of its identification """
if flag == 16 or flag == 272:
return '%s_comp' % (name)
else:
return name |
def _normalise_options(options):
"""
Return a sequence of (value, label) pairs for all options where each option
can be a scalar value or a (value, label) tuple.
"""
out = []
if hasattr(options, '__call__'):
options = options()
for option in options:
if isinstance(option, tup... |
def ci2se(ci):
"""
Converts a tuple of (lower, upper) confidence interval bounds to standard error
"""
ci = sorted(ci)
return (ci[1] - ci[0]) / (2 * 1.96) |
def _extract_xy_from_gdcm_str_seg(lines, dicom_tags):
"""Get rows, columns from gdcmdump output."""
rows = 0
cols = 0
for line in lines:
line = line.lstrip()
tag = line.split(" ")[0]
if tag == dicom_tags["rows"]:
rows = line.split(" ")[2]
elif tag == dicom_tag... |
def minimallyEqualXML(one, two, removeElements=()):
""" Strip all the whitespace out of two pieces of XML code, having first converted
them to a DOM as a minimal test of equivalence.
"""
from xml.dom import minidom
sf = lambda x: ''.join(filter(lambda y: y.strip(), x))
onedom = mi... |
def do_intervals_intersect(a, b):
"""Returns true if the given 2-tuples overlap.
Args:
a: 2-tuple containing integer coordinates representing a half-open interval.
b: 2-tuple containing integer coordinates representing the other half-open interval.
Returns:
True if a and b overlap.... |
def split_path_to_parts(path: str):
"""
Helper to separate paths into information pieces
:param path: the path of a java file including the file itself
:return: a tuple of path,file,package name derived from path and class name derived from file
"""
parts = path.split('/')
package = ".".join... |
def connectChunk(key, chunk):
"""
Parse Card Chunk Method
"""
upLinks = []
schunk = chunk[0].strip().split()
for idx in range(4, len(schunk)):
upLinks.append(schunk[idx])
result = {'link': schunk[1],
'downLink': schunk[2],
'numUpLinks': schunk[3],
... |
def array_madness(arr1: list, arr2: list) -> bool:
""" This function returns True if the sum of the squares of each element in arr1 is strictly greater than the sum of the cubes of each element in arr2. """
if len(arr1) and len(arr2) >= 1:
return True if sum([i**2 for i in arr1]) > sum([i**3 for i in ar... |
def merge_dict(*dicts):
"""Merge dicts and return a dictionary mapping key to list of values.
Order of the values corresponds to the order of the original dicts.
"""
ret = dict()
for dict_ in dicts:
for key, val in dict_.items():
ret.setdefault(key, []).append(val)
return re... |
def get_value(values, register):
"""Gets the value"""
if register >= 'a' and register <= 'z':
return values[register]
return int(register) |
def havrilliak_negami_conductivity(x, logtau0, de, a, b, einf):
"""2-d HNC: HNC(x, logtau0, de, a, b, einf)"""
return de / ( 1 + ( 1j * x * 10**logtau0 )**a )**b + einf / ( x * 8.854187817 * 10**(-12) ) |
def simplified(text, delete=""):
"""Returns text with multiple whitespace reduced to single spaces
Any characters in delete are excluded from the resultant string.
>>> simplified(" this and\\n that\\t too")
'this and that too'
>>> simplified(" Washington D.C.\\n")
'Washington D.C.'
>... |
def Snow(temp, rf, sf, wc_old, sp_old, tt, cfmax, cfr, cwh):
"""
========================================================
Snow(cfmax, temp, tt, cfr, cwh, rf, sf, wc_old, sp_old)
========================================================
Snow routine.
The snow pack consists of two states: ... |
def get_inv_otu_map(lines):
"""Map a cluster member to its GG ID"""
otu_map = {line[0]: line[1:] for line in lines}
inv_otu_map = {}
for gg_id, deblur_seqs in otu_map.items():
for seq in deblur_seqs:
inv_otu_map[seq] = gg_id
return inv_otu_map |
def tvm_callback_verilog_postproc(code):
"""Hook to inspect the verilog code before actually run it"""
print(code)
return code |
def del_chars_from_string(s, chars_to_del):
"""
Delete characters from list
:param s: string to clean
:param chars_to_del: characters to delete in string
"""
if type(chars_to_del) != "str":
for c in chars_to_del:
s = s.replace(c, "")
else:
s = s.replace(chars_to_... |
def config_prop_name(cls, name, value):
"""Configure name on property.
Attempts to configure the name of a property. If attribute value has
__config__ method will call it with attribute name.
Args:
cls: Class property will belong to.
name: Name of attribute.
value: Value of attribute.
Returns:... |
def convertir(string):
"""
Definicion de la funcion convertir:
Funcion para convertir un string separado por comas
en elementos de una lista
Parametros
----------
string: string
String que contiene la palabra que tiene que convertirse
en u... |
def get_projects(slug):
"""Get project information from TimeSync"""
p_list = [
{
"uri": "https://code.osuosl.org/projects/ganeti-webmgr",
"name": "Ganeti Web Manager",
"slugs": [slug if slug else "gwm"],
"uuid": "a034806c-00db-4fe1-8de8-514575f31bfb",
... |
def back_to_tag(tag, attrs):
"""
recover tag from tag name and attributes.
"""
sol = '<' + tag
for (prop, val) in attrs:
sol += ' ' + prop + '="' + val + '"'
sol += '>'
return sol |
def _group_by_expression(potential_types):
"""Group a dictionary of potentials by their expression."""
expr_group = {}
for potential in potential_types:
potential_type = potential_types[potential]
atom_types_list = expr_group.get(str(potential_type.expression), [])
atom_types_list.a... |
def reverse_class_def(class_def_dict):
"""Reverses a ClassDef dictionary."""
reverse = {}
for key in class_def_dict:
value = class_def_dict[key]
try:
reverse[value].add(key)
except KeyError:
reverse[value] = {key}
return reverse |
def hamming_distance(str1, str2):
"""
Calculate the hamming distance of the two strings
Args:
str1(string),str2(string): Strings to be used for finding the hamming distance
Returns:
int: Hamming Distance
"""
if len(str1) != len(str2):
return None
hamming_dist = 0
... |
def _find_top_ten(current,candidate,size=10):
"""
Private: find top ten highest numbers
* current list
* new candidate
* (optional) size of list. default=10
Returns array with top
"""
if len(current) == 0:
current.append(candidate)
else:
if candidate > current[0]:
... |
def _assign_modality_from_estimate(mean_alpha, mean_beta):
"""
Given estimated alpha and beta parameters from an Markov Chain Monte Carlo
run, assign a modality.
"""
# check if one parameter is much larger than another, and that they're
# both larger than 1
if mean_alpha / mean_beta > 2 or m... |
def nationality_normalizer(nationality: str) -> str:
""" Take a nationality string and return a normalized nationality.
E.g. Taiwan -> Taiwanese, R.O.C. -> Taiwanese """
nationality = str(nationality).lower()
if 'bangl'.lower() in nationality:
return 'Bangladeshi'
elif 'fili'.lower() in... |
def days_in_year_month(year: int, month: int) -> int:
"""Return the number of days in the given (year, month). The
month is usually 1-12, but can be 0 to indicate December of the previous
year, and 13 to indicate Jan of the following year.
"""
DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31,... |
def dBoxId(boxId):
"""Return box id if valid, raise an exception in other case"""
if boxId >= 0:
return boxId
else:
raise ValueError(
'{} is not a valid Box Id, Box Id must be >= 0'.format(boxId)) |
def pathdata_first_point(path):
"""
Return the first (X,Y) point from an SVG path data string
Input: A path data string; the text of the 'd' attribute of an SVG path
Output: Two floats in a list representing the x and y coordinates of the first point
"""
# Path origin's default values are use... |
def verify_filetype(filename: str, valid_filetypes=["jpeg", "png", "jpg"]) -> bool:
"""
Helper function which determines the filetype of
a file based on it's filename
valid filetypes = ["jpeg", "png", "jpg"]
Parameters:
filename (str)
Returns:
True if filetype is valid
"""... |
def ordinal(number):
"""Returns the string ordinal for the input number.
Algorithm from Gareth's solution at:
http://codegolf.stackexchange.com/questions/4707/outputting-ordinal-numbers-1st-2nd-3rd
"""
k = number % 10
return "{}{}".format(number,
"tsnrhtdd"[(number / 10 ... |
def get_head(content):
""" xxx """
return_data = '<head>'+ content +'</head>'
return return_data |
def get_file_path(local_path):
"""Get file and dir given a full path
Note: only to be used internally right now
Args:
device (Device): This is the device object of an NX-API enabled device
using the Device class within device.py
path (str): path of a dir on switch
Returns... |
def group_nearby_indices(indices, max_gap=None, max_group_spread=None):
"""Return a list of groups of the different indices.
Indices are considered from smaller to larger and placed into groups
Parameters
----------
max_gap
Maximal allowed difference between two consecutive numbers of a grou... |
def palindrome(step):
"""
Turn sequential integers into a palindromic sequence (so look-ahead mapping is not a function, but requires state)
"""
return (5.0 - abs(float(step % 10) - 5.0)) / 10.0 |
def flatten_fieldsets(fieldsets):
"""Returns a list of field names from an admin fieldsets structure."""
field_names = []
for name, opts in fieldsets:
for field in opts['fields']:
# type checking feels dirty, but it seems like the best way here
if type(field) == tuple:
... |
def get_tot_pop(dwellings):
"""Get total population of all dwellings
Return
------
tot_pop : float or bool
If population is not provided, return `None`,
otherwise summed population of all dwellings
"""
tot_pop = 0
for dwelling in dwellings:
if dwelling.population is... |
def bubble(arr):
"""
:param arr: an array to be sorted
:returns: sorted array
"""
n = len(arr)
# Traverse through all array elements
for i in range(n - 1):
# range(n) also work but outer loop will repeat
# one time more than needed.
# Last i elements are already in pl... |
def make_italic(text: str) -> str:
"""
Returns the text surrounded by *
"""
return "*" + text + "*" |
def get_module_short_name(klass):
"""
Return the short module name.
For example, full module name is `django.forms.fields` and
the short module name will be `fields`.
"""
return klass.__module__.rsplit('.', 1)[-1] |
def append(text, to_append):
"""Appends text to a title string in a way that respects tags."""
first_open = text.find("[")
if first_open == -1:
return text + to_append
else:
return "%s%s %s" % (text[:first_open].strip(),
to_append,
... |
def embeddedness(target_list, compared_list):
"""
Measure the embedddedness of one list within another; embeddedness of A in B = #(A int B)/#A
:param target_list: The target list
:param compared_list: The list to be compared with
:return: Embeddedness score
"""
intersection = [e for e in tar... |
def list_split(lst, sections):
"""
Splits a list into N sections. From https://stackoverflow.com/a/2135920.
Examples:
>>> list_split(list(range(10)), 3)
[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> list_split(list(range(20)), 4)
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16... |
def list_to_string(lst):
""" Convert [1.0, 2, 0] ==> '1.0 2 0' """
return " ".join([str(v) for v in lst]) |
def substitute_all_by_empty(qry_string, qry_subs):
"""Substitute all occurrences of specific string sequences by an empty sequence.
Args:
qry_string: A string containing sequences to be replaced.
qry_subs: A list of strings containing sequences to be replaced.
Returns:
The ... |
def remote_branch(stdin_first_line):
"""
Reads the name of the remote git branch from runtime parameters.
In the pre-push.py hook the name of the remote branch is passed as the $1 parameter.
:param stdin_first_line the first line of the standard input
>>> remote_branch("refs/heads/master a9d45baccd... |
def mass_funct(pb, x):
"""
mass_funct(pb, x):
Return the mass function of an orbit given the following:
'pb' is the binary period in days.
'x' is the projected semi-major axis in lt-sec.
"""
pbs = pb * 86400.0
return 8015123.37129 * x**3.0 / (pbs * pbs) |
def initial_investment(pv_size, battery_size, n_batteries = 1, capex_pv = 900, capex_batt = 509):
"""Compute initial investment"""
return pv_size*capex_pv + battery_size*capex_batt*n_batteries |
def roll_right(sequence, alleles, ref_pos, bound):
"""Determines common distance all alleles can be rolled (circularly permuted) right
within the reference sequence without altering it.
Args:
sequence (str): The reference sequence.
alleles (list of str): The sequences to be normalized.
... |
def hasPTC(sequence):
"""
Determines whether a PTC exits in a sequence
Arguments:
sequence (str): IMGT gapped sequence in frame 1.
Returns:
int: negative if not PTCs, position of PTC if found.
"""
ptcs = ("TAA", "TGA", "TAG", "TRA", "TRG", "TAR", "TGR", "TRR")
for i in range(0... |
def yte_syntax(file):
"""
Return the full name of a YouTube Editor syntax based on the short name.
"""
return "Packages/YouTubeEditor/resources/syntax/%s.sublime-syntax" % file |
def string_in_file(file_name, string_to_search):
""" Check if any line in the file contains given string """
with open(file_name, 'r') as read_obj:
for line in read_obj:
if string_to_search in line:
return True
return False |
def delete_repeated_lines(log):
"""Lines have format [step, [formula], value, clause, on_steps]"""
log2 = list()
# Start backwards
for index1 in range(len(log)-1, 0, -1):
# Then check forward until that index for the same formula
for index2 in range(index1):
if log[index1][1]... |
def parse_mimetype(mimetype):
"""Parses a MIME type into its components.
:param str mimetype: MIME type
:returns: 4 element tuple for MIME type, subtype, suffix and parameters
:rtype: tuple
Example:
>>> parse_mimetype('text/html; charset=utf-8')
('text', 'html', '', {'charset': 'utf-8'})
... |
def vprint(verbose: bool):
"""
Utility function for optional printing.
Parameters
----------
verbose: bool
If True, returns print function, else False
Returns
-------
callable
"""
return print if verbose else lambda *a, **k: None |
def yields_from_leung_nomoto_2018_table10(feh):
"""
Supernova data source: Leung & Nomoto, 2018, ApJ, Volume 861, Issue 2, Id 143, Table 10/11
The seven datasets are provided for Z/Zsun values of 0, 0.1, 0.5, 1, 2, 3 and 5.
Using Zsun = 0.0169 the corresponding FeH values are -1, -0.301, 0.0, 0.301, 0.4... |
def validate_file(rules: dict, fname: dict):
"""
Validates files by BIDS-compatible identifiers
Parameters
----------
rules : dict
Dictionary with keys of BIDS-recognized key and their accepted values.
fname : str
File to validate.
"""
valid = []
for key, value in rul... |
def propsample(freqs, num):
"""Proportionally samples from the given frequencies.
Returns a list of same length with the number of times each index should be
sampled such that the total number of elements sampled is `num`.
"""
lens = [int(f*num)+1 for f in freqs]
total = 0
for i, l in enumer... |
def map_clone(function, xs):
"""
Sends every element xs to the function and returns a processed list.
"""
map_cloned_lst = []
for elem in xs:
map_cloned_lst += [function(elem)]
return map_cloned_lst |
def prep_querystring( get_params ):
""" Makes querystring from params.
Called by handle_bad_params() """
if get_params:
querystring = '?%s' % get_params.urlencode() # get_params is a django QueryDict object, which has a urlencode() method! yay!
else:
querystring = ''
return quer... |
def get_mtl_tile_file_name(secondary=False):
"""Convenience function to grab the name of the MTL tile file.
Parameters
----------
secondary : :class:`bool`, optional, defaults to ``False``
If ``True`` return the name of the MTL tile file for secondary
targets instead of the standard, pr... |
def coalesce_repeated_switches(cmd):
"""Combines known repeated command line switches.
Repetition of a switch notably happens when both per-test switches and the
additional driver flags specify different --enable-features. For instance:
--enable-features=X --enable-features=Y
Conceptually, this i... |
def get_name(parameters):
#{{{
"""
Generate a model name from its parameters.
"""
l = []
for k, v in parameters.items():
if type(v) is str and "/" in v:
l.append((k, v[::-1][:v[::-1].index('/')][::-1]))
else:
l.append((k, v))
name = ",".join(["%s=%s" % (k,... |
def uses_all(word, required):
"""Checks if the word uses all the required letters."""
for letter in required:
if letter not in word:
return False
return True |
def extract_risk(risk_assessment):
"""Extracts risk from a RiskAssessment resource."""
prediction = risk_assessment['prediction']
return prediction[0]['qualitativeRisk']['coding'][0]['code'] |
def parse_section(fin):
""" Parse a section from the tssv report
NOTE: this moves the file pointer forwards
"""
section = list()
# Get the header
header = next(fin).strip('\n').split('\t')
# Parse the other files
for line in fin:
# End of section
if not line.strip('\n'... |
def human_readable(bytes):
"""Return a human-readable representation of the input bytes."""
for n, label in enumerate(['bytes', 'KiB', 'MiB', 'GiB', 'TiB']):
value = bytes / (1024 ** n)
if value < 1024:
return f'{round(value, 2)} {label}'
else:
continue |
def nearest(items, pivot):
"""Find nearest value in array, including datetimes
Args
----
items: iterable
List of values from which to find nearest value to `pivot`
pivot: int or float
Value to find nearest of in `items`
Returns
-------
nearest: int or float
Valu... |
def make_string_pep440_compatible(raw_str):
"""
pep440 only allows a subset of characters in the
version name:
- alphanumeric
- dots
this will restrict the string to that set.
"""
final_chars = []
for r in raw_str:
if "a" <= r <= "z" or "A" <= r <= "Z" or "0" <= r <= "9" or... |
def parse_url_name_args(string):
"""
Parse and return url_name and kwargs as a tuple from the node's
url_name parameter (which can be just the url_name or additionally define
some kwargs)
Example: node['url_name'] = 'url_name|kwarg1:value,kwarg2:value'
"""
chunks = string.split('|')
ur... |
def pascal(n):
"""Prints out n rows of Pascal's triangle.
It returns False for failure and True for success."""
row = [1]
k = [0]
for x in range(max(n,0)):
print(row)
row=[l+r for l,r in zip(row+k,k+row)]
return n>=1 |
def prefix_path(prefix, path):
""""Return True if prefix is a parent directory of path.
Assume that prefix and path are strings."""
return prefix == path or (prefix + '/' == path[:len(prefix) + 1]) |
def _list_separators_in_xmlformat(separators, indent=''):
"""Generates XML encoding of a list of list separators.
Args:
separators: A list of list separators. Usually, this should be a
string whose characters are the valid list separators, e.g., ','
means that both comma (',') and space (' ') are ... |
def how_many_5(numbers):
"""Returns number of numbers greater than 5."""
# Modify example to take argument that specifies threshold
return sum( 1 for number in numbers if number > 5 ) |
def _get_gid(name):
"""Returns a gid, given a group name."""
if name is None:
return None
try:
from grp import getgrnam
except ImportError:
return None
try:
result = getgrnam(name)
except KeyError:
result = None
if result is not None:
... |
def dont_give_me_five(start, end):
"""
Start number and the end number of a region and should return the count of all numbers except numbers with a 5 in
it. The start and the end number are both inclusive!
:param start: starting integer for range.
:param end: ending integer for range.
:return: t... |
def collapse_list(items):
"""Given an ordered list of numbers, returns the ranges of
present items. For example, given a list [1,2,3,5,6,7,10], it
would return the string '1-3, 5-7, 10'."""
result = ""
previtem = items[0]
sequence = False
for i in items:
if sequence:
if n... |
def sort_by_name(seq):
"""Returns a copy of the input sequence sorted by name."""
return sorted(seq, key=lambda x: x['name']) |
def parse_boolean(value):
"""
Parses strings into booleans using the following mapping (case-sensitive):
'true' => True
'false' => False
'1' => True
'0' => False
"""
if value in ["true", "1"]:
return True
elif value in ["false", "0"]:
return False
el... |
def calc_years(seconds: int) -> int:
"""
Calculate years
:param seconds:
:return:
"""
return seconds // (60 * 60 * 24 * 365) |
def changeCourses(previousMessage, currentMessage):
"""
Determine if one of observed currency has changed..
:param previousMessage (str) : previous message for twitter
:param currentMessage (str) : current message for twitter
:return change (bool) : change currencies ?
"""
... |
def urljoin(url, suffix=""):
"""
Will join url and its suffix
Example:
"https://google.com/", "/" => "https://google.com/"
"https://google.com", "/" => "https://google.com/"
"https://google.com", "api" => "https://google.com/api"
"https://google.com", "/api" =... |
def key_der_to_list(key_data):
"""
Read AES key from der format
:param key_data: Input Key data
:return: key as list
"""
key_list = list()
for i in key_data:
key_list.append(ord(chr(i)))
return key_list |
def str_xor(a, b):
"""
(string, string) -> string
xor two strings(a and b) of different lengths
>>> str_xor("string", "integer")
'\x1a\x1a\x06\x0c\t\x02'
>>> str_xor("hello", "world")
'\x1f\n\x1e\x00\x0b'
>>> str_xor("foo", "bar!")
'\x04\x0e\x1d'
>>> str_xor("AI", " ")
'ai... |
def get_as_dict(x):
"""Return an object as a dictionary of its attributes."""
if isinstance(x, dict):
return x
else:
try:
return x._asdict()
except AttributeError:
return x.__dict__ |
def demandNameItem(listDb,phrase2,mot):
"""
put database name of all items in string to insert in database
listDb: list with datbase name of all items
phrase2: string with database name of all items
mot: database name of an item
return a string with database name of all items separated with ','
... |
def indent(st, indent=4):
"""
Indent string.
"""
if isinstance(indent, int):
indent = " " * indent
return "".join(indent + ln for ln in st.splitlines(keepends=True)) |
def calculate_divisors(meta):
""" Description ...
Args:
l: ...
Returns:
meta ... For example:
...
"""
for unique_value in meta.keys():
k = int(unique_value)
k_indices = meta[unique_value]["indices"]
k_divisors = meta[unique_value]["divisors"]
... |
def curly_bracket_to_img_link(cb):
"""
Takes the curly-bracket notation for some mana type
and creates the appropriate image html tag.
"""
file_safe_name = cb[1:-1].replace('/', '_').replace(' ', '_')
ext = 'png' if 'Phyrexian' in file_safe_name or file_safe_name in ('C', 'E') else 'gif'
ret... |
def quote_string(s: str, force:bool=False) -> str:
"""Sometimes wraps strings in double quotes, depending on the content and force parameter.
Description:
This function provides conditional wrapping of strings inside double quotes.
If the input string contains a space, OR force is set to T... |
def get_query_counter(request):
""" hhs_oauth_server.request_logging.RequestTimeLoggingMiddleware
adds request._logging_pass
we grab it or set a counter and return it.
"""
if not hasattr(request, '_logging_pass'):
return 1
else:
return request._logging_pass |
def rk4(fun, tn, h, yn):
"""Rugge-Kutta 4, single step
Args:
fun (func): function in t (float) and y (ND numpy array)
tn (float): time
h (float): time step
yn (n-d array): values as of tn
Returns:
n-d array: values as of tn+1
"""
k1 = fun(tn, yn)
k2 = fu... |
def flag(argument):
"""
Check for a valid flag option (no argument) and return ``None``.
(Directive option conversion function.)
Raise ``ValueError`` if an argument is found.
"""
if argument and argument.strip():
raise ValueError('no argument is allowed; "%s" supplied' % argument)
e... |
def isNumber(n):
"""
checks if n is an integer
:param n: value to be check
:return: true if n is a number, false in other case
"""
try:
int(n)
return True
except ValueError:
return False |
def sizeof(bsObj):
""" Size of object in bytes. Size is contextual by object type. """
return bsObj.__sizeof__() |
def is_valid_minimizer(object):
"""
Checks if the minimzer object has the following attributes/methods:
* minimize
"""
has_minimize = hasattr(object, "minimize")
return has_minimize |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.