content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
def float_one(vals):
""" Return a constant floating point value of 1.0
"""
return 1.0 | 31ce58c2629fb36fd67d84ff440adae05a823c2e | 42,442 |
def get_public_key_location(private_key_location: str) -> str:
"""Returns the location of the public key based on the location of
the private key.
:param private_key_location: Private key location.
"""
return private_key_location + ".pub" | e415e3807db648a42cd40f2ade31a08979e58172 | 440,475 |
def FindActiveBundle(config):
"""Find the name of first active bundle in a config."""
bundle = next(b for b in config['bundles'] if b['active'])
return bundle['id'] | be0fb1e2cff864562390df84c07c85b6923a83fd | 435,683 |
import re
def clean_string(string):
"""
Clean a given string from Umlauts and unwanted characters to ensure
compatibility with the file system.
:param string: String that needs cleaning
:type string: string
:return: Cleaned string
"""
string = re.sub('Ä', 'Ae', string)
string = re.... | af92f79744e1d1497b468a2dd198b6afb9fd8685 | 460,349 |
import random
def parent_idxs_choice(sorted_idxs, n_total, **optional_args):
"""Returns the idexes of the new parents for the next iteration. Used in Scheduler.loop()
Args:
sorted_idxs (list): Indexes of the sorted loss for the previous iteration.
n_total (int): Length of the returned array.
... | 2224772d9586db9972fdf7dc21346e7ce370166d | 498,619 |
import torch
def gaussian_sample(mu, logvar):
"""
Sample from N(mu, Sigma):
z ~ mu + Cholesky(Sigma(x)) * eps
eps ~ N(0,I_n)
The variance is restricted to be diagonal,
so Cholesky(...) -> sqrt(...)
Parameters
----------
mu : torch.Tensor
Location parameter of Gaus... | c810ef61005c639ffa39c766251f4a4f5c266bb7 | 189,303 |
import cProfile
def run_profiler(func, *args, **kwargs):
"""Runs the cProfiler.
Args:
func (callable): The function to profile.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Returns:
cProfile.Profile: The profiler results.
"""
profi... | 845cc60b37233f43adb49fecdcf405b2c3de530b | 339,333 |
def get_spark_df(spark, data, schema):
"""
This function returns a spark data frame
:param spark: Spark session
:param data: data fields
:param schema: schema of the data fields
:return: spark data frame
"""
df = spark.createDataFrame(data=data, schema=schema)
return df | ee044f060f299b14dfa0242dc20f270a2223304d | 388,760 |
import importlib
def load_from_path(path):
"""
Load class or function from string path.
"""
module, attr = path.rsplit('.', 1)
mod = importlib.import_module(module)
return getattr(mod, attr) | 6404bbb1dd77bd611dff9dae4c967c99fd5fa22a | 251,578 |
import itertools
def take_line(iterable):
""" Advances the iterable to the next new line and returns the passed characters as a string.
The newline character is not included.
:param iterable: any iterable
:return: a string of the next line.
"""
return ''.join(itertools.takewhile(lambda c: c ... | 0b89ebf5b8bbf1e87c9cd99c7d7db2e808c2970f | 406,167 |
import pytz
def dt_to_ms(dt):
"""Converts a datetime to a POSIX timestamp in milliseconds"""
if dt.tzinfo is None:
dt = dt.replace(tzinfo=pytz.UTC)
return int(dt.timestamp() * 1000) | aa3050ce15e09b9c1ddeb1edbda8c6e4275f3ce6 | 18,693 |
def replace_segment(seq, start, end, replacement):
"""Return the sequence with ``seq[start:end]`` replaced by ``replacement``."""
return seq[:start] + replacement + seq[end:] | a19b052924f2dbef3fc44a109ab14b4cc07048d8 | 376,569 |
def np_to_xyz(
geom,
xyz2,
):
""" Convert from xyz array to xyz file format in order to write xyz
Params:
geom ((natoms,4) np.ndarray) - system reference geometry (atom symbol, x,y,z) from xyz file
xyz2 ((natoms,3) np.ndarray) - system geometry (x,y,z)
Returns:
geom2 (... | 253e31df4042f4957dbec1e23387261dfb7876b3 | 517,470 |
import torch
def expand_as_one_hot(input_, C, ignore_label=None):
"""
Converts NxSPATIAL label image to NxCxSPATIAL, where each label gets converted to its corresponding one-hot vector.
NOTE: make sure that the input_ contains consecutive numbers starting from 0, otherwise the scatter_ function
won't ... | 5ced4e82c37f1d803a4b92eb9e089a0d90e7226e | 119,129 |
def is_triangle(side_a, side_b, side_c):
"""Returns True if the input satisifes the sides of a triangle"""
return side_a + side_b > side_c and side_a + side_c > side_b and side_b + side_c > side_a | 1eb22261edb13c734430e3ef8207bd256436d1c5 | 628,916 |
def sipi(b3, b4, b8):
"""
Structure Intensive Pigment Index \
(Peñuelas, Baret and Filella, 1995).
.. math:: SIPI = b3/b8 - b4
:param b3: Green.
:type b3: numpy.ndarray or float
:param b4: Red.
:type b4: numpy.ndarray or float
:param b8: NIR.
:type b8: numpy.ndarray or float
... | b0a7181970e9165d0e75ab7319646fd6bd1c6bbd | 695,986 |
def partition(lis, predicate):
"""
Splits a list into two lists based on a predicate. The first list will contain
all elements of the provided list where predicate is true, and the second list
will contain the rest
"""
as_list = list(lis)
true_list = []
false_list = []
for l in as_list:
pred_value... | 0d1cdb7e410ccce46c02209df83d16d4f9823a2d | 48,468 |
def plot_line(m, line, colour='b', lw=1, alpha=1):
"""
Plots a line given a line with lon,lat coordinates.
Note:
This means you probably have to call shapely `transform` on your
line before passing it to this function.
There is a helper partial function in utils called `utm2lola` w... | 4cf04142b7205116ff52c93ab32825a4874a28db | 682,877 |
import logging
import re
def get_landsat_angles(productdir):
"""
Get Landsat angle bands file path.
Parameters:
productdir (str): path to directory containing angle bands.
Returns:
sz_path, sa_path, vz_path, va_path: file paths to solar zenith, solar azimuth, view... | 8b22cb6fc1ea8ae3f3869664aff2f165c418f20c | 24,364 |
def _get_subword_units(token, gram):
"""Return subword-units presentation, given a word/token.
"""
if token == '</s>': # special token for padding purpose.
return [token]
t = '#' + token + '#'
return [t[i:i + gram] for i in range(0, len(t) - gram + 1)] | 6790aeb5fdadc082bc472ef4d8dd149a61723a9c | 290,126 |
from typing import Iterable
from typing import Tuple
from typing import Any
from typing import Dict
def provider_dict(items: Iterable[Tuple[str, Any]]) -> Dict[str, Any]:
"""Prepare provider data for JSON dump"""
output = {}
for key, value in items:
if key == "ranges":
output[key] = {
... | 0ec3170d6f198b5cc61341fb97a4baa24391a13b | 281,387 |
def qw(s):
"""
Examples :
>>> print(qw(“ -correlated-propagation -scalarrepl -lowerinvoke”))
(-correlated-propagation, -scalarrepl, -lowerinvoke)
Args:
s (str): s is a list of all the possible passes that can be used (the passes shoul dvbe separated by whitespace).
Returns:
Returns a tuple of... | 9e0ec379da947e6210115cc4be4265414929e5d6 | 115,172 |
def admin_display(short_description):
"""
A decorator that will make a method a display method
for django admin. Basically adding a name to it.
"""
def inner(func):
func.short_description = short_description
return func
return inner | e609f2530a1c9e736a2216a99d9142027d955a97 | 266,318 |
import itertools
def sumlist(x):
"""
sumlist(x)
Returns the running sum of a list of numbers, x. For example:
sumlist([1,2,3]) would return [1,3,6].
"""
try:
return list(itertools.accumulate(x))
except TypeError:
raise TypeError("unsupported argument type for 'sumlist'") | 11250fff0b1794accd2fccf53887480b2879cfd9 | 506,925 |
def get_best_response_actions_as_string(best_response_actions):
"""Turns a dict<bytes, int> into a bytestring compatible with C++.
i.e. the bytestring can be copy-pasted as the brace initialization for a
{std::unordered_,std::,absl::flat_hash_}map<std::string, int>.
Args:
best_response_actions: A dict map... | cf2b475d6bb76d262c17dc7753f1624e38cc69f4 | 178 |
def csv_append(csv_string, item):
"""
Appends an item to a comma-separated string.
If the comma-separated string is empty/None, just returns item.
"""
if csv_string:
return ",".join((csv_string, item))
else:
return item | 38d1bde31225a8bf42156f3472a8bad99d3d9dfb | 61,650 |
def _check_n(fields, name, dim=0):
"""
Check the field consistency
Args:
fields (list): a list of fields
name (str): name to use when raising error
dim (int): dimension for comparing
Returns: int or None
"""
n_candidates = []
for i in fields:
if i is not None... | 0b8086f9e88363db414be8cf2fcab6d893c2978a | 333,000 |
import torch
def _compare_fn(preds, target) -> dict:
"""Comparison function for map implementation.
Official pycocotools results calculated from a subset of https://github.com/cocodataset/cocoapi/tree/master/results
All classes
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=... | aa69c14e57eb3ba08765e06a4c888f2e62eadb90 | 406,788 |
import re
def escape_ansi(line):
"""Remove colors from error message."""
ansi_escape = re.compile(r"(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]")
return ansi_escape.sub("", line) | 829c69d83776b1e0a80b5a6a2fcd76b2e054b9fa | 378,456 |
def tests_generator(slug, names):
"""
Returns a string containing repetitive tests.
:param str slug: Slug containing protoype test.
:param list names: Names to be substituted into the slug.
"""
tests = ""
slug = slug.strip()
for name in names:
tests += slug.replace("%s", name)
... | 273fdc4c0cdc9d9ec6628d86a870c9b5d852e72f | 268,806 |
def matching_subset(main, sub):
"""check that all the keys in a dictionary are in sub and agree with main
Example:
main = {"a": 3, "b": 4, "c": 10}
sub1 = {"a": 3, "c": 10}
sub2 = {"a": 3, "foo": 5}
sub3 = {"a": 0, "b": 1}
sub1 is a "matching subset" of main since all its key... | a1b541b93897866a8ffeb79bd963d136ca042f5e | 509,405 |
def format_value(text):
"""
Format text. Replaces newlines and carriage returns with spaces so that
each line of the output is one document.
"""
return text.encode('utf8').replace('\n', ' ').replace('\r', ' ') | 475dc34a1ad3588bf8ef103a53fe09582a25b002 | 178,832 |
def rna_id(entry):
"""
Get the UPI for the entry, or fail if there is none.
"""
if entry["DB"] == "RNAcentral":
return entry["DB_Object_ID"]
raise ValueError("All entries are expected to come from RNAcentral") | 2990b6eb29f7d5f3e8c8e3cf8ef50d5f7ccd81f5 | 147,789 |
def build_xaxis(num, radius_increment=0.05):
"""Calculate the radius/diameter for the x-axis in plots."""
x_axis = {
'radius': [],
'diameter': []
}
radius = 0
for _ in range(num):
radius = round(radius, 2)
diameter = round(radius * 2, 1)
x_axis['radius'].append(radius)
x_axis['dia... | f3de8bb1abb6190350f2939ea12148b39ffe4ed8 | 477,504 |
from typing import List
from typing import Sequence
from typing import Tuple
def get_metadata_indices(license_numbers: List[int], start: int = 0) -> Sequence[Tuple[int, int]]:
"""Returns the metadata indices range in the license numbers array
Args:
license_numbers (List[int]): [description]
s... | 1ad51d1d75df939732f6f7e92f77df615b31f822 | 562,273 |
import random
def generate_random_sequence(length, prob_dist=[0.25, 0.25, 0.25, 0.25]):
""" generates a DNA sequence given length according to the given nucleotide distribution """
seq = ''
for p in range(length):
n = random.random()
if n < prob_dist[0]: seq += 'A'
elif n < sum(prob_dist[0:2]): seq += 'C'
... | 35d3eb9e92f2b4cf2ac23d018c44c94c8b1629ce | 598,174 |
def is_intel_email(email):
"""Checks that email is valid Intel email"""
return email and len(email) > 10 and " " not in email and email.lower().endswith("@intel.com") | 2eb72e4cbe61fbf9c2b4b28d39c0e4bb62d6a324 | 148,963 |
def missing_integers(l):
"""Find the gaps in a sequence of integers"""
return sorted(set(range(min(l), max(l) + 1)) - set(l)) | 5322f2d618c44e6765c64092e67420859fe61f0e | 192,237 |
def user_key2(application2):
"""User key for second application"""
key = application2["user_key"]
return key | 05b241dc708344dbe8a3aa3015e7b8774e6f6cb0 | 497,855 |
def getQaMask(img):
"""Gets the BQA quality band as boolean layer.
1 indicates a good pixel and 0 indicates a bad pixel.
Args:
img: An ee.Image originating from msslib.getCol().
Returns:
A image with one boolean band named BQA_mask.
"""
return img.select('BQA').eq(32).rename('... | fd8a54d4a26dfd58b3faffaa3561cca6d0e933c5 | 300,117 |
def get_comm_cost(tensor_size, coef, intercept):
"""Returns communication cost (in microsecs) for the given tensor size."""
return int(round(coef * tensor_size + intercept)) | 47e52cff6c6f22a126e1e4f67f7919903583c6eb | 210,210 |
def encode_address(address: int) -> bytes:
"""Encodes an HDLC address as a one-terminated LSB varint."""
result = bytearray()
while True:
result += bytes([(address & 0x7f) << 1])
address >>= 7
if address == 0:
break
result[-1] |= 0x1
return result | ee68f5f73737146550d19c540fa1f3123d2acd45 | 353,337 |
def noneorcomplex(s):
"""Turn empty or 'none' to None, else evaluate to complex."""
if s.lower() in ('', 'none'):
return None
else:
return complex(eval(s, {}, {})) | 81154ef925fbcd695e90c33f3620ba8ba75b2832 | 475,574 |
def load_filepaths(filename):
"""Read in a list of file paths.
Args:
filename: A text file containing a list of file paths. Assume that
each line has one file path.
Returns:
filepaths: A list of strings where each is a file path.
"""
with open(filename) as f:
filepa... | 2475a28b2afaf8380502270d79bc2f756dd5a25e | 628,679 |
import random
def stat_check(stat1, stat2):
"""
Checks if stat1 wins over stat2 in competitive stat check.
"""
roll1 = random.randrange(stat1)
roll2 = random.randrange(stat2)
return roll1 >= roll2 | 64b96fad051d20c182dfbf719403590951f1b13c | 681,616 |
from pathlib import Path
from typing import Optional
def secret(
name: str, secrets_dir: Path = Path("/run/secrets")
) -> Optional[str]:
"""Load the named secret from /run/secrets and return None otherwise."""
path = secrets_dir / name
if path.exists():
return path.read_text().strip()
... | 47ad7381157fa469b963c7fc769611c39713044d | 592,065 |
def _join_lines(lines):
"""
Simple join, except we want empty lines to still provide a newline.
"""
result = []
for line in lines:
if not line:
if result and result[-1] != '\n':
result.append('\n')
else:
result.append(line + '\n')
return ''.join(result).strip() | 83735124391dde628e4eb651dc663057a0b54eea | 390,859 |
def get_list_inverse_index(unique_ids):
"""Get value to position index from a list of unique ids.
Args:
unique_ids: A list of unique integers of strings.
Returns:
result: a dict from value to position
Raises:
TypeError: If unique_ids is not a list.
"""
if not isinstance(uniq... | d2ff690106271b18784208f953e4e98d7f74840f | 576,201 |
def get_dense_network_shapes(n_layers, hidden_size, n_features, n_outputs):
"""
Helper function to generate the input/output shapes for the layers of a densely connected network
:param n_layers: Number of hidden layers in the network
:param hidden_size: How many hidden neurons to use
:param n_featur... | ea5e74fcdc3fe0b923f1377e202284f0576bff87 | 32,112 |
def split_filtStr(filtStr):
"""
...doctest:
>>> split_filtStr('a>b;c<=d; e == f; bc=[0,1]')
['a>b', 'c<=d', 'e == f', 'bc=[0,1]']
>>> split_filtStr('a>b AND c<=d AND e == f')
['a>b', 'c<=d', 'e == f']
>>> split_filtStr('bc=[0,1]')
['bc=[0,1]']
>>> split_f... | 2f2221afeb53ac4bfa626dfb4b7ad3082fc2c7c1 | 636,951 |
def error_response(status:str, message:str):
"""
This function returns a dictionary of status and message
:param status: The status code of the response
:type status: str
:param message: The message you want to display to the end user
:type message: str
:return: A dictionary o... | 574ffb9644793633348201d78e9aa2870067e994 | 341,569 |
def barxor(a, b): # xor two strings of different lengths
"""XOR the given byte strings. If they are not of equal lengths, right pad the shorter
one with zeroes.
"""
if len(a) > len(b):
return [ord(x) ^ ord(y) for (x, y) in zip(a[:len(b)], b)]
else:
return [ord(x) ^ ord(y) for (x, y) in zip(a, b[:l... | cbe2a490eb1ee901160511665f948b6699b20015 | 221,009 |
def _object_lookup_to_pk(model, lookup):
"""Performs a lookup if `lookup` is not simply a primary key otherwise is
returned directly."""
try:
pk = int(lookup)
except (TypeError, ValueError):
# otherwise, attempt a lookup
try:
pk = model._default_manager.get(**lookup).pk
... | 20d282dc9c85313c71d16b878584cb155f538ecd | 468,812 |
def compute_Rn(albedo_c, albedo_s, T_air, T_c, T_s, e_atm, Rs_c, Rs_s, F):
"""Compute Soil and Canopy Net Radiation
Parameters
----------
albedo_c : ee.Image
albedo_s : ee.Image
T_air : ee.Image
Air temperature (Kelvin).
T_c : ee.Image
Canopy temperature (Kelvin).
T_s : ... | bde06911937a44d479999854d224d07bc89b15a9 | 342,921 |
def a_send(text, ctx):
"""Send text line to the controller."""
ctx.ctrl.send(text)
return True | 7a5b8412f5099138afedc892c893f413ab4eba21 | 35,766 |
def get_duplicate_indices(words):
"""Given a list of words, loop through the words and check for each
word if it occurs more than once.
If so return the index of its first ocurrence.
For example in the following list 'is' and 'it'
occurr more than once, and they are at indices 0 and 1 so you would
... | 0ea94957d5ff8ae9cd7488ea9b4adcf33020ec2c | 663,941 |
def _to_time(integ, frac, n=32):
"""Return a timestamp from an integral and fractional part.
Parameters:
integ -- integral part
frac -- fractional part
n -- number of bits of the fractional part
Retuns:
timestamp
"""
return integ + float(frac)/2**n | b558806f174f5305bf1c1b0c20c2a5fab8331037 | 666,071 |
def t_pulse(t_2, f_2, f, DM):
"""
Function for generating the time values of a pulse given the frequency
values. Eq. 1 in FDMT paper.
t_pulse, t_2 in s
f_2, f in MHz
DM in pc cm^-3
"""
return t_2 + 4.148808*DM*((f/1000.)**(-2) - (f_2/1000.)**(-2))/1000. | 12604a6ea9eb7a808eacb4fbeff972d5180c0429 | 607,236 |
def merge_dict(*args):
""" Merges any number of dictionaries into a single dictionary.
# Notes
In Python 3.5+, you can just do this:
```python
r = {**x, **y}
```
But if you want a single expression in Python 3.4 and below:
```python
r = merge_dict(x, y)
```
"""
result = {}
... | 9211ad5c859609194199e02960af497373085eb5 | 568,717 |
def _check_level_to_classname(check):
"""
Return the appropriated css classname for the check level
"""
if check.has_failed:
if check.level == check.LEVEL_ERROR:
return 'danger'
elif check.level == check.LEVEL_WARNING:
return 'warning'
else:
re... | 3c2e92eaae71fc04e356a0953d2efd86b5494e46 | 523,822 |
def rgb2hex(rgb):
"""Converts RGB colours to HEX
:param rgb: RGB colour
:return: HEX colour as a string with a '#'
"""
return '#' + '%02x%02x%02x' % (rgb.r, rgb.g, rgb.b) | 7ddd7fab7840a42b2c090d107ffbf60d0ac8b630 | 52,856 |
def _parse_outputs(outputs_data):
"""
Parses outputs from .tfstate file
:param outputs_data: dict
"output": {
"value": string,
"type": string
}
:return: dict, with the following structure:
{
"{name}": string,
}
"""
res_outputs = {}
for name, value... | 5a732a9d23e6e2d2480ce623e30e6cdecaad80bb | 599,859 |
import re
def to_snake_case(text: str):
"""
Convert string to snake_case.
Example:
'Hi there!' -> 'hi_there_'
"""
return re.sub("\W+", "-", str(text)).lower() | c6d6962d4b5fa34f1bbe3cd762c9871cf4a5e3bd | 128,480 |
from typing import Optional
from typing import Callable
import functools
from typing import Any
import warnings
def deprecated(*, deadline: str, fix: str,
name: Optional[str] = None) -> Callable[[Callable], Callable]:
"""Marks a function as deprecated.
Args:
deadline: The version where... | 2a7f27ed60bc10d6dcfb2ddf68d17230206e8a0b | 604,733 |
def _get_qpoint_pos(pos):
"""Return the coordinates of a QPointF object."""
return pos.x(), pos.y() | 5f7c850599bacc335dbcba73cb23922abccc0107 | 58,567 |
def has_no_numbers(value):
"""Checks if the string does not contains numbers"""
if isinstance(value, str):
return not(any(char.isdigit() for char in value))
return False | 97428385a68bd461d5cad3528e38ecc1861b2828 | 100,638 |
def get_circle_points(xy, radius):
""" Returns tuples of (x0, y0), (x1, y1) for a circle centered at x, y with radius
Arguments:
xy: tuple of x, y coordinates
radius: radius of circle to draw
Returns:
[(x0, y0), (x1, y1)] for bounding box of circle centered at x, y
"""
x, y... | d89145d1ec5ede042003fc9f952e78f6c136424c | 124,069 |
def listify(value):
"""
Convert an option specified as a string to a list. Allow both
comma and space as delimiters. Passes lists transparently.
"""
if isinstance(value, (list, tuple)):
# Already a sequence. Return as a list
return list(value)
else:
# assume `value` is a... | 8b8636884ecaddf9f558ca8cb4d80583c01b59bf | 653,041 |
def base_cli(parser):
"""Generate CLI with arguments shared among all interfaces"""
parser.add_argument('out_dir', type=str,
help='The path to the output directory. Created if it'
'does not already exist')
parser.add_argument('--regressor_files', nar... | d6c4249e3cb6ae6859563d282b9a4911ea36dfc0 | 375,961 |
def _host_is_direct_connect(host: str) -> bool:
"""Check if a host is a unifi direct connect domain."""
return host.endswith(".ui.direct") | 343019500fce8a4b8d977a6a224252603c332471 | 588,194 |
def prime_sieve(maximum) -> list:
"""Return a list of primes < maximum."""
maximum = int(maximum)
prime_list = []
primalities = [1] * maximum
for i in range(2, maximum):
if primalities[i]:
prime_list.append(i)
for j in range(i, len(primalities), i):
pr... | 74779acb8aba278fd80e15130231c1304d8a4cba | 534,655 |
def reverter_1(frase: str) -> str:
"""
>>> reverter_1('the sky is blue')
'blue is sky the'
>>> reverter_1('uma baita frase')
'frase baita uma'
"""
lista_de_palavras = frase.split() # Tempo e memória linear
palavras_reordenadas = reversed(lista_de_palavras)
return ' '.join(palavras_... | bff89726903c0d50cc7e6d61a888bad041518ae9 | 94,134 |
def validate_provider_name(name, supported_majors):
"""
Validates the provider name against the format in the specification:
<protocol-version>.dnscrypt-cert.<zone>
"""
try:
version, cert, _ = name.split('.', 2)
return version in supported_majors and cert == 'dnscrypt-cert'
exce... | 83af6362dce2b28b83f904fd635c0988722acddb | 377,972 |
def respond(input_string, answers):
"""Finding the imput string in the dictionary and return the value
Parameters
----------
input_string : string
String to look for as the keys in the dictionary of answers.
answers : dictionary
dictionary of answers
Returns
--... | 4a49cc3fef326da54769bb94b40847e4df2096f4 | 160,628 |
from typing import List
def gen_table_str(class_list: List) -> str:
"""
给定聚类后的类别列表,返回头展开形式的广义表,用于给Java后台解析
:param class_list: 聚类后的类别列表,如[1 0 1 1 1 0]
:return: 头展开形式的广义表,如((0,2,3,4),1,2)
"""
# 构件越多的放在越里面,所以这里维护几个集合
# 然后按集合的元素个数从大到小排序,再去生成结果
# 类别号 -> 构件下标集合
set_dict = dict()
for ... | ea2a8c897f28655b74ebbc9ff32c9bb342ceaff3 | 553,503 |
def moira_user_emails(member_list):
"""
Transform a list of moira list members to emails.
Assumes kerberos id => <kerberos_id>@mit.edu
Args:
member_list (list of str): List of members returned by Moira
Returns:
list of str: Member emails in list
"""
return list(
map... | 0fea9c6140a4ee6e79960ee97cbce721cda931fe | 475,282 |
def is_verb(string):
"""
Check if a predstring is for a verb or a noun
"""
return string.split('_')[-2] == 'v' | a25210710a82d031ff49a2b9d7fc5e86f0f8d2a6 | 199,864 |
def rescale_list_to_range(original, limits):
"""
Linearly rescale values in original list to limits (minimum and maximum).
:example:
>>> rescale_list_to_range([1, 2, 3], (0, 10))
[0.0, 5.0, 10.0]
>>> rescale_list_to_range([1, 2, 3], (-10, 0))
[-10.0, -5.0, 0.0]
>>> rescale_list_to_rang... | bdd38bb24b597648e4ca9045ed133dfe93ad4bd8 | 706,047 |
def all_phases_positive(pha):
"""
Converts an array of phases to be positive definite.
:param pha : 1-dimensional ndarray
The phases to be modified.
:return:
pha: 1-dimensional ndarray
Positive definite version of pha.
"""
while not (pha >= 0).all(): # make sure that all elements... | fcb67d95f5516758d1d28cccbdded543d0885c06 | 252,859 |
from typing import List
def permutar(arr: List[int]) -> List[List[int]]:
"""Devuelve todas las permutaciones del array.
:param arr: Lista de enteros únicos.
:arr type: List[int]
:return: Lista de permutaciones.
:rtype: List[List[int]]
"""
if len(arr) == 0:
return []
if len... | 98af485e0834d5f108312fa188d3e21fc23bec8e | 693,217 |
def parse_metrics(metrics: tuple) -> dict:
"""
Parse and convert the metrics captured by get_metrics() into a dictionary.
The main role of this is to parse the enormous dict returned by docker.container.stats and just
retrieve the memory usage as a list. NOTE: memory_usage_mb is total usage in megabyte... | 0dd726c39662c680f6c5d3e18585bce20391d2a6 | 582,984 |
import zipfile
def scan_zip(filepath):
""" Scan the files inside of a .zip archive. """
return zipfile.ZipFile(filepath).namelist() | 47a2bec01e9c4c2d544c42cbbeb442d677a2e378 | 516,814 |
from datetime import datetime
def parse_date(date_string):
"""Parse a date string with the format '%Y-%m-%dT%H:%M:%S.%fZ'.
Args:
date_string (str): string with the format '%Y-%m-%dT%H:%M:%S.%fZ'
Returns:
datetime:
"""
return datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%fZ') | ee5ef83cad13259640c750b05c9f046df2827b1a | 175,904 |
def pca_dim_reduction(x, basis, xmean=None):
"""(x, basis, xmean) --> xnew
Dimensionality reduction with PCA.
x: N*D data matrix
basis: D*K basis matrix
xmean: 1-D vector, mean vector used in PCA, if not set, use the mean of x instead
xnew: N*K new data matrix
"""
if xmean is None:
... | 63ce86d46c38d9d8cfda646890ae45c237e97c79 | 451,865 |
def validate_sql_select(value: str) -> str | None:
"""Validate that value is a SQL SELECT query."""
if not value.lstrip().lower().startswith("select"):
raise ValueError("Incorrect Query")
return value | 1cd221373adee7d0a06d30c39b82d6610273463f | 126,899 |
def delegate(session_attributes, slots):
"""
Defines a delegate slot type response.
"""
return {
"sessionAttributes": session_attributes,
"dialogAction": {"type": "Delegate", "slots": slots},
} | 6b1bc9d72b2e83a35516ef461d6d2517d458e36e | 173,919 |
import torch
def decimate(tensor, m):
"""
Decimate a tensor by a factor of 'm', i.e. downsample by keeping every 'm'th value.
"""
assert tensor.dim() == len(m)
for d in range(tensor.dim()):
if m[d] is not None:
tensor = tensor.index_select(
dim=d, index=torch.ar... | f646aebf6e2a3f5998f3be0cd622874c7cd93df7 | 441,980 |
def combine_first_last_name(df):
"""
Combine first and last name if df has those columns.
"""
if 'first_name' in df.columns and 'last_name' in df.columns:
df['full_name'] = df['first_name'] + ' ' + df['last_name']
df.drop(['first_name', 'last_name'], axis=1, inplace=True)
return df | ae10f70b86c485e0ed65cbdb44036f03b485250a | 655,644 |
from typing import List
from typing import Tuple
def iter_together(path_1: str, path_2: str) -> List[Tuple[str, str]]:
"""Open two files and iterate over them together.
:param path_1: The file path of the left file
:param path_2: The file path of the right file
:returns: Pairs of lines for the two fi... | dfa770408cc39fdfb0fd8f2b0ae6601fdecd4341 | 192,526 |
def import_from_string(import_string):
"""
Import a class from a string
import_string - string path to module to import using dot notation (foo.bar)
"""
import_split = import_string.split(".")
import_class = import_split[-1]
module_path = ".".join(import_split[:-1])
mod = __import__(modu... | 9176fa1716a2c09b20dfe5fcb6743aebccf94823 | 163,091 |
def _read_headers(entry_file,header_size):
""" Read the HTTP header (stream 0 data) from a cache entry file """
# read header_size bytes to get the raw bytes of the HTTP headers
# parse the raw bytes into a HttpHeader structure:
# It is a series of null terminated strings, first is status code,e.g., "HT... | 60fb71a292f20e0609848987c8e6677636f22de8 | 132,218 |
def lcs_analysis(Nb,Ne,Mb,Me,lcs,identical,equivalent,different):
"""This routine is derived from lcs_to_diff. Instead of writing out the
diff based on the snake list it analyses the snake list and establishes
whether the reference file and the data file are identical, equivalent,
or different.
... | e85f06d3733acd5e20db40c44c8d479e2ecd68b9 | 90,442 |
def shorten(string, length):
"""Shorten a string to a specific length, cropping in the middle"""
len2 = length // 2
len3 = length - len2 - 1
lens = len(string) + 2
if lens > length:
return ("[\033[32m{}…{}\033[0m]".
format(string[:len2], string[lens - len3:]))
return ("[\... | 86d9e6f990c0f2a98d349804360da1338005fe94 | 252,498 |
def _query_item(item, query_id, query_namespace):
"""
Check if the given cobra collection item matches the query arguments.
Parameters
----------
item: cobra.Reaction or cobra.Metabolite
query_id: str
The identifier to compare. The comparison is made case insensitively.
query_namesp... | f3e418ab5cf2830d2c1dd6b4e83275e14dc8f4c8 | 683,020 |
import torch
def _get_corners_3d(dim, o=[0] * 6):
"""Get eight corners of 3D image volume.
Parameters
----------
dim : (3,), list or tuple
Image dimensions.
o : (6, ), default=[0] * 6
Offsets.
Returns
-------
c : (8, 4), tensor_like[torch.float64]
Corners of v... | de528426e853c020455f85643bdb3b714f5bb79c | 604,762 |
import tqdm
def apply_groups(groups, client, func, *args, return_futures=False,
progress_bar=True, **kwargs):
""" Call `func` on each group in `groups`.
Additionally, `args` and `kwargs` are passed to the function call.
Parameters
----------
groups: pandas.DataFrameGroupBy
The re... | 5bca1f2eb0945fa72c517134b0ea4deaf61ecd23 | 631,014 |
def cropToROI(input_image,
SIDE_MARGIN=(20.0, 80.0),
TOP_BOTTOM_MARGIN=(20.0, 80.0)):
"""
Method:
Crops image to include only the
Region of Interest (ROI)
Parameters:
input_image - Image to crop.
SIDE_MARGIN - Tuple of margins on left... | 6663b5be21001cc95d83b15456dd0469ef1a43d2 | 229,403 |
from pathlib import Path
def get_project_dir() -> Path:
"""
Returns the project directory Path object
:return: Path
"""
return Path(__file__).parent.parent.absolute() | 82e07dab73d1cfee18ec77663d22caa992d10f6c | 583,198 |
def rescale(value, max_range=0.6, min_range=0.3):
"""
rescale deltaG values by random distribution
:param value: actual value
:param max_range: max range
:param min_range: min range
:return: rescaled value
"""
scaled = value
if value > min_range:
scaled = (value) * (max_rang... | f92850d428ce3af836b8f18c22dd98a62808c195 | 263,340 |
def read_lines(filename="", nb_lines=0):
"""read n lines of file"""
i = 0
with open(filename, "r") as my_file:
for line in my_file:
i += 1
print(line, end="")
if i == nb_lines:
break
my_file.close()
return (i) | 88daa680ff27f4053788ab9073c6be30942ade1f | 333,943 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.