content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
import torch
def pairwiseL2Dist(x1, x2):
""" Computes the pairwise L2 distance between batches of feature vector sets
res[..., i, j] = ||x1[..., i, :] - x2[..., j, :]||
since
||a - b||^2 = ||a||^2 + ||b||^2 - 2*a^T*b
Adapted to batch case from:
jacobrgardner
https://github.com/p... | f87a9e07e09ea247d4747b161a8af28ac2648473 | 395,985 |
import markdown
def markdownify(value):
"""Converts Markdown string to HTML
"""
html = markdown.markdown(value)
return html | dd130325cfbc6f04deb539b3047817e41f3d17a8 | 124,666 |
def strep(s):
"""Format a string for display as a line context."""
return (s
.replace(' ', '␣')
.replace('\n', '⏎ ')
.replace('\t', '⇥')
) | 6be32c63f2d88615552483fd7c8bd1dfe1a8e36d | 287,705 |
from typing import Counter
def create_vocabulary(train_set, vocab_size):
"""Creates vocabulary out of the training set tokens.
Arguments:
train_set: CoNLL2003 train_set from HuggingFace
vocab_size (int): Maximum number of tokens in the vocab
Returns:
vocab (dict): Vocabulary of al... | d7d87b703e8fe9a7682d75797db09f94efc56da5 | 264,177 |
def parse_fastq(fh):
""" Parse reads from a FASTQ filehandle. For each read, we
return a name, nucleotide-string, quality-string triple. """
reads = []
while True:
first_line = fh.readline()
if len(first_line) == 0:
break # end of file
name = first_line[1:].rstr... | d33d3efebdd1c5f61e25397328c6b0412f1911dd | 1,996 |
def minimum_grade(parsed_list, passing_grade, overall_min=True):
"""
This function calculates the minimum grade from the given grades.
:param parsed_list: the parsed list of the grades
:param passing_grade: the grade passing threshold
:param overall_min: True, when calculating the minimum of all th... | 2ffbecd3177964ae89be8bedf8ee7a0884affea0 | 634,844 |
from typing import Union
def expand_abnormality_questions(
answer: str, matched_word: str, modality: Union[None, str]
):
"""Create new questions for abnormality with given information.
Args:
answer: Original answer to the question
matched_word: The keyword which labeled the original quest... | 34b58b9d87fcaab1df052fcf9069f6432d0fb006 | 400,986 |
def argv_to_module_arg_lists(args):
"""Converts module ldflags from argv format to per-module lists.
Flags are passed to us in the following format:
['global flag', '--module', 'flag1', 'flag2', '--module', 'flag 3']
These should be returned as a list for the global flags and a list of
per-mod... | 847597d09e56af4221792a9a176bddfea334e622 | 17,132 |
import ast
def _convert_to_expression(node):
""" convert ast node to ast.Expression if possible, None if not """
node = ast.fix_missing_locations(node)
if isinstance(node, ast.Module):
if len(node.body) != 1:
return None
if isinstance(node.body[0], ast.Expr):
expr ... | a24c8c304b21d2a4f90e7946267620a853db1a4b | 647,305 |
def read_lines(file_path):
""" Read lines from the file and return then as a list. """
lines = []
with open(file_path, 'r', encoding='utf8') as asm_file:
lines = asm_file.readlines()
return lines | a6f97f60edefac2207369624c4421829b8637fbe | 677,239 |
def _ev(x, pdist):
"""
expectation value via x, p(x)
"""
ev = 0
for idx, p in enumerate(pdist):
ev += x[idx] * pdist[idx]
return ev | b126c712d81f124e3140f84d44cf75c0ad46034e | 150,855 |
def get_port_information(vips, matching_no):
"""
Get information on what sampling ports are used in a match, and check that
everything is set up correctly.
Return the number of the first port, and a boolean indicating if the second port is used.
"""
sample_port_1 = int(vips.getValue(f'Template m... | b338c9413f0e9056c7c6cc7f3048b0318b59c8d9 | 565,376 |
def check_list(in_lst, dtype=str):
"""Helper function to ensure input is a list of correct data type."""
assert isinstance(in_lst, (list, dtype, tuple))
if isinstance(in_lst, list):
for itm in in_lst:
assert isinstance(itm, dtype)
else:
in_lst = [in_lst]
return in_lst | 597c7cbde33dd0dd53ede958338b8bd71ca14458 | 123,930 |
def manifest_to_file_list(manifest_fn):
"""
Open a manifest file and read it into a list.
Entries in the list are relative, i.e. no leading
slash.
manifest_fn -- the manifest file to read
"""
image_manifest_list = []
with open(manifest_fn) as image:
image_manifest_list = [x[1:] ... | 982f02e0b00fad20af8d50d44673d65d9bba5a37 | 7,438 |
def fp(v, g, x):
"""FP-neuron function, as described in Equation 7 in the Paper.
Parameters
----------
v : np.array
filter vector 1
g : np.array
filter vector 2
x : np.array
position
Returns
-------
float
function value
"""
sp_vx = (v * x).su... | d24d445ab8238e4c78cfec9347bd66065245f5b4 | 178,861 |
def are_all_letter_in_grid(grid, text_box):
"""
Args:
grid (Grid): either the main grid or the temporary grid
text_box (TextBox): the text box where the player/user types the words/characters
Returns:
boolean: True if all letters are in the grid. Otherwise, False
"""
for ch... | 82559cb858aee8c34e93d606529d57f712c172bb | 420,390 |
def integers_from_file(filename):
""" Return a list of the integers in FILENAME, assuming one per line. """
integers = []
with open(filename,"r") as f:
for line in f:
integers.append(int(line))
return integers | 885406ff59a760bcfc7c2b800a34044da3855150 | 256,122 |
def dunder_get(_dict, key):
"""Returns value for a specified dunderkey
A "dunderkey" is just a fieldname that may or may not contain
double underscores (dunderscores!) for referrencing nested keys in
a dict. eg::
>>> data = {'a': {'b': 1}}
>>> nesget(data, 'a__b')
1
key... | 3cfdccd5cf78c3bcbd7942e836583fa68ef656b7 | 379,072 |
import calendar
def days_in_year(year: int) -> int:
"""Returns the number of days in the year
Args:
year (int): The year.
Returns:
int: The number of days in the year.
"""
return 366 if calendar.isleap(year) else 365 | b8a03d7bca79c3513ac4d278028b6c65b4fc3329 | 651,255 |
from typing import Dict
def values_from_bucket(bucket: Dict[str, float]) -> set:
"""Get set of price formatted values specified by min, max and interval.
Args:
bucket: dict containing min, max and interval values
Returns:
Formatted set of values from min to max by interval
"""
rng = ... | 848af64d3396cc77c3fa109821d72eb80961eec0 | 15,310 |
def formatfloat(x):
"""Convert x to a %.3f-format string."""
ret = "%.3f" % float(x)
if float(x) >= 0.0:
return f" {ret}"
return ret | a70e9550d09f44bff44d22987c35be2233e4af56 | 612,502 |
def isHappy(n):
"""
Is happy takes in a number and returns True if it is a happy number, False otherwise. A happy number
is a number defined by the following process: Starting with any positive integer, replace the number by the
sum of the squares of its digits, and repeat the process until the number e... | 8c313a114d6c381d1a6ea05c520f75a8be368f8b | 491,294 |
def isnpdot(val):
"""
check if the entry starts with 'np.'
Parameters
----------
val: str
a string entry
Returns
-------
bool
True if the entry is a string and the first three characters are 'np.',
False otherwise
"""
if isinstance(val, str):
if... | c0ad9f9f7847d9510aa38837d34e230388f03abc | 233,741 |
import json
def get_best_hyperparams(hyperparams_dict, fit_params_dict, best, file_name=None):
"""
Helper function to extract the numerical values of best hyperparameters from hyperopt into a more easily usable format.
:param hyperparams_dict: Dictionary of hyperparameter values
:param fit_params_dict... | 1bac0d463ba5cf5a69912673d1235794d9a448ff | 26,226 |
import hashlib
def sha256_encode(text):
"""
Returns the digest of SHA-256 of the text
"""
_hash = hashlib.sha256
if type(text) is str:
return _hash(text.encode('utf8')).digest()
elif type(text) is bytes:
return _hash(text).digest()
elif not text:
# Generally for cal... | 88a39fb82beefa4f89abda7f995347ca16573d28 | 686,371 |
def redcap_event_to_vbr_protocol(event_name: str) -> int:
"""Map redcap event name to VBR protocol."""
# NOTE - this must be manually synced with src/scripts/data/protocol.csv
events = {
"informed_consent_arm_1": 2,
"baseline_visit_arm_1": 3,
"6wks_postop_arm_1": 30,
"3mo_pos... | 8c1198c7037d29123b86ac927fed7c931022a99b | 276,910 |
def followers_count(user):
"""
Returns user followers count
:param user: An User instance
"""
if not user or user.is_anonymous():
return 0
return user.followers() | 8c386bc2199661b2a771371a0e7c1833569a71c9 | 693,383 |
def divisors_list(n):
""" Returns the list of divisors of n """
return [d for d in range(1, n // 2 + 1) if n % d == 0] + [n] | b440cac93ce4ebc787d3dbc3760645a2a5d0d960 | 269,279 |
def try_clone(repo, path):
"""
Clone a repository and wait for it to finish
:param repo: repository
:param path:
:return: returncode
"""
return repo.aclone(path).wait() | d9cf7388fc9c2eae29cdb87a9f86264a1c32e284 | 149,618 |
import struct
def readu16(file):
""" Reads two bytes bytes from the file, treating them as a 16 bit unsigned integer."""
data = file.read(2)
return struct.unpack("H", data)[0] | 19b3b476dbbfea96c5259c99ac8dee1191d6bc9d | 464,900 |
import json
def load_json(input_file):
"""
Load a JSON file into a JSON object
:param input_file:
:type input_file:
:return: loaded JSON object
:rtype: list
"""
with open(input_file, 'r') as f:
output_json = json.load(f)
return output_json | 48a47abfafb99e3cf20935f1a2fc1419fd05d765 | 156,357 |
def diff(new, old):
"""
Compute the difference in items of two revisioned collections. If only
`new' is specified, it is assumed it is not an update. If both are set,
the removed items are returned first. Otherwise, the updated and edited
ones are returned.
:param set new: Set of new objects
... | 49aa2a4eb770ba812f5bf81131d7bad6fc01abde | 211,765 |
import functools
import warnings
def deprecated(since=None, until=None, reason=None):
"""
Warns when a deprecated callable is used.
Examples:
```python
from flashback import deprecated
@deprecated
def func():
pass
func()
#=> func is deprecated... | 82ceee1271ba6daa2a5217a0b914819eacb0aa05 | 220,825 |
import json
def mock_requests_get(*args, **kwargs):
""" Mock requests.get() function for testing meme parsing functions.
"""
class MockResponse(object):
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
def json(... | fb5f03ba2bb30093b1565e2419f354c76d4b3ca8 | 210,269 |
def cast_byte_timeout(arg):
"""
Приведение времени в секундах к коду таймаута.
>>> cast_byte_timeout(0.0)
0
>>> cast_byte_timeout(0.15)
150
>>> cast_byte_timeout(0.30)
151
>>> cast_byte_timeout(15.0)
249
>>> cast_byte_timeout(30.0)
250
>>> cast_byte_timeout(105.0)
... | 7d73215777318414bd510721ef288b8b94b473e2 | 364,979 |
def player_last_awarded_submission(profile):
"""Returns the last awarded submission date for the profile."""
entry = profile.scoreboardentry_set.order_by("-last_awarded_submission")
if entry:
return entry[0].last_awarded_submission
else:
return None | 15e3ec8b0b0fc218c89a3dbe0484b30191dd399f | 640,276 |
def _hemi_direction(hemisphere):
"""Return `1` for 'north' and `-1` for 'south'"""
return {'north': 1, 'south': -1}[hemisphere] | 8305bafc5b3d4fb6039d28a2c7aa9fbf3d62ecb7 | 581,796 |
import re
def nonschema_title_formatter(title):
"""
Formatting a path, that is absent in schema, to human-readable form
:param title: str
:return: formatted title
>>> nonschema_title_formatter('legalEntityTypeDetail')
'Legal Entity Type Detail'
>>> nonschema_title_formatter('fuenteFinanci... | b04d359280f4a8593a6f48081b29b0acd9e21641 | 463,325 |
def cloner_tableau(arr: list) -> list:
"""
Description:
Permet de cloner un tableau dans un tableau.
L'opérateur ( * ) permet de déballer un itérable pour en créer un nouveau.
Paramètres:
arr: {list} -- Tableau à cloner.
Retourne:
{list} -- le tableau cloné.
Ex... | eae9b4c6f127827f377050f3328f2aeacc38bf05 | 291,876 |
import uuid
import base64
def cspvalues(ui):
"""Obtain the Content-Security-Policy header and nonce value.
Returns a 2-tuple of the CSP header value and the nonce value.
First value is ``None`` if CSP isn't enabled. Second value is ``None``
if CSP isn't enabled or if the CSP header doesn't need a no... | cf967415321b31d6f9c802a794c5177942173f0c | 210,715 |
import re
def remove_regex_pattern_from_keys(d: dict, pattern_to_remove: str, **regex_compile_kwargs) -> dict:
"""Remove `pattern_to_remove` from all keys in `d`.
Return a new dict with the same values as `d`, but where the key names
have had `pattern_to_remove` removed.
"""
new_dict = {}
reg... | 8ae1918dbbc4f6054e95606a2019d7409749c71f | 387,931 |
def _stripBold(s):
"""Returns the string s, with bold removed."""
return s.replace('\x02', '') | da94e26846091ac3c4a501b86cd00a18bf06b1c1 | 689,640 |
import itertools
def get_all_combos(list_of_lists):
"""
Get all the position-specific combos of a list of lists.
This is taken directly from Stack Overflow:
http://stackoverflow.com/questions/798854/all-combinations-of-a-list-of-lists
:param list_of_lists: A list of lists we would like combos... | aee5c69aa2e86b6362fa885b7575ebf36c4c81ba | 162,751 |
def convertToIndexList(vertList):
""" convert components given to a list of indices
:param vertList: list of components
:type vertList: list
:return: list of integers representing the components values
:rtype: list
"""
indices = []
for i in vertList:
index = int(i[i.index("[") +... | ad45ce456bae4c4caba9a60d101a4f853121a08f | 414,782 |
def sign(x):
"""
Objective: take the sign of x
e.g.,
>>>sign(-1.2)
-1
>>>sign(5)
1
"""
if x>0: return 1
elif x<0: return -1
else: return 0 | 795b8813b95321168de5b7fc5d84b5b474325062 | 586,096 |
def partition(seq, fn):
"""Partitions one sequence into two sequences, by testing whether each element
satisfies fn or not. """
pos, neg = [], []
for elt in seq:
if fn(elt):
pos.append(elt)
else:
neg.append(elt)
return pos, neg | 45964faef26715d0edcd36b7dd102e642e9e7154 | 239,893 |
def with_precision(val, precision):
"""
Round `val` to the closest multiple of `precision`.
"""
return float(round(val / precision) * precision) | b83c1373147368ee35cf27db1926449c8fad37a2 | 363,766 |
def extract_code(lines, node, lstrip="", ljoin="\n", strip=""):
"""Get corresponding text in the code
Arguments:
lines -- code splitted by linebreak
node -- PyPosAST enhanced node
Keyword Arguments:
lstrip -- During extraction, strip lines with this arg (default="")
ljoin -- During extra... | 2a468e136f822db96963247ea86877b1e3c872f9 | 165,890 |
def is_directory_automatically_created(folder: str):
"""
Verifies the name of the directory -> if it contains a month it returns True, otherwise False.
"""
months = [
"(01)Janvier",
"(02)Fevrier",
"(03)Mars",
"(04)Avril",
"(05)Mai",
"(06)Juin",
"(0... | 7e78466a662d7bcd899d1a15994bf03b2cf8c3da | 101,869 |
def string_char(char):
"""Turn the character into one that can be part of a filename"""
return '_' if char in [' ', '~', '(', ')', '/', '\\'] else char | e318811d68858ff090447fa0e0e0aab9133461c8 | 60,823 |
def calculate_degrees_between_angle(norm_1, norm_2, angle):
"""
Takes two normalized values in a range of [0, 1] and calculates a proportion inside an angle.
:param norm_1: First normalized value.
:param norm_2: Second normalized value.
:param angle: Angle in which to calculate the proportion.
... | cefdb4f09d24cf8db3274f07e04a63dba6555864 | 381,601 |
from typing import Dict
from typing import Any
from typing import Set
def scalar_training_metrics_names(exp: Dict[str, Any]) -> Set[str]:
"""
Given an experiment history, return the names of training metrics
that are associated with scalar, numeric values.
This function assumes that all batches in an... | 16290499e6f100fa7b81e4858f85accc60562922 | 377,578 |
import uuid
def create_id() -> str:
"""
Create a unique ID, UUIDv4 style
Returns:
str:
"""
return str(uuid.uuid4()) | ab9e629d032e23f581dd0bf0602599059669a461 | 501,312 |
def v_first_group_option(group_options):
"""
Returns the first value in a menu-select that uses option_groups. This is
useful when you create an options structure and you want the first item as
the placeholder or the default selected value.
Parameters
----------
group_options : list[dict]
... | 99250919c116c2a10a9df736122d7bafbc8bcc89 | 282,702 |
def calc_text_width(widget, text):
"""Estimate the width that the given text would take within the widget."""
return (widget.fontMetrics().width(text) +
widget.fontMetrics().width('M') +
widget.contentsMargins().left() +
widget.contentsMargins().right()) | 977914f060d4029f1b6c7d0ffe947fde256b9829 | 531,554 |
def docs_append_to_section(docstring, section, add):
"""Append extra information to a specified section of a docstring.
Parameters
----------
docstring : str
Docstring to update.
section : str
Name of the section within the docstring to add to.
add : str
Text to append t... | c4c58d809aaeff2a9f68c193894dae155fa7a9dd | 662,006 |
import re
def parse_show_interface_mgmt(raw_result):
"""
Parse the 'show interface mgmt' command raw output.
:param str raw_result: vtysh raw result string.
:rtype: dict
:return: The parsed result of the show interface mgmt command in a \
dictionary of the form:
::
{
... | 6944b872a6672a51237645bf58bbeaece534a756 | 586,601 |
import string
def idx_to_label(n: int) -> str:
"""Convert a number to a corresponding letter in the alphabet.
In case the number is higher than the number of letters in the english alphabet, then
a second character is appended.
>>> For instance:
>>>idx_to_label(0)
>>> 'a'
>>> idx_to_label... | 67e4bba437016dddb6d90f286e1d65f2bfec8caf | 16,728 |
def unescape(text):
"""
Returns the given text with ampersands, quotes and angle brackets decoded
for use in URLs.
This function undoes what django.utils.html.escape() does
"""
return text.replace(''', "'").replace('"', '"').replace('>', '>').replace('<', '<' ).replace('&', '... | a2de8102996f220996b34ae9228ece9cc8424e97 | 512,463 |
from typing import Union
def listify(data: Union[str, int, float, list, set, tuple, bool]) -> list:
"""
A convenience function that converts data into a list unless it's already one.
Bool is a subset of int.
:param data: Data to turn into a list
:return: list
"""
if isinstance(da... | b45b15ffcbbd5359cf4d11e6d1c9e619220f308c | 419,840 |
from typing import Iterable
from typing import Generator
def listify(x, *args):
"""Convert `x` to a `list`."""
if args:
x = (x,) + args
if x is None:
result = []
elif isinstance(x, list): result = x
elif isinstance(x, str) or hasattr(x, "__array__") or hasattr(x, "iloc"):
... | 532be9b805c8e503d0873eeaa6df5cd635317216 | 358,070 |
def createTable(values):
"""Creates a table from a list of one dimensional dicts. Keys of the first dict will be used as headings.
:param values list woth dicts that only have one layer of dicts (subdicts lead to ugly results)
and each dict needs to have the same keys or they won't be displayed or an er... | 71bc2265363fc93a54928683a1d106c13f9efe1e | 106,132 |
def parse_index_specification(spec):
"""Parses an index specification used as arguments for the -f
and -F options."""
result = []
for part in spec.split(","):
part = part.strip()
if "-" in part:
lo, hi = list(map(int, part.split("-", 1)))
result.extend(range(lo, h... | e61bf0ac9f5313106569f0ad1fe364ed5e47f1c1 | 431,430 |
def find_metadata_item(metadata_items, key_name):
""" Finds a metadata entry by the key name. """
for item in metadata_items:
if item['key'] == key_name:
return item
return None | 3f5f9a7eb36825fb399588c222f651332abe2a2f | 222,893 |
import re
def is_valid_zcs_container_id(zcs_container_id):
"""
Validates Zadara Container Services (ZCS) container IDs, also known as the
ZCS container "name". A valid ZCS container name should look like:
container-00000001 - It should always start with "container-" and end with
8 hexadecimal cha... | 9b8ef0e6281def4d09e787123e9582c9c4be9c84 | 44,675 |
def get_cell_corners(cell:list, width:float, height:float, rel_loc='mid') -> list:
"""
Returns the corners of a cell (x1, x2, y1, y2).
:param cell: A tuple of lat/Y,lon/X-coordinates of the cell
:param width: Width of the cell
:param height: Height of the cell
:param rel_loc: Which position... | 32e041dbabb4b968e799bdb65e995a33ae69d55e | 120,465 |
def point_distance_wrapper(line:list):
"""
wrapper function to return a function that finds the distance between point and the line
:param line: line to be compared to (gradient, y_intercept)
:return: function that takes in a point and finds the distance from the input point to the line
"""
def ... | 0f08668efcb3107fd190169151580cd7874d1bce | 434,153 |
def rpc_get_methods(client, current=None, include_aliases=None):
"""Get list of supported RPC methods.
Args:
current: Get list of RPC methods only callable in the current state.
include_aliases: Include aliases in the list with RPC methods.
"""
params = {}
if current:
params... | 7473720d7bfe354300176a107660ad4bdd4d4a88 | 664,891 |
def calc_sums(variable):
"""
Aggregates the instance dimension of the variable tensor object (see documentation in ../process_raw_data.py)
by summing up the values of the instances.
note: this function is called after the database check -> all values are available and valid.
:param variable: data s... | b88a4352926b23dd27ea598d746619e8c9911ada | 620,083 |
def request_too_large(e):
"""Generates a valid ELG "failure" response if the request is too large"""
return {'failure':{ 'errors': [
{ 'code':'elg.request.too.large', 'text':'Request size too large' }
] } }, 400 | eac64f3ab4316a59e23c60a0d29146de953a615d | 83,439 |
import re
def create_rules(fileObj):
"""Creates a dictionary of key - values pairs. Used by replaceChars
function
Args:
fileObj: A file object with lines in the following format (no spaces)
old_char=new_char
Returns:
A python dictionary with key - value pairs
... | bd20cad1022bc9691326f6e33acd3421755afb81 | 147,821 |
def remove_suffix(string: str, suffix: str) -> str:
"""Remove suffix from the end of the string if present."""
if suffix and string.endswith(suffix):
return string[:-len(suffix)]
return string[:] | a3f4ff3c0299f91302b29f36a43c43f94dc53e4d | 433,016 |
def demandNameItem(listDb,phrase2,mot):
"""
put database name of all items in string to insert in database
listDb: list with datbase name of all items
phrase2: string with database name of all items
mot: database name of an item
return a string with database name of all items separated with ','
... | 67af8c68f0ba7cd401067e07c5de1cd25de9e66c | 590 |
def add_data_args(parser):
"""Train/valid/test data arguments."""
group = parser.add_argument_group('data', 'data configurations')
group.add_argument('--train_h5_path', type=str, help='Pathway to the train H5 file.')
group.add_argument('--val_h5_path', type=str, help='Pathway to the val H5 file.')
... | 90fb746fad6d80e319fce51c6be221ba8f79db37 | 530,234 |
from typing import List
def compute_ap(pos: List[str], amb: List[str], ranked_list: List[str]):
"""Compute average precision against a retrieved list of images. There are some bits that
could be improved in this, but is a line-to-line port of the original C++ benchmark code.
Args:
pos (List[str]):... | ea653c614990b7b588e2e3413fc5b3dd278c4092 | 612,851 |
def get_call_name(call):
"""Get name of a `mock.call` function."""
call_str = str(call)
call_str = call_str[call_str.find('call.') + len('call.'):]
call_str = call_str[:call_str.find('(')]
return call_str | b08974edfcea74adb65bd0d98fddcb313ddf8da6 | 517,887 |
def update_yaml_with(target, source):
"""Merge the source YAML tree into the target. Useful for merging config files."""
if isinstance(target, dict) and isinstance(source, dict):
for k, v in source.items():
if k not in target:
target[k] = v
else:
t... | 244b81220622a1ebd308519e730111a78f32cce1 | 441,523 |
def __inverse_name(name):
"""get the name of the inversed registration pair"""
name = name + '_inverse'
return name | c1a8321380aba1caf65553646d313d5092d0ddc8 | 179,671 |
def create_chunks(ascdata, max_bytes_in_barcode):
"""Chunk ascdata into a list of blocks with size max_bytes_in_barcode or less.
Only specific ASCII characters are allowed in ascdata so we don't worry about Unicode.
Each block begins with ^<sequence number><space> (1-based).
This allows to easily put th... | 8505a3ebfdcd286ed7472ab23e57f2dc7ca27491 | 331,938 |
def rightmost(root):
"""
Returns the rightmost leaf in this tree.
:param root: The root node of the tree.
:type root: d20.ast.ChildMixin
:rtype: d20.ast.ChildMixin
"""
right = root
while right.children:
right = right.children[-1]
return right | 85f5ed1f0cc326fd1211295432b41c63bfac0311 | 124,888 |
import pathlib
def get_expected_file_in_directory(file: pathlib.Path, directory: pathlib.Path) -> pathlib.Path:
"""
Return the expected file in the directory -- the file may exist or not.
"""
expected_file = directory / file.name
return expected_file | 57f24a0f53376988a84b0a84e3cd24d4dcfe02d5 | 431,436 |
import random
import string
def gen_random_str(min_length, max_length, prefix=None, suffix=None,
has_letter=True, has_digit=False, has_punctuation=False):
"""
指定一个前后缀、字符串长度以及字符串包含字符类型,返回随机生成带有前后缀及指定长度的字符串
:param:
* min_length: (int) 字符串最小长度
* max_length: (int) 字符串最小长度
... | d7039df8299a858c6fbe62619230740e466772e2 | 46,505 |
import time
def game(engine1, engine2, print_move_times=False):
"""engine1 and engine2 are instances of EngineInterface.
engine1 makes the first move.
Return (result, engine1 time, engine2 time)
result is 0 if the game is a draw, 1 if engine1 win and 2 if engine2 win.
"""
total_time_engine_1 =... | 04f6d6ff18aed8922fc3884f8e05ef958d1c0e5f | 256,774 |
def _initialize_tableau(A_ub, b_ub, A_eq, b_eq, tableau, basis):
"""
Initialize the `tableau` and `basis` arrays in place for Phase 1.
Suppose that the original linear program has the following form:
maximize::
c @ x
subject to::
A_ub @ x <= b_ub
A_eq @ x == b_eq
... | 53e34d84978bb1604d3b5e8270b4df8f1e16037a | 113,156 |
def get_distance(distance_df, location_1, location_2):
"""
Return distance value between two locations.
Args:
distance_df (pandas DataFrame) : distance dataframe
location_1 (str) : name of the first location
location_2 (str) : name of the second location
... | 35d0f5bbd42de7b3f8bbe0c341c220c2682a333a | 203,802 |
def get_extra_couchdbs(config, couch_database_url):
"""
Create a mapping from database prefix to database url
:param config: list of database strings or tuples
:param couch_database_url: main database url
"""
extra_dbs = {}
for row in config:
if isinstance(row, tuple):... | 75357442b19c0297f314ed45682c5f950e8ccedd | 521,659 |
import types
def _copy_fn(fn):
"""Create a deep copy of fn.
Args:
fn: a callable
Returns:
A `FunctionType`: a deep copy of fn.
Raises:
TypeError: if `fn` is not a callable.
"""
if not callable(fn):
raise TypeError("fn is not callable: %s" % fn)
# The blessed way to copy a function. co... | 37fca64ddaadfc8a6a24dce012af2143038cacd2 | 705,241 |
def is_interior_link(link, grid):
"""Return True if both nodes are core; False otherwise."""
return (
grid.status_at_node[grid.node_at_link_tail[link]] == grid.BC_NODE_IS_CORE
and grid.status_at_node[grid.node_at_link_head[link]] == grid.BC_NODE_IS_CORE
) | c4292584b264ac5dd96c4f290c6a728a2ac2cf5f | 258,880 |
def Sign(number):
"""
Sign(number)
Return the mathematical sign of the given number.
"""
return number and (-1, 1)[number > 0] | 7a7c4dae06df05715c4d4dd0680b000bccb924d4 | 632,601 |
import inspect
def two_column_header(left: str, right: str) -> str:
"""
Create exam header with two column
:param left: left text
:param right: right text
:return: Latex code
"""
return inspect.cleandoc(rf"""
\begin{{tabular}}{{ *{{2}}{{ p{{ \dimexpr0.5\linewidth-2\tabcolsep\relax }} ... | 4c1315906a9f8b6d79de9871cb20744ec1856cd6 | 483,046 |
import hashlib
def img_name_processor(src):
"""
This function is used to handle the file name of the saved picture.
Hash the URL of the picture as its filename.
:param src: image url
:return: image filename
"""
h5 = hashlib.md5()
h5.update(src.encode('utf-8'))
img = h5.hexdigest() ... | 712d5fea13d6ec92096b0d88b9e06aa4cd18551d | 516,513 |
def row_to_dict(row):
"""
This takes a row from a resultset and returns a dict with the same structure
:param row:
:return: dict
"""
return {key: value for (key, value) in row.items()} | 0d1f2d55f50c70906964368706214e2a36c3578d | 522,761 |
def organism_matches(organism, patterns):
"""Tests organism filter RegEx patterns against a given organism name."""
for pattern in patterns:
if pattern.match(organism):
return True
return False | 135b6ad0472a2b0904ececc8b8f226c77b2294e6 | 20,897 |
def dict_merge2(*dicts):
""" Return a dict with all values of dicts.
If some key appears twice and contains iterable objects, the values
are merged (instead of overwritten).
"""
res = {}
for d in dicts:
for k in d.keys():
if k in res and isinstance(res[k], (list, tupl... | dff7f5577ee71652cf09d29c945852584092dcb7 | 148,142 |
from pathlib import Path
def file_exists(file_path: str) -> bool:
"""Checks if a file exists at `file_path`
:param file_path: path to file whose existence is being checked.
:type file_path: str
:rtype: bool
"""
return Path(file_path).is_file() | 11b14ccf73ed753ccf46a7100f98d63052ea5fc5 | 59,143 |
from typing import Any
def get_record_code(record: dict[str, Any]) -> str:
"""Returns the concatenated codes for a taric record."""
return f"{record['record_code']}{record['subrecord_code']}" | 11e438fbee7a831494ce47fcd59dce8c3840adcb | 665,252 |
def template_size(template):
"""
Returns template width and height
:type template: list of list of int
:rtype: dict
"""
size = {
"width": max([len(row) for row in template]) if len(template) > 0 else 0,
"height": len(template),
}
return size | b993e1a1d8c7386e2429ce569cae5c4e59e19682 | 313,001 |
def blend_components(original_component, tint_component, tint_strength):
"""Blend the two color components with the given tint strength.
original_color and tint_color are integers in [0, 255].
Formula: http://stackoverflow.com/a/29321264
"""
tint_factor = tint_strength / 100.
return round((
... | 996504628d26cc59f79b3320baa909a31be3a546 | 249,790 |
def reshape(array, step):
""" Reshapes array of size n to matrix with dimensions n/step by step.
Args:
array: List to reshape.
step: Number of elements in row after reshaping.
"""
result = []
for i in range(0, len(array), step):
result.append(array[i:i+step])
return result | 7fde2888e5cd2dca4ef498f7c943441044bafa93 | 389,543 |
def non_hydrogen_count(mol):
"""Non-hydrogen atom count """
return sum(1 for _, a in mol.atoms_iter() if a.symbol != "H") | 956ee6b6c1c46bee54a4e42a65550ffba0612091 | 257,639 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.