content stringlengths 42 6.51k |
|---|
def overflow(keyword):
"""Validation for the ``overflow`` property."""
return keyword in ('auto', 'visible', 'hidden', 'scroll') |
def hex2rgb(hex_color):
""" '#B4FBB8' => 'rgb(180, 251, 184)' """
hex_color = hex_color.strip('#')
rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
return 'rgb{}'.format(rgb) |
def maybe_truncate(s, length=30):
"""Truncate long strings (and append '...'), but leave short strings alone."""
s = str(s)
if len(s) > length-3:
return s[0:length-3] + "..."
else:
return s |
def type_name(x):
"""
Describe (very briefly) what type of object this is.
THANKS: https://stackoverflow.com/a/5008854/673991
"""
the_type_name = type(x).__name__
if the_type_name == 'instance':
the_type_name = x.__class__.__name__
# NOTE: Possibly this is only different in Py... |
def people_clashes(events_definition, clashes_definition):
"""
Parameters
----------
events_definition : list
of dicts of the form
{'title': Event title,
'duration': <integer in minutes>,
'tags': <list of strings>,
'person': <string>,
... |
def emit(expr, indent, body):
"""
Expression to string, formatted in executable python syntax
"""
# Default implementation, for types not specialized below
return str(expr) |
def tokens_url(tenant_url):
"""Returns the tokens API endpoint
"""
return '{0}/token'.format(tenant_url) |
def architecture_flag(compiler, arch):
"""
returns flags specific to the target architecture and compiler
"""
if not compiler or not arch:
return ""
if str(compiler) in ['gcc', 'apple-clang', 'clang', 'sun-cc']:
if str(arch) in ['x86_64', 'sparcv9']:
return '-m64'
... |
def cast_cube(value, cursor):
"""
"""
if value:
return map(float, value[1:-1].split(',')) |
def lm1(data):
"""
Regresses the linear model y = a + b*x and returns (a, b,
r2) for the best fit (where r2 is the R-squared value).
The input, *data*, should be a series of (x,y) pairs. y can
be None in which case the pair is ignored.
"""
sxx = sxy = syy = sx = sy = n = 0
for (x, y) in... |
def uri_to_curie(uri):
"""Converts URI to curie (short identifier).
Args:
uri: a full URI of an ontology term, e. g. http://purl.obolibrary.org/obo/MONDO_0009796. URIs are globally
unique among all ontologies (and even other internet resources).
Returns:
curie: a short identifi... |
def pixellate_factor(px_x=4, px_y=4):
"""
A pixellation filter that uses factor values instead of resolution sizes.
Author: SolarLune
Date Updated: 6/6/11
px_x = size of pixels on the x-axis
px_y = size of pixels on the y-axis
"""
return ("""
uniform sampler2D bgl_RenderedTexture;... |
def _average_nadir_gain_dbi(pattern, angles):
"""Average gain on the nadir face of the satellite.
For simplicity, this function assumes some hard-coded values of
65-degrees off of boresight. That translates to 0->65 and (360-65)->360
"""
s = 0
n = 0
offset = 65
for i in range(len(patte... |
def key_to_frequency(key):
"""Returns the frequency of the note (key) keys from A0"""
return 440 * 2 ** ((key - 49) / 12.0) |
def time_formatter(milliseconds: int) -> str:
"""Inputs time in milliseconds, to get beautified time,
as string"""
seconds, milliseconds = divmod(int(milliseconds), 1000)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
tmp = (
... |
def merge_lists(iterable_of_lists):
""" """
result = []
for i in iterable_of_lists:
result += i
return result |
def exit_mode(mode):
"""
Returns 'min' if given 'max' and vice-versa.
Arguments
- mode: 'max' or 'min'
"""
return 'min' if mode == 'max' else 'max' |
def data_sort_type(text, type='number'):
"""Define data-sort-type for wikitext table auto sorting"""
text = str(text)
if '|' not in text[1:]:
return f"| data-sort-type={type} | {text}\n"
else:
parts = text.split('|')
return f"|{parts[1]}data-sort-type={type} | {parts[-1]}" |
def clean_team_name(team_names):
"""Take a list of team_names, modify the names to match the format specified in br_references, and return a new list
Args:
team_names: a list of team_names to be checked for validity, and if needed, modified
"""
new_team_names = []
for team in team_names:
... |
def sequential_search_ordered_input(list1, val):
"""
Carry out a sequential search of the given sorted list for a given value
Parameters
----------
list1: input list, assumed to be sorted in ascending order
val: the value to be searched
Returns
-------
True/False
"""
fo... |
def solution1(A: list):
""" pair indexes of values 0 with 1s"""
n = len(A)
pair = []
for i in range(n):
if A[i] == 0:
# if the value is 0 iterate over again
# skipping over 0s and pairing indexes
# of non zeroes and pairing the indexes
for k in ran... |
def format_time(start, end):
"""
Computes the interval time between a start and an end point.
:param start: starting time
:param end: ending time
:return:
"""
elapsed_time = end - start
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
r... |
def normalize_location_code(code: str) -> str:
"""
Removes any quantity designation from location code value
"""
try:
s = code.index("(")
e = code.index(")")
return f"{code[:s]}{code[e + 1:]}"
except ValueError:
return code |
def cria_peca(peca):
"""
cria_peca: str -> peca
Recebe um identificador de jogador (ou peca livre) e devolve um dicionario
que corresponde ah representacao interna da peca.
R[peca] -> {'peca': peca}
"""
if type(peca) != str or len(peca) != 1 or peca not in 'XO ':
raise ValueError('cr... |
def dm2skin_normalizeWeightsConstraint(x):
"""Constraint used in optimization that ensures
the weights in the solution sum to 1"""
return sum(x) - 1.0 |
def is_inside_circle(x, y):
"""Check if a given point falls inside the enclosed circle
:param x: x coordinate of the point
:param y: y coordinate of the point
:return: a boolean True/False
"""
r = 0.5 # radius of the circle
if x ** 2 + y ** 2 < r ** 2:
return True
else:
... |
def normalizeInternalObjectType(value, cls, name):
"""
Normalizes an internal object type.
* **value** must be a instance of **cls**.
* Returned value is the same type as the input value.
"""
if not isinstance(value, cls):
raise TypeError("%s must be a %s instance, not %s."
... |
def get_num_in_channel_2D(dataset_name="mesa"):
"""
get the number of input channels, this is for the hybrid fusion strategy as they use two Conv1D networks.
@param dataset_name: dataset name
@return: a set, one for each network
"""
if dataset_name == "mesa":
in_channel_1 = 1
in_... |
def validate_validator_type(validator_type):
"""
Property: Validators.Type
"""
VALID_VALIDATOR_TYPE = ("JSON_SCHEMA", "LAMBDA")
if validator_type not in VALID_VALIDATOR_TYPE:
raise ValueError(
"ConfigurationProfile Validator Type must be one of: %s"
% ", ".join(VALI... |
def parse(arg):
"""Convert a series of zero or more numbers to an argument tuple"""
return tuple(map(str, arg.split())) |
def _alpha(n):
"""Excel-style column numbering A..Z, AA..AZ..BA..ZZ.., AAA"""
if n < 1:
raise ValueError(f"Can't represent {n} in alphabetic numbering")
p = []
while n > 0:
n, r = divmod(n - 1, 26)
p.append(r)
base = ord('A')
ords = [(base + v) for v in reversed(p)]
r... |
def app_config(app_config):
"""Application configuration fixture."""
app_config["SEARCH_COPY_TO_METADATA"] = True
return app_config |
def fixipmapping(ipparams, posflux, etc = [], retbinflux = False, retbinstd = False):
"""
This function returns the fixed best-fit intra-pixel mapping.
Parameters
----------
ipparams : tuple
unused
bestmip : 1D array, size = # of measurements
Best-fit ip mapping
... |
def is_pj_lop(value):
""" Is the given value a PJ_LOP
:param value: The value being checked
:type value: Any
:return: True if the value is a PJ_LOP, False otherwise
:rtype: Boolean
"""
return isinstance(value, list) |
def volumetric_thermal_expansion_coefficient(rho):
"""
Takes oil density and returns oil volumetric thermal expansion coefficient
:param rho: the density of an oil, kg/m3
:return: volumetric thermal expansion coefficient, 1/(Celsius degree)
"""
a = None
if 700 <= rho <= 719:
a = 0.00... |
def get_keys_from_value(value, _dict, case_sensitive=False):
"""Returns list of keys in dict with value given"""
matching_keys = []
for key in _dict.keys():
for val in _dict.get(key):
if not case_sensitive:
if value.lower() == val.lower():
matching_ke... |
def convert_data_to_storable_format(response: dict) -> dict:
"""
Takes response data from YouTube API and converts it to
a format which can be stored in the database - removes
irrelevant information and stores in accessible format
Args:
response: Response Data from API
Returns:
... |
def normalize_email(email):
"""
Normalize the address by lowercasing the domain part of the email
address.
"""
email = email or ''
try:
email_name, domain_part = email.strip().rsplit('@', 1)
except ValueError:
# return None to leave error handling for serializer
retur... |
def is_upper_case_name(name: str) -> bool:
"""
Checks that attribute name has no upper-case letters.
>>> is_upper_case_name('camelCase')
True
>>> is_upper_case_name('UPPER_CASE')
True
>>> is_upper_case_name('camel_Case')
True
>>> is_upper_case_name('snake_case')
False
>>... |
def mm2cin(arg):
"""
Convert millimeters to 1/100 in.
Arguments:
arg: Number or sequence of numbers.
Returns:
Converted number or sequence.
"""
if not type(arg) in [list, tuple]:
return float(arg) * 100.0 / 25.4
return [float(j) * 100.0 / 25.4 for j in arg] |
def vnf_package_obj(attrs=None, onboarded_state=False):
"""Create a fake vnf package.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeVnfPackage dict
"""
attrs = attrs or {}
# Set default attributes.
fake_vnf_package = {"id": "60a6ac16-b50d-4e92-964... |
def LastLineLength(s):
"""Returns the length of the last line in s.
Args:
s: A multi-line string, including newlines.
Returns:
The length of the last line in s, in characters.
"""
if s.rfind('\n') == -1: return len(s)
return len(s) - s.rfind('\n') - len('\n') |
def is_call_id_active(call_id):
"""Check if reactor.callLater() from callID is active."""
if call_id is None:
return False
elif (call_id.called == 0) and (call_id.cancelled == 0):
return True
else:
return False |
def find_padding(dilation, kernel):
"""
Dynamically computes padding to keep input conv size equal to the output
for stride = 1
:return:
"""
return int(((kernel - 1) * (dilation - 1) + (kernel - 1)) / 2.0) |
def get_website_from_host(http_host):
"""Try to find the website name from the HTTP_HOST name"""
return http_host.split(':')[0] |
def _mangle_name(internal_name, class_name):
"""Transform *internal_name* (which is assumed to be an "__internal"
name) into a "_ClassName__internal" name.
:arg str internal_name:
the assumed-to-be-"__internal" member name
:arg str class_name:
name of the class where *internal_name* is de... |
def invert_label(args, column):
"""Invert mapping.
Find key corresponding to value column in dict args["labels"].
Returns `column` if the value does not exist.
"""
reversed_labels = {value: key for (key, value) in args["labels"].items()}
try:
return reversed_labels[column]
except Exc... |
def find_max_path(triangle):
"""
Find maximum-sum path from top of triangle to bottom
"""
# Start by copying the values
sums = [[x for x in row] for row in triangle]
# Efficient algorithm: start at the bottom and work our way up, computing max sums
for reverse_index, row in enumerate(reverse... |
def count_failed_validations(sessions):
"""Count failed challenges from certbot logs"""
failed_validations = 0
for session in sessions:
for line in session:
if "'--staging'" in line:
break
if 'FailedChallenge' in line:
failed_validations ... |
def create_player_list(records, fields):
"""This function change tuples to list of dicts using fields"""
players = []
for record in records:
player = {}
for idx, field in enumerate(fields):
player[field] = record[idx]
players.append(player)
return players |
def counter(clstr_lst, remove_single=True):
"""_summary_
Args:
clstr_lst (dictionary): A dictionary with the number of the cluster as key and the UniProt ID's for the sequences inside each cluster as value.
remove_single (bool, optional): Decides to remove single sequence clusters. Defaults to ... |
def formatData(account):
"""Format the account data into printable format."""
account_name = account["name"]
account_desc = account["description"]
account_country = account["country"]
return f"{account_name}, a {account_desc} from {account_country}" |
def dsub_to_api(job):
"""Extracts logs from a job, if present.
Args:
job: A dict with dsub job metadata
Returns:
dict: Labels key value pairs with dsub controller, stderr, and stdout
log files
"""
if job['logging'] and job['logging'].endswith('.log')... |
def calculate_gc_lo(subseq):
"""Calculate the GC and lowercase (RepeatMasked) content of a string."""
cnt_at_lo = subseq.count('a') + subseq.count('t')
cnt_at_up = subseq.count('A') + subseq.count('T')
cnt_gc_lo = subseq.count('g') + subseq.count('c')
cnt_gc_up = subseq.count('G') + subseq.count('C'... |
def color_green(val):
"""
Takes a scalar, returns a string with the CSS
property `'color: green'`
"""
# color = 'green' if type(val) == np.float64 else 'black'
# return f'color: {color}'
return 'color: green' |
def frequency_map(text, k):
"""
Find the frequency of all k-mers in a string.
Args:
text (str): text.
k (int): length of the substring (i.e. kmers).
Returns:
Dictionary, a dictionary that contains the count of all the k-mers in text.
Examples:
Computes the frequenc... |
def is_close(float1, float2, relative_tolerance=1e-9, absolute_tolerance=0.0):
""" This is a comparison for floats and taken from Python 3.5
:param float1: Float - Value 1
:param float2: Float - Value 1
:param relative_tolerance: the relative tolerance in nano
:param absolute_tolerance: minimum abs... |
def get_fits_name(expnum, prodtype):
"""
Get the name of a fits image based on its exposure number and prodtype
"""
if prodtype=='image':
fits_name = '{0}.fits'.format(expnum)
else:
fits_name = "{0}.{1}.fits".format(expnum, prodtype)
return fits_name |
def get_label(method):
"""Standardizes labels for graphs given METHOD list
Input
- method (list): list of methods to standardize
Output
- label (string): standardizes labels
"""
label = method.capitalize()
if label == "Random":
label = "random"
return label |
def string_to_array(string):
"""Return a list from input string."""
if string == "":
return [""]
return string.split() |
def has_next(iterable):
""" pick element from generator
"""
try:
return next(iterable)
except StopIteration:
return None
finally:
del iterable |
def scalarProduct(s, v):
"""
Calculate s * v
:param s: Scalar.
:param v: Vector.
:return:
"""
return [s * v[c] for c in range(len(v))] |
def _is_set(data: str) -> bool:
"""Returns False if data is a special mmCIF character indicating 'unset'."""
return data not in (".", "?") |
def sales_velocity(units_sold_last_12m, number_of_days_in_stock, velocity_days=30):
"""Return the sales velocity of a product for a given number of days.
Args:
units_sold_last_12m (int): Total number of units sold in the past 12 months.
number_of_days_in_stock (int): Total number of days in the... |
def get_total_time_in_sec(last_user_stats):
"""Calculates the total time you listen to music"""
total_time_sec = 0
for song in last_user_stats:
try:
total_time_sec += int(song['play_count']) * (int(song['duration_millis']) / 1000)
except:
continue
return total_tim... |
def merge_dicts(inputs):
"""
Merge multiple input dicts into a single dict.
Parameters
----------
inputs : list
List of dictionaries.
"""
output = {}
for i in inputs:
output.update(i)
return output |
def lorentzian(x, ll, yshift=0.):
""" Peak normalized Lorentzian function
:arguments
x: float / np1darray
ll: float Lorentzian FWHM
yshift: float shift of y value (used for root finding)
:returns
y: float / np1darray
"""
return ll**2 / (4 * x**2 + l... |
def replace_escapes(text):
"""
Replace escape sequences with the characters they represent
:param text: string to replace escapes in
:return: New string with escapes replaced by the represented characters
"""
escape = False
out = ""
for char in text:
if escape is True:
... |
def _score_sentences(tf_idf_matrix) -> dict:
"""
score a sentence by its word's TF
Basic algorithm: adding the TF frequency of every non-stop word in a sentence divided by total no of words in a sentence.
:rtype: dict
"""
sentenceValue = {}
for sent, f_table in tf_idf_matrix.items():
... |
def generate_registers_sifive_clic0_clicintie(intr, addr):
"""Generate xml string for riscv_clic0 intie register for specific interrupt id"""
return """\
<register>
<name>clicintie_""" + intr + """</name>
<description>CLICINTIE Register for interrupt id """ + ... |
def drange(v0: float, v1: float, d: int) -> range:
"""Returns a discrete range."""
return range(int(v0) // d, int(v1 + d) // d) |
def parseRange(string):
"""Parses a dash-separated string of ints into a tuple"""
splitarray = string.split("-")
if len(splitarray) == 1:
return (int(splitarray[0]), int(splitarray[0]))
if len(splitarray) == 2:
return (int(splitarray[0]), int(splitarray[1]))
raise ValueError("Cannot... |
def preprocessLines(sourceLines):
"""
Delete comments from the lines and change them to upper case
sourceLines - array of assembly lines with comments
return: resulting array of lines
"""
for i, line in enumerate(sourceLines):
line = line.upper()
line = line.split(";")[0] # trim ... |
def compareHour(hour1, operator, hour2) :
"""
Function that allows to compare two hours
that are defined in string formats as : HH:MM
"""
hour1 = hour1.split(":")
hour_1 = int(hour1[0])
minute_1 = int(hour1[1])
hour2 = hour2.split(":")
hour_2 = int(hour2[0])
minut... |
def _key_by_signature(operations, signature_func):
"""Creates a dictionary of operations keyed by signature
Args:
operations (iterable[Operations]): the input operations
Returns:
dict[string, [Operations]]: the operations keyed by signature
"""
return dict((signature_func(op), op) for... |
def is_gradoop_id(value) -> bool:
"""Check if value is a valid Gradoop id.
Gradoop ids are 12 byte hexadecimal strings
Parameters
----------
value
Value to check
Returns
-------
bool
True if is valid Gradoop id
"""
if isinstance(value, str) and len(value) == 24... |
def make_headers(keep_alive=None, accept_encoding=None, user_agent=None, basic_auth=None):
"""
Shortcuts for generating request headers.
keep_alive
If true, adds 'connection: keep-alive' header.
accept_encoding
Can be a boolean, list, or string.
True translates to 'gzip,deflate... |
def read_input_samples(data_dict):
"""
Function that takes only the input property from the dictionary
Ignores train or test and just takes all inputs as equal
:param data_dict: data dictionary with the full file input structure loaded
:return: a dictionary of just input values
>>> dict = {'tra... |
def localDigitsStrToInt(value, digitsToLocalDict, localToDigitsDict):
"""Convert digits to integer."""
# First make sure there are no real digits in the string
tmp = value.translate(digitsToLocalDict) # Test
if tmp == value:
return int(value.translate(localToDigitsDict)) # Convert
... |
def _get_liblinear_solver_type(multi_class, penalty, loss, dual):
"""Find the liblinear magic number for the solver.
This number depends on the values of the following attributes:
- multi_class
- penalty
- loss
- dual
The same number is also internally used by LibLinear to determin... |
def get_pattern(guess, solution):
"""generates the patterns for a guess"""
hint = ""
for index in range(len(guess)):
if not guess[index] in solution:
hint += "b"
else:
if guess[index] == solution[index]:
hint += "g"
else:
hi... |
def try_int(s, *args):
"""Convert to integer if possible."""
try:
return int(s)
except (TypeError, ValueError):
return args[0] if args else s |
def median(iterable):
"""Obtain the central value of a series
Sorts the iterable and returns the middle value if there is an even
number of elements, or the arithmetic mean of the middle two elements
if there is an even number of elements
:param iterable: a series of ordeable items
:return: th... |
def multiply_basic(a, b):
"""
This function is NOT a standard matrix multiplication operation. It
instead multiplies two matrix directly(first index with first index).
Both Matrices must be: MxN where M=1 and N=any positive number.
:param a: (list) 2D matrix with only one row
:param b: (list)... |
def parse_config_line(line):
"""
:param line: string with a single line from config file
:return: config_key and config_value
config_key: 0 - empty line or comment
1 - section (section name in line)
"""
# Remove comments and strip whitespace
line = line.split("#")[0]... |
def reduce_reflex_angle_deg(angle):
""" Given an angle in degrees, normalises in [-179, 180] """
new_angle = angle % 360
if new_angle > 180:
new_angle -= 360
return new_angle |
def bytes_xor(a: bytes, b: bytes) -> bytes: # pylint: disable=invalid-name
"""XOR two bytes values."""
return (int.from_bytes(a, "big") ^ int.from_bytes(b, "big")).to_bytes(len(a), "big") |
def similarity(token1, token2):
"""
Calculate the similarity of two tokens.
:param token1: First token.
:param token2: Second token.
:return: similarity of the two inputs
"""
total = len(token1)
same = 0
for i in range(len(token1)):
if token1[i] == token2[i]:
sam... |
def chunk_to_str(chunk):
"""Convert the given chunk to a string.
chunk - list of Tagged characters.
return - string representation of the list of Tagged characters
"""
return "".join(c.c for c in chunk) |
def get_content(html_code):
""" Separates the webpage's message section from the content section and returns both for further processing
@parameters
html_code (str) html code of the downloaded web page
@returns
tuple (of lists) message_list: list of string... |
def get_protocol(url):
"""Returns the protocol of a given URL."""
try:
return url.split("://")[0]
except:
return "" |
def _check_regular_chunks(chunkset):
"""Check if the chunks are regular
"Regular" in this context means that along every axis, the chunks all
have the same size, except the last one, which may be smaller
Parameters
----------
chunkset: tuple of tuples of ints
From the ``.chunks`` attri... |
def apply_gain_x2(x, AdB):
"""Applies A dB gain to x^2"""
return x * 10 ** (AdB / 10) |
def _get_new_shape(name, shape, num_heads):
"""Checks whether a variable requires reshape by pattern matching."""
if "self_attention_output/kernel" in name:
return tuple([num_heads, shape[0] // num_heads, shape[1]])
if "self_attention_output/bias" in name:
return shape
patterns = [
"self_attentio... |
def _exact_phrase(phrase):
"""
Returns a query item matching messages that have an exact phrase.
Args:
phrase (str): The exact phrase to match.
Returns:
The query string.
"""
return f'"{phrase}"' |
def coding_problem_48(io, po):
"""
Given pre-order and in-order traversals of a binary tree, write a function to reconstruct the tree.
Example:
>>> def pre_order(tree):
... return [] if tree is None else [tree[0]] + pre_order(tree[1]) + pre_order(tree[2])
>>> def in_order(tree):
... ... |
def listangar(a_list):
"""this method recives a list and returns the first an last elements as a list"""
listanga = [a_list[0],a_list.pop()]
return listanga |
def ztf_magnitude_zero_point(bands=''):
"""
Sample from the ZTF zeropoint distribution
"""
dist = {'g': 26.325, 'r': 26.275, 'i': 25.660}
return [dist[b] for b in bands.split(',')] |
def isnum(*xs):
""" Tests if all of xs are numeric
:param xs: vals of whatever
:returns: True or False
"""
try:
for x in xs:
float(x)
return True
except (ValueError, TypeError):
return False |
def make_table_line(input_list, header_flag):
"""wandelt eine Liste in eine HTML-Tabellenzeile um. Wenn das Flag sesetzt ist, wird eine Kopfzeile erzeugt."""
tag_str_open = "<td>"
tag_str_close = "</td>"
if header_flag:
tag_str_open = "<th>"
tag_str_close = "</th>"
output_stri... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.