content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
def read_file(path):
"""Returns the entirety of a file's contents"""
with open(path, 'r', encoding="utf-8") as content:
return content.read() | f718df4503093f14359d7dd33e68921667e711d3 | 161,635 |
def linear_predictor(X):
"""A simple linear effect with features 'a' and 'b'."""
return X["a"] + X["b"] | 588e2876ba87b85c61b69fc4568a757100f4cdaa | 341,871 |
def getParameter(parameter, key, default = None):
"""Gets a parameter from a dict, returns default value if not defined
Arguments:
parameter (dict): parameter dictionary
key (object): key
default (object): deault return value if parameter not defined
Returns:
object... | b2146c8964062e64b1a3ada64b6d69e2da978353 | 510,390 |
def get_links(_stories):
"""
function extract links from received stories
:param _stories: list of stories including story title, link, unique id, published time
:return: list of links
"""
# Get the stories titles
links = []
for story in _stories:
links.append(story['link'])
... | 9bbe677f9b8600b232efa3ce78fc4b3edbbe0403 | 365,803 |
def peaks_with_signal(peaks, signal_range):
"""Returns peaks with at least one peak in signal_range
Parameters
----------
peaks: 2D array of peaks
signal_range: tuple(float)
(min, max) of signal window
Returns
-------
peaks_with_signal: 2D np.array
"""
peaks_with_signal... | 4fa8018eb04ce7af62fcac493403e8fd4c5b0930 | 185,893 |
def write_file(file_path, data):
""" Write data to a specified file path by appending """
try:
with open(file_path, 'a+') as fp:
fp.write(data)
return True
except Exception as err:
raise err
return False | 222ab5a288805f32613297cb3f4ca48696f0e41c | 373,432 |
def get_db_login(path='../data/db_details.txt'):
"""Get login details for database from file."""
with open(path, 'r') as f:
lines = [line.strip() for line in f]
login_dict = {i.split(":")[0].strip(): i.split(":")[1].strip()
for i in lines}
return login_dict | 01f2e25a351af6d19d3c93989c00cba7cf3161a8 | 595,982 |
import re
def PostUploadHook(cl, change, output_api):
"""git cl upload will call this hook after the issue is created/modified.
This hook adds extra try bots list to the CL description in order to run
tests on the Windows 10 try bot in addition to CQ try bots.
"""
rietveld_obj = cl.RpcServer()
issue = cl... | b909505b807c79f5a8c08b530cc82bccb9b96338 | 530,070 |
def date_search(df, unix_ts_date):
"""!
Create a filtered DataFrame that only contains entries that occurred on or
past the specified date.
@param df DataFrame: contains the readings from the sensor
@param unix_ts_date Integer: the universal unix time of the starting date from
... | 5d3c0ef7bb47de5d5ce5126f7d564d686cae3dca | 210,466 |
import re
def get_exclamations(text):
""" Count exclamation marks attached to words or standalone"""
ex_count = re.findall("[a-z#]*[!]+", text.lower())
return len(ex_count) | 4f79268d1f868b447c6fac1fdf6985e132ca55af | 99,630 |
def open_path(path, mode='r'):
""" Python < 3.6 doesn't support calling open on a Pathlib Path.
Parameters
----------
path : Path
Pathlib path to open
"""
return open(str(path), mode) | 55d9a49fa5097df279b30ed29d5f82db9f335144 | 159,917 |
import ipaddress
def get_ip(request):
"""
Retrieves the remote IP address from the request data. If the user is
behind a proxy, they may have a comma-separated list of IP addresses, so
we need to account for that. In such a case, only the first IP in the
list will be retrieved. Also, some hosts... | d5e030ca60dfb7400dffab7ed02335e146e4b91f | 480,775 |
def calculate_alpha(mu1: float, mu2: float, a: float) -> float:
"""
Args:
mu1: mean of the first Gaussian random variable :math:`Y_1`
mu2: mean of the second Gaussian random variable :math:`Y_2`
a: this number is calculated using standard deviations and the coefficent of linear correlati... | f3857e7792a4afecb6887f62fbe3ea3ff9596dcd | 484,328 |
def _expr_to_smtlib(e, daggify=True):
"""
Dump the symbol in its smt-format depending on its type
:param e: symbol to dump
:param daggify: The daggify parameter can be used to switch from a linear-size representation that uses ‘let’
operators to represent the formula as a dag or a s... | 8afb21100585f48bc3ccea58e95b02cacfb67338 | 429,255 |
def is_windows_path(path):
"""Checks if the path argument is a Windows platform path."""
return '\\' in path or ':' in path or '|' in path | 88d856751744f0400bd4db09013492215b363c2a | 638,591 |
def no_validation(s):
"""anything goes"""
return s | 7ab880b9e85577b600cf583a72d0680c339f06be | 159,160 |
from pathlib import Path
def get_datapath_base(data_type: str, filename: str) -> Path:
"""Return the path to the footprints test data file"""
return Path(__file__).resolve(strict=True).parent.joinpath(f"../data/{data_type}/{filename}") | 9bccb92b1c4a5dbaa625b2fa52b3c77163104c11 | 38,200 |
def ensure_chosen_alternatives_are_in_user_alt_ids(choice_col,
wide_data,
availability_vars):
"""
Ensures that all chosen alternatives in `wide_df` are present in the
`availability_vars` dict. Raises a help... | 73fe39be549de10a5eac9502ba9ad0e34aa633a8 | 310,372 |
def squeeze_generic(a, axes_to_keep):
"""squeeze_generic
Numpy squeeze implementation keeping <axes_to_keep> dimensions.
Parameters
----------
a: numpy.ndarray
array to be squeezed
axes_to_keep: tuple, range
tuple of axes to keep from original input
Returns
----------
... | 1aaadaa73b533256a6a5206b3629044ad62eb9d2 | 404,317 |
def legacySTAkwargs(**kwargs):
"""Provide support for legacy SpikeTrainArray
kwargs. This function is primarily intended to be
a helper for the new STA constructor, not for
general-purpose use.
kwarg: time <==> timestamps <==> abscissa_vals
kwarg: data <==> ydata
kwarg: unit_i... | 516f69c358d59f415f54ffa17ca585f7436e1531 | 323,521 |
import requests
import re
def get_urls(root_url, regex_str):
"""
Scrape all regex matches from a web page. Used to extract data from NOAA
servers since their html structure isn't amenable to beautiful soup.
"""
html = requests.get(root_url).text
date_re = re.compile(regex_str)
url_suffixes... | 0f76bcde6380b978cb6fa66c0dee99efa331d640 | 218,636 |
def calcul_distance_euclidienne(point_1, point_2):
"""
Calcul de la distance euclidienne au carré entre `points_1` et `point_2`.
Soient `point_1` = [x1, x2, ..., xn] et `point_2` = [y1, y2, ..., yn]
alors distance = somme de i=1 à n de (xi - yi)**2 .
Paramètres
----------
point_1 : array_l... | 8e51009db436a5187bbc1e0a76672f9680fe2115 | 147,112 |
from typing import List
def unity2bullet_position(unity_position: List[float]):
"""Converts from unity to bullet position.
Args:
unity_position: The xyz position in unity coordinates.
Returns:
bullet_position: The xyz position in bullet coordinates.
"""
x, y, z = unity_positi... | 9bb221dcde0d222e269f9a741bb51913b2735acf | 522,885 |
def create_link(url):
"""Create an html link for the given url"""
return (f'<a href = "{url}" target="_blank">{url}</a>') | 77a5375369be2be140a69a4521c50a92cee2d5ed | 704,787 |
def add_two(val):
"""
Add 2 to a number.
Parameters
----------
val: number
The number to which two should be added.
Returns
-------
The number plus two.
"""
return val + 2 | a1c32a975841518a5fd70a745859839aad743635 | 201,134 |
def fillna_column(df, col_name, val):
"""Replace all N/As in a given column with a given value.
Doesn't mutate the original dataframe but returns a new dataframe.
"""
result = df.copy()
result[col_name] = result[col_name].fillna(val)
return result | 20496f87cfe97a587872f0f932486e45557dd6c6 | 286,958 |
from typing import Tuple
def overlay_onto_background_image_bboxes_helper(
bbox: Tuple, overlay_size: float, x_pos: float, y_pos: float, **kwargs
) -> Tuple:
"""
The src image is overlaid on the dst image offset by (`x_pos`, `y_pos`) & with a
size of `overlay_size` (all relative to the dst image dimens... | c3ba4f1e53c0ddbd892dfd0de7c8880156cedc9c | 376,729 |
from datetime import datetime
def read_log_file(path):
"""
Parse a LabView log file and return a dictionary of the parameters.
Parses the text of a file line-by-line according to how they're written. Several values present in only newer
log files are attempted and excepted as None. Failure to parse o... | b9f060d4392267ef5862dd4ff44ea8a62be203ef | 116,152 |
def game_over(player_decks):
"""
Determines if either of the player decks are empty
and if so, game is over.
Parameters:
player_decks -
Decks for each player
Returns:
True if either deck is empty, False otherwise
"""
return_value = Fa... | 7806a483b4c5decc5b7ab7ff3bd909547da7a3e4 | 104,078 |
def normalized_coords_from_coords(coords: tuple, shape:tuple) -> tuple:
"""Return the normalized coordinates in [0, 1] for coordinates in
[0, shape]. Note that a coordinate (normalized or otherwise) centered on
pixel 0 should be slightly larger than 0."""
return tuple([c/s for (c,s) in zip(coords, shape... | 72bf0dd456390c25a50bde0326be8b22d0a73ac7 | 247,545 |
def human_readable(millis):
"""
Take a number of milliseconds and turn it into a string with the format
"min:sec.millis".
"""
if not isinstance(millis, int):
raise TypeError('millis must be a positive integer')
if millis < 0:
raise ValueError('millis must be a positive integer... | bf58816ea3cb31495268a440fd400abb58e5edba | 397,515 |
def statusError(pkt):
"""
Grabs the status (int) from an error packet and returns it. It retuns -1
if the packet is not a status packet.
"""
if pkt[7] == 0x55:
return pkt[8]
return -1 | 03322c97e3b1563bc2dec9498317fc7672ca51c8 | 112,904 |
def value_left(self, right):
"""
Returns the value of the right type instance to use in an
operator method, namely when the method's instance is on the
left side of the expression.
"""
return right.value if isinstance(right, self.__class__) else right | f28c2f0548d3e004e3dd37601dda6c1ea5ab36f6 | 709,624 |
import mpmath
def invsf(p, loc=0, scale=1):
"""
Inverse survival function of the logistic distribution.
"""
with mpmath.extradps(5):
p = mpmath.mpf(p)
loc = mpmath.mpf(loc)
scale = mpmath.mpf(scale)
x = loc + scale*(mpmath.log1p(-p) - mpmath.log(p))
return x | a50fd557527d5a1c65892bb901f4fbe2851dc43f | 498,032 |
from typing import List
from typing import Dict
from typing import Tuple
import collections
def get_dataset_stats(sample_list: List[str],
sample_to_study: Dict[str, str],
sample_metadata: dict,
sample_to_label: Dict[str, str]) -> Tuple[collections.Coun... | 4fb7eb10c8624ca028690fb48d7a6eb6099fb6e7 | 461,510 |
import math
def probability(ec, en, t):
"""
Probability function
:param ec: current energy
:param en: next energy
:param t: temperature ratio
:return: probability value
"""
return math.exp((ec - en) / t) | c055081cd93473ecf4abeab1c8b5cc36fb38f0a4 | 13,930 |
def selectNodeByAttributeValue(graph, attr_field, attr_value):
"""
Returns a list of nodes filterd by the given attribute field and corresponding value.
Note: To select a single unique node. You must define a unique attribute value in your layer.
PARAMETER(S)
: graph : A networkx graph object.
... | 41a89f72e9635c252bca0517878ad264e8876c05 | 527,673 |
def get_path(whereis_output):
"""Returns the full path of a command from the output of a whereis command."""
if len(whereis_output) == 1:
data = whereis_output[0]
else:
data = whereis_output[1]
more_data = data.split()
if len(more_data) < 2:
return ""
else:
return more_data[1] | 5ebfe3a8271111e156153bc74a72eae920ed5d24 | 235,917 |
def get_submission_files(jobs):
"""Return the filenames of all jobs within a submission.
Arguments:
jobs: jobs retrieve filenames from
Returns:
array of all filenames within the jobs given"""
job_list = []
for job in jobs:
if job.filename not in job_list:
job_li... | b823202ae9afd6a8791f0d73e6d9a996e848e8e1 | 458,246 |
def VLOOKUP(table, **field_value_pairs):
"""
Vertical lookup. Searches the given table for a record matching the given `field=value`
arguments. If multiple records match, returns one of them. If none match, returns the special
empty record.
The returned object is a record whose fields are available using `.f... | 2a865196cd528593b757aded7ca58fbbe0cd862a | 106,382 |
import math
def determine_max_possible_base_size(h: int, w: int, crop_sz: int) -> int:
"""Given a crop size and original image dims for aspect ratio, determine
the max base_size that will fit within the crop.
"""
longer_size = max(h, w)
if longer_size == h:
scale = crop_sz / float(h)
... | eaf1a908d7217d398fd46ea93a7d6cea9c8e15b2 | 546,026 |
import secrets
def randbelow(exclusive_upper_bound: int) -> int:
"""Return a random int in the range [0, n)."""
return secrets.randbelow(exclusive_upper_bound) | 06bf74858cb0ff057cae64351159d5198fd895b0 | 428,428 |
def rprpet_point(pet, snowmelt, avh2o_3, precip):
"""Calculate the ratio of precipitation to ref evapotranspiration.
The ratio of precipitation or snowmelt to reference
evapotranspiration influences agdefac and bgdefac, the above- and
belowground decomposition factors.
Parameters:
pet (flo... | 94473f9dc1dab4dffaf8a7556ed4109a3a7497a1 | 660,122 |
import ast
def can_eval(s):
"""Returns True if the string can be evaluated."""
if not s:
return False
try:
ast.parse(s, mode="eval")
except SyntaxError:
return False
else:
return True | cca587373ad5c9d57a6f54b1e15d95928df39203 | 155,104 |
def digits_to_number(digits, running=0):
"""Convert a list of digits to an integer"""
if len(digits) == 0:
return running
else:
r = (running * 10) + int(digits[0])
return digits_to_number(digits[1:], r) | 85dc57d4611578dcf65d79615bce5ee7a449579f | 487,367 |
def binary_search(val, grid):
"""
Binary search that returns the bin index of a value `val` given
grid `grid`
Some special cases:
`val` < min(`grid`) --> -1
`val` > max(`grid`) --> size of bins
`val` = a grid point --> bin location whose upper bound is `val`
... | 0b26653c30ccf94eb062aa5fda685a220a30de8c | 74,601 |
def parse_query(query_to_parse):
"""
Converts a comma or space-separated string of query terms into a list to use
as filters. The use of positional arguments on the CLI provides lists (which
we join and resplit to avoid any formatting issues), while strings passed
from pssh.py just get split since ... | edd1cb6b42cd895a2eeb5f4f024dc1f2c92743af | 48,941 |
def compute_spiral_diagonal_sum(first_elem, loop_wh):
"""
Compute the sum of the four diagonal elements for the given loop
first_elem: First element (in the right-most, second-down element of this loop)
loop_wh: Width / height of the spiral's loop to compute the diag-sum
return: sum of the four diag... | 42ec7f1d95afbcb84e8bf90415803e1f950a2c01 | 339,788 |
import calendar
def date_format(date_ymd: str) -> str:
"""Convert date to IMAP SEARCH Command acceptable format.
Args:
date_ymd (str): The date in `YYYY-MM-DD` format.
Returns:
str: The date in `DD-MM-YYYY` format and the month replaced with its
abbreviated form.
"""
... | 49e494500e75460a79b52d5d9f95424e128923ae | 570,337 |
def calculate_monthly_average(total: int, sales: list[list[str]]) -> float:
"""Calculate the monthly sale average."""
return total / len(sales) | e562fd6e7692cfc9ef7c72acce3867b21d804152 | 356,928 |
def mask_out_bits(segbits, mask, tags_to_mask=None):
"""
Given a set of bits and a list of tags to affect (optional) removes all
the bits from each tag that are present (and equal) in the masking set.
"""
if tags_to_mask is None:
tags_to_mask = segbits.keys()
# Mask out matching bits
... | 5d8358b225e4e28e668eab4d4a5f9b79fec3bd23 | 551,891 |
def has_recent_snapshot(webcam, latest_snapshot):
"""
Returns a tuple with a boolean telling whether the webcam image was updated
after latest_snapshot, and the latest of the two dates
"""
updated = int(webcam.updated_epoch)
return updated > latest_snapshot, max(updated, latest_snapshot) | def6ba367f3cde5494f66e24be8da75bf4ef7861 | 523,678 |
def query_split(msg):
"""Split a message's query up into (key, [*value]) pairs from a
?key=value&key2=value2 style Uri-Query options.
Keys without an `=` sign will have a None value, and all values are
expressed as an (at least 1-element) list of repetitions.
>>> m = aiocoap.Message(uri="coap://ex... | 73ed6352364b4ce10d241b862f067fa48b26e146 | 543,685 |
def concatenate(*args):
"""
If no argument is None, then join all strings.
:param args: Can be strings, MyString objects or None
:return: None or string.
"""
for e in args:
if e is None:
return None
return ''.join([str(e) for e in args]) | 165147efc3b6c6c263214d6320cfc7a02a257368 | 401,423 |
import re
def re_lines(pat, text, match=True):
"""Return a list of lines selected by `pat` in the string `text`.
If `match` is false, the selection is inverted: only the non-matching
lines are included.
Returns a list, the selected lines, without line endings.
"""
assert len(pat) < 200, "It... | aa04cfdbf5dfa2c3aed6d22e3f3671f69ab4481f | 81,829 |
def ImCrossTermID(atom_names):
"""
# From a list of 4 atom names, corresponding two a pair
# of angles between atoms# 3,2,1 and 3,2,4,
# and replaces the list of atoms with a canonical tuple
# which eliminates order ambiguity.
# If you swap the first and last atom (#1 and #4), then
# the -pa... | 119bc4e36d20eae6c2b2d1fac4eab5c60ebc59f2 | 350,904 |
def cast_to_date(session):
"""cast session.data datetime object to a date() instance
"""
if session.data is not None:
session.data = session.data.date()
return session.data | 6c38c9a14ce6bdca68ba90f06dba9a5aa6254eec | 211,909 |
import zipfile
import tempfile
def training_model_archive(filepath):
"""
Create zip archive file for training model.
:param filepath:
:return: temporary archive file
"""
if zipfile.is_zipfile(filepath):
return open(filepath, 'rb')
tmp_file = tempfile.NamedTemporaryFile(suffix='.zip... | 4d8dd1d6166c50d8c368cb34599126ab7d838333 | 61,459 |
import inspect
def sig_arg_names(fun):
"""Return argument names from function signature."""
sig = inspect.signature(fun)
return tuple(sig.parameters.keys()) | dcf53ef09b7094242572f4c1b0d7950b776c597f | 296,480 |
def add_delay_columns(data):
"""Compute edge durations and delays."""
data["event_delay"] = (
data["event_time_real"] - data["event_time_planned"]
).dt.total_seconds().astype("float32") / 60
data["edge_duration_planned"] = (
data["next_event_time_planned"] - data["event_time_planned"]
... | a9c3d623638a1506bc0b79cf2a356eecd8b20ee5 | 331,562 |
def GenerateAuthProviderCmdArgs(kind, cluster_id, location):
"""Generates command arguments for kubeconfig's authorization provider.
Args:
kind: str, kind of the cluster e.g. aws, azure.
cluster_id: str, ID of the cluster.
location: str, Google location of the cluster.
Returns:
The command argum... | 1f10d734d8392165360e33fc47cc4ef80498dff7 | 201,634 |
import math
def __even__(f):
"""
Help function to round each number to even.
:param f: given number
:return: even approximation of the given number
"""
return math.ceil(f / 2.) * 2 | 3faf2160cadaff9ab97660ffdac51aaeb2ca52af | 412,104 |
def _check_flexibility_scenario(flex):
"""Checks the flexibility scenario input to
:py:func:`partition_flexibility_by_sector`.
:param str flex: The input flexibility scenario that will be checked. Can be any of:
*'Base'* or *'Enhanced'*.
:return: (*set*) -- The formatted set of flexibility scen... | e7bfceec23f6e02e7d8895f0593b3704628a8e28 | 577,562 |
def add_keys(destdict, srclist, value=None):
"""
Nests keys from srclist into destdict, with optional value set on the final key.
:param dict destdict: the dict to update with values from srclist
:param list srclist: list of keys to add to destdict
:param value: final value to set
:return: dest... | b326da0380090b3a749dc2131161b94ed67ae7db | 537,617 |
def compute_squared_bp(channels):
"""Compute band power estimates by squaring channel values."""
squared_channels = dict()
for channel in channels:
if channel == "Time":
squared_channels[channel] = channels[channel][:]
continue
squared_channel = [x**2 for x in channel... | 29f596a15b73beb857ad7f4fb4b07919b5602a79 | 151,597 |
def b_to_gb(value):
"""Bytes to Gibibytes"""
return round(int(value) / (1000**3), 3) | 7f4abbd4db51e1a57917948405f0ce4affb61149 | 172,153 |
import torch
import math
def exp_sigmoid(x, exponent=10.0, max_value=2.0, threshold=1e-7):
"""Exponentiated Sigmoid pointwise nonlinearity.
Bounds input to [threshold, max_value] with slope given by exponent.
Args:
x: Input tensor.
exponent: In nonlinear regime (away from x=0), the output varies... | e0f4076a75d760194f6f78d4c38b319aa9bdc690 | 502,361 |
def sort_response(response_dict, *args):
"""
Used in tests to sort responses by two or more keys.
For example if response includes experimentKey and FeatureKey, the function
will sort by primary and secondary key, depending which one you put first.
The first param will be primary sorted, second seco... | 3e4993469071c79e107aaaee8979005e4161d37b | 381,079 |
def distance_difference_calc(r2, s1, gap):
"""
Computes the necessary distance between mocks given geometrical
components of the survey.
Parameters
-----------
r2 : `float`
s1 : `float`
gap : `float`
Returns
----------
dist_diff : `float`
"""
# Converting to float... | 499fbfddab00c2e3d2a81861b0e23ed07d4e0b5c | 687,105 |
def point_dist(p0, p1):
"""
Calculate distance between two points in 2D.
:param p0: first point, array like coordinates
:param p1: second point, array like coordinates
:return: distance, float
"""
return ((p1[0] - p0[0])**2 + (p1[1] - p0[1])**2)**(1/2) | a7bed525e0256f180a2f0702697bdb19834c8b5f | 196,625 |
from typing import Dict
from typing import Any
import yaml
def _mock_yaml_load() -> Dict[str, Any]:
"""Create a mock openAPI-specification dokument."""
with open("./tests/files/petstore.yaml", "r") as file:
_yaml = yaml.safe_load(file)
return _yaml | 2093e6696a6eca3bff7a56f85bde582accf151aa | 300,475 |
def get_class(class_name, module_name):
"""
Retrieves a class based off the module using __import__.
"""
if not isinstance(class_name, str) or not isinstance(module_name, str):
return None
try:
# requiring parameter `fromlist` to get specified module
module = __import__(modul... | 4c0b46c0d56e05454dff8bd395e61541a5ff80f9 | 101,749 |
import random
def generate_random_key(seed_rule, start, end=None):
"""
依照指定的亂數長度範圍參數,產生隨機網址碼
Args:
seed_rule(str):亂數的種子規則 e.g abcABC1234+
start(int): 起始的長度
end(int): 最長的長度 (如果沒有提供則為 start 的固定長度)
Returns:
url_key(str): 隨機網址碼 (長度 15 - 20)
"""
if end:
url_length = random.randint(start, end)
else:
url_... | dd8edd61c2458fcdba063f2ce591123adf727022 | 144,186 |
def remove_dups(list_):
"""Get new list without duplicates, order-preserving."""
seen = set()
seen_add = seen.add
return [x for x in list_ if not (x in seen or seen_add(x))] | bbd0dc3e1bad8be4dc7b870e8de8c198bc470525 | 181,084 |
def build_variant_display_title(chrom, pos, ref, alt, sep='>'):
""" Builds the variant display title. """
return 'chr%s:%s%s%s%s' % (
chrom,
pos,
ref,
sep,
alt
) | 3461166b459e9a5715353080f271ec9ee014bb34 | 633,530 |
def diverging_cmap(cmap='RdBu_r'):
"""Default diverging colormap"""
return cmap | 7caf998c325b698c9b1a84c90bd91e2ad3ebc27b | 265,874 |
import requests
def process_response(wrapped, instance, args, kwargs):
"""
Decorator to process requests.Response
Raises:
Exception: Service Unavailable
Returns:
dict: json data
"""
try:
resp = wrapped(*args, **kwargs)
except (requests.ConnectionError, requests.... | f2d47dd156a8c38f154206639d2ab439459cbe83 | 193,529 |
def create_open_url_payload(url: str):
"""Create and return "openUrl" dictionary to send to the Plugin Manager.
Args:
url (str): Url to open in browser.
Returns:
dict: Dictionary with payload to open url in browser.
"""
return {
"event": "openUrl",
"payload": {
... | 3492031523a7c88a7fbd426f4f822658aee31558 | 314,022 |
def get_total_figure_number(axis_matrix):
"""
Gets the number of figures from an axis_matrix generated by pyplot. Used by the hash to place configs in the
correct part of the axis_matrix
Parameters:
axis_matrix (1D or 2D numpy array) = Array returned by pyplot once multiple figu... | dcab29bd85735bee19756a084c182fb0a60e9549 | 616,040 |
def S(*regexps) -> str:
"""Just a shortcut to concatenate multiple regexps more easily"""
return ''.join(regexps) | 38984899c7c3000c39dc38a66b3e790a3b36207d | 511,673 |
import torch
def compress_weights(W, l):
"""Compress the weight matrix W of an inner product (fully connected) layer
using truncated SVD.
Parameters:
W: N x M weights matrix
l: number of singular values to retain
Returns:
Ul, L: matrices such that W \approx Ul*L
"""
# numpy doesn'... | 94b43e1bd4eff1a4e90eb13653de28c0fdfb72a9 | 568,547 |
def is_between(val, bound_1, bound_2):
"""
Return if a value falls between two boundary values
"""
if val > max([bound_1, bound_2]):
print("not between!")
return False
if val < min([bound_1, bound_2]):
print("not between!")
return False
return True | 9fc710e947b389ad5412749f1a60b876ed04fb54 | 246,339 |
import math
def entropy(probabilities):
""" Calcola entropia della distribuzione di probabilità
:param probabilities: lista contenente una distribuzione di probabilità
:return: valore dell'entropia relativa alla distribuzione in input
"""
entropy = 0.0
for i in range(len(probabilities)):
entropy += probabil... | 81b7f4b4b95664804a2ed17086c12a43125ac6b6 | 657,752 |
import tokenize
import codecs
def read_pyfile(filename):
"""Read and return the contents of a Python source file (as a
string), taking into account the file encoding."""
with open(filename, "rb") as pyfile:
encoding = tokenize.detect_encoding(pyfile.readline)[0]
with codecs.open(filename, "r",... | a6fce0f2acdb5156872ef572ab7e77ba6507873d | 695,901 |
def to_unsigned(number, nb_bits):
"""Compute the unsigned value of the given number according to the given number of bits used for encoding.
>>> to_unsigned(5, 32)
5
>>> to_unsigned(-5, 8)
251
>>> to_unsigned(-5, 9)
507
:param int number: the number to convert.
:param int nb_bits: ... | 6071519006a294a24c52381472ea683cbdc31663 | 218,844 |
import itertools
def adjacent_pairs(seq):
"""From e0, e1, e2, e3, ... produce (e0,e1), (e1,e2), (e2,e3), ..."""
return zip(seq, itertools.islice(seq, 1, None)) | 99382158526602f44e25f8d0611e5599afcd328e | 153,941 |
def updateCharacterName(old_name: str, new_name: str) -> str:
"""Return a query to update a given character's name."""
return (f"UPDATE game_character "
f"SET name='{new_name}' "
f"WHERE name='{old_name}';"
) | eb829e6be49393baf1c007c0331fd45a50050af5 | 16,930 |
def str2num(string, decimal_sep='.'):
"""
A helper function that converts strings to numbers if possible
and replaces the float decimal separator with the given value.
eg. '1' => 1
'1.2' => 1.2
'1,2' => 1.2
'True' => True
"""
if not isinstance(string, str): return string
... | 1e677e7c9bad46266125373f56040e3f053b7d56 | 200,533 |
def sec2hms(sec):
"""
Convert seconds to hours, minutes and seconds.
"""
hours = int(sec/3600)
minutes = int((sec -3600*hours)/60)
seconds = int(sec -3600*hours -60*minutes)
return hours,minutes,seconds | efea3a641c5f13313adb571c201cc25d2895757e | 3,492 |
import math
def next_halton(index, base):
"""Calculate next halton number """
result = 0.0
f = 1.0
i = index
while i > 0:
f = f / base
result += + f * (i % base)
i = math.floor(i / base)
return result | d9500c341a7438a388e54833434bdb761865786e | 264,780 |
def get_treeview_action(self, **options):
"""Return the tree view action for `self`.
If the `self` is a singleton, return the view type 'form' so that it's
easier to see the full object.
:rtype: An action `dict` you can return to the web client.
`options` is used to override **almost any** key in... | dedbf9f3945afea72c05d4b2ad42b66e89aa7d0f | 207,794 |
def norm_angle(a):
"""Normalizes an angle in degrees to -180 ~ +180 deg."""
a = a % 360
if a >= 0 and a <= 180:
return a
if a > 180:
return -180 + (-180 + a)
if a >= -180 and a < 0:
return a
return 180 + (a + 180) | 0ee87049eda911bbd9307aa64464b1508791a16c | 510,511 |
def _event_as_string(event):
"""Convert a midi event packet to a string representation."""
return '[id, status, cnum, cval, d1, d2] = %3d, %3d, %3d, %3d, %3d, %3d' % (
event.midiId, event.status, event.controlNum, event.controlVal, event.data1, event.data2) | 95bc65cfa40664cad4c953b3d6eb09b9d14562eb | 329,968 |
def get_cheat_sheet(cheat_sheet):
"""converts a cheat sheet from .json to string to display
Parameters
----------
:param dictionary cheat_sheet: dictionary that stores the content of given cheat sheet.
:return: a str representation of a cheat sheet.
"""
sheet = []
separator = '\n'
... | b8d401e0c73c0f103cf0b3f404a2150b7bc28a36 | 36,887 |
def datetime_to_iso_8601(dt):
"""Format a datetime as an iso 8601 string - YYYY-MM-DDTHH:MM:SS with optional timezone +HH:MM."""
if dt:
return dt.replace(microsecond=0).isoformat()
else:
return None | 1f1610b6d34409a00f4a1b0d8d47caa5ec8e8ad8 | 680,177 |
def fullname(clazz):
"""
Return fully qualified name of the given class.
>>> from collections import Counter
>>> fullname(Counter)
'collections.Counter'
"""
clazz_name = clazz.__name__ if hasattr(clazz, '__name__') else clazz._name
return f'{clazz.__module__}.{clazz_name}' | 35262e7c2f9e5428745054aa3f0b4ff7107a7ef1 | 634,522 |
def _str_to_bool(text):
"""
convert a string to boolean. 'true', 'TRUE', 'TrUe', 'Yes', '1', 'y' and
similar returns True, otherwise returns False
:param text: text to convert to boolean
:return: the boolean representation of the text
"""
return text.lower() in ['true', 'yes', 'y', '1'] | e6fd0098c8605a56501211320931874c8a52f974 | 383,045 |
def create_empty(val=0x00):
"""Create an empty Userdata object.
val: value to fill the empty user data fields with (default is 0x00)
"""
user_data_dict = {}
for index in range(1, 15):
key = "d{}".format(index)
user_data_dict.update({key: val})
return user_data_dict | 55e614059940b7eda5a0684dd105a60dbc7f1549 | 698,746 |
def get_group(parser, group_name, env_var_prefix=None):
"""
Return an argument group based on the group name.
This will return an existing group if it exists, or create a new one.
:argument parser: :py:class:`python3:argparse.ArgumentParser` to
add/retrieve the group to/from.
... | 3ec99a16c34d438b4b3c66a890a7b63f1060deea | 141,806 |
def set_if_true(func):
"""Decorate a setter to only set if the value is nonzero"""
def new_func(self, var):
if var:
func(self, var)
return new_func | 4afafe5e8ecd72cbebbc49fd5e619ccf33ec3b1f | 475,644 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.