content stringlengths 39 14.9k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
import requests
def does_exist(url: str) -> bool:
"""Determines if a particular file exists on the server."""
resp = requests.head(url)
if resp.status_code == requests.codes.ok:
return True
return False | b506d654e5a89a1a35911e8c8089bcc442169e6b | 35,830 |
from typing import OrderedDict
def build_fhir_id(key1, value1, key2, value2, key3, value3):
"""
Construct an OrderedDict for ID
:param key1:
:param value1:
:param key2:
:param value2:
:param key3:
:param value3:
:return:
"""
id_info = OrderedDict()
id_info[key1] = valu... | f2db277f21683b3ce910b5398b90e607f9cf6a40 | 35,831 |
def is_basic_type(signature):
"""Returns True if the signature is a basic type
'a', '(', '{', and 'v' are not considered basic types because they usually
cannot be handled the same as other types."""
basic_types = ('b','d', 'g', 'i','n','o','q','s','t','u','x','y')
return signature in basic_types | 5a454a699e6e7c0f89806f3cdba12ce78e42477e | 35,838 |
import re
def correct_namespace(name, api_name, env_name) -> bool:
"""
Checks that a name of a thing we want to create in Apigee matches our namespacing conventions.
e.g. for api_name="canary-api" and env_name="internal-dev"
|--------------------------------------------------------------+--------|
... | 8e812a5e2729779837b85eed29ff9bb7a4a05953 | 35,841 |
def confusion_matrix (dataset,subgroup,targetColumn):
"""Returns the confusion matrix of a dataset with a subgroup."""
total_rows = len(dataset)
#Calculate the complement of the dataset over the subgroup
complement = dataset[~dataset.index.isin(subgroup.index)]
#Elements of confusion matrix
s... | bffd2c8f2aebc7d5e3f41373cfa8291797f122df | 35,846 |
from typing import Any
from typing import Callable
def _map(obj: Any, fn: Callable) -> Any:
"""Recursively maps a function to a nested object.
If the passed object is a list, dictionary, set, or tuple, then all child
elements are recursively mapped.
Args:
obj: The object to map against
... | cecbfec90f2a870624b9f5ff51d5f4b9ed3865c7 | 35,849 |
def str2int(val, base=None):
"""String to integer conversion"""
try:
if isinstance(val, int) or val is None:
return val
elif base:
return int(val, base)
elif '0x' in val:
return int(val, 16)
elif '0b' in val:
return int(val, 2)
... | b4c9a4329670bf28f01b292f1686800f6186d487 | 35,854 |
def get_commun_films(df_movies,Actor1,Actor2):
"""
Function that gives the movies in which two actors have played
Parameters
----------
df_movies : dataframe
IMDb movie database
Actor1 : string
name of the first actor entered by the user via the Tkinter interface
Actor2 : st... | d716138ff19b3a58c668a1f9b050e16bde927ffe | 35,859 |
def echo_worker(data):
"""
Example of worker that simply echoes back the received data.
:param data: Request data dict.
:returns: True, data
"""
return data | d694b301aefdcb1631567b3e1c269b24aa827824 | 35,860 |
from typing import Sequence
from typing import Tuple
def get_default_powerup_distribution() -> Sequence[Tuple[str, int]]:
"""Standard set of powerups."""
return (('triple_bombs', 3), ('ice_bombs', 3), ('punch', 3),
('impact_bombs', 3), ('land_mines', 2), ('sticky_bombs', 3),
('shield',... | 1e125dfe64627b25e56e9f905d4fc8cc1a878684 | 35,863 |
def interpolate(x0, y0, x1, y1, x):
"""Linear interpolation between two values
Parameters
----------
x0: int
Lower x-value
y0: int
Lower y-value
x1: int
Upper x-value
y1: int
Upper y-value
x: int
Requested x-value
Returns
-------
int... | 082cc92c4c170dbba479e396731326e450b5d765 | 35,868 |
import requests
def requests_adapter(url: str) -> dict:
"""An adapter that encapsulates requests.get"""
resp = requests.get(url)
return resp.json() | f57c03cf6573ba6043390a8a099125ecf3ba3315 | 35,874 |
import math
def math_round(number: float, decimals: int = 0) -> float:
"""Округлить математическиим (не банковским) способом.
Работает обычным математическим образом, в отличие от встроенной функции round(),
которая использует банковское округление.
:param number: число, которое требуется округлить
... | a223494af85a016ed8b1c0e3ffe6aa9593bd8da2 | 35,878 |
def indent_string(string, indent=' ', include_first=True, include_last=False):
"""
Indent a string by adding indent after each newline.
:param string: The string to indent
:param indent: The string to use as indentation
:param include_first: Also indent the first line of the string (before the firs... | 95bc16848fe6e095677f4a95a517b43fb10fd315 | 35,879 |
def generate_TF(corpus, corpus_dict):
"""
Function to generate TF for queries.
:param corpus: Corpus of words in the queries.
:param corpus_dict: Mapping of query number and its corpus.
:return tf_dict: Term Frequency Mapping.
"""
tf_dict = dict()
for document_number, words_in_it in cor... | 4967f6968c4974fa3c8b400c9fec1a6b39f5ae43 | 35,881 |
def build_feature_dict_mapper(feature_names):
"""Build a function for tf.data.Dataset.map.
Args:
feature_names: List of feature names.
Returns:
A function converting tuples into (dictionary of features, label).
"""
def mapper(*tuple_args):
d = {}
for i in range(len(feature_names)):
d[... | 9b3837cf3d1ff7bcc39242d660c255863a8dc98c | 35,884 |
def reshape_axis(ax, axis_size_pix):
"""reshape axis to the specified size in pixels
this will reshape an axis so that the given axis is the specified size in pixels, which we use
to make sure that an axis is the same size as (or an integer multiple of) the array we're
trying to display. this is to pre... | 5a029753014ebb4af4683be3a1d50ae4130ccc32 | 35,885 |
import csv
import io
def read_model_analysis_csv(csvfile):
"""
Reads CSV generated from a spreadsheet of the same form as 'EXAMPLE template results spreadsheet v2 warming levels'.
Returns the model analyses as a list of Dicts.
"""
# List of keys corresponding to the column headings for v2 of the ... | 62a32a66ba56c487ee2ef12ac1518f4be92f04d5 | 35,894 |
import typing
import asyncio
async def exec_as_aio(
blocking_fn: typing.Callable[..., typing.Any], *args: typing.Any
) -> typing.Any:
"""Asynchronously run blocking functions or methods.
Args:
blocking_fn (Callable[..., Any]): The blocking function/method.
Returns:
Any: The return va... | 0f8d0eb069ad8f33534931b4b9134486a641fa47 | 35,903 |
import re
def did_parse(did):
"""
Parse a DID into it's parts.
:param did: Asset did, str.
:return: Python dictionary with the method and the id.
"""
if not isinstance(did, str):
raise TypeError(f'Expecting DID of string type, got {did} of {type(did)} type')
match = re.match('^di... | a0ed14d68aac933ead173b53ba26a80c1e6c83fd | 35,905 |
def max_standard_deviation(window):
"""Return the maximal spatial velocity standard deviation in a window over all the times."""
max_std = 0
for i in range(window.shape[1]):
current_std = window[:, i].std()
max_std = max(max_std, current_std)
return max_std | dd99de7170ce942b70b34f44cc50aff7da03091c | 35,910 |
def get_bounds(params):
"""Gets the bounds of the parameters
A list of ``(min, max)`` pairs will be returned. And the None value for the
unbound parameters will be kept.
:param params: An iterable for the model parameters.
:returns: The list of bounds for the parameters.
:rtype: list
"""
... | 5fffe3c863e57de8f141b7a742e1b6ac65c8fc94 | 35,925 |
def baskets(items, count):
""" Place list itmes in list with given basket count.
Original order is not preserved.
Example:
> baskets([1,2,3,4,5,6,7,8, 9, 10], 3)
[[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]]
"""
_baskets = [[] for _ in range(count)]
for i, item in enumerate(items):
... | dabf82b5229595276fd6dc2cf6bdb7c104d9867a | 35,934 |
def dates(df, params, field='dates'):
"""
Return an inclusive sliced portion of the input data frame based on a min and max date
Args:
df (pd.DataFrame): The input data
params (Tuple[dt.datetime, dt.datetime]): Dates, must be in order of MIN, MAX
field (str): The date field used to ... | 4bf52d35d4c9d6601edea6eeb621163d318dd975 | 35,935 |
def get_hyperparams_wd(optimizer_def):
"""
returns the weight decay stored in the hyperparameters
"""
return optimizer_def.hyper_params.weight_decay | f9a097e3d6c9a8963bc6cf6a8a96c334551c730b | 35,937 |
def sobloo_opensearch_params(latitude, longitude, max_cloud_cover):
"""Build Sobloo open search query string to get the latest Sentinel-2 image
with minimal cloud cover at the requested location
Arguments:
latitude {float} -- latitude of search point
longitude {float} -- longitude of search... | 1ee722c56e42c6594dd3476825af345a78481c1b | 35,943 |
from pathlib import Path
def data_file(path):
"""
Get a path to a file in the local 'data' directory.
"""
my_location = Path(__file__).parent
return str(my_location / 'data' / path) | 89b41133878c0b22ae7c1f3ee65cae20a96f58f9 | 35,945 |
def _map_boolean_to_human_readable(boolean, resource, token):
"""
Map a boolean into a human readable representation (Yes/No).
:param boolean: boolean with the value that we want to transform
:param resource: resource containing all the values and keys
:param token: user token
"""
if boolea... | 380a9ff38cc5999a9e062b2487a7e54158c02a69 | 35,946 |
def json_path_components(path):
"""Convert JSON path to individual path components.
:param path: JSON path, which can be either an iterable of path
components or a dot-separated string
:return: A list of path components
"""
if isinstance(path, str):
path = path.split(".")
retur... | 0b9a1c7e68b368b04616d7a29254bd4509673d51 | 35,950 |
def find_kth(nums1, nums2, k):
"""find kth number of two sorted arrays
>>> find_kth([1, 3], [2], 2)
2
>>> find_kth([2], [1, 3], 1)
1
>>> find_kth([1, 3], [2], 3)
3
>>> find_kth([1], [2], 1)
1
>>> find_kth([1], [2], 2)
2
"""
# assume len(nums1) <= len(nums2)
if le... | 8471f79de2d388ff942482f758076a5ebe39164c | 35,952 |
def inertia_update(iteration,n_iterations,wmin,wmax):
"""
Time varying acceleration inertia:
w^k = wmax - (wmax - wmin)/kmax * k
Parameters:
iteration: int
The number of the iteration
n_iterations: int
The number of total iterations
wmin: float
... | 20831f2eeac8cdb269674188704a0944469f66d1 | 35,957 |
def crop_scores(player):
"""Select specific only parts of the full score object
Args:
player (dict): player object from which to read the score
Returns:
score (dict): filtered scores from the game data
"""
score = {}
# if there is no active username, nobody finished, but the
... | 5466cf39b59ce4b49b7da3ff2c355b2d7b46455c | 35,959 |
def get_header_item_group(header, group):
"""
Filter header and return list of items of a specific header
group (e.g. 'CTYPE').
Return empty list when unable to find @group in @_header.items().
Parameters
----------
header: dictonary
The dictonary that contains the header of the SST... | 5417756a05186fa17f67b83e520d8b035a43a21c | 35,961 |
def title(title):
""" Generate reST title directive.
:Examples:
>>> section('Page title')
'''
==========
Page title
==========
<BLANKLINE>
'''
"""
title_len = len(title)
return '\n'.join((
'',
'=' * title_len,
title,
'=' * title_len,
... | 86bd46cc28ad704d9f90844511574a912f6df9a5 | 35,964 |
from functools import reduce
def concat_cols(df, cols, delim):
"""
Concatenate columns in a dataframe with a
delimiter.
Args:
df (DataFrame): input DataFrame
cols (list-like): columns to concatenate
delimiter (str): delimiter to join column values
Returns:
Series... | 5088848fbc6337d8ae251dedf3b7c4d56df4192c | 35,967 |
import inspect
def import_subclass_from_module(parent_cls, imported_module):
"""
Import class(es) from the Python module `imported_module` if
it is a subclass of a parent class, `parent_cls`
"""
child_classes = []
for cls_name, cls_path in inspect.getmembers(
imported_module, inspe... | e8696663b821c0a572df9875a3dd8bdfb101fc7a | 35,969 |
def update_dictionary(default_dict, overwrite_dict=None, allow_unknown_keys=True):
"""Adds default key-value pairs to items in ``overwrite_dict``.
Merges the items in ``default_dict`` and ``overwrite_dict``,
preferring ``overwrite_dict`` if there are conflicts.
Parameters
----------
default_di... | 503934d42362ea2b7ff7732bb9f752de45140898 | 35,974 |
def find_in_mapping(sequence, key, value):
"""
Search a sequence of mappings for one with a matching key-value pair.
Only searches one level deep.
Args:
sequence (list(dict)): Sequence of mappings.
key: Key to match.
value: value to match
Returns:
The first matchin... | 105738b193b9003c108726a05c65e2b41295dde4 | 35,977 |
from typing import List
from typing import Dict
import logging
def decode_one_sequence_label_to_span(sequence_label: List[str]) -> List[Dict]:
"""
对 BIO 序列进行解码成 List. 例如:
["B-Per", "I-Per", "O", "B-Loc"] ->
[ {"label": "Per", "begin": 0, "end": 2},
{"label": "Loc", "begin": 3, "end": 4} ]
:... | 5df60c24b5ab1276568d2193bf276ef3ef8e54fd | 35,979 |
from typing import Any
def cast_number(number: Any) -> float:
"""Cast to a float"""
number_float = float(number)
return number_float | d43bd8db72a4817ab18e5c24fe9225a3b1702d00 | 35,980 |
def readInput(inFile):
"""
Reads in the key values.
@ In, inFile, Python file object, file containing inputs
@ Out, (x,y,z), tuple(float,float,float), input values
"""
x, y, z = 0,0,0
for line in inFile:
var,val = line.strip().split('=')
if var.strip() == 'x':
x = float(val)
elif... | 68a194375d880070f5404318b4ebfccb62ae479f | 35,981 |
def get_label(labels, index):
""" Gets label if exists, otherwise returns label number """
if index < len(labels):
return labels[index]
else:
return '#%d' % (index) | 40ca42a95c5ac3252aa63e84708177b87c538411 | 35,986 |
def formatHowpublished(howpublished):
"""How something strange has been published. The first word should be
capitalized. """
return howpublished.capitalize() | 480c6b3f7f08a3e79c496b1a40a65390175893d6 | 35,989 |
def apply_at(
arr,
func,
mask=None,
else_=None,
in_place=False):
"""
Apply a function to an array.
Warning! Depending on the value of `in_place`, this function may alter
the input array.
Args:
arr (np.ndarray): The input array.
func (callable... | 1af1e62f6e1ede616508d017bd14ca0fcd4556d6 | 35,990 |
import re
def parse_data_refs(tf_content):
"""
Look for references to other remote states. These references look like this:
gcp_org_id = "${data.terraform_remote_state.foundation.org_id}"
"""
result = []
p = re.compile(r'data\.terraform_remote_state\.([_a-z][_\-0-9a-z]*)\.')
result = p.findall(tf_conten... | 63af09c6ac830b6d822629cfeb8a02f50b366d56 | 35,991 |
import struct
def read_varint(buffer: bytearray) -> int:
""" Parse a varint, read bytes are consumed.
"""
i, = struct.unpack('<B', buffer[:1])
if i < 0xfd:
del buffer[:1]
res = i
elif i < 0xfe:
res, = struct.unpack('<H', buffer[1:3])
del buffer[:3]
elif i < 0xff... | ae3a41d4efa8b13a7fda86fc39acc0d396a22b77 | 35,992 |
def custom_formatwarning(msg, *a):
"""Given a warning object, return only the warning message."""
return str(msg) + '\n' | 5da47d79d37c79d3072aedb754f9fcac13baf0b1 | 35,995 |
def calculate_interest_amount_in_years(starting_amount, number_of_years, interest_rate, stipend_rate):
"""
After X number of years, how much would I have in the bank?
:param starting_amount: The amount of money the bank has to start with.
:type starting_amount: double
:param number_of_years: The amo... | ea6a7e503c92f4b65e1ebaba8cca1bfce89ceff1 | 35,996 |
def _shadow_level(kernel, threshold, sensitivity):
"""
Calculates the greyscale shadow level for a given kernel,
the diode threshold and the diode sensitivity.
:param kernel: pooling kernel for down scaling
:type kernel: float array (2 dimensional)
:param threshold: thresholds for... | 6aa09975ab8933d812ca599fae2d6116fc9190e9 | 36,002 |
def combine_ctrlpts_weights(ctrlpts, weights=None):
""" Multiplies control points by the weights to generate weighted control points.
This function is dimension agnostic, i.e. control points can be in any dimension but weights should be 1D.
The ``weights`` function parameter can be set to None to let the ... | b55b77159ec04aa287314f8771f0056c820c5cad | 36,004 |
import torch
def diagonal_mask(dim, num_diagonals):
"""Creates a binary mask with ones around the major diagonal defined by
`num_diagonals`.
Parameters
----------
dim : int
dimension of the mask matrix
num_diagonals : int
number of diagonals. The number gets rounded up to the ... | 2c6653e36da449bfba7321965b8091952e316263 | 36,006 |
def locate_card_linear(cards, query):
"""Linear search for `query` in a desc sorted list `cards`."""
# time complexity: O(N)
# space complexity: O(1)
position = 0
while position < len(cards):
# check if current elem matches the query
if cards[position] == query:
... | c3acb6064228cd0357fceb5804b0f4a500aae780 | 36,009 |
def string_to_tuple( word ) :
"""Convert string word into a tuple suitable for use as a key to a dictionary
of anagrams"""
this_word_list = list(word) # convert the string to a list so we can sort it
this_word_list.sort()
this_word_tuple = tuple( this_word_list ) # Conver the list to a tuple which... | a80ecbdeaa9b9a3d185d00befd6b9a33a970eb73 | 36,011 |
def findCon(name,conList):
"""
Returns the index of a constituent from a list
"""
return next((i for i, j in enumerate(conList) if j == name)) | 3de9c922459175156900649800465a3546e330a1 | 36,015 |
def find_chunk_shape(shape, n_max=None):
"""
Given the shape of an n-dimensional array, and the maximum number of
elements in a chunk, return the largest chunk shape to use for iteration.
This currently assumes the optimal chunk shape to return is for C-contiguous
arrays.
"""
if n_max is N... | 3370be64a4ba13a5d7a3f1e2e85858068202df38 | 36,018 |
import re
def clean_name(text):
"""
Return a cleaned version of a string - removes everything
but alphanumeric characters and dots.
:param str text: string to clean.
:returns: cleaned string.
:rtype: str
"""
return re.sub(r'[^a-zA-Z0-9\n\.]', '_', text) | 51c9663d4d6a7f5b3099fc30b0108e99ac2608d0 | 36,022 |
def remove_non_datastore_keys(form_data):
"""Remove keys not relevant to creating a datastore object."""
form_dict = {k: v[0] for k, v in form_data.items()}
for key in ["csrfmiddlewaretoken", "name", "type", "owner"]:
form_dict.pop(key, None)
return form_dict | 0d0e561cb24eaf7cb3ee77060ed2149c55140812 | 36,025 |
import re
def _parse_boolean(value):
"""
Attempts to convert a value to a boolean and returns it.
If it fails, then it raises an Exception.
:param value: a value
:return: boolean
"""
if re.match("^(on|true|yes|1)$", str(value), re.IGNORECASE):
return True
if re.match("^(off|f... | 3090f60eaccbfc65ecb3cf6f956ab3e57d572798 | 36,026 |
import torch
def padding_mask(x_lens):
"""
transform lengths of samples to a binary mask.
inputs:
- x_lens: length of each sample in minibatch. # tensor # (batch_size, )
outputs:
- mask: 1-0 binary mask. 1 means valid and 0 means invalid.
# tensor # (batch_size, longest_t... | ea9e3c06d61f5d9b19795a59dbb2956e8bdb4385 | 36,033 |
def certificate_files(create_certificate):
"""Returns a dict with the certificate files
The dict contains the following keys:
caKeyPath
caCrtPath
clientKeyPath
clientCrtPath
"""
return create_certificate | b30ae8b72b5b933b8f7cb973c1b5022a9e97ed53 | 36,035 |
def get_seed(seed_id):
"""
This function provides the random seed.
:param seed_id: int
the seed_id is the 'seeds' vector index
:return:
"""
seeds = [1859168769, 1598189534,
1822174485, 1871883252, 694388766,
188312339, 773370613, 2125204119,... | d568962485aa02f88ed0b12a2b6dcfb397773ec2 | 36,036 |
import json
def format_json(data):
"""
Returns a human-formatted JSON
"""
return json.dumps(data, sort_keys=True, indent=2, separators=(',', ': ')) | a0ea13af5e95d5cd9879de0cdf5055d6fa1dd4c8 | 36,038 |
def find_request_end_token(data):
"""Find token that indicates that request is over."""
lines = data.splitlines(True)
if not lines:
return False
elif 'command_list_ok_begin' == lines[0].strip():
return 'command_list_end' == lines[-1].strip()
else:
return lines[0].endswith('\n... | 5b09d57fdb020940f6d16183c0ae3d84dcb2ce2c | 36,046 |
def set_invalid(field, render_kw=None):
"""
Returns *render_kw* with `invalid` added to *class* on validation errors.
Set (or appends) 'invalid' to the fields CSS class(es), if the *field* got
any errors. 'invalid' is also set by browsers if they detect errors on a
field.
"""
if render_kw ... | 5e32005e1a5405b3ba86293c986e6368c177ad40 | 36,047 |
from datetime import datetime
def convert_unix_to_date(d):
""" Convert millise since epoch to date formatted MM/DD/YYYY HH:MI:SS """
if d:
dt = datetime.utcfromtimestamp(d / 1000)
return dt.strftime('%m/%d/%Y %H:%M:%S')
return 'N/A' | e587839c4c8cc6464704d7b2ce1b4ae0bf9db302 | 36,052 |
def add_key_arguments(parser):
"""
Adds the arguments required to create a new key to the parser given as a parameter
Args:
- parser: Parser where to add the key parameters
+ Type: argparse.ArgumentParser
Return:
- group: Argument group containing all the key... | 329a90dbf639283e62765690cfe724038ce61bbd | 36,053 |
def weight_normalization(weight1, weight2):
"""
A function to normalize the weights of each modality so the weights' sum is 1 for each pixel of the image
:param weght1: The weight of madality 1, a grayscale image
:param weight2: The weight of modality 2, a grayscale image
:return: Two weights, weig... | 0e04960ba7baec3e0e657117ebb5c3da8c9bd845 | 36,054 |
import uuid
def CreateShoppingCampaign(client, budget_id, merchant_id):
"""Creates a shopping campaign with the given budget and merchant IDs.
Args:
client: an AdWordsClient instance.
budget_id: the str ID of the budget to be associated with the shopping
campaign.
merchant_id: the str ID of the... | 208b2ad37d2fda5ee5b85827029597ed8bc6801b | 36,057 |
from typing import Dict
from typing import Tuple
def get_conversion_endpoints(options: Dict) -> Tuple[str, str, Dict]:
"""Returns conversion direction with endpoints
(Text to DNA) or (DNA to Text)."""
_from, _to = "TEXT", "DNA"
if options["convert_to"] == "DNA":
_from, _to = "TEXT", "DNA"
... | 02fd713646efb72959d3b42dd1f2c3f9de343da8 | 36,063 |
def dice_coefficient(outputs, targets, eps=1.0):
"""Calculates the Dice coefficient between the predicted and target masks.
More information:
- https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient
Args:
outputs (torch.tensor): Outputs of the model (N x 1 x H x W).
targets (torch... | b74d2ae00e30bc1127c098a17b427e9fcc3ac30b | 36,064 |
def calculate_center(shape: tuple):
"""
Calculate and return the center point of ``shape``.
:param shape: A tuple (width, height) of odd numbers
:return: A ``tuple`` (x, y) containing the center points coordinates
"""
if any(d%2 == 0 for d in shape):
raise ValueError("width and height ... | dbe2485b39c7670adfb832d3626e7e9106e5b2c0 | 36,070 |
def format_datetime(datetime_):
"""Convert datetime object to something JSON-serializable."""
if datetime_ is None:
return None
return datetime_.strftime('%Y-%m-%dT%H:%M:%SZ') | 16fbcff4737ec985405c8267b74e9085f56d06df | 36,071 |
def _set_key2sl(key):
"""Convert a key part to a slice part."""
if isinstance(key, int) or key is Ellipsis:
return key
elif isinstance(key, slice):
# Forbid slice steps
if key.step is not None:
raise ValueError("farray slice step is not supported")
return key
... | c735f009ef5576f4eaf293152b715aed8d20f965 | 36,073 |
def sb_xs_compare(sb, sheet):
"""Compute the absolute and percentage difference between the maximum fields of one CrossSection in a SectionBook and all the other CrossSections
args:
sb - SectionBook object
sheet - sheet string of the CrossSection in sb that should be
compared wit... | 491dd1ada0b23882d2f9de474b2ccb56c36eb705 | 36,079 |
def percent_change(a,b):
"""
Returns the percentage change from a to b.
"""
return float((b - a)/a) | 34bf312683f72107404919bb01b9f01464e23c47 | 36,081 |
import torch
def make_cuda(tensor):
"""Turn the tensor into cuda if possible."""
if torch.cuda.is_available():
return tensor.cuda()
return tensor | 14f05f1f27a0f846448ac89a2e8371b4acac9711 | 36,083 |
def check_rent_history(rent_list, username):
"""
return farm ids that the given username has rented before
"""
farm_rent_before = []
for rent in rent_list:
if rent.get('username') == username:
farm_rent_before.append(str(rent.get('farm_id')))
return farm_rent_before | b480651ab7e17673df91c7e34dc5d70f8f360ac7 | 36,086 |
def db2lin(x):
"""From decibel to linear"""
return 10.0**(x/10.0) | 653fb36943baeb393d4cec07544df95a230a5880 | 36,088 |
def remove_nan_columns(dataframe):
"""Removes columns of data frame where any value is NaN.
Args:
dataframe (pandas.DataFrame): Input dataframe.
Returns:
tuple: tuple containing:
pandas.DataFrame: Dataframe with columns containing NaN values removed.
numpy.array: A... | 058c0469dd93a71866e1a09d8dd16b80379c7d15 | 36,090 |
import torch
def kl_divergence(q, p):
"""Calculates the KL divergence between q and p.
Tries to compute the KL divergence in closed form, if it
is not possible, returns the Monte Carlo approximation
using a single sample.
Args:
q : torch.distribution
Input distribution (posterior... | 3d0be3599332840057f07d9bee71681f4a710557 | 36,096 |
import re
def ipv4_address_validator(addr):
"""
Regex to validate an ipv4 address.
Checks if each octet is in range 0-255.
Returns True/False
"""
pattern = re.compile(
r"^([1]?\d?\d|2[0-4]\d|25[0-5])\.([1]?\d?\d|2[0-4]\d|25[0-5])\.([1]?\d?\d|2[0-4]\d|25[0-5])\.([1]?\d?\d|2[0-4]\d|25[0... | 6204d9d54536e510b5556d13ee259ec3d7b3fc95 | 36,098 |
def plot_hist(self, parameters=None, mean_line=False, **kwds):
"""
Make a histogram of the WaterFrame's.
A histogram is a representation of the distribution of data.
This function calls pandas.DataFrame.hist(), on each parameter of the
WaterFrame, resulting in one histogram per parameter.
Param... | 5c927a170fff37691135dd367f0b42247d4aa3e1 | 36,101 |
def isDelimited(value):
"""
This method simply checks to see if the user supplied value has delimiters.
That is, if it starts and ends with double-quotes, then it is delimited.
"""
if len(value) < 2:
return False
if value[0] == '"' and value[-1] == '"':
return True
else:
... | 4a89211c27f6cf826b3b29cc718b0e00a4184a0f | 36,103 |
import types
def asmodule(module):
"""
Return the module references by `module` name. If `module` is
already an imported module instance, return it as is.
"""
if isinstance(module, types.ModuleType):
return module
elif isinstance(module, str):
return __import__(module, fromlis... | 1e2def23f770f9bc84aa8af3273f830fedea3282 | 36,105 |
def flatten(list_of_lists):
"""Takes a list of lists and turns it into a list of the sub-elements"""
return [item for sublist in list_of_lists for item in sublist] | 11df76be33e96295e4e5230873368be707ae032f | 36,106 |
def leftmost(n):
"""
Return the leftmost item in a binary tree.
"""
while n.left is not None:
n = n.left
return n | f22dbb611a088e52c37002c1dad3a704033145e0 | 36,107 |
def sdfGetPropList(mol):
"""
sdfGetPropList() returns the list of all property names in molecule mol
"""
sdfkeyvals = mol["keyvals"]
return [pair[0] for pair in sdfkeyvals] if sdfkeyvals else [] | da897a54a4e0bfc59ca2bd78a2d39d0b64b32db1 | 36,112 |
from typing import Dict
def str_dict_to_bytes(str_dict: Dict[str, str]) -> Dict[bytes, bytes]:
"""
Converts the key and the value of a dict from str to bytes.
"""
out = {}
for key, value in str_dict.items():
out[key.encode()] = value.encode()
return out | 43b62237b9c80c6e613c363aace17324647086b9 | 36,117 |
def gcd(x, y):
"""
Euclid's algorithm
- If n divides m evenly, then n is the GCD. Otherwise the GCD is the GCD of n and the remainder of m divided by n.
"""
if x % y == 0:
return y
else:
return gcd(y, x%y) | db31a4b36929c9b7e83508beee724b94eea27a12 | 36,119 |
def string_breakdown(the_string :str) -> list:
"""Accepts a string and returns a list of tuples of form [(char, count)]"""
checked = []
components = []
for char in the_string:
if char not in checked:
count = the_string.count(char)
checked.append(char... | 2283297aff80d548625930482249a31284e49eae | 36,121 |
def read_lines(path):
"""Return list of lines comprising specified file.
Newlines at the end are included.
"""
with open(path, "r") as file:
return file.readlines() | e0986c649ab6b64c665e8aa887c2625ad21b77be | 36,122 |
from typing import List
def _sampling_from_alias_wiki(
alias: List[int],
probs: List[float],
random_val: float,
) -> int:
"""
Draw sample from a non-uniform discrete distribution using Alias sampling.
This implementation is aligned with the wiki description using 1 random number.
:param a... | c67d2d698ace15c798cda51049b7ddd880c48a71 | 36,124 |
def get_item_attr(idmap, access):
"""
Utility for accessing dict by different key types (for get).
For example::
>>> idmap = {(1,): 2}
>>> get_item_attr(idmap, 1)
2
>>> idmap = {(1,): 2}
>>> get_item_attr(idmap, {"pk": 1})
2
>>> get_item_attr(idmap, (... | abaf1250c34b94393851e7a0764af3bf1c3eb116 | 36,127 |
def round_to_second(dt):
"""
datetime对象round到秒
:param dt:
:return:
"""
res = dt.replace(microsecond=0)
return res | 2be5f589be54bbb113b307d5b38ba5d6e0a13a32 | 36,131 |
from typing import List
from typing import Dict
from typing import Any
def split_docker_list(d_list: List[Dict[str, Any]]):
"""Splits the list of docker in test generation docker and test environment docker.
The split is done by checking for the 'generator' key in the dockers dict defined in the json file.
... | 6c01f81db08270c60d08153448dd8989ff87ad4c | 36,133 |
def round_to_int(number, precision):
"""Round a number to a precision"""
precision = int(precision)
rounded = (int(number) + precision / 2) // precision * precision
return rounded | 75f7b23c3f6426dc3ed0c54f3baa491e3c658a14 | 36,138 |
def mod_exponent(base, power, mod):
"""
Modular exponential of a number
:param base : number which is going to be raised
:param power : power to which the number is raised
:param mod : number by modulo has to be performed
:return : number raised to power and modulo by mod [(base ^ power) % mod... | f0b0de989c8ab38a11ca41126e30234765571ca6 | 36,140 |
import requests
def post_input_file(input_text):
"""Posts a string as a file (multipart/form-data) named 'input' to the
REST API and returns the response.
"""
url = 'http://localhost:5000/parse'
return requests.post(url, files={'input': input_text}) | 4b6613e95f3221e1a92c88d449aa41a562182304 | 36,141 |
def _get_low_pressure(self, g_l, d_l, frac_cells):
"""
Obtains the coefficients of the (projected) lower-dimensional pressures
Parameters
----------
g_l : PorePy object
Lower-dimensional grid.
d_l : Dictionary
Lower-dimensional data dictionary.
frac_cells : NumPy nd-Array
... | 374c812a39aa045b68d70515d62d13aad69655c3 | 36,147 |
def read_from_y(state):
"""Reads the contents of the Y scratch register."""
return state.y_register | f5d6f258f267d4c1392b393ffc680cdf8aa59748 | 36,150 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.