content stringlengths 39 14.9k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
def normalize_time(timestamp):
"""Normalize time in arbitrary timezone to UTC"""
offset = timestamp.utcoffset()
return timestamp.replace(tzinfo=None) - offset if offset else timestamp | bd8b7f0417afabe8c58eba3393e7d466c35a70be | 24,875 |
def round_up(n, size):
"""
Round an integer to next power of size. Size must be power of 2.
"""
assert size & (size - 1) == 0, "size is not power of 2"
return ((n - 1) | (size - 1)) + 1 | 02f34fd5f2c059a9ee1b657f099b4699d90dfc01 | 24,878 |
def recombination(temperature):
"""
Calculates the helium singlet and triplet recombination rates for a gas at
a certain temperature.
Parameters
----------
temperature (``float``):
Isothermal temperature of the upper atmosphere in unit of Kelvin.
Returns
-------
alpha_rec_1... | c09e887053794a4c00b0daa3a48a57e563d89992 | 24,879 |
import torch
def batch_quadratic_form(x: torch.Tensor, A: torch.Tensor) -> torch.Tensor:
"""
Compute the quadratic form x^T * A * x for a batched input x.
Inspired by https://stackoverflow.com/questions/18541851/calculate-vt-a-v-for-a-matrix-of-vectors-v
This is a vectorized implementation of out[i] =... | 4e639fc210e944cdc6726c2daab85e486de58134 | 24,880 |
def _codepoint_is_ascii(ch):
"""
Returns true if a codepoint is in the ASCII range
"""
return ch < 128 | 931a3a67956bffd28f73e938e5d312951a2b3b80 | 24,881 |
import re
def valid_email(email):
"""Check if entered email address is valid
This checks if the email address contains a "@" followed by a "."
Args:
email (str): input email address
Returns:
bool: True if the input is a valid email and False otherwise
"""
# Ensure email is a... | a675856a7bb8dae87a77990c9e752b0bb4177c9b | 24,882 |
def get_iou(gt_bbx, pred_bbx):
"""
Calculate the Intersection over Union (IoU) of two bounding boxes.
Based on:
https://stackoverflow.com/questions/25349178/
calculating-percentage-of-bounding-box-overlap-for-image-detector-evaluation
Parameters
----------
gt_bbx : dict
ym... | 20ce6ed931e1a26ed078113191c6de06c4030510 | 24,884 |
def directory_get_basename(path: str) -> str:
"""Returns the last directory in a path."""
p = path
if p.endswith('/'):
p = p[:-1]
elif '.' in p:
p = p[:p.rfind('/')]
return p[p.rfind('/') + 1:] | 69af9336142c88cd705162b2e4522aef2ac95403 | 24,889 |
def _return_quantity(quantity, return_quantity, units_out=''):
"""Helper method to return appropriate unit type
Parameters
----------
quantity : :class:`~vunits.quantity.Quantity` obj
Quantity object to use
return_quantity : bool
If True, returns :class:`~vunits.quant... | de68065aad70134e4e7778e099711013a9930e13 | 24,890 |
def _get_currency_pair(currency, native):
"""
Format a crypto currency with a native one for the Coinbase API.
"""
return '{}-{}'.format(currency, native) | 92c3f43d1661f912a6bb63d14df1a7095eb784f3 | 24,891 |
import six
def sanitize_command_output(content):
"""Sanitizes the output got from underlying instances.
Sanitizes the output by only returning unicode characters,
any other characters will be ignored, and will also strip
down the content of unrequired spaces and newlines.
"""
return six.text_... | 55a42507b24d2fcb3993cfa8abda0cd04e4c7718 | 24,892 |
def countmatch(str1, str2, countstr):
"""checks whether countstr occurs the same number of times in str1 and str2"""
return str1.count(countstr) == str2.count(countstr) | a25f77cc6b847ff6f9b81af33836b3e4a715b056 | 24,894 |
def get_histogram_limits(filename):
"""Read a histogram file `filename' and return the smallest and largest values in the first column."""
hmin = 0
hmax = 0
with open(filename, "r") as f:
line = f.readline().split("\t")
hmin = float(line[0])
for line in f:
line = line... | 8ceed4f939c40f0266afa9f3218073842578f26f | 24,897 |
from datetime import datetime
def str2timestamp(s, fmt='%Y-%m-%d-%H-%M'):
"""Converts a string into a unix timestamp."""
dt = datetime.strptime(s, fmt)
epoch = datetime.utcfromtimestamp(0)
return (dt - epoch).total_seconds() | 69dd680623da8a61837676b540e1d5c053c8e198 | 24,899 |
def is_cached(o, name):
"""Whether a cached property is already computed.
Parameters
----------
o : object
The object the property belongs to.
name : str
Name of the property.
Returns
-------
bool
True iff the property is already computed.
Examples
----... | eb7b1356ded56dddb4cd917b27461e9108bd7b76 | 24,900 |
def minutes_to_human_duration(minutes_duration):
"""
Convert a duration in minutes into a duration in a cool format human readable
"""
try:
hours,minutes = divmod(minutes_duration,60)
return "%sh %smin" %(hours,minutes)
except TypeError:
return None | 22197c568505e366d5d4f6b020f8b61466deb43a | 24,907 |
def chart_filter(df, year = None, month = None, neighbourhood = None, crime = None):
"""
Filters the given database in order to wrange the database into the proper dataframe
required the graphs to display relevant information. Default value of None will allow
the maps to display every single data poin... | 0958af7abd9d302adc2aff91b081d309a88a505e | 24,908 |
def fibonacciAtIndex(index):
"""
Returns the fibonacci number at a given index.
Algorithm is using 3 variables instead of recursion in order to reduce memory usage and runtime
"""
a1, a2 = 1,1
position = 3
# while position <= index:
while index > 2:
temp = a1
a1 = a2
a2 += temp
# position += 1
index -... | 2dde77a70bfe8d1c32777910521a9b4695ab326e | 24,910 |
from typing import Tuple
from typing import List
def define_2d_correlations_hists() -> Tuple[List[str], List[float], List[Tuple[str, float, float, str]]]:
""" Define the 2D correlation hists.
Args:
None.
Returns:
(ep_orientation values, assoc_pt values, (ep_orientation, lower_pt_bin_edge,... | 00a0f59fe7c238a271fa75a83085556fc51f4acc | 24,913 |
def _ParseClassNode(class_node):
"""Parses a <class> node from the dexdump xml output.
Returns:
A dict in the format:
{
'methods': [<method_1>, <method_2>]
}
"""
methods = []
for child in class_node:
if child.tag == 'method':
methods.append(child.attrib['name'])
return {'m... | 2d801173230a065e668b89cac155b31991cee656 | 24,921 |
import select
def _is_readable(socket):
"""Return True if there is data to be read on the socket."""
timeout = 0
(rlist, wlist, elist) = select.select(
[socket.fileno()], [], [], timeout)
return bool(rlist) | 79b258987171e3a5e4d3bab51a9b8c9a59e2415e | 24,922 |
def export_sheet(ss, sheet_id, export_format, export_path, sheet_name):
"""
Exports a sheet, given export filetype and location. Allows export format 'csv', 'pdf', or 'xlsx'.
:param ss: initialized smartsheet client instance
:param sheet_id: int, required; sheet id
:param export_... | 76b49fa0904140571eb84526f6021448db54dea9 | 24,924 |
def adapt_sample_keys(sample_list, key_type):
"""
Converts sample_list to a new format where instead of "scene_id", "object_id" and "ann_id"
there is a "sample_id".
:param key_type:
'kkk' for {scene_id}-{object_id}_{ann_id}
'kk' for {scene_id}-{object_id}
'k' for {scene_id}
:retu... | 8845da67ee9627cf377efc1c7b789eaa4cfb2c65 | 24,930 |
import types
import inspect
def get_pytorch_model(module, model_settings):
"""
Define a DeepSphere-Weather model based on model_settings configs.
The architecture structure must be define in the 'module' custom python file
Parameters
----------
module : module
Imported python m... | a32a01163b71b1610c97adad1b3febfa1d4d5a25 | 24,932 |
import torch
def get_criterion(config):
"""
Creates the torch criterion for optimization
"""
if config["name"] == "cross_entropy":
return torch.nn.CrossEntropyLoss()
else:
raise NotImplementedError | 2d1a9cc5983ab64fe2016637c292de3b9551079b | 24,939 |
def is_magic(attribute):
"""Check if attribute name matches magic attribute convention."""
return all([
attribute.startswith('__'),
attribute.endswith('__')]) | d392a34902ce22127d92bc4d678228d59b97cba9 | 24,944 |
def expand_row(header, row):
"""Parse information in row to dict.
Args:
header (dict): key/index header dict
row (List[str]): sambamba BED row
Returns:
dict: parsed sambamba output row
"""
thresholds = {threshold: float(row[key])
for threshold, key in head... | 79ea16b498f6fd5c7c002bf822a34da65de0d2c9 | 24,945 |
import pickle
def retrieve_model(filename='model.pkl'):
"""
Retrieve probability model pickled into a file.
"""
with open(filename, 'rb') as modfile: return pickle.load(modfile) | 3e05c3c9b3bc2bc3974dab422ced5f53589c2823 | 24,949 |
def aggregator(df, column):
"""
Return multiple aggregate data values, compiled into a list. summ (total), minn (lowest value), maxx (highest value), avg (mean), med (median), mode (most repeated value).
Parameters
----------
df : pandas object
dataFrame from which to pull the values
... | f940270662715859f42e6b9ffc4411c492085651 | 24,951 |
def gen_compare_cmd(single_tree, bootstrapped_trees):
"""
Returns a command list for Morgan Price's "CompareToBootstrap" perl script <list>
Input:
single_tree <str> -- path to reference tree file (Newick)
bootstrapped_trees <str> -- path to bootstrapped trees file (Newick)
"... | df9d2d4c21ca28012107b8af69706d45804e5637 | 24,953 |
def silent_none(value):
"""
Return `None` values as empty strings
"""
if value is None:
return ''
return value | 48b9709dc4bffc659b168f625f4f6be5608a645d | 24,955 |
def splinter_screenshot_encoding(request):
"""Browser screenshot html encoding."""
return "utf-8" | 17e6055332e7bc63778e1e80ba01c1631b0c876e | 24,960 |
import math
def rms_mean(elements):
"""
A function to calculate the root mean squared mean value of a list of elements
:param elements: a list of elements
:return: root mean squared mean value
"""
return math.sqrt(sum(x * x for x in elements) / len(elements)) | a1178e70f210063c6559fa15789bfd15c1f89b79 | 24,961 |
from typing import Any
def maybebool(value: Any) -> bool:
"""
A "maybified" version of the bool() function.
"""
return bool(value) | e40d112291ce7bfb58d94208be662cb5370506d9 | 24,964 |
import random
def digits() -> str:
"""Generate a random 4 digit number."""
return str(random.randint(1111, 9999)) | 8d4f9195e74c2b1b0c31a108a502a028239bea8e | 24,965 |
from typing import Sequence
def crop_to_bbox_no_channels(image, bbox: Sequence[Sequence[int]]):
"""
Crops image to bounding box (in spatial dimensions)
Args:
image (arraylike): 2d or 3d array
bbox (Sequence[Sequence[int]]): bounding box coordinated in an interleaved fashion
(e... | 0d6d4a2c77be0343b7557485e06fd8c33a49e72f | 24,969 |
def mean_std(feature_space, eps=1e-5):
"""
Calculates the mean and standard deviation for each channel
Arguments:
feature_space (torch.Tensor): Feature space of shape (N, C, H, W)
"""
# eps is a small value added to the variance to avoid divide-by-zero.
size = feature_space.size()
a... | 2b228edab0397257b1deacdecae95265d955c76c | 24,976 |
import torch
def bbox_transform(boxes, gtboxes):
""" Bounding Box Transform
from groundtruth boxes and proposal boxes to deltas
Args:
boxes: [N, 4] torch.Tensor (xyxy)
gtboxes: [N, 4] torch.Tensor (xywh)
Return:
delta: [N, 4] torch.Tensor
"""
gt_w = gtboxes[:, 2] - gt... | fa2bf83d24206b83508612ac728636905e80ebcc | 24,981 |
from typing import Union
from pathlib import Path
import pickle
def load_picke(ffp: Union[Path, str]):
"""Loads the pickle file"""
with open(ffp, "rb") as f:
return pickle.load(f) | 39145f2c1dd51226f19b89aaeb984a9434ebb06c | 24,985 |
from typing import List
def replace_words_with_prefix_hash(dictionary: List[str], sentence: str) -> str:
"""
Replace words in a sentence with those in the dictionary based on the prefix match.
Intuition:
For each word in the sentence, we'll look at successive prefixes and see if we saw them before
... | bb46b0dc61eab2be358d44f8fb46782f867e3c30 | 24,990 |
def register_endpoints(app):
"""Register endpoints defined in openapi.yml
Additionally register any flask endpoints / blueprints
Parameters
----------
app : Connexion
A Connexion application instance.
"""
app.add_api('openapi.yml', strict_validation=True)
# Add health check
... | 5c7990be3bde5c7d99cbac1e34a98aa6df5761e3 | 24,991 |
def is_project_admin_context(context):
"""Check if this request has admin context with in the project."""
if context.is_admin:
return True
return False | a2365f7a0d830cdbb3ca76347b5d152d42ce178e | 24,992 |
def get_last_timestamp(db):
"""Return the last update timestamp from the database"""
return db.execute('SELECT update_time FROM meta_slurm_lastupdate').fetchone()[0] | bce062d16b00e46c9ff5943e99329885fd1a0dc9 | 25,000 |
def line_parameter_form_from_points(a):
"""
Parameters of line in parameter form: x = v + t * u, determined from points in a.
Only the first and last element of a are used.
"""
# u = a[0] - a[-1]
u = a[-1] - a[0]
v = a[0]
return u, v | d8f6abcbacc9bca3feea8f556630a4499512b8b1 | 25,002 |
def calc_db_cost_v3(db) -> float:
"""Returns a noise cost for given dB: every 10 dB increase doubles the cost (dB >= 45 & dB <= 75).
"""
if db <= 44:
return 0.0
db_cost = pow(10, (0.3 * db)/10)
return round(db_cost / 100, 3) | bad2506da5cb53aa552294498122e70dd95f780f | 25,005 |
def read_chrom_sizes(chrom_sizes):
"""Read chrom.sizes file."""
ret = {}
f = open(chrom_sizes, "r")
for line in f:
ld = line.rstrip().split("\t")
ret[ld[0]] = int(ld[1])
f.close()
return ret | ccba3e56f006746d1e34953364d3f8a40fc70086 | 25,009 |
def serializeRegressor(tree):
""" Convert a sklearn.tree.DecisionTreeRegressor into a JSON-compatible format """
LEAF_ATTRIBUTES = ['children_left', 'children_right', 'threshold', 'value',
'feature', 'impurity', 'weighted_n_node_samples']
TREE_ATTRIBUTES = ['n_classes_', 'n_features_'... | d94f8cde0144cd842175480332a398def8e19ae8 | 25,012 |
def linear_probe(h, i, m):
"""
Finds a possible next position using linear probing
:param h: Computed hash value that has resulted in a collision
:param i: Offset
:param m: Size of the table
:return: The next index to be checked if it is open
"""
return (h + i) % m | 49ecb3ca389255b99bdde3e61bb503d3d517549b | 25,013 |
def categorize(contenttype: str):
"""Return 'cbor', 'json' or 'link-format' if the content type indicates it
is that format itself or derived from it."""
media_type, *_ = contenttype.split(';')
_, _, subtype = media_type.partition('/')
if subtype == 'cbor' or subtype.endswith('+cbor'):
ret... | d065c9c3823bda424108d70a8e5b8d7dc70eab9b | 25,016 |
def colrows_to_xy(screen_size, cursor_position):
"""Convert cursor position to x, y pixel position
Args:
screen_size (tuple): The screen size (width, height)
cursor_position (tuple): The cursor position (row, col)
Returns:
(tuple): The screen position in pixels (x, y)
"""
... | 63d3b9f7af789c613d2067fe0ceb671e5ed04465 | 25,019 |
from typing import Optional
def irepeat(obj: object, cnt: Optional[int] = None) -> object:
"""
Yield the object cnt times if specified or infinitely.
Notes
-----
The Python itertools repeat class implementation.
Parameters
----------
obj : object
to be repeated.
cnt : Opt... | 7d0c3cefb763d099c75ebfd070368882004d1cf0 | 25,024 |
def max_pairwise_product(numbers):
"""
max_pairwise_product gets the two biggest numbers and returns the product of them
TimeComplexity: O(n)
"""
biggest = -9999999999999
second_bigest = -9999999999999
for ele in numbers:
if ele > biggest:
biggest, second_bigest = ele, bi... | 470a1400fc235de7c4a6eb459577f674717b6ced | 25,025 |
from typing import List
import glob
def filter_filetype(filetype: str) -> List:
"""
Filtra según el tipo de archivo indicado.
Args:
filetype: Tipo de archivo a filtrar (ej: *.png).
Returns:
Lista de coincidencias según el tipo filtrado.
"""
return glob.glob(filetype) | 45ea03ac4a148d2817df1c5fdea2969e395dfcaa | 25,029 |
import json
def deserializeValue(v):
""" Deserialize single value from JSON string format """
try:
return json.loads(v)
except ValueError:
raise ValueError("No JSON object could be decoded from \"%s\"" % v) | 5bc26f42f7873030d9bf79a502c2604433df150f | 25,030 |
def hydrobasins_upstream_ids(fid, df):
"""Return a list of hydrobasins features located upstream.
Parameters
----------
fid : feature id
HYBAS_ID of the downstream feature.
df : pd.DataFrame
Watershed attributes.
Returns
-------
pd.Series
Basins ids including `fid` an... | 3fed2e9f5bad0d58bd21175750b6e733c930b952 | 25,032 |
import functools
def with_header(header=None):
"""Decorator that adds a section header if there's a any output
The decorated function should yield output lines; if there are any the
header gets added.
"""
def wrap(func):
@functools.wraps(func)
def wrapped(cls, remaining_attrs):
... | 653e6a710acdca1b6ac28a6ed5ffb3ac60c84bc0 | 25,034 |
from typing import Union
from pathlib import Path
def is_relative_to(absolute_path: Union[str, Path], *other) -> bool:
"""Return True if the given another path is relative to absolute path.
Note:
Adapted from Python 3.9
"""
try:
Path(absolute_path).relative_to(*other)
return T... | 72076c54a024de3aa83d5355aaa1e04838f53f1e | 25,046 |
import yaml
def get_config(config_file):
""" Read CircleCI config YAMLs as dictionaries """
with open(config_file) as fstream:
try:
return yaml.safe_load(fstream)
except yaml.YAMLError as err:
print(err) | 13005c11aab352ff96ef422d724ef91ec96074cb | 25,048 |
from typing import Any
def is_iterable(obj: Any) -> bool:
"""
Return whether an object is iterable or not.
:param obj: Any object for check
"""
try:
iter(obj)
except TypeError:
return False
return True | 4053ede1d7ac825ec0e9723892df9e5fe638e8b6 | 25,049 |
def get_x_y(df):
"""
Split the dataframe into the features and the target
:param df: The data frame
:return: X, y - The features and the target respectively
"""
X = df.drop('isFraud', axis=1)
y = df.isFraud
return X, y | 902aba32c2ed36b31ac2e59a804a0ad009fb32d2 | 25,051 |
def get_child_schema(self):
"""An optional function which returns the list of child keys that are associated
with the parent key `docs` defined in `self.schema`.
This API returns an array of JSON objects, with the possible fields shown in the example.
Hence the return type is list of lists, because this plugin ret... | 4d91ff18ab8e3f6cec5610169dc6d52e7a647960 | 25,052 |
from typing import Tuple
def calculate_broadcasted_elementwise_result_shape(
first: Tuple[int, ...],
second: Tuple[int, ...],
) -> Tuple[int, ...]:
"""Determine the return shape of a broadcasted elementwise operation."""
return tuple(max(a, b) for a, b in zip(first, second)) | 5d565b2b5f38c84ab1f1573f4200fc65d6ae8e6a | 25,055 |
from typing import List
from typing import Dict
from typing import Any
from typing import Tuple
from typing import Optional
def prepare_outputs_for_categories(records: List[Dict[str, Any]]) -> \
Tuple[List[Dict[str, Optional[Any]]], List[Dict[str, Optional[Any]]]]:
"""
Prepares human readables and con... | 8215214d79f46666aec167ed2b99bf73663692eb | 25,057 |
import math
def divide_into_subsets(list_of_element, subset_size):
"""
Given a list of elements, divide into subsets. e.g. divide_into_subsets([1,2,3,4,5], subset_size=2) == [[1, 2], [3, 4], [5]]
:param list_of_element:
:param subset_size:
:return:
"""
element_gen = (el for el in list_of_... | 7014135efd2866e42be78def18c578eaef0e9a4e | 25,063 |
def parse_str(s):
"""Try to parse a string to int, then float, then bool, then leave alone"""
try:
return int(s)
except ValueError:
try:
return float(s)
except ValueError:
if s.lower() == 'true':
return True
if s.lower() == 'false':... | 251cd0bdbf47464a6a424e012ef22cbf0f38452f | 25,066 |
def generate_lambda_key(domain):
"""Generate the S3 key name for the lambda's zip file.
Args:
domain (str): Use the domain as part of the key.
Returns:
(str)
"""
key = 'dynamodb_autoscale.' + domain + '.zip'
return key | d8dcd6897c2773a1ba31df72c2e8336badc42e68 | 25,067 |
def remove_brace(value):
"""Remove braces (which indicete capital leters in latex).
Args:
value (str): string which have ``{``, ``}``.
Returns:
str (``{``, ``}`` is removed.)
Examples:
>>> val = "The {CNN}-based ..."
>>> remove_brace(val)
"The CNN-based ..."
... | b4144fde9890128572a1f88caa26f6515f13b924 | 25,070 |
def generate_list_articles(bib):
"""Description of generate_list_articles
From the bib file generates a ReadMe-styled table like:
| [Name of the article](Link to the .pdf) | Code's link if available |
"""
articles = ""
for entry in bib:
if "title" in entry:
if "link" in entry... | 362439030116376687f6d00bce5963a6a6587309 | 25,073 |
from typing import Union
import torch
def cam_init2orig(cam, scale: Union[float, torch.Tensor], start_pt: torch.Tensor, N=224):
"""
Args:
cam (bs, 3): (s, tx, ty)
scale (bs,): scale = resize_h / orig_h
start_pt (bs, 2): (lt_x, lt_y)
N (int): hmr_image_size (224) or IMG_SIZE
... | 99edd5049b28cb09c479e9e1bef9c4c820bd2e12 | 25,074 |
def find_prefixed_labels(labels, prefix):
"""Util for filtering and cleaning labels that start with a given prefix.
Given a list of labels, find only the specific labels with the given prefix.
Args:
prefix: String expected to be prefix of relevant labels
labels: List of string labels
Return:
Filt... | 49631b8cf257bad0e2c3ec1be4e2c48c5e49e963 | 25,075 |
import re
def resolve_value(value):
"""
Convert "1k" to 1 000, "1m" to 1 000 000, etc.
"""
if value is None:
return None
tens = dict(k=10e3, m=10e6, b=10e9, t=10e12)
value = value.replace(',', '')
match = re.match(r'(-?\d+\.?\d*)([kmbt]?)$', value, re.I)
if not match:
r... | ef3880c532f143b7bc3493d6cc942529329e040a | 25,082 |
def pkcs7_pad(inp, block_size):
"""
Using the PKCS#7 padding scheme, pad <inp> to be a multiple of
<block_size> bytes. Ruby's AES encryption pads with this scheme, but
pycrypto doesn't support it.
Implementation copied from pyaspora:
https://github.com/mjnovice/pyaspora/blob/master/pyaspora/dia... | 5a5aae6f588e5e67dc30c85ab6a6afcdb9c728c0 | 25,083 |
import torch
def dr_transformation_cate(
y: torch.Tensor,
w: torch.Tensor,
p: torch.Tensor,
mu_0: torch.Tensor,
mu_1: torch.Tensor,
) -> torch.Tensor:
"""
Transforms data to efficient influence function/aipw pseudo-outcome for CATE estimation
Parameters
----------
y : array-li... | 7bcff5f42f5aa664fd0250dd9faba49889621205 | 25,087 |
def create_adjusted_coefficients(targets):
"""
Create a dictionary of "adjusted-coefficient" elements (as XML text) for the given targets,
where the key is the year and the value is the text for all elements starting at that year.
"""
template = '<adjusted-coefficient year="{year}">{coefficient}</ad... | 89f6ba9d6a1a1977ac4a175b28f8db652ba9ae37 | 25,093 |
def link_to_url(link):
"""
>>> from scrapy.link import Link
>>> link_to_url(Link("http://example.com/?foo=bar"))
'http://example.com/?foo=bar'
>>> link_to_url(Link("http://example.com/?foo=bar", fragment="id1"))
'http://example.com/?foo=bar#id1'
>>> link_to_url(Link("http://example.com/?foo=... | 6a6bf4a1f33748175ac7a95899c1fcffcf9751b0 | 25,095 |
def zeroPrepender(source, length):
"""
Append extra zeros to a source number based on the specified length
"""
if (not source and source != 0) or not length:
return None
result = str(source)
if len(result) >= length:
return result
for i in range(length - len(result)):
... | ce9fc94e4b745f5782af818dcd7c66789857a342 | 25,105 |
def bounds_elementwise(lst):
"""Given a non-empty list, returns (mins, maxes) each of which is the same
length as the list items.
>>> bounds_elementwise([[0,6,0], [5,0,7]])
([0,0,0], [5,6,7])
"""
indices = list(range(len(lst[0])))
mins = [min(el[i] for el in lst) for i in indices]
maxes... | 5fa4fbe75db310d971005c88fc6d04058d3cd998 | 25,108 |
from typing import Counter
def get_probs(occurrences):
"""
Computes conditional probabilities based on frequency of co-occurrences
Parameters
----------
occurrences: occurences[x][y] number of times with (X=x and Y=y)
Returns
-------
probs : probs[x][y] = Pr(Y=y | X=x)
reverse_pr... | 2e4dc69646a1800496bd5366bde1be78c3d18061 | 25,115 |
def get_countN(x,n):
"""Count the number of nucleotide n in the string."""
return x.upper().count(n.upper()) | 7e5577dcf2a5666f77a915dd943cd5f59e2bd260 | 25,124 |
import hashlib
def fileSHA ( filepath ) :
""" Compute SHA (Secure Hash Algorythm) of a file.
Input : filepath : full path and name of file (eg. 'c:\windows\emm386.exe')
Output : string : contains the hexadecimal representation of the SHA of the file.
returns '0' if file c... | 98bddf8ef32c769b77dde704838c188e2b02ad49 | 25,128 |
import pickle
def load_pickle(path):
"""
Load a WordGraph object from a pickle file.
:param path: path to pickled WordGraph object
:return:
"""
with open(path, 'rb') as input:
# G = wg.WordGraph()
return pickle.load(input) | 3108097ee85f9947606c6b74bfe0d5ba12cea517 | 25,129 |
import yaml
def yaml_config_file_provider(handle, cmd_name): # pylint: disable=unused-argument
"""Read yaml config file from file handle."""
return yaml.safe_load(handle) | e95295a0413290957d7b319b73876209ba11b5c6 | 25,130 |
def Command(*_args, **_kw):
"""Fake Command"""
return ["fake"] | a3d435534a045fe1b08eaffc7327065492b07026 | 25,132 |
def argmin(l) -> int:
"""
From a previous AoC.
>>> argmin([9,9,9,1,9])
3
"""
mini = 0
for i, e in enumerate(l):
mini = i if e < l[mini] else mini
return mini | 4b8228dd94b57fc03a01865f2e67a5f17e997c5a | 25,134 |
from typing import List
def relationship_headers() -> List[str]:
"""
Produce headers for a relationship file.
"""
return ["from", "to", "relationship"] | 7fdaa7d14f0fd7b8a2a01fd808d69aac6d41d9ca | 25,135 |
def signe(a):
""" return 0 if a<0 , 1 else"""
if a < 0:
return 0
else:
return 1 | 71d0891f5dfb1d6d4ea3b8a9ad802e976de6e742 | 25,140 |
def _bool_value(element):
"""
Given an xml element, returns the tag text converted to a bool.
:param element: The element to fetch the value from.
:return: A boolean.
"""
return (element.text.lower() == "true") | d5eb1c94ec6a09b6a81508124f36bacf8c253fb8 | 25,143 |
def get_hr_val(choices, db_val):
"""
Get the human readable value for the DB value from a choice tuple.
Args:
choices (tuple): The choice tuple given in the model.
db_val: The respective DB value.
Returns:
The matching human readable value.
"""
for pair... | 26791ccc16e6b1e9399bc6d10b4292c7c7780ebd | 25,145 |
def resolve_templating_engine(args):
""" Figures out what templating engine should be used to render the stack
"""
# Figure out what templating engine to use.
# Only use -t option when stack comes from stdin
if args.stack.name == "<stdin>":
return args.templating_engine
elif ".mako" in a... | aef956cd3a5a9cca8451f069a986407af631694e | 25,147 |
def parse_value(value, base_value=0):
"""Parse a numeric value spec which is one of:
NNN integer
NN.MMM float
NN% proportional to base_value
"""
if not isinstance(value, str):
return value
if value[-1] == '%':
value = base_value * float(value[:-1]) / 100
... | 186ef21e453af617449294bc44d97765b28e6676 | 25,148 |
def _html_tidy_cell_item(item, key):
"""
Returns HTML-tidied item for putting in table cell.
:param item: item to be tidied
:param key: dictionary key (for reference)
:return: tidied HTML item or string
"""
if isinstance(item, dict):
resp = "<br/>\n".join(["{}: {}".format(key, value... | 748ad64ca025a76472d2146cd9e369f525470bee | 25,152 |
def get_entrypoint(request_path):
""" Get the entrypoint url from a request path, splitting off sub-indexes and query strings """
entrypoint = request_path.replace('/index', '').split('?')[0]
if entrypoint == '':
entrypoint = '/index'
return entrypoint | 52fec0fd6933e26bc38e26a52b3124d1a5914258 | 25,160 |
from typing import Dict
from pathlib import Path
def create_paths(run_id: str, run_dir: str = "runs/") -> Dict[str, Path]:
"""
Create the necessary directories and sub-directories conditioned on the `run_id` and run directory.
:param run_id: Unique Run Identifier.
:param run_dir: Path to run director... | 2448d2621647b084161312bf572a2294705ea713 | 25,162 |
def getDimensions(self):
"""Gets the number of rows and columns of the map"""
return (self.__numrows__, self.__numcols__) | fa56d7ec97ba237fb41cc70bbbb7a2348a621f04 | 25,165 |
import re
def convert_fa_spaces(input_value: str) -> str:
"""
Convert space between Persian MI and De-Yii to zero-width non-joiner (halfspace) char
:param input_value: String contains persian chars
:return: New string with converted space to half space char
"""
# u200C is the code for unicode... | f295402cdb086b62c12abbf935bb12b24c223ca0 | 25,167 |
from multiprocessing import RLock as rlockp
from threading import RLock as rlockt
def create_rlock(process=False):
"""Creates a reentrant lock object."""
if process:
return rlockp()
else:
return rlockt() | eb4cbdd72c5f649e3907cda0054c96913b722381 | 25,168 |
def to_hex_string(string: str) -> str:
"""Converts UTF-8 string into its hex representation
:param string: str
The string to convert to hex
:return:
Hex representation of the given string
"""
return string.encode('utf-8').hex() | 62b9b71af31bccdde136aa6d2dabbb2ee3df2ea7 | 25,169 |
import random
def sample(p):
"""Given an array of probabilities, which sum to 1.0, randomly choose a 'bin',
e.g. if p = [0.25, 0.75], sample returns 1 75% of the time, and 0 25%;
NOTE: p in this program represents a row in the pssm, so its length is 4"""
r = random.random()
i = 0
while r > p[i... | 886da94e2c9e35bd07ceba606de92d2126197b99 | 25,173 |
import random
def cartesian_choice(*iterables):
"""
A list with random choices from each iterable of iterables
is being created in respective order.
The result list can be seen as an element of the
Cartesian product of the iterables
"""
res = []
for population in iterables:
... | ad9ff73909b17b65d98e61c36ceef7b9ace1a1f3 | 25,177 |
def add(number_one, number_two):
"""
两个数字相加
:param number_one:第一个数字
:param number_two:第二个数字
:return:相加后的结果
"""
result = number_one + number_two
return result | ac85b372ebf48c4a6b4dc67b61d74dfa6d9b4246 | 25,178 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.