content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
def dataframe_summary (df):
"""
Computes summary statisitcs of a data frame
:param df: Dataframe to comoute statistics for
:return: Dataframe with summary statisitcs for input data frame
"""
df_summary = df.describe(include='all')
df_summary.loc['Missing #'] = df.isnull().sum(axis=0)
df... | 7948a28380894ac6b5ba4f5e31ca3499215c633a | 596,860 |
def get_opcode(instruction_bytes):
"""
Returns the 6-bit MIPS opcode from a 4 byte instruction.
"""
op = (instruction_bytes & 0xFC000000) >> (3 * 8 + 2)
return op | 85dab0a35b0e31a3f2f2b5c1f3d5323b1ccf3dae | 101,276 |
def normintens_to_countrate(ni_map, star_photrate, collecting_area,
coron_thrupt_peakpixel, optloss = 0.5,
qe = 0.9, ifsloss = 1.0):
"""
Convert a normalized intensity array to an array of photoelectron count
rates, based on parameters for stellar irr... | 7dccaa36335fadbfe2356ffa3f7a07829e60ed2d | 365,439 |
def euclidean_distance(point_a, point_b):
"""
Returns the euclidean distance between two points with (i, j) coordinates.
"""
return (point_a[0] - point_b[0]) ** 2 + (point_a[1] - point_b[1]) ** 2 | d5789f2aad96f44eeed0c09f7d1766fe370268f9 | 158,545 |
def merge_lists(a, b):
"""
Merge lists - e.g.,
[1, 2, 3, 4, 5, 6] & ['a', 'b', 'c']
=> [1, 'a', 2, 'b', 3, 'c', 4, 5, 6]
:param a: List a
:param b: List b
:return: Merged lists
"""
result = []
length = min([len(a), len(b)])
for i in range(length):
result.append(a[i])
... | 8272fe726299953f1cd28aea662e6720fb75c300 | 694,373 |
import re
def removePunctuation(text):
"""Removes punctuation, changes to lowercase, and strips leading and trailing spaces.
Note:
Only spaces, letters, and numbers should be retained. Other characters should should be
eliminated. (e.g. it's becomes its)
Args:
text (str): A stri... | b8d3fdf38507acf61d34dd702ae402de87726ce3 | 307,826 |
def beta(**kwds):
"""Return the parameters of the beta distribution (alpha, beta) given the
mean and variance."""
mean = kwds['mean']
var = kwds['variance']
alpha = -(mean*var + mean**3 - mean**2) / var
beta = ((mean-1)*var + mean**3 - 2*mean**2 + mean) / var
return alpha, beta | 0b2a8e8a45b99671bf05e863e9c469004ac9244e | 522,279 |
import json
def read_json_file(file_path):
"""
Read a JSON file.
:param file_path: the local path to the JSON file.
:return: a JSON object - usually a list or a dict.
"""
with open(file_path, 'r') as outfile:
return json.load(outfile) | 5fa35c88844b26ef5f3b10c4799f4da071b073a0 | 401,515 |
def main_float_input(message):
"""
Takes message to print when asking for input, the converts to float.
Repeats user input until it can be converted to float without error.
Returns that float once done.
"""
try_input = input(message)
while True:
try:
x = float(try_input)
... | 8bc3aa4f78069dbdc115a90751254ab4652dacdc | 86,932 |
def get_cidr(netmask):
"""Convert a netmask to a CIDR."""
binary_str = ''
for octet in netmask.split('.'):
binary_str += bin(int(octet))[2:].zfill(8)
return str(len(binary_str.rstrip('0'))) | 95c98496f63daf7eb4c8c441fdfc161541d0e294 | 113,387 |
def fully_connected(input, params):
"""Creates a fully connected layer with bias (without activation).
Args:
input (:obj:`tf.Tensor`):
The input values.
params (:obj:`tuple` of (:obj:`tf.Variable`, :obj:`tf.Variable`)):
A tuple of (`weights`, `bias`). Probably obtained ... | 77815acfe17674bc20035900b75d8e4ddc982855 | 12,022 |
def check_2Dinput_consistency(structure, parameters):
"""
Check if structure and parameter data are complete and matching.
:param input: structure, needs to be a valid aiida StructureData node
:param input: parameters, needs to be valid aiida ParameterData node
returns (False, errormessage... | 14c2326a35efced9186b9067e2851539677419f1 | 538,342 |
def _IsValidComposerUpgrade(cur_version, candidate_version):
"""Validates that only MINOR and PATCH-level version increments are attempted.
(For Composer upgrades)
Checks that major-level remains the same, minor-level ramains same or higher,
and patch-level is same or higher (if it's the only change)
Args:... | 0683c90e22df65eb0e27acd50ee21d08c1a366d9 | 87,405 |
def bid_order_array(market,p,q,reference_price):
""" make array of orders from array of percents, quantities and reference price """
i = 0
orders = list()
print ("bid order array. ref: " + str(reference_price))
print (str(p))
for px in p:
submit_price = reference_price * (1-px)
o... | 0e2810b6acc2644e797fd568a7f33ae1542bde2c | 570,357 |
import torch
def tensor2im(x: torch.Tensor, norm=False, dtype='float32'):
"""Convert tensor to image.
Args:
x(torch.Tensor): input tensor, [n, c, h, w] float32 type.
norm(bool): if the tensor should be denormed first
dtype(str): not used yet.
Returns:
an image in shape of... | 937f256e06ff2a18976796ab982989ce19b79798 | 507,985 |
def forcestring(value):
"""Test a value, if it is not an empty string or None, return the string
representation of it, otherwise return an empty string.
"""
if value is None:
return ""
return str(value) | 4d0fe3aee9de7d7c9edff1aaf48b898d9818808b | 533,826 |
def read_embedding_set(args):
"""
Reads a nodelist with vertices to be embedded.
"""
vertices = set()
with open(args.embed_subset, 'r') as f:
for line in f:
vertex = int(line.strip())
vertices.add(vertex)
return list(vertices) | 06401cd3581ccfc1f406ac9c7833381c85865497 | 72,515 |
def web_tile_zoom_out(zxy):
""" Compute tile at lower zoom level that contains this tile.
"""
z, x, y = zxy
return (z-1, x//2, y//2) | b3eb5664791ba54576f1c8bb92c0ac738bdc5dcd | 178,523 |
import csv
import random
def addThing(indVign,config):
"""This function adds a small image (object) to a larger image
obj.csv definition is: name of the image (i.e. A001.png), x-coord, y-coord, subsequent columns possible outcomes
It returns a tuple (object file name, x, y)"""
with open(config["csvLocation"]+"/"+... | 7317eaa9938f7c11c3bbfce77a61532f5c9b1bf5 | 203,135 |
def byte_align(pos):
"""Return the smallest multiple of 8 greater than ``pos``. Raises
``ValueError`` if ``pos`` is negative.
"""
if pos < 0:
msg = "Expected positive integer, got {}"
raise ValueError(msg.format(pos))
return ((pos + 7) // 8) * 8 | 50f050da2c24f35e7c53b887a3b81c23458ddbef | 76,803 |
import hashlib
def getHashForFile(f):
"""Returns a hash value for a file
:param f: File to hash
:type f: str
:returns: str
"""
hashVal = hashlib.sha1()
while True:
r = f.read(1024)
if not r:
break
hashVal.update(r)
f.seek(0)
return hashVal.hexd... | 7adc2383a9f555ae64a4bc9fd34715c4c3057039 | 58,527 |
def sensitivity_metric(event_id_1, event_id_2):
"""Determine similarity between two epochs, given their event ids."""
if event_id_1 == 1 and event_id_2 == 1:
return 0 # Completely similar
if event_id_1 == 2 and event_id_2 == 2:
return 0.5 # Somewhat similar
elif event_id_1 == 1 and eve... | b04c5fa27ef655dd3f371c3ce6ef0410c55dd05b | 3,309 |
def l2_distance(pts1, pts2):
"""
Args:
pts1, pts2: float, (n_frames, n_points, 2)
Return:
float: total l2 distance of these frames
"""
distance = 0
for i in range(pts1.shape[0]):
for j in range(pts1.shape[1]):
distance += ((pts1[i][j][0] - pts2[i][j][0])*(pts1... | bac296a2460d090adb756ece088fc969ee836480 | 529,042 |
import json
def read_json_file(path):
"""Returns JSON parsed data from file."""
with open(path, "r") as file:
return json.load(file) | eb036c43a5f16d5bf2d803089b84e95c3d932d74 | 615,907 |
def row_to_dict(row, field_names):
"""Convert a row from bigquery into a dictionary, and convert NaN to
None
"""
dict_row = {}
for value, field_name in zip(row, field_names):
if value and str(value).lower() == "nan":
value = None
dict_row[field_name] = value
return d... | 10c903dda90524e6aacfb54a17d4839c8498a769 | 667,410 |
def parse_header(header):
"""
Extract size= and barcode= fields from the FASTA/FASTQ header line
>>> parse_header("name;size=12;barcode=ACG;")
('name', 12, 'ACG')
>>> parse_header("another name;size=200;foo=bar;")
('another name', 200, None)
"""
fields = header.split(';')
query_name... | fcebaea017bf024e71da29b8bea505f0e6423fb1 | 127,106 |
def sos_model_dict(scenario_only_sos_model_dict):
"""Config for a SosModel with one scenario and one sector model
"""
config = scenario_only_sos_model_dict
config['sector_models'] = [
{
'name': 'economic_model',
'inputs': [],
'parameters': [],
'out... | 90e6de680b22b77ca2eb32ee015afcfcbe415364 | 82,606 |
def _trim_name(image):
"""Remove the slash at the end of the filename."""
return image[:-1] if image[-1] == '/' else image | 823dd63920673352a18d73f83190853d5a234483 | 4,773 |
def _parse_title(dom, details):
"""
Parse title/name of the book.
Args:
dom (obj): HTMLElement containing whole HTML page.
details (obj): HTMLElement containing slice of the page with details.
Returns:
str: Book's title.
Raises:
AssertionError: If title not found.
... | 41e0581ac886ed6301b765ef53fcc21db3d4f15c | 417,314 |
def _create_expr(symbols, prefix='B', suffix='NK'):
"""Create einsum expr with prefix and suffix."""
return prefix + ''.join(symbols) + suffix | d5e0aff866f375d611b4a4fa82665a0a5447bf02 | 36,720 |
def power_n( data, power=2.0, verbose=False ):
"""Returns the voxel-wise power of either a data array or a NIfTI-1 image object.
To get the nth root, use 1/power.
nth root of either a data array or a NIfTI image object.
E.g. power_n(nii, 1./3) returns a NIfTI image whose voxel values are cube ... | e29bd9170a14c91fcba25159e9e8a1708c3dcb6d | 235,787 |
def CheckAgreement(ex, min_agreement, all_targets, max_agreement=100):
"""Return the labels that at least min_agreement raters agree on."""
sum_ratings = ex[all_targets].sum(axis=0)
agreement = ((sum_ratings >= min_agreement) & (sum_ratings <= max_agreement))
return ",".join(sum_ratings.index[agreement].tolist(... | 4095405e8965cb363da638c6d73aabff86900a81 | 457,980 |
def translate_variable(variable_number: int) -> str:
"""
Get variable name from its number
Variable numbers represent the index in the following sequence:
```
Y, X1, Z1, X2, Z2, X3, Z3, ...
```
"""
# return "Y" if the number is 1
if variable_number == 1:
return "Y"
# X if even index, Z if odd index
letter... | c579310f58ef087e2f4475e4bdbb0dc22a958416 | 390,463 |
def apply_mask(image, mask):
"""Return a masked image."""
return image * mask | f8898e7e746a690a443d8280abd5b29c4ffff781 | 441,062 |
def fprime(x, A, b):
""" The gradient of the objective function with respect to the heights (x).
This gradient is a vector the same size as x.
A^T (Ax-b)
"""
return A.transpose() * (A * x - b) | 3be17624f363ff5db106c27f725ed5559cce7dfa | 450,305 |
def crop(img, left, top, right, bottom):
"""
Crop rectangle from image.
Inputs:
img - The image to crop.
left - The leftmost index to crop the image.
top - The topmost index.
right - The rightmost index.
bottom - The bottommost index.
Outputs:
img - The c... | 1507a55bba07dc656f51f873d2328b69f70682c9 | 709,521 |
import time
def measureTime(method):
"""
Decorator for measuring how long the given function took to execute.
Credit to: https://thenextweb.com/news/decorators-in-python-make-code-better-syndication
Args:
method (function): Function to measure time to execute.
"""
def wrapper():
... | d4f54954bed53fcfb50da0641d49b58b04567f6e | 57,870 |
def getImName(image_path,folder_path):
"""
get image name by clean up image path
Parameters:
-image_path: path of image in the folder
-folder_path: path of folder that contain images
Returns:
-name: name of the image
"""
name = image_path.replace(folder_path.replace("/*.j... | e1b6a432d06f7c45d0a7408b52fcfc6464da2b07 | 65,848 |
import random
def get_random_int(min_v=0, max_v=10, number=5, seed=None):
"""Return a list of random integer by the given range and quantity.
Parameters
-----------
min_v : number
The minimum value.
max_v : number
The maximum value.
number : int
Number of value.
se... | fb551cbcbb8983dfada2368c17607dfad4f1aa66 | 621,336 |
from typing import Dict
from typing import Any
def _transform_underscore(d: Dict[str, Any]) -> Dict[str, Any]:
"""transform "-" to "_" recursively
Args:
d:
Returns:
Dict[str, Any]: new dict
"""
new_dict = d.copy()
for k, v in new_dict.items():
if isinstance(v, Dict):
... | 6c541c41929c8443c3185159cf98a40f2aa78d1e | 151,150 |
def DiffValueLists(new_list, old_list):
"""Give an old list and a new list, return the added and removed items."""
if not old_list:
return new_list, []
if not new_list:
return [], old_list
added = []
removed = old_list[:] # Assume everything was removed, then narrow that down
for val in new_list:
... | 4c3dc471bd0e9e9aea3b5f0b8c906eb1ca27d196 | 52,124 |
import re
def slugify(text):
"""
Generates an ASCII-only slug.
"""
return re.sub('-$', '', re.sub('[^A-Za-z0-9\-]+', '-', text)) | 18859e4e3237d392c2d2b18ed82b9d6d27fe6598 | 341,793 |
def average_error(state_edges_predicted, state_edges_actual):
"""
Given predicted state edges and actual state edges, returns
the average error of the prediction.
"""
total=0
for key in state_edges_predicted.keys():
#print(key)
total+=abs(state_edges_predicted[key]-state_edges_... | 40d132682e49b556e8cc0fc71015947202b9cf08 | 28,643 |
def extract_steering_wheel_image(screenshot_rs):
"""Extract the part of a screenshot (resized to 180x320 HxW) that usually
contains the steering wheel."""
h, w = screenshot_rs.shape[0:2]
x1 = int(w * (470/1280))
x2 = int(w * (840/1280))
y1 = int(h * (500/720))
y2 = int(h * (720/720))
ret... | 6bb3d982d695bcfc1615e7f15b59ca14ed439416 | 263,572 |
def split_columns(string):
"""
Splits the return-columns argument or reads it from .txt
--return-columns 'temperature c, "heat$"' -> ['temperature c', '"heat$"']
--return-columns my_vars.txt -> ['temperature c', '"heat$"']
"""
if string.endswith('.txt'):
with open(string, 'r') as file:
... | 0977f978a2ae5d22eb7b873901a6f4bfa6c92cc4 | 52,164 |
def get_slot_values(slotted_instance):
"""Get all slot values in a class with slots."""
# thanks: https://stackoverflow.com/a/6720815/782170
return [getattr(slotted_instance, slot) for slot in slotted_instance.__slots__] | 4adfe1d7dc66cd42cc12c2c26b5e1069d7dac7a8 | 219,852 |
def writexl_new_content_types_text(db):
"""
Returns [Content_Types].xml text
:param pylightxl.Database db: database contains sheetnames, and their data
:return str: [Content_Types].xml text
"""
# location: [Content_Types].xml
# inserts: many_tag_sheets, tag_sharedStrings
# note calcCh... | 3762cfe7680fa1754885622b123c914a341aea34 | 680,691 |
import textwrap
def multiline_fix(s):
"""Remove indentation from a multi-line string."""
return textwrap.dedent(s).lstrip() | 9cb964eb88ebcaadc00acc222d174d6a320c44cf | 679,426 |
def get_random_urls(prefix='http://www.example-shop.com/product/', size=1000, start_index=None):
"""
Create random url endpoints.
Args:
size (int): number of urls to be created.
start_index (int): optional argument for starting number.
"""
if not start_index:
start_inde... | e302b4f1003391e1eb6e4e400b9d49e0710782fd | 48,670 |
def getIntersection(in1_list, in2_list):
"""Function for determining the intersection of two lists. Returns the common elements."""
return set(in1_list).intersection(in2_list) | 6396d883ab4acc8c3247ba4eada1c7722d0375d8 | 516,168 |
import difflib, time, os
def pydiff(text1, text2, text1_name='text1', text2_name='text2',
prefix_diff_files='tmp_diff', n=3):
"""
Use Python's ``difflib`` module to compute the difference
between strings `text1` and `text2`.
Produce text and html diff in files with `prefix_diff_files`
a... | 98cc5dcb7726120ecd85237b4795467a16676291 | 302,593 |
def original_scorer(worda_count, wordb_count, bigram_count, len_vocab, min_count, corpus_word_count):
"""Bigram scoring function, based on the original `Mikolov, et. al: "Distributed Representations
of Words and Phrases and their Compositionality" <https://arxiv.org/abs/1310.4546>`_.
Parameters
-------... | 228ba69d5598cac078c3ed3d25ee907c1d58aef8 | 212,252 |
async def healthCheck():
"""
Returns 200 for a healthcheck for AWS
"""
return {'ok'} | 6cc232df6661f26a1db4e4f6bf35e5de284abff1 | 691,926 |
import torch
def gpu_non_dominated_sort(swarm: torch.Tensor):
"""
The GPU version of non-dominated sorting algorithms
Args:
swarm (np.ndarray): m x n scoring matrix, where m is the number of samples
and n is the number of objectives.
Returns:
fronts (List): a list of Paret... | 8aff282bbc6b6af5dbe1f64dfa1499dc3b3936ef | 223,915 |
import re
def delete_useless(statement: str) -> str:
"""
無駄な移動/計算を相殺、削除
>>> delete_useless("+++--<<>>>")
'+>'
>>> delete_useless("---++>><<<")
'-<'
>>> delete_useless(">++++[-][-]")
'>[-]'
>>> delete_useless(">--[-]++[-]")
'>[-]'
"""
while True:
if "<>" in stat... | 3bd2b63f0634b53360c18637029a27eb30957c91 | 623,522 |
def get_targets_in_sif_file(sif_file, targets):
"""
Get the targets that are inside the network given by the user
"""
targets_in_network = set()
str_tar = [str(x) for x in targets]
with open(sif_file, 'r') as sif_fd:
for line in sif_fd:
node1, score, node2 = line.strip().spli... | 5f788edaa60a6fa99f8c26458598f825ed8fe2ce | 297,802 |
import re
def argument_exists(arg_name, args):
"""
Test whether given argument name exists in given argument list.
:param arg_name: Argument name.
:param args: Argument list.
:return: Whether given argument name exists in given argument list.
"""
# Create regular expression object that ... | 5d3ed9335989c6e8466697b091267b8b1968e5bc | 198,469 |
import math
def cosine(r_tokens: list, s_tokens: list) -> float:
"""Computes cosine similarity.
COS(r, s) = |r ∩ s| / sqrt(|r| * |s|)
Parameters
----------
r_tokens : list
First token list.
s_tokens : list
Second token list.
Returns
-------
Cosine similarity of r... | 234b7298e8c0c29cbb3d79d420e63518decfe4e9 | 694,152 |
def trimmed(goal, num=200):
"""Trim the goal column to ``num`` words so it fits in the notes column."""
words = goal.split()
trimmed_words = words[:num]
joined = " ".join(trimmed_words)
if len(trimmed_words) < len(words):
# trimmed_words is actually shorter than words, so we have cut
... | 56876c10def91f5b1dd2352f432378cde1398afc | 382,355 |
import math
def get_auto_embedding_dim(num_classes):
""" Calculate the dim of embedding vector according to number of classes in the category
emb_dim = [6 * (num_classes)^(1/4)]
ref: Ruoxi Wang, Bin Fu, Gang Fu, and Mingliang Wang. 2017. Deep & Cross Network for Ad Click Predictions.
In Proceedings o... | 9c91c8bf2d775d029349849c7efa56ba51714448 | 671,734 |
import csv
def read_from_csv(_path:str):
"""Read csv file and return field names and data rows."""
fieldNames, dataRows = [], []
with open(_path, "r", encoding="utf-8") as file:
# Create a csv reader object.
csvReader = csv.reader(file)
# Extract field names through first row.
... | e973b13c916afd0707435278f8391870d160263e | 308,652 |
def moving_func_trim(window_diameter, *arrays):
"""Trim any number of arrays to valid dimension after calling a centered bottleneck moving window function
Parameters
----------
window_diameter: int
odd number window width
arrays: 1 or more numpy.ndarray
Returns
-------
tuple of... | 4a30466c9d0dcc50914ca84b63042db8bf22948c | 303,694 |
import math
def nice(v):
"""Returns nearest nice number
Give highest value if equally close to two nice numbers
"""
e = math.floor(math.log10(v)) # Exponent
b = v / 10**e
if b < 1.5:
a = 1
elif b < 3.5:
a = 2
elif b < 7.5:
a = 5
else:
a = 10
d... | f5ba294932d95986705688dae7e3bba9ab5bf294 | 171,881 |
def cn(uc, w, eis, frisch, vphi):
"""Return optimal c, n as function of u'(c) given parameters"""
return uc ** (-eis), (w * uc / vphi) ** frisch | 74247338f5d26c81e24748f597f1e47386829832 | 126,673 |
def expand_and_clamp(box, im_shape, s=1.25):
"""Expand the bbox and clip it to fit the image shape.
Args:
box (list): x1, y1, x2, y2
im_shape (ndarray): image shape (h, w, c)
s (float): expand ratio
Returns:
list: x1, y1, x2, y2
"""
x1, y1, x2, y2 = box[:4]
w =... | 1ac2f5d66d190af46832ed984092979aa70cb58e | 408,313 |
def isSuffixOf(xs, ys):
"""``isSuffixOf :: Eq a => [a] -> [a] -> Bool``
Returns True if the first list is a suffix of the second. The second list
must be finite.
"""
return xs == ys[-len(xs):] | c57da0216d8064f4a5902f2397025d29fa04784d | 575,917 |
def discrimine(pred, sequence):
"""Split a collection in two collections using a predicate.
>>> discrimine(lambda x: x < 5, [3, 4, 5, 6, 7, 8])
... ([3, 4], [5, 6, 7, 8])
"""
positive, negative = [], []
for item in sequence:
if pred(item):
positive.append(item)
else:
... | 23beece3fe4771fbe155d3960978d50e929f82b9 | 74,155 |
def _format_to_rows(dl):
"""Helper method to take data in DOL (dict of lists) format, and convert it to LOD (list of dict)
Args:
data (list): Dict of lists to be converted
Returns:
list: A list of dicts representing data in row form
"""
return [dict(zip(dl, t)) for t in zip(*dl.va... | 2daa034a172e4a75a697c0d799e657729f3617ee | 182,220 |
import json
def load_dataset(data_path):
"""Load existing json format dataset
"""
datafile = open(data_path)
return json.load(datafile) | 7d125a6308c89de15cf4fa6a6edb8cd7c656148c | 630,384 |
from typing import Iterable
def is_seq_consecutive(it: Iterable[int]) -> bool:
"""
:param it: any Iterable of integers
:return: whether the iterable consists of consecutive numbers
"""
sorted_list = sorted(it)
consecutive_list = list(range(sorted_list[0], sorted_list[-1] + 1))
return cons... | 9759b0c46efe6b2bd30f6425c2f52bb2ec816291 | 359,854 |
def signature(obj):
"""Generate a timestamp.
"""
return obj.get_timestamp() | 2fbdbb4e77286f3339334f40794f596fe62d2bef | 136,699 |
import re
def parse_show_mirror(raw_result):
"""
Parse the 'show mirror' command raw output.
:param str raw_result: vtysh raw result string.
:rtype: dict
:return: The parsed result of the show mirror command in a \
dictionary of the form. Returns None if no mirror found:
for 'show mi... | ae4d86a1a5bc3ff363dbff62c313f18538f6b126 | 468,826 |
def atom(text):
"""Parse text into a single float or int or str."""
try:
x = float(text)
return round(x) if round(x) == x else x
except ValueError:
return text | f8d3856c7864a1f6a07ad0c9e8a6cd7f2f16ac8b | 45,033 |
def compute_trajectory(cpu_status, mem_status):
""" Function to alert whether mutliple resources are crossing thresholds or not.
Args:
cpu_status: Keyword 'High', 'Normal' or 'Low' mentioning the status of cpu
mem_status: Keyword 'High', 'Normal' or 'Low' mentioning the status of memory
Re... | 6986eb577e5322e67d0917250048a00841d47297 | 576,330 |
def _profile_function(function, profiles, game):
"""Map a profile function over profiles"""
return [function(game, prof) for prof in profiles] | 540831714c29380407f04f2d50ecf883da585bbd | 520,792 |
def allowed_file(filename, allowed_exts):
"""
Given a filename, check if its extension is allowed from the list of allowed extensions.
"""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in allowed_exts | 342eb59a3095de34698025e4c34ce272219bce60 | 203,239 |
import torch
def expand_many(x, axes):
"""Call expand_dims many times on x once for each item in axes."""
for ax in axes:
x = torch.unsqueeze(x, ax)
return x | 22ef04759db98be7d7eeeab85fd468dfa257b392 | 64,852 |
def fail(exc_type, exc_val, exc_tb):
"""
Transitions the execution to fail with a specific error.
This will prompt the execution of any RequestTemplate.after_exception hooks.
Args:
exc_type: The exception class.
exc_val: The exception object.
exc_tb: The exception's stacktrace.... | d53f6a6047abb9608f661114f248431fbeb8e7e7 | 473,973 |
from typing import Callable
def item_getter(*args, default=None) -> Callable:
"""
Get a function that gets multiple items.
Similar to `operator.itemgetter` but always returns a tuple, and supports defaults.
"""
def f(x):
return tuple(x.get(arg, default) for arg in args)
return f | 958c659493683bc83f02b20089ba2d16790d2a3e | 528,390 |
def diffNumAbs(arg1: int, arg2: int) -> int:
"""
The function takes two arguments arg1 and arg2, both int.
The function as result returns the absolute value of the difference of the two numbers feeded as arguments.
Example :
>>> diffNumAbs(4,6)
2
>>>
"""
res... | c0ac15cfe8b57213f22e39c6fc4e5465c63f6942 | 56,539 |
def predict(model, test_x):
"""
The method predicts labels for testing samples by using trained model.
Parameters
----------
model: trained model
test_x: features of testing data
"""
return model.predict(test_x) | 4c23431eebb47b7dd22bc7ed42c6762fbd3b7295 | 293,777 |
from typing import OrderedDict
def transform_deploymentParam(result):
"""Transforms a paramter into a row for a table"""
result = OrderedDict([('Description', result['description']),
('Name', result['name'] if 'name' in result else ''),
('Type', result['type'] i... | cc6fa4fc80ad18df16731aab6272bade96dd3120 | 512,945 |
import pytz
def validate_timezone(zone):
"""Return an IETF timezone from the given IETF zone or common abbreviation.
If the length of the zone is 4 or less, it will be upper-cased before being
looked up; otherwise it will be title-cased. This is the expected
case-insensitivity behavior in the majorit... | 41673d0f70ead61b74a186206d198f2278ace0b7 | 647,733 |
def abs2(x):
""" Calculates the squared magnitude of a complex array.
"""
return x.real * x.real + x.imag * x.imag | 981dc43c1b1f4cf6ac882b23624cbb022fde7c10 | 549,757 |
def patched_file_serialize(mocker):
"""Patches function that deserializes file contents to website content"""
return mocker.patch(
"content_sync.backends.github.deserialize_file_to_website_content"
) | 477be05acd3f44088ee2d11ad627658419ffd2f0 | 581,834 |
import base64
def encode(a):
"""Base64 encode a numpy array"""
try:
data = a.tobytes()
except AttributeError:
# np < 1.9
data = a.tostring()
return base64.b64encode(data).decode('utf-8') | a73ea9b683b6399142235d9fcb9153e0b9d31f89 | 410,194 |
def dt_to_hms(td):
"""
convert a datetime.timedelta object into days, hours,
minutes and seconds
"""
days, hours, minutes = td.days, td.seconds // 3600, td.seconds % 3600 // 60
seconds = td.seconds - hours*3600 - minutes*60
return days, hours, minutes, seconds | d58ac27a77690f60fff594bdae300576f46f8774 | 387,870 |
from typing import Any
from contextlib import suppress
def safe_get(key, obj, default=None) -> Any:
"""
Safely retrieve key from object
Args:
obj: Any. target object
key: inner object adders token
default: value to return on failure
Returns:
on success: value of request... | 21929399b123f46e7a24289052b1e9a9bbc71fa0 | 648,116 |
import json
def read_configuration_file(path):
"""
Read the configuration file and return a configuration object
"""
with open(path, "r") as configuration_file:
return json.load(configuration_file) | 425cbb9291cba0e4f54ce55f3bab681a322978ec | 162,306 |
def _get_token(credential) -> str:
"""Extract token from a azure.identity object."""
token = credential.modern.get_token("https://management.azure.com/.default")
return token.token | f11193743b91b1b7bf7fe0025c4e65d20410a4ab | 475,661 |
def aggregate_customers_sessions(sessions):
"""
Receives as input what products customers interacted with and returns their final
aggregation.
Args
----
sessions: list of list of dicts.
List where each element is a list of dict of type: [{'action': '', 'sku': ''}]
... | d500b11d66f26f4ba0ec99004ec84035a1f9b536 | 192,004 |
def sftp_prefix(config):
"""
Generate SFTP URL prefix
"""
login_str = ''
port_str = ''
if config['username'] and config['password']:
login_str = '%s:%s@' % (config['username'], config['password'])
elif config['username']:
login_str = '%s@' % config['username']
if config... | 225ae15212f7024590b1aa91f3ad7a32594cb9c3 | 41,250 |
def rgb_to_ansi256(r, g, b):
"""
Convert RGB to ANSI 256 color
"""
if r == g and g == b:
if r < 8:
return 16
if r > 248:
return 231
return round(((r - 8) / 247.0) * 24) + 232
ansi_r = 36 * round(r / 255.0 * 5.0)
ansi_g = 6 * round(g / 255.0 * 5.0... | bf5912b0f0be5b3c60e7ef7803d1e7f73c0573ff | 400,320 |
import functools
import time
def retry(exception_check, tries=3, delay=1, backoff=1):
"""Retry calling the decorated function using an exponential backoff.
original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
:param exception_check: ``Exception || Tuple`` the exception to check.
... | ae5f7616320fb64207e6c2aab3acc05a5e559c5a | 472,264 |
import json
def read_json(file_path: str) -> dict:
"""Reads a json file and returns the dict
"""
with open(file_path) as f:
return json.load(f) | 93df73379300052dbecc6706050b0779ac568b98 | 668,236 |
def sieve_of_eratosthenes(end: int, start: int = 2) -> list[int]:
"""compute all primes between certain bounds
:param end: upper bound for primes
:param start: lower bound for primes
:return: list of all primes between start and end
"""
prime = [True for _ in range(end + 1)]
p = 2
while ... | 24523c3037ea5671352a8baab83298908b6c26d8 | 268,012 |
def _ParseIssueReferences(issue_ref_list):
"""Parses a list of issue references into a tuple of IDs added/removed.
For example: [ "alpha:7", "beta:8", "-gamma:9" ] => ([ "7", "8" ], [ "9" ])
NOTE: We don't support cross-project issue references. Rather we
just assume the issue reference is within the same pro... | a8c8ebea8ebd289c84bd34bfdc064b8b90daf830 | 689,214 |
def get_duplicates(iterable):
"""Return a set of the elements which appear multiple times in iterable."""
seen, duplicates = set(), set()
for elem in iterable:
if elem in seen:
duplicates.add(elem)
else:
seen.add(elem)
return duplicates | 7e14f37a3819c6d7fe28c577173ba91aae2b687f | 631,773 |
def SimpleElement(tag, value):
"""
Args:
tag: xml tag name
value: character data
Returns:
XML: <tag>value</tag>
"""
return '<%s>%s</%s>\n' % (tag, value, tag) | 8684d661f0fbf04c6d6cb4153041201378a258cc | 65,422 |
def _escape_html(text):
"""Escape text for inclusion in html"""
return (
text.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace(" ", " ")
) | 8f194b03630f64fcc7f64ff26b22c2f80f8fba2b | 459,950 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.