content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
def reverse(seq):
"""
Recursively reverses a string
@param {string} sequence to reverse
@return {string}
"""
if seq == '':
return ''
last_index = len(seq) - 1
return seq[last_index] + reverse(seq[:last_index]) | 5395f8be64f48145e45612fb5b9635d6639dafed | 592,357 |
import array
def round_float32(x_double):
"""
Uses the array module to retun a (truncated) 32-bit float
representation of a python floating point number (double)
"""
return array.array('f', [x_double])[0] | b614f83123d90bafc8d72c8fc723aae24331b3b2 | 590,777 |
def gen_wordlist(wordfile):
"""Generates a list of words given a line-delimited wordfile."""
# read text in
with open(wordfile,"r") as fil:
text=fil.read()
# Split on lines but not spaces because of multiword phrases
return text.split("\n") | 2f9c88e0db2b7b54639abc54c58bf69d04677e8e | 183,808 |
def __sbiw_rd_i(op):
"""
Decode and return the 'Rd' and 'immediate' arguments of a SBIW opcode
"""
AVR_SBIW_RD_MASK = 0x0030
AVR_SBIW_CONST_MASK = 0x00CF
imm_part = op & AVR_SBIW_CONST_MASK
imm = ((imm_part >> 2) & 0x30) | (imm_part & 0xF)
# rd_part is 2 bits and indicates r24, 26, 28,... | 1e34e08c60b87bf27956e796390334efcbb8402f | 194,440 |
def has_anonymous_link(node, auth):
"""check if the node is anonymous to the user
:param Node node: Node which the user wants to visit
:param str link: any view-only link in the current url
:return bool anonymous: Whether the node is anonymous to the user or not
"""
if auth.private_link:
... | c5941bce3f0110dfcd5e9bbb19bae0682c5e731f | 707,328 |
import re
def collapse_linebreaks(string, threshold=1):
""" Returns a string with consecutive linebreaks collapsed to at most the given threshold.
Whitespace on empty lines and at the end of each line is removed.
"""
n = "\n" * threshold
p = [s.rstrip() for s in string.replace("\r", "").split(... | 5bf230b210cbccadeb7dc21f762d3499234b700e | 516,021 |
import pathlib
def tmp_fixed_file(tmp_dir):
"""Return a path for a tmp yaml file."""
return pathlib.Path.joinpath(tmp_dir, 'fixed_file') | 861cd579ee3c3254b8eccd2ac5b10a6a8435e09a | 547,567 |
def generate_new_row(forecast_date, target, target_end_date,
location, type, quantile, value):
"""
Return a new row to be added to the pandas dataframe.
"""
new_row = {}
new_row["forecast_date"] = forecast_date
new_row["target"] = target
new_row["target_end_date"] = targ... | 212438098b7ad56f2046fdc3c5f6a73cab96f420 | 599,042 |
import six
import uuid
def six_uuid5(namespace, name):
"""A uuid.uuid5 variant that takes utf-8 'name' both in Python 2 and 3.
:param namespace: A UUID object used as a namespace in the generation of a
v5 UUID.
:param name: Any string (either bytecode or unicode) used as a name in t... | c99e4b6a609a9594728e2ecc155b453dc1739f6c | 514,502 |
import fnmatch
def csv_matches(list_of_files):
"""Return matches for csv files"""
matches = fnmatch.filter(list_of_files, "*.csv")
return matches | ee81a533731b78f77c4b4472c2e19c643d04525f | 56,370 |
def _identify_loss_method(loss_method):
"""
Identifies the symmetry for the given loss method.
Parameters
----------
loss_method : str
The loss method to use. Should have the symmetry identifier as
the prefix.
Returns
-------
symmetric : bool
True if `loss_metho... | 21b07e898f0d45381f2834aac632cb3350d38694 | 404,409 |
def convert_age(age: str):
"""
Convert age string.
All age ranges in format AXX-AXX with X = numeric.
Exception: A80+
"""
age_range = age.split("-")
if len(age_range) > 1:
return {
"start": float(age_range[0][1:3]),
"end": float(age_range[1][1:3]),
}
... | 7b22029ce52be3f29fb45482ba87f99287c44429 | 79,724 |
def create_queries(group, joiner, template, maxlen):
"""Create queries combining `maxlen` entities of a `group` to search for.
Parameters
----------
group : list
Entities (authors, documents, etc.) to search for on Scopus.
joiner : str
On which the group elements should be joined t... | 87bd96b4816a3950d905607e47dbd5ab85e31d55 | 362,202 |
def micro_f1_similarity(
y_true: str,
y_pred: str
) -> float:
"""
Compute micro f1 similarity for 1 row
Parameters
----------
y_true : str
True string of a space separated birds names
y_pred : str
Predicted string of a space separated birds names
Returns
... | fbe6b07e329ce5727e8488f8627dc7904694f719 | 592,573 |
def crop_by_bbox(pil_image, bbox):
"""
Crops the specified PIL image with the given bounding box
:param pil_image: PIL image to crop
:param bbox: bounding box object to crop by
:return: PIL image cropped.
"""
box = bbox.get_box()
box[2] += box[0]
box[3] += box[1]
crop_result = p... | cd829f9b3cb134e73dc28ecfccdedabc2db529e0 | 345,415 |
def GetMaxOrder(Scat):
"""Function compute max Order (n) for :math:`S_1 \, \\& \, S_2` computing
MaxOrder = :math:`\\big( 2+x + 4 x^{1/3} \\big)`;
x begin the size parameter of the scatterer.
Parameters
----------
Scat : :class:`BaseScatterer`
Description of parameter `Scat`.
Retu... | 0b89eaf60244f93f68d5b3e9172c506e9c550646 | 426,204 |
from typing import Callable
def action(func: Callable):
"""
Decorator to mark functions as playbook actions
"""
setattr(func, "_action_name", func.__name__)
return func | f3b9ef46da4a1a801a0cfe0ae0c04bf398aa6120 | 207,990 |
def mult(terms):
"""
Takes a 2-element tuple TERMS and multiplies the two terms
"""
a,b = terms[0], terms[1]
product = 0
for i in range(a):
product += b
return product | 58184fa0143e61d058bdd71d5c6651588770fc43 | 391,423 |
import re
def search_matches(match_list, search_list, ignore_list=None):
"""
Return list of football matches that match search
"""
if ignore_list is None:
ignore_list = []
search = re.compile('|'.join(search_list))
my_matches = [m for m in match_list if search.search(m['fixture'])]
... | 51ae6dcd18b1f89825b6193f114a09bc1aab3682 | 102,797 |
def DoesTestHaveLabels(cls, labels):
"""Returns true if any tests in cls have any of the labels."""
labels = set(labels)
for name in dir(cls):
if name.startswith("test"):
item = getattr(cls, name, None)
if labels.intersection(getattr(item, "labels", set(["small"]))):
return True
return... | 61c5e816a2fe6bde0798d51f0b832673655d8130 | 134,862 |
from typing import Counter
def count_words_fast(text):
"""count the number of times each word occurs in text (str).
Return dictionary where keys are unique words and values are
word counts. skip punctuations"""
text = text.lower() #lowercase for the counting letters so the function can cont the same... | 113eba5988651177b353e7afedbe8e63e2fdb452 | 529,514 |
def chunker(iterable, inc):
"""
Convert an iterable into a list of inc sized lists.
:param iterable: Iterable to chunk.
:type iterable: list/tuple/string
:param inc: Increment; how big each chunk is.
:type inc: int
"""
chunks = [iterable[x:x+inc] for x in range(0, len(iterable), inc)]
... | a6a78efd0f9a92d24f2395adbec0cf00aea30686 | 329,464 |
def never(_):
"""Predicate function that returns ``False`` always.
:param _:
Argument
:returns:
``False``.
"""
return False | 38a949525bb45b556eb5131bd9ff3d11b83a616e | 214,375 |
def first_not_none(itr):
"""
returns the first non-none result from an iterable, similar to any() but return value not true/false
"""
for x in itr:
if x:
return x
return None | de04721394fd45f8d3886b6adb17a9db0893825b | 641,641 |
def has_duplicates(t):
"""Returns True if any element appears more than once in a sequence.
t: list returns: bool
"""
s = t[:] # make a copy of t to avoid modifying the parameter
s.sort()
for i in range(len(s)-1):
if s[i] == s[i+1]: # check for adjacent eleme... | 2200d4c5c5b22937f60e560937794d7419082f40 | 653,394 |
def nfa_determinization(nfa: dict) -> dict:
""" Returns a DFA that reads the same language of the input NFA.
Let A be an NFA, then there exists a DFA :math:`A_d` such
that :math:`L(A_d) = L(A)`. Intuitively, :math:`A_d`
collapses all possible runs of A on a given input word into
one run over a larg... | 9ee37893974a7c5e06ad8486359c4460bbda763d | 507,623 |
import torch
def smooth_l1_loss(input,
target,
beta: float,
reduction: str = "none",
size_average=False):
"""
Smooth L1 loss defined in the Fast R-CNN paper as:
| 0.5 * x ** 2 / beta if abs(x) < beta
smoothl1(... | a794842340f58b366959f2ac7806afb6579c9abe | 529,827 |
from functools import reduce
def sum_groups_by_key(unsummed, key_name):
"""Computes the sum of the values for a given key across multiple dicts.
Steps through a dictionary of lists. Each list is a list of dictionaries.
For each dictionary in a list this function computes the sum of the value
of a giv... | 0a14a64f5d85cae819796d56b7a5896694269f50 | 206,868 |
def note_to_frequency(note):
""" Translates a MIDI sequential note value to its corresponding frequency
in Hertz (Hz). Note values are of integer type in the range of 0 to
127 (inclusive). Frequency values are floating point. If the input
note value is less than 0 or greater than 127, ... | fb77367a86341b728fdfa9cf9e98ec8ea699390d | 153,514 |
import json
import logging
def parse_json_to_dict(text_data):
"""parse json-containing text, return a python dictionary"""
try:
results = json.loads(text_data)
return results
except:
#print('error parsing json result: {}'.format(sys.exc_info()[0]))
logging.exception('unexpe... | 5528ac68bec9f8d3cd8338bffbe7b3a65d719417 | 110,210 |
def initialize_log(file_path, mode='a'):
"""
Initialize a file in which logs will be written during the execution of
training, validation...
Args:
file_path: string with the path to the file
mode: default 'a', (if the file exists, it will continue writing logs
after the... | 0c32270ab5c0ba8ca5633b486f27d109bcb72cf0 | 275,211 |
def coef_updation(c1min,c1max,c2min,c2max,iteration,n_iterations):
"""
Coeficients update function
Parameters:
c1min: float
Minimum value of the acceleration coefficient c1
c1max: float
Maximum value of the acceleration coefficient c1
c2min: float
... | 952d767f645d3bc757f034a715da2081beb8086b | 142,119 |
def check_value_above_filter(value, threshold):
"""
Returns a boolean to indicate value at or above threshold.
:param value: integer from a column "*read count".
:param threshold: threshold for the filtering of these read counts.
:return: boolean whether integer is equal or greater than threshold.
... | d67802f591d3925f233249b388719cca230e5cc3 | 619,552 |
def valid_time(time):
""" 針對時間格式進行偵錯
minute、second 介於 0 ~ 60;hour is positive
return: boolean
"""
if time.second < 0 or time.minute < 0 or time.hour < 0:
return False
if time.second >= 60 or time.minute >= 60:
return False
return True | 633febba2a8b05643ff7f622d8372d2cf7ab4f13 | 176,667 |
def find_first_between(string, left, right):
""" Find first string in `string` that is between two strings `left` and `right`. """
start = string.find(left)
if start != -1:
end = string.find(right, start + len(left))
if end != -1:
return string[start + len(left):end] | 199c2c1ce91633e1881c84d359124b767dcbabfa | 340,329 |
import dill
def apply_packed_function_for_map(arg, ):
"""
Unpack dumped function as target function
and call it with arguments.
:return:
result of target function
"""
(dumped_function, item, args, kwargs) = arg
target_function = dill.loads(dumped_function)
res = target_functi... | ad48f30ff2769903144e37039f03c2f68598475b | 183,935 |
def register_new_peak(background_index_left, background_index_right, keys):
"""Registers a new peak by creating a peak from the background values given
Args:
background_index_left (int): index in compressed depths of background left to a peak
background_index_right (int): index in compressed de... | af943a71bb530be3c087912961d331875771b355 | 266,932 |
def screen_collection(src, cols, possibilities):
"""Return entries in a list of dicts where a set of fields match one of a set of possible values for those fields."""
rows = []
for row in src:
check = tuple([row[col] for col in cols])
if check in possibilities:
rows.append(row)
... | ff3b8deada521639bf4978ea15da29327283f602 | 412,053 |
def in_comment(regex):
"""Builds a regex matching "regex" in a comment"""
return '^[ \t]*//[ \t]*' + regex + '[ \t]*$' | c071bcca0cdc2bdc0982ae7bd27d427a180d07d2 | 365,830 |
import re
def is_ipv4(address):
""" Checks if given address is valid IPv4 address
:param str address: IP address to check
:return bool: True if address is valid IPv4 address, False otherwise
"""
regexp = "^(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|... | 65a1fde7d94daaec39dc76658d8fa0504acf6b3b | 485,413 |
def dict_to_aws_tags(d):
"""
Transforms a python dict {'a': 'b', 'c': 'd'} to the
aws tags format [{'Key': 'a', 'Value': 'b'}, {'Key': 'c', 'Value': 'd'}]
Only needed for boto3 api calls
:param d: dict: the dict to transform
:return: list: the tags in aws format
>>> from pprint import pprin... | 12680d9ccd1cfc04a6ac94910256d52018daa22d | 656,177 |
def make_xz_ground_plane(vertices):
"""
Given a vertex mesh, translates the mesh such that the lowest coordinate of the mesh
lies on the x-z plane.
:param vertices: (N, 6890, 3)
:return:
"""
lowest_y = vertices[:, :, 1].min(axis=-1, keepdims=True)
vertices[:, :, 1] = vertices[:, :, 1] - ... | e05622420f431882a11cf60f4db28c094cbc0dca | 658,561 |
def AT(y,x):
"""
Returns control codes to set the coordinates of the text cursor.
Use this in a ``PRINT`` or ``SET`` command. Example:
``PRINT("normal",AT(5,15),"row 5 column 15",AT(14,4),"row 14 column 4")``
Args:
- y - integer - the y coordinate to move to (0-23)
- x ... | 2c42890dd20e1a436d978b7cc08dff9205a902b4 | 240,714 |
def sorting_rivers(station):
"""sorting function for stations_by_rivers"""
river = station[1]
return river | f790ebcaeeff93a0859d36e4f57a78ea52d3f2b5 | 619,558 |
def ver_list_float(param_name, param_value):
"""
This function verifies if all elements in a given list are float.
Parameters
----------
param_name : str
the parameter being evaluated
param_value : list
the list to evaluate for float values
Returns
-------
eval : b... | 5d2fa5d993c8293ea3464b055cc488be48b602c7 | 352,123 |
import string
def is_valid_letter(letter):
"""
Returns True if the input letter contains only one character
in ascii_letters. Both upper and lower case is OK. Otherwise
returns False.
:param letter: string
:return: True or False
"""
if len(letter) != 1:
return False
if lett... | 45e038df4a18f970793fa76363b0a10813c28f5b | 378,135 |
def _parse_suspected_cls(predator_result):
"""Parse raw suspected_cls into dict."""
if not predator_result:
return None
# The raw result contains some additional information that we don't need here.
# Everything we're concerned with is a part of the "result" object included
# with the response.
predato... | 0f4d72542fc138512d4bbfe3c997f8e9b940a96c | 629,806 |
def string_to_list(s):
"""
e.g. '124' --> [1, 2, 4]
"""
return list(map(int, list(s))) | c4e8ea51f8c7bedf8c5d69e22f02d53bc9cab7f2 | 68,606 |
def format_example_name(example):
"""Formats an example command to a function or file name
"""
return '_'.join(example).replace('-', '_').replace(
'.', '_').replace('/examples/', '')[1:] | 223301709fb52aac98e2622c7df760fd3d7056e1 | 35,747 |
def line_with_jacoco_test_footer(line, report_type):
"""Check if the given string represents JaCoCo unit test footer."""
return report_type == "jacoco" and line == "Code coverage report END" | 63bc1e47c61e4898c017242865fc2b8039b2bf7c | 453,308 |
def lcm(num1, num2):
"""Returns the lowest common multiple of two given integers."""
temp_num = num1
while (temp_num % num2) != 0:
temp_num += num1
return temp_num | bdb1c66c2e155fe930ffb84d2657e1536d2387dc | 101,134 |
def assure_list(s):
"""Given not a list, would place it into a list. If None - empty list is returned
Parameters
----------
s: list or anything
"""
if isinstance(s, list):
return s
elif s is None:
return []
else:
return [s] | f15cf892c1e278f5a99db57b91594cfc209c5fe4 | 249,214 |
def slice_size(slice_objects):
"""
Returns the total number of elements in the combined slices
Also works if given a single slice
"""
num_elements = 0
try:
for sl in slice_objects:
num_elements += (sl.stop - (sl.start + 1)) // sl.step + 1
except TypeError:
num_e... | ef3ae504537390cbe2a1458298813f51a4ddb89f | 56,040 |
import yaml
def load_yaml(stream):
"""
Load YAML data.
"""
loader = yaml.Loader(stream)
try:
return loader.get_single_data()
finally:
loader.dispose() | 8f46e91d0e4c69866950854a31b11ec2a0e610e3 | 257,534 |
def IsGlobalUrlMapRef(url_map_ref):
"""Returns True if the URL Map reference is global."""
return url_map_ref.Collection() == 'compute.urlMaps' | 82280837da4cd9a16baf27fe08b88f6464535494 | 403,364 |
def whether_in_file(email, rows):
"""
Check if email is in CSV file.
:param email: str
:param rows: list of csv file rows [row0, row1, ...]
:return: is_in_file: boolen
"""
is_in_file = False
for row in rows:
if email.lower() == row[0].lower():
is_in_file = True
... | f14d593d5b16e6c3f5718821be5c6425b768f1b9 | 334,267 |
def best_med_worst(f1s, c_test_genomes, tla_to_tnum):
"""
Get the best, median, and worst reconstructions from the test set, as measured by F1 score
Arguments:
f1s (list) -- test F1 scores
c_test_genomes (list) -- tlas of genomes in the test set
tla_to_tnum (dict) -- maps tla to tnum for each genome
Retu... | 717ffade5c7e52ddf279279d931ac18ea84c074c | 288,639 |
def basic_block_size(bb):
""" calculate size of basic block """
return bb.end_ea - bb.start_ea | 57986803a9de2555fd419311200ba342a31bc07b | 325,983 |
def validate_vlan_id(vlan_id):
"""
Validate VLAN ID provided is an acceptable value
:param vlan_id: int
:return: list
"""
errors = []
if vlan_id < 1 or vlan_id > 4094:
errors.append("Invalid ID: must be a valid vlan id between 1"
" and 4094")
return errors | 638270360b718587371a23952d321f88b9de3f1c | 244,431 |
def find_field_with_suffix(val, suffix):
"""
Return ``val[field]``, where ``field`` is the only one whose name ends
with ``suffix``. If there is no such field, or more than one, raise KeyError.
"""
names = []
for field in val.type.fields():
if field.name.endswith(suffix):
na... | 8ec2fe6d48bd4e8ea3377f6c126292229518b17a | 127,523 |
def _downsample_simple(arr, n):
"""
Skip n elements of a 1D array.
:param arr: 1D array.
:param n: integer which defines the skips.
:return: Downsampled 1D array.
"""
return arr[::n] | bd531cf5a5771966dfb3edba5136dd69ea2e2849 | 647,779 |
from typing import Callable
import inspect
import functools
def wrap_in_coroutine(func: Callable) -> Callable:
"""Decorator to wrap a function into a coroutine function.
If `func` is already a coroutine function it is returned as-is.
Args:
func: A callable object (function or coroutine function)... | 3134241771749d63ce5213180a34d7c26f8f0c76 | 40,677 |
import re
def regex_prettifier(scraped_data, regex):
"""Prettify the scraped data using a regular expression
Positional Arguments:
scraped_data (list): data scraped from a website
regex (str): a regular expression
Return:
list: the regex modified data
"""
data_list = []
... | 5eb42d0df2a0f93dbc14ec5fb5dd68bc6fe127ca | 34,814 |
def bending_stresses(mx, my, mxy, z, h):
"""
Computes the bending stresses at given shell thickness and fiber height.
"""
def stress(m):
return m * z / ((h ** 3) / 12)
return stress(-mx), stress(-my), stress(mxy) | 0a089542ccfee722dfa5746f357ef529b0124fa7 | 397,813 |
def summarize_missing_df(dataframe):
"""
Populate the fraction missing counter from a given DataFrame
"""
results = {
'frac_missing': float((dataframe != 0.0).sum().sum()) \
/ float((dataframe.shape[0]*dataframe.shape[1]))
}
return results | d69fe13bfcb0f176d0ae1024d0f043816f732a8d | 549,180 |
import codecs
def load_utf8(filename):
"""Load UTF8 file."""
with codecs.open(filename, 'r', encoding='utf-8') as f:
return f.read() | 9ce0f51879b2bc4b83690fb06a0353c3c73235cb | 637,694 |
import struct
def get_bool(s: bytes) -> bool:
"""
Get bool value from bytes
:param s: bytes array contains bool value
:return: bool value from bytes array
"""
if str(s) == "b\'F\'":
return False
elif str(s) == "b\'T\'":
return True
else:
return struct.unpack('?'... | 8da9c3c76a578ca347f8835404ef415005a39ed4 | 609,406 |
def label_from_id(id_string):
"""
Returns a label string constructed from the suppliued Id string
Underscore characters in the Id are replaced by spaces.
The first character may be capirtalized.
>>> label_from_id("entity_id") == "Entity id"
True
"""
temp = id_string.replace('_', ' ').... | 9b52abdce169d26412a69585281ebadcff7cb0c2 | 40,036 |
import json
import base64
def decode_environ_backup(encoded_environ_backup: str) -> dict:
"""
Decode environ backup from safe string.
"""
return json.loads(base64.b64decode(encoded_environ_backup).decode("utf-8")) | 1aa9f90bf9aa7f64c761a174e95394779534ad59 | 605,242 |
def quote_item(item, pre='', post=''):
"""Format an string item with quotes.
"""
post = post or pre
return f'{pre}{item}{post}' | 5b3877a5ba1cd991406a6900d2dfd6ad7d3d42d1 | 177,779 |
def soup_extract_enzymelinks(tabletag):
"""Extract all URLs for enzyme families from first table."""
return {link.string: link['href']
for link in tabletag.find_all("a", href=True)} | 7baabd98042ab59feb5d8527c18fe9fa4b6a50af | 707,306 |
def get_counts(filename):
"""
reads the .counts_edit file and extracts the counts
:param filename: counts file (original or simulated)
:return: list of counts
"""
with open(filename, "r") as counts_file:
counts = []
for line in counts_file:
line = line.strip()
... | 2bb9914fbca82550e121f0dcb5c55f6817da6c91 | 687,739 |
def default(v, d):
"""Returns d when v is empty (string, list, etc) or false"""
if not v:
v = d
return v | 17d0ee06fa28c088512aa6f5ecb7181949bbc315 | 541,872 |
def generate_parameter_map(parameters):
"""
Generates mapping of parameters for fasm features for architecture
definition.
"""
xml = []
xml.append("<meta name=\"fasm_params\">")
for feature, parameter in sorted(parameters):
xml.append(" {} = {}".format(feature, parameter))
xml.... | de54f03de88b858cf4face404e47a1c5ced6d954 | 329,307 |
from typing import Iterable
from typing import List
import re
def split(delimiters: Iterable[str], s: str, maxsplit: int = 0) -> List[str]:
"""Split the string over an iterable of delimiters.
Based on https://stackoverflow.com/a/13184791
"""
pattern = "|".join(map(re.escape, delimiters))
return re... | 42ab4216b6c24e28fb98970b85e7492612ac1a21 | 29,297 |
from typing import List
def getVariantRedirects(rTitle: str) -> List[str]:
"""Get list of variant abbreviations similar to rTitle.
Similar means obtained by replacing an ISO-4 abbreviation with a
popular non-ISO-4 alternative hardcoded here, or by removing dots.
In particular both rTitle and rTitle.r... | a2801b20dff3d8ad5175c5fd2fa577d3307e8268 | 304,750 |
def hop_to_str(offset):
"""
Formats a two-character string that uniquely identifies the hop offset.
>>> hop_to_str([-3, 0])
'L3'
>>> hop_to_str([1, 0])
'R1'
>>> hop_to_str([0, -2])
'T2'
>>> hop_to_str([0, +7])
'B7'
"""
# Zero offsets are not allowed
assert offset[0]... | e5b59a37ef76b8a6fcbe1f5afc5ce57b861ca4b9 | 481,829 |
def fib(x):
"""
Returns the xth term of the Fibonacci sequence
"""
a, b = 1, 1
for i in range(1, x):
a, b = b, a + b
x += 1
return(a) | 76cb08e89f11152c1aa2240b14c216042850b0ab | 44,432 |
def binary_search(arr, key):
"""
Searches for the key in a list and returns the position of the key in the array.
If the key can not be found in the list, raise a ValueError
Will keep shifting the middle point for as long as the middle value is not equal to the search key
If, the search key is less ... | 8b7fbd9ded4741c32c766c57d3698d87ea739e18 | 55,725 |
import math
def offset(point, angle, dist):
"""Find the point `dist` from `point`, in the direction `angle`."""
x, y = point
angle = math.radians(angle)
x += dist * math.cos(angle)
y += dist * math.sin(angle)
return x, y | 5a593a13cd979bcfd08299c9245e3526f5e2586c | 195,185 |
def xml_find_single_tag (root, tag_name, required=True):
"""
Fetch an XML tag from XML.
:param root: XML to utilize
:param tag_name: Name of tag to fetch
:param key: A boolean indicating whether tag is required for a valid manifest XML
:return Tag if found
"""
tag = root.findall (tag_... | 89234b451a34239373227201688b2dd4317d4d35 | 307,073 |
import torch
def H(x, eps=1e-6):
""" Compute the element-wise entropy of x
Arguments:
x {torch.Tensor} -- array of probabilities in (0,1)
Keyword Arguments:
eps {float} -- prevent failure on x == 0
Returns:
torch.Tensor -- H(x)
"""
return -(x+eps)*torch.log(x+eps) | 25c0e1684130fbffcd64c42b142063a0f501a303 | 475,226 |
def api_version() -> str:
"""Which Adwords API version should be called"""
return 'v201809' | e304cff2fe010095f77719731bf9bedd310f0feb | 220,814 |
import re
def workdir_from_dockerfile(dockerfile):
"""Parse WORKDIR from the Dockerfile."""
WORKDIR_REGEX = re.compile(r'\s*WORKDIR\s*([^\s]+)')
with open(dockerfile) as f:
lines = f.readlines()
for line in lines:
match = re.match(WORKDIR_REGEX, line)
if match:
# We need to escape '$' sinc... | 33a927626a023ba988534afe5b3f8885e18db471 | 47,126 |
def get_change_extent(str1, str2):
"""Determines the extent of differences between two strings.
Returns a pair containing the offset at which the changes start,
and the negative offset at which the changes end.
If the two strings have neither a common prefix nor a common
suffix, ``(0, 0)`` is retu... | 275ae78cddc8bfe2948dfdc48e66b85b79cd27b8 | 496,993 |
import operator
import functools
def all_answering_yes(survey: list[str]) -> int:
"""
Counts the number of A-Z questions where every person
in the survey group answered yes.
"""
set_intersect_op = operator.and_
central_response = functools.reduce(set_intersect_op, (set(response) for response i... | 4b870dd1ca730f354727e8a9ce0d2d12839139f1 | 509,368 |
def get_hit_fields(es_resp_obj_dict):
"""Returns a list of they fields in an elasticsearch search response
hit object."""
if es_resp_obj_dict['hits']['hits']:
fields = [key for key in es_resp_obj_dict['hits']['hits'][0]['_source'].keys()]
else:
return []
return fields | be090011f8ad96ff0bb4c745c9fd50fb762d87ee | 253,992 |
def ask(question, answers, default_answer, type_validator=str):
"""Ask question to user via console.
:param question: [str] The question to ask.
:param answers: [list of str, str] The possible answers to choose from. If
'' (empty string), then no answer will be suggested.
:param default_answer:... | 1e3c687eab1179591136b2390817ee45557a7e21 | 242,981 |
def checkH1(strings_h1, text_h1):
"""Checks if the string in h1 tags is present.
:param strings_h1: A list with all the strings contained in the H1 tags.
:param text_h1: the string to look for inside the tags.
:return: True if the text is present in the tags, False otherwise
:rtype:... | cd13dbed935633ae11ed5e16fa2a5c5c469cdb2f | 265,996 |
def _find_label_rows(sheet):
"""Search excel file column A for cells containing 'Label'.
Return a list of zero-indexed rows.
"""
label_rows = []
for i in range(sheet.nrows):
if "Label" in sheet.cell_value(i, 0):
label_rows.append(i)
return label_rows | 5515874500c5ef514df02019e745d609b0474b2f | 38,151 |
def padded_print(field_name, value):
"""Pad a string with leading spaces to be the same length as field_name."""
offset = len(field_name) - len(str(value))
if offset > 0:
return (' ' * offset) + str(value)
return str(value) | 05cbe76850b5f41c55a4180477783b5f32f635f3 | 136,861 |
import base64
def Base64WSEncode(s):
"""
Return Base64 web safe encoding of s. Suppress padding characters (=).
Uses URL-safe alphabet: - replaces +, _ replaces /. Will convert s of type
unicode to string type first.
@param s: bytes to encode as Base64
@type s: bytes
@return: Base64 representation of... | 5b07d94f4e5cdc35eb4094fe85f9567ef9f93455 | 444,066 |
def _getShapeSize(df, shape_name_str):
"""
Get the arbitrary shape size with its shape name.
Args:
df: tuple-(df_CompartmentData, df_NodeData, df_ReactionData, df_ArbitraryTextData, df_ArbitraryShapeData).
shape_name_str: str-the shape name.
Returns:
shape_size_list: list... | 76bb20e423b26472c841bfd345fc8f42ce0888bd | 624,034 |
import time
def date_str(t):
"""Convert seconds since the Epoch to formatted local date string."""
t = time.localtime(t)
return time.strftime('%Y-%m-%d',t) | c54ee8b480f843a9508025beed591905929a1f3a | 358,693 |
def echo(message='hello'):
"""
Very simple endpoint that just echos your message back to you
:param message: str of the message to echo
:return: str of the message echoed
"""
return 'ECHO: %s' % message | 7c023b7a33043459460c179e0f7f2abae36a5a99 | 355,675 |
def process_content(df, config, dataframes):
"""
Applies a function to each value of the column
"process_content": [
{
"function": lambda x: str(x),
"columns": ["column1", "column2"]
}
]
"""
for function_dict in config.process_content:
for column i... | 41040b0eafe0f97526b645fdecf363d653c1cf85 | 87,077 |
import codecs
def read_lines(infile, ignore_comments=True, strip=True, skip_empty=True):
"""Read lines from infile and return as a list, optionally stripping comments,
whitespace, and/or skipping blank lines."""
lines = []
with codecs.open(infile, "r", "utf-8") as f:
for line in f:
... | 70aac2290bac6f99818fd7e199f1cd442ec56617 | 300,175 |
import logging
def _WarnAboutDuplicates(offsets):
"""Warns about duplicate offsets.
Args:
offsets: list of offsets to check for duplicates
Returns:
True if there are no duplicates, False otherwise.
"""
seen_offsets = set()
ok = True
for offset in offsets:
if offset not in seen_offsets:
... | 50f9e6891e79afc61f53ae5adb8d6832e8ff2856 | 70,427 |
def get_string_between(start, stop, s):
"""Search string for a substring between two delimeters. False if not found."""
i1 = s.find(start)
if i1 == -1:
return False
s = s[i1 + len(start):]
i2 = s.find(stop)
if i2 == -1:
return False
s = s[:i2]
return s | 2a9927d2a7a5a1efc4774051d1f86247b6195f07 | 279,720 |
def field(name, transform=None):
""" Convenience function to explicitly return a "field" specification for
a Bokeh :class:`~bokeh.core.properties.DataSpec` property.
Args:
name (str) : name of a data source field to reference for a
``DataSpec`` property.
transform (Transform, o... | bc391f991fcebad956464c1f0cc741505d3d4028 | 637,027 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.