content stringlengths 39 14.9k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
def get_node_val(node, val_type):
"""Return the value as a string of a child node of the specified type,
or raise ValueError if none exists"""
for child in node.children:
if child.expr_name == val_type:
return child.text.strip('"')
raise ValueError("No value of specified type.") | d501dd7ba20620e844a5d4d2d33112e64a9dfef0 | 694,804 |
def _make_pretty_arguments(arguments):
"""
Makes the arguments description pretty and returns a formatted string if `arguments`
starts with the argument prefix. Otherwise, returns None.
Expected input:
Arguments:
* arg0 - ...
...
* arg0 - ...
...... | b6e7571c3d0e432649edf8295d3aa640140c551f | 694,806 |
import torch
def nested_stack(params, roll: bool = False):
"""Form a tensor from a nexted list of tensors.
This function is a generalization of torch.stack. For proper usage,
it's important that params is a nested list with shape consistent with
and array. The innermost elements of that nested list s... | 9b8b56eb15f55cbc5bf0b726e7aaf7fd3d476ada | 694,807 |
def count_features_type(features):
""" Counts three different types of features (float, integer, binary).
:param features: pandas.DataFrame
A dataset in a panda's data frame
:returns a tuple (binary, integer, float)
"""
counter={k.name: v for k, v in features.columns.to_series().groupby(... | 7c759ac9289e7f2cdb542d67ad30bf36e06749c8 | 694,808 |
import re
def default_cleaner_fn(fld):
""" Return a copy of the given field cleaned up by removing any unwanted characters. """
if (isinstance(fld, str)):
return re.sub("[\"\'\\\\]", "", fld) # remove quotes and backslashes
else:
return fld | 7f9bacfe981f1b3591cde5cc7ae04b654689b0c4 | 694,810 |
def get_pagination_parameters(request, paginator, paginated):
"""
Prepare and return the template parameters needed for pagination.
Thanks to https://gist.github.com/sbaechler/5636351
Args:
``request`` (django.http.HttpRequest): The request object.
``paginator`` (django.core.paginator... | 0dd54bffdf31a3cf78bfdc510ac20a901e3adc5e | 694,812 |
from typing import Tuple
def extend_shape(original_shape: Tuple, new_size: int, axis: int = 0) -> Tuple:
"""Extend a dimension of a shape tuple"""
shape = list(original_shape)
shape[axis] = new_size
return tuple(shape) | c48e2559900e88ec6c808735ac017c65bc82741a | 694,813 |
def _resource_for_help(pkg_info, help_file):
"""
Get the resource name that references the help file in the given help
package. The help file should be relative to the document root of the
package.
"""
return "Packages/%s/%s" % (pkg_info.doc_root, help_file) | 39214fe39db71935a85763e1731c0da97fe9e74b | 694,817 |
def str2bool(v):
"""
Convert a string to a boolean
:return boolean: Returns True if string is a true-type string.
"""
return v.lower() in ('true', 't', '1', 'yes', 'y') | 32dc16194fa6096e53e1a0b21f0287c31a7cd824 | 694,819 |
def adjustbufsize(
bufsize: int, bits: int) -> int:
"""Adjust buffer size to
account for bit depth
Args:
bufsize: initial estimate
of buffer size
bits : bit depth of bitmap
(1, 4, 8, 24) bits
Returns:
An adjusted int value of the
... | 9130c0fa174d9daf6dffabba67c77e9d85b29c13 | 694,821 |
def and_(a, b):
"""Same as a & b
"""
return a & b | a07f69143fd9eaa3b27bb07ee72c04efa31c5b7f | 694,823 |
import re
def generate_top_kmer_md_table(t_kmer_dic, g_kmer_dic,
top=5,
val_type="c"):
"""
Given k-mer count dictionaries for genomic and transcript context set,
generate a markdown table with top 5 k-mers (sorted by decending
dictionary va... | f0be470a6b2e10a5f786cd7d01c6b09b7f1e395e | 694,824 |
def schedule(year=None, month=None, day=None, week=None, day_of_week=None, hour=None, minute=None,
second=None, start_date=None, end_date=None, timezone=None):
"""Schedule a function to be executed according to a crontab-like schedule
The decorated function will be executed according to the schedu... | 051ed1fbdf113834bcff626b3679f1dc6ea5580c | 694,826 |
def get_default_ext(delim):
"""Retrieves the default extension for a delimiter"""
if delim == ',':
return "csv"
if delim == '\t':
return "tsv"
return "txt" | 37e27bebcb6c1fbfe3d794d3d2ac34d9909ed40c | 694,830 |
def to_camel(s):
"""Convert an underscored title into camel case. 'PARENT_ORGANISATION_ID' => 'parentOrganisationId'"""
bits = [(x.lower() if i == 0 else x.title())
for (i, x) in enumerate(s.split("_"))]
return "".join(bits) | 34400be6a346d886b2fca562b737b7811b871af1 | 694,831 |
import csv
import json
from typing import Counter
def convert_newsqa(file_path):
"""
Converts NewsQA dataset to jtr format.
Args:
file_path: path to the NewsQA CSV file (data/NewsQA/)
Returns: dictionary in jtr format
"""
# meta info
if '/' in file_path:
filename = file_p... | 0e48883e179f2d440ac8c72c8a5ff9344f595f1f | 694,835 |
def _zones_to_regions(zones):
"""
Return list of regions from the input list of zones
:param zones: List of zones. This is the output from `get_zones_in_project()`.
:return: List of regions available to the project
"""
regions = set()
for z in zones:
# Chop off the last 2 chars to tu... | 83c59cc6c2a9fc6e36a64044dc8ccc73ec039801 | 694,836 |
import re
def reformat_ISBNs(text: str, match_func) -> str:
"""Reformat ISBNs.
:param text: text containing ISBNs
:param match_func: function to reformat matched ISBNs
:type match_func: callable
:return: reformatted text
"""
isbnR = re.compile(r'(?<=ISBN )(?P<code>[\d\-]+[\dXx])')
tex... | a440d65e4bd747c28fb186e8b76389e8bb59526c | 694,843 |
def get_memory_usage(pandas_df):
"""
Returns the number of bytes used by a pandas dataframe
"""
return pandas_df.memory_usage(deep=True).sum() | 7ed31e0f20269224ea0517a71045992106df9030 | 694,848 |
def plot_evoked_topomaps(epochs, events, average_method, times):
"""
Plot evoked topomaps.
One figure is generated for each event.
Parameters
----------
epochs : mne.epochs.Epochs
Epochs extracted from a Raw instance.
events : list[str]
Events to include.
average_method... | 83e040930cc8971aeb045b4accaa315fcb53cc4f | 694,851 |
def CheckDoNotSubmitInDescription(input_api, output_api):
"""Checks that the user didn't add 'DO NOT ''SUBMIT' to the CL description.
"""
keyword = 'DO NOT ''SUBMIT'
if keyword in input_api.change.DescriptionText():
return [output_api.PresubmitError(
keyword + ' is present in the changelist descript... | c2ac7c263db43897933120fac475a5a2fc6774bc | 694,854 |
import math
import itertools
def permutate(k, l_exp):
"""
Generate the permutations for all exponents of y
:param k: number of meaningful directions
:param l_exp: expansion order
:return perms: array of permutations
"""
Nt = int(math.factorial(l_exp + k) / (math.factorial(l_exp) * math.f... | c0de0d2964299e2956ba4e04fe3ad5df9cafbc2d | 694,856 |
import math
def ebob(a,b):
"""
Returns the greatest common divisor of a and b.
"""
return math.gcd(a,b) | 43039a85b7a1e5065f7365c17c308a1c9d9df37d | 694,858 |
def convert_comma_separated_integer_to_float(comma_separated_number_string):
"""Converts a string of the form 'x,xxx,xxx' to its equivalent float value.
:param comma_separated_number_string: A string in comma-separated float form to be converted.
:returns: A float representing the comma-separated number.
... | daddeaa78a3efb8ffd2d5eac122757f041da5f97 | 694,860 |
from datetime import datetime
import json
import requests
def send_metrics(
uuid,
data,
solution_id,
url="https://metrics.awssolutionsbuilder.com/generic",
):
"""sends metric to aws-solutions
Args:
uuid (string): unique id to make metrics anonymous
data (dict): usage metrics f... | ab29d0c8830cb6ead501e84fa09ec9c0c28c9dcd | 694,861 |
def _encode_Bool_Flag(bBoolFlag : bool) -> int:
"""
Description
-----------
Method encoding a bool value into an integer flag.
Parameters
----------
`bBoolFlag` : bool
Boolean flag which should be converted into an integer value
Return
------
`iEncodedFlag` : int
... | 1df72f50b6b3e8206a09634386cd14f308f931f5 | 694,865 |
def dataToTuple(stringData):
"""
Formats string to tuple ("id:data" -> (id,data))
"""
splitLine = stringData.split(": ")
result = (splitLine[0],splitLine[1].strip("\n"))
return result | dce23300746935541822cd9e6924fca84bc702d4 | 694,867 |
def login(client, secret_id, rresp):
"""logging in as 'secret_id' with 'g-recaptcha-response'
client (class Flask.testing.FlaskClient): the client
secret_id (str): the secret_id to login
rresp (str): the recapctha response code. can be empty in testing
"""
return client.post('/auth',... | fdcf5a8646638fb14ec11a1a3e35c4f8aa71b96c | 694,868 |
def aggregate_count(keyname):
"""
Straightforward sum of the given keyname.
"""
def inner(docs):
return sum(doc[keyname] for doc in docs)
return keyname, inner | 431896d1c8d8c1f773b684b5235919ce1728a8ce | 694,869 |
def new_version_entry(version):
"""
Returns a new entry for the version index JSON schema.
"""
return {
"allOf": [
{"properties": {"version": {"oneOf": [{"const": version}]}}},
{
"$ref": "https://raw.githubusercontent.com/deepset-ai/haystack/master/json-sc... | 7198e859b1634ebfbf181a4ff0a9f488f29afd32 | 694,875 |
def fourier(framearray):
""" Fourier transforms all waves from array.
(Real values only)
:param framearray: array of waves (frames)
:return: array of FFT waves (spectrums)
"""
fourier_frame = []
for frame in framearray:
index = frame.make_spectrum()
fourier_frame.append(in... | 0f97ec9bd3f9ceda8d449a35623e9a2beea0ccda | 694,877 |
def get_media_stream(streams):
"""Returns the metadata for the media stream in an MXF file,
discarding data streams."""
found = None
for stream in streams:
# skip 'data' streams that provide info about related mxf files
if stream['codec_type'] == 'data':
continue
if f... | 563ef35c2be3e9787340d7527e7fd0f27b6db036 | 694,879 |
def validate_iyr(issue_year: str) -> bool:
"""iyr (Issue Year) - four digits; at least 2010 and at most 2020."""
return len(issue_year) == 4 and int(issue_year) >= 2010 and int(issue_year) <= 2020 | b30614523c1633ecf40a4f68556dcc820e1aefef | 694,882 |
def drawAsInfinite(requestContext, seriesList):
"""
Takes one metric or a wildcard seriesList.
If the value is zero, draw the line at 0. If the value is above zero, draw
the line at infinity. If the value is null or less than zero, do not draw
the line.
Useful for displaying on/off metrics, such as exit c... | 8cb8273d57abca57600071101f6bd9b4a595ea38 | 694,883 |
def parentChainContainsCycle(term):
"""
Check if the chain of parents from an item contains a cycle by navigating along the sequence of parents
:param term: The ontology term to check
:type term: pronto.Term
:return: Whether the parent chain contains a cycle
:rtype: bool
"""
seen = set()
toProcessed = [term]... | 162267808cfbafee1c1c4d07467d1b50eddcb58f | 694,885 |
def _make_dict(my_tup, lang):
"""Make tuple in a dictionary so can be jsonified into an object."""
labels = ['comment_text', 'course_code', 'learner_classification', 'offering_city',
'offering_fiscal_year', 'offering_quarter',
'overall_satisfaction', 'stars', 'magnitude', 'nanos']
results = {key: val for ... | b7f010b4486889fe938a7a69513b0d5ca6e6cb86 | 694,886 |
import typing
import re
def split_sentences(text:str) -> typing.List[str]:
"""Split multiple sentences in one string into a list. Each item being a sentence.
Args:
text (str): Incoming string with multiple sentences.
Returns:
typing.List[str]: list of sentences.
"""
SEP_REGEX = r... | b748311fb4b9c0b5249782ceb140a9f64b17fa15 | 694,893 |
def get_apim_nv_secret(client, resource_group_name, service_name, named_value_id):
"""Gets the secret of the NamedValue."""
return client.named_value.list_value(resource_group_name, service_name, named_value_id) | 186030bca5285a82e133c8fc3acbf617d7247eb5 | 694,894 |
def extract_condition_disease(condition):
"""Extracts the disease encoded in the Condition resource.
Example resource:
{
...
"code":{
"coding":[
{
"code":"Yellow Fever",
"system":"http://hl7.org/fhir/v3/ConditionCode"
}
]
}
...
}
Args:
cond... | a3adcf89c58c5a95790e8c9ea9089ccd6e8818af | 694,900 |
def get_train_na_percentages(train):
"""
Return a Series with the percentage of Na values per columns in train dataframe.
Must be called just after impute_train_missing_data().
Keyword arguments:
train -- the train dataframe
"""
na_cols_pctg_train = train[train.columns[train.isna().sum() > 0]].isna()... | 4de1d8f509a6a94a7ef73de3f17fb4d218791a7b | 694,903 |
import json
def parse_node_file(filename):
"""
Parses a node file and creates the following variables:
graph = {child:{None}, child:{Parent1, Parent2}, ...}
prior = {node: [prior values]}
lambdas = {parent:{child1:lambda1, child2:lambda2, leak_node:lambda0}}
Those can then be used with the s... | 736786825bf9a3f44bc641b4ccdc1d2b46fe9816 | 694,911 |
import socket
def isip(id):
"""
is the string an ipv4 address?
"""
try:
socket.inet_aton(id)
return True
except:
return False | 258b0f09f5471ea276ee136ebfc7833c3e54fd04 | 694,912 |
def parameters_property(parameters):
"""Given the list of parameters this function constructs a property that simply
returns the given list. It doesn't provide a setter so that the list of parameters
cannot be overridden."""
def getter(self):
return parameters
return property(fget=getter) | 65aa43e925d07ace01705234bb4130251c50adda | 694,913 |
async def get_tz_offset(client, user_id):
"""
Retrieve the (seconds as an integer) time zone offset from UTC for a user.
Outputs an integer in the range [-43200, 43200]. (-12h, +12h.)
client: the client from the context object.
user_id: the human-opaque Slack user identifier.
"""
# https://... | 02bcfd0cb21cc35eb331951e77bca41d8a58722c | 694,915 |
def feature_normalize(X, mean=None, sigma=None):
"""
returns a normalized version of X where
the mean value of each feature is 0 and the standard deviation
is 1. This is often a good preprocessing step to do when
working with learning algorithms.
the normalization is processed separately for ea... | 81342428799f5ac8d1815b54400e8eb4d7cc302b | 694,919 |
def periodic_ordering(amin, amax, bmin, bmax):
"""Figures out the order of the permutation that maps the minima and
maxima to their order, in canonical form (amin<amax, bmin<bmax if
possible).
Parameters
----------
amin : float
minimum of first range
amax : float
maximum of ... | ec81f998ea9abe2f23825f0de1890b6b2d62ba0e | 694,920 |
def strip_comments(string, markers):
"""
Complete the solution so that it strips all text that follows any of a set of comment markers passed in.
Any whitespace at the end of the line should also be stripped out.
:param string: a string input.
:param markers: list of characters.
:return: a new s... | 0ab980996897a7c42254a318ed325a9108033f97 | 694,921 |
def encode_schedule(schedule):
"""Encodes a schedule tuple into a string.
Args:
schedule: A tuple containing (interpolation, steps, pmfs), where
interpolation is a string specifying the interpolation strategy, steps
is an int array_like of shape [N] specifying the global steps, and pmfs is
an... | d660bc6826dcef2bbc5fbfca0eafaf2d72ab061f | 694,922 |
def ansi(num: int):
"""Return function that escapes text with ANSI color n."""
return lambda txt: f'\033[{num}m{txt}\033[0m' | 88297df114c3670a24e5ffa248d5756de09183cb | 694,923 |
from typing import Union
def compound_interest(
capital: float,
application_time: float,
fess: float,
*,
ret_dict=False,
ret_text=False,
) -> Union[tuple, dict, str]:
"""
Function to apply compound interest.
>>> from snakypy import helpers
>>> helpers.calcs.compoun... | a70b75c867fc1795e9f2491751b6a93e2bea92ec | 694,924 |
import torch
def _edge_error(y, y_target, mask):
"""
Helper method to compute edge errors.
Args:
y: Edge predictions (batch_size, num_nodes, num_nodes)
y_target: Edge targets (batch_size, num_nodes, num_nodes)
mask: Edges which are not counted in error computation (batch_size, num... | 900ce7bcf61a0f40cc9a36dd930a72c7d6f9cc51 | 694,926 |
import re
def get_url_jk_part(urlpart):
"""given a url substring containing the jk id from the job detail page - strip out the jk id and return it
string urlpart: a url substring containing the jk id
returns (str): the jk id param only e.g. jk=886c10571b6df72a which can be appended to a working job detail... | 97f1e5366f7ded9da84793fc39dbff965e3381d5 | 694,927 |
def unique_nonzero(ngb):
"""Return unique non-zero values from vector."""
uni = list()
for n in ngb:
if n > 0 and n not in uni:
uni.append(n)
return uni | 88ab425a2e8fa85a733fcd0887772219beae0403 | 694,931 |
def is_current_connection(context, connection):
"""Returns True if connection named name is the default connection"""
if context.pywbem_server_exists():
current_connection = context.pywbem_server
else:
current_connection = None
if current_connection and current_connection.name == connec... | 44cf36d4d389e9416ed469d76b63e46241c3eece | 694,937 |
def to_dero(value):
"""Convert number in smallest unit to number in dero"""
return value/10**12 | b4a48ca750ec9f40c826d5dec3eb6209b54ccf24 | 694,945 |
def get_absolute_import_name(dir_path: str, import_name: str) -> str:
"""Joins a relative import path with an import name."""
return f'{dir_path}:{import_name}' | 5cebf043df64b2f0160f5d7230c2a1eca94ce7a8 | 694,946 |
def install_start_end_marker(name: str, length: float) -> str:
"""Method to add start and end marker to the lattice.
Parameters
----------
name : str
lattice name
length : float
length of the lattice
Returns
-------
str
MADX install string.
"""
# define ... | 58dd4a789d4a12efdc454741ac8c9cafc40e22fe | 694,947 |
import torch
def remove_edge_cells(mask: torch.Tensor) -> torch.Tensor:
"""
Removes cells touching the border
:param mask: (B, X, Y, Z)
:return: mask (B, X, Y, Z)
"""
left = torch.unique(mask[:, 0, :, :])
right = torch.unique(mask[:, -1, :, :])
top = torch.unique(mask[:, :, 0, :])
... | c5370d9f8519ba4e0b76c3069f72601f3c0e90f4 | 694,952 |
def strategy(history, memory):
"""
Orannis's punitive detective:
Cooperate but when the other player defects, cooperate one more turn to
see if they defect again. If they do, defect for 10 turns.
Cooperate twice more and if they defect the second time, defect forever.
memory is a tu... | 6d3b4a1b7029a8eb43eac935531603cff7c865dc | 694,954 |
def main(*, left, right):
"""entrypoint function for this component
Usage example:
>>> main(left = pd.Series(
... {
... "2019-08-01T15:20:12": 1.2,
... "2019-08-01T15:44:12": None,
... "2019-08-03T16:20:15": 0.3,
... "2019-08-05T12:00:3... | 4656d789a5a705fee7d6e552a64542e22ab1e73e | 694,957 |
def _extract_jinja_frames(exc_tb) -> str:
"""
Extract all the frames in the traceback that look like jinja frames
Returns:
A multiline string with a formatted traceback of all the Jinja
synthetic frames or an empty string if none were found.
"""
lines = []
while exc_tb:
... | 170ede7e5fed4292c9e375555b6152ea4c8927bd | 694,960 |
import mmap
def get_lines(file_path):
"""
return an integer representing the number of lines
in the given file
:param file_path: Path to the given file
:return: The number of lines in a file
"""
with open(file_path, 'r+') as file:
line_count = 0
buffer = mma... | 9f135ded40890a62fd99a24ee72943d12b21f6e8 | 694,961 |
def load_data(filename):
"""Open a text file of numbers & turn contents into a list of integers."""
with open(filename) as f:
lines = f.read().strip().split('\n')
return [int(i) for i in lines] | 2baf679166eb1ee36f2b36c3e18f4f1d6a5272d9 | 694,964 |
import math
def _compute_page(offset: int, items_per_page: int) -> int:
"""Compute the current page number based on offset.
Args:
offset (int): The offset to use to compute the page.
items_per_page (int): Nimber of items per page.
Returns:
int: The page number.
"""
return... | 94a0a0c18b8090cf0a1a8ac3eacdc2bcff6643b6 | 694,967 |
def dim_returns(k, inverse_scale_factor):
"""
A simple utility calculation method.
Given k items in posession return the benefit of a K + 1th item given some
inverse scale factor.
The formula used is utility = 100% if no items are in posession or
utility = 1 / inverse_scale_factor * (k + 1)
... | 05d8b9cfe690737fc03b2d2fa9410bafae06bd2b | 694,968 |
def _filter_annotations(annotations, image, small_object_area_threshold,
foreground_class_of_interest_id):
"""Filters COCO annotations to visual wakewords annotations.
Args:
annotations: dicts with keys: {
u'objects': [{u'id', u'image_id', u'category_id', u'segmentation',
... | 3b0fb8d97b805808bc422cc68a6957102e494e69 | 694,970 |
import math
def _rotate(point, angle, origin = (0,0),unit = 'degree'):
"""Rotate a point counterclockwise by a given angle around a given origin.
Angle can be both in radian or degree. Helper function for rotating a layout.
Parameters
----------
point : tuple
position in (x,y) form
... | ec01d3fc7038b2846b1f33626fa1a33b2b23968f | 694,971 |
def exists(env):
""" Check if `cscope` command is present """
return env['CSCOPE'] if 'CSCOPE' in env else None | f8feae0af16efbcbef1cce57a622d72f4e92a7b0 | 694,972 |
def _splitNameByAbbreviations(parts):
"""
If a name contains abbreviations, then split so that first name is abbreviated parts and last name is parts that
are not abbreviated.
:param parts: Individual words in the name
:raises: ValueError if parts cannot be partitioned into given and family nam... | c3ca4655fc15801ad57f88629e8f5ad5e3c7f530 | 694,973 |
import torch
def log1p_exp(x):
"""
Computationally stable function for computing log(1+exp(x)).
"""
x_ = x * x.ge(0).to(torch.float32)
res = x_ + torch.log1p(torch.exp(-torch.abs(x)))
return res | e3a8cadd89a53af44254e1f28f61998125238dfc | 694,974 |
def ape(accounts):
"""Get account."""
return accounts[0] | 62edac0af9e3a45cdac6d179b3835698dcced587 | 694,975 |
def binary_search_iter(sorted_nums, target):
"""Binary search in sorted list by iteration.
Time complexity: O(logn).
Space complexity: O(1).
"""
# Edge case.
if len(sorted_nums) == 0:
return False
# Compare middle number and iteratively search left or right part.
left, right = ... | c0a5a02a6ef128dce44bcc6833bfbb281b5fb7c9 | 694,977 |
def get_uplink_downlink_count(duthost, tbinfo):
"""
Retrieves uplink and downlink count from DEVICE_NEIGHBOR_METADATA based on topology
Args:
duthost: DUT host object
tbinfo: information about the running testbed
Returns:
uplink count, downlink count
"""
config_facts ... | 73349db69fe403356b2a27b79f3cb0467235973a | 694,981 |
import socket
def is_valid_ipv6(ipv6_str):
"""Check if the input ipv6_str is an valid ipv6 address
Returns: True/False
"""
try:
socket.inet_pton(socket.AF_INET6, ipv6_str)
return True
except:
return False | 17116c17caa4909a792040ebea74e5dd7b1741ca | 694,986 |
def convert_seconds_to_str(sec: float):
"""Returns a str representing a number of seconds"""
msg = ""
sec = round(sec)
years = sec // 31536000
if years != 0:
msg += str(int(years)) + "y "
sec -= years * 31536000
days = sec // 86400
if days != 0:
msg += str(int(days)) + "d... | 6e575b4b5d9641436330600cda8d8b71be2fe9e0 | 694,988 |
def remove_contained(a):
"""
remove contained intervals
:param a: list of tuples (start, end, header)
:return: intervals with contained intervals removed
"""
o = []
a = sorted(a, key=lambda x: (x[0], -x[1]))
max_end = -1
for i in a:
if i[1] > max_end:
max_end = i... | 36c3473e6fbc5de2c788662e21f67ff89add5610 | 694,989 |
def unflatten_dict(d_flat):
"""Unflattens single-level-tuple-keyed dict into dict
"""
result = type(d_flat)()
for k_tuple, v in d_flat.items():
d_curr = result
for i, k in enumerate(k_tuple):
if i == len(k_tuple) - 1:
d_curr[k] = v
elif k not in d_... | 9beee326a40f323ea123028de39da594cd237385 | 694,990 |
def _is_string_same_case(input: str):
"""Returns flag indicating whether input string is a single case.
"""
return input == input.lower() or input == input.upper() | 93e8a41859cf8e6e6d871c787c18519c51ab5a4d | 694,991 |
def rank_freq(hist):
"""Returns a list of (rank, freq) tuples.
hist: map from word to frequency
returns: list of (rank, freq) tuples
"""
# sort the list of frequencies in decreasing order
freqs = list(hist.values())
freqs.sort(reverse=True)
# enumerate the ranks and frequencies
r... | fc7731190ce44464526d9eda40cda235e07d0985 | 694,993 |
def get_subs(relativize_fn, links):
""" Return a list of substitution pairs, where the first item is the
original string (link) and the second item is the string to replace it
(relativized link). Duplicate subs are filtered out."""
subs = ((l, relativize_fn(l)) for l in links)
subs = filter(lambda p: p[0] != p[1],... | f22b7fd13cf95ac2860c42d0f72b3fed4977d6cb | 694,996 |
from typing import List
from typing import Dict
from typing import Any
def list_dict_swap(v: List[Dict[Any, Any]]) -> Dict[Any, List[Any]]:
"""Convert list of dicts to a dict of lists.
>>> list_dict_swap([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]) == {'a': [1, 3], 'b': [2, 4]}
"""
return {k: [dic[k] for di... | 7953a19dfaa326297e666b0f378540c90b079989 | 695,004 |
def _parse_metal_choice(s, max_value):
"""
Parse user options provided in ``detect_metal_options``.
The syntax is <position>,[<new name>], using semicolons to choose several ones.
<new name> can only be 3-letters max and should not collide with existing Amber
types. This is not checked, so be caref... | 0ad06942f782e5362dc8c9f530194320cbbb3a28 | 695,005 |
def invert_graph(graph):
"""
Inverts a directed graph.
:param graph: the graph, represented as a dictionary of sets
:type graph: dict(set)
:return: the inverted graph
:rtype: dict(set)
"""
inverted = {}
for key in graph:
for value in graph[key]:
inverted.setdefau... | 943337d278107d520dde6a724c89016cd1452bee | 695,006 |
def qobjects(cls_target, oid, foreign_name):
"""
Get queryset objects based on `cls_target` given the object/id `oid` and
the `foreign_name` is equal.
"""
if isinstance(oid, int):
fetcher = 'id'
else:
fetcher = 'in'
kwargs = {'{0}__{1}'.format(foreign_name, fetcher): oid}
... | dc5c46e0aee6b65e41add750a40c9d10afd88235 | 695,019 |
def is_classic_netcdf(file_buffer):
"""
Returns True if the contents of the byte array matches the magic number in
netCDF files
:param str file_buffer: Byte-array of the first 4 bytes of a file
"""
# CDF.
if file_buffer == b"\x43\x44\x46\x01":
return True
return False | dbb55b582526ad90132f4ef6d9485907e621ceed | 695,020 |
def trim_version(version, num_parts=2):
"""
Return just the first <num_parts> of <version>, split by periods. For
example, trim_version("1.2.3", 2) will return "1.2".
"""
if type(version) is not str:
raise TypeError("Version should be a string")
if num_parts < 1:
raise ValueErro... | 5f23485c914a695bfb652fbc8cccb738147a8e7b | 695,025 |
def is_vowel_sign_offset(c_offset):
"""
Is the offset a vowel sign (maatraa)
"""
return (c_offset >= 0x3e and c_offset <= 0x4c) | eaee5757d80e8775c802bc0136d34733dc955610 | 695,038 |
def custom404handler(err):
"""Custom handler for 404 errors."""
return dict(err=err) | b1771a98d95903214e96bd6dfa2055c62c783af1 | 695,039 |
import math
def degrees(rad_angle) :
"""Converts any angle in radians to degrees.
If the input is None, the it returns None.
For numerical input, the output is mapped to [-180,180]
"""
if rad_angle is None :
return None
angle = rad_angle * 180 / math.pi
while angle > 180 :
... | e889ff09cf0bccc36beabc3451a9b64730f8e9e6 | 695,041 |
import re
def remove_log_text(text):
""" Remove log information from the given string. """
# The pattern basically searches for anything following ".cpp:[0-9]{1-4}] "
pattern = "(?:(?<=\.cpp:[0-9]] )|(?<=\.cpp:[0-9][0-9]] )|(?<=\.cpp:[0-9][0-9][0-9]] )|(?<=\.cpp:[0-9][0-9][0-9][0-9]] )).+"
m... | 8ec14757aae16d1de7c2651faea0f927bb324593 | 695,042 |
import io
def make_pdf_from_image_array(image_list):
"""Make a pdf given an array of Image files
:param image_list: List of images
:type image_list: list
:return: pdf_data
:type pdf_data: PDF as bytes
"""
with io.BytesIO() as output:
image_list[0].save(
output,
... | 333c3e3475e345eeee7bb3ec17291db9abf990b2 | 695,043 |
def get_primary_label(topic, primary_labels):
"""Function that returns the primary (preferred) label for a topic. If this topic belongs to
a cluster.
Args:
topic (string): Topic to analyse.
primary_labels (dictionary): It contains the primary labels of all the topics belonging to clusters.
... | 58bda0113e6199e609d6160cf6c689d6f9c9f95e | 695,046 |
def find_or_create_location(location, dbh):
"""Find or create the location"""
cur = dbh.cursor()
cur.execute('select location_id from location where name = %s', (location, ))
res = cur.fetchone()
location_id = 0
if res is None:
print(f'Loading location "{location}"')
cur.execut... | 363c41a94cdacb5a4c241483917c8523159f574f | 695,047 |
def indent(string):
""" indent string
>>> indent('abc\\ndef\\n')
'\\tabc\\n\\tdef\\n'
"""
return '\n'.join(
'\t' + line if line else ''
for line in string.split('\n')
) | e1e88eda9ae0e9ad11a9eb68c8b51302bbbc5ab6 | 695,049 |
def scrabble(wort):
"""
Berechnet den Scrabble-Wert zu einem deutschen Wort
:param wort: Das Wort dessen Wert berechnet wird (muss klein geschrieben sein)
:return: den zugehörigen Scrabble-Wert
"""
werte = {"a": 1, "b": 3, "c": 4, "d": 1, "e": 1, "f": 4, "g": 2,"h": 2, "i": 1, "j": 6,
... | 1865dea59c3ff8f391975d4ee59e5c501c8d3781 | 695,050 |
def _days_before_year(year):
"""year -> number of days before January 1st of year."""
y = year - 1
return y * 365 + y // 4 - y // 100 + y // 400 | 5070c747c0e28235e51dadd107de77cedfca2886 | 695,051 |
import math
def point_distance(p1, p2, squared=False):
""" Calculate the squared eucliden distance of two points.
Parameters
----------
p1 : associative array $p1 first point
p2 : associative array $p2 second point
Returns
-------
float
Examples
--------
>>> point_distan... | 28e13856380783eab469a10b01ebd2769422ff1b | 695,052 |
def path_starts_with(path, prefix):
"""Test whether the path starts with another path.
>>> path_starts_with([1], [1])
True
>>> path_starts_with([1, 2], [1])
True
>>> path_starts_with([2], [1])
False
>>> path_starts_with([1,2,3], [1,2,3])
True
>>> path_starts_with([1,2,3], [1,2])... | dd2fae5ff605ab5b0c9ff49aaa846fb287e58270 | 695,057 |
def to_string(quantity_or_item: str) -> str:
""" Returns the same string. """
return quantity_or_item | e1828ed0ddd6ea3f2db1140a2da2a94965df84b7 | 695,058 |
import requests
def tmdb_data_for_id(tmdb_id: int, tmdb_api_token: str) -> dict:
"""
Get additional information for a movie for which you already have the ID
Args:
tmdb_id (int): the ID for a movie on The Movie Database
tmdb_api_token (str): your tmdb v3 api token
Returns:
di... | dbac37d7969e85138de18a24f1caf963cc7d44da | 695,069 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.