content stringlengths 39 14.9k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
def dict2cfgString(dictionary, separator="\n", assigner="="):
"""
Converts a dictionary into a string
Parameters
----------
dictionary : dict
The dictionary to be transformed.
separator : str, optional
The character to be used to separate individual
entries. The default... | 12c1f3b102429c22d1bb15714631908be41a63f2 | 22,010 |
def convert_indentation(indentation):
"""
Converts integer- or string- indentation into string indentation for prepending.
:param int | str indentation:
:return: str
"""
return " " * indentation if isinstance(indentation, int) else indentation | 97b223e0b1d3a210e0d3cc875d6b7bd4d4486d73 | 22,015 |
def intersect(l0, l1):
"""Given two lists return the intersection."""
return [e for e in l0 if e in l1] | 4dbc2307eabfbe6312407b19e0924952c6dcc9cc | 22,017 |
import re
def seems_like_section_name(line):
"""Check whether `line` starts with 'Para' or ends with ':', ignoring case and whitespace."""
return bool(
re.search(r'(^[^a-záéíóúü0-9]*para\b|:\s*$)', line, re.IGNORECASE)
) | d1c10116319c39cb5e0b5c57a2007703578da75c | 22,019 |
import copy
def get_recurs_class(g, derivLink):
"""Find the recurs_class property in the contents.
Return its value and the dictionary with recurs_value removed."""
recursClass = 0
if derivLink['content'] is None or len(derivLink['content']) <= 0:
return 0, derivLink
newDerivLink = copy.de... | e787614a5d433352551c684d134d5d85b870ec4a | 22,026 |
def city_state(city, state, population=0):
"""Return a string representing a city-state pair."""
output_string = city.title() + ", " + state.title()
if population:
output_string += ' - population ' + str(population)
return output_string | ae958598a57128cf36f63ff6bfb8181f9d07db31 | 22,028 |
import random
def coin_flip(p=0.5):
"""Simulate a coin flip."""
return True if random.random() > p else False | 51cd54a946cedb60589fdc24eb5f061afb713681 | 22,031 |
def getBoundingBox(veclist):
"""Calculate bounding box (pair of vectors with minimum and maximum
coordinates).
>>> getBoundingBox([(0,0,0), (1,1,2), (0.5,0.5,0.5)])
((0, 0, 0), (1, 1, 2))"""
if not veclist:
# assume 3 dimensions if veclist is empty
return (0,0,0), (0,0,0)
# fin... | a2c035f85071e5a9f8dfee2c98cc46e86439a0cc | 22,033 |
def feed_options_str(feed_options):
"""Convert a FeedOptions dict of values into an appropriate string value.
Amazon docs for VAT upload with details:
https://m.media-amazon.com/images/G/01/B2B/DeveloperGuide/vat_calculation_service__dev_guide_H383rf73k4hsu1TYRH139kk134yzs.pdf
(section 6.4)
... | 995b2927efb94cd92733b1058f423e863ca9c6e2 | 22,034 |
from typing import OrderedDict
def create_param_dict(param_file_name, outputPrefix):
"""
Create a dictionary with the parameters and file with posterior density
:param param_file_name: original parameter file
:param outputPrefix: prefix given in ABCtoolbox config file for estimation
:return: param... | 9344c1727b5f7d23d3df1b79987d38e9bdff6191 | 22,039 |
def isempty(line):
""" Checks if a line is empty (contains only witespaces or tabs)"""
if len(line.replace("\n","").replace(" ","").replace("\t","")) == 0:
return True
else:
return False | abd8e429125cea6c9575d5e539f69f6a39a7ccfe | 22,041 |
def filter_by_gender(df, male):
"""Filters the data by gender.
Args:
df: DataFrame.
male: True if male, False otherwise.
Returns:
DataFrame.
"""
gender = 1 if male else 0
return df[df.male == gender] | ce8a339a24cbf930fd4e96dda9acc786dda45e07 | 22,042 |
def get_matched_dyads(
dyads,
d0_key="doctype",
d1_key="doctype",
d0_values=["foxnews"],
d1_values=["foxnews"],
):
"""Filter which returns dyads which match the specified conditions.
Args:
dyads (list): list of 2-item tuples. Each item is a dictionary (document).
d0_key (str... | bffe8e624d9c08d377d712d111be4f0a396ff07c | 22,043 |
def clean_consecutive_duplicates(
move_data, subset=None, keep='first', inplace=False
):
"""
Removes consecutive duplicate rows of the Dataframe, optionally only
certain columns can be consider.
Parameters
----------
move_data : dataframe
The input trajectory data
subset : Array... | 574ba4f6fba2d65f9680869178be1be9b45f0b97 | 22,047 |
from pathlib import Path
def hmm_data_exists(file_path: Path) -> bool:
"""
Checks if HMM data exists in the local data path.
:param file_path: Path to where `profiles.hmm` should be
:return: True if both the `hmm` directory and `profiles.hmm` exist, else False
"""
return file_path.parent.is_d... | 417e98366a6e458badd8c7b16d58468efdb9af53 | 22,049 |
def get_program_number(txt_row):
""" Checks if the current line of text contains a program
definition.
Args:
txt_row (string): text line to check.
Returns:
An integer number. If program number cannot be found, or is
invalid in some way, a large negative number is returned.
"""
... | 92f67bef6f36ad4e88e4840faa0878e72c0bb78c | 22,052 |
def write_dfs_to_filepaths(dfs, filepaths):
"""
Accepts a list of pandas dataframes - dfs
and a parralel list of filepaths - pathlib path objects
Writes the dataframes to the filepaths as csvs with no index
Returns the number of files written integer
"""
n = 0
for df, filepath in zip(dfs... | 2e66c419f1a0a4472de0527cb157d2bf3bf33de8 | 22,053 |
def gnome_sort(lst: list) -> list:
"""
Pure implementation of the gnome sort algorithm in Python
Take some mutable ordered collection with heterogeneous comparable items inside as
arguments, return the same collection ordered by ascending.
Examples:
>>> gnome_sort([0, 5, 3, 2, 2])
[0, 2, 2... | 5aae393eabd046c20f51125e405d211555ed9bdb | 22,057 |
def get_numeric_trace_attribute_value(trace, trace_attribute):
"""
Get the value of a numeric trace attribute from a given trace
Parameters
------------
trace
Trace of the log
Returns
------------
value
Value of the numeric trace attribute for the given trace
"""
... | fd757861972dce8d9624efa13773bd4624cf9ace | 22,058 |
def get_in_out_tensors(graph):
"""
Get the input and output tensors from the TensorFlow graph 'graph'.
"""
# Get the graph nodes that perform computation on tensors
ops = graph.get_operations()
# Initialize input and output tensors
inputs = []
outputs_set = set(ops)
# Process operations
for op in ops:
# The... | 8c7c5c068bcb11d5d4f80f191af899dbf7e7aab1 | 22,061 |
import re
def _match_channel_pattern(channel_name):
"""Returns a regex match against the expected channel name format.
The returned match object contains three named groups: source, detector,
and wavelength. If no match is found, a ValueError is raised.
Parameters
----------
channel_name : s... | e40ba1b1c075d2fabae570bab36869b168ef6121 | 22,062 |
from datetime import datetime
import pytz
def with_gmt_offset(timezones, now=None):
"""
Given a list of timezones (either strings of timezone objects),
return a list of choices with
* values equal to what was passed in
* display strings formated with GMT offsets and without
under... | b1916a3889016d3c608806fe0dab62f05801d5eb | 22,068 |
def apply_activation_forward(forward_pass):
"""Decorator that ensures that a layer's activation function is applied after the layer during forward
propagation.
"""
def wrapper(*args):
output = forward_pass(args[0], args[1])
if args[0].activation:
return args[0].activation.for... | 950f2b96cadfc8df075763b25961c7f088ec9232 | 22,069 |
def get_option_name(name): # type: (str) -> str
"""Return a command-line option name from the given option name."""
if name == 'targets':
name = 'target'
return f'--{name.replace("_", "-")}' | 77d4625be99944fb0748f6f3524d453fbf511795 | 22,071 |
def get_latest_file_url(files: dict, starts_with: str, file_extension: str) -> str:
"""
Get the url to a file which should start and have a specific file extension.
Parameters
----------
files : dict
Keys are the filenames and the values are the urls.
starts_with : str
Start of ... | 4b9d115c983f9cdcf1a96905d7c198b688851dea | 22,075 |
def _extract_from_subworkflow(vs, step):
"""Remove internal variable names when moving from sub-workflow to main.
"""
substep_ids = set([x.name for x in step.workflow])
out = []
for var in vs:
internal = False
parts = var["id"].split("/")
if len(parts) > 1:
if par... | bd565b444084aa2b5dfab0444aaf13344240d17d | 22,078 |
def chebyshev_distance(a, b):
"""
Calculate the Chebyshev distance of two vectors.
"""
distances = []
for x, y in zip(a, b):
distances.append(abs(x - y))
distance = max(distances)
return distance | aa6fccc804ccbeb312e0bb41693feb05787d84f4 | 22,081 |
def sort(li):
"""
Performs a mini radix sort on the top ten documents by first sorting
on document ids, then sorting on document ranking. As sorted() is stable,
this ensures that any documents with identical rankings will be sorted on
their document ids in increasing order
"""
... | 1bd1c440e8e2492f22d67b78215d9a8c1f483c60 | 22,084 |
def lammps_equilibrated_npt(job):
"""Check if the lammps equilibration step has run and passed is_equilibrated for the job."""
return job.isfile("equilibrated_npt.restart") and True | 4d4ff20fc020b29319393989f852a7b2479627f0 | 22,086 |
def _flatten(d):
""" Pack a hierarchical dictionary of variables into a list
Sorting is important as it ensures the function is called with
the inputs in the same order each time!
"""
l = []
# This sorting is important!
for (k,v) in sorted(d.items(), key=lambda t: t[0]):
if ... | d1aea2b85e161747262ec424e9b63fb336967236 | 22,093 |
def get_logging_options_string(args):
""" This function extracts the flags and options specified for logging options
added with add_logging_options. Presumably, this is used in "process-all"
scripts where we need to pass the logging options to the "process" script.
Args:
args (n... | 070ed0cd906845abf784bd566118a1959af875f2 | 22,097 |
def to_bytes(text, encoding='utf-8'):
"""Make sure text is bytes type."""
if not text:
return text
if not isinstance(text, bytes):
text = text.encode(encoding)
return text | 367da58c31dddd4c243c56a9780b47ef6682bee2 | 22,098 |
def email_get_unread(imap, from_email_address):
"""Returns (status, list of UIDs) of unread emails from a sending email address.
"""
search = '(UNSEEN UNFLAGGED FROM "{}")'.format(from_email_address)
status, response = imap.search(None, search)
if status != 'OK':
return status, response
... | 48c4cf036e24acadec425bb7bb8ac9395488229a | 22,099 |
def _lambda_risk_mapper(risk_level: int) -> str:
"""Helper methods
Parameters
----------
risk_level: int
number from range 0-4 represents risk factor for given vault
Returns
-------
string:
text representation of risk
"""
mappings = {0: "Non Eligible", 1: "Least", 2:... | e19ef85b82d4b36bb6e0dfdcae373f25a894dbff | 22,100 |
import math
def torsion_angle(c1, c2, c3, c4):
"""
float <- torsion_angle(a, b, c, d)
returns the torsion angle in degrees between 3D pts a,b,c,d
"""
v1 = (c1[0]-c2[0], c1[1]-c2[1], c1[2]-c2[2])
v2 = (c2[0]-c3[0], c2[1]-c3[1], c2[2]-c3[2])
v3 = (c3[0]-c4[0], c3[1]-c4[1], c3[2]-c4[2])
... | 32eb380fd8e4d0645481ab816a32e01144fb3c7b | 22,101 |
def accumulate(instructions: list) -> dict:
"""
Read and execute instructions until an infinite loop if found:
acc +12 -> add 12 to 'acc'
jmp +48 -> jump to the instruction located at 'index' + 48
nop -56 -> do nothing & go to the next instruction
if an instruction has already been ... | bf5a5bb278e71e783968eafed6a790ff2bdf77c1 | 22,105 |
def build_sampler( sampler_class, pe_method, force_method, T = 1.0e-4, \
dt = 1.0e-1, traj_len = 100, absxmax = 1.0e2, dt_max = None, min_rate = 0.6, \
max_rate = 0.7, gaussianprior_std = None ):
"""Builds a sampling.Sampler class object of type sampler_class.
Args:
sampler_class : Sampler... | 528966ec88dd8d4a910753290e19849f7919cf22 | 22,107 |
import time
from datetime import datetime
def SecondsToZuluTS(secs=None):
"""Returns Zulu TS from unix time seconds.
If secs is not provided will convert the current time.
"""
if not secs: secs = int(time.time())
return(datetime.utcfromtimestamp(secs).strftime("%Y-%m-%dT%H:%M:%SZ")) | 2ca96ed779020037eb360a1250243dfe628f6195 | 22,109 |
def _LookupMeOrUsername(cnxn, username, services, user_id):
"""Handle the 'me' syntax or lookup a user's user ID."""
if username.lower() == 'me':
return user_id
return services.user.LookupUserID(cnxn, username) | 68ef5ea6d6c3076717660848a0b8a9c3cb4847d4 | 22,111 |
import hashlib
import ctypes
def size_t_hash(key):
"""Hash the key using size_t.
Args:
key (str): The key to hash.
Returns:
str: The hashed key.
"""
hash_digest = hashlib.blake2b(key.encode()).hexdigest() # pylint: disable=no-member
return '%u' % ctypes.c_size_t(int(hash_dig... | 19aa7ec430c9c0fbbba45baa9812f755d19a4cfe | 22,120 |
def check_scenarios(scenes):
"""
Make sure all scenarios have unique case insensitive names
"""
assert len(scenes) == len(dict((k.lower(), v) for k, v in scenes))
return scenes | c9b437d396a4d0ca17c17a85cb99e8574cc78fe3 | 22,127 |
def fix_tract(t):
"""Clean up census tract names.
:param t: Series of string tract names
:returns: Series of cleaned tract names
"""
if type(t) == str:
return t
return str(t).rstrip("0").rstrip(".") | e8e2f6bee61596c57bb805717e9a34eb88f1c91e | 22,132 |
def moving_avg_features(df):
"""
Function to calculate the exponential moving averages and moving averages over different intervals of days
Input: Dataframe
Output: Dataframe with new moving average features
"""
df['EMA_9'] = df['Close'].ewm(9).mean().shift()
df['EMA_9'] = df['EMA_9'].fillna(... | 820eb6b0ad42592393d50d93d5718a6a10562907 | 22,135 |
def _old_style_nesting(vocab):
"""Detect old-style nesting (``dict[str, List[Tuple[str, Field]]]``)."""
return isinstance(vocab, dict) and \
any(isinstance(v, list) for v in vocab.values()) | bce0ba4ee2fe7de4916f970c6f765b493ccdf0db | 22,136 |
from datetime import datetime
def convert_date(date_string):
"""Convert the date_string to dd-mm-YYYY format."""
date = datetime.strptime(date_string, "%d.%m.%Y")
return date.strftime('%d-%m-%Y') | 33a67ba33fed1f195812e9839a811411de1e2987 | 22,140 |
def squeeze_whitespace(text):
"""Remove extra whitespace, newline and tab characters from text."""
return ' '.join(text.split()) | ef476f4ed6cd524c1cb115345151e4bc18c616b5 | 22,142 |
def iso_string_to_sql_date_sqlite(x: str) -> str:
"""
Provides SQLite SQL to convert a column to a ``DATE``, just by taking the
date fields (without any timezone conversion). The argument ``x`` is the
SQL expression to be converted (such as a column name).
"""
return f"DATE(SUBSTR({x}, 1, 10))" | 349c560589d4f03938538f74dbc188577ac63a2d | 22,143 |
from pathlib import Path
def save_NN_sequential(model, model_name):
"""
Saving a Neural Network as h5 file
:param model: sequential model
:param model_name: name to save the model
:return: True
"""
file_name = 'Model_' + model_name
file_path = Path().joinpath('Pickles', file_name + ".h... | a7481ace8971debb1be3af2140f6fdd33b3f679b | 22,144 |
def get_second_validate_param(tel_num):
"""
Assemble param for get_second_validate
:param tel_num: Tel number
:return: Param in dict
"""
param_dict = dict()
param_dict['act'] = '1'
param_dict['source'] = 'wsyytpop'
param_dict['telno'] = tel_num
param_dict['password'] = ''
par... | efbd31c56e3fdd4cb0e75bcae92cf51d996aac5d | 22,146 |
def typeless_equals(entity1, entity2, check_class, check_instance):
"""
Checks if entities are equal. The check is different whether
entities are classes or instances, which is specified in
corresponding parameters. If neither checks are specified,
True is returned
"""
if check_class:
... | a0f1360509d8f2f1191c158426fa29ec42d3a3c8 | 22,149 |
def node_name(node):
"""Return lxml node name without the namespace prefix."""
try:
return node.tag.split('}')[-1]
except AttributeError:
pass | 0e61ae70563784b5c845a73a82c3b2265ce63202 | 22,150 |
def get_most_read_or_features(
amb_sams: list,
counts: dict) -> list:
"""
Get the samples that have the most
counts (reads of features).
Parameters
----------
amb_sams : list
Sample IDs.
counts : dict
Count per Sample ID.
Returns
-------
cur_best... | 147a9e3a1df579f1673a832bf7dd3a7267ac4394 | 22,151 |
def find_index_unsafe(val, bin_edges):
"""Find bin index of `val` within binning defined by `bin_edges`.
Validity of `val` and `bin_edges` is not checked.
Parameters
----------
val : scalar
Assumed to be within range of `bin_edges` (including lower and upper
bin edges)
bin_edge... | 08f89f8f6096d930e5d6c043374567d6646e9a37 | 22,156 |
import time
def func_to_time(x):
"""This is sleeps for x seconds for timing tests."""
time.sleep(x)
return 'Slept for {0} second(s)'.format(x) | 8698514f6efe4b7b58aab3da5687cb72c2083fd7 | 22,158 |
def to_float_str(
val):
"""to_float_str
convert the float to a string with 2 decimal points of
precision
:param val: float to change to a 2-decimal string
"""
return str("%0.2f" % float(val)) | c15c4e43e788ab416170f21413906fc2d218b345 | 22,160 |
def limits2slice(limits):
"""
Create a set of slice objects given an array of min, max limits.
Parameters
----------
limits: tuple, (ndarray, ndarray)
Two tuple consisting of array of the minimum and maximum indices.
Returns
-------
slices : list
List of slice objects w... | f796f1e468f560d72e7d037e27a110ee50d3a45d | 22,166 |
def has_clockwise_numbering(coords):
""" tests if a polygon has clockwise vertex numbering
approach: Sum over the edges, (x2 − x1)(y2 + y1). If the result is positive the curve is clockwise.
from:
https://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-o... | e9d55bfe9c5ef66e4b3e611c579c3ca940ba98d2 | 22,168 |
def convert_to_float(value: str) -> float:
"""
Get the float value from, for example, "R9 323.46".
"""
return float(value[1:].replace(' ', '')) | 5ebea176aa6cbfaf6318b246fda73ff443efd092 | 22,169 |
def sift(iterable, predicate):
"""
Sift an iterable into two lists, those which pass the predicate and those who don't.
:param iterable:
:param predicate:
:return: (True-list, False-list)
:rtype: tuple[list, list]
"""
t_list = []
f_list = []
for obj in iterable:
(t_list ... | 347ae7cd9f79bccdc6fc6ae1efa1a29b5563322a | 22,170 |
def compare_datetime(date, span):
"""
Compare information within datetime object with a span
Parameters
----------
date: datetime
Datetime to compare.
span: Span
Span to compare.
Returns
-------
bool
True if match.
"""
return span.text in str(date) | d2bd942d7b6c536ac35d1cd18b10ed57a465622a | 22,173 |
def reduce_list(data_set):
""" Reduce duplicate items in a list and preserve order """
seen = set()
return [item for item in data_set if
item not in seen and not seen.add(item)] | e84b87b45c8a7aea14beee3b7822f55a0a99151e | 22,174 |
def validate_knot(knot):
""" Confirm a knot is in the range [0, 1]
Parameters
----------
knot : float
Parameter to verify
Returns
-------
bool
Whether or not the knot is valid
"""
return (0.0 <= knot <= 1.0) | 61ab023d61248268db74febd72df6ecf3ef0c056 | 22,175 |
def _get_all_deps(*, deps, split_deps_keys = []):
"""Returns a list of all dependencies from a Label list and optional split attribute keys.
Args:
deps: Label list of (split) dependencies to traverse.
split_deps_keys: (optional) List of split attribute keys to use on split deps.
Returns:
... | 30521b0ce646c3ee51b1d45d06e05aeb71058456 | 22,180 |
def get_time_string(codetime):
"""
Utility function that takes the codetime and
converts this to a human readable String.
Args:
codetime (`float`):
Code execution time in seconds (usually the difference of two time.time() calls)
Returns:
`str`: A string indicating the t... | 2cdc53ba83e06297c3c09b59095553db72d41643 | 22,184 |
def remove_duplicates(lst):
"""
This function removes all duplicate object from a list.
:param lst: A list.
:return: The same list, with all its elements appearing just once.
"""
if len(lst) == 1:
return lst
return [i for n, i in enumerate(lst) if i not in lst[:n]] | bf28a109a1af4760c39e31fdda88ea30b1e55f8a | 22,185 |
def aln_abuts_unknown_bases(tx, fasta):
"""
Do any exons in this alignment immediately touch Ns?
:param tx: a GenePredTranscript object
:param fasta: pyfasta Fasta object for genome
:return: boolean
"""
chrom = tx.chromosome
for exon in tx.exon_intervals:
if exon.start == 0: # ... | ceb31739be0091b3b52f763c0c0d6d15b1aebd19 | 22,188 |
import json
import hashlib
def get_md5(obj, trans_func=None):
"""get a object md5, if this obj is not supported by `json.dumps` please provide a trains_func.
Args:
obj (object): obj to get md5
trans_func (function, optional): use this to trans obj to str. Defaults to None.
"""
if tran... | ae7da1f0bab1815a2d357617dd93d26e020a2316 | 22,190 |
import re
def extract_courts(s: str) -> list:
"""
Extract a list of court numbers listed in the statute's text.
Args:
s (str): The text of the statute that lists the court numbers.
Returns:
(list): A list court numbers, all cleaned up.
"""
my_s = re.sub(r'[^0-9\s]', '', s)
... | d15e279ca2368d23fc75c7c2151329cfd60bb43a | 22,191 |
def fa_attachment(extension):
"""
Add fontawesome icon if found. Else return normal extension as string.
:param extension: file extension
:return: matching fontawesome icon as string
"""
if extension == 'pdf':
return "<i class='fa fa-file-pdf-o fa-lg'></i>"
elif extension == 'jpg' o... | 3add6bf4c177cba893a2242df352fd0ae619ee90 | 22,194 |
def get_worksheet_keys(data_dict, result_info_key):
"""Gets sorted keys from the dict, ignoring result_info_key and 'meta' key
Args:
data_dict: dict to pull keys from
Returns:
list of keys in the dict other than the result_info_key
"""
keys = set(data_dict.keys())
keys.remove(re... | 1092eee46980a5e4f745d3a99ff6abe7d5c9db62 | 22,196 |
def add_options(click_options):
"""
Decorator that adds multiple Click options to the decorated function.
The list is reversed because of the way Click processes options.
Note: This function has its origins in the
https://github.com/pywbem/pywbemtools project (Apache 2.0 license)
Parameters:
... | fe6c5bda8f0606cc7fcdec87dd3332d1abb9b695 | 22,197 |
import itertools
def _expand(the_set, expand_fn):
"""Returns a concatenation of the expanded sets.
I.e.
Returns a set of all elements returned by the expand_fn function for all
elements in the_set.
E.g.
With expand_fn = lambda x: (10*x, 100*x) and the_set = set([1, 2, 3])
this function returns set([10... | 694661c0cc6d2d09d72d65ea63cc1241fc32d4d5 | 22,198 |
import re
def ingredients_from_food(food):
"""
Extract ingredients from food description
:param food: A string, value from the Food column
:return: A list of ingredients (strings) or empty list
"Салат "Папарать-Кветка"(Говядина,ветчина,помидоры,огурцы)" -> ["говядина", "ветчина", "помидоры", "огур... | 4314bce50e602f0e3c0d8db4d7c22ef2bd693c2a | 22,201 |
import json
def json_content(directory, file_name):
"""
This function gets the content of a json file and
returns as dictionary.
:param directory: String
The folder where the json file is located
:param file_name: String
The name of the jso... | 98cf97b4a1d6853cae9b4087fd02a1d316afb492 | 22,202 |
def get_cai_ptr(X):
"""
Function gets the pointer from an object that supports the
__cuda_array_interface__. Raises TypeError if `X` does not support it.
"""
if hasattr(X, '__cuda_array_interface__'):
return X.__cuda_array_interface__['data'][0]
else:
raise TypeError("X must supp... | 467e4437b34693e37f3f90e822899dc3a548710e | 22,209 |
def validate_float(s):
"""Convert s to float or raise a ValueError."""
try:
return float(s)
except ValueError:
raise ValueError('Could not convert {0!r} to float'.format(s)) | 1559e4b8465e4d380c74784f0dab68aaf7965dbc | 22,210 |
def show_element(elem):
""" Output whole a element as it is. """
return elem | bcb8d2ae273c105524a7518a2f4247a5aa48410f | 22,212 |
import re
import warnings
def parse_psp_name(psp_name):
"""
Parse the name of vasp's psp
Parameter
psp_name: str
The name of vasp's psp, e.g. GGA, LDA, potpaw_LDA
Return
psp_name_norm: str
The normalized psp name
"""
psp_name = psp_name.upper()
psp_... | 0b5e025fa503f23fc17101690b978d414003197b | 22,214 |
def _format_list_items(list_items):
"""Generate an indented string out of a list of items."""
list_string = ''
if list_items:
for item in list_items:
list_string = list_string + " '" + item + "',\n"
list_string = "[\n {}\n]".format(list_string.strip()[:-1])
else:
... | dd677277650e5d3105c01f6636518b8bbd2a1bff | 22,216 |
import torch
import math
def importance_sampling_cross_validation(logp):
"""Compute the importance-sampling cross validation (ISCV) estimate.
The ISCV estimates the holdout log-likelihood from just an approximation to
the posterior predictive log-likelihoods on the training data.
### References:
... | 9bef3b3c3775e359d52a321a8e72b69d38f0fcb7 | 22,217 |
import re
def convert_character(text : str):
"""
Convert consecutive full-size numbers to half-size numbers.
Convert a single half-size number into a full-size number.
Convert half-size English characters to full-size ones.
Parameters
----------
text : str
input text
Retu... | f388de9eac9c92daceb96a46fce3efc525ce3eff | 22,218 |
import time
def retry(func, exc=Exception, tries=3, delay=1):
"""
Call ``func()`` up to ``tries`` times, exiting only if the function
returns without an exception. If the function raises an exception on
the final try that exception is raised.
If given, ``exc`` can be either an `Exception` or a t... | 5384afd77840b77b2cb278502d8fc64890af6be7 | 22,227 |
import json
def _get_entry_count(doi_file):
"""
Given a file path, will return the number of entries in that file. If the file reading files, returns None.
"""
try:
with open(doi_file) as f:
content = json.load(f)
return len(content)
except:
return None | fba5a3152811fbc01f4d91b72bdbe5659a65a152 | 22,230 |
from typing import Dict
from typing import Any
def as_lta_record(catalog_record: Dict[str, Any]) -> Dict[str, Any]:
"""Cherry pick keys from a File Catalog record to include in Bundle metadata."""
# As created by the nersc_verifier component...
# ---------------------------------------------
# "uuid":... | 96eb177bb8de6a8faa5f2647e922e70f2516187e | 22,239 |
import decimal
import math
def factorPR(n: int) -> int:
"""Return a factor of n using the Pollard Rho method.
The return value is 1, if n is prime, and a non-trivial factor,
otherwise. Note: This method will occasionally fail to find a
non-trivial factor when one exists.
Examples:
>>> ... | a429f4e5c7fa603615a8bd4f687fc076a4becc56 | 22,242 |
from typing import List
from typing import Any
def _get_lemmas(synsets: List[Any]) -> List[str]:
"""
Return all the lemma names associated with a list of synsets.
"""
return [lemma_name for synset in synsets for lemma_name in synset.lemma_names()] | 43b6be39b733c9fee82958476b32b4ab296c5276 | 22,245 |
def normalize(x):
"""
Normalize input data.
Args:
x (NDarray):
Returns:
Normalized NDarray
"""
return (x-x.min())/(x.max()-x.min()) # Normalize (0.0-1.0) | 5e6adbaff542afd54490665bc1764aa3c2688545 | 22,250 |
def rsplit(_str, seps):
"""
Splits _str by the first sep in seps that is found from the right side.
Returns a tuple without the separator.
"""
for idx, ch in enumerate(reversed(_str)):
if ch in seps:
return _str[0:-idx - 1], _str[-idx:] | 9755b328e0b414721a7db4fe52293100bb03d1a8 | 22,258 |
def compare(numA, numB):
"""
compare(numA, numB):
Compares two numbers. Returns:
1, if the first number is greater than the second,
0, if they are equal,
-1, if the first number is smaller than the second.
Parameters
----------
numA: integer or float
numB: integer ... | 7ad62cb677882d22b32adb517a31a4149685ecef | 22,259 |
def ratio_col(df, df_cols): # Tested [Y]
"""
This function computes the ratio between two columns and returns a Dataframe containing the ratio as a column.
Args
df (pd.DataFrame): Dataframe containing the columns to compute a ratio.
df_cols (tuple): A tuple containing the n... | a4bfc13a5e87604ddae865f6df3b9b123359be52 | 22,264 |
def clean_hanging_newline(t):
"""
Many editors will silently add a newline to the final line of a
document (I'm looking at you, Vim). This function fixes this common
problem at the risk of removing a hanging newline in the rare cases
where the user actually intends it.
"""
if... | 16c80b00530ef333ce1ad39ebf083e12a04ab58b | 22,266 |
def has_video_search_args(request):
"""
Returns whether the object has any video search filter args in it
"""
search_kws = (
"q",
"location",
"channel_ids",
"collection_id",
"tag_ids",
"date",)
for kw in search_kws:
if getattr(request, kw,... | 49dcbdc681be0867c80fc9c8e90a1cbf10a9f5bb | 22,267 |
def _parse_one_level_list(dat):
"""Get list information from queued message
Args:
dat (bytes): received message data
Returns:
list: list of information
"""
results = []
count = int.from_bytes(dat[:2], 'big')
for i in range(count):
base = 2 + 32 * i
results.app... | 6c090cb9e396c1a5ae273f250fd7b0a88fcf418a | 22,269 |
def parse_predecessor_ids(predecessor_ids_string):
"""Parses a comma seperated list of task IDs
Args:
predecessor_ids_string: [string] comma separated task IDs
Returns:
List of task IDs as integers
"""
predecessor_ids = []
predecessor_ids_strings = predecessor_ids_string.split(',')
for predecessor_id_st... | a8c344c5c51a5735899515c10df7b51349d4971e | 22,270 |
import socket
def _receive_devices(port: int, sock: socket.socket, devices: list) -> list:
"""
After sending the device discovery request, collect all available
devices
Args:
port (int): Local port
sock (socket.socket): Socket object
devices (list): List of available devi... | 176a6606e5179f28edefa56c0d79474715d15b5b | 22,274 |
def extract_gif_param(proc: str):
"""
Extracts the parameter for an animated GIF, currently just the frame
display duration in milliseconds, from a string that ends with an
integer, in parentheses.
"""
a = proc.strip(")").split("(")
assert len(a) == 2
return int(a[1]) | 0b9f5f6cc7ecfe38ad36731fc28d932316b5b0b3 | 22,280 |
def R(a0,a1,a2,a3,a4,T):
"""
Troscompt et al (2009) coefficients using Faure et al (2004) equation:
log10(R) = sum(a_n T^{-n/6})
where n=0..4, R is presumably cm^3 s^-1
"""
return a0 + a1*T**(-1./6.) + a2*T**(-2./6.) + a3*T**(-3./6.) + a4*T**(-4./6.) | ea5111f0c745bfc396271092596b932822cd4ada | 22,285 |
def permission_check(check):
"""
Class decorator for subclasses of PublicTask to sprinkle in re-usable
permission checks::
@permission_check(user_id_matches)
class MyTask(PublicTask):
def run_public(self, user_id):
pass
"""
def decorator(cls):
cl... | 14880bf052c7659447dbd9388174860f8f74a133 | 22,286 |
import math
def calcFrag(values):
"""Given a set of parsed values that were generated by macs2 predictd,
see get_size fn, this fn calculates the estimated fragment size and
the sd.
**IMPORTANT: this fn is a python translation of the R code in
chilin2/modules/macs2_fragment/qc.py -- stat_fra... | dd4079265ca300760e364cfbfbcf5705dcf265f5 | 22,300 |
def solution(integers):
"""
Finds the two entries that sum to 2020 and returns their product.
Raises `ValueError` if there is no solution.
"""
inverse = set()
for n in integers:
if 2020 - n in inverse:
return n * (2020 - n)
inverse.add(n)
raise ValueError('no so... | 188aff6b889b7903361ac347fa3806f94a86d39e | 22,301 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.