content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
import string
def range_converter(xl_col_length=3):
"""
Construct conversions between Excel array ranges and
Pythonic indices (up to column ZZ in Excel)
:param xl_col_length: Length of the longest desired
Excel column (e.g., 2 for "A" to "ZZ", 3 for "A" to "ZZZ")
"""
alpha_initial = string... | f904309b2428700005254b45567d2d503c2d3aa8 | 222,839 |
def parse_filename(fname, return_ext=True, verbose=False):
"""
Parses `fname` (in BIDS-inspired format) and returns dictionary
Parameters
----------
fname : str os os.PathLike
Filename to parse
return_ext : bool, optional
Whether to return extension of `fname` in addition to key... | 1512b50fa6d07a0bcbb69831418a935f28abe2d8 | 700,283 |
from datetime import datetime
def fromisoformat(isoformat):
"""
Return a datetime from a string in ISO 8601 date time format
>>> fromisoformat("2019-12-31 23:59:59")
datetime.datetime(2019, 12, 31, 23, 59, 59)
"""
try:
return datetime.fromisoformat(isoformat) # Python >= 3.7
exc... | bcb7e277e907a5c05ca74fd3fbd7d6922c0d7a36 | 30,073 |
import re
def is_indvar(expr):
"""
An individual variable must be a single lowercase character other than 'e', 't', 'n', 's',
followed by zero or more digits.
@param expr: C{str}
@return: C{boolean} True if expr is of the correct form
"""
assert isinstance(expr, str), "%s is not a st... | c00e62199263214596a0b9519868ffdeb86e9580 | 11,004 |
def add_decorators(p):
"""Adds ping and normalize decorators to pool."""
@p.ping
def ping(con):
return True
@p.normalize_connection
def normalize(con):
pass | ba2b02564ff0b2b07ca301cf89b215b71102090b | 89,972 |
def readtxt(ifile):
""" Simple function to read text file and remove clean ends of spaces and \n"""
with open(ifile, 'r') as f:
content = f.readlines()
# remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content]
return content | 8a6490a9e2a49bb0a9fc9c0ac44b03de6b7addbc | 592,344 |
def get_default_values(json_data):
"""
Method to make default values dict for properties in json schema.
Set default value as None if property havent 'default' key in schema
:param json_data: json schema with properties as dict with 'default'
keys or without it
:return: dict wit... | e5727d04fd0e2462a653d8ec4d0d5b2c7e83dc77 | 524,382 |
import torch
def compute_rank(predictions, targets):
"""Compute the rank (between 1 and n) of of the true target in ordered predictions
Example:
>>> import torch
>>> compute_rank(torch.tensor([[.1, .7, 0., 0., .2, 0., 0.],
... [.1, .7, 0., 0., .2, 0., 0.],
... | 0aed5b14ef9b0f318239e98aa02d0ee5ed9aa758 | 13,819 |
import torch
def euclidean_dist(x, y):
"""
Distance calculation between two sets of vectors x (n x d) and y (m x d)
"""
n = x.size(0)
m = y.size(0)
d = x.size(1)
assert d == y.size(1)
x = x.unsqueeze(1).expand(n, m, d)
y = y.unsqueeze(0).expand(n, m, d)
return torch.pow(x... | 31d1e60eac411e87b8c269a713b86b16ef7aa481 | 608,669 |
def merge(a, b):
""" merge two already sorted lists """
s = []
index_a = 0;
index_b = 0;
while index_a < len(a) and index_b < len(b):
if a[index_a] < b[index_b]:
s.append(a[index_a])
index_a += 1
else:
s.append(b[index_b])
index_b += 1... | 10a337af7c42fac382774e95012ab0286db7483c | 403,695 |
from typing import Iterable
def unique_list_conserving_order(iterable: Iterable) -> list:
"""List of unique elements in the original order
>>> unique_list_conserving_order([3, 2, 3, 5, 1, 2, 6, 3, 5])
[3, 2, 5, 1, 6]
"""
seen = set()
return [x for x in iterable if len(seen) < len(seen.add(x) o... | 861dc58dc556b54c72f44afe2372b4e0f1b6cf0d | 444,410 |
def deepmap(func, seq):
""" Apply function inside nested lists
>>> inc = lambda x: x + 1
>>> deepmap(inc, [[1, 2], [3, 4]])
[[2, 3], [4, 5]]
"""
if isinstance(seq, list):
return [deepmap(func, item) for item in seq]
else:
return func(seq) | 43856a93e472f30b84bf842586003952649369c0 | 29,678 |
def two_gaussian_potential_bc(vnew, f2, coords):
"""
Apply Boundary Condition to the potential, force, and coordinates.
Parameters:
-----------
vnew : float (or array of floats)
Potential Energy
f2 : float (or array of floats)
Force
coords ... | 2d9eda2eb4db4a800f72f7b7eaf61c8508c5f848 | 59,885 |
def escape(string):
"""Escape strings to be SQL safe"""
if '"' in string:
raise Exception("Can't escape identifier {} because it contains a backtick"
.format(string))
return '"{}"'.format(string) | 054b3b2908d10f82be41bfaa2e587d4e800dea84 | 653,644 |
def is_valid_channel_name(name):
"""
Return True if the given name is a valid channel name.
"""
# Check channel name length
if len(name) == 0 or len(name) > 50:
return False
# Check if channel name is valid
valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#... | fd4db526972746d0f4fda9d8055e58c93da131c3 | 606,234 |
def env_wise_score(estimator, X, y, scorer, env, env_column):
"""
Filter data to evaluate only a specific environment using a
certain scorer.
"""
env_mask = X[env_column] == env
evaluation = scorer(estimator, X[env_mask], y[env_mask])
return evaluation | d78f8c5e18e8b74395101eb2ca45c238c91930db | 492,311 |
def execute(conn, query, *args):
"""Execute query in connection"""
cursor = conn.cursor()
cursor.execute(query, *args)
return cursor.fetchall() | 9828119c07fba5f91cc7aed28d297d49b87d7c13 | 471,460 |
def infinity_norm(x):
"""
Compute infinity norm of x.
:param x: rare matrix represented as dictionary of dictionaries of non zero values
:return max_value: maximum value of x
"""
max_value = None
for line in x.keys():
for column in x[line].keys():
if not max_value or max_... | f2fa9bf4478c77251cc00c7831fef67751556d0e | 289,616 |
def error_format(search):
"""
:param search: inputted word
:return: bool.
Checking every element in the inputted word is in alphabet.
"""
for letter in search:
if letter.isalpha() is False:
return True | 3b6f6778cfe41e8a2e535f4fce71a7df602bced5 | 676,175 |
def double_char(s):
"""Return word with double characters."""
word = ''
for i in s:
word += i * 2
return word | 7460bf28267dafa68b776c4b24d32c9084272731 | 44,684 |
from typing import OrderedDict
def group_unique_values(items):
"""group items (pairs) into dict of lists.
Values in each group stay in the original order and must be unique
Args:
items: iterable of key-value pairs
Returns:
dict of key -> lists (of unique values)
Raises:
... | cd25d657117b34fe408c27149ddf034f3956383d | 28,917 |
def wordCounter(word:str)->int:
"""
글자수 세는 프로그램
input: str type의 word
output: int type의 글자 수
"""
cnt = 0
a = word.split()
cnt = len(a)
return cnt | b21d94664b75c6a0aa9a7403defe726330a8c0e9 | 310,756 |
def grad(u, *args):
"""
Compute gradient for the cost function
f(u) = ||u-B^-1Av||_B
as del(f(u)) = Bu - Av
"""
A,B,v = args
gradient = B@u - A@v
return gradient | 6ee3275bdd76e2dd3451c9078353fa05dfecbe8f | 509,360 |
def getKeyIdx(key, key2Idx):
"""Returns from the word2Idx table the word index for a given token"""
if key in key2Idx:
return key2Idx[key]
return key2Idx["UNKNOWN_TOKEN"] | 0638c7bd9bc03ca74fa2aff826cf80dac15e3bd0 | 128,744 |
import glob
def get_file_list(search_string):
""" Generates a list of files matching the given glob string
Parameters:
search_string: string
glob-formatted search pattern
Returns:
file_list: list
list of files matching pattern
Outputs: nothing
"""
fi... | 6994b3d87c6a7c74e2d63513a5c5e43c72f3d54a | 526,547 |
def clean_bibitem_string(string):
"""Removes surrounding whitespace, surrounding \\emph brackets etc."""
out = string
out = out.strip()
if out[:6] == "\\emph{" and out[-1] == "}":
out = out[6:-1]
if out[:8] == "\\textsc{" and out[-1] == "}":
out = out[8:-1]
return out | 7420862d495dbb1faa35866da772b63d0bb0ccc3 | 295,407 |
def get_status_embeded_detail(status_embedded):
"""
Retrieve details of status related fields.
:param status_embedded: status details from response
:return: status detail
:rtype: dict
"""
return {
'State': status_embedded.get('state', ''),
'StateName': status_embedded.get('s... | 6605b6daf1d2b80f2f8dc2e545f80059b610c17b | 426,219 |
def _get_skip_class_names(skip_class_map):
"""Returns list of class names to skip
Returned list only contains names of classes where all methods are skipped.
If skip_class_map is None, returns None
:param skip_class_map: Result of passing parsed arg for --skip command line
argument to parse_te... | 892c4d6f70bbf5942dc5f480ba8cdb8c234d49e1 | 240,497 |
import logging
def _test_usability(imported_module, entity_type, entity_ids_arr):
"""
This returns None if a module of a script is usable, otherwise an error message which explains
why this script cannot be used in this context: Wrong platform, unavailable resources etc...
"""
# PROBLEM: When an ... | c317282b465dd7b9a230c07f37bb6e678a3b62c5 | 429,031 |
def pitch_to_midi_pitch(step, alter, octave):
"""Convert MusicXML pitch representation to MIDI pitch number."""
pitch_class = 0
if step == 'C':
pitch_class = 0
elif step == 'D':
pitch_class = 2
elif step == 'E':
pitch_class = 4
elif step == 'F':
pitch_class = 5
elif step == 'G':
pitch_... | 3f506c82523042ad6fcf5f34990ed2f6d4dc9316 | 399,932 |
def get_density(tensor):
""" Return density of the tensor """
shape = tensor.shape
dense_vals = 1.0
for i in shape:
dense_vals *= i
return dense_vals/tensor.nnz | 249f200cd2d2f2c8059c38b6f9e7da1dd595b26a | 462,379 |
def lint(ctx):
"""Run the linter."""
return ctx.run('flake8 calleee tests', pty=True).return_code | 74c9dc88c9f1a5dc552e4f9cc7e24728c518bf49 | 537,144 |
import re
def _match_label(pattern, issue):
"""
:param pattern:
:param issue:
:return: [True] if issue contains the pattern
>>> _match_label('aaa', {'labels': [{'name': 'aaa'}]})
True
>>> _match_label('aaa', {'labels': [{'name': 'bbb'}]})
False
"""
... | e87d603d213950f86d9f968a97bdc8e752f1b79f | 448,080 |
def sizeof_fmt(num, suffix='B'):
"""Returns human-readable file size
Supports:
* all currently known binary prefixes
* negative and positive numbers
* numbers larger than 1000 Yobibytes
* arbitrary units (maybe you like to count in Gibibits!)
Example:
--------
>>> sizeof_fmt(16... | ba1b6cc8b7121a87ccee00bc333d77e4b251dd61 | 645,680 |
def min_value(uncert_val):
"""Minimum confidence interval for a ufloat quantity."""
return uncert_val.nominal_value - uncert_val.std_dev | d8a227c92d89958cc5a4af4991c5f28e80445df7 | 213,004 |
def is_valid_port(port):
"""Checks if a given value might be an existing port. Must be between 1 and 65535, both included.
:param port: Port candidate
:type port: int, str
:returns: True if is valid, False if not
:rtype: bool
"""
try:
int_port = int(port)
except ... | 4a6091f79a71c90ad4535b8008941ff47b89e83c | 518,018 |
def squash_spaces(inp):
""" Convert multiple ' ' chars to a single space.
:param inp: (str)
:return: same string with only one space where multiple spaces were.
"""
return ' '.join(inp.split()) | 5ba2f56fdc5d4a5d7fbc1d482115015fbfc8c7ee | 375,667 |
def __carta(soup):
"""
Gets the most read news from the Carta Capital page
:param soup: the BeautifulSoup object
:return: a list with the most read news from the Carta Capital Page
"""
news = []
container = soup.find('dd', id='fieldset-maisacessadas-semana')
most_read = container.find_a... | 867de5b58333cea3e84c811920b89135f1659f59 | 627,351 |
import json
def get_raw_metrics_from_file(filename):
"""
Parse the given file as JSON and return a dictionary of the
given metric names to their values.
:param filename: JSON file name.
:return: Dictionary of {beanName.metricName->Value}.
"""
values = {}
with open(filename) as mf:
... | 4ef4f47bc8a1d4f8629e9a6bbc382aa60e74d963 | 367,991 |
def replace_negative(l, default_value=0):
"""
Replaces all negative values with default_value
:param l: Original list
:param default_value: The value to replace negatives values with. Default is 0.
:return: Number of values replaced
"""
n_replaced = 0
for i in range(len(l)):
if l[i] < 0:
l[i... | 431781a48a36a00329537b92b589cf223b945ca4 | 13,536 |
from typing import List
import json
def load_train_data(train_data_files: str) -> List:
"""Load jsonl train data as a list, ready to be ingested by spacy model.
Args:
train_data_local_path (str): Path of files to load.
Returns:
List: Tuple of texts and dict of entities to be used for tra... | 78f0517c12b04a630d788ded3c56fd926b0990ec | 616,700 |
def get_note_ancestor(e):
"""
Return the first ancestor of `e` with tag "note", or None if there is no such
ancestor.
"""
for a in e.iterancestors():
if a.tag == 'note':
return a
return None | fd7f2dd7cefaa10460e609a7bc7972b7ba7c3898 | 457,622 |
from pathlib import Path
def read_final(path: Path) -> str:
"""
Repeatedly read the file until it is non-empty.
Some notebook editors empty the source file before updating it.
"""
contents = ''
while len(contents) == 0:
with path.open() as file:
contents = file.read()
... | e9a8951d7fd14ec881a567018277cd3fcbb989f1 | 607,403 |
import ast
import uuid
def get_imports(path: str) -> dict:
"""
This function parse the module at specified path to look for import statements and return a dictionary
representing the import statement.
:param path: path to the Python module
:return: information related to the import statements
... | 9a1e412c832eec29a71b5ccf2fee925d71223277 | 411,974 |
def bfint(self, fname1="", ext1="", fname2="", ext2="", kpos="", clab="",
kshs="", tolout="", tolhgt="", **kwargs):
"""Activates the body force interpolation operation.
APDL Command: BFINT
Parameters
----------
fname1
File name and directory path (248 characters maximum,
... | 0d61efebb0d4f377a70277a68b79a596869a582f | 144,949 |
import math
def effective_priority(value, levels):
"""
Method to determine effective priority given a distinct number of
levels supported. Returns the lowest priority value that is of
equivalent priority to the value passed in.
"""
if value <= 5-math.ceil(levels/2.0): return 0
if value >= ... | b8d7764f71a14522999743778649e6ecf81639a6 | 269,037 |
def extend_diff_outliers(diff_indices):
""" Extend difference-based outlier indices `diff_indices` by pairing
Parameters
----------
diff_indices : array
Array of indices of differences that have been detected as outliers. A
difference index of ``i`` refers to the difference between vol... | 8803567fdede0ad585dc21af4660dbc66838af07 | 608,028 |
def _build_utilization_context(last_week, last_month, this_fy):
"""Build shared context components of utilization reports"""
return {
'last_week_start_date': last_week['start_date'],
'last_week_end_date': last_week['end_date'],
'last_week_totals': last_week['totals'],
... | 3d2a67e8472584faf94320d41fe642bcd1ab0876 | 421,427 |
import math
def get_exp_between_levels(level1: int, level2: int):
"""
Gets the amoount of exp between two levels
:param level1: The low level
:param level2: The high level
"""
exp1 = 0
exp2 = 0
for i in range(1, level2):
if i < level1:
exp1 += math.floor(i + 300 ... | e0e1ee32f9e17ed8e4e5b83c807bb648475e3431 | 210,586 |
def get_rst_bullet_list_item(text, level=1):
"""
Return a list item in the RST format
"""
item = '* ' + str(text) + '\n'
return item | 9104847b8f4432599c9182836a757755d938b4bc | 358,496 |
def talos_colour(role):
"""Check if a role is a Talos Colour"""
return role.name.startswith("<TALOS COLOR>") | 5e6b71cee8ab2dc5a115d28ddf987ecbd28280ba | 446,438 |
def norm_package_version(version):
"""Normalize a version by removing extra spaces and parentheses."""
if version:
version = ','.join(v.strip() for v in version.split(',')).strip()
if version.startswith('(') and version.endswith(')'):
version = version[1:-1]
version = ''.jo... | d6120f9500bb7be879929204938444341d3178e0 | 632,550 |
def filter_api_changed(record):
"""Filter out LogRecords for requests that poll for changes."""
return not record.msg.endswith('api/changed/ HTTP/1.1" 200 -') | caa93f19ce00238786ae0c1687b7e34994b73260 | 686,775 |
def reverseString(string: str):
"""Reverses a string."""
return string[::-1] | 2e1fcf4be067bd78661b81db8d7d4e58ca31c0d4 | 481,006 |
def extract_mac_address(scanlist):
"""
Extracts MAC address from the scan output.
"""
if "Address:" in "".join(scanlist):
mac_address_index = scanlist.index("Address:") + 1
mac_address = scanlist[mac_address_index]
return mac_address.strip()
else:
return "Unknown" | 454aab0f10fb5724053a341fa0affbb5943a84ec | 304,349 |
import string
import random
def get_random_str(size=10, chars=string.ascii_lowercase + string.ascii_uppercase + string.digits):
"""
return a random string
size: size of an output string
chars: characters to use
"""
return ''.join(random.choice(chars) for x in range(size)) | b0d9582829d19bab32d42f3af270d71ccfc6dad4 | 344,409 |
def calculate_bearing_difference(bearing1, bearing2):
"""
Calculate smallest difference from bearing 1 -> bearing2.
:param bearing1: start bearing in degrees (0-360)
:param bearing2: end bearing in degrees (0-360)
:return: angle between -180 and +180 degrees.
"""
# always return difference b... | 438b1c75cd2f6aa77300be17021eff1e796c0135 | 234,485 |
def strip_space(string):
"""Remove spaces from string
:argument string: target string
:type string: str
:returns str
"""
return string.replace(' ', '') | a7a80ec56d4d68b55279804ff9647e881dcca94d | 189,038 |
def size(tensors, axis=0):
"""Measures the size of tensors along an axis.
Args:
tensors: Iterator of tensors.
axis: Optional, axis along which to measure (default 0).
Returns:
Size of tensors along `axis`.
Raises:
ValueError: If shape of tensors do not match along `axi... | 1fad48320b1da16d9a27266509fc33e88c0396d5 | 189,269 |
from typing import List
def get_episode_indices(episodes_string: str) -> List[int]:
"""
Parse a string such as '2' or '1-5' into a list of integers such as [2] or [1, 2, 3, 4, 5].
"""
episode_indices = []
if episodes_string is not None and episodes_string is not '':
ll = [int(item) for it... | 100c21cb438b04587eef635720ca0752a87403ed | 152,835 |
def insertion_sort(array):
"""Insertion sort."""
length = len(array)
for i in range(1, length):
val = array[i]
while i > 0 and array[i - 1] > val:
array[i] = array[i - 1]
i -= 1
array[i] = val
return array | 8f32e1907ec748e656239db12bb5d6d77739606e | 306,150 |
import re
def find_identity_in_list(elements, identities):
"""Matches a list of identities to a list of elements.
Args:
elements: iterable of strings, arbitrary strings to match on.
identities: iterable of (string, string), with first string
being a regular expression, the second string b... | 597b0e89547046a5ff7746344175ef6c0494b5ad | 623,582 |
def getel(s):
"""Returns the unique element in a singleton set (or list)."""
assert len(s) == 1
return list(s)[0] | e4af6436237dca46e5267e8d4ba7f55df68c98a6 | 440,910 |
import json
def load_file_to_json(filename):
"""
Loads a file into a JSON object. Apart from loading the file into JSON, this function also removes
any comments (denoted with #) and imports any other JSON objects (denoted with __LOAD__filename).
:param filename: the filename of the file to be loaded.
:returns:... | 0930240c3fffc604190938f40faa7d94c7cd5898 | 471,743 |
def construct_supervisor_command_line(supervisor_ip, cols, rows, area_size, traffic, road_cells, nodes, apn):
"""Creates the command to start up the supervisor.
:type supervisor_ip: str
:param supervisor_ip: the private IP address of supervisor
:type cols: int
:param cols: city cols
:type rows:... | c72a08139efcb08397a9ad2acb4f82af462e123b | 476,803 |
def get_geom(es_place):
"""Return the correct geometry from the elastic response
A correct geometry means both lat and lon coordinates are required
>>> get_geom({}) is None
True
>>> get_geom({'coord':{"lon": None, "lat": 48.858260156496016}}) is None
True
>>> get_geom({'coord':{"lon": 2.... | e41f6dad73dd41724bb7aa7fa62fca959f2e5a5b | 473,018 |
def generate_projwfc_node(generate_calc_job_node, fixture_localhost, tmpdir):
"""Fixture to constructure a ``projwfc.x`` calcjob node for a specified test."""
def _generate_projwfc_node(test_name):
"""Generate a mock ``ProjwfcCalculation`` node for testing the parsing.
:param test_name: The na... | 9e167013a320e6b69d0121a820ea62efc9fc9f92 | 691,185 |
def Cintegrate(phi,
HC,
dt):
"""
Explicit Euler integration to simulate a classical random walker.
Parameters:
phi (array): prob. distr. at current time
HC (sparse matrix): classical Hamiltonian
dt (float): time step
Returns:
sparse matrix: prob.... | a10e294f7604f7e5733a08296f8a169e5e171077 | 378,076 |
def drop_labels(events, min_pct=.05):
"""
Snippet 3.8 page 54
This function recursively eliminates rare observations.
:param events: (data frame) events
:param min_pct: (float) a fraction used to decide if the observation occurs less than
that fraction
:return: (data frame) of events
"... | 49a93a0ad4ed81bd86733f275cd199f0f56d9f34 | 58,898 |
def stop_word_removal(text_all, cached_stop_words):
"""
Returns text with removed stop words
Keyword arguments:
text_all -- list of all texts (list of str)
cached_stop_words -- list of all stopwords (list of str)
"""
new_text_all = []
for text in text_all:
text1 = ' '.join([word ... | 543e78edb078778d9acb2fd26be90c267a5b6450 | 696,769 |
import math
def rms_error(seq1, seq2):
"""Returns the RMS error between two lists of values"""
assert len(seq1) == len(seq2)
return math.sqrt(sum((x - y) ** 2 for x, y in zip(seq1, seq2)) / len(seq1)) | de846eb1b318e24563319ce11013f6a0ca7583c5 | 623,309 |
def format_data(tax_order, relAbs):
"""
Formats data for plotting
Arguments:
tax_order (list) -- names of tax groups in order of decreasing median median relative abundance
relAbs (dict) -- maps tax group index (determined by which column it is in the csv file) to list of the group name... | cdcec4de49567bac1cfeff64c1492ecd2c1693cc | 443,184 |
import uuid
def uuid_is_valid(test_uuid):
"""
Determine if a UUID is valid
"""
try:
uuid_object = uuid.UUID(test_uuid)
except ValueError:
return False
return True | fbc171e2aeed0fd3587ffbc839f27cdd5b594619 | 362,583 |
def _properties(source, size, require_staging, remote):
"""Args:
source (str): source of the data
size (int): number of rows of the dataset
require_staging (bool): whether the file requires staging
remote (str): remote path
Returns:
dict: set of importer properties... | f5e365a2ca8587c71afb2a868a8411be08529a1f | 424,630 |
def utc_to_utcf(utc):
"""
Converts a UTC datetime into UTC string format %Y-%m-%dT%H:%M:%SZ.
:param utc: python UTC datetime
:return: a string in UTC string format %Y-%m-%dT%H:%M:%SZ
"""
return "%04d-%02d-%02dT%02d:%02d:%02dZ" % (utc.year, utc.month, utc.day, utc.hour, utc.minute, utc.second) | 59c3bba879b33a9c73008245fe57b155aa63a3b1 | 236,482 |
def constant(value: float = 0):
"""
A constant float.
Parameters
----------
value : float
The float value.
"""
return value | de13e64083765fbd429e66f7c55afa887fcbd34f | 597,626 |
import string
import random
def generate_key(length=50):
""" Generate a random string
Args:
length: Number of characters in returned string (default = 50)
Returns:
Randomized secret key string
"""
options = string.digits + string.ascii_letters + string.punctuation
key = ''.j... | dd8463da1e7fca2b51d4e1899e5999ea3e6e1f48 | 180,856 |
def f(n):
"""
f(0) = 0
f(1) = 1
f(2) = 1
f(3) = 2
f(4) = 3
f(5) = 5
f(n) = f(n-1) + f(n-2)
"""
if n < 2:
return n
prev_number = 1
current_number = 1
# f(2)
result = 1
for idx in range(3, n+1):
result = current_number + prev_number
pre... | 093ae51ab1c74ee7eed2cf43a011431de7191f4f | 247,777 |
def blocks(text):
"""Split the text into blocks deliminated by a blank line."""
return text.split("\n\n") | bda99561d35b729203fb7fe945c23147c62ebc24 | 25,495 |
def bisect(list_, x, begin=0, end=None):
"""
Return the insertion point for x to maintain sorted order, in logarithmic
time.
list_ -- the sorted list of items; we assume ascending order by default
x -- the item we search for
begin -- the first index of the range we want to search in
en... | 9d988af61399332d17be7981eaf08f5631aca34f | 436,809 |
def get_file_contents(file_path, method="r"):
"""
Get file contents.
Arguments:
file_path : str
method : str
Returns:
str
"""
with open(file_path, method) as f:
contents = f.read()
f.close()
return contents | dd0735f7ca2fcce54e1234e9aba07cadab8d980f | 501,670 |
def convert_to_list(argument):
"""Convert a comma separated list into a list of python values"""
if argument is None:
return []
else:
return [i.strip() for i in argument.split(",")] | 0bd8ea8c3a0928155dfad9992eb4492b234f3efa | 367,609 |
def get_endpoint(name, array):
"""Return Endpoint or None"""
try:
return array.get_volume(name, pending=True, protocol_endpoint=True)
except Exception:
return None | c85a7a38055358dc5c489286a22002a75616f966 | 539,499 |
import hashlib
def isrightpwd(pwd, encrypted_pwd):
"""
Make Sure The Password(pwd) Is The Right One.
Judge By The SHA512 Encrypted Password(encrypted_pwd).
"""
if hashlib.sha512(pwd.encode('utf-8')).hexdigest() == encrypted_pwd:
return True
else:
return False | b747b9dfee6b9f27c192f27af5b69c32556bff39 | 206,382 |
def get_pokemon_type(pokemon_types):
"""Asks the user for a type of pokemon and displays the names of all
pokemon with that type.
Implicitly converts the type entered by the user to lower case.
If the user enters an invalid type, warns the user and repeats until they
enter a valid one.
Args:
... | 571dbe5d04c749d83bf2bbcd7e6ee3b3f6bf1a62 | 19,293 |
import random
def get_epsilon_greedy_action(epsilon, Q_actions):
"""Choose an action using the epsilon-greedy strategy.
:param epsilon:
:param Q_actions:
:return:
"""
if random.random() > epsilon:
max_actions = []
max_v = float("-inf")
for action in Q_actions:
... | 89edadedbbd532e5635fa8d9467ca1b20fbf45a0 | 252,436 |
import torch
def get_padded_tensor(tensor, n_padding, padding_index, special_embedding, front =True):
"""
Args:
tensor (torch.Tensor): tensor to be padded
n_padding (int): padding nums
padding_index (int): which chars to use: (0-SOS 1-NULL 2-UNK 3-EOS)
special_embedding (torch... | e056e190d7e816b8eebac51c77186658397332e6 | 342,760 |
def perform_substitution(match, substitution_map):
"""Substitutes C++ identifiers with C# identifiers.
We want to perform subsitutions on function names, but not accidentally hit
anything else in the string. For example, if the line looks like this:
/// Returns true if `firebase::crash::Initialize()` has been... | e9fc887fb16a759b76a5e13d8104650d095ce4f9 | 367,867 |
from typing import List
def parse_timeseries(
timeseries_str, time_input_unit="minute", time_output_unit="second"
) -> List[List[float]]:
"""Create a list of 2-list [timestep (seconds), value (mm/hour)]."""
if not timeseries_str:
return [[]]
output = []
for line in timeseries_str.split():
... | 90c498650adf5764a6e7e4fedc1bcf7da454f192 | 655,305 |
def get_user_pw(request):
"""
Obtains user's credentials from request object.
@return (username, password) if credentials are available
and (None, None) otherwise
"""
if 'Authorization' not in request.headers:
return (None, None)
auth = request.authorization
return (auth.username... | 16587db1d82634efede9c6424353a7278332105e | 124,884 |
import json
def load_vocab(f):
"""Load the vocab from a file.
"""
json_dict = json.loads(f.read())
vocab = {int(k): v for k, v in json_dict.items()}
vocab.update({v: k for k, v in vocab.items()})
return vocab | e8c65382377b1c21f881409d3d71bbd5d9395711 | 427,000 |
def get_headers(oauth_token: str) -> dict:
"""Common headers for all requests"""
return {
'Authorization': f'OAuth {oauth_token}'
} | 285f88b6268f50209432698a12ba2b4b57ecd1ee | 691,446 |
def generate_description(slug: str) -> str:
"""
Fungsi yang menerima input berupa string dengan kebab-case
dan mengubahnya menjadi string UPPERCASE.
Fungsi ini juga akan mengakronim input yang terlalu panjang
(lebih dari 30 karakter).
Contoh:
>>> generate_description("home")
'HOME'
... | 9fe1e270d799dc47d8a28d1c4e2eeb4c28525ef3 | 630,731 |
import torch
def kl_term(mean, scale):
"""KL divergence between N(mean, scale) and N(0, 1)"""
return .5 * (1 - 2 * torch.log(scale) + (mean * mean + scale * scale)) | 8654619040a6ff138a0f65c2874f136f0759c9c8 | 127,528 |
def roll(arr, step):
""" Roll array, by "step". """
return arr[step:] + arr[:step] | 11a23fc878b8623b8014ed1b5b440d0db133f996 | 525,488 |
def sigfig(number, places):
"""
Round `number` to `places` significant digits.
Parameters:
number (int or float): A number to round.
places (int): The number of places to round to.
Returns:
A number
"""
# Passing a negative int to round() gives us a sigfig determinatio... | 0aae9fff082b35a18418b2dae8c6b6787e3c947a | 683,944 |
import re
def is_literature(paragraph: str) -> bool:
"""
Check if a paragraph is a literature entry.
Parameters
----------
paragraph : str
Returns
-------
is_literature : bool
"""
doi_regex = re.compile(r"""(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])\S)+)""")
issn_regex... | 861c6332fb4eea0a696e1705c68dd49c6bab0885 | 46,708 |
import math
def atan(x):
"""Get the arc tangent of x, an angle in radians.
The range of result values is [-π / 2, π / 2].
"""
return math.atan(x) | 2a7ec09a5c159f08e4265c192de9b1cfc452c668 | 519,745 |
from struct import pack, unpack
def zfp_accuracy_opts(accuracy):
"""Create compression options for ZFP in fixed-accuracy mode
The float accuracy parameter is the absolute error tolarance (e.g. 0.001).
"""
zfp_mode_accuracy = 3
accuracy = pack('<d', accuracy) # Pack as IEEE 754 double
high = ... | 3a30c75e1ff25d7aeb685f49d7df1aa20527080a | 196,938 |
def adjust_fit(dst_w, dst_h, img_w, img_h):
"""
given a x and y of dest, determine the ratio and return
an (x,y,w,h) for a fitted image (note x or y could be neg).
>>> adjust_fit(4,3,5,5)
(0.5, 0, 3.0, 3.0)
>>> adjust_fit(8,6,5,5)
(1.0, 0, 6.0, 6.0)
>>> adjust_fit(4,3,5,2)
(0, 0.6999... | febcfcb1f35af4b54c6f9d0c2da09bb82e1b0664 | 458,274 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.