content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
import click
def prompt_text(text, default=None):
"""Prompt user text input"""
prompt_ans = click.prompt(click.style(text, fg="blue"), default=default)
return prompt_ans | 0df96f28370e87fe4fb2a35f00c97610573afeba | 561,173 |
from datetime import datetime
def change_date_format(date: str) -> str:
"""
Parsing "%Y-%m-%d to %m/%d/%Y
"""
datetime_object = datetime.strptime(date, "%Y-%m-%d")
return datetime_object.strftime("%m/%d/%Y") | 284910b267e87f4616b4d0e8aa4aefd3db9fcafe | 639,078 |
def deep_round(A, ndigits=5):
"""
Rounds numbers in a list of lists. Useful for approximate equality testing.
"""
return [[round(val, ndigits) for val in sublst] for sublst in A] | cebfb6b2dbe83bcc7222e0dc1b67ca98e95576c5 | 18,719 |
import gzip
def opengz(filename, mode):
"""Return either the system open or the gzip open """
if filename.endswith('.gz'):
return gzip.open(filename, mode)
else:
return open(filename, mode) | 1e0ed8489bc1059e3edf23de49f2420a224d932d | 265,040 |
import binascii
import re
def humanhexlify(data, n=-1):
"""Hexlify given data with 1 space char btw hex values for easier reading for humans
:param data: binary data to hexlify
:param n: If n is a positive integer then shorten the output of this function to n hexlified bytes.
Input like
'ab\x... | 883323524ecc8b9f55138d290a38666e5c06bac3 | 699,934 |
def get_members(api, operation):
"""
Gets the operation's members from api.json. Includes metadata such as required
parameters needed for the schema.
Example: get_members(api, 'RegisterTaskDefinitionRequest')
:param api: the api.json file read into JSON format
:param operation: the type of task... | a28d1f3e1385ba37760c4c5055ee253493f405fe | 547,709 |
def clip(text, max_len=80):
"""Return max_len characters clipped at space if possible"""
text = text.rstrip()
if len(text) <= max_len or ' ' not in text:
return text
end = len(text)
space_at = text.rfind(' ', 0, max_len + 1)
if space_at >= 0:
end = space_at
else:
spac... | 01530ebe0e25a03c4f49204b7816d0f000796a43 | 657,813 |
def apply_message_parser(parser):
"""
Decorator that passes the result of a message parser to a handler as kwargs.
The parser will only be passed a `message` kwarg.
"""
def inner_decorator(handler):
def wrapped(client, message):
parser_result = parser(message=message)
... | 400fa032e2ce84a8e19bec8d11216b052a0b24e1 | 120,670 |
import torch
def l2_normalize(x, dim=None, eps=1e-12):
"""Normalize a tensor over dim using the L2-norm."""
sq_sum = torch.sum(torch.square(x), dim=dim, keepdim=True)
inv_norm = torch.rsqrt(torch.max(sq_sum, torch.ones_like(sq_sum)*eps))
return x * inv_norm | 2247413b4ce424c13563169ed0a8f31cfb3f1a3c | 319,457 |
def addf(a, b):
"""Add values without converting them to integers (as |add seems to do)"""
return a + b | 9c9822fb849bd00fe25411e75f46010819ed5e92 | 547,285 |
import torch
def reshape(input, shape):
"""
Returns a tensor with the same data and number of elements as ``input``,
but with the specified shape. When possible, the returned tensor will be a view of ``input``.
Examples::
>>> import torch
>>> import treetensor.torch as ttorch
... | b75a57b9aaf00ea006ea3fc0ab04c759c1e69663 | 182,836 |
def uniq_stable(elems):
"""uniq_stable(elems) -> list
Return from an iterable, a list of all the unique elements in the input,
but maintaining the order in which they first appear.
A naive solution to this problem which just makes a dictionary with the
elements as keys fails to respect the stabili... | 15ecacbb1561cc1b972c5bd974286c128ee01488 | 553,872 |
import torch
def calc_discounted_return(rewards, discount, final_value):
"""
Calculate discounted returns based on rewards and discount factor.
"""
seq_len = len(rewards)
discounted_returns = torch.zeros(seq_len)
discounted_returns[-1] = rewards[-1] + discount * final_value
for i in range(... | d937ae4f066e275a32e547541505b7ad9c28b8ba | 405,447 |
import torch
def is_tensor_like(inp):
"""
Returns ``True`` if the passed-in input is a Tensor-like.
Currently, this occurs whenever there's a ``__torch_function__``
attribute on the type of the input.
Examples
--------
A subclass of tensor is generally a Tensor-like.
>>> class SubTe... | 3084351eb5bf7cb26dcba0c3aa4b22f09293e960 | 392,128 |
def get_flags(s):
"""
Get flags, replacing empty flags with '_' for clarity (' S ' becomes '_S_')
"""
m_flag = s.read(1)
m_flag = m_flag if m_flag.strip() else '_'
q_flag = s.read(1)
q_flag = q_flag if q_flag.strip() else '_'
s_flag = s.read(1)
s_flag = s_flag if s_flag.strip() else ... | c663b57adea3724e6c6b9d553497c5defa3d8111 | 208,767 |
import torch
def euclidean_reward(target_mel, pred_mel, reward_min=None, reward_max=None, verbose=0):
"""Negative Euclidean norm of the difference between the two
flattened inputs. If reward_min or reward_max values are given, it caps
the reward at appropriate levels.
"""
target_mel = torch.flat... | f01a11a8c2fa51f09dc5529dfc605b93a0021b98 | 86,799 |
def get_by_pickup(state,b1):
"""Generate a pickup subtask."""
if state.is_true("clear", [b1]): return [('pickup_task',b1)]
return False | 3a5a6c9960b210ca8d860f914fb848472590d0be | 594,220 |
from typing import List
from typing import Match
import re
def replace_math(input: str, math: List[str]) -> str:
"""
input: a string, already processed into HTML by some markdown renderer;
may contain @@number@@ blocks indicating where math was removed by
remove_math.
math: a list of strings cont... | 5103c2cea2e5a085c4461f5cac8f05c77411d085 | 186,363 |
import re
def header_match(line):
""" Return True if line match the HPROF header line, False otherwise"""
pattern = 'JAVA PROFILE \d.\d.\d, created \w+ \w+ +\d+ \d{2}:\d{2}:\d{2} \d{4}'
return re.match(pattern, line) is not None | 19aa37c993d3d5a1d9bf50224b853bf54477c6da | 199,755 |
def to_wiki_format(site, title, ignore_ns=False):
"""Convert a page title or username to 'canonical' wiki format.
Wiki format uses spaces instead of underscores, and capitalizes the first
letter. It also uses custom (not canonical) namespace names. The first
parameter should be a Pywikibot Site object,... | 9141457b10496fcbc17c84614bc13a0e20815682 | 561,326 |
def l1_loss(x: float, y: float) -> float:
"""
Compute the L1 loss function.
This is the absolute value of the difference of the inputs.
https://www.bitlog.com/knowledge-base/machine-learning/loss-function/#l1-error
Parameters
----------
x : float
The estimate
y : float
... | 361e52b5dc2c72e259c55137686fbe0efb2b0f05 | 171,980 |
def collect_attributes(attributes, *containers):
"""Collect attributes from arguments. `containers` are objects with
method `all_attributes` or might be `Nulls`. Returns a list of attributes.
Note that the function does not check whether the attribute is an actual
attribute object or a string."""
# ... | dd5beaa29dbb7b6d0b137dda8154ae492dd8dae2 | 291,440 |
def make_default_user_query(users, query_data, values, search_on, extra_data={}):
"""
given a query_data dict and values which come from the ui,
generate a dict that will be used for a user query
this default query is a within query, that optionally adds some
extra key/value data to the query dict
... | eb055b6fcace0425d783973ca9dc75932c862f28 | 439,252 |
def get_nice_size(size, base=1000.0):
"""
Convert number of bytes to string with KB, MB, GB, TB suffixes
"""
if size < base:
return '%sB' % (size)
if size < base**2:
return '%.2fKB' % (size / base)
if size < base**3:
return '%.2fMB' % (size / base**2)
if size < bas... | f9a00fd3434f7892766ab481d76bd7c5f48f6355 | 517,971 |
def make_figure_basename(local_station_id,
reference_station_id,
xy_or_yx,
matlab_or_fortran):
"""
Parameters
----------
local_station_id: str
station label
reference_station_id: str
remote reference stat... | c6d5e29215050cd507220e7df2fb733e04a64d7e | 512,952 |
def pwd_checker(pwd):
"""A utility to use for verify_client_func. Checks a client's auth password"""
return (lambda id_data: pwd == id_data.get("pwd")) | aaa0702ebdc296bb0a1e2df308ed0db40b0cc367 | 570,391 |
def removeNCharsFromCol(data, n, col, start):
""" Removes n characters from the value of a given column
for every row either from the start or the end of
the string
:param data: The data to process
:param n: The number of characters
:param col: The index of the column to alt... | 195ddde00341103175ef4110b25706785d0e59e3 | 426,904 |
def add_row(key, value):
""" Add a row to the HTML table """
body = '<td>' + key + '</td>\n'
body += '<td>' + str(value) + '</td>\n'
body += '</tr>\n'
return body | 5c8b78e25001df77a3560234a1eda8a2043a7c9d | 473,158 |
def select(S, i, key=lambda x: x):
"""
Select the element, x of S with rank i (i elements in S < x)
in linear time
Assumes that the elements in S are unique
Complexity: O(n)
Every step in this algorithm is approximately linear
time, the sorting here only ever happens of lists of
length <= 5
"""
#... | 25e2efe2da161af70f4355859f42867b5570b0b8 | 358,777 |
def control_change_rate_cost(u, u_prev):
"""Compute penalty of control jerk, i.e. difference to previous control input"""
return (u - u_prev) ** 2 | 09439fa7b2b6afa306e41e85434db3064be0a481 | 637,904 |
def get_area_by_player_position(areas, x, y):
"""
This function checks the area the player is in given its position in the maps
"""
epsilon = 1 # Variance of 1 block
for i, area in areas:
if (
area["x1"] - epsilon <= x <= area["x2"] + epsilon
and area["y1"] - epsilon... | 88469e04ff95f74311a5427399a4c2c4dec00bc8 | 192,339 |
import ast
def handle_operator_code(self, opcode):
"""
Parses an operator code and returns its string representation.
Returns an empty string on error.
"""
if isinstance(opcode, ast.Add):
op = "+"
elif isinstance(opcode, ast.Sub):
op = "-"
elif isinstance(opcode, ast.Mult):... | 701cac1ad4485115b3aca94246e06bd1fee504dc | 76,264 |
import re
def find_mapping(mapping, target):
"""Retrieves sample type from recipe
Args:
mapping (dic): regex-to-value mapping
target (string): target match for regexes
Returns:
value (any): desired mapping of target
"""
for key_re, val in mapping.items():
expr = re.compile(key_re, re.IGNORECA... | 574b28e91734d78cfa4fe299d8939ca497136467 | 175,070 |
def add_dicts(dict1, dict2) -> dict:
"""
Return a dictionary with the sum of the values for each key in both dicts.
"""
return {x: dict1.get(x, 0) + dict2.get(x, 0) for x in set(dict1).union(dict2)} | 3a17303cee6005bb87ed779dae20fce7e30ef148 | 155,211 |
import math
def fcst_hr_min(time,start):
"""!Return forecast time in hours and minutes.
Given a forecast datetime.datetime and an analysis
datetime.datetime, this returns a tuple containing the forecast
hour and minute, rounded to the nearest integer minute.
@param time forecast time as a datetim... | 2916387b979e8825501f15fd683cb4a64d4bc937 | 485,475 |
def del_suffix(target: str, suffix: str):
"""
If `target` ends with the `suffix` string and `suffix` is not empty,
return string[:-len(suffix)].
Otherwise, return a copy of the original string.
"""
if (len(suffix) > 0) and (target.endswith(suffix) is True):
try: # python >= 3.9
... | ab9307e6b2673a68d5d87af25b02547e6755fb4b | 149,342 |
def concat_body_paragraphs(body_candidates):
"""
Concatenate paragraphs constituting the question body.
:param body_candidates:
:return:
"""
return ' '.join(' '.join(body_candidates).split()) | a0faaa0ae0be0cda007c2af1f6e47f3b745862b3 | 9,536 |
def partition(m):
"""Returns the number of different ways one hundred can be written as a sum
of at least two positive integers.
>>> partition(100)
190569291
>>> partition(50)
204225
>>> partition(30)
5603
>>> partition(10)
41
>>> partition(5)
6
>>> partition(3)
... | a629145d817089ecb3c2943ab3c3bb195dc9d016 | 446,823 |
import unicodedata
def remove_control_characters(s):
"""Some input (like filenames) has some really nasty control chars.
This trick removes those (https://stackoverflow.com/a/19016117)"""
return "".join(ch for ch in s if unicodedata.category(ch)[0] != "C") | f47788f52ccd9d0863349e52d766ecbcc3f64c06 | 549,447 |
def read_nix_prop(prop):
"""Returns a nox property value across different nix versions.
:param prop: the nix Property.
:return: The value stored in the property.
"""
try:
return prop.values[0].value
except AttributeError:
return prop.values[0] | a330216dfc3c080e466d5c18e2b9368ca02cf9b3 | 170,263 |
def available_colors(G, vertex, number_of_colors):
"""Returns all the available colors for vertex
Parameters:
G: a networkx graph with Graph Nodes
vertex: the vertex number (int)
number_of_colors: the number of colors (int)
Returns:
colors: list of available colors (list)
... | b19dfe9516eb7a74d259a3d69b868e78fe56d3e9 | 696,341 |
from typing import Iterator
import json
def dictsets_match(dictset_1: Iterator[dict], dictset_2: Iterator[dict]):
"""
Tests if two dictsets match regardless of the order of the order of the
records in the dictset. Return is True if the sets match.
Note that this will exhaust a generator.
Paramet... | c966c553096851c876f4f4f14675073f861d3372 | 576,215 |
from pathlib import Path
def get_image_paths(image_folder: Path) -> list:
"""
Get all images in image folder ending in .jpg or .png
"""
suffix_set = {'.jpg', '.png'}
return [
image for image in image_folder.iterdir() if image.suffix in suffix_set
] | edbeacbe899f2e19a1d8a460d2b30215e28c2a46 | 643,909 |
import torch
def dice_loss(yhat, ytrue, epsilon=1e-6):
"""
Computes a soft Dice Loss
Args:
yhat (Tensor): predicted masks
ytrue (Tensor): targets masks
epsilon (Float): smoothing value to avoid division by 0
output:
DL value with `mean` reduction
"""
# compute ... | 332ff43c0ce0f46b2b577823eeb3becbe1087aac | 510,257 |
def safe_zip(*args):
"""zip, with a guarantee that all arguments are the same length.
(normal zip silently drops entries to make them the same length)
"""
length = len(args[0])
if not all(len(arg) == length for arg in args):
raise ValueError("Lengths of arguments do not match: "
... | 24b0c8c84f7f45d32ca4cc41bcbdf89327973191 | 399,751 |
def _DecodeRawShape(op):
"""Shape function for the DecodeRaw op."""
# NOTE(mrry): Last dimension is data-dependent.
return [op.inputs[0].get_shape().concatenate([None])] | 40d90298ac0bb672d14469b681a96da5b83fb7e7 | 146,811 |
def isVC(E, C):
"""checks if C is a vertex cover for graphs E
C -- set of vertices
E -- graphs represented as a list of edges
returns True/False"""
for (x, y) in E:
if (x not in C) and (y not in C):
return False
return True | 35faa426df5ed64bfa956b88d594bf05e9338291 | 475,537 |
def abs_value_equal(x, y):
"""Return whether or not the absolute value of both numbers is the same.
Please refrain from using libraries (abs)
>>> abs_value_equal(-2, -2)
True
>>> abs_value_equal(-3, 3)
True
>>> abs_value_equal(1, 2)
False
>>> abs_value_equal(3, 3)
True
>>> ... | 5047caa23e0e1fd78e8696f706c5aa8ad43b0e22 | 48,291 |
def Top5Criterion(x,y, model):
"""Returns True if model prediction is in top5"""
return (model(x).topk(5)[1]==y.view(-1,1)).any(dim=-1) | 2442e70360a22ecd538aa378baeadf44b431213f | 397,751 |
def is_valid_iteration(n):
"""
Returns: True if n is an int >= 1; False otherwise.
Parameter n: the value to check
Precondition: NONE (n can be any value)
"""
return (type(n) == int and 1 <= n) | a1ad9722af342f6b33d4380bde3bf97071cc1dc5 | 565,667 |
import hashlib
def get_hashed(key):
"""
Returns a hashed version of a given key.
One way encryption.
"""
return hashlib.md5(key.encode('utf-8')).hexdigest() | cc88aeb0073f8e5733c11cef613ed009ca55aaf0 | 343,380 |
def remove_vectored_io_slice_suffix_from_name(name, slice):
# type: (str, int) -> str
"""Remove vectored io (stripe) slice suffix from a given name
:param str name: entity name
:param int slice: slice num
:rtype: str
:return: name without suffix
"""
suffix = '.bxslice-{}'.format(slice)
... | 00041b582eff2bf1a78717dfaf5ed00d042d8c94 | 394,469 |
def splitter(model):
"""
Splits model parameters into multiple groups to allow fine-tuning
"""
params = list(model.parameters())
return [
# weights and biases of the first linear layer
params[:2],
# weights and biases of the second linear layer
params[2:],
] | 2ac16b536bd50884c50ade3fb67f6ca43a16799d | 13,517 |
from typing import Iterable
from typing import Optional
def mconcat(xs: Iterable[Optional[str]]) -> str:
"""Concatenate strings that are not None from a sequence."""
return ''.join([x for x in xs if x is not None]) | a1c91f442bd9b1c77a7fbd1eba5705032a079284 | 295,232 |
def ismarionette(session):
""" bool: Whether the session is using Marionette. """
return getattr(session.driver, "_marionette", False) | 24368759f626ee1c1d3b6aade19fcf8ad0b19f17 | 131,424 |
def _update_versions_in_requirements(requirements: "list[str]", packages: dict) -> str:
"""Update the versions in requirements with the provided package to version mapping."""
for i, package in enumerate(requirements.copy()):
if package.startswith("#"):
continue
if not len(package.st... | 374b4b6c3f212754a011511bf59d71f8e6e3a5d6 | 509,701 |
def unsort(input, indices, dim=1):
""" unsort the tensor based on indices which are created by sort """
""" dim is the dimension of batch size """
output = input.new(*input.size())
output.scatter_(dim, indices.unsqueeze(0).unsqueeze(2), input)
return output | 49cf5555d087942796289e7378198f2943345f7c | 135,044 |
def distance(a, b):
"""Simple distance between two 1D arrays"""
return ((a - b) ** 2).sum() ** (0.5) | 073b5e7ceb41266dd8edd1cafd62da140ac55846 | 615,419 |
def filter_example(ex, use_src_len=True, use_mt_len=True,
use_pe_len=True,
min_src_len=1, max_src_len=float('inf'),
min_mt_len=1, max_mt_len=float("inf"),
min_pe_len=1, max_pe_len=float("inf")):
"""Return whether an example is an acceptable... | bbec0e3b3e4e13485eea698bc7a6a9e726ffbd78 | 585,646 |
def fish(fs):
"""
Transform a list of fish represented by their time-to-spawn state into a list of the
number of fish in each state indexed by their time-to-spawn.
Under this strategy, the example input [3, 4, 3, 1, 2] would be transformed into
[0, 1, 1, 2, 1, 0, 0, 0, 0].
"""
fis = [0] * 9... | 79837d897c730be13b150eb56d9c5bb49d8bb643 | 170,344 |
def get_el_feedin_tariff_chp(q_nom, el_feedin_epex=0.02978, vnn=0.01):
"""
Calculates feed-in tariff for CHP-produced electricity.
Parameters
----------
q_nom : nominal thermal power of chp in kW
el_feedin_epex : epex price for electricity in Euro/kWh
vnn : avoided grid charges ("vermiedene... | d25c28aa678a5b30a2c13f143b3cfb46cd9d2edb | 609,194 |
def check_padding(query):
"""
Check for missing padding in base64 encoding and fill it up with "=".
:param query:
:return: query
"""
missing_padding = len(query) % 4
if missing_padding:
query += "=" * (4 - missing_padding)
return query | d8ad3c96074d311dbd5ba17bb93d7ca7a8b5ccab | 10,841 |
def create_set_list(test_set, fil_df):
"""
This function creates the list that is going to be appended to the dataframe to determine which specific sentences
in the data frame are a part of the testing and training sets. When this list is added to the dataframe as a column
called 'set' it is going to ha... | 575789eae77e916ac7c47620ee42da4e7efb351c | 369,331 |
def _ensure_rst_content_directive(text: str):
"""
Ensures a `contents` directive at the top of every document.
"""
return (
'.. contents::\n'
'\n' +
text
) | 598ae77a87cfb648620f96d0d1b516a449c3befb | 140,019 |
def collect_fragments(folder, foldername, order):
"""Collects the fragments in 'folder' and returns the pathnames of the
fragements (starting from folder) ordered by the value of the order
metadata parameter in each fragment.
"""
fragments = [entry for entry in folder if folder[entry].is_fragment()]... | dde6b4622f93a2f2a934a479e10a894480e56fe2 | 500,713 |
import math
def logit(x):
"""Inverse sigmoid for a single value x"""
return math.log(x / (1 - x)) | eb56c75166bfb768cec64990d11afdb6352784d8 | 516,670 |
import json
def json_loads(data, *args, **kwargs):
"""A custom JSON loading function which passes all parameters to the
json.loads function."""
return json.loads(data, *args, **kwargs) | 1fa1c4af090920e9eb82a8c32b0a1418c07f5801 | 245,706 |
def trajectory_importance_max_avg(states_importance):
""" computes the importance of the trajectory, according to max-avg approach:
delta(max state, avg) """
avg = sum(states_importance) / len(states_importance)
return max(states_importance) - avg | 8fde9c62c661f8ea2041a159864d2c27e662988d | 64,266 |
from typing import Union
from pathlib import Path
from typing import List
def parse_file_to_list(file: Union[Path, str]) -> List[str]:
"""
Given a text file, read contents to list of lines. Strip lines, ignore empty and comment lines
Args:
file: Input file.
Returns:
List of lines.
... | 1c5208399eb42658aca80debc169399bf7d32579 | 273,288 |
def average(lst):
"""
calculate average of a list (of numbers)
"""
return sum(lst) / len(lst) | dc18524ac4abf4e7fd98a7fc6e11bace04686747 | 53,052 |
import json
def fetch_abi(erc721=False):
"""
Fetch ABI of a given contract, if it is verified
Return default ABI, otherwise
:param erc721: if the contract is ERC721
:type erc721: bool
:return: ABI of the contract
:type: dict
"""
if erc721:
with open('app/abi/default-erc721.... | d5074ecf3142161e63116c34b2028b18feb58ee7 | 554,077 |
import torch
import math
def generate_A_with_L_mu(n, d, L, mu=-1):
"""
Generate a data matrix A for
f(x) = \frac{1}{n} || A x - b ||_2^2
with L-smoothnes and mu-convexity.
The L-smoothness is the largest eigenvalue of the hessian of f
hessian(f)(x) = \frac{2}{n} A^T A
"""
asse... | b511429ce28480137f7b229bac4c1dc683e8bbf5 | 319,677 |
def select_atoms_from_list(PDB_atom_name, atoms_list):
"""
Given a pdb atom name string and a list of atoms (BioPython Atom) it returns the Bio.Atom correspondent to the atom
name.
:param PDB_atom_name: string with an atom name
:param atoms_list: list of Bio.Atoms
:return: Bio.Atom correspondent... | fbeec150431898407bac6b000759dcaa5908463c | 422,969 |
def ner_to_sent(sent, replaced, tag="<NE>"):
"""
Args:
- sent is the sentence that has the NER tags in them instead of
the actual named entities.
- replaced is the corresponding list of named entities that
will be inserted in the order of appearance for the tags.
- ta... | 6c2d0d6e3f88ef23b7924eca98d28e1adda47b28 | 500,988 |
def _lin_f(p, x):
"""Basic linear regression 'model' for use with ODR.
This is a function of 2 variables, slope and intercept.
"""
return (p[0] * x) + p[1] | 52bc228dd48ee7939fa60cd052989de70a44b197 | 698,315 |
import math
def crop(pois, lat, long, max_dist):
"""
Crop a list to points that are within a
maximum distance of a center point.
:param pois: list of lats and longs
:param lat: center lat
:param long: center long
:param max_dist: max distance
:return: a filtered list
"""
# Conv... | 687dd3b073fc5e20fb7e3e4b0b511374a590bf14 | 127,823 |
def nvl(value, default):
""" Evaluates if value es empty or None, if so returns default
Parameters:
value: the evalue to evaluate
default: the default value
Returns:
value or default
"""
if value:
return value
return default | 67df45a6e63c107dcef99fc7bdbaa7064b695f66 | 700,438 |
def unique(valuelist):
"""Return all values found from a list, but each once only and sorted."""
return sorted(list(set(valuelist))) | 1218bb7c353a898b2815c5cd95b8ef71e386b91f | 30,023 |
def normalise_activity_by_sector_month(
topic_activity, sector_month_labels, sector_variable="sector"
):
"""Normalise s.t. each [sector, month] sums to 1."""
norm_factor = (
sector_month_labels.dropna()
.groupby(
[
"month",
sector_variable,
... | 0c9fb4d8cd3b877cc08568bedb7019cbbc40cd90 | 679,806 |
def e_mag2frac(errmag):
"""Convert mag error to fractionary flux error"""
return 10.**(.4*errmag)-1. | 2db0b3985d427cdf45ae76867396e06b6ee4b68d | 477,437 |
def get_idx_slice(axis, i, ndim):
"""
Returns a slice `s` where
mat[s] == mat[:, ---, :, i, :, ---, :]
|_______| |_______|
axis ndim - axis - 1
This allows indexing into an arbitrary dimension of an n-dimensional array.
"""
idx = [slice(None)] *... | 2bf90d2d8fc955a0fa14cfccb17bed991d0be84e | 295,027 |
import time
import json
async def tts(ws, text, voice):
"""Send a text-to-speech request on an established websocket connection.
:param ws: an established websocket connection
:param text: the text to be converted to an WAVE file
:param voice: voice used to generate the WAVE file
:return: the TTS... | 8b318f2524de729bc1038d7fb5385b0762e82bf2 | 525,587 |
def load_file(file_path):
"""Load the contents of a file into a string"""
try:
with open(file_path, 'r') as file:
return file.read()
except EnvironmentError: # parent of IOError, OSError *and* WindowsError where available
return None | 2cceabeb5b4bee494357001e7c6235acb99439fe | 218,690 |
def compute_something(a: float, b: int) -> float:
"""Sums `a` and `b`.
Args:
a: A brief explanation of `a`.
b: A brief explanation of `b`.
Returns:
float: The sum of `a` and `b`.
Notes:
The addition of an `int` and a `float` returns a `float`.
Mathematically,... | 997d6d811d10a2d70606addc76f7e906e8f9c73d | 15,783 |
import json
def load_weather_data(file_path):
"""Given the `file_path` to the file containing the weather data in JSON format,
this function loads the data back into a dictionary and returns it."""
with open(file_path) as weather_input:
weather_data = json.load(weather_input)
return weather_d... | dca0e6a59e8341cb9ebf8eca63e00eace52175d9 | 227,666 |
def merge(nums1, nums2):
"""
Merge two given sorted arrays by merge sort
:param nums1: first array
:type nums1: list[int]
:param nums2: second array
:type nums2: list[int]
:return: merged array
:rtype: list[int]
"""
result = []
i = j = 0
while i < len(nums1) and j < len(... | 4ec45d0c65bb85f45d9b8266bba268ace22177f6 | 634,872 |
def findMatching(device, device_arr):
"""
Check the array of devices to see if there is a matching element.
Args:
device (string): The name of the device attempting to be added.
device_arr (array): An array of the devices attached to a profile.
"""
matches = [x for x in device_arr i... | 388e7ee8c66d91f52935dfe61e9b2ce8cb3ac661 | 643,970 |
import string
def term_frequency(term: str, document: str) -> int:
"""
Return the number of times a term occurs within
a given document.
@params: term, the term to search a document for, and document,
the document to search within
@returns: an integer representing the number of times a... | fd3650360e2f1177c5e0514c10ba908767c2daf0 | 651,204 |
def heading_sub_degrees(a, b, mod=360):
"""
Calculate the difference from -mod to mod between two angles.
a > b will return positive difference, a < b will return negative difference
(this example does not account for all quadrant. Consider this example in
the first quadrant for clarity).
:par... | 20262062065c7de8b689f2dec34c7cdb1329e0fc | 190,680 |
def all_boolean_reductions(request):
"""Fixture for boolean reduction names."""
return request.param | f4ac578c5ab5777872c9345182b5f1081bd0f00d | 625,787 |
import json
def get_json_file_content(json_path: str):
"""
Get the json content of the json_path.
:param: json_path, the path to the json file.
:return: a dict representing the file content.
"""
with open(json_path) as json_file:
return json.load(json_file) | b77d6ac89107799cb5722c49a8cb0261a661c7f0 | 372,952 |
def vaf_to_ar(vaf):
"""
Convert VAF to AR.
VAF (variant allele frequency) = V-AF
AR (allele ratio) = V-AF / WT-AF
V-AF + WT-AF = 100 (%)
Note:
if VAF == 100:
AR = -1
(instead of 100 / 0)
Args:
vaf (dc.Decimal): VAF to convert.
Returns:
... | bf1847fd9d832d278f059e036d02cb198fc076f3 | 362,622 |
def get_classid(class_attribute, feat_name):
"""
Parameters
----------
class_attribute:
Contains the name of the attribute/column that contains categorical values
feat_name : string
Contains the name of the attribute/column
Returns
-------
class_idx:int
Returns... | f327204e3adb2c2a553c5a7c2d830573fed08dc5 | 531,106 |
def is_ref(frag):
""" Test whether a given Bokeh object graph fragment is a reference.
A Bokeh "reference" is a ``dict`` with ``"type"`` and ``"id"`` keys.
Args:
frag (dict) : a fragment of a Bokeh object graph
Returns:
True, if the fragment is a reference, otherwise False
"""
... | bd9fecac28c0f5aa4c5743c1c176806cdc8d865c | 350,618 |
def format_number(x):
"""Format number to string
Function converts a number to string. For numbers of class :class:`float`, up to 17 digits will be used to print
the entire floating point number. Any padding zeros will be removed at the end of the number.
See :ref:`user-guide:int` and :ref:`user-guide... | ac0e402dbbdc7d868a36f4176072905bfd54318a | 165,600 |
def strip_dict(d):
"""
Return a new dictionary, like d, but with any string value stripped
>>> d = {'a': ' foo ', 'b': 3, 'c': ' bar'}
>>> result = strip_dict(d)
>>> type(result)
<type 'dict'>
>>> sorted(result.items())
[('a', 'foo'), ('b', 3), ('c', 'bar')]
"""
return dict(... | 2267ed6e4bb48a84a0213ce85f88ae7f1ffd8e33 | 478,456 |
def elem(x, xs):
"""
elem :: Eq a => a -> [a] -> Bool
elem is the list membership predicate, elem(x, xs). For the result to be
False, the list must be finite; True, however, results from an element
equal to x found at a finite index of a finite or infinite list.
"""
return x in xs | e5a40b1a89f40ce7d005f1d6313f5dd5fd8fe581 | 339,186 |
def partition(xrange, sz, overlap=0, skip=[]):
"""Divide range into subsets specified by size, overlap, and indices to skip.
Args:
xrange (range)
sz (int): target size of subset
overlap (int): amount of overlap between subsets
skip ([int]): indices to skip in start/end of subset... | 661c2fd33b0c5b0428ec35e2599b73d3a0bedbae | 337,458 |
def read_file_lines(file):
"""Returns a list of str lines in text file"""
with open(file, mode="rt", encoding="utf-8") as f:
return f.readlines() | dac8acbf25c7c9833f20b913d4aff068afa57fa9 | 644,334 |
def ordinal_suffix(n):
"""Return the ordinal suffix for a positive integer
>>> ordinal_suffix(0)
''
>>> ordinal_suffix(1)
'st'
>>> ordinal_suffix(2)
'nd'
>>> ordinal_suffix(3)
'rd'
>>> ordinal_suffix(4)
'th'
>>> ordinal_suffix(11)
'th'
>>> ordinal_suffix(12)
'th'
>>> ordinal_suffix(13)
... | 53617737aaf28c2d239301358f01d1b0cea9f6cb | 10,604 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.