content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
def calc_starting_row(page_num, rows_per_age=10):
"""
Calculate a starting row for the Solr search results. We only retrieve one page at a time
:param page_num: Current page number
:param rows_per_age: number of rows per page
:return: starting row
"""
page = 1
try:
page = int(pag... | e9e467f152c0daad2cd9f5431f6abf916893955b | 542,121 |
def sanitize_doc(comment):
"""Substitute HTML breaks for new lines in comment text.
:param comment: The comment text
:returns: Sanitized comment text
"""
if isinstance(comment, list):
return sanitize_doc("\n".join(filter(None, comment)))
else:
return comment.replace("\n", "<br/... | a57ed53aa9941b695d4dcf00f44c8b149881a96b | 473,116 |
from typing import OrderedDict
def extend_vocab(current_vocab, new_vocab, max_tokens=10000):
"""Extends current vocabulary with words from vocab that are not
present in the current vocabulary. Adds up to max_tokens words.
# Arguments:
current_vocab: Current dictionary of tokens.
new_v... | 1613699ac87f9bef361de48417116eb57b90b791 | 510,160 |
def tf_left_split(op):
"""Split the parameters of op for left recursion.
Args:
op: tf.Operation
Returns:
A tuple of the leftmost input tensor and a list of the
remaining arguments.
"""
if len(op.inputs) < 1:
return None, []
if op.type == "Concat":
return op.inputs[1], op.inputs[2:]
... | e02335360329938100873bd2975735aa6330185d | 511,607 |
def discrete_signal(signal0, step_size):
"""
SNIPPET 10.3 - SIZE DISCRETIZATION TO PREVENT OVERTRADING
Discretizes the bet size signal based on the step size given.
:param signal0: (pandas.Series) The signal to discretize.
:param step_size: (float) Step size.
:return: (pandas.Series) The discre... | 279fc47068c706f7634d37e1e49d44cf291bd9dd | 654,400 |
def create_demag_params(atol, rtol, maxiter):
"""
Helper function to create a dictionary with the given
demag tolerances and maximum iterations. This can be
directly passed to the Demag class in order to set
these parameters.
"""
demag_params = {
'absolute_tolerance': atol,
'... | 9de9db696f5f022569ab31d8fe069e34b2d7dc08 | 105,016 |
def currency(value):
"""
Returns a string currency representation of a float
"""
return '${:,.2f}'.format(value) | c9ce50794d1e5c9ede66018b9ded5a25b7e69eaf | 640,962 |
import re
def is_iso_time(time_string):
"""
Checks if string represents valid time in ISO format,
with the default delimiter.
Regex is somewhat convoluted, but general enough to last
at least until the 9999 AD.
:returns:
True if string matches the pattern.
False otherwise.
... | 7df7824af03e3047eb8bfa50b2301e06c977b0d4 | 550,935 |
from typing import Tuple
def format_time(elapsed: float) -> Tuple[int, int]:
"""Convert a quantity of time, in seconds, to minutes and seconds.
:param elapsed: float
A quantity of time, in seconds.
:return: Tuple[int, int]
The number of minutes in `elapsed` and the remaining number of sec... | 6a1ed9a0009e4f219a1903deb1b8f61c4df14e63 | 496,004 |
def data_section(timestamps, data):
"""
Inputs:
timestamps - Nested list CLC formatted timestamps
data - Nested list of CLC formatted data
Outputs:
List of lists where each row in list corresponds to data rows
in the CLC file
"""
data_section = []
for time_idx in ... | f1cb9d96a9a187ec27ee79b2b66fc74ed320cc58 | 164,435 |
def FilterSet(df, annotSet="", annotSetArr=[], annotSetCompare="=", limit=0, negate=False):
"""Provide the ability to filter a Dataframe of AQAnnotations based on the value in the annotSet field.
Args:
df: Dataframe of AQAnnotations that will be filtered by the specified annotation set.
annotSet: String to... | e56f7fb5aba3efeba231a03f1be3817550ae7b4a | 663,639 |
def valid_xss_content_type(http_res):
"""Check whether the returned content-type header allow javascript evaluation."""
# When no content-type is returned, browsers try to display the HTML
if "content-type" not in http_res.headers:
return True
# else only text/html will allow javascript (maybe ... | 14d7148521d4eb0457c58db9728beb082ec87e75 | 549,479 |
import requests
def get_linked_status(gh_headers, linked_issue_info):
"""
Returns the open/closed status of the linked issue
"""
lorg, lrepo, lnum = linked_issue_info
print(f"::set-output name=linkedIssueInfo::Found a \
linked issue: {lorg} {lrepo} {lnum}")
issue_url = f"https://api... | 1874833868cf8132457e75b95eb02ffe17078c54 | 575,401 |
def List_genomes (file_genomes_fasta):
"""
Convert the TXT file with the individual genomes into a list with the complete
names of each individual genome.
"""
# Create the list:
list_genomes = []
# Open the file to read.
genomes = open(file_genomes_fasta, 'r')
for line in genomes:
... | 90becaec12da4278bd9dc77b73f3b1e9c1b41ae4 | 543,073 |
def pegtop_blending(rgba, norm_intensities):
""" Calculates image colors with the Pegtop Light shading of ImageMagick
See:
http://www.imagemagick.org/Usage/compose/#pegtoplight
Forked from Ran Novitsky's blog (no license found)
http://rnovitsky.blogspot.nl/2010/04/u... | 878d1c09d9746b20d47284d931a5da0f4aa0cf29 | 593,272 |
def bitarray2fasm(bitarray):
""" Convert array of bits ('0', '1') into FASM value.
Note: index 0 is the LSB.
"""
bitstr = ''.join(bitarray[::-1])
return "{}'b{}".format(len(bitstr), bitstr) | b1a94ab76dc1569fb835d36f3c6754060c5f6114 | 408,274 |
def _shift_twelve(number):
"""Shifts the number by 12, if it is less than 0.
Parameters
----------
number : int
Returns
-------
int
"""
return number + 12 if number < 0 else number | cca6689a7caabbaae0e352b4e91b64ebb1f63ad7 | 686,070 |
def is_ref(value):
"""
Whether the value is a reference.
"""
return isinstance(value, str) and (value.startswith('$') or value.startswith('@')) | 454315286dce05d1bb95b237556b575b321f99a5 | 466,842 |
import re
def mk_rule_fn(rule, name):
"""Test helper for converting camelCase names to
underscore_function_names and returning a callable rule fn."""
parts = re.sub('(?!^)([A-Z][a-z]+)', r' \1', name).split()
parts.insert(0, 'with')
fn_name = '_'.join(map(lambda x: x.lower(), parts))
retur... | e6ba5e1c409077911e2b67878dba078dc2890c38 | 659,032 |
from typing import Union
def decrement_by_n(n: Union[int, float]):
"""Generates a function that
will decrement by n.
Args:
n (int): integer to decrement
by.
>>> decrement_by_n(2)(2)
0
"""
def decrementor(base: Union[int, float]):
return base - n
return decre... | 77230babd08ffcb0643cdaf37ed4cbec79ce08ef | 318,768 |
def preprocess_logic_form(lf, conversions: dict) -> list:
""" Preprocess a single logic form. """
new_lf = []
for term in lf:
if isinstance(term, list):
term = preprocess_logic_form(term, conversions)
elif isinstance(term, str):
if conversions.get(term):
... | 9a17a2edebbc5344f6dda4495016f182e1203fbb | 155,297 |
def split_gates(inputs: int) -> tuple[int, int]:
"""Splits the number of inputs across the left and right of the karnaugh map."""
left = inputs // 2 # Larger for odd
top = inputs - left
return left, top | 76e5eeae7e50b075eced8753da44c229e2b87578 | 74,478 |
import yaml
def yaml_loader(filepath, log):
"""Load a yaml file"""
try:
with open(filepath, "r") as myfile:
return yaml.load(myfile, Loader=yaml.BaseLoader)
except FileNotFoundError as e:
log.error(e)
quit() | 55de05c37aebb724e101b306f439a2836929d09f | 573,175 |
import ast
def parse_comment(comment):
"""
Parse a comment of the form
# investigation_time=50.0, imt="PGA", ...
and returns it as pairs of strings:
>>> parse_comment('''path=('b1',), time=50.0, imt="PGA"''')
[('path', ('b1',)), ('time', 50.0), ('imt', 'PGA')]
"""
names, vals = [], []... | 9e46e817a7125a9f677064955653c81300d3e610 | 232,814 |
def get_val_unit(json_data, arg1, arg2, unit):
"""Extract value from JSON and concatenate with a unit."""
value = str(json_data[arg1][arg2])
return f"{value}{unit}" | de66fe4b87c43eccfb6e30e3e577e14613a855a7 | 577,672 |
def time_to_seconds(duration):
"""
时间字符串转换成秒
:param duration: 时间格式串, 例如"01:46:00"
:return seconds: int
"""
hours, minutes, seconds = duration.split(':')
seconds = int(seconds)
if minutes > '00':
seconds += int(minutes) * 60
if hours > '00':
seconds += int(hours) * 360... | fe13fbac838186a4803acbcf814b68a0c30abf8e | 382,204 |
import struct
def find_coordinator_error(error):
"""
Encode an error'd out FindCoordinatorResponse v0.
:param error: Kafka errno
"""
return b"".join([ # FindCoordinator Response v0
struct.pack('>h', error), # Error Code
struct.pack('>i', -1), # Coordinator id
... | e909411e07c143dbe13f77904bfc824e33626695 | 343,242 |
def is_uniform(x, epsilon=0.000000001):
"""Determine if the vector is uniform within epsilon tolerance. Useful to stop a simulation if the fitness landscape has become essentially uniform."""
x_0 = x[0]
for i in range(1, len(x)):
if abs(x[i] - x_0) > epsilon:
return False
return True | 95441cbc7ad1c06461d1e029d2490250e491e020 | 585,030 |
def add(a: int, b: int) -> int:
"""
Add of a and b.
>>> add(3, 3)
6
>>> add(3, -3)
0
>>> add(99, 1)
100
"""
return a + b | 8196b5e31564d293bd2fb7cceae3641986740e30 | 559,823 |
def gt(x, y):
"""Implement `gt`."""
return x > y | ceb5d4cf5812a587fc8aaea8f2c5e97b6ac7c973 | 65,610 |
def _is_int(value):
"""Use casting to check if value can convert to an `int`."""
try:
int(value)
except ValueError:
return False
else:
return True | 56e1b97651afe017fa4e9f0c8c5542754d112d70 | 106,365 |
import re
def validate_container_name(name):
"""Make sure the container name conforms to Azure's expectations
"""
label = '[a-z0-9]+(?:[a-z0-9\-]*[a-z0-9])*'
validate_name = re.compile('^' + label + '$')
return (
len(name) >= 3 and len(name) <= 63 and bool(validate_name.match(name))
) | f1ab6f30f22a259b812b784c831fbb9b5753b613 | 412,655 |
def array_find(arr, obj) -> int:
"""A helpher function which finds the index of an object in an array.
Instead of throwing an error when no index can be found it returns -1.
Args:
arr : the array to be searched
obj : the object whose index is to be found.
Returns:
int: The i... | df8733b073d24d47f7be8f3b01b3f9e2d78f51bc | 683,247 |
def filter_dictionary(dictionary, keys):
"""Filters a dictionary from a list of keys."""
return {key: dictionary[key] for key in dictionary if key in keys} | ffc1d0591d69e27ff2d5acdd826c1ebe39b59c08 | 274,949 |
def _tf_tensorarray_append(target, element):
"""Overload of append that stages a TensorArray write at the last position."""
return target.write(target.size(), element) | 3f064a1399839b9200a77a2ecce9dc540af3c440 | 543,059 |
import math
def slopeRadians(dist):
"""
Given the distance in meters find the slope in radians.
We assume the "road" is an undulating sine wave
with a constant frequency and amplitude. If amplitude
is 0 there is no undulations.
"""
# The cos(sin(x)) is the slope of a line tagent to the si... | 8b1f2303df259e499ef2f8e26472bd76ac0ee349 | 480,411 |
def _check90deg(pd):
"""Return True if the Euler angles are 0,90,180, etc"""
psi, phi, theta = int(pd['psi']), int(pd['phi']), int(pd['theta'])
if psi % 90 == 0 and phi % 90 == 0 and theta % 90 == 0:
return True
else:
return False | f159d7f10e96b13b818ed51b2e0f0add731264a3 | 514,318 |
def _get_item(i, j, block):
"""
Returns a single item from the block. Coords must be in block space.
"""
return block[i, j] | 45a12ecb3959a75ad8f026616242ba64174441fc | 707,953 |
def v0_lstrip(iterable, strip_value):
"""Return iterable with strip_value items removed from beginning."""
stripped = []
is_beginning = True
for item in iterable:
if is_beginning:
if item != strip_value:
is_beginning = False
else:
continue
... | 33c89d02614a12caad04530fa191bc2009810eca | 232,430 |
def K(A,L=None):
"""
Function we are trying to maximize: sum_{i<j} A[L[i]][L[j]]
A is m x m matrix,
L is a subset of range(m) (or defaults to range(m) if no L given)
"""
if L==None:
L = range(len(A))
return sum([A[L[i]][L[j]] for j in range(len(L)) for i in range(j)]) | de8c83c695a8262131b4bef707d48255a3be89de | 642,361 |
def get_novel_gene_ids(cursor):
""" Fetch IDs of all novel genes in the database """
query = """SELECT DISTINCT(gene_ID) FROM observed
LEFT JOIN gene_annotations AS ga ON ga.ID = observed.gene_ID
WHERE (ga.attribute = 'gene_status' AND ga.value = 'NOVEL')
"""
... | 2f8d5b5c9cef77e7c289d03739897ef7af4a85a8 | 273,182 |
import re
def apply_lit_token(arg, macro):
"""Apply the LIT or ARG_LIT macro to a single token."""
# The macro must only be applied to a floating-point constant, not
# to an integer constant or lit_* value.
sign_re = r'[+-]?'
exp_re = r'([+-])?[0-9]+'
suffix_re = r'[lLfF]?'
dec_exp_re = r'... | 54bd3210298bc3bd693408f5aa2769a0d73b95a5 | 409,263 |
import imp
from importlib.machinery import SourceFileLoader
import six
def load_module_from_file(name, filepath):
"""Load a python module from a sourcefile.
Args:
name (str): Module name.
filepath (str): Python sourcefile.
Returns:
`module`: Loaded module.
"""
if six.PY2:... | c9d066af0a66bdd80420e86fe81d889de91ff704 | 238,174 |
import re
def natural_sort(string):
"""
Natural sorting function which sorts by numerical value of a string,
rather than raw ASCII value.
"""
return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string)] | ebb771e4ffc877da74b8d4014eff4a7ab8e04fa5 | 116,390 |
import math
def softmax_mom(p, q, n = 5):
"""
Two-place softmax function.
Parameters
----------
p = float
one interpolant.
q = float
another interpolant.
n = float
scaling parameter.
"""
return 1 / n * math.log(1.01**(n * p... | a2749c2f573e1a1c8e79f9d29b6d9a152279b7dd | 262,046 |
def is_ldap_user(user):
"""Check whether user is a LDAP user.
"""
return user.source == 'LDAP' or user.source == 'LDAPImport' | 98631660abc1aa087f1e2ab9c6f6381b44757c41 | 470,358 |
def ensure_ascii(in_string):
"""Remove any non-ASCII characters from the input string
Parameters
----------
in_string : str
Input string
output_string : str
String with non-ASCII characters removed
"""
encoded_string = in_string.encode('ascii', 'ignore')
return encoded_... | 6440cb66d6bee0af7cd2e93052636598e553ec19 | 99,049 |
def lcm(x: int, y: int):
"""
Returns the least common multiple of two integers.
:param x: any integer
:param y: any integer
:return: integer z such that remainder(z/x) == 0 and remainder(z/y) == 0
Not necessarily faster solution, just a different solution using filter
"""
# if either n... | 7d0d1f28c805b4cc21baaba6adc105f442e1ee9f | 548,585 |
def choosecat(str, classnames):
""" Gets a string and tries to match its category with an int. """
for i, id in enumerate(classnames):
if id in str:
return i
return None | ad3ccefd63e92be774386209f114465dbc5d482e | 346,859 |
import pkgutil
import importlib
def import_solver(package, day):
"""Import and return a solver module for a day of the Advent calendar.
Return None if the module can't be found. No ImportError is raised in this
situation in order to distinguish it from an error occurring during import.
"""
prefi... | 2ce1fd094ef0b9373e603fe5a4e991bb8bf1a3ce | 261,255 |
def get_union_metrics(metric_a, metric_b):
"""Union of metric_a and metric_b
:param metric_a: list
:param metric_b: list
:return: Union metrics list from metric_a and metric_b
"""
if metric_a is None and metric_b is None:
return None
elif metric_a is None:
return metric_b
... | 145ccd006ecf6ddb30045c7d9fd3647a6644d7cc | 204,140 |
import json
from pathlib import Path
def get_total_epochs(save_path, run, last_gen):
"""
Compute the total number of performed epochs.
Parameters
----------
save_path: str
path where the ojects needed to resume evolution are stored.
run : int
curre... | a9f046640b2502ae5057ab9cfc88ea37d895863e | 686,701 |
def str_rstrip(text):
"""Strips whitespaces from the end of the text."""
return text.rstrip() | 95136f1153d3710703a5a22f34bc91e83d8da1d7 | 594,872 |
import functools
import shutil
def save_workspace(new_workspace):
"""Decorator to save a workspace to a new location.
If `new_workspace` already exists on disk, it will be recursively
removed.
Example usage with a test case::
import natcap.invest.testing
@natcap... | 85312257d87450fa4eb10a8aac1fdfa7c69c4c48 | 210,005 |
def folder_contents_html(folder_path, files, folders):
"""Given files and folders generate html."""
html = "<!DOCTYPE html><html><body>{}</body></html>"
atag = '<a href="{}">{}</a>'
files_and_folders = ''
for folder in folders:
files_and_folders += '<h4>' + atag.format(folder_path + '/' + fo... | 6b6b37ca9452319d309a61c877ebf6d1fba201aa | 698,954 |
def find_next_coefficient_c(k, previous_c, lamb, beta):
"""Return c_k = lamb^(k-1) * (1 - lamb)^(k * beta - k + 1) * \\binom{k * beta}{k - 1} / k.
To avoid large values of \\binom when 'k' is sufficiently big, and to decrease the runtime,
the value is computed as:
с_1 = (1-lamb)^beta,
c_k = c_{k-... | 3d9f68e222c2cf9320e2c9a1fdc5970e80f0a159 | 591,640 |
import math
def compute_fuel_mass(mass: int) -> int:
"""
Computes the fuel required for a specific mass, taking
into account that the fuel has mass itself.
"""
# Stopping condition
if mass <= 0:
return 0
fuel = (math.floor(mass/3)-2)
fuel = fuel if fuel > 0 else 0
return ... | 43c160d1344a79b2d5480a315a8fa6ac75ee54a4 | 562,560 |
import math
def isPrime(n):
"""
check if the input number n is a prime number or not
"""
if n <= 3:
return n > 1
if n % 6 != 1 and n % 6 != 5:
return False
sqrt = math.sqrt(n)
for i in range(5, int(sqrt)+1, 6):
if n % i == 0 or n % (i+2) == 0:
return Fa... | 91da5b13840181d039902e2db3efb8cc09609465 | 5,091 |
def DifferenceLists(entries):
"""
Find difference of one list with another.
Useful for existing lists or complex searches.
Inputs:
entries (list) : list of two lists to
difference [[...],[...]]
Outputs:
diff (list) : difference of all entry lists
"""
if len(entri... | e9d0c9474d7e21e267f425c5409df472ace28b6d | 506,345 |
def py(url, description, doc=''):
"""Make a markdown table entry for a .py file."""
if doc: doc = f'[documentation]({doc})'
return f'|[{url}](/blob/master/py/{url})|*{description}*|{doc}|' | a9353cbd206e39dec74593bd349600e886e65384 | 315,546 |
import re
def natural_sort_key(string_to_split):
""" Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
Can be used to implement "natural sort" order. See:
http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html
http... | 0ff6c6e1b522eb2b92c18164249aa17454afefb0 | 223,509 |
from typing import List
def pad_batch(context_tokens: List[List[int]], pad_id: int, pad_len: int):
"""
pads context lengths in context_tokens with pad_id to equal neox_args.seq_length,
and returns the padded batch and the new lengths.
context_tokens: list of lists of tokens
pad_id: int, integer t... | f447ad215acb21fcd95c427dce043426d7224594 | 580,852 |
def upsample_method(request):
"""Fixture for parametrization of Grouper upsample methods."""
return request.param | 9baed452ea6ac68b456ecbda6e7f9b56faf2f299 | 252,132 |
def kron_d(i, j):
""" The Kronecker delta. """
return 1 if i == j else 0 | 4b4bfe35bc4407ebde917b70d4052259c984c0d4 | 77,484 |
def calc_rank(someset, n):
"""
Calculates the rank of a subset `someset` of {1, 2, ..., `n`}
in the ordering given by grey sequenccce of characteristic vectors.
"""
assoc_seq = [k + 1 in someset for k in range(n)]
bit = False
rank = 0
for k, x in enumerate(assoc_seq):
bit ^= x
rank += bit * 2**... | 4b3a7a226d90a5431d7622788e1d84ab406b40c3 | 629,799 |
def aggregate_conversations(li, delta):
"""Aggregates the conversation count.
Keyword arguments:
li --- The list of tuples containing tweet creation date
and number of user mentions.
delta --- The timedelta object by which the conversations
should be aggregated.
... | 06682bef2693b3a528074f1796e45d548d628425 | 408,636 |
def distribute_events_between_lines(n_events: int, n_lines: int) -> list[int]:
"""
Distribute evenly the specified number of events between the specified number of lines.
:param n_events:
number of events
:param n_lines:
number of lines
:return:
list of numbers of events in ... | 70add1c8e66c3fee10d5f4cd2b9ae342f8e8cd24 | 327,750 |
def isDatetimeObjTzAware(dt):
"""Find if the datetime object is timezone aware"""
return dt.tzinfo is not None and dt.tzinfo.utcoffset(dt) is not None | fe40e4a2814380d9672a76b24a4d38d728e36141 | 393,316 |
def cont(X):
"""Returns True for all non-categorical columns, False for the rest.
This is a helper function for OpenML datasets encoded as DataFrames simplifying the handling
of mixed data types. To build sklearn models on mixed data types, a ColumnTransformer is
required to process each type of column... | 9b4842936d82764f3e1c15aa2edeff4946fcda65 | 547,234 |
import tokenize
def is_name_token(token, name):
"""Check if a token is a NAME and has a specific string value
NAME tokens are returned by the ``tokenize`` module for identifiers
and keywords: ``if``, ``lambda``, ``a_variable``, ``is_name_token``, ...
Arguments:
token (tokenize.TokenInfo): Th... | a4e01855a59e9c86ff93b1a60239eeafad5caeae | 167,068 |
def Confirm(text, default='N'):
"""Asks the user to confirm something.
Args:
text(str): the text of the question.
default(str): set the accepted value if user just hits Enter.
Possible values: 'Y' or 'N' (the default).
Returns:
bool: True if the user confirms, False otherwise.
"""
print(tex... | 57e55ad780b9a442d245b2e12863d52650a7aa56 | 614,628 |
def corr_row_i_row_j(row_i, row_j):
"""This function will compute the correlation between one row, i, and a second row, j"""
return row_i.corr(row_j) | 2db4a1b1ce5c99b963a4fc6dea8573c62923c9fd | 424,178 |
def clamp_short(x):
"""Clamps a signed short to be within its upper and lower bounds."""
return -32768 if x < -32768 else 32767 if x > 32767 else x | daab709a37e3689c1639bc0505721bd329c13126 | 519,250 |
def list_difference(list1, list2):
"""
Given two lists with alignments list1 and list2, return a new list
(new_list2) that contains all elements of list2 without elements of
list1.
"""
# Create an inverted list of alignments names in list1 to allow fast
# lookups.
inverted_l... | 38821fead3f769c16721ad1f7e4f84a2af9238fa | 514,751 |
def parse_filenames_and_sizes(list_file):
"""
Takes a file with tab-delimited filename/size pairs and returns a
filename-->size dict.
"""
filename_to_size = {}
with open(list_file,'r') as f:
for line in f:
if ('catalog.json' in line) or ('stac.json' in lin... | ca029d217a6b0b6ba65acea63255542179a413c2 | 600,032 |
import random
def rationed_split(examples, train_ratio, val_ratio, test_ratio, shuffle):
"""
Splits a list of examples according to the given ratios and returns the
splits as a tuple of lists (train_examples, valid_examples, test_examples).
The list can also be randomly shuffled before splitting.
... | dd18c21c34f1d18bdede81c90592264517d91f1b | 227,260 |
def num_spaces(src: str) -> int:
"""Count the number of spaces at the beginning of the string."""
return len(src) - len(src.lstrip()) | da3889e968f40eaf106da3d3bfa67d148fb8dbf4 | 390,202 |
from pathlib import Path
def _get_uml_filename(module_filename) -> str:
"""
Return the UML file name, for a given Python module name.
:param module_filename: e.g. cylc.flow.ws_messages_proto.pb2.
:type module_filename: str
:return: UML file name (e.g. ws_messages_proto).
:rtype: str
"""
... | de3ef084c8d6896ac54962ad13320977db4d72f5 | 246,827 |
def make_aux_coord(cube, axis='Y'):
"""Make any given coordinate an Auxiliary Coordinate."""
coord = cube.coord(axis=axis)
cube.remove_coord(coord)
if cube.ndim == 2:
cube.add_aux_coord(coord, 1)
else:
cube.add_aux_coord(coord)
return cube | 51c0ea85fca3397d8326ef6ae763f204baaaaa71 | 192,706 |
import six
def anykey(d):
"""
Return any key in dictionary.
:param d: dict: dictionary
:return object
"""
return next(six.iterkeys(d)) | 428e63d856ea2615d594b79013692e10baf0e80c | 198,634 |
import torch
def flatten(lst):
"""
Flattens a list or iterable. Note that this chunk allocates more memory.
Argument:
lst (list or iteratble): input vector to be flattened
Returns:
one dimensional tensor with all elements of lst
"""
tmp = [i.contiguous().view(-1, 1) for i in lst]
... | 8a6d38c2e8d8e031f19bd7860237a32475709bea | 654,310 |
def adjust_volume_gravity(vol, og, new_vol):
"""
Calculate the new gravity after boil off or dilution to ``new_vol``
This is unit independent and the volume can be used for liters
and or gallons.
Ending Gravity = (Beginning Volume * Beginning Gravity) / End Volume
:arg vol: Original volume of ... | df73322ae59f3551296756415889c5907d850e4a | 427,195 |
def check_max_drawdown(
initial_balance: float, current_balance: float, max_drawdown: float
) -> bool:
"""
check if the loss exceed the max given drawdown
"""
percentage = 0.01
max_drawdown_percentage = max_drawdown * percentage
is_in_drawdown = False
if current_balance < (initial_balanc... | 8c0c57f95ac072795c654648b8721a9ed1f61676 | 463,595 |
def to_matrix_vector(transform):
"""Split a transform into it's matrix and vector components.
The tranformation must be represented in homogeneous coordinates
and is split into it's rotation matrix and translation vector
components.
Parameters
----------
transform : ndarray
NxM tra... | 210a01d0d4fb53e69f9a18ffa6358a0de0aca415 | 571,817 |
def extract_turn_10_more(df):
"""
Given a concise conversation dataframe, extract those with 10 or more dialog turns.
Arg:
df: A conversation dataframe from a subreddit.
Return:
turn_10_more: A dataframe containing only those conversations with 10 or more turns.
"""
turn_dist = ... | 5f4559f023dfc85bf02d7e0ed6326fd1ffd1abce | 102,947 |
import re
def replaceall(replace_dict, string):
"""
replaceall will take the keys in replace_dict and replace string with their corresponding values. Keys can be regular expressions.
"""
replace_dict = dict((re.escape(k), v) for k, v in list(replace_dict.items()))
pattern = re.compile("|".join(lis... | 66c6ec299476986011de21a5f28a613c44435d33 | 41,894 |
import re
def parse_url(url, quadkey_zoom):
"""Parse quadkey from url
"""
name = url.split('/')[-1]
match = re.findall(r'([0-3]{{{0}}})'.format(quadkey_zoom), name)
qk = match[-1]
return qk | d4cb66e5507af5f141a4c3c0b4fb661677130dc8 | 339,569 |
def compute_gradient(y, y_predicted, tx, N=1, regularization=0):
"""Computes gradient of a linear regression model with squared loss function
Parameters
----------
y : np.ndarray
Class labels
y_predicted : np.ndarray
Labels predicted by model
tx : np.ndarray
Data
N :... | 6b5b69ca3726f1d05010a0e73e12da9e84e7d50b | 120,769 |
def parse_jcamp_line(line,f):
"""
Parse a single JCAMP-DX line
Extract the Bruker parameter name and value from a line from a JCAMP-DX
file. This may entail reading additional lines from the fileobj f if the
parameter value extends over multiple lines.
"""
# extract key= text f... | 84061c3f4bc42a62e308d5f93877e5c55d85efc1 | 13,833 |
import pickle
def load_calib(filename):
""" Loads calibration parameters from '.pkl' file.
Parameters
----------
filename : str
Path to load file, must be '.pkl' extension
Returns
-------
calib_params : dict
Parameters for undistorting images.
"""
# read python dict back from the file
pk... | 93700abe123df3ebcd17bddf16a6acd1a42ea1a7 | 22,647 |
import re
def _expand(string):
""" Convert number ranges with hyphens and commas into list of numbers """
result = []
for element in re.split(', *', string):
# Expand 1-2 to [1, 2] or 1/3-4 to [1/3, 1/4] or 1/5-1/6 to [1/5, 1/6]
m = re.match(r'([0-9/]*?)(\d+)-\1?(\d+)', element)
if... | 8eab6c2408b40457cbcda2c34641414a2dcceea5 | 198,458 |
import requests
from bs4 import BeautifulSoup
def get_url_paths(url, ext='ff'):
"""
Function to obtain the list of elements in ``url``.
Parameters
-------------
url : `str`
Path to the Internet address.
ext : `str`, optional
File extension to search for. This variable is set ... | db0e4d12011d8394b5db4f6276e98e5b49cba479 | 167,967 |
def camelcase(var): # someVariable
"""
Camel case convention. Include an uppercase at every first element except the first.
:param var: Variable to transform
:type var: :py:class:`list`
:returns: **transformed**: (:py:class:`str`) - Transformed input in ``camelCase`` convention.
"""
... | 7591ba43aaa05c10892f9699687b0f7306fc22b3 | 327,494 |
def load_capcodes_dict(filename):
"""Load capcodes to dictionary."""
capcodes = {}
try:
print("Loading data from '{}'".format(filename))
with open(filename, "r") as csv_file:
csv_list = [
[val.strip() for val in r.split(",")] for r in csv_file.readlines()
... | a0172a8bc58609604b3d457f088cf445367e41a4 | 111,901 |
def is_number(c):
"""
Checks if the given character is a number.
:param c: The character to check.
:return: True if the character is a number, False otherwise.
"""
return '0' <= c <= '9' | 4c115405d46e878863de4bf84b058fce586df331 | 584,579 |
def fib(n):
"""Calculates the nth fibbonaci number. NOTE: Uses index counting"""
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2) | 1662f9b826dc8a74eab9776f9d3032d2ffb88a38 | 596,589 |
def prompt_output(cli_input, converted=None):
"""Return expected output of simple_command, given a commandline cli_input string."""
return f'Opt: {cli_input}\n{converted or cli_input}\n' | beceecf80039452e68acb02005ab35ce939be735 | 685,957 |
import math
def translate_point(point, angle, distance):
"""Translate a point a distance in a direction (angle)."""
x, y = point
return (x + math.cos(angle) * distance, y + math.sin(angle) * distance) | a32c4209cad97fc670c18acb47c27ec7fbc8bc5c | 47,228 |
def data_repeated(data):
"""
Generate many datasets.
Parameters
----------
data : fixture implementing `data`
Returns
-------
Callable[[int], Generator]:
A callable that takes a `count` argument and
returns a generator yielding `count` datasets.
"""
def gen(count... | ace8addf548e07655584cc20f7c7038a4cc94579 | 212,053 |
def calculate_polynomial_term(coefficient, variable, order):
"""Calculates a term in a polynomial.
Args:
coefficient (float): The coefficient to use in
calculating the polynomial term.
variable (float): Value to plug in for the variable in the polynomial
term.
or... | 1f7d657ded741c4ba840f6657fe469c6014d38d5 | 382,784 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.