content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
def remove_duplicate_index(df):
"""Remove all entries in a data frame that have a duplicate index.
:param df: DataFrame containing duplicate indicies.
:returns: DataFrame with the duplicates removed
"""
index_name = df.index.name
md = df.reset_index()
md.drop_duplicates(index_name, inplace=... | 1a22c39b82c91aa00bbb7c7c71e18427cbd3058a | 296,938 |
def struct_dict(struct):
"""
take a ctypes.Structure and return its field/value pairs
as a dict.
"""
get_pair = lambda field_type: (
field_type[0], getattr(struct, field_type[0]))
return dict(list(map(get_pair, struct._fields_))) | dcabfa370af5446255cb71faeccb2f8086b35ff6 | 324,051 |
def _get_c_string(data, position):
"""Decode a BSON 'C' string to python unicode string."""
end = data.index(b"\x00", position)
return data[position:end].decode('utf8'), end + 1 | 6cd01aba9c675cdef0db5df4d523e3fdbf7a2ff6 | 249,232 |
from typing import List
def as_package(names: List[str]) -> str:
"""Joins names as a package name."""
return '.'.join(names) | 7369de516104d94d11784e1dfef64ca9c86ddb26 | 612,463 |
def option_list(opts):
"""Convert key, value pairs into command-line options.
Parameters
----------
opts : dict-like
Convert a dictionary into command-line options.
Returns
-------
:class:`list`
A list of command-line options.
"""
optlist = []
for key, val in op... | db4acd20b1509b757776916411609cfa576168ea | 555,866 |
def list_all_equal(lst):
"""Return true if all elements in the list are equal."""
return len(set(lst)) == 1 | 1c01928305ada17390e9198cb25d8af2e913ad9b | 437,092 |
import re
def configSingleEntry (lineData, configSection):
""" Searches for section and returns single entry lines after section heading """
sectionContentsString = ""
error='NONE'
sectionfound = 0
for line in lineData:
#print(line)
if sectionfound is 0:
if re.match(con... | da5325d6ff525e70a9fa46b7380a2585d831a15e | 474,887 |
def narrowpeaks_cols() -> list:
"""
Return list of narrowpeak column names
:return: a list of string
"""
return ['chr', 'start', 'stop', 'name', 'score', 'strand',
'signalValue', 'pValue', 'qValue', 'peak'] | 3cd49c738fff19775c0ee934a1a2158dd0b06fce | 557,953 |
import logging
def collect_element(tree, selector: str, default=""):
""" Selects first element from html tree by selector and returns its text_content(), or default if element not found """
elements = tree.xpath(selector)
if not elements:
logging.info("Collecting isn't possible by selector: %s", s... | d055b3c638d5c2982173c04635e8c699a038d200 | 242,818 |
def keep_expired_by(flags, mstone):
"""Filter flags to contain only flags that expire by mstone.
Only flags that either never expire or have an expiration milestone <= mstone
are in the returned list.
>>> keep_expired_by([{'expiry_milestone': 3}], 2)
[]
>>> keep_expired_by([{'expiry_milestone': 3}], 3)
... | c5c6b7e7a2cc730a77e3928e78977b14d10759b4 | 452,922 |
def argmax(x):
"""
Returns the index of the largest element of the iterable `x`.
If two or more elements equal the maximum value, the index of the first
such element is returned.
>>> argmax([1, 3, 2, 0])
1
>>> argmax(-abs(x) for x in range(-3, 4))
3
"""
argmax_ = None
max_... | 847f7f7a599c0623d8ee6218ea85a8176fa07d79 | 220,926 |
import tempfile
import requests
def file_from_url(url: str):
"""
file_from_url requests a file from an URL.
Raises an exception if the request fails.
Parameters
==========
url : str
The resource URL.
Returns
=======
_TemporaryFileWrapper
Requested file as tempor... | f0ae2801e49604129f359bc8d5d6cbb97d58a887 | 116,395 |
def get_unicode(text: str) -> bytes:
"""Returns the unicode for input text
Parameters
----------
text : str
Text to encode
Returns
-------
bytes
Text with characters encoded in raw unicode.
"""
return text.encode("raw_unicode_escape") | 701e37c96e9c93921c744a16afae570fc345d02c | 247,704 |
def fix_initial_data(initial, initial_data_keymap):
"""
Take a dict like this as `initial`:
{ 'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
and a dict like this as `initial_data_keymap`:
{ 'newkey1': ['key1', 'key2'], 'newkey2': ['key3']}
and remap the initial dict to have this form:
{ 'n... | 5f72592e460c058c7a3ffee6f1e2d5957c5fe7b1 | 576,708 |
def invcalcbarycentric(pointuv, element_vertices):
"""
Convert barycenteric coordinates into 3d
https://en.wikipedia.org/wiki/Barycentric_coordinate_system
https://math.stackexchange.com/questions/2292895/walking-on-the-surface-of-a-triangular-mesh
:param pointuv: Point in barycenteric coordinates ... | 9aebf9e0579321788b242653a8c51b20dcad2fea | 80,752 |
def readall(read_fn, sz):
"""Reads `sz` bytes using `read_fn`
Raises `EOFError` if `read_fn` returned the empty byte array while reading
all `sz` bytes.
"""
buff = b''
have = 0
while have < sz:
chunk = read_fn(sz - have)
have += len(chunk)
buff += chunk
if l... | a5f1395dd5306f7e907a598e716090fc3f19681d | 163,504 |
def get_load_config_timestamp(pefile_object):
"""
Retrieves the timestamp from the Load Configuration directory.
:param pefile.PE pefile_object: pefile object.
:return: Recovered timestamps from PE load config (if any).
None if there aren't.
:rtype: int
"""
timestamp = 0
if hasattr... | 4b04afa7d844ce05761fa5b8f484540a1ae243a4 | 21,184 |
def append_default_extension(filename, default_extension='.png'):
"""If a filename has no extension yet, add the default extension to it"""
if '.' in filename:
return filename
else:
return filename + default_extension | 70181b03aba9ec73836d6e33784b2123ade67406 | 118,006 |
def hexify(s):
"""
Used to convert message to hex integers when encrypting.
:param s: a string.
:return: a string like '0x..', decode each character as a ascii hex number.
"""
if not s: # if input is null, evcrypt the message: '!'
return '0x21' # ascii code for '!'
lst = []
fo... | e457880c713cbc8d70ba3463a6fe9331a96ba5a8 | 630,709 |
import torch
def init_optimizerD(opt, D, train_last_layer_only=False):
"""Initialize optimizer for discriminator D
"""
params_to_train = D.parameters()
if train_last_layer_only and opt.DiscArch == 'concat_first':
params_to_train = D.last_linear.parameters()
optimizerD = torch.optim.Adam(pa... | 9c788aae510ab6cf64f588646450b9c285934375 | 159,934 |
def _substitute(template, fuzzer, benchmark):
"""Replaces {fuzzer} or {benchmark} with |fuzzer| or |benchmark| in
|template| string."""
return template.format(fuzzer=fuzzer, benchmark=benchmark) | 84e110aa6aca334176ae1bd682472c008d0ee228 | 291,880 |
import yaml
def read_params_from_file(fname):
"""Read model parameters from a file.
Parameters
----------
fname : str
Name of YAML-formatted parameters file.
Returns
-------
dict
A dict of parameters for the heat model.
"""
with open(fname, "r") as fp:
par... | cec5fe2af0578a6288c415823618d005144f6151 | 472,702 |
def _CustomMachineTypeString(cpu, memory_mib):
"""Creates a custom machine type from the CPU and memory specs.
Args:
cpu: the number of cpu desired for the custom machine type
memory_mib: the amount of ram desired in MiB for the custom machine
type instance
Returns:
The custom machine type n... | bfa285d9de4a66c9b88ef89925ce5266bb8e68c7 | 479,528 |
def _strip_shape(name: str) -> str:
"""Strip the dimension name."""
if name[0] == "/":
return name[1:]
return name | ba6329bdca1cf4e7c4db7daf0fcbb3c5c5f475b7 | 579,191 |
def serialize_results(results : dict) -> str:
"""Serialize a results dict into something usable in markdown."""
n_first_col = 20
ans = []
for k, v in results.items():
s = k + " "*(n_first_col-len(k))
s = s + f"| {v[0]*100:.1f} | {v[1]*100:.1f} |"
ans.append(s)
return "\n".j... | c64ba7bbf898e3085290615d163401b7c2382ca6 | 626,979 |
def is_in_any_txt(txt, within_txts_list, case_insensitive=False):
"""is "txt" in any of the texts list ?"""
for within_txt in within_txts_list:
if case_insensitive: # slower
if txt.lower() in within_txt.lower():
return True
else:
if txt in within_... | 5e5316fdbaf21617ee8ae3172a0efe4a3c17ae8c | 669,588 |
def tensor_size_bytes(tensor, unit='MB'):
"""
Get the size of the tensor in bytes, or a unit that's multiple of bytes
:param tensor: the pytorch tensor
:param unit: GigaBytes or GB (assumes GB=1e9 Bytes), MegaBytes or MB (assumes MB=1e6 Bytes),
KiloBytes or KB (assumes KB=1e3 Bytes), Bytes or B
... | f8d68b99597b2bcfb90ff18f7f1d9f9b02478de1 | 110,700 |
def slicex(df, values, keep_margins=True):
"""
Return an index-wise slice of df, keeping margins if desired.
Assuming a Quantipy-style view result this function takes an index
slice of df as indicated by values and returns the result.
Parameters
----------
df : pandas.DataFrame
The... | 6719eba7824b04b2e1d5735fd699412190a8811e | 53,718 |
def _step_id(step):
"""Return the 'ID' of a deploy step.
The ID is a string, <interface>.<step>.
:param step: the step dictionary.
:return: the step's ID string.
"""
return '.'.join([step['interface'], step['step']]) | c1ff629a09758ff5817f76ea487af467fbbffc84 | 68,298 |
def add_titlebox(ax, text, x=0.05, y=0.9, alignment='left', fontsize=12.5):
"""
add title text box.
Arguments:
ax {`matplotlib.axes.Axes`} -- the `Axes` instance used for plotting.
text {string} -- title text
Keyword Arguments:
x {float} -- title x position (default: {0... | ce8804d5f25097046c54cbeb4c15936888539eae | 554,533 |
import re
def byteform_to_num(byte_format):
"""Converts a string expressing a size of a file in bytes into the
corresponding number of bytes. Accepts commas and decimal points in nums.
Allows 'b', 'mb', 'gb', and variants like 'bytes', but not 'tb', 'zb', etc.
Note that format_bytes is a lossy... | 300ede4ef120b9e3a8db85effcb6611dd9299953 | 10,953 |
import tempfile
def write_tmpacl(acl, process_name='_tmpacl'):
"""Write a temporary file to disk from an Trigger acl.ACL object & return the filename"""
tmpfile = tempfile.mktemp() + process_name
f = open(tmpfile, 'w')
for x in acl.output(acl.format, replace=True):
f.write(x)
f.write('... | 7aa735a85dc60ce1975769045c6ee52031f7ba38 | 360,283 |
def set_config_var(from_file, from_env, default):
"""
Set a configuration value based on the hierarchy of:
default => file => env
:param from_file: value from the configuration file
:param from_env: value from the environment
:param default: default configuration value
:return: value to... | 48e0fa5aa4b9e67ceb4a7cb1fea7e23529a5f3bf | 102,741 |
def get_synsets(lemma, synset_dict):
"""Return synonym set given a word lemma.
The function requires that the synset_dict is passed into it. In our case
we provide downloadable models from MCR (Multilingual-Central-Repository).
:cite:`gonzalez2012multilingual`. If the lemma is not found in the
syns... | 1e0ef9669a6bcdc3f8c1a19013d1cbf109266330 | 296,981 |
import traceback
def _str_backtrace(backtrace=None):
""" Return a string representation of an existing or new backtrace """
if backtrace:
return "".join(traceback.format_tb(backtrace))
else:
return "".join(traceback.format_stack()[:-1]) | 6f4038fde8d8e0ab350cb9a41bfabe75b4017997 | 506,266 |
def vshale_to_vclay(vshale, multiplier):
"""
Converts a shale volume to clay volume using a multiplier.
Parameters
----------
vshale : float
Shale volume
multiplier : float
Shale to clay multiplier (decimal)
Returns
-------
float
Returns a clay volume.
... | f46fd70940a513d990b25d505a59d516ae525a15 | 356,192 |
def is_nonterminal(text: str):
"""Return True if string text starts with ( and doesn't end with )."""
return text[0] == '(' and not text[-1] == ')' | d668d1edf541e5a381282caadb36daa974c17d8a | 545,782 |
def read_mg_types(mgfile):
"""
Read the metagenome types from a file where the first four columns
are [domain, type, species, seqid]. For example, sharks_fish.distance.labelled.tsv
which is the output from join.pl
:param mgfile: the sharks_fish.distance.labelled.tsv file
:return: a dict of all t... | b7eabc1a52199951404f114e0732020a6c807334 | 133,897 |
def _parse_line(line):
"""If line is in the form Tag=value[#Comment] returns a (tag, value)
tuple, otherwise returns None."""
non_comment = line.split('#')[0].strip()
if len(non_comment) > 0:
tag_value = [x.strip() for x in non_comment.split('=')]
if len(tag_value) != 2:
rais... | b39f6862768fe8937acd17a9830329dc213fc31f | 477,679 |
def quotify(comment, username):
"""
Converts 'Foo\nbar' to:
> @username said:
> Foo
> bar
\n\n
"""
header = "@%(username)s said:" % {'username': username, }
lines = comment.splitlines()
quote = "\n> ".join(lines)
quote = "> %(header)s\n> %(quote)s\n\n" % {'header': header, '... | ad601fb15199cbf481daea26b9baecb4b0b0028d | 459,403 |
def normalize(name):
"""
normalizes the name of the company for comparison by lowercasing, substitution
:param name: the name string
:return: normalized string
"""
if name == "-":
return "company_name_not_available"
name = name.lower().strip().replace("co ", "company").replace("co.",... | bfabb84b004c242a6c34b8a4efd54e265c1c711d | 321,751 |
def get_normalized(x, xmax, xmin):
""" Normalized a value given max and min """
x_norm = (x - xmin)/(xmax - xmin)
return x_norm | 6db770cf71eb73d2a5cddbfc5bc1f88db92cf397 | 280,592 |
from datetime import datetime
def text_to_datetime(dt):
"""Convert text date and time to datetime format then put in ISO8601 format."""
converted = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
reformat_iso = datetime.strftime(converted, "%Y-%m-%dT%H:%M:%S")
return reformat_iso | 5d7e9c2a744e4b56d815987cd5110fccd0476cc8 | 114,320 |
def interaction_test(configuration, order=None):
"""
Returns True if the configuration has an interaction
Parameters
----------
order : int, optional
Specific order to check for. E.g. a value of 3 checks for ternary interactions
Returns
-------
bool
True if there is an ... | 052d82d9c218ae54abb739e789d5789f83029dcf | 253,465 |
def dichotomy(list, value):
"""
list: array of integers in ascending order
value: value to return its position in list
return: False in value not in list, else the position of value in the list
"""
list_beginning = 0
list_end = len(list)
list_middle = ((list_beginning+list_end)//2)
if value not in lis... | 4aa89c0b28a92cf371c1b4b688931cdf99331c70 | 304,392 |
import torch
def particle_to_quantile(predictions):
""" Converts a particle prediction to a quantile prediction.
Not fully differentiable because of the sorting operation involved. Should be thought of as a permutation function
Args:
predictions (tensor): array [batch_size, n_particle... | 11a88bee1b3f420344a6d5f8f0eab027856ec27d | 290,176 |
def get_mu_tilda(x_i, r, n):
"""Calculates the conditional descendant normal distribution *expectation*
for generation-gap n.
Latex equation:
tilde{\mu}_{i+n} = r^n X_i
(See the paper for the derivation.)"""
return (r**n) * x_i | 3ff119704d3c9db2fbc1b52c683948a44a0ceb89 | 243,673 |
def imageSplit(
data,
labelcol='Label_nr',
imagedim=[455,423],
testsize=0.3):
"""
Split test and traing dataset taking the testsize proportion from the
top of the image as test dataset and the rest as training dataset
Parameters
----------
data: numpy.dataFra... | 1b3501572242e4091259970a882bd5fd950ea0ca | 345,082 |
def _resample_params(N, samples):
"""Decide whether to do permutations or random resampling
Parameters
----------
N : int
Number of observations.
samples : int
``samples`` parameter (number of resampling iterations, or < 0 to
sample all permutations).
Returns
------... | f1493aa3409cef023d05c9e74951d59f481b11bc | 121,021 |
def is_influx_rule(rule):
"""
A function that detects if the alert rule defined uses influx,
Influx rules should not contain a '#' because split returns
"""
if len(rule.check_field.split('#')) == 1:
return True
else:
return False | 60f168f2f859522607127fa82cc32f75401794f6 | 424,383 |
def timedelta_to_seconds(delta):
"""
Take a timedelta object and return the total number of seconds.
"""
return delta.seconds + (delta.days * 86400) | 23bc2b9e36f58db68e39625e41dd4f0728d25457 | 646,039 |
def strftime(format, p_tuple=None): # real signature unknown; restored from __doc__
"""
strftime(format[, tuple]) -> string
Convert a time tuple to a string according to a format specification.
See the library reference manual for formatting codes. When the time tuple
is not present, current ti... | 218d2a78bbd6bacadb2dcfd17800708dd7526038 | 539,830 |
def list_square_to_input(list_signs,list_index_signs):
"""
Take the list of squares fill in by a sign and create a 9x1
array (input_layer for neural network) with values -1 for a cross,
0 if empty square and 1 for a circle.
"""
input_layer = [0 for k in range(9)]
for i in range(len(list_sign... | 532918046029493ca8e2fb43369808e46dcd8501 | 79,743 |
import re
def alphanum_key(key):
"""
Used to generate a natural numeric sort key
Example: ("p10" => ["p", 10]) is greater than ("p1" => ["p", 1])
adapted from https://stackoverflow.com/a/2669120/240553
"""
def convert(text):
return int(text) if text.isdigit() else text
return [co... | 16a0f085b2051dbd951b801dd6713845eae9c09c | 88,034 |
def abs(var):
"""
Wrapper function for __abs__
"""
return var.__abs__() | c2e834da463e9e3e57933489537545d927f40e85 | 615,720 |
def to_digits_base10(n):
"""
Return the digits of a number in base 10.
"""
digits = []
remaining = n
while remaining > 0:
digit = remaining % 10
remaining = (remaining - digit) // 10
digits.append(digit)
return digits[::-1] | 5acc6a2ef1e10bc3142371944232c7d1bcad3a32 | 24,843 |
def power_spectrum(k,a):
"""Simple function for power laws
Args:
k (float array): wave number
a (float): power law
Returns:
float array: k^-a (note minus sign is assumed!)
"""
return k**-a | 4caa3bf6af923d4406a939cf14cd9d8893ef2e09 | 523,731 |
def get_turn_on_descriptions(get_turn_on, initial_state, current_state):
"""
Get all 'turn on' descriptions from the current state (if any).
Parameters
----------
get_turn_on: function
Function that gets the color of light which is on.
initial_state: nd.array
Initial state of the... | fe5e2bbca20be6fe3e008fd6167ec035d13d5fd7 | 279,702 |
def get_default_guess_params(model, df, pressure_key, loading_key):
"""
Get dictionary of default parameters for starting guesses in data fitting
routine.
The philosophy behind the default starting guess is that (1) the saturation
loading is close to the highest loading observed in the data, and (2... | 5921078cf6c566b4b4bf040941b3241ac519d431 | 410,662 |
def recall_pos_conf(conf):
"""compute recall of the positive class"""
TN, FP, FN, TP = conf
if (TN + FP) == 0:
return float('nan')
return TP/float(TP+FN) | 333f0aec1a6e84e0affa340c8bfcaa44d153230c | 574,820 |
def m3_hr2cfm(m3_hr):
"""m^3/hr -> cfm"""
return 0.58857778*m3_hr | f36cde87efb1c9a34477eee128450ea8d157add8 | 315,486 |
def get_geo_coordinates(tweet):
"""
Get the user's geo coordinates, if they are included in the payload
(otherwise return None)
Args:
tweet (Tweet or dict): A Tweet object or dictionary
Returns:
dict: dictionary with the keys "latitude" and "longitude"
or, if unavaiab... | a78af74152670cc29277a9b3c4f23dce4d778c7e | 110,100 |
def get_file_attachment(res_client, incident_id, artifact_id=None, task_id=None, attachment_id=None):
"""
call the Resilient REST API to get the attachment or artifact data
:param res_client: required for communication back to resilient
:param incident_id: required
:param artifact_id: optional
:... | 8dcf887f2773addbbf8a29791157f9be833ab9e9 | 646,803 |
import json
def parseEnvLog(envLog):
"""
Parse env log file
Parameters
----------
envLog : str
Env lof file path.
Returns
-------
sysInfo : dict
system information .
progList : dict
programs used.
"""
#parse the env log
with open(envLog) as f:... | 896e44939a9f4ec70538c7c556c93ae72f894911 | 218,462 |
def fake_task_sync(data):
"""Fake task for testing."""
return data | e51214646642999147ac4f5a9fba71447f63154f | 133,085 |
from typing import Any
from typing import Mapping
from typing import Counter
from typing import Iterable
def process_result(result) -> Any:
"""Make the result of a query JSON-serializable.
Parameters
----------
result :
The result of a query
Returns
-------
:
The processe... | 710e791b8aa802f0d147bf446a2db272a52033a4 | 169,541 |
def addCol(adjMatrix):
"""Adds a column to the end of adjMatrix and returns the index of the comlumn that was added"""
for j in range(len(adjMatrix)):
adjMatrix[j].append(0)
return len(adjMatrix[0])-1 | 29ee11953cbdb757e8ea80e897751059e42f1d90 | 6,572 |
from typing import Sequence
from typing import Dict
def _get_dict(l: Sequence[str], name_as_key: bool, starts_with: int = 1) -> Dict[str, str]:
"""
Converts a list into a dict, where keys/values are the list indices.
:param l: A list of categories
:param name_as_key: Whether to return the dict with c... | 6f8c682c99d332648443f0cfeea59e02f54162fc | 569,448 |
def tidy_source(code):
"""
Strips and fixes the indentation of python code.
eg.
| def foo():
| bar()
or
|def foo():
| bar()
becomes
|def foo():
| bar()
"""
raw_lines = code.splitlines()
lines = []
# Remove all empty lines
for line... | 48748013b8e2ccd5d118ba661807d3ff9e6f052e | 271,790 |
def _compare_first_n(series_1, series_2, n):
"""
Utility function that sees if the first n rows of a Pandas series are the same
"""
for i in range(n):
if series_1.iloc[i] != series_2.iloc[i]:
return False
return True | a06059cebd758a12953fc31eb4b06867ea3851b0 | 52,562 |
def check_intensifiers(text, INTENSIFIER_MAP):
"""
Utility function to check intensifiers of an emotion
:param text: text chunk with the emotion term
:return: boolean value and booster value for intensifiers
"""
# BOOSTER_MAP = {"B_INCR": 2,
# "B_DECR": 0.5}
intensity_word... | 2c35b6b66395bc7105b9fb1b9cf7f04b5686cb8d | 14,278 |
import re
def limit(pageurl):
"""Finds the current '?start=' limit from the page"""
b = re.findall('Τέλος.*start=([0-9]+)', pageurl)
slimit = int(b[0])
# print(type(slimit))
return slimit | 3fcaa343aed8e48820db79e14bd1bc9fcf75f2f7 | 568,399 |
def phase_fold(times, period):
"""
Folds the given light curve over its period to express the curve in terms
of phase rather than time.
Parameters
----------
times : numpy.ndarray
The light curve times.
period : numpy.float64
The light curve period.
Returns
-------
... | 949c71bab8d070e312c6a41b4d5393b0375de6d2 | 413,584 |
def widget_generator(klass, fields):
"""
Takes in a class and a list of tuples consisting of field_name and an attribute dictionary
:param klass: Class of the input widget
:param fields: List of tuples mapping field names to attribute dictionaries
:return: A dict of input widget instances
"""
... | 5e09950148f902ace17b6f52e06ee0f16760c195 | 687,610 |
import random
def generateDistribution(maxLength, totalCount, numDice, randomness):
"""
Returns a bell curve distribution.
maxLength controls how wide your x-axis will be.
totalCount is the total number of points you want to generate.
numDice is the number of random numbers you want to add togeth... | 0415a90ece99c22634f6abb2b652e93c410f9310 | 589,578 |
import math
def xy_intersect_of_polar_coords(rho1, theta1, rho2, theta2):
"""(x, y) coordinates of 2 lines defined by their parameters in polar coordinates
@param rho1: distance from origin to line 1
@param theta1: angle in radians between horizontal line and perpendicular to line 1
@param rho2: dist... | 144a0518878af5af2a37d1d4dfbdf8f0d39954d3 | 312,283 |
def encode_all(
header: bytes, stream: bytes, funcs: bytes, strings: bytes, lib_mode: bool
) -> bytes:
"""
Combine the various parts of the bytecode into a single byte string.
Parameters
----------
header: bytes
The bytecode's header data.
stream: bytes
The actual bytecode i... | 9325a01b6b485feb3c634b56df1da9b6c9182e04 | 578,947 |
def get_two_hospital_plot_labels(measurement_type):
"""A function to get necessary labels for the two hospitals plot"""
if measurement_type == "w":
title = "Waiting times of two hospitals over different distribution of patients"
y_axis_label = "Waiting Time"
else:
title = (
... | 1be47f2a320eb55c5cb1da40c56e6d3a6fc6615d | 166,183 |
def indented_setup(setup, indentation):
"""Indent each line of setup with given amount of indentation.
>>> indented_setup("x = 1\\n", " ")
' x = 1\\n'
>>> indented_setup("x = 1\\ny = 2\\n", " ")
' x = 1\\n y = 2\\n'
"""
return ''.join([indentation + line for line in setup.splitli... | 8ca289d78253cea6435399f0a44cdce3bd1e32a1 | 217,809 |
import requests
def lookup_mailinglists(prefix):
"""Find all mailing lists owned by `prefix`"""
req = requests.get("https://lists.srcf.net/getlists.cgi", params={'prefix': prefix})
req.raise_for_status()
assert req.headers['content-type'].split(';')[0] == 'text/plain'
return [listname for listname... | 293043fdead6affd667655a5a2135dd32c6f7893 | 217,735 |
from typing import List
def convertTo(data) -> List[dict]:
"""
Converst scores from new to old format
:param data: New format data
:return: Old format data
"""
return [
{
'Team': {
'Number': team['teamno'],
'DisplayName': team['teamname']
... | 68d4686e93bb2fb4b754643168f3b181d9ec9438 | 207,367 |
def jaccard(r_tokens: list, s_tokens: list) -> float:
"""Computes jaccard similarity.
JAC(r, s) = |r ∩ s| / |r ∪ s|
Parameters
----------
r_tokens : list
First token list.
s_tokens : list
Second token list.
Returns
-------
Jaccard similarity of r and s.
"""
... | 427bd308e153cf6781ada6ba45f4bdd0b8f73220 | 82,120 |
import torch
import math
def chebyshevLobatto(n: int):
"""
Compute the chebyshev lobatto points which
are in the range [-1.0, 1.0]
Args :
n : number of points
Returns :
A tensor of length n with x locations from
negative to positive including -1 and 1
[-1,...,+1]
... | 28ab75cad21812ed32e689b73ca535190de49e91 | 219,336 |
def filter_kwargs(kwargs, prefix):
"""Keep only kwargs starting with `prefix` and strip `prefix` from keys of kept items."""
return {k.replace(prefix, ''): v for k, v in kwargs.items() if k.startswith(prefix)} | 3f1475a2a655f37094383491745996027d1363bb | 532,270 |
def get_key(rule_tracker, value):
"""
Given an event index, its corresponding key from the dictionary is returned.
Parameters:
rule_tracker (dict): Key-value pairs specific to a rule where key is an activity, pair is an event index
value (int): Index of event in event log
Returns:
... | 1921e9a68d0df0867248ca83e2ba641101735fc7 | 708,421 |
def prefixify(d: dict, p: str):
"""Create dictionary with same values but with each key prefixed by `{p}_`."""
return {f'{p}_{k}': v for (k, v) in d.items()} | aaad6b11640df7c5e288bdd039b039ce373dcba1 | 657,513 |
def _validate_args(user_input_args: tuple[str, ...],
num_allowed: int,
*args: str) -> list:
"""
Ensures kwargs entered in a function call are acceptable.
Args:
input (dict): The kwarg(s) input into a function.
num_allowed (int): Limit, if any, on number... | 9e309329dd9f20799ed7c80c31af0cb5e046f3f0 | 142,212 |
from functools import reduce
from operator import mul
def prod(iterable):
"""Product of all values in an iterable, like sum() but for multiplication."""
# NOTE: in Python 3.8 this is now available as math.prod()
return reduce(mul, iterable, 1) | 51a7fd605d2df46b4c81f89defbc66e474833b82 | 346,232 |
def manhattan_distance(x, y):
""" Returns the Manhattan (City Block) distance between two lists
"""
return sum(abs(a - b) for a, b in zip(x, y)) | 4887024603a8fe3398ec80a17d1d70fbe15fdfab | 10,407 |
def get_resource_path(resource):
""" Return the full path for the resource (with or without a parent).
"""
return ("%s/%s" % (resource.parent and resource.parent.id or '',
resource.id.lstrip('/'))).lstrip('/') | 2ece218a78f93050d008f5729839e68a9c1a4a06 | 122,533 |
def clean_path(path):
""" remove index.html from end of a path, add / if not at beginning """
path = path.split("index.html")[0]
if not path.startswith("/"): path = "/" + path
if not path.endswith("/"): path += "/"
return path | 55360f0e0729b9cb0510308f5e5be32a404f1e70 | 48,930 |
def linear_search(item, my_list):
"""
Searching position by position
:param item: the number to look for
:param my_list: a list of integers
:return: either True or False if the item is in the list or not.
"""
found = False
for i in range(len(my_list)):
if item == my_list[i]:... | 463c23c85626be396c06f56d913fca9b5972fc0e | 19,110 |
import cgi
def check_api_v3_error(response, status_code):
"""
Make sure that ``response`` is a valid error response from
API v3.
- check http status code to match ``status_code``
:param response: a ``requests`` response
:param status_code: http status code to be checked
"""
assert n... | ca1be2ac3cff99b06fc0aaba73a075ff90024c9b | 675,264 |
import re
def Author_name_sep(value: str) -> list:
"""Splits the authors up into a list of different authors. checks on commas and 'and'.
:param value: string of author text
:return: list of authors
"""
x = re.split(", +| +and +", value)
return x | e0fca4494968e396b425606b25aa94cc0cc2085c | 155,261 |
def cagr(prices):
"""
Calculates the Compound Annual Growth Rate (CAGR) of a stock with given prices
"""
delta = (prices.index[-1] - prices.index[0]).days / 365.25
return ((prices[-1] / prices[0]) ** (1 / delta) - 1) * 100 | 49fec9f5eb5cca70c9d9cd52f386a4f312a0671a | 230,417 |
def _get_indentation(value: str) -> int:
"""Calculates the number of leading spaces in a string.
Args:
value: the string to check for leading spaces.
Returns:
An integer indicating the number of leading spaces in the string.
"""
return len(value) - len(value.lstrip()) | 9e7d4923a9fde230bc4ce3d0c91685d8d7cbf78a | 399,176 |
def unpad(string):
# type: (str) -> str
"""
Simple function that exctracts the initial string from a padded string
"""
return string[0 : -ord(string[-1])] | 375518cc656f8eaaf03e6626ad729a7f34ea71b3 | 255,225 |
def binary_search_iterative(array, target):
"""
Search target element in the given array by iterative binary search
- Worst-case space complexity: O(1)
- Worst-case performance: O(log n)
:param array: given array
:type array: list
:param target: target element to search
:type target: A... | 0cca6c4344e384b058408e935c663409b0f78b00 | 585,589 |
from functools import reduce
def ip_string_to_num(s):
"""Convert dotted IPv4 address to integer."""
return reduce(lambda a, b: a << 8 | b, map(int, s.split("."))) | 3dc5e9ac103149ff12bef43728421e3d30973281 | 603,712 |
def get_accept(environ):
"""get_accept(environ) -> accept header
Return the Accept header from the request, or */* if it is not present.
environ is the WSGI environment variable, from which the Accept header is read.
"""
if 'HTTP_ACCEPT' in environ:
return environ['HTTP_ACCEPT']
retu... | c58e94a185e475c48fa2f6dd58a0719297db730a | 109,509 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.