content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
import re
def is_regex_match(regex_pattern, search_context):
"""
Wrapper method to perform regex searches on a passed in regex pattern and a string
If the search context matches the regex_pattern, returns True. Else false.
"""
regex_search = re.compile(regex_pattern)
if regex_search.match(se... | 7c5087a107fa0a8f77fafbf00b88fc24c64a2de1 | 450,478 |
def _ToYAMLDefault(appinfo_config):
"""Converts an app config to default (alphabetical by key) YAML string.
Args:
appinfo_config: appinfo.AppInfoExternal object. Configuration object
for either a module or app.yaml.
Returns:
String containing YAML for the app config object.
"""
return appinfo_... | dee59899a30a1ebc00af497ee82cd08e6e7ba01c | 317,187 |
import math
def _round_to_nearest_multiple_down(x, n=5):
"""Round down from x to the nearest multiple of n."""
return n * math.floor(float(x) / n) | fb937b48e1f6f682c46ac76c7d94ffb1da3c8818 | 381,529 |
import six
def msg2geojson(msg):
"""
**Experimental**
Convert a single positional message to GeoJSON. `lat` and `lon` are used
to create the geometry and all other fields are placed in the `properties`
key.
Input:
{
"radio": 0,
"course": 321.1000061035,
... | 16ffc1c1905379b848212bdf118f6451c6655b67 | 159,139 |
def o_counter(listy):
"""
Input: A list of numbers.
Ouptut: The number of entries in a list, using the len function.
"""
return (len(listy)) | 80cfe29cd8bc132fbd877598923b4d0d823ada36 | 615,608 |
def csv2list(filename, column, lower=True, header=True, dedupe=True): #------<<<
"""
Create a list from a column in a CSV file.
filename = name of .CSV file
column = column # (0-based) to be returned as a list
lower = whether to make the values in the list lowercase
header = whether .CSV file h... | cf85b45d3ff7e58971e3f06ea23081d0011aa00a | 308,160 |
def get_google_access_groups_for_service_account(service_account):
"""
Return list of fence.models.GoogleBucketAccessGroup objects that the
given service account should have access to based on it's access
privileges.
Args:
service_account (fence.models.UserServiceAccount): service account
... | 6408d0e44c0f26a537e4e329967abd73a59883d7 | 298,217 |
import math
def standard_logistic_function(x: float) -> float:
"""
Computes value of standard logistic function.
:param x: Point to be evaluated
:return: Value of logistic function evaluated in x
"""
return 1 / (1 + math.exp(-x)) | 5cc6559949f3d20064432dc0a3d1c60698f253bf | 141,496 |
def map_tcp_flags(bitmap):
"""
Maps text names of tcp flags to values in bitmap
:param bitmap: array[8]
:return: dictionary with keynames as names of the flags
"""
result = {}
result["FIN"] = bitmap[7]
result["SYN"] = bitmap[6]
result["RST"] = bitmap[5]
result["PSH"] = bitmap[4... | f6388e3aa6a7540df8b26ba2061be5807f6c19be | 72,749 |
from typing import Dict
def merge_dict(dest: Dict, src: Dict, path=None, overwrite=True):
"""
Merges src dict into dest dict.
Parameters
----------
dest: Dict
dest dict
src: Dict
source dict
path: List
merge path
overwrite: bool
Whether overwrite dest d... | a9d25f2fff6755f0d947b319182e4d0dfbd22f81 | 618,501 |
import torch
def model_to(on_gpu, model):
"""Transfers model to cpu/gpu.
Args:
on_gpu (bool): Transfers model to gpu if True otherwise to cpu
model (torch.nn.Module): PyTorch defined model.
Returns:
torch.nn.Module:
The model after being moved to cpu/gpu.
"""
... | c8731f84448cf6fccc387fe50b359e118cc2ce5e | 670,773 |
def Data_Type(data):
"""
This will return whether the item received is a dictionary, list, string, integer etc.
CODE: Data_Type(data)
AVAILABLE PARAMS:
(*) data - This is the variable you want to check.
RETURN VALUES:
list, dict, str, int, float, bool
EXAMPLE CODE:
test1 = ['this','is','a','list... | 1720c922905671241e36459bb9c13020d3ac6cae | 459,560 |
def binary_search(seq, f, target):
"""Find no such that f(seq[no-1]) <= target and f(seq[no]) > target.
If target < f(seq[0]), return 0
If target > f(seq[-1]), return len(seq)
Assume f(seq[no]) < f(seq[no+1]).
The integer result is useful for indexing the array.
Args:
seq: sequence o... | fa3187d6d12955cc6bc66c98b5c8b31e5b8b0e61 | 542,173 |
import json
def load_tweets(path):
"""Loads tweets that have previously been saved as a json file.
Calling load_tweets(path) after save_tweets(tweets, path)
will produce the same list of tweets.
Args:
path (str): The place where the tweets were be saved.
Returns:
list: A... | 85e6933aa78db2e76a5c3e4abcdda897deabe3bf | 235,304 |
def key_in_dict(input_dict, input_key):
"""Method that checks if a key is in a dict. Returns a bool"""
return input_key in input_dict | 42c24889d7863fa3500a0f7a3eebd11b18e5bdfc | 426,477 |
def segment_segment_intersection_2(x1, y1, x2, y2, t):
""" Return point of intersection between two segments given t value """
return [x1 + t * (x2 - x1), y1 + t * (y2 - y1)] | 75f68699e1c3adac8be23338052610bba19dde95 | 261,837 |
def PostMessage(session, queueurl, message):
"""Post a message to the given queue
:param session: Session to use for AWS access
:type session: boto3.session.Session
:param queueurl: URL of the queue to which we will post messages
:type queueurl: str
:param message: Body of message to post
:type message: ... | e62dbd044e3cdae2462b1a81c54d0faf5c0e7563 | 130,668 |
def _IsNamedTuple(x):
"""Returns whether an object is an instance of a collections.namedtuple.
Examples::
_IsNamedTuple((42, 'hi')) ==> False
Foo = collections.namedtuple('Foo', ['a', 'b'])
_IsNamedTuple(Foo(a=42, b='hi')) ==> True
Args:
x: The object to check.
"""
return isinstance(x, tupl... | 93c7438f5472c8d8b95b48aaced4833e0632c3c7 | 685,404 |
def factorial(n):
"""
Def: n mathematics, the factorial of a non-negative integer n, denoted by
n!, is the product of all positive integers less than or equal to n. For
example: 5! = 5 * 4 * 3 * 2 * 1 = 120
"""
result = 1
if n == 0:
return result
else:
for i in range(1,... | bcc7c7be75274e3cbfa9ad6dc751f2255dc86e24 | 347,656 |
import unicodedata
def strip_accents(line: str) -> str:
"""Strips accents from a piece of text."""
nfd = unicodedata.normalize("NFD", line)
output = [c for c in nfd if unicodedata.category(c) != "Mn"]
if len(output) == line:
return line
return "".join(output) | 4100a1748ec1911ef7fc11e0a625f295263381ac | 449,382 |
from datetime import datetime
def generate_folder(source_date):
"""
Generate partial folder name based on provided source_date
:param source_date: datetime
"""
assert isinstance(source_date, datetime)
date_part = source_date.date()
year = date_part.year
month = date_part.month
... | 651ff58e0a81b5708b7b99803686f2aeb849e56e | 221,852 |
def hello(name):
"""
Function that returns a greeting for whatever name you enter.
Usage:
>>> hello('Emiel')
'Hello, Emiel!'
"""
return ''.join(["Hello, ", name, '!']) | ffbdf2ee0869b3fd20f0cb85eff8eae748e30d28 | 49,609 |
from typing import Deque
def json_path(absolute_path: Deque[str]):
"""Flatten a data path to a dot delimited string.
:param absolute_path: The path
:returns: The dot delimited string
"""
path = "$"
for elem in absolute_path:
if isinstance(elem, int):
path += "[" + str(elem... | 7c61f784fa269925e42ac5f1cc3de9e2c55b9718 | 26,725 |
def complement(base):
""" Complement nucleotide """
d = {'A':'T', 'T':'A', 'G':'C', 'C':'G'}
if base in d:
return d[base]
return base | 213855c4ff3569c4469e6aadf50df18917d861f9 | 412,465 |
import json
def format_containers(containers, json_out):
"""Format container data for Ansible
Args:
containers: [(hostname, metadata), ...]
json_out: If True, return JSON, else dictionary.
Returns:
Dictionary of container information formatted to Ansible specs.
"""
data =... | cc25ceb34d2029e2f43f78d10f6dce0c4f3074aa | 105,459 |
def _get_objective_info(num_classes):
"""Provide information on classifier objective.
:param num_classes: integer greater than 1; number of classes
:return: dict with information on objective and evaluation metric for XGBoost
"""
if num_classes == 2:
return {"objective": "binary:logistic", ... | 3c0c7880cd21a7078cc27eafaf780ddfcdded0ed | 281,852 |
def logged_in_user(client, user):
"""Log the user in and yield the user object"""
client.force_login(user)
return user | 7177a2fff19951c8d0166a691303e12e17512b25 | 368,784 |
import hashlib
def md5_key_mangler(key):
"""Receive cache keys as long concatenated strings;
distill them into an md5 hash.
"""
d = hashlib.md5(key.encode('utf-8'))
return d.hexdigest() | 92edb4d2ce2a1c6c5d0724213aa854e9b34a4961 | 479,280 |
def some(predicate, seq):
"""If some element x of seq satisfies predicate(x), return predicate(x).
>>> some(callable, [min, 3])
1
>>> some(callable, [2, 3])
0
"""
for x in seq:
px = predicate(x)
if px: return px
return False | f39d8fb62081f06eaf229041423e50ca3b2b817d | 690,145 |
import typing
import glob
def find_requirement_files() -> typing.List[str]:
"""Find all requirements.txt files."""
return glob.glob('**/*requirements.txt', recursive=True) | b9a4729617049ad8ba53152fffd9368024f71203 | 86,438 |
def check_rack_labels(df_map):
"""Removing '.' for rack labels (causes execution failures)
"""
cols = ['TECAN_sample_labware_name', 'TECAN_primer_labware_name', 'TECAN_dest_labware_name']
for x in cols:
df_map[x] = [y.replace('.', '_') for y in df_map[x].tolist()]
return df_map | 9d30237412e3b1fb658ec705c89b9825503091b4 | 312,232 |
def card(id, title, s):
"""Build a card by ID."""
return """<card id="%(id)s" title="%(title)s"><p>%(s)s</p></card>""" % \
{'id': id, 'title': title, 's': s} | a112372f8fa7cc159df57cf465c2838114353992 | 593,542 |
def parse_tint(tint):
"""Parse tint string, returns tuple."""
if len(tint) == 0:
return None
return tuple(int(n) for n in tint.split(',')) | 5917f3bfd2bc206b385008d5bd3e318ccf4f2bc5 | 328,116 |
def sub_symbols(pattern, code, symbol):
"""Substitutes symbols in CLDR number pattern."""
return pattern.replace('¤¤', code).replace('¤', symbol) | 4d87e263ba53e99368fb82c8c93b3998834852ad | 38,800 |
from typing import OrderedDict
from typing import Type
import collections
def kwtypes(**kwargs) -> OrderedDict[str, Type]:
"""
This is a small helper function to convert the keyword arguments to an OrderedDict of types.
.. code-block:: python
kwtypes(a=int, b=str)
"""
d = collections.Ord... | 8024e6940f84f2eab8d4c44924889624d75ca3bd | 698,354 |
def lerp(x0, x1, t):
""" Linear interpolation """
return (1.0 - t) * x0 + t * x1 | 82d9ce36dd5879c7aab64dc5615a2fb298471383 | 143 |
from typing import List
import random
def uniformly_split_num(
sum: int,
n: int,
) -> List[int]:
"""Generate `n` non-negative numbers that sum up to `sum`"""
assert n > 0, 'n should be > 0'
assert sum >= 0, 'sum should be >= 0'
random_numbers = [random.randint(0, sum) for _ in range(n - 1)]
... | 8136cce9bd258e7b187646967402dc428f04a764 | 680,551 |
def externalarray(file_manager):
"""
An array of external array references.
"""
return file_manager.external_array_references | cee20da69b485ef2260e7222135cd58466d8ad49 | 637,333 |
def get_block_lines(b, mdata):
"""
Return the number of lines based on the block index in the RAW file.
"""
line_counts = [1, 1, 1, 1, 1, 4, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0]
if b == 5: # for transformer
if mdata[0][2] == 0: # two-winding transformer
return 4
els... | f00a8da81dde73b75564b8968be02c8597c0b1bc | 54,830 |
def remove_prefix(s: str, prefix: str) -> str:
"""Remove the prefix from the string. I.e., str.removeprefix in Python 3.9."""
return s[len(prefix) :] if s.startswith(prefix) else s | 5a2f74477ad38c3ba1a96e03560fc1c0d6664a91 | 117,870 |
import random
def rand_iterable(it, len_it=None):
"""
Choose a uniformly random value from the given iterable. If len(it) is
available, len_it may be None. Otherwise, len_it must be provided as the
"length" of the given iterable (the number of values which will be
produced).
random.choice()... | 8f97ba985adfa0f4c047f8e177f1e09066a564f2 | 350,981 |
def define_bounds(bounds):
"""
Extracts bounds from region drawn/defined
Args:
`bounds`= json bounds captured
Returns:
`bbox`= bounds of the region defined (list)
`bbox2`= bounds of the region defined (string)
"""
#ew_poly=GeoJSON(data=dc.last_draw)
new_poly=str(bounds)
if... | d7863135ca0c122acaaae0deb547a30bc763212c | 205,410 |
def flatten_dict(a_dict, parent_keys=None, current_parent_key=None):
"""Given a dict as input, return a version of the dict where the keys
are no longer nested, and instead flattened.
EG:
>>> flatten_dict({"a": {"b": 1}})
{"a.b": 1}
NB: The kwargs are only for internal use of the functi... | cdd69b1d5119d83c36266c13c5bec772ae138cb5 | 684,542 |
def dpdy(a, x, y, order=4):
"""Differential with respect to y
The polynomial is defined as p(x,y) = a[i,j] * x**(i-j) * y**j, summed over i and j
Then dp/dy = j * a[i,j] * x**(i-j) * y**(j-1)
Parameters
----------
a an array of polynomial coefficients in JWST arrangement.
The n... | b5d4bf2eb03a8583c3bf6c9b93e8384484a42d77 | 473,039 |
def x2trace(distance, interval):
"""Return the trace number closest to a given distance. The input distance is effectively dimesionless given that the interval is proportional to it. NB: Not sure if the returned trace is a python index or gpr trace but that needs to be specified here.
Attributes:
distan... | 7a9af5ff21293566993d2921ac0ff4df3ac780ac | 567,977 |
def get_field(proto_cls, field_name):
"""Return proto field from class and field name."""
return proto_cls.DESCRIPTOR.fields_by_name[field_name] | 86bbd22bb02fe84cf9e9f03f8e77be28c328a6ff | 189,863 |
def default_empty(default):
"""Check an input against its falsy value or return a default."""
def get_value(test_value):
if test_value:
return test_value
else:
return default
return get_value | b3ff34d8fb5d46a62ad11a439dad7d03fb4425f2 | 28,515 |
def validate_predefined_argument(argument_name: str, argument_value: object, argument_options: list) -> bool:
"""
Validate predefined argument is a valid option.
Args:
argument_name (str): The name of the argument to validate.
argument_value (object): The value of the argument to validate.
... | 1157ceb56e563701edf564c8ba685b0966320955 | 89,719 |
def to_numpy(a):
"""Convert tensors to numpy arrays."""
return [x.cpu().detach().numpy() for x in a] | 21c3261382e614131fee9c6e2e2b61da3679801a | 135,819 |
def parse_ipmi_sdr(output):
"""Parse the output of the sdr info retrieved with ipmitool"""
hrdw = []
for line in output:
items = line.split("|")
if len(items) < 3:
continue
if "Not Readable" in line:
hrdw.append(('ipmi', items[0].strip(), 'value', 'Not Reada... | 7525b21926ced8dc44f4730c283432b869905786 | 672,778 |
def int_to_bytes(value: int) -> bytes:
""" Return the bytes for a given integer. """
return value.to_bytes((value.bit_length() + 7) // 8, byteorder='big') | c4c5c02d437964e006acef6292ffffdf48c20c88 | 577,920 |
import pickle
def load_checkpoints(pklf_name):
"""
Loads in saved checkpoints from pickle file (used e.g. to repeat the analysis part...)
:param pklf_name: name of the saved pickle file
:return: obejects saved by BluePyOpt"""
with open(pklf_name, "rb") as f:
cp = pickle.load(f)
return ... | d485e30ddf99af968d4a20070a3cd4f3f88c0603 | 216,940 |
def convert36ToAscii(start, chs):
"""Convert decimal address to ascci address
:param start: NBP adress in decimal reverse:Big Endian Byte Order: The most significant byte (the "big end") of the data is placed at the byte with the lowest address
:param chs:tsring of all digits and ascii upper
:return:NB... | e8b00d5d0a49d4f128b84a4c874e268b5d92ca9e | 648,390 |
from typing import Callable
from typing import Dict
def expected_utility(u: Callable[[float], float], X: Dict[int, float]) -> float:
"""
効用関数uの下で確率くじXのもたらす期待効用を返します。
Parameters
----------
u: Callable[[float], float]
効用関数
X: Dict[int, float]
確率くじ。X[k] = p とすると確率くじXは確率pでk円になることを... | 50e9bbd5b5c4df8cf706c1050300931e0d79e46a | 560,282 |
import re
def convert_to_seconds(duration_str) -> int:
"""
for (s)econds, (m)inutes, (h)ours, (d)ays, (w)eeks
return duration in seconds
"""
seconds = 0
if re.match(r"[0-9]+$", duration_str):
seconds = int(duration_str)
elif re.match(r"[0-9]+s$", duration_str):
seconds = i... | e46ccb0d7b51c8c805f44b77f23fdfb18f6e0d97 | 374,013 |
def fn_filter_words_in_column(df, col_to_search, search_words, any_words=True):
"""Filter for any (default) or all words in a dataframe column. Case insensitive.
df: dataframe to search
col_to_search: column name to search as a str
search_words: list of search words
any_words: Default True to search... | c90fbc0ef0df1859049b37a90005d3ff901edfcf | 134,005 |
def lda_filter_articles(ids, articles):
"""From sets of ids to set of sentences (str)
:argument ids: dictionary (keys are topics and values are ids of articles
:argument articles: list of str
:returns a dictionary, keys are topics and values are sentences (str)
"""
articles_per_topic = {}
fo... | 20fbb9b5c64c491654b5999e93580167213c4f8e | 263,615 |
def intersect(list1, list2):
"""
Compute the intersection of two sorted lists.
Returns a new sorted list containing only elements that are in
both list1 and list2.
This function can be iterative.
"""
intersection = []
append = intersection.append
idx1 = 0
idx2 = 0
... | 36ead411915016330387e6e589a95cac9ebb62b7 | 518,222 |
from typing import Dict
from typing import List
def _get_available_sensor_identifier(pID_dict: Dict[str, Dict[str, List[str]]]) -> str:
"""Returns an available sensor identifier that has been recorded. Will favor
"name" over "mapped_id" over "sensor_id" over "number".
Parameters
----------
pID_di... | d7e92d84694b52fa1847801a833b8679be272a6a | 554,908 |
def invert_dict(d):
"""Inverts a dictionary, returning a map from val to a list of keys.
If the mapping key->val appears in d, then in the new dictionary
val maps to a list that includes key.
d: dict
Returns: dict
"""
inverse = {}
for key in d:
val = d[key]
inverse.setdef... | b9ec2128b8035a2c122bfd177fe92426a942e74d | 416,423 |
def turn_Series_into_string(series_obj):
"""
Turn a pandas Series object (of strings)
into one large string. Intended for text columns
in pandas DataFrames
:param series_obj : a pandas Series object (usually text column)
:returns long_string : string of the series_obj
"""
try:
... | a4dcbd86aeed245f15590604cc19c776ed65d8ad | 400,230 |
def parse(utxo, offset=0):
""" Parses a given serialized UTXO to extract a base-128 varint.
:param utxo: Serialized UTXO from which the varint will be parsed.
:type utxo: hex str
:param offset: Offset where the beginning of the varint if located in the UTXO.
:type offset: int
:return: The extrac... | 6330898ae2113370b1f1ec5e2b8ba0ec326eb48d | 689,066 |
def ubtou(str_in):
""" Shorthand for converting unicode bytes to UTF-8 """
if not isinstance(str_in, bytes):
return str_in
return str_in.decode('utf-8') | 9f9949e2b61cc73d4edde7295fd809f212414289 | 376,316 |
def get_base_model(model):
"""
For the given model, return the highest concrete model in the inheritance tree -
e.g. for BlogPage, return Page
"""
if model._meta.parents:
model = model._meta.get_parent_list()[0]
return model | bd8d717ae6806859e6ea7461e45ce0a0eb6cb922 | 468,926 |
import re
def get_genres_from_soup(soup):
"""Get the genres of a book.
Parameters
----------
soup : BeautifulSoup
BeautifulSoup object created from a book page.
Returns
-------
list
Book genres.
"""
genres_elements = soup.find_all('a', {'href': re.compile('/genres... | 16db0fc8cb58cdcf19aa89ea8fef27078d33a390 | 701,661 |
def check_if_site_is_ordered(structure, site_index):
"""Checks if a given site in a given structure is fully ordered.
Args:
structure (pymatgen.Structure): A pymatgen structure.
site_index (int): The site index.
Returns:
(bool)
"""
return (len(structure[site_index].species... | c73b2839bccbb1d520287188656666ec035bfa13 | 258,816 |
from typing import List
from typing import Tuple
def filter_multiple_pronunications(
lexicon: List[Tuple[str, List[str]]]
) -> List[Tuple[str, List[str]]]:
"""Remove multiple pronunciations of words from a lexicon.
If a word has more than one pronunciation in the lexicon, only
the first one is kept, ... | faa106c3b16e7efbcd49471714260cb734f72385 | 666,935 |
def wrap(func, keys):
"""
Primitive to wrap the output of a function into dictionary keys.
Args:
func: The function to enable output wrapping on.
keys: The name of the keys to use in the output dictionary.
"""
if type(keys) is not tuple:
keys = [keys]
def _wrapped(*args... | f3fcdfb2a2796fc09ff48386ac5e4131118cd041 | 109,040 |
def goal_key(tup):
"""Sort goals by position first and by id second (transform str ids into ints)"""
goal_id, goal_pos = tup
if isinstance(goal_id, str):
return goal_pos, int(goal_id.split("_")[0])
return goal_pos, goal_id | 88e51faf9ea6395cfb636b4c88057a662a2041c4 | 177,517 |
def get_games_for_module(module, active_only=False):
"""
For a given module return a queryset of related game postings.
"""
queryset = module.gameposting_set.all()
if active_only:
queryset = queryset.exclude(status__in=['closed', 'cancel'])
return queryset | a59798d7b829448670d89d281dfdd8e4d9a1989f | 255,015 |
def list_to_tuple(x):
""" Convert a list to a tuple recursively. """
assert isinstance(x, list)
return tuple(list_to_tuple(y) if isinstance(y, list) else y for y in x) | 38fded1bd81fd529fddfd33030c452bfd00bc898 | 198,173 |
import torch
def get_all_predictions(model, loader, device):
"""Get All predictions for model
Args:
model (Net): Trained Model
loader (Dataloader): instance of dataloader
device (str): Which device to use cuda/cpu
Returns:
tuple: all predicted values and their targets
... | aca84ffaa16408bc691d60c8207318c7e214a509 | 669,643 |
def get_utm_crs(lon_center, lat_center):
"""Derive CRS for UTM zone matching coordinates
Parameters
----------
lon_center, lat_center : float
coordinates of center of image
Returns
-------
dict
init: epsg code
"""
zone_base = 32700 if lat_center < 0 else 32600
e... | 54a068e9786a870f9eef9c7c82270f776acf0bb1 | 186,664 |
def nset(dic, keys, val):
""" Set a nested key-value pair - in-place.
No-op when a value already exists.
Example:
x = {}
nset(x, ['a', 'b', 'c'], 0)
print(x)
> {'a': {'b': {'c': 0}}}
"""
for key in keys[:-1]:
dic = dic.setdefault(key, {})
dic[keys[-1]] = ... | 943dd923e0c6b6232d33dd0238f1e8af05c74c36 | 217,576 |
def strfdelta(tdelta, fmt):
"""Utility function. Formats a `timedelta` into human readable string."""
d = {"days": tdelta.days}
d["hours"], rem = divmod(tdelta.seconds, 3600)
d["minutes"], d["seconds"] = divmod(rem, 60)
return fmt.format(**d) | 0057311e65dc8517a46ff4bbaff53b4e99417187 | 633,847 |
def request_is_from_scholarship_head(request): # pylint: disable=invalid-name
"""
Returns whether the request from the scholarship head user.
Returns: bool
"""
return request.user.has_perm("Scholarship.scholarship_head") | f4044f92c5fc88497fbd19c7a3b7da0b33f2abb8 | 231,982 |
def keyword_filter(keys, **kwargs):
"""
Find first instance of a key in kwargs and return the matching key/value pair.
:param keys: Iterable containing keys we which is search kwargs for.
:param kwargs: Mapping which contains key/value pairs we're filtering down.
"""
return next(iter(((k, str(v... | b7ea0a078cb04556076a449aed1744267a62ba50 | 238,888 |
def is_coloring(G, coloring):
"""Determine if the coloring is a valid coloring for the graph G."""
# Verify that the coloring is valid.
for (s, d) in G.edges:
if coloring[s] == coloring[d]:
return False
return True | b10a934f1c9f229c85dcf7ba4c5e5304c9c1afed | 596,612 |
def get_resolution(wofls):
"""Get the resolution for a WOfLs product."""
resolutions = {
'ga_ls_wo_3': (-30, 30),
'wofs_albers': (-25, 25),
'ga_s2_wo_3': (-10, 10),
}
return resolutions[wofls] | 45d45585f0ae1f60e3089a6e2f4fe771327b3abe | 79,588 |
def round_updown_to_x(num_in, x, direction="up"):
"""
Rounds a given value (num_in) to the nearest multiple of x, in a given
direction (up or down)
:param num_in: Input value
:param x: Value to round to a multiple of
:param direction: Round up or down. Default: 'up'
:return: Rounded number
... | 206b582843eea234b5b655dddd54eea6e32eec42 | 701,128 |
def calc_precision(tp: int, fp: int) -> float:
"""Calculate Precision.
Args:
tp (int): amount of TP.
fp (int): amount of FP.
Returns:
float: precision for the given amounts of TP and FP.
"""
if tp + fp != 0:
precision = float(tp / (tp + fp))
else:
# pr... | 60a5908da00f20c718c17bd5d3cd1306751256e0 | 122,992 |
import csv
def get_rows(csv_file):
"""Open csv file and return contents as list of lists."""
with open(csv_file, newline='') as f:
rows = [row for row in csv.reader(f)]
return rows[1:] | 3020f124645bb12bc475abaa9e7ce271cfda8a74 | 182,210 |
def framework_monitor_context_switch(client, enabled=None):
"""Query or set state of context switch monitoring.
Args:
enabled: True to enable monitoring; False to disable monitoring; None to query (optional)
Returns:
Current context switch monitoring state (after applying enabled flag).
... | 977e4372ba93f299f63aeeceec94084333e6d360 | 254,519 |
def get_side_nav_menu(html_soup):
"""
Returns -> [list] list of side navigation menus[type dict]
Menu -> [Dict] attrs:
'name' -> [str] name of menu
'link' -> [str] link for the menu
'image' -> [str] URL for menu image
Params -> [requests.HTML object] HTML of web page.
"""
... | bf222548b9b55dd032c76e52cfce73570b12f9fe | 574,322 |
def dedup(records):
"""Remove any identical records from the list.
Args:
records (list(dict)): the list of dicts to be filtered.
Returns:
list(dict): the list of records with any duplicates removed.
The list returned contains records in the same order as the original list.
"""
... | f3aadddf1458a08d36331a74722e12057d8ab8f9 | 12,689 |
def is_ethiopia_dataset(name):
"""Names with 'TR' at start or Ethiopia"""
return name.upper().startswith('TR') or \
name.lower().find('ethiopia') > -1 | 81f9981a4389f9c9fe7a19f23f8a66541116b04d | 673,676 |
def cmd_opts(request):
"""Returns pytest request options object"""
return request.config.option | 61c9b2f14d9efc90a648dc3a0ce485c7dd18a7ec | 534,773 |
def clean_outcomert(row):
"""
Intended for use with DataFrame.apply()
Returns an array with 'outcomert' converted from milliseconds to seconds if the 'study' variable is '3'
"""
if int(row['study']) >= 3:
return float(row['outcomert'] * .001)
else:
return row['outcomert'] | a6f2ea1d688555796e56caf3be9f1390fea83cea | 524,762 |
def strip_plus1(number: str) -> str:
"""
Strip leading "+1-" if present. NANP numbers on the platform seem to be stored as 10D only
:param number:
:return:
"""
return number and number.startswith('+1-') and number[3:] or number | 298872bccd693f3d67df09c9d77c02dce72e5711 | 51,528 |
import re
def regex_replacement_stack(replacements):
"""
Make a function that applies a sequence of regex replacements to text.
"""
compiled_replacers = [(re.compile(match), replace) for (match, replace) in replacements]
def _replace(text):
for compiled_re, replacement in compiled_replacer... | 5075749cd78263a7805e1820280fadba819c75cb | 505,182 |
def replace_header(headers, name, value):
"""
Updates the headers replacing the first occurance of the given name
with the value provided; asserting that no further occurances
happen. Note that this is _not_ the same as remove_header and then
append, as two distinct operations (del followed by an ap... | 11ea82f897e6a080a643da8bd36ee1aa9fa9b9c2 | 146,903 |
def apply_filter(filter_conf: dict, name: str) -> bool:
"""Given a filter configuration, determine if the name should
be included or excluded.
Confer the default_filter function for the structure of a
filter configuration.
Returns:
A flag indicating if the table is ok to be included.
"... | 66256ce98085e7d83f0bf6565baddfc859de2c80 | 371,771 |
def read_input(path: str) -> list:
"""
Read game board file from path.
Return list of str.
"""
board = []
with open(path, 'r', encoding='utf-8') as board_fl:
for line in board_fl:
if '\n' in line:
board.append(line[:-1])
else:
boar... | c0b22624c3b313f81b347cf8d6469044364e8676 | 435,426 |
import zlib
def complexity(s):
"""Compress string and return the size of the compression."""
s = s.encode("utf-8") # Python 3 fix.
compr = zlib.compress(s)
c = float(len(compr))
return c | 11986b3f56761fa50e90005093d1b205bd2841f5 | 574,599 |
def num_coin_changes_dp(amount, coins):
"""Number of coin changes by bottom-up dynamic programming.
Time complexity: O(a*n), where a is amount, and n is number of coins.
Space complexity: O(a*n).
"""
# Apply bottom-up DP with memoization tabular T: (n+1)x(amount+1).
n = len(coins)
T = [[0] ... | b472e0ff2e43a26cb16d0dade972d27ae4eec508 | 436,590 |
def chao1_var_no_doubletons(singles, chao1):
"""Calculates chao1 variance in absence of doubletons.
From EstimateS manual, equation 7.
chao1 is the estimate of the mean of Chao1 from the same dataset.
"""
s = float(singles)
return s*(s-1)/2 + s*(2*s-1)**2/4 - s**4/(4*chao1) | 6b93743a35c70c9ed5b9f3fc9bece1e9363c5802 | 708,619 |
def _sort_dict_by_keys(dictionary: dict) -> dict:
"""
Utility method to recursively sort a dictionary by it's keys.
Keys are sorted alphabetically in ascending order.
:type dictionary: dict
:param dictionary: input dictionary
:rtype dict
:return dictionary sorted by keys
"""
retur... | bfd7a7a4166947509bb34f8c713accd595d8ec9d | 434,994 |
def get_passive_action(df, trial, sample):
"""Get data for a replay.
Parameters
----------
df : pandas.DataFrame
Data to be replayed
trial, sample : int
Indices into the data for which to fetch actions
Returns
-------
keys_rts : list of tuples
each entry in the... | 9985fa075b0864c9707aff5d234e93512df23424 | 256,531 |
import torch
def place_fourth_atom(a_coord: torch.Tensor, b_coord: torch.Tensor,
c_coord: torch.Tensor, length: torch.Tensor,
planar: torch.Tensor,
dihedral: torch.Tensor) -> torch.Tensor:
"""
Given 3 coords + a length + a planar angle + a dihe... | 928fb14497e8856a8edcfdd7ac233207ee32ee4b | 527,467 |
def diff(it1, it2):
"""Find the differences between two iterables"""
s2 = set(it2)
diff = [x for x in it1 if x not in s2]
return diff | 163a445679bcaee0ce8aa7db320456ecee49d794 | 43,472 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.