content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
import pickle
import pkg_resources
from pathlib import Path
def load_model(model):
"""Loads the model from the passed path or name"""
if isinstance(model, Path):
return pickle.loads(model.read_bytes())
model = 'models/' + model + '.pkl'
model = pkg_resources.resource_string('botrecon', model... | 7b416e39a740a25f360fcbd4371d76213de3e2ef | 614,225 |
import struct
def _ReadCoverageInfoEntry(data_file):
"""Reads a packet of data from the specified file."""
UINT32_SIZE = 4
pkt_size_buf = data_file.read(UINT32_SIZE)
if len(pkt_size_buf) != UINT32_SIZE:
raise ValueError("Invalid packet size read.")
pkt_size = struct.unpack("I", pkt_size_buf)[0]
pk... | 3d820f25074f7eb21ee46a5e6f0c6a79d290a5e7 | 262,728 |
def _calculate_working_record_checksum(working_record):
"""
Calculates the checksum of the argument ascii-hex string
@retval int - modulo integer checksum value of argument ascii-hex string
"""
checksum = 0
# strip off the leading * and ID characters of the log line (3 characters) and
# str... | 759d661908ab7049adabd0496bdb6ecbc48c7544 | 370,465 |
def is_distributed_model(state_dict):
"""
determines if the state dict is from a model trained on distributed GPUs
Parameters:
-----------
state_dict: collections.OrderedDict
Returns:
--------
Boolean
"""
return all(k.startswith("module.") for k in state_dict.keys()) | d5ca818ba4c005c487c6e5a6e8b09597aa61a499 | 544,819 |
def check_used_once(g):
"""Returns True if a graph has only one usage."""
mng = g.manager
return sum(mng.graph_users[g].values()) == 1 | b13a98066709e7749c4f38da9453fef547bf02ee | 502,434 |
def _date_to_int(d):
"""Return a date object as a yyyymmdd int."""
return int(d.strftime("%Y%m%d")) | f41c150e303723ecc2d8a3e93fb06e8bc41c3676 | 302,988 |
def score_substitute(a_c1, a_c2):
"""Score substitution of two characters.
Args:
a_c1 (str): first word to compare
a_c2 (str): second word to compare
Returns:
int: 2 if the last characters of both words are equal, -3 otherwise
"""
return 2 if a_c1[-1] == a_c2[-1] else -3 | 6c329daf5cdc9cfda54523d7322e6e2b92e4d4f7 | 573,495 |
def getSlotFromCardName(cardName):
"""
cardName is expected to be of the form 'gem-shelfXX-amcYY' where XX & YY are integers
"""
slot = (cardName.split("-")[2])
slot = int(slot.strip("amc"))
return slot | e269321b3547142f4180ab906383c57aed91dc74 | 475,150 |
def genAliases(name):
"""
Generates aliases for metabolite names, e.g.:
val --> set(['Val-L', 'Val', 'val', 'val-L'])
"""
name = name.replace('-L','').replace('-l','')
output = []
output.append(name)
output.append(name.lower())
output.append(name.lower()+'-L')
... | 28b88a35588197765e296528fd3b05f34baa1351 | 31,227 |
def dic_sum_up_lengths(in_dic):
"""
Given a dictionary with strings or numbers, sum up the numbers /
string lengths and return the total length.
Currently works for integer numbers and strings.
>>> in_dic = {'e1': 5, 'e2': 10}
>>> dic_sum_up_lengths(in_dic)
15
>>> in_dic = {'e1': 'ACGT'... | c019d0e9b7425b02b9fb661a5ae85b27ac05ffe1 | 466,677 |
def xaxis3D(
xaxis3d_type=None,
xaxis3d_name="",
xaxis3d_name_size=16,
xaxis3d_name_gap=20,
xaxis3d_min=None,
xaxis3d_max=None,
xaxis3d_interval="auto",
xaxis3d_margin=8,
**kwargs
):
"""
3D x 轴配置项
:param xaxis3d_type:
3D x 轴类型
:param xaxis3d_name:
x 轴... | be5a05ddbf5b0c4d0bb93eb6384c5a260905ca8e | 344,248 |
def arraytize(v):
"""
convenience function that "transforms" its arguments
into a list. If the argument is already a list, returns
it. If the argument is None, returns an empty list.
Otherwise returns [argument].
"""
if v is None:
return []
try :
return list(v)
excep... | 38810e67cc10a14fe27dc4991d736b8a77fe6843 | 662,843 |
def base64(value):
"""The intrinsic function Fn::Base64 returns the Base64 representation of \
the input string.
This function is typically used to pass encoded data to
Amazon EC2 instances by way of the UserData property.
Args:
value: The string value you want to convert to Base64
Re... | 6d8a69e5160a57f8b50085434643e6f738cfbc17 | 303,031 |
def _get_all_policy_ids(zap_helper):
"""Get all policy IDs."""
policies = zap_helper.zap.ascan.policies()
return [p['id'] for p in policies] | bd48cb1e5021f09c68286fdd83b6367f518290a3 | 186,667 |
import typing
def no_parsing(f: typing.Callable):
"""Wrap a method under test so that it skips input parsing."""
return lambda *args, **kwargs: f(*args, _parse=False, **kwargs) | ca2222ef87f25dda6beb2a9af12dbd5e2f76ea01 | 76,229 |
import torch
def calculate_accuracy(inputs: torch.Tensor, targets: torch.Tensor) -> float:
"""
A function that calculates accuracy for batch processing.
Returns accuracy as a Python float.
Args:
inputs (torch.Tensor): shape == [N, n_class]
targets (torch.Tensor): shape == [N]
Ret... | 53eff3901d675370a9ed0f260d1230c2c5badb79 | 34,064 |
from typing import Sequence
from typing import Callable
from typing import Any
from functools import reduce
def compose(functions: Sequence[Callable]) -> Callable:
"""
Compose a sequence of functions
:param functions: sequence of functions
:return: combined functions, e.g. [f(x), g(x)] -> g(f(x))
... | 9b3bcda936c55f53b9891fd1b522742c76a87097 | 583,795 |
def base36decode(base36_string):
"""Converts base36 string into integer."""
return int(base36_string, 36) | 66da9d391705cd0748e0e7c0ea5c69be2366ed4e | 22,527 |
from typing import Optional
def int_input(prompt: str) -> Optional[int]:
"""Print ``prompt``; ensure the user enters an integer and return it."""
text = input(prompt + " ")
if not text:
return None
try:
number = int(text)
except ValueError:
print("No, you must type an integ... | dff7cef9fae05d5e47d3d8aa6ec7b75d469abaa1 | 517,455 |
def _small_body(close, low, open, high):
"""
do we have a small body in relation to the wicks
:param close:
:param low:
:param open:
:param high:
:return:
0 if no
1 if yes (wicks are longer than body)
"""
size = abs(close - open)
if close > open:
top_wick = ... | c466fd7ebba16f62cc957d3ec43143d8d4a13500 | 322,418 |
def separate_units_by_type(all_units):
"""Separate all_units to their respective unit type group."""
immune = {}
infection = {}
unit_type_to_group = {
'immune': immune,
'infection': infection,
}
for unit_no, unit in all_units.items():
group = unit_type_to_group[unit['type... | 197bce89abeb2f7472e2c2c2b28115d491af718f | 166,752 |
def stringToList(inputString):
"""Convert a string into a list of integers."""
return [ord(i) for i in inputString] | 127e6b5d0e63d81dafa6c139d39517b1fd0c961e | 312,939 |
from typing import Dict
from typing import Any
def _sort_contact_models(contact_models: Dict[str, Any]) -> Dict[str, Any]:
"""Sort the contact_models.
First we have non recurrent, then recurrent contacts models. Within each group
the models are sorted alphabetically.
Args:
contact_models (Di... | cc1c350726c9dbcf8bbdb9cf36561c72ffc767d5 | 430,700 |
def _get_pgh_obj_pk_col(history_model):
"""
Returns the column name of the PK field tracked by the history model
"""
return history_model._meta.get_field(
'pgh_obj'
).related_model._meta.pk.column | 8ff914a7c0142973b58b48b22d9c7429682d69de | 635,662 |
def zaid2za(zaid):
"""
Convert ZZAAA to (Z,A) tuple.
"""
# Ignores decimal and stuff after decimal.
zaid = str(int(zaid))
Z = int(zaid[:-3])
A = int(zaid[-3:])
return (Z, A) | 58643b9d7adb3c0756e4de5d51c5a7d68a7a65e0 | 654,558 |
def merge_dicts(*args):
"""Merge multiple dictionaries into a new dictionary as a shallow copy."""
merged_dict = {}
for input_dict in args:
merged_dict.update(input_dict)
return merged_dict | 4328c53d4b1c8b423465d3e69a4df6253869ec0e | 586,806 |
import torch
def coin_flip(prob):
"""
Return the outcome of a biased coin flip.
Args:
prob: the probability of True.
Returns: bool
"""
return prob > 0 and torch.rand(1).item() < prob | 672929fb49a0e65101a4bdfdd13e981ae5eae31c | 6,858 |
def success_email_subject_msid_author(identity, msid, author):
"""email subject for a success email with msid and author values"""
return u"{identity}JATS posted for article {msid:0>5}, author {author}".format(
identity=identity, msid=str(msid), author=author
) | c65f946e87140c9c28166daa0e664a994910b559 | 685,874 |
import re
def get_r_filename(r_file):
"""Remove the file extension from an r_file using regex. Probably unnecessary
but improves readability
Parameters
----------
r_file : string
name of R file including file extension
Returns
-------
string
name of R file without f... | bde15f79afcc585d0f86b45debf932c22d22f271 | 631,392 |
def value_in_many_any(a, b):
"""return true if item 'a' is found
inside 'b': a list/tuple of many iterators
else return false
"""
for c in b:
if a in c:
return True
return False | 0da7a168a023d6af72369a19180f6bdbdc237d42 | 550,078 |
def totient(lim):
"""Computes Euler's totient for values up to lim included."""
# http://en.wikipedia.org/wiki/Euler%27s_totient_function
tot = list(range(lim + 1))
tot[0] = -1
for i in range(2, lim + 1):
if tot[i] == i:
for j in range(i, lim + 1, i):
tot[j] = (to... | ad42fb6bce3233becd56b22d30b966a5e2487a00 | 174,615 |
def get_format(s):
"""
Returns the Open Babel format of the given string.
Note: It is primitive, distinguishes only xyz, smiles, inchi formats.
>>> print(get_format('C'))
smi
>>> print(get_format('InChI=1S/H2O/h1H2'))
inchi
>>> print(get_format(get_xyz('C')))
xyz
"""
frm = 'u... | e3ec8c253b4cf14dd2401d4be710a6a596f6116b | 627,820 |
def tableName(table):
"""Return a string with the name of the table in the current db."""
return table.sqlmeta.table | 23ea66b80c8516d191d5f66e9d647d296480e0ab | 566,859 |
def crc32_tab_rev(prev, crctab, byte):
"""
return next = crc32(prev, byte)
crc32(p0,b0) ^ crc32(p1,b1) = crc32(p0^p1, b0^b1)
"""
return crctab[(prev^byte)&0xff] ^ (prev>>8) | d3adb5585d640cf3452441ccee721f2be9c1f89b | 239,077 |
def itemKey(item):
"""
Build the form item's key from the the item's name and the name of all
ancestors.
"""
parts = [item.name]
parent = item.itemParent
while parent is not None:
parts.append(parent.name)
parent = parent.itemParent
parts.reverse()
return '.'.join(par... | 29d317d62960594562577adc06b0db5279a7fbb6 | 52,636 |
import time
def make_timestamp(precision=0):
"""
Returns timestamp string that represents the current time
Precision is number of decimal places to add after the seconds
"""
#Outputs time stamp in format YYYYMMDD_hhmmss
t = time.localtime()
d = time.time()%1 #decimal places. May be needed later
YYYY = str(t.t... | 701350d7ff57eb1203738cabce9da4ffb5e051fd | 426,994 |
import collections
import math
def one_unit_tdelta(tdelta):
"""Return the timedelta as a string with 1 unit of time.
Args:
tdelta (timedelta): A non-negative timedelta object.
Returns:
A string in the form #.#x or ##x where x is a time unit.
The units ar... | 3d7eae6b46a4c717ccc7cadc46f7147301a23bf3 | 446,249 |
def recvall(sock, n):
"""
returns the data from a recieved bytestream, helper function
to receive n bytes or return None if EOF is hit
:param sock: socket
:param n: length in bytes (number of bytes)
:return: message
"""
#
data = b''
while len(data) < n:
print("Start func... | 5641e7f13c4e61f9cdf672dc524c397f415e8a62 | 613,763 |
def compute_derivs_matrices(vecs, adv_vecs, dt):
"""Computes 1st-order time derivatives of vectors from data in matrices.
Args:
``vecs``: Matrix with vectors as columns.
``adv_vecs``: Matrix with time-advanced vectors as columns.
``dt``: Time step between ``vecs``... | c953b1a04e3faab5f4af75fdc5029fe62f0864f8 | 227,999 |
def is_wgs_accession_format(contig_accession):
"""
Check if a Genbank contig is part of WGS (Whole Genome Shotgun) sequence
:param contig_accession: Genbank contig accession (ex: CM003032.1)
:return: True if the provided contig is in the WGS format
"""
wgs_prefix = contig_accession[:4]
wgs... | 1e4ece9c428264ed5e74e8f83ad9b0521bc57988 | 43,879 |
import six
def GetDictItems(namedtuple_object):
"""A compatibility function to access the OrdereDict object from the given namedtuple object.
Args:
namedtuple_object: namedtuple object.
Returns:
collections.namedtuple.__dict__.items() when using python2.
collections.namedtuple._a... | 92fc7098065846f0393a82b06d11afca7038075f | 564,786 |
from typing import List
from typing import Any
def shard_list(alist: List[Any], shard_count: int) -> List[List[Any]]:
"""Breaks the list up into roughly-equally sized shards.
Args:
alist: A list of things.
shard_count (int): The total number of shards.
Returns:
List[List[Any]]: T... | d5f5188514db968882c154c0118dff64dbc174fa | 600,629 |
def createRefVal(pool_n=1):
"""Return a 2D list of reference distance and power"""
dist = [100, 200, 400]
power = [2000, 5000]
liste = []
for i in range(pool_n):
for p in power:
for d in dist:
liste.append([p, d])
return liste | 3e3c7588912118c10bb606cba63e7f27d4e2d3f6 | 239,762 |
import re
def valid_sexp_predicate(sexp):
"""
文字列中の"のエスケープ判定もしている。文字列リテラルを除外しsexp中に出現する()が全て関数コールとなるようにしてから、(と)の対応関係をチェックする。
Args:
sexp (str): euslispに送りつけるS式
Returns:
bool: S式が正しいかどうか
>>> valid_sexp_predicate("(+ 1 2)")
True
>>> valid_sexp_predicate('(princ "hoge")')... | a66e013ee2f0a8a6ec10fc7f4acd46c9046925ee | 205,344 |
from typing import Callable
from typing import Any
from typing import Iterable
def takewhile(predicate: Callable[[Any], bool], seq: Iterable) -> Iterable:
"""Lazily evaluated takewhile
:param predicate: First failure of predicate stops the iteration. Should return bool
:param seq: Sequence from which to ... | b068d79386e8a6694de969992c265dd1a39dc9a3 | 534,236 |
def EnumsConflict(a, b):
"""Returns true if the enums have different names (ignoring suffixes) and one
of them is a Chromium enum."""
if a == b:
return False
if b.endswith('_CHROMIUM'):
a, b = b, a
if not a.endswith('_CHROMIUM'):
return False
def removesuffix(string, suffix):
if not strin... | 33b096c4ffb1a620e83bbc19654d57218ab3e3be | 459,972 |
def jar_file_filter(file_name):
"""
A function that will filter .jar files for copy operation
:type file_name: str
:param file_name:
Name of the file that will be checked against if it ends with .jar or not
"""
return bool(file_name) and isinstance(file_name, str) and file_name.endswith... | b330cea355116acde9269006f9574cf194424c1a | 403,491 |
def _normalize_typos(typos, replacement_rules):
"""
Applies all character replacement rules to the typos and returns a new
dictionary of typos of all non-empty elements from normalized 'typos'.
"""
if len(replacement_rules) > 0:
typos_new = dict()
for key, values i... | fc47995303b00bc4d612a6a161dfad4c0bcd8e02 | 682,371 |
def unpack_ipv4_bytes(byte_pattern):
""" Given a list of raw bytes, parse out and return a list of IPs
:param byte_pattern: The raw bytes from the DHCP option containing
a list of IP addresses. The RFC specifies that an IP list will
be a list of octets, with each group of 4 octets representing
... | f2ba6dd77acef4871a8511663fc494f03320f1ba | 117,065 |
def exclude_pattern(f):
"""
Return whether f is in the exclude pattern.
Exclude the files that starts with . or ends with ~.
"""
return f.startswith(".") or f.endswith("~") | b81208d37fe53cbbeb973d4cd56cd11022458a3d | 249,781 |
def temp_coeff_cold(lower, upper):
"""
Calculates and returns the m and b coefficients for y = m*x + b
for a line intersecting (lower, 0) and (upper, 255).
"""
m = 255/(upper-lower)
b = 255 - m * upper
return m, b | 0391f2d918627f43577555056d09e9c9efe1d15d | 650,663 |
def get_scope(field):
"""For a single field get the scope variable
Return a tuple with name:scope pairs"""
name = field['name']
if 'scope' in field['field']:
scope = field['field']['scope']
else:
scope = ''
return (name, scope) | 1b931ec1a7c5a629fe6b39034c23fd02568ed5a7 | 30,724 |
def convert_pixels(image, **kwargs):
"""Converts an image containing pixel values in [0, 255] to [0, 1] floats."""
image = image.astype('float32')
image /= 255.0
return image | 426875f4dfe8abdfde234418b63373a6fe5e5ec3 | 339,817 |
def exists(list_elements, check_function):
"""exists
Check whether at least an element x of l is True for f(x)
:param list_elements: A list of elements to test
:param check_function: The checking function (takes one parameter and \
return a boolean)
"""
for element in list_elements:
... | 76553c72b733ce46d6b4db563ba79a5009559d2a | 322,075 |
def map_across_table(fn, rows):
"""
Returns:
(list of lists): A table expressed as a list of lists, having a applied
a function to each cell.
Args:
fn (function): a single argument function to apply to each cell
rows (list of lists): A table expressed as a li... | 32f421c8702da2378aaafbb50bbfeda3484a5473 | 381,033 |
from typing import Tuple
def seat_to_seat_id(seat: Tuple[int, int]) -> int:
"""Convert a row/column pair into a seat ID"""
return seat[0] * 8 + seat[1] | b78c45e73ff4c148927748e4aafd9e2c5489a2a5 | 179,472 |
def add_producer_function(new_producer_list, xml_producer_function_list, output_xml):
"""
Check if input list is not empty, write in xml for each element and return update list if some
updates has been made
Parameters:
new_producer_list ([Data_name_str, Function]) : Data's name and prod... | 7c30289d18d79fd6c2a7ee006f528bf5dd5d56a1 | 59,064 |
def display_timedelta(minutes):
"""Converts timedelta in minutes to human friendly format.
Parameters
----------
minutes: int
Returns
-------
string
The timedelta in 'x days y hours z minutes' format.
Raises
------
ValueError
If the timedelta is negative.
"... | c8606cf6defcc38e5a12dc88cfc65d7c21aefd69 | 674,099 |
def find_item(list_containing_list, item):
"""
Find the index of the list that contains the item
:param list_containing_list: List of lists; one of them must contain the item
:param item: The item we are looking for
:return: Index of the item in the outer list
>>> find_item([[1,2,3],[4,5,6]],5... | a7d9c3c745e8da8c83edb8941494188980250bf5 | 449,343 |
def get_cover_image(beatmapset_id: int):
"""Return url of cover image from beatmapset_id."""
return f"https://assets.ppy.sh/beatmaps/{beatmapset_id}/covers/cover.jpg" | 54f5bf96c8e2e5dd266d5c3d2a7ae64c48e85c99 | 124,752 |
def percentile(sorted_values, p):
"""Calculate the percentile using the nearest rank method.
>>> percentile([15, 20, 35, 40, 50], 50)
35
>>> percentile([15, 20, 35, 40, 50], 40)
20
>>> percentile([], 90)
Traceback (most recent call last):
...
ValueError: Too few data points (0... | a2704a59629b4634fb1f651fdbbfd9b19209816e | 591,095 |
def remove_degenerate_bboxes(boxes, dim0: int, dim1: int, min_boxside=0):
"""Remove bboxes beyond image or smaller than a minimum size.
This assumes a format where columns 0, 1 are min dim0 and dim1, while
columns 2, 3 are max dim0 and dim1, respectively.
"""
# adjust boxes
boxes[boxes < 0] = 0
... | 9f15955fc52fd679af9ec55215111177876553ed | 457,260 |
def is_unit_str(ustr):
"""Check if a string defines a unit"""
ustr = ustr.strip()
if(len(ustr)>=2 and ustr[0]=="[" and ustr[-1]=="]"):
return True
else:
return False | 60a28058d1fb35d5a8f69e76dc416099b4e9718a | 252,888 |
import math
def ols(dist, scale, kappa):
"""Calculate OLS based on distance, scale and kappa."""
e = dist ** 2 / 2 / (scale ** 2 * kappa)
return math.exp(-e) | 666eb6ba1b4ca4b93953930ebf049d8a76771f51 | 599,464 |
def add_score(score, pts, level):
"""Add points to score, determine and return new score and level."""
lvl_multiplier = 10
score += pts
if (score % (pts * lvl_multiplier)) == 0:
level += 1
return score, level | e65f9cf989068513131610e80b12821976a6546e | 362,840 |
def get_parameter_list_from_request(req,parameter):
"""Extracts a parameter from the request.
Parameters
----------
req : HttpRequest
The HTTP request.
parameter : str
The parameter being extracted.
Returns
-------
List
List of comma separated parameters.
"... | 4b9f66e64455229a998eb199a3fc890d2ba8ba02 | 648,745 |
import inspect
def func_source_data(func):
"""
Return data about a function source, including file name,
line number, and source code.
Parameters
----------
func : object
May be anything support by the inspect module, such as a function,
method, or class.
Returns
----... | 0f8c16365979505bac0304bd05435c8f700ce800 | 382,864 |
import io
def as_csv(df):
"""Returns an in-memory csv of Pandas.DataFrame"""
f = io.StringIO()
df.to_csv(f, index=False)
f.seek(0)
return f | 4401bb096c6c0f6c578ac45d8210c7b21ff86d2e | 336,880 |
from typing import Union
def next_page(page: str) -> Union[None, str]:
""" Given the contents of a Letterboxd page, returns
the relative path to the next page to parse. It
handles the pagination of any type of page,
from followers, to following, to movies watched,
etc.
Ret... | f0d041548a91553a512907f951813912a50f6182 | 73,915 |
def calc_conformance(results):
"""Returns a tuple with the number of total and failed testcase variations and the conformance as percentage."""
total = len(results)
failed = sum(1 for status, _ in results.values() if status != 'PASS')
conformance = (total-failed)*100/total if total > 0 else 100
retu... | 91d7406a9d8dc34505e16237b2f66cb2b2e6c65e | 519,951 |
def tf(term, document):
"""
computes term frequency. TF is defined as how often the term in question
appears in a document over the sum of all terms in the document:
(term/all_terms_in_doc).
Parameters:
term: a string containing the search term
document: a list representing the docu... | 033c65fb1165431a63ce5d758f65dc6a1d40346d | 514,927 |
def valid_op_json(op_json):
"""Asserts object is in the form of `[command, {payload}]`."""
assert isinstance(op_json, list), 'json must be a list'
assert len(op_json) == 2, 'json must be a list with 2 elements'
assert isinstance(op_json[0], str), 'json[0] must be a str (command)'
assert isinstance(o... | 820e4554bd559aff114fa90aa32da7b362416523 | 494,382 |
import base64
def release_asset_node_id_to_asset_id(node_id: str) -> str:
"""
Extracts and returns the asset id from the given Release Asset |node_id|.
The "id" returned from the GraphQL v4 API is called the "node_id" in the REST API v3.
We can get back to the REST "id" by decoding the "node_id" (it ... | 5046f9aa6740e2de8254db83de31f308b4becc31 | 237,078 |
def nthword(n, sep=None):
"""
Construct a function to return the nth word in a string. E.g.::
>>> import petl as etl
>>> s = 'foo bar'
>>> f = etl.nthword(0)
>>> f(s)
'foo'
>>> g = etl.nthword(1)
>>> g(s)
'bar'
Intended for use with :func:`pe... | 025957c032db349dadc0eed876fc2b44e966a5fc | 289,804 |
import random
def do_random_test(negexp=3):
"""
Return :obj:`True` if a random probability is not greater than a threshold,
otherwise return :obj:`False`.
:type negexp: :obj:`int` (non-negative)
:arg negexp: This is the negative exponent that is used to compute a
probability threshold. Hi... | 8f7549a3a781abb728b1ac9b1289bee8bacaf352 | 222,581 |
import torch
def compute_var_and_mean_sq(lst):
"""Compute variance and mean square of a list of samples."""
num_samples = len(lst)
tensor = torch.stack(lst)
mean = torch.mean(tensor, 0, keepdim=True)
# estimate variance
var = (tensor - mean).pow(2).sum(0) / (num_samples - 1)
# estimate E[x^2]. cannot es... | 69869040485a9162ec68b62ed4e3a1e0ab2e1170 | 435,576 |
from typing import Dict
from typing import List
import csv
def read_temp_csv_data(filepath_temp: str) -> Dict[str, Dict[int, List[float]]]:
"""Return a mapping from the state to a mapping of the year to yearly data in the format
[average temperature, precipitation, wildfire counts].
Currently, the values... | 4342eb9b1ed392cd8ee94dfa400a194948fef3a4 | 663,909 |
import numbers
def discrete_signal(signal, step_size):
"""Discretize signal
Parameters
----------
signal: pd.Series
Signals for betting size ranged [-1, 1]
step_size: float
Discrete size
Returns
-------
pd.Series
"""
if isinstance(signal, numbers.Numbe... | 183af7cf6ca30daaebb44b0d41c860b001109136 | 70,887 |
def org_default_payload(login: str, uid: int):
"""Provide the basic structure for an organization payload."""
return {
'action': 'member_added',
'membership': {
'url': '',
'state': 'pending',
'role': 'member',
'organization_url': '',
'u... | af542f5a9da78ff3b08e80cd49e84f828e9a8cbb | 628,702 |
def grab_job_links(soup):
"""
Grab all non-sponsored job posting links from a Indeed search result page using the given soup object
Parameters:
soup: the soup object corresponding to a search result page
e.g. https://ca.indeed.com/jobs?q=data+scientist&l=Toronto&start=20
... | 159b3d768266900446bc17326aeb41bfd048b267 | 666,852 |
def _shorten_decimals(self, fValue : float, iDecimals : int) -> float:
"""
Description
-----------
Method for reducing the amount of decimals of a float value
Parameters
----------
`fValue` : float
Value whose decimals should be cut
`iDecimals` : int
Amount of decimals
... | 74746e2ab4eca01149feeea6603fabf9cd42b795 | 512,673 |
import inspect
def arg_names(receiver):
"""
Get the expected keyword arguments for a function or class constructor.
"""
return list(inspect.signature(receiver).parameters.keys()) | 3267c2cb36ee54ff99f972a7a0e1e8ba90393bef | 654,771 |
def generate_graph(edges):
""" Generate a graph as dict using edges generator.
Args:
edges (generator): generator of edges.
Returns:
dict. the graph as
{
"node": ["dependencies"],
...
}
"""
grap... | 782c89041005207b8187eb1a9dfac836c50ae484 | 514,758 |
def load_glove_vocab(filename):
"""Loads GloVe's vocab from a file.
Args:
filename (str): path to the glove vectors.
Returns:
set: a set of all words in GloVe.
"""
print('Building vocab...')
with open(filename) as f:
vocab = {line.strip().split()[0] for line in f}
p... | ad36dffb75dec1bb44108de8de2b4ecbd9d066dd | 31,147 |
import logging
def Handle(
logger,
handler=logging.NullHandler(),
formatter="%(asctime)s %(name)s - %(levelname)s: %(message)s",
level="WARNING",
):
"""
Handle a logger with a standardised formatting.
Parameters
-----------
logger : :class:`logging.Logger` | :class:`str`
L... | 0b2f2c7e29f3702c4154ca29f457bbba2f0677e4 | 693,631 |
def get_forwards_dict(forwards_file):
"""Returns a dictionary with new ott_ids for forwarded ott_ids
"""
fwd_dict = {}
fi=open(forwards_file)
header = fi.readline()
for lin in fi:
lii = lin.split()
fwd_dict[int(lii[0])]=lii[1]
return fwd_dict | 1f0e50aac045e8c9ce2fdee500abc46e1d04d2dc | 314,777 |
def find_episode(episode_id, seasons):
"""
Return metadata for a specific episode from within a nested
metadata dict.
Returns an empty dict if the episode could not be found.
"""
for season in seasons:
for episode in season['episodes']:
if str(episode['id']) == episode_id:
... | 64255ca8e330c3b45768704644ac8bfddbfc1416 | 30,862 |
def makeOfficialGlyphOrder(font, glyphOrder=None):
"""Make the final glyph order for 'font'.
If glyphOrder is None, try getting the font.glyphOrder list.
If not explicit glyphOrder is defined, sort glyphs alphabetically.
If ".notdef" glyph is present in the font, force this to always be
the first ... | 95c3cf0096a8390a10c90092aa6b96636c061c5c | 368,365 |
from typing import Tuple
from typing import Union
def central_pixel_coordinates_2d_from(
shape_native: Tuple[int, int]
) -> Union[Tuple[float], Tuple[float, float]]:
"""
Returns the central pixel coordinates of a data structure of any dimension (e.g. in 1D a `Line`, 2D an `Array2D`,
2d a `Frame2D`, et... | 1b9acfcf7751c6f6224200f68fd06154940a10d5 | 477,814 |
from typing import Mapping
from typing import Tuple
from typing import Optional
from typing import Sequence
def _get_sorted_experiment_kinds(
experiment_kinds: Mapping[str, Tuple[str]],
distance_kinds_order: Optional[Sequence[str]],
) -> Mapping[str, Tuple[str]]:
"""Sorts experiment_kinds` in order of `di... | 910c401c229f4716132a66984a87cee976788534 | 328,778 |
def __row_helper(title, value, unit=None, seperator=None):
"""Helps format package information in a standardized way.
Args:
title (str): The title of the value. Left aligned.
value (any): The value to be displayed.
unit (str, optional): The value's unit.
seperator (str): The sep... | b5ff037b913c7a0f26c135d4f9fcdeb015b05ba4 | 394,875 |
import json
def _parse_request_body(request):
"""Parse the JSON from the request body, or return its bytes"""
body = request.body
if not body:
return b''
if request.headers.get("Content-Type") == "application/json":
if isinstance(body, bytes):
return json.loads(body.decode... | d6faaa7b18474e66679260bca9fc9e00db22fcec | 192,656 |
def is_title(result):
"""Returns true if the result is a title match."""
for metadatum in result.metadata:
if metadatum.name == "is_title":
return metadatum.value == "true"
return False | 2760a421755947126c89b7b0d193c6d4d3a56579 | 334,816 |
def get_input_path_contents(path):
"""Get the contents of a file."""
if not path.is_file():
error = "Unable to find file: '{p}'"
error = error.format(p=path)
raise Exception(error)
contents = path.read_text()
return contents | f94aca95afd56a454399becab1c61ab9dd646a1e | 154,311 |
def clean_cluster_seq_id(id):
"""Returns a cleaned cd-hit sequence id
The cluster file has sequence ids in the form of:
>some_id...
"""
return id[1:-3] | b690d7472b1fb90743be27fed9ce9ef7c3b06694 | 687,241 |
def _get_r2(y, y_hat):
"""Calculate the goodness of fit.
Arguments:
y {ndarray} -- 1d array object with int.
y_hat {ndarray} -- 1d array object with int.
Returns:
float
"""
m = y.shape[0]
n = y_hat.shape[0]
assert m == n, "Lengths of two arrays do not match!"
a... | e40dc26e016ec319b8658685fd00dbc3bdd63ffe | 142,530 |
def replace_variant(ref, variant, start, stop=None):
"""Take a string, ref. Insert a string variant that replaces the bases
in ref from start to stop, inclusive.
start and stop are 0-based Pythonic coordinates
if stop is None, the variant will simply be inserted before the start base
"""
if stop... | 4a449b2cf51cf4e996519cc441345cd98ced97ef | 298,269 |
def del_none_col(df,threshold = 0.5):
"""delete those columns whose none values number is bigger than threshold."""
l = []
for each in df:
if df[each].isnull().sum()/len(df[each]) > threshold:
l.append(each)
df.drop(l,axis = 1,inplace = True)
if len(l) > 0:
print(f"Delete... | e7c7301366bafded460ef39b669f1b01e743b59f | 461,215 |
def _make_params_string(params):
"""Convert list of parameters to string"""
p_str = '&'.join(['{}={}'.format(f, params[f]) for f in params.keys()])
return p_str | db94c55492d506a364b6064bda3638995caf546f | 72,185 |
def make_default_headers(n):
"""
Make a set of simple, default headers for files that are missing them.
"""
return [u'column%i' % (i + 1) for i in range(n)] | c82045035f989cd4d462f901af36529816387bd9 | 441,584 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.