content stringlengths 35 416k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
import os
def certificate(cert_name):
"""Return the path to the PEM file with the given name."""
return os.path.join(os.path.dirname(__file__), 'lib', cert_name) | 5dc02c85158ae7b020f069976a581d41f31d338c | 1,772 |
def boolToYes(b):
"""Convert a Boolean input into 'yes' or 'no'
Args:
b (bool): The Boolean value to be converted
Returns:
str: 'yes' if b is True, and 'no' otherwise.
"""
if b:
return "yes"
else:
return "no" | ff94b66b5a166592062bf1d5b286b425e7997304 | 1,773 |
def vint_mask_for_length(length):
"""
Returns the bitmask for the first byte of a variable-length integer (used for element ID and size descriptors).
:arg length: the length of the variable-length integer
:type length: int
:returns: the bitmask for the first byte of the variable-length integer
:rtype: int
... | 92fe3cb0fa09713ff4b650349294a2b241bb3918 | 1,774 |
from itertools import tee
def parse(tokens):
"""
S-expr ::= ( S-expr* ) | AtomSymbol | ' S-expr
' S-expr = (quote S-expr)
"""
def _parse(tokens):
while True:
token = next(tokens)
if token == "(":
s_expr = []
while True:
... | 90c8e3cd8482899749d30d5344390cfd5f24989f | 1,775 |
def find_children(node, tag, xml_ns, ns_key):
"""
Finds the collection of children nodes
Parameters
----------
node : ElementTree.Element
tag : str
xml_ns : None|dict
ns_key : None|str
"""
if xml_ns is None:
return node.findall(tag)
elif ns_key is None:
retu... | b51d9f588661c3f609dc53adaa328f974e17d5fb | 1,776 |
import re
def normalize_string(string, ignore_spaces, ignore_punctuation):
"""Normalizes strings to prepare them for crashing comparison."""
string = string.upper()
if ignore_punctuation:
string = re.sub(r"[^1-9a-z \n\r\t]", "", string, flags=re.I)
if ignore_spaces:
string = re.sub(r"\... | 31de2b9644eb0943470430c6c3f2ea8a94dfb3cf | 1,777 |
def reduce_expr(expr):
"""
Reduces a boolean algebraic expression based on the identity X + XY = X
Args:
expr (str): representation of the boolean algebraic expression
Returns:
A string representing the reduced algebraic expression
"""
reduced = True
... | 2c278e6ea6f133c51c5e98796f288f366fd10cb3 | 1,778 |
def createVskDataDict(labels,data):
"""Creates a dictionary of vsk file values from labels and data.
Parameters
----------
labels : array
List of label names for vsk file values.
data : array
List of subject measurement values corresponding to the label
names in `labels`.
Returns
-------... | a4669e4a173aaeef534d13faceaeab869eb62cb3 | 1,779 |
def h_eval(data):
"""
Function takes dictionary
Evaluate values and convert string to correct type (boolean/int/float/long/string)
"""
if isinstance(data, dict):
for _k in list(data.keys()):
data[_k] = h_eval(data[_k])
if data[_k] is None or (isinstance(data[_k], dic... | 28a3529283719cab321c712a9e8723d5ff314ef8 | 1,782 |
import math
def fmt_bytes(size_bytes):
"""Return a nice 'total_size' string with Gb, Mb, Kb, and Byte ranges"""
units = ["Bytes", "KB", "MB", "GB"]
if size_bytes == 0:
return f"{0} Bytes"
for unit in units:
digits = int(math.log10(size_bytes)) + 1
if digits < 4:
ret... | 40613403092bdc9d8dca8b0b487d5af6c887b075 | 1,783 |
def grid_reference_to_northing_easting(grid_reference):
"""
Needs to include reference
:param grid_reference:
:return:
"""
grid_reference = grid_reference.strip().replace(' ', '')
if len(grid_reference) == 0 or len(grid_reference) % 2 == 1 or len(grid_reference) > 12:
return None, No... | 3da96e36f9be1e369d0425f2b9c34e432eb5ca77 | 1,785 |
from typing import Dict
def _remove_attribute(note_dict: Dict, attribute: str) -> Dict:
""" Create a copy of the note where a single attribute is removed """
d = dict(note_dict)
d[attribute] = None
return d | d2659b887c1a2a7c67f6785889db2aa2039f9627 | 1,786 |
def write_site_pair_score_data_to_file(sorted_data_list, output_file_path, algorithm_used, max_iterations=None, num_threads=None):
"""Since site indices are starting from zero within python we add one to
each of them when they are being written to output file.
"""
formater = '#' + '='*100
formater +... | 5ee4ec97fbc1b6f86e36946ee6d925604858b063 | 1,789 |
import os
def check_icon_arg(src, default):
"""
Checks if icon arguments are valid: either a URL or an absolute path.
:param src: Source of the icon
:param default: default value of the icon
:return: src (possibly pre-pended with "file://")
"""
if src != default:
# check if URl
... | 33db8c760df0bdf5e26474cdc281e4d6504cca27 | 1,790 |
def only_half_radius(
subsampled_radius: float, full_diameter: float, radius_constraint: float
):
"""
Check if radius is smaller than fraction of full radius.
"""
assert 0.0 <= radius_constraint <= 1.0
return subsampled_radius <= ((full_diameter / 2) * radius_constraint) | 565c301932d5445e8bbb594085e65df63814663a | 1,793 |
def sanitise_description(original: str) -> str:
"""
Remove newlines from ticket descriptions.
:param original: the string to sanitise
:return: the same string, with newlines as spaces
"""
return original.replace("\n", " ") | 741aa7df758fb342a0d9a0fa182d24a643f5dbbc | 1,794 |
def mockselect(r, w, x, timeout=0): # pylint: disable=W0613
"""Simple mock for select()
"""
readable = [s for s in r if s.ready_for_read]
return readable, w[:], [] | 920810aea2f7813885805011d646ddaabfd1901c | 1,795 |
from typing import List
def merge(input_list: List, low: int, mid: int, high: int) -> List:
"""
sorting left-half and right-half individually
then merging them into result
"""
result = []
left, right = input_list[low:mid], input_list[mid : high + 1]
while left and right:
result.app... | 0d53b0670899b4853563c9dda0eb47a8c66bae00 | 1,796 |
def splitmod(n, k):
"""
Split n into k lists containing the elements of n in positions i (mod k).
Return the heads of the lists and the tails.
"""
heads = [None]*k
tails = [None]*k
i = 0
while n is not None:
if heads[i] is None:
heads[i] = n
if tails[i] is not... | a4a1885ce0c9541c534145d0236996a511cbdd00 | 1,797 |
import os
def dirname_to_prefix(dirname):
"""Return filename prefix from dirname"""
return os.path.basename(dirname.strip('/')).split("-", maxsplit=1)[1] | 8e1124e66669dd051987c55812052d36c78bdc4a | 1,798 |
def binary_accuracy(a,b):
"""
Calculate the binary acc.
"""
return ((a.argmax(dim=1) == b).sum().item()) / a.size(0) | 5f9b09199b2e88169a0cbe9ee7cb4bb351c09e4a | 1,799 |
def _generate_var_name(prefix, field_name):
"""
Generate the environment variable name, given a prefix
and the configuration field name.
Examples:
>>> _generate_var_name("", "some_var")
"SOME_VAR"
>>> _generate_var_name("my_app", "some_var")
"MY_APP_SOME_VAR"
:param prefix: the pr... | 9065d1deb76789582e68df779ec2c961a7d4aedc | 1,800 |
def num_decodings2(enc_mes):
"""
:type s: str
:rtype: int
"""
if not enc_mes or enc_mes.startswith('0'):
return 0
stack = [1, 1]
for i in range(1, len(enc_mes)):
if enc_mes[i] == '0':
if enc_mes[i-1] == '0' or enc_mes[i-1] > '2':
# only '10', '20' ... | ae4ff7181e34003dcc7ec264ed2727bc716708a5 | 1,801 |
import decimal
def truncate_decimal_places(value: decimal.Decimal, places: int = 1) -> float:
"""
Truncate a float (i.e round towards zero) to a given number of decimal places.
NB: Takes a decimal but returns a float!
>>> truncate_decimal_places(12.364, 1)
12.3
>>> round_decimal_places(-12.... | 11b924a5e4f6560674b1f7378f6a4001a3265a97 | 1,803 |
import pathlib
import sys
def get_resource_path(relative_path):
"""
relative_path = "data/beach.jpg"
relative_path = pathlib.Path("data") / "beach.jpg"
relative_path = os.path.join("data", "beach.jpg")
"""
rel_path = pathlib.Path(relative_path)
dev_base_path = pathlib.Path(__file__).resolv... | becad13eb95d988b49ea7ef141e9c3436379af6e | 1,804 |
def flip_dict(d):
"""Returns a dict with values and keys reversed.
Args:
d: The dict to flip the values and keys of.
Returns:
A dict whose keys are the values of the original dict, and whose values
are the corresponding keys.
"""
return {v: k for k, v in d.items()} | c9c960209663639613739979c0dc4066a63c44cb | 1,805 |
import subprocess
def branch_exists(branch: str) -> bool:
""" Check if the branch exists in the current Git repo. """
try:
subprocess.check_call(
["git", "rev-parse", "--quiet", "--verify", branch],
stdout=subprocess.DEVNULL,
)
return True
except subprocess.... | 352adba56d824fff29bf5c91788a1154bea64f1b | 1,806 |
def has_sample(args):
"""Returns if some kind of sample id is given in args.
"""
return args.sample or args.samples or args.sample_tag | c2ae87acb11232d7f56cb9e09eb8509720669058 | 1,807 |
import numpy
import math
def pearsonr(a0, a1):
"""Pearson r, product-moment correlation coefficient, of two samples.
Covariance divided by product of standard deviations.
https://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient#For_a_sample
"""
n = len(a0)
assert n == len... | 64135ebc840bb1673ece1aec24f22c960f89af20 | 1,809 |
def __column(matrix, i):
"""Returns columns from a bidimensional Python list (a list of lists)"""
return [row[i] for row in matrix] | f455245eb8bbda90f185479afc85eecfb481c70c | 1,810 |
import copy
import os
def write_stats_file(run_output_dict):
"""Writes a dummy PolyChord format .stats file for tests functions for
processing stats files. This is written to:
base_dir/file_root.stats
Also returns the data in the file as a dict for comparison.
Parameters
----------
run_... | 5a3d7b81d8315fd39d5348f9140a001b020c7584 | 1,811 |
import json
def dict_serialize(seqlen_dist_dict):
"""
dict->str
Turns {1:'a',2:'b'}->"[[1,'a'],[2,'b']]"
Why? Because this format plays nice with shell script that runs xlmr_bench.
Avoids curly braces and spaces that makes shell script str input unhappy.
"""
seqlen_dist_lst = list(seqlen... | a61c51debff922d128fbb26bbe2121063511d4c4 | 1,812 |
def _path_list_creator(path, file_prefix_name, number_of_digits_zfill, file_suffix_name):
"""Creates a list of paths where the files have a predefined prefix,
an incremental number and a predefined suffix on their name,
respectively. Eg.: img01.zdf
Args:
path: a path that leads to the f... | 4850edbbf544284b0736ee52188bd53119c50fdf | 1,813 |
import re
def tamper(payload, **kwargs):
"""
Replaces instances of UNION with -.1UNION
Requirement:
* MySQL
Notes:
* Reference: https://raw.githubusercontent.com/y0unge/Notes/master/SQL%20Injection%20WAF%20Bypassing%20shortcut.pdf
>>> tamper('1 UNION ALL SELECT')
'1-.1UNION ... | cbf4fc5b81bc7760aafe6cf65fa498945285e5bb | 1,814 |
def get_source_tokens_tensor(src_tokens):
"""
To enable integration with PyText, src_tokens should be able to support
more features than just token embeddings. Hence when dictionary features are
passed from PyText it will be passed as a tuple
(token_embeddings, dict_feat, ..). Thus, in this case whe... | cf20ceeba82c595dc62b267794ca758360e0386b | 1,815 |
def merge_config_and_args(config, args):
"""
Creates a configuration dictionary based upon command line arguments.
Parameters
----------
config : dict
configurations loaded from the config file
args : object
arguments and there values which could be \
passed in the c... | 3935cfc525fb99b9513a608ef0e5e8fd7de708f3 | 1,816 |
import random
import string
def get_random_string(length: int) -> str:
"""
With combination of lower and upper case
"""
return ''.join(random.choice(string.ascii_letters) for i in range(length)) | b9d0c760e92603a4fe1f625615b96a1c2265f22a | 1,817 |
from typing import Union
from pathlib import Path
from typing import List
from typing import Dict
import json
def readJSONLFile(file_name: Union[str, Path]) -> List[Dict]:
"""
Read a '.jsonl' file and create a list of dicts
Args:
file_name: `Union[str,Path]`
The file to open
Return... | 8e33fad766a255578179828dc76ec793c02f90b9 | 1,818 |
def makeSiteWhitelist(jsonName, siteList):
"""
Provided a template json file name and the site white list from
the command line options; return the correct site white list based
on some silly rules
"""
if 'LHE_PFN' in jsonName:
siteList = ["T1_US_FNAL"]
print("Overwritting SiteWh... | 8f8b11739a30b4338b8dd31afb6c3c57545af6d0 | 1,819 |
from typing import List
def max_crossing_sum(lst: List[int], mid: int, n: int) -> int:
"""
Parameter <mid> is the floor middle index of <lst>.
Parameter <n> is the length of the input list <lst>.
Pre: <lst> is a list of integers and len(lst) >= 2.
Post: returns the maximum contiguous crossing sum ... | 3d873907cb7ed0c14152ec3c2e92a742bd52aa85 | 1,820 |
import argparse
def parse_arguments():
"""
Parse the argument list and return the location of a geometry file, the
location of a data file, whether or not to save images with a timestamp of
the four default plot windows and the VisIt session file in the current
directory, and whether or not to ope... | 5ba0e6e65801cfc93cc2864368eb2fac4b75e840 | 1,821 |
def get_duration(df):
"""Get duration of ECG recording
Args:
df (DataFrame): DataFrame with time/voltage data
Returns:
float: duration of ECG recording
"""
start = df.time.iloc[0]
end = df.time.iloc[-1]
duration = end - start
return duration | 77698afc8ef7af557628d5fea760dc101c3e6112 | 1,823 |
import click
import subprocess
def launch(cmd, args=None, separate_terminal=False, in_color='cyan', silent=False, should_wait=True):
"""
Launch a system command
:param cmd: The command to run
:param args: The arguments to pass to that command (a str list)
:param separate_terminal: Should we open a... | 48de0ef8b80973fede05444ec78ab09de6b783b9 | 1,824 |
def calc_triangular_number(n: int):
"""
A triangular number or triangle number counts objects
arranged in an equilateral triangle.
More info: https://www.mathsisfun.com/algebra/triangular-numbers.html
:param n:
:return:
"""
return int((n * (n + 1)) / 2) | e3bfefd6e0e9451849cee8f6da252ec128285c85 | 1,826 |
def find_ccs(unmerged):
"""
Find connected components of a list of sets.
E.g.
x = [{'a','b'}, {'a','c'}, {'d'}]
find_cc(x)
[{'a','b','c'}, {'d'}]
"""
merged = set()
while unmerged:
elem = unmerged.pop()
shares_elements = False
for s in merged.copy... | 4bff4cc32237dacac7737ff509b4a68143a03914 | 1,827 |
import os
def checkLastJob(jobsFolder):
"""Count number of folders in folder
:param jobsFolder: directory with jobs
:return: number of created jobs
"""
allFolders = os.listdir(jobsFolder)
jobsFolders = [f for f in allFolders if f.startswith('job')]
jobsCount = len(jobsFolders)
ret... | 17ea83ffc07134d91d66a08ee59ed85b499c8e4d | 1,828 |
def gen_image_name(reference: str) -> str:
"""
Generate the image name as a signing input, based on the docker reference.
Args:
reference: Docker reference for the signed content,
e.g. registry.redhat.io/redhat/community-operator-index:v4.9
"""
no_tag = reference.split(":")[0]
... | ccaecfe91b5b16a85e3a3c87b83bbc91e54080b1 | 1,829 |
def get_chat_id(update):
"""
Get chat ID from update.
Args:
update (instance): Incoming update.
Returns:
(int, None): Chat ID.
"""
# Simple messages
if update.message:
return update.message.chat_id
# Menu callbacks
if update.callback_query:
return ... | 1669382fd430b445ea9e3a1306c1e68bf2ec0013 | 1,830 |
import time
def timeit(method):
""" Timing Decorator Function Written by Fahim Sakri of PythonHive (https://medium.com/pthonhive) """
def timed(*args, **kwargs):
time_start = time.time()
time_end = time.time()
result = method(*args, **kwargs)
if 'log_time' in kwargs:
... | 598667950bc707b72239af9f4e5a3248dbe64d96 | 1,833 |
import requests
def upload_record(data, headers, rdr_project_id):
""" Upload a supplied record to the research data repository
"""
request_url = f"https://api.figsh.com/v2/account/projects/{rdr_project_id}/articles"
response = requests.post(request_url, headers=headers, json=data)
return res... | 7431234757668f9157f90aa8a9c335ee0e2a043b | 1,834 |
def generate_url_fragment(title, blog_post_id):
"""Generates the url fragment for a blog post from the title of the blog
post.
Args:
title: str. The title of the blog post.
blog_post_id: str. The unique blog post ID.
Returns:
str. The url fragment of the blog post.
"""
... | c846e6203fa4782c6dc92c892b9e0b6c7a0077b5 | 1,835 |
def leap_year():
"""
This functions seeks to return a leap year after user input << integer(4).
Rules for a leap year:
As you surely know, due to some astronomical reasons, years may be leap or common.
The former are 366 days long, while the latter are 365 days long.
Since the introduction of t... | 5cf459514ce768c1cf633fdddab5f986004bc1c8 | 1,836 |
def login(request):
"""Login view for GET requests."""
logged_in = request.authenticated_userid is not None
if logged_in:
return {'logged_in': True,
'form_enabled': False,
'status': u'Already logged in',
'status_type': u'info'}
status = u''
s... | 8cab36d8d059d0683ef2e84a40cca5c99a27c6fc | 1,837 |
def of_type(_type, value_1, *args) -> bool:
"""
Check if a collection of values are of the same type.
Parameters:
_type (any): The type to check for.
value_1 (any): The first value to check.
*args (any): Rest of values to check against given type.
Return... | eab1e70655ff74b1cbfc338a893719b7f0681f4a | 1,838 |
def _make_source(cls_source: str, cls_name: str, instance_method: str):
"""Converts a class source to a string including necessary imports.
Args:
cls_source (str): A string representing the source code of a user-written class.
cls_name (str): The name of the class cls_source represents.
... | 105ca5d34c0de2bfc81937aaaf14b4d610eaa35a | 1,840 |
def get_accessible_cases(item, user):
"""Return all accessible for a cohort and user."""
return getattr(item, "get_accessible_cases_for_user")(user) | 42d54ebf672ce401ac311f9868f6b19f93418065 | 1,841 |
def hex_to_byte(hexStr):
""" Convert hex strings to bytes. """
bytes = []
hexStr = ''.join(hexStr.split(" "))
for i in range(0, len(hexStr), 2):
bytes.append(chr(int(hexStr[i:i + 2], 16)))
return ''.join(bytes) | a424d65b0a02c0d10ee5c7c25409f4a0ce477528 | 1,842 |
def _vital_config_update(cfg, cfg_in):
"""
Treat a vital Config object like a python dictionary
Args:
cfg (kwiver.vital.config.config.Config): config to update
cfg_in (dict | kwiver.vital.config.config.Config): new values
"""
# vital cfg.merge_config doesnt support dictionary input
... | 35a0092013229f3b71a1ba06bbb660f861ef391c | 1,843 |
import struct
def _read_extended_field_value(value, rawdata):
"""Used to decode large values of option delta and option length
from raw binary form."""
if value >= 0 and value < 13:
return (value, rawdata)
elif value == 13:
return (rawdata[0] + 13, rawdata[1:])
elif value == 14:... | 12a1f665f133f6ea5ffc817bf69ec0a9e0e07dbc | 1,844 |
def get_dcgan_args(parser, args=[]):
"""
parameters determing the DCGAN parameters
"""
# DCGAN:
# ------------------------------------------------------------------------
parser.add_argument(
"--lam", type=float, default=10, help="Factor for scaling gradient penalty"
)
parser.add... | 28d00721fad62ecbc381190b05d81fe578860f8e | 1,845 |
import os
def _gen_span_id() -> str:
"""Return 16 random hexadecimal digits.
The id is used for distributed tracing.
"""
return os.urandom(8).hex() | 4c70028da278eb26c947c9ca24e0c527f6744860 | 1,846 |
from typing import Tuple
import re
def _parse_cli_variable(mapping_str: str) -> Tuple[str, str]:
"""Checks that the input is of shape `name:value` and then splits it into a tuple"""
match = re.match(r"(?P<name>.+?):(?P<value>.+)", mapping_str)
if match is None:
raise ValueError(f'CLI variable inpu... | f701b7e85c45c2df35e1252721cd3215357909ba | 1,847 |
def get_point(points, cmp, axis):
""" Get a point based on values of either x or y axys.
:cmp: Integer less than or greater than 0, representing respectively
< and > singhs.
:returns: the index of the point matching the constraints
"""
index = 0
for i in range(len(points)):
if cmp <... | b59035d390e83b45a0131e28c4acf7e302cf3e45 | 1,848 |
import pathlib
def create_jobs_list(chunks, outdir, *filters):
# TO DO
# Figure out the packing/unpacking
"""
Create a list of dictionaries that hold information for the given
chunks
Arguments:
chunks: list: A list of lists. Each nested list contains the
filepaths to be processed
... | 433992eb34bc1f80d12f8cdcee3dbd99d04d22c1 | 1,849 |
def parse_decodes(sentences, predictions, lengths, label_vocab):
"""Parse the padding result
Args:
sentences (list): the tagging sentences.
predictions (list): the prediction tags.
lengths (list): the valid length of each sentence.
label_vocab (dict): the label vocab.
Retur... | bf40d8570e0a552853108e860fd193c0d9940e98 | 1,851 |
def _check_max_features(importances, max_features):
"""Interpret the max_features value"""
n_features = len(importances)
if max_features is None:
max_features = n_features
elif isinstance(max_features, int):
max_features = min(n_features, max_features)
elif isinstance(max_feature... | 816daf9d99ac4ecd2d5024a3be63f793d7669e1f | 1,854 |
def premises_to_syllogism(premises):
"""
>>> premises_to_syllogism(["Aab", "Ebc"])
'AE1'
"""
figure = {"abbc": "1", "bacb": "2", "abcb": "3", "babc": "4"}[premises[0][1:] + premises[1][1:]]
return premises[0][0] + premises[1][0] + figure | a048d44acea1eb4c9346880a74547a9cd100ebf0 | 1,855 |
def is_sum_lucky(x, y):
"""This returns a string describing whether or not the sum of input is lucky
This function first makes sure the inputs are valid and then calculates the
sum. Then, it will determine a message to return based on whether or not
that sum should be considered "lucky"
"""
if x... | 081b5e8cc2657a00ea160e398fb00f84187e2ab6 | 1,856 |
import asyncio
def unsync_function(func, *args, **kwargs):
"""Runs an async function in a standard blocking way and returns output"""
return asyncio.run(func(*args, **kwargs)) | cd7c19bf226b78c9e3c4b19325e7acb4fcc90e21 | 1,857 |
def imputation_Y(X, model):
"""Perform imputation. Don't normalize for depth.
Args:
X: feature matrix from h5.
model: a trained scBasset model.
Returns:
array: a peak*cell imputed accessibility matrix. Sequencing depth
isn't corr... | 75e2de758c3544655d4332098d4398255770d7c3 | 1,858 |
def is_row_and_col_balanced(T1, T2):
"""
Partial latin squares T1 and T2 are balanced if the symbols
appearing in row r of T1 are the same as the symbols appearing in
row r of T2, for each r, and if the same condition holds on
columns.
EXAMPLES::
sage: from sage.combinat.matrices.latin... | f0a9d1522da2fc079d4021603198e79c438de727 | 1,860 |
def submit(ds, entry_name, molecule, index):
"""
Submit an optimization job to a QCArchive server.
Parameters
----------
ds : qcportal.collections.OptimizationDataset
The QCArchive OptimizationDataset object that this calculation
belongs to
entry_name : str
The base entr... | 50a30a25af59906ce5636ce8a176e29befd27d60 | 1,861 |
def _ensure_min_resources(progs, cores, memory, min_memory):
"""Ensure setting match minimum resources required for used programs.
"""
for p in progs:
if p in min_memory:
if not memory or cores * memory < min_memory[p]:
memory = float(min_memory[p]) / cores
return cor... | f311259242a73a7bc527e3601765c95153a08748 | 1,862 |
import ctypes
def ctypes_pointer(name):
"""Create a ctypes type representing a C pointer to a custom data type ``name``."""
return type("c_%s_p" % name, (ctypes.c_void_p,), {}) | d87f10ac06391379a24f166272fd42fa938e3676 | 1,863 |
def split_and_load(data, ctx_list, batch_axis=0, even_split=True):
"""Splits an NDArray into `len(ctx_list)` slices along `batch_axis` and loads
each slice to one context in `ctx_list`.
Parameters
----------
data : NDArray
A batch of data.
ctx_list : list of Context
A list of Co... | 4b8f0d1b6b256895da3e37fbb4b1be0cd0da5c46 | 1,867 |
def string_to_weld_literal(s):
"""
Converts a string to a UTF-8 encoded Weld literal byte-vector.
Examples
--------
>>> string_to_weld_literal('hello')
'[104c,101c,108c,108c,111c]'
"""
return "[" + ",".join([str(b) + 'c' for b in list(s.encode('utf-8'))]) + "]" | d85b016091988c9307cbed56aafdd5766c3c9be5 | 1,869 |
def verify_model_licensed(class_name : str, model_path:str):
"""
Load a licensed model from HDD
"""
try :
m = eval(class_name).load(model_path)
return m
except:
print(f"Could not load Annotator class={class_name} located in {model_path}. Try updaing spark-nlp-jsl") | 057987d838982a85925f70c93ff2f4166b038cec | 1,870 |
def parse_metrics(match, key):
"""Gets the metrics out of the parsed logger stream"""
elements = match.split(' ')[1:]
elements = filter(lambda x: len(x) > 2, elements)
elements = [float(e) for e in elements]
metrics = dict(zip(['key', 'precision', 'recall', 'f1'], [key] + elements))
return m... | 70de1ad16edfe827e0a851c719d902695696700f | 1,871 |
def listtimes(list, c):
"""multiplies the elements in the list by the given scalar value c"""
ret = []
for i in range(0, len(list)):
ret.extend([list[i]]*c);
return ret; | 8aef63677a1a926f355644187d58b47e437e152c | 1,873 |
def eval_f(f, xs):
"""Takes a function f = f(x) and a list xs of values that should be used as arguments for f.
The function eval_f should apply the function f subsequently to every value x in xs, and
return a list fs of function values. I.e. for an input argument xs=[x0, x1, x2,..., xn] the
function e... | 00c6ed7fc59b213a3ec9fec9feeb3d91b1522061 | 1,874 |
import numpy
def rmSingles(fluxcomponent, targetstring='target'):
"""
Filter out targets in fluxcomponent that have only one ALMA source.
"""
nindiv = len(fluxcomponent)
flagger = numpy.zeros(nindiv)
for icomp in range(nindiv):
target = fluxcomponent[targetstring][icomp]
... | 013d5f3169fd1dcb277733627ecd5b0135bc33fb | 1,876 |
import torch
def compute_accuracy(outputs, targets, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = targets.size(0)
_, preds = outputs.topk(maxk, 1, True, True)
preds = preds.t(... | 6cfcc9e43aaaed09baae567f9cc27818c555fe5f | 1,877 |
import io
def unpack_text_io_wrapper(fp, encoding):
"""
If *fp* is a #io.TextIOWrapper object, this function returns the underlying
binary stream and the encoding of the IO-wrapper object. If *encoding* is not
None and does not match with the encoding specified in the IO-wrapper, a
#RuntimeError is raised.
... | f2c93babab4bff1f08e6fe5c04fbd97dd1ee8a84 | 1,878 |
def scale_bounding_box(bounding_box,scale):
"""Scales bounding box coords (in dict from {x1,y1,x2,y2}) by x and y given by sclae in dict form {x,y}"""
scaled_bounding_box = {
"x1" : int(round(bounding_box["x1"]*scale["x"]))
,"y1" : int(round(bounding_box["y1"]*scale["y"]))
,"x2" : int(ro... | 8aa374537ed2ae3ae2324bd8a4819e981f281b71 | 1,880 |
import click
def is_command(obj) -> bool:
"""
Return whether ``obj`` is a click command.
:param obj:
"""
return isinstance(obj, click.Command) | 8159aea42baca70b3218a0b82e2f4dc3f34278aa | 1,881 |
def GetContigs(orthologs):
"""get map of contigs to orthologs.
An ortholog can be part of only one contig, but the same ortholog_id can
be part of several contigs.
"""
contigs = {}
for id, oo in orthologs.items():
for o in oo:
if o.contig not in contigs:
con... | 0c449a31e60f1a149317de815d630c4d8a817ca1 | 1,882 |
def remove_hydrogens(list_of_lines):
"""
Removes hydrogen from the pdb file.
To add back the hydrogens, run the reduce program on the file.
"""
return (line for line in list_of_lines if line['element']!=" H") | 164ac79171cf6b3632fe7909ace91ffe75192b61 | 1,883 |
import torch
def label_smooth_loss(log_prob, label, confidence=0.9):
"""
:param log_prob: log probability
:param label: one hot encoded
:param confidence: we replace one (in the one hot) with confidence. 0 <= confidence <= 1.
:return:
"""
N = log_prob.size(0)
C = log_prob.size(1)
s... | f1164d1a41d2c275ae4e406e2a46a0d50a2d240d | 1,884 |
import re
def parse_field_pubblicazione(field):
"""
Extracts year, place and publisher from the field `pubblicazione` by applying a cascade of regexps.
"""
exp2 = r'^(?P<place>\D+)(?:\s?\W\s?)(?P<publisher>.*?)\D{1}?(?P<year>\d+)?$'
exp1 = r'^(?P<place>.*?)(?::)(?P<publisher>.*?)\D{1}?(?P<year>\d+)?$'
exp3 = r'... | 91aee4dabf62b3ec5bccff2a07d664312226448c | 1,885 |
import re
def _strip_build_number(api_version):
"""Removes the build number component from a full api version string."""
match = re.match(r"^([A-Z]+-)?([0-9]+)(\.[0-9]+){2}$", api_version)
if match:
return api_version[:match.start(3)]
# if there aren't exactly 3 version number components, just leave it un... | 20d8023281f05dfcb8c9fdd021b77796c72e1001 | 1,886 |
def five_five(n):
"""
This checks if n is a power of 2 (or 0).
This is because the only way that n and (n-1) have none of the same bits (the
& check) is when n is a power of 2, or 0.
"""
return ((n & (n-1)) == 0) | 0b1cc310b5d8bd6dab6299b6a999a5dd0720ea80 | 1,888 |
def convert_bytes_to_size(some_bytes):
"""
Convert number of bytes to appropriate form for display.
:param some_bytes: A string or integer
:return: A string
"""
some_bytes = int(some_bytes)
suffix_dict = {
'0': 'B',
'1': 'KiB',
'2': 'MiB',
'3': 'GiB',
... | d1579e0fc0850a98145910c056b3fac8be7c66f1 | 1,889 |
def degreeList(s):
"""Convert degrees given on command line to a list.
For example, the string '1,2-5,7' is converted to [1,2,3,4,5,7]."""
l = []
for r in s.split(','):
t = r.split('-')
if len(t) == 1:
l.append(int(t[0]))
else:
a = int(t[0])
... | 3b517831ddab47da5cd0e36fa5913d6d59e73715 | 1,891 |
def get_bounding_box(font):
""" Returns max and min bbox of given truetype font """
ymin = 0
ymax = 0
if font.sfntVersion == 'OTTO':
ymin = font['head'].yMin
ymax = font['head'].yMax
else:
for g in font['glyf'].glyphs:
char = font['glyf'][g]
if hasattr... | 98161ef3426c2bb9b6dc4079c69f5c1f9d4e93a2 | 1,892 |
def create_user(client, profile, user, resend=False):
""" Creates a new user in the specified user pool """
try:
if resend:
# Resend confirmation email for get back password
response = client.admin_create_user(
UserPoolId=profile["user_pool_id"],
U... | 4c1f83c0ab7fd28dc7b1e2d8f2efa224360dfdb1 | 1,893 |
import re
def sort_special_vertex_groups(vgroups,
special_vertex_group_pattern='STYMO:',
global_special_vertex_group_suffix='Character'):
"""
Given a list of special vertex group names, all with the prefix of
special_vertex_group_pattern, selec... | 0cc8f0992553e5da5b37ea9a9886996cb9013582 | 1,895 |
import hashlib
def obtain_file_hash(path, hash_algo="md5"):
"""Obtains the hash of a file using the specified hash algorithm
"""
hash_algo = hashlib.sha256() if hash_algo=="sha256" else hashlib.md5()
block_size = 65535
with open(path, 'rb') as f:
for chunk in iter(lambda: f.read(block_si... | daa996339c638eaab4f3d067dcaaa4b865a6f923 | 1,896 |
def keras_decay(step, decay=0.0001):
"""Learning rate decay in Keras-style"""
return 1. / (1. + decay * step) | f26f1f100ecf1622d6da9958d0a6cd95a37b8b2a | 1,897 |
import collections
def _build_pep8_output(result):
"""
Build the PEP8 output based on flake8 results.
Results from both tools conform to the following format:
<filename>:<line number>:<column number>: <issue code> <issue desc>
with some issues providing more details in the description within
... | a4abda2f9d3a2d9b3524c60429b047cbfe0285d9 | 1,898 |
def form_value(request, entity, attribute):
"""
Return value from request params or the given entity.
:param request: Pyramid request.
:param entity: Instance to get attribute from if it isn't found in the request
params.
:param str attribute: Name of attribute to search for in the request ... | 1daea77474dae5a1cb6fdab0b075a5b2f5c40865 | 1,899 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.