content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
def get_word(word):
"""Return normalized contents of a word."""
return "".join([n.string.strip() for n in word.contents if n.name in [None, "s"]]) | 7625e485b66a95e53cb9d0ccea30190408d7579b | 652,513 |
def hmean(*args):
"""Calculate the harmonic mean of the given values.
http://en.wikipedia.org/wiki/Harmonic_mean
Args:
*args: List of numbers to take the harmonic mean of.
Returns:
Harmonic mean of args, or NaN if an arg is <= 0.
"""
for val in args:
if val <= 0:
return float('NaN')
re... | 2083ce1f9ea321827494fbcdfa45ed3edba56bc5 | 206,547 |
def kmp(sequence, sub):
"""
Knuth–Morris–Pratt algorithm, returning the starting position
of a specified sub within another, larger sequence.
Often used for string matching.
"""
partial = [0]
for i in range(1, len(sub)):
j = partial[i - 1]
while j > 0 and sub[j] != s... | dae57355b8da6e7f33662196c78d6f3832a8d009 | 187,004 |
from typing import Union
import torch
def convert_dict_entries_to_tensors(
loss_params: dict, device: Union[str, torch.device]
) -> dict:
"""Set scalars in loss to torch tensors for use with unsupervised losses.
Args:
loss_params: loss dictionary to loop over
device: device to send floats... | 3620a7a9858d14c0bb01319a92a406a6cf525946 | 72,317 |
def typeOf(value):
"""
Get the type of value.
:param value: What to get the type of.
:type value: anything
:returns: str
"""
return str(type(value)) | 081500ca6554f71b0a87439be16bd3fb067bdc2a | 607,782 |
def get_ages(df, sex):
"""Returns the ages of men or women, whom Embarked from section C.
Parameters:
df(pyspark.sql.DataFrame): based on the titanic.csv module
sex(str): for our dataset, it is a binary classification of 'male' or 'female'
Returns: List[float]
"""
... | 93d49fc11845b8dc560c7235e441eb7b101d19d1 | 643,327 |
from pathlib import Path
import logging
def evaluate_implementation(callback_file: Path, function_name: str) -> bool:
"""Checks whether a function name is found in a source file or not."""
found = False
impl = callback_file.read_text()
if function_name in impl:
logging.info(f"Found '{function_... | 3ad087dc2db09aeb78874c9b934a553f7457e511 | 38,088 |
def task_id_in(ids, body, message):
"""Return true if task id is member of set ids'."""
return body['id'] in ids | 2e9576dab616f70a1181e2f52808e24bb295ca17 | 314,383 |
from typing import Callable
def function_name(func: Callable) -> str:
"""
Return the qualified name of the given function.
Builtins and types from the :mod:`typing` package get special treatment by having the module
name stripped from the generated name.
"""
# For partial functions and objec... | 4b992fdcfa4e47a0a289d0eba7a97136625d8e5d | 430,163 |
def open_symbols_file(index):
"""
This function accept an index symbol as parameter, opens the corresponding file
and returns a list of the symbols.
Parameters:
----------
index: str, the index symbol.
Returns:
-------
symbols: list of symbols.
"""
f = open('data/symbols_files/' + index + '_symbols.dat'... | bbc303f73a2b0a408c3e9416d6746a376fe12d57 | 442,362 |
def validateDSType(dsType):
"""
>>> validateDSType('counter')
'COUNTER'
>>> validateDSType('ford prefect')
Traceback (most recent call last):
ValueError: A data source type must be one of the following: GAUGE COUNTER DERIVE ABSOLUTE COMPUTE
"""
dsType = dsType.upper()
valid = ['GAUGE... | 61cbab13382d2cf997a22bc2b993be18d00d6cfb | 246,700 |
from typing import Any
def _make_arg_str(arg: Any) -> str:
"""Helper function to convert arg to str.
:param arg: argument
:type arg: Any
:return: arg converted to str
:rtype: str
"""
arg = str(arg)
too_big = len(arg) > 15 or "\n" in arg
return "..." if too_big else arg | 480161c8061df2fdfc14f051fd0c29657fbebc56 | 82,608 |
def next_power_of_two(num):
"""Returns the next power of two >= num.
Args:
num: A positive integer.
Returns:
The next power of two >= num.
"""
val = 1
while val < num:
val *= 2
return val | f15a8e303612a8537c65eb33f7a43b88e046393d | 354,320 |
def state_symbols_match(pattern, value):
""" returns True if the value matches the pattern, where '.' can be used
as placeholders that match every state """
return all(a == '.' or a == b
for a, b in zip(pattern, value)) | 4398bbd23d9ab526784c6109cc54251f5fb6d9f4 | 167,461 |
def evaluate_g8( mu, kappa, nu, sigma, s8 ):
"""
Evaluate the eighth constraint equation and also return the jacobian
:param float mu: The value of the modulus mu
:param float kappa: The value of the modulus kappa
:param float nu: The value of the modulus nu
:param float sigma: The value of the... | d1be8deb9f38dae55082a341e2501044d7b2aef7 | 700,981 |
def bb_center(row):
""" Takes a twitter place bounding box (in lon, lat) and returns the centroid of the quadrilateral (in lat, lon)."""
bb_coords = row[0]
avg_lon = (bb_coords[0][0] + bb_coords[1][0] + bb_coords[2][0] + bb_coords[3][0])/4
avg_lat = (bb_coords[0][1] + bb_coords[1][1] + bb_coords[2][1] ... | 2c2dd977b92b514c75ebb82de797466ef2d497fa | 562,972 |
import mmap
def get_num_lines(file_path: str) -> int:
"""Count the number of lines in the log file
Parameters
----------
file_path: str
The filepath to the file to be counted
Returns
-------
int
The number of lines in the file
"""
fp = open(file_path, "r+")
bu... | 6ebed4620d57fa1959e4d8c2cb2db841fcb94dc7 | 577,521 |
def _fake_status_for_screenshot(status):
"""Return a fake status line for screenshots."""
return {
'stopped': '0:00:00 / 3:42:37 stopped [|| ]',
'playing': '0:20:85 / 3:42:37 playing [||| ]',
'recording': '0:11:37 / 3:42:37 recording * [|||||||| ... | 839983a2e68ab2e12bba1a8023333e616d72eeac | 438,053 |
def constant_function(c):
"""Create a constant function
Parameters
----------
c : float
constant to return
Returns
-------
function
Constant function equal to `c`
"""
return lambda x: c | 6f36f1ef9cb061073e01dc032cc568bc5da85155 | 315,611 |
def clarke_error_zone_detailed(act, pred):
"""
This function outputs the Clarke Error Grid region (encoded as integer)
for a combination of actual and predicted value
Based on 'Evaluating clinical accuracy of systems for self-monitoring of blood glucose':
https://care.diabetesjournals.org/content/1... | 83ddd6d5df4515c274cc4fa919a8716d6d08d4e6 | 75,278 |
def minRefuelStops_DP(
target_distance: int, start_fuel: int, stations: list[list[int]]
) -> int:
"""1-D Dynamic Programming
DP_table[num_stops]
the furthest distance (== max gas) that we can get
with num_stops times of refueling.
So for every station, stations[i],
if the curre... | af445b1af66e2758eb5a7f9ac0032a90b60963db | 657,049 |
def clip(value, min_v, max_v):
"""
Clip the given value to be within a range
https://stackoverflow.com/questions/9775731/clamping-floating-numbers-in-python
"""
return max(min(value, max_v), min_v) | cd4a5977832acbdc8522fa0637aa79743e586fe9 | 55,958 |
def count_nines(loss_rate):
"""
Returns the number of nines after the decimal point before some other digit happens.
"""
nines = 0
power_of_ten = 0.1
while True:
if power_of_ten < loss_rate:
return nines
power_of_ten /= 10.0
nines += 1
if power_of_ten ... | e95894f03b5d976a12537517cfaea02b41520fee | 605,657 |
def force_int(s):
"""Forcibly convert to int
:param s: any python object
:return: int or None
"""
try:
return int(s)
except ValueError:
return None | 93723eab77e4f7141a48bd4c0a5e01d825478d34 | 432,747 |
def clean_code(code: str, lengte: int) -> str:
"""
Schoont een code van voor- en naloop whitespace en voorziet de code van het juiste aantal voorloop nullen.
:param str code: De code.
:param int lengte: De gewenste lengte van de code.
:rtype: str
"""
return code.zfill(lengte) | 275ac814f8110675c5382b1a88ce68452d4a9f40 | 427,134 |
def get_child_object(obj, child_name):
"""Return the child object
Arguments:
obj {object} -- parent object
child_name {str} -- cild name
Returns:
object -- child object
"""
if hasattr(obj, '__getattr__'):
try:
return obj.__getattr__(child_name)
... | d9404e09cdeaaf755c75675e6c0dc42f5fc7adf2 | 51,221 |
def parse_logistic_flag(kwargs):
""" Checks whether y_dist is binomial """
if "y_dist" in kwargs:
if kwargs["y_dist"] == "binomial":
return True
return False | 2f4add10cebc7b0975212ca33acbbaed5717f13a | 458,735 |
def test_student(incorrect_msg, max_attempts, description, solution):
"""
Parameters: There are 4 parameters in this function:
-incorrect_msg_list is a list of strings that correspond to messages to show to the student if the answer is incorrect (e.g., [“Better luck next time”, “Not quite right”, “Try a... | d330c8528d695aa8c722dbb9dfe2e6c2ca95e83d | 283,604 |
def get_list_of(key, ch_groups):
"""
Return a list of values for key in ch_groups[groups]
key (str): a key of the ch_groups[groups] dict
ch_groups (dict): a group-name-indexed dict whose values are
dictionaries of group information (see expand_ch_groups)
Example: get_list_of('system', ch_g... | f5bbc32c836d0de5e9aa2c055d97bacdfc794d0e | 14,546 |
import pkg_resources # part of setuptools
def get_pkg_version(pkg_name, parse=False):
"""
Verify and get installed python package version.
:param pkg_name: python package name
:param parse: parse version number with pkg_resourc.parse_version -function
:return: None if pkg is not installed,
... | 11d8ffb604cb403f461e1eab814b48c5807d95eb | 438,482 |
def is_rescue_entry(boot_entry):
"""
Determines whether the given boot entry is rescue.
:param BootEntry boot_entry: Boot entry to assess
:return: True is the entry is rescue
:rtype: bool
"""
return 'rescue' in boot_entry.kernel_image.lower() | ba456c2724c3ad4e35bef110ed8c4cc08147b42c | 4,208 |
def rate(hit, num):
"""Return the fraction of `hit`/`num`."""
return hit / (num or 1.0) | b055fa6995f85ab223747dd369a464644046f7d7 | 696,493 |
def extract_active_ids(group_status):
"""Extracts all server IDs from a scaling group's status report.
:param dict group_status: The successful result from
``get_scaling_group_state``.
:result: A list of server IDs known to the scaling group.
"""
return [obj['id'] for obj in group_status['g... | 2247e1aec63058e78b32e4d837e14ef5d978db16 | 95,639 |
import io
import torch
def reload_script_model(module):
"""
Save a jit module and load it back.
Similar to the `getExportImportCopy` function in torch/testing/
"""
buffer = io.BytesIO()
torch.jit.save(module, buffer)
buffer.seek(0)
return torch.jit.load(buffer) | d3bb5b9efa7c6a9cd28fd007d9cc18cd65475baf | 290,473 |
def getColumnsEndingAt(columns, zLevel):
"""Returns columns w/ endPoint at zLevel"""
columnGroup = {}
for columnID, column in columns.inventory.iteritems():
diff = abs(zLevel - column.endJoint.z)
if diff <= 0.001:
columnGroup[column.uniqueName] = column
return columnGroup | 4f6b7aac922bd5985b6faeb509d26bf6aec98629 | 696,047 |
def find_max(a):
"""
Find the coordinates (i0, j0) in terms of pixels of the maximum correlation peak from
the polynomial surface fit parameters
:param a: List of polynomial surface fit parameters returned by polyfit2d()
Returns i0, j0
"""
i0 = ((a[2]*a[3]) - (2*a[0]*a[4])) / (4*a[0]*a[1]... | 8ee0a6ce5866e62ad7708e7696495cebe0c745b4 | 475,293 |
import toml
import logging
def _parse_conf(conf_file: str):
"""Parses the incoming toml config file"""
try:
conf = toml.load(conf_file)
except Exception as e:
logging.warning(f"Failed to decode TOML file: {e}. Using chrome as browser.")
return "chrome", "", "", "", ""
else:
... | af5e659305210db6f74a1e99870506034a6bc4d9 | 606,555 |
from typing import Tuple
def to_slice(start: Tuple[int, ...], shape: Tuple[int, ...]):
"""Convert start and shape tuple to slice object"""
return tuple(slice(st, st + sh) for st, sh in zip(start, shape)) | 99bd9ef3ec37e23cabf50c45dbe13fff14c29eec | 540,376 |
def _cut_series_by_rank(df, ts_settings, n=1, top=True):
"""
Select top-n or bottom-n series by rank
df: pandas df
ts_settings: dict
Parameters for datetime DR projects
n: int
number of series to select
top: bool
Select highest (True) or lowest series (False)
... | 2032b16d67cd38413abd985d40a5084b5c3a11d4 | 165,689 |
def get_type_and_data(h_node):
""" Helper function to return the py_type and data block for a HDF node """
py_type = h_node.attrs["type"][0]
if h_node.shape == ():
data = h_node.value
else:
data = h_node[:]
return py_type, data | 49c2478bb02c370a7e916aef7cd256fa1dd92a77 | 358,951 |
def alpha(requestContext, seriesList, alpha):
"""
Assigns the given alpha transparency setting to the series. Takes a float value between 0 and 1.
"""
for series in seriesList:
series.options['alpha'] = alpha
return seriesList | 25ef9bad1b39549585bdb175e487236591d0816f | 16,194 |
def amortization(loan, r, c, n):
"""Amortization
Returns: The amount of money that needs to be paid at the end of
each period to get rid of the total loan.
Input values:
loan : Total loan amount
r : annual interest rate
c : number of compounding periods a year
n : total number of compo... | 9f0a9c4f51c623a7935fcd595604ce9593a9530e | 406,580 |
from functools import reduce
def process_group(grp):
"""
Given a list of tokens of tokens
where each token is an `x` or a
number, compute the minimum value
of t such that:
For all indices i such that arr[i] != 'x':
it is the case that
t = 0 mod (arr[i] - i)
This is a mod... | 9977152cd78cbd1bf5a23b566cefdf7489348ff5 | 510,348 |
from typing import Optional
import re
def email_is_valid(email: Optional[str]) -> bool:
"""
Validate that a valid email was provided.
None is a valid option.
Parameters
----------
email: Optional[str]
The email to validate.
Returns
-------
status: bool
The valida... | 58efc79668c4856fc74b7fc470405c5e27256c60 | 42,626 |
def calculate_iou(proposals, label):
"""
Calculate the intersection over union between bounding boxes of selective search and labels
This code is adapted from https://gist.github.com/meyerjo/dd3533edc97c81258898f60d8978eddc
:param proposals: A region proposal bounding box
:param label: Bounding box... | fb2e724e2426c47d742b8c55be8e095b0304d13a | 609,313 |
def has_repetition(page_list):
"""
Check if a list of page hits contains a page repetition (A >> B >> A) == True.
Run on journeys with collapsed loops so stuff like A >> A >> B are not captured as a repetition.
Similar to cycles/triangles, but from a flat perspective.
:param page_list: list of page ... | 685ac3698cc29fe3bf9980676c4212ef110d12a2 | 395,640 |
def calculate_overshoot(cigar):
""" The calculate overshoot function calculates the number of basepairs that have not been mapped but are part of
the read.
:param cigar: a list containing tuples representing the cigar string.
:return overshoot: an integer indicating the number of basepairs in the read ... | 687ce44bef05376339fbe7892b1c07b84981710f | 79,018 |
def get_filenames(i):
"""Returns the filepaths for the output MusicXML and .png files.
Parameters:
- i: unique identifier for the score
Returns:
- (sheet_png_filepath, musicxml_out_filepath)
"""
output_folder_prefix = "dataset/"
sheet_png_out_filepath = output_folder_prefix + "{}-s... | 4e26391eff80e08756431221d53c862c810c8071 | 692,048 |
from typing import List
import math
def lcm(nums: List[int]) -> int:
"""
Computes the LCM of the specified numbers
:param nums: Numbers to computer the LCM for
:return: LCM of the numbers
"""
n = nums[0]
for i in nums[1:]:
n = n * i // math.gcd(n, i)
return n | d4aef55265989a8cae9966fc59c94c99c52ffbdd | 361,858 |
import torch
def offsets_from_counts(counts):
"""Creates a tensor of offsets from the given tensor of counts
Parameters
----------
counts : torch.Tensor
1-d tensor representing the counts in a ragged array
Returns
-------
torch.Tensor
A 1-d tensor representing the offsets... | 667d277afa76d3b746b5409888d25057f790712f | 92,852 |
import torch
def tlbr2cthw(boxes):
"""
Convert top/left bottom/right format `boxes` to center/size corners."
:param boxes: bounding boxes
:return: bounding boxes
"""
center = (boxes[..., :2] + boxes[..., 2:])/2
sizes = boxes[..., 2:] - boxes[..., :2]
return torch.cat([center, sizes], d... | 15dff8e597e1386e88a8a0417cf5061e830c863f | 658,127 |
import asyncio
def async_test(test):
"""
Decorator to run async test methods.
"""
def wrapper(*args, **kwargs):
asyncio.run(test(*args, **kwargs))
return wrapper | b03c10f6b16fb7af148d21a89f1d3485b4fe4681 | 689,574 |
from typing import List
import re
def substitution(arr: List[str]) -> List[str]:
"""
>>> substitution(['a = 1;', 'b = input();', '',
... 'if a + b > 0 && a - b < 0:',' start()', 'elif a*b > 10 || a/b < 1:',
... ' stop()', 'print set(list(a)) | set(list(b))',
... '#Note do not change &&& or |... | 66c4ad7a1d8a14c703a71f20c97ecbabd7b484e7 | 379,103 |
def sanitize_sacred_arguments(args):
"""
This function goes through and sanitizes the arguments to native types.
Lists and dictionaries passed through Sacred automatically become
ReadOnlyLists and ReadOnlyDicts. This function will go through and
recursively change them to native lists and dicts.
... | 07849b685bbbd84e9164352c1cf03f69f9ef731d | 654,443 |
def ft2m(ft):
"""
Converts feet to meters.
"""
if ft == None:
return None
return ft * 0.3048 | ca2b4649b136c9128b5b3ae57dd00c6cedd0f383 | 5,187 |
def is_string_arg(parameter):
"""Whether the parameter's type is string or bytes."""
return parameter["grpc_type"] in ["string", "bytes"] | d0f7a3016bdf20edae9dc8bbcf2736da38b259ce | 643,710 |
import re
def tokenize(string):
"""Parse query string for ngram into token objects
Parameters
----------
string : str
Query string with each token enclosed in a pair
of square brackets. In each token, the tag ``word``
and ``pos`` could be given as ``[word="他們" pos="N.*"]`... | aa6117e41f891cdaecfe72f0d234e33d996e90dd | 521,612 |
from typing import OrderedDict
def usage_table_format(result):
"""Format usage information as a table."""
table = []
for item in result:
row = OrderedDict()
row['Value'] = item['name']['localizedValue']
row['Usage'] = item['currentValue'] or "0"
row['Limit'] = item['limit']... | 9a15e2c159878a6a4598c21c44ef2f1d17ec8450 | 164,232 |
def not_none_dim(rel):
""" Returns the index of dimension which is not None (which is 1/-1) in rel
Index is 0 based: 0 is right/left, 1 is front/back, 2 is above/below. """
for index, element in enumerate(rel):
if element == 1:
return index
continue
# Should never reach here:... | b7d5378a7eaa3758e3fbd52c569d252ddeb66abe | 330,891 |
def exit(*args, **kwargs):
""" Craft and return (without raising!) a SystemExit exception """
exc = SystemExit("\n\t".join(args))
return exc | e1b79c7c249233c4fee870af2b146bc791350059 | 136,633 |
from typing import Dict
def split_data(dish_dataset: Dict) -> Dict:
"""Splits dataset.
Args:
dish_dataset (Dict): Dataset.
Returns:
names (dictionary): Dish names in a dictionary.
"""
# Get dish names
dish_names = dish_dataset[['name']].values.flatten().tolist()
# Pack and ret... | 75a58f486693683bb02d3716c729a971d3da210f | 347,911 |
def get_level(level):
"""Returns the level below the level specified, defaults to District.
"""
if level.lower() == 'district':
return 'Site'
elif level.lower() == 'ta' or level.lower() == 'site':
return 'Household'
return 'District' | 389f8f3013455cc3607041f523a1da5df5f07055 | 145,418 |
import random
def get_shuffled_range(n):
"""
Utility function to generate a random ordering of the integers in [0, n)
:param n: The length of the returned list
:returns: A list of the numbers from 0 to n-1, inclusive, shuffled
"""
return random.sample(list(range(n)), k=n) | cc83d44903df9a587554fdd6f7e2898799a28f23 | 446,158 |
def _fetch_one(cursor, row_callback):
"""Fetch exactly one row and return it or `None`."""
return row_callback(cursor.fetchone(), cursor) \
if cursor.rowcount == 1 else None | e4cdceba8b656905df3a7a8f47e7b1f575857704 | 236,296 |
def is_valid_project_type(type):
""" Validates supported project types; currently only maven is supported; hopefully gradle in the future """
if not type:
return False
if "maven" != type:
return False
return True | b450423c06d778701c639c3175b87b271ddfb5d9 | 638,075 |
def vectorize(function):
"""
:param function: Function to be vectorized
:return: Vectorized function
Create new function y= vf(x), such that:
y = f(x)
/ map(f, x) if x is list
vf =
\ f(x) if x is not list
Example:
>>> import math
>>>
>>> sqrt... | 551daccbb1264da1bae31f0477a65d76d2d29035 | 636,534 |
def __is_now_playing(track):
"""
Returns True if the track is now playing.
"""
return '@attr' in track and track['@attr']['nowplaying'] == 'true' | c454a7e3158d2c3af29c665ecb5b618e5a90512e | 124,218 |
def term_info_as_list(term_info):
"""
Given a dictionary as returned by `parse_term_code`, return a list
suitable for use as a sort key.
"""
return [term_info["year"], term_info["spring"]] | aaf1dd9c5d155d0d2b44779f3a2ae3994fbe9efe | 293,265 |
def flatten(items):
"""Removes one level of nesting from items.
items can be any sequence, but flatten always returns a list.
"""
result = []
for i in items:
try:
result.extend(i)
except:
result.append(i)
return result | d064409430085dc0ebbb535565c0faa51a9dd510 | 154,914 |
def _getView(syn, view_id, clause=None):
"""
Based on a user-defined query calls to synapse's tableQuery function and returns the entity-view generator object.
:param syn: A Synapse object: syn = synapseclient.login(username, password) - Must be logged into synapse
:param view_id: A Synapse ID of... | c61f2b1b674b7e31bbdbbdb9d4cda629400a3e03 | 676,242 |
def subsample_fourier(x, k):
"""
Subsampling of a vector performed in the Fourier domain
Subsampling in the temporal domain amounts to periodization
in the Fourier domain, hence the formula.
Parameters
----------
x : tensor_like
input tensor with at least 3 dimensions, the last
... | 418a28bd9d96f70c338301fd0067811816513b52 | 606,081 |
import inspect
def is_legacy_scheduler(scheduler_class):
"""
Determines whether a scheduler is a legacy implementation that gets passed task parameters instead of only the
target throughput.
"""
constructor_params = inspect.signature(scheduler_class.__init__).parameters
return len(constructor_... | f7bb4291b5dca6faba52eec0062fdc242e072edf | 520,238 |
def n_missing(s):
"""Get the number of missing feature subsets.
Parameters
----------
s : pandas.Series
Records of feature subsets.
Returns
-------
int
See also
--------
subrela.records.iterate_missing : Iterate missing feature subsets.
Examples
--------
>... | 3a7b642f9e15e4abd251eab364f700b19d5115f4 | 60,273 |
def run(
*, # Force kwargs.
head_branch="master",
event="push",
status="completed",
conclusion="success",
is_fork=False
) -> dict:
"""Generate Github run like dict."""
return {
"head_branch": head_branch,
"event": event,
"status": status,
"conclusion": co... | f253365451c4f5847ad2f344540fec0512c2125f | 591,993 |
def next_nl_or_end(s, n=0):
"""Next newline or end of text"""
# first identify starting newlines, we pass them
start = n
while start < len(s) - 1 and s[start] == '\n':
start += 1
p = s.find('\n', start + 1)
if p > -1:
# Another newline found, continue until no more newlines or en... | 6cfff226259f3df499f20ae3390ba42297f4597a | 494,676 |
import logging
def saveData(fileName, data):
"""Save data to persistent file. If file does not already exist it will
be created. The function will return True if successful, else False.
:param fileName: Name of the file to save to.
:type fileName: string
:param data: The data object to save
:... | c9bc0e9a223376c8bf5563ad48b66a1cbed5301d | 189,770 |
def siwsi(b8a, b11):
"""
Shortwave Infrared Water Stress Index \
(Fensholt and Sandholt, 2003).
.. math:: SIWSI = (b8a - b11)/(b8a + b11)
:param b8a: NIR narrow.
:type b8a: numpy.ndarray or float
:param b11: SWIR 1.
:type b11: numpy.ndarray or float
:returns SIWSI: Index value
... | 9a718dbce9eeb5963d8117093568d85d183a0bb3 | 194,832 |
import math
def cubehelix(samples=16, start=0.5, rot=-0.9, sat=1.0, gamma=1., alpha=None):
"""
Create CubeHelix spectrum colourmap with monotonically increasing/descreasing intensity
Implemented from FORTRAN 77 code from D.A. Green, 2011, BASI, 39, 289.
"A colour scheme for the display of astronomica... | 5ee4cb9f62430bd125dd0a8fb618035016c20f00 | 410,943 |
def _bfs_for_reachable_nodes(target_nodes, name_to_input_name):
"""Breadth first search for reachable nodes from target nodes."""
nodes_to_keep = set()
# Breadth first search to find all the nodes that we should keep.
next_to_visit = target_nodes[:]
while next_to_visit:
n = next_to_visit[0]
del next_t... | b22464d315415af40bc31ad8d707de19319d0707 | 327,345 |
def can_import(name):
"""Attempt to __import__ the specified package/module, returning
True when succeeding, otherwise False"""
try:
__import__(name)
return True
except ImportError:
return False | 7b02767184e73935c92b9e0e15769bb73ac24d95 | 666,209 |
def startstrip(string: str, part: str) -> str:
"""
Remove ``part`` from beginning of ``string`` if ``string`` startswith ``part``.
Args:
string (str): source string.
part (str): removing part.
Returns:
str: removed part.
"""
if string.startswith(part):
return st... | 671af88f6f502f98a1da7a421eff92b0631d296f | 411,504 |
import re
def highlight_query_string(text: str, qstring: str) -> str:
"""
Highlights query word/phrases in input text by surrouding them with <span> tags
Args:
text: Section raw text from db
qstring: A query string from search result page (could be in single/doubl quotes)
Returns:
... | 8d7b67565d61b6b13b142f96c91ff555065aeb17 | 376,391 |
def get_outcome(game_info):
"""
Get the score of the game based on one team's score and the plus minus.
Parameters
----------
game_info : list
A list of the info for this game, coming form LeagueGameLog. From the
perspective of the home team.
Returns
-------
game_outcom... | 43c8b4f8aab026c99948825dae89013913251604 | 566,174 |
def check_for_ticket_name_error(ticket_name):
"""
Returns any error message if the ticket name is not alphanumeric, if there is an
invalid space in the name or the name is too long for a ticket name. If
there is no errors it returns false.
:param ticket_name: a string for the ticket's name
:r... | 38163e28e8acb5788b21ee832c037cc707574a83 | 111,923 |
def date2String(date_time):
"""This function is used to take a datetime object and convert it to a string
representing the month, day, and time in 12 hour form"""
day = str(date_time.strftime("%b")) + " " + str(date_time.day)
time = date_time.strftime("%I:%M %p")
displayDate = day + ", " + time
... | c5ce14d7bc2603d1068ac4309035b6cf36d660b0 | 73,940 |
def get_valid_init_tree(trees):
"""Returns first NewickTree entry that is not NoTree"""
for i in range(len(trees)):
if trees[i] == "NoTree":
continue
else:
return trees[i] | 69841abe1e8720c42104a961e8165306b399f710 | 657,084 |
def clip_pt_to_im(pt, im_size):
"""Clips a 2D point to the image frame.
:param pt: 2D point (x, y).
:param im_size: Image size (width, height).
:return: Clipped 2D point (x, y).
"""
return [min(max(pt[0], 0), im_size[0] - 1),
min(max(pt[1], 0), im_size[1] - 1)] | 386b845fd1561976ccbce0e8b098ca6128a938a0 | 99,859 |
def salary_job_stats(df):
"""
Get the stats (count, mean, std, min, 25%, 50%, 75%, max) for each job
(Data Scientist, BI, Data Analyst, Developpeur, Data Engineer)
Parameter:
df : a dataframe with salary and job title
Returns:
salary_df : a dataframe with mean/std salary for each j... | c2291b1f4aeb542f196441a16e6accfad1844955 | 119,148 |
import re
def parse_show_vrrp_statistics(raw_result):
"""
Parse the 'show vrrp statistics' command raw output.
:param str raw_result: vtysh raw result string.
:rtype: dict
:return: The parsed result of the show vrrp statistics command in a \
dictionary of the form:
::
{
... | 8132ad997f186550b94c3607bc919290f6e37eca | 420,985 |
def get_modelpath_and_name(savedmodel_path):
"""
Help function which returns the full path to a model, excluding the epoch
number at the end, e.g. "./mybestmodel-40" returns "./mybestmodel".
"""
return savedmodel_path[:savedmodel_path.rfind("-")] | 3b14a97a07acbbabc7b93c0249418f641bcf8a48 | 562,991 |
def tcllist(l: list) -> str:
"""Translate a Python list to the equivalent Tcl command.
All elements in the list are quoted using brackets and will not support
variable substitution. Do that on the Python side.
Example:
>>> tcllist([1.0, 2, "sam's the best!"])
"[list {1.0} {2} {sam's the best!}... | 1426d1cc35345ae28b4e1363e361b60d15959006 | 550,194 |
def eight_decimal_places(amount, format="str"):
"""
>>> eight_decimal_places(3.12345678912345)
"3.12345679"
>>> eight_decimal_places("3.12345678912345")
"3.12345679"
>>> eight_decimal_places(3.12345678912345, format='float')
3.12345679
>>> eight_decimal_places("3.12345678912345", format=... | 1ebd8eda2fe162984812dd60ab87221714903811 | 500,961 |
def get_all_inds(string, substr):
"""Returns indices of all occurrences of a substring."""
# initialize list of indices of occurrences with dummy entry
inds = [0]
# find first occurrence
i_curr = string.find(substr)
# while end of string has not been reached
while i_curr != -1:
# det... | c1e4cca7556529029583d47c154bfd7af77dccd6 | 461,326 |
def strip_meta(value: dict) -> dict:
"""Strip the "_meta" node from dict, recursively."""
result = {}
for k, v in value.items():
if k == "_meta":
continue
if isinstance(v, dict):
result[k] = strip_meta(v)
else:
result[k] = v
return result | 88cd5bc6667823043a6231a8c35c9bf022e6d33b | 82,662 |
import re
def is_url_valid(string):
"""
This function checks whether input string follow URL format or not
Args:
string: Input string
Returns:
True: if string follows URL format
False: if string doesn't follow URL format
>>> is_url_valid("C:/users/sample/Desktop/image.jpg... | a6ff543fa68f1cdaef5f7a4c4be8f6e5979e6a5d | 63,998 |
def get_empty_tiles_positions(grid): # Fonction renvoyant la liste des positions des tuiles vides
"""
Gets all the empty tiles' coordinates.
:param grid: The game grid
:return: List with all the coordinates of empty tiles
"""
list_empty_positions = []
for i in range (len(grid)):
f... | 7cacffd0c54040011276f72c9f7337a1ea8d453c | 408,405 |
def _has_page_changed(url, driver_url):
"""
Has the page's URL changed? Exclude everything after a hash.
"""
return \
url[0:(url.find('#') \
if url.find('#') > 0 \
else len(url))]\
.rstrip('/') \
!= \
driver_url[0:(driver_url.find('#') \
... | 0920896a3ad4e6373d1af706698323ce1fff725a | 293,618 |
import math
def num_k_of_n(n: int, k: int) -> int:
"""Return number of combinations of k elements out of n."""
if k > n:
return 0
if k == n:
return 1
return math.factorial(n) // (math.factorial(k) * math.factorial((n - k))) | de99dd88fc6e747421e36c698a525b7e58b1e4de | 6,832 |
import timeit
def measure_overhead(timer_factory):
"""Measure the overhead of a timer instance from the given factory.
:param timer_factory: callable which returns a new timer instance
:return: the average duration of one observation, in seconds
"""
timeit_timer = timeit.Timer(
globals={'... | 3e981430487e444e599de5faa9b9afd89dcec536 | 165,643 |
def decode_bytes(obj):
"""If the argument is bytes, decode it.
:param Object obj: A string or byte object
:return: A string representation of obj
:rtype: str
"""
if isinstance(obj, bytes):
return obj.decode('utf-8')
elif isinstance(obj, str):
return obj
else:
ra... | 48c56e899cc83deb478cc665b3f051e1e99a18ae | 696,809 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.