content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
from typing import Union
from typing import Any
from typing import Tuple
from typing import Callable
import attrs
def iter_validator(iter_type, item_types: Union[Any, Tuple[Any]]) -> Callable:
"""Helper function to generate iterable validators that will reduce the amount of
boilerplate code.
Parameters
... | 58add73ae65e5cf41ec151c0172810958b1b117d | 103,099 |
def check_results(results, expected):
"""
Compare the two MoonLight results dictionaries: one from
moonlight_solution.json and the other from the execpted result.
"""
# Mapping of result keys to their data types
keys_to_compare = dict(corpus_size=int,
solution_size=int... | cc880c646b19f0e9394ff58c15daa808150f2a09 | 579,562 |
from typing import List
import pathlib
def filter_file_existence(c: List[str], files: List[pathlib.Path]) -> List[str]:
"""Filter for files that are not found
:param c: list of files to find
:type c: List[str]
:param files: list of file paths
:type files: List[pathlib.Path]
:return: list of f... | 50e7b8b8986bfa50d5c66367c1575f6b29f13ddf | 298,547 |
def getRow(data, rowIndex):
"""
Returns the row indicated by rowIndex from data which is assumed to be an array.
Assuming that rowIndex will be a value from 0 to length of data - 1
"""
return data[rowIndex] | 678fcc082a4ba494f0c2e5597cc6aded6441eb7f | 243,421 |
def count_stair_ways(n):
"""Count the numbers of ways to walk up a flight of stairs with 'n' steps while taking a maximum of 2 steps at a time
>>> count_stair_ways(2)
2
>>> count_stair_ways(3)
3
>>> count_stair_ways(4)
5
>>> count_stair_ways(5)
8
"""
"""BEGIN PROBLEM 3.1"""
... | de6a72eb722df431b77f95dffb7f48c3cefc5a18 | 120,839 |
def adjacency(indices, length):
"""Given a list of indices and length of the output list,
Return a list of 1s for adjacencies and 0s for non-adjacencies."""
return [ int(i in indices) for i in range(length) ] | a0150033dcbbd2e60adf6755c5a83c2985b6cb3e | 480,950 |
def split_and_check(s, separator, n):
""" Turn string into tuple, checking that there are exactly as many parts as expected.
"""
parts = s.split(separator)
if len(parts) != n:
raise ValueError('Failed to parse "{}"'.format(s))
return tuple(parts) | de2fb2714d3c056a91bed41f6301532d0b89a617 | 497,399 |
from datetime import datetime
def is_good_time(time: list):
"""
Возвращает совпадает ли текущее время с временем для публикации
:param time: list с временем публикаций
:return: bool
"""
now = datetime.now().strftime('%H:%M')
if now in time:
return True
else:
return Fal... | 45f966a9a54ea36717e22a83157880df91def39c | 380,066 |
from typing import Counter
from functools import reduce
def create_maps(words, tags, min_word_freq=5, min_char_freq=1):
"""
Creates word, char, tag maps.
:param words: word sequences
:param tags: tag sequences
:param min_word_freq: words that occur fewer times than this threshold are binned as <u... | f873ebc6447eb2024f3abac4aec0a0feccd08470 | 67,097 |
def _build_batch_norm_params(batch_norm, is_training):
"""Build a dictionary of batch_norm params from config.
Args:
batch_norm: hyperparams_pb2.ConvHyperparams.batch_norm proto.
is_training: Whether the models is in training mode.
Returns:
A dictionary containing batch_norm parameters.
"""
batc... | 862db398b427bc4ab17c976db2d4eaf4c1f2e133 | 479,080 |
def colWidth(collection, columnNum):
"""Compute the required width of a column in a collection of row-tuples."""
MIN_PADDING = 5
return MIN_PADDING + max((len(row[columnNum]) for row in collection)) | 9a300106cf57fa6a78af37caa3f6b2a74c3e5b2c | 50,155 |
import sympy
def variable (name):
"""
A convenient frontend to sympy.symbols, except that it escapes commas, so that the single
variable name can contain a comma. E.g. 'Y[0,1]'.
"""
return sympy.symbols(name.replace(',','\\,')) | 2194c1f674f9510148ce84d50b4d00dd0017c3a3 | 482,420 |
def echo(data):
"""
Just return data back to the client.
"""
return data | 80655150d1578c12b2f196b664df8935bae569f1 | 41,297 |
from typing import Union
from typing import List
from typing import Any
def has_multiple_references(summaries: Union[List[List[str]], List[List[List[str]]]]) -> bool:
"""
Checks to see if ``summaries`` has multiple references or not by examining
the type of the summaries. Each individual summary is repres... | 4dc42acda8781681debb951d26f3811623033060 | 317,046 |
def calculate_objective(sol, D):
""" The objective is the total cost of all routes in VRP solution. D is
the full distance matrix of the points in the problem (also includes the
depot with index of 0), and solution is a list containing giant tour
encoded VRP solution, where 0 indicates a visit to the d... | 4daff795533db06f976fdd4490bad96abd35b02e | 253,583 |
def extractNonRunSimple(q, factoryConfig=None):
"""All NOT Running Glideins: JobStatus != 2
q: dictionary of Glideins from condor_q
factoryConfig (FactoryConfig): Factory configuartion (NOT USED, for interface)
Returns:
dict: dictionary of Not Running Glideins from condor_q
"""
... | e6e14593f3aede06a00e2c81f568c600dc7899e7 | 414,014 |
from typing import List
from typing import Optional
def resolve_is_aggregate(values: List[Optional[bool]]) -> Optional[bool]:
"""
Resolves the is_aggregate flag for an expression that contains multiple terms. This works like a voter system,
each term votes True or False or abstains with None.
:param... | 7fa150dcfc0fbcf8ebf507d9812aba5132b5637b | 516,193 |
def get_indentation(line):
"""
Returns the leading spaces and/or tabs of a line of text.
Preconditions: None
@type line: str
@rtype: str
"""
ptr = 0
while ptr < len(line):
if line[ptr] != ' ' and line[ptr] != '\t':
break
ptr += 1
return line[0:ptr] | f193302b7edc6b3a10e6136cd3b8f7ab7a04d7dc | 164,292 |
import json
def dumps(resource):
"""Serialize to string
Parameters
----------
resource : `pyannote.core` data structure
Returns
-------
serialization : string
"""
return json.dumps(resource, encoding='utf-8', for_json=True) | 43d216c2d35aa8881a61416ae5fb4737bddce15e | 552,438 |
def _parse_line(s):
"""Parses a line of a requirements.txt file."""
requirement, *_ = s.split("#")
return requirement.strip() | 5c0c96898c288a7c358bf978a4415c17c7fb19c4 | 29,081 |
def calculateNetIncome(gross, state):
"""
Calculate the net income after federal and state tax
:param gross: Gross Income
:param state: State Name
:return: Net Income
"""
state_tax = {'LA': 10, 'SA': 0, 'NY': 9}
# Calculate net income after federal tax
net = gross - (gross * .10)
... | 915528d8bfd15c18003eaeb8f6b3f1e8ad5565a0 | 699,110 |
def format_content(article):
"""
This function takes an Intercom article and formats its content
as the HTML we'll use for the Guru card. Because Intercom has some
extra fields on its articles, like the # of views, conversations, and
counts of user reactions and we don't have these fields in Guru, we
displa... | 78274dca56bc1657c0af5ddee1c6967bc5c233a5 | 641,182 |
def main_heading() -> str:
"""Get index page heading page."""
return 'Video Archive' | 89057fd3ac0d510cfde43ce8b61cacd4f9f76340 | 137,679 |
def bfs(graph, start):
"""
Breadth first search
Args:
graph (dict): graph
start (node): some key of the graph
Time: O(|V| + |E|)
"""
seen = set()
path = []
queue = [start]
while queue:
current = queue.pop(0)
if current not in seen:
seen.ad... | a743ee4505eee2b378c141789429aea5944589c2 | 123,583 |
def proposed_scaling_both(current, desired):
"""
identify a single scalar to scale the image by so that
it is closer to the desired scaling (fills the space as much as possible)
Arguments
---------
current : tuple
float tuple of current size of svg object
desired : tuple
fl... | a3bb42c10dfcfcbfc01e74a42198b7b14238453a | 613,626 |
import unicodedata
def normalize_text(text):
""" Normaliza um texto para otimizar as buscas.
- Remove os acentos.
- Remove os traços.
- Remove os espaços duplos.
- Remove os espaços das extremidades.
- Altera o texto para minúsculo.
Exemplo:
>>> print(normalize_text('São Paul... | 8543ab4d21e9703cede55be9ab1a2a0dd2ded398 | 418,673 |
import copy
def merge_left_with_defaults(defaults, loaded_config):
"""
Merge two configurations, with one of them overriding the other.
Args:
defaults (dict): A configuration of defaults
loaded_config (dict): A configuration, as loaded from disk.
Returns (dict):
A merged confi... | 630db012b0705e7c0ab12c9cd4e7df07b3ab56f0 | 667,842 |
def hours2days(input_hours):
""" Receive the hours and will return ow many days and hours there is in the input """
# Floor division to get the days but not the extra hours
days = input_hours // 24
# Modulus to get the left hours after calculating the days
hours = input_hours % 24
# Returns tupl... | fcd04352aa990154d55bd8c3352c717d06ad50f0 | 686,636 |
def map_raw_ids_to_sequential_ids(samples):
"""
For each unique user or item id, this function creates a mapping to a sequence of number starting in 0.
This will be the index of the embeddings in the model.
Items ids will be from 0 to n_items - 1.
Users ids will be from n_items to n_items + n_users... | f32baeaa596ce24f99280fe37b6568d1386b7653 | 255,706 |
def detective_rate(statistic, threshold):
"""
function to calculate detective rate
parameters
----------
statistic:
statistics of testing data
threshold:
threshold by the offline data
return
------
fault detective rate or false alarm
"""
... | e3401f628b0b48fe105a690ffe2f5167dafe46c4 | 486,810 |
from textwrap import dedent
def package_load_instructions(inst_distributions):
"""Load instructions, displayed in the package notes"""
per_package_inst = ''
for dist in inst_distributions:
if dist.type == 'zip':
per_package_inst += dedent(
"""
# Loadi... | 321a7486f27a3cb327ae7556e317bc53c24726ac | 4,529 |
def solution(A): # O(N)
"""
Given an array A, multiply each of the values by all of the other
values in the array except itself.
>>> solution([1, 2, 3, 4, 5])
[120, 60, 40, 30, 24]
"""
length = len(A) ... | cc5f8ff7193d22a7de0fb2a5a56608e4cce0d013 | 257,526 |
def _numpy_array_ufunc(ufunc, method):
"""Trival wrapper for numpy.ufunc"""
return getattr(ufunc, method) | 1f55f725bb159c903dd386597f7ebcdce5f273be | 392,110 |
def _get_file_contents(path):
"""
Gets the contents of a specified file, ensuring that the file is properly
closed when the function exits
"""
with open(path, "r") as f:
return f.read() | cfe84e52e2ac48d3f7d9d20fd1c85c71a222ef95 | 8,754 |
from typing import List
def list_types() -> List[str]:
"""
Returns supported item types.
"""
return ['string', 'boolean', 'integer', 'number', 'file', 'object', 'array'] | 43f682546c3815316057ef23a720bf21246bc367 | 197,249 |
def _normalize_mac(mac):
"""Convert MAC to a well-known format aa:bb:cc:dd:ee:ff."""
if '-' in mac:
# pxelinux format is 01-aa-bb-cc-dd-ee-ff
mac = mac.split('-', 1)[1]
mac = mac.replace('-', ':')
return mac.lower() | c7ec28025001c03b3ba65bfe54750165a1974bd5 | 390,331 |
def df_binary_columns_list(df):
""" Returns a list of binary columns (unique values are either 0 or 1)"""
binary_cols = [col for col in df if
df[col].dropna().value_counts().index.isin([0,1]).all()]
return binary_cols | 9a2b5313452fac09bb671fe2b12b9f40203fb446 | 78,570 |
def diff_nomod(ctx):
"""Get the unmodded difficulty values of a map."""
return {
"cs": ctx.beatmap.diff_size,
"ar": ctx.beatmap.diff_approach,
"od": ctx.beatmap.diff_overall,
"hp": ctx.beatmap.diff_drain,
"sr": ctx.beatmap.difficultyrating,
"bpm": ctx.beatmap.bpm,... | 4e025ed19b6deeb1a88d41d1d940940a034c4430 | 567,389 |
def count_from_1(index, collection):
"""Numbering function: consecutive integers starting at 1."""
return index + 1 | 46e58fb0d47a1c71153766620f672c850aabd3ba | 253,720 |
def te_exp_minus1(posy, nterm):
"""Taylor expansion of e^{posy} - 1
Arguments
---------
posy : gpkit.Posynomial
Variable or expression to exponentiate
nterm : int
Number of non-constant terms in resulting Taylor expansion
Returns
-------
gpkit.Posynomial
Taylor ... | 9818ee831c9b6b51d9142d0c463099d595b11afc | 150,402 |
import json
import csv
from bs4 import BeautifulSoup
def read_file(path, mode="default"):
"""Open a file and return its content (parsed if possible)
:param path: (abs) path to the file
:param mode: "default|json"
:type path: str
:type mode: str
:return: content of the file
:rtype: str or d... | 8b92de37f21a3141cddbe79641b0a7c5117b7739 | 308,286 |
def get_line(merged_vcf):
"""Build a list of relevant data to be printed.
Args:
merged_vcf (dict): an object from project merge VCF response
Returns:
str: tab delimited list of relevant data to be printed
"""
return "\t".join(
[
str(merged_vcf.id),
m... | b684b6bffc3bf6b603c2fbcee9abfc0cdbbe4943 | 573,009 |
def pprint(l):
"""Pretty print
Parameters:
l (list/float/None): object to print
Returns:
pretty print str
"""
if isinstance(l, list):
if len(l) == 0: return None
else:
if l == None:
return None
else:
return '%.2f' % l | d2b10630ece9f5ac3c1fad745435fc043561ce8c | 405,178 |
def vec(X, order='F'):
"""Returns the vectorization of X. Columns of X are stacked. (The opposite of X.flatten())."""
assert X.ndim == 2, 'vec operator requires a matrix.'
return X.flatten(order=order) | 591e5b3f0c9fb3139551ab17025bbf4a941e2984 | 348,793 |
def to_dict(sequences, key_function=None) :
"""Turns a sequence iterator or list into a dictionary.
sequences - An iterator that returns SeqRecord objects,
or simply a list of SeqRecord objects.
key_function - Optional function which when given a SeqRecord
returns a uni... | c9d728cca2748791562a6c2bc657fc3296ced5c9 | 395,383 |
def has_children(elem):
"""Check if elem has any child elements."""
if type(elem) == list:
return True
try:
for child in elem:
if type(child) == list:
return True
except TypeError:
return False
return False | 0ed1f2743231a0df45fb6634bd58664e2115154f | 239,700 |
def ip_range(network):
"""Return tuple of low, high IP address for given network"""
num_addresses = network.num_addresses
if num_addresses == 1:
host = network[0]
return host, host
elif num_addresses == 2:
return network[0], network[-1]
else:
return network[1], networ... | b0c4f17e6a8646a92e7141828efc7e364b5a547d | 673,351 |
def sorted_completions(completions):
"""Sort completions case insensitively."""
return list(sorted(completions, key=lambda x: x[0].lower())) | a22fb6bdde0e8eef15ab5baba3d87c12e7906c1c | 290,529 |
def relu_p(z):
"""
Computes the gradient of the relu for a given numpy array.
"""
result = z
result[result <= 0] = 0
result[result > 0] = 1
return result | f7ec1111e2079131616261a16d55310254c42bc8 | 422,569 |
import ipaddress
def _calculate_address_pool(addrmask, ipv6=False):
"""
Return a pool of addresses contained in the network.
@param addrmask: Network in IP/mask format.
@return: A list of ip addresses
"""
if ipv6:
netobj = ipaddress.IPv6Network(addrmask, strict=False)
else:
... | 3fb1b60324e47e38c2f74caf9f47d02dcb30cb9f | 638,646 |
def get_extension(t_path):
""" Get extension of the file
:param t_path: path or name of the file
:return: string with extension of the file or empty string if we failed
to get it
"""
path_parts = str.split(t_path, '.')
extension = path_parts[-1:][0]
extension = extension.lower()
re... | 70fa800714c1fbd71f3071ed4832a1cd96f1949f | 45,708 |
def line_list(filename):
"""(Internal) Returns a list of tuples (indentation-level, line) one for every line
Empty lines, comments and extra whitespace are striped out.
Indents are expected to be 4 spaces.
:param filename: Path of the asl file name (may be relative or absolute).
:type filename: st... | ac1c4accca6042916aff28dfb299b55c169652d2 | 443,666 |
def matches(item, *, plugins=None, subregions=None):
"""
Checks whether the item matches zero or more constraints.
``plugins`` should be a tuple of plugin classes or ``None`` if the type
shouldn't be checked.
``subregions`` should be set of allowed ``subregion`` attribute values or
``None`` if... | bed1c1515489799f8695bfa50aedab2888f67407 | 579,482 |
from typing import Counter
def bag_jaccard(x, y):
"""
Jaccard similarity measure between two multisets (bags)
:param x: list of words (strings) for the first sentence
:param y: list of words (strings) for the second sentence
:return: similarity score between two sentences
"""
if len(x) == ... | 9f946512afc97c3a5cc9c466d9bf2ed61208f7b7 | 338,943 |
import logging
def run_flushdb(redis_con):
"""
Purpose:
Run Redis FlushDB Command. Will clear all
keys and values from the Redis Database (good
for clearing cache when redis is used as
a caching solution)
Args:
redis_con (Redis StrictRedi... | 7e65b420a827a2204d0f40cb8534ce35c1427ce0 | 81,704 |
def text_remove_empty_lines(text):
"""
Whitespace normalization:
- Strip empty lines
- Strip trailing whitespace
"""
lines = [ line.rstrip() for line in text.splitlines() if line.strip() ]
return "\n".join(lines) | eee3aee2e7f56e8567d6b87c9d11cb4d63a7a6e5 | 508,554 |
def get_exponent(number, base):
"""If number = base**k, returns k. Else returns None
"""
if number <= 1: return 0
k = 0
if base > 1:
while number % base == 0:
number /= base
k += 1
return k if number == 1 else None | 5628fa0b88b0a8b93ac3016744e2775701a30bfa | 238,398 |
import re
def snake(s):
""" snake cases ``s``
:param str s:
:return: str
"""
# insert a space before each uppercase character preceded by a
# non-uppercase letter
s = re.sub(r'(?<=[^A-Z])\B([A-Z])', r' \1', s)
# lowercase everything, split on whitespace and join
return '_'.join(s.... | 077dae87deda6cc00d055a2fa1829c19caa8a27d | 377,330 |
def calc_control(num):
"""
Рассчитывает контрольное число кода EAN-13.
Цифры набора нумеруются справа налево.
Подсчитываются суммы цифр, стоящих на четных и нечетных местах.
Сумма цифр, стоящих на четных местах, суммируется с утроенной суммой цифр, стоящих на нечетных местах.
Если цифра единиц ... | c131058deca2e1f5fe5d50be7b751a7b42b3d467 | 335,966 |
from typing import Counter
from functools import reduce
def merge_raw_vocabs(*raw_vocabs: Counter) -> Counter:
"""
Merges multiple raw vocabularies into a single one.
:param raw_vocabs: Raw vocabularies.
:return: Merged raw vocabulary.
"""
raw_vocab = reduce(lambda c1, c2: c1 + c2, raw_vocabs... | a80ed338830a16004a7281e553d69b189564cce9 | 243,181 |
import re
def is_valid_ttl(ttl):
"""Check that ttl value is valid (ie. positive signed 32 bit number)"""
if len(ttl) == 0:
return False
match = re.search(r"[^0-9]", ttl)
if match is not None:
return False
value = int(ttl)
if not (0 <= value < 2 ** 31):
return False
... | 267a2ead566b9dfe5a7e881995a592257b103e43 | 594,080 |
from typing import List
def generate_gmt_file(output_file: str, gene_sets: List) -> str:
"""
Generate a GSEA gmt file
:param output_file: name of output file
:param gene_sets: list of gene sets; each entry is (gene_set_name, gene_set_origin, list of gene symbols)
:return:
"""
with open(out... | 9c3c343d589d0ae798806da63e67d1fa9576c3e1 | 124,624 |
def read_contents(file_path):
"""Reads the contents of file_path without decoding."""
with open(file_path) as fin:
return fin.read() | 4331371c012420a176fc5a6f191afce325058004 | 267,138 |
import re
def extract_md5_from_text(text):
"""
extract base-128 hash from string
Args:
text(str): string output from gsutil command
Returns:
hash(str) or None
"""
m = re.search("[0-9,a-f]{32}", text.lower())
if m:
return m.group(0)
else:
return None | 9104d78fc16a25b6a0dc1ed0adb1457c8b56e05f | 599,469 |
def is_float4x4(items):
"""Verify that the sequence contains 4 sequences of each 4 :obj:`float`.
Parameters
----------
items : iterable
The sequence of items.
Returns
-------
bool
"""
return (
len(items) == 4 and
all(
len(item) == 4 and
... | b01feb2f728aa693922b4743d158194234681e67 | 577,325 |
def create_set_state_payload(context: str, state: int):
"""Create and return "setState" dictionary to send to the Plugin Manager.
Args:
context (str): An opaque value identifying the instance's action you want to modify.
state (int): A 0-based integer value representing the state of an action w... | 3bff49a05b66d7fc4b0615c0e4c8659a8666d364 | 583,685 |
def from_date(val):
"""escape a python datetime.date"""
return val.strftime("'%Y-%m-%d'") | 2f7ca7ac6966c6249836d76e9d94b57da35f7a90 | 405,788 |
import glob
def _get_header_files(include_dirs):
"""Return a list of all extra headers to include in checks."""
headers = []
for path in include_dirs:
headers += glob.glob(path + "/**/*.h", recursive=True)
headers += glob.glob(path + "/**/*.hh", recursive=True)
headers += glob.glo... | 981652963a8bbf71beef10a31a24d02fafd71695 | 637,874 |
def areaRectangulo(base, altura):
"""Function that finds the area of a rectangle given its width and height
Args:
base (float): the value for the width of the rectangle
altura (float): the value for the height of the rectangle
Returns:
float: The area of the rectangle
"""
r... | c013f86cac04ee405a5d6cf06184a19ffbf7f254 | 62,583 |
def tuple_incr(t1, idx, val=1):
"""Return a tuple with the index idx incremented by val"""
return t1[:idx] + (t1[idx]+val,) + t1[idx+1:] | bbf7c9d7b669fff5a42e224790b1af9135cce231 | 653,171 |
def getInputShape(model):
"""
Gets the shape when there is a single input.
Return:
Numeric dimensions, omits dimensions that have no value. eg batch
size.
"""
s = []
for dim in model.input.shape:
if dim.value:
s.append(dim.value)
... | 628f61a995784b9be79816a5bbcde2f8204640be | 707,243 |
def seq_del(s, i):
"""
Returns a new sequence with i missing. Mysteriously missing from the
standard library.
"""
return s[:i] + s[i+1:] | d72be1f0317d8252881e1a00384c9cc3a9a69488 | 295,604 |
def path_sequence(list_of_links, source, target):
"""
Convert set of tuples representing path in random order to a path
:param list_of_links: set of tuples representing path
:param source: source node
:param target: destination node
:return: pretty path as sequence of nodes
"""
pretty_p... | 696126c38fdb092033bee64846627bfbaa2c1523 | 450,482 |
def ascii_to_bit(ascii_string: str) -> str:
"""
Converts a given string of ASCII chars to a string of bits.
:param ascii_string:
:return:
"""
result = bin(int.from_bytes(ascii_string.encode(), 'big'))
result = result[2:] # We don't want this '0b' at the beginning.
while len(result) % 8 !... | 7600d7290fd638e25b06ef8fcbab6c19a93e1dd2 | 293,999 |
from typing import Any
def _get_ending_line(expr: Any) -> int:
"""Get the ending line number of a Python expression
This method gets the final line number of a
(possibly-multiline) Python expression.
Args:
expr (ast.AST): a Python expression object
Returns:
int: the line number ... | 164ed7fcc84a6d9b61b1f896358b52b7d6b1fdfb | 307,288 |
from pathlib import Path
def baddir(path: Path) -> bool:
"""
tells if a directory is not a Git repo or excluded.
A directory with top-level file ".nogit" is excluded.
Parameters
----------
path : pathlib.Path
path to check if it's a Git repo
Results
-------
bad : bool
... | 027366d5c1ddf1f55e2011709d826561d86950de | 678,379 |
def file_open(*args, **kwargs):
"""Open file
see built-in open() documentation for more details
Note: The reason this is kept in a separate module is to easily
be able to provide a stub module that doesn't alter system
state at all (for unit tests)
"""
return open(*args, **kwar... | 170e6a1980242bb0be20adc7db91a0dea2535298 | 508,961 |
def is_409(item):
"""Return True if the item has a status_code of 409 (conflict).
Return TypeError if item doesn't have a status_code.
"""
if hasattr(item, "status_code"):
return (True, "Conflict") if item.status_code == 409 else False
return TypeError("is_409 cannot check item with mi... | 9b3ab5ec8e0d69c41e82544ab20db4beee63c80a | 466,169 |
def index_gen(length):
"""returns a callable
Parameters
----------
length : int
length of series of integers to generate
Returns
-------
callable
Notes
-----
When invoked with an int, returns all indices except that provided.
The result can be used with a numpy.tak... | 4d00f214e8ef15e392187206efaa6a5d44910182 | 177,692 |
def is_within_interval(value, min_value=None, max_value=None):
"""
Check whether a variable is within a given interval. Assumes the value is
always ok with respect to a `None` bound. If the `value` is `None`, it is
always within the bounds.
:param value: The value to check. Can be ``None``.
:pa... | 8b82858d33b56fcd479f97a4077c03bd1392bb75 | 94,541 |
import ast
def convert(val):
"""Convert input to interpreted data type.
:param val: value to be interpreted
:returns: interpreted value
"""
try:
return ast.literal_eval(val)
except BaseException:
pass
return val | 02995e3aac6a16beea1e0f36e1c4bec4343ea0f9 | 479,134 |
def get_one_of(object, attribs):
"""Try to get the first one of attribs."""
for a in attribs:
v = getattr(object, a, None)
if v:
return v | 1c440e332b269c5542eac26c299bc2afc24cb2f4 | 522,963 |
def parse_header(line):
"""Parse output of tcpdump of pcap file, extract:
time
date
ethernet_type
protocol
source ip
source port (if it exists)
destination ip
destination port (if it exists)
length of the data
"""
ret_dict = {}
... | 461d970e3ab686f9b14bfb55fbe56552e0f9e90b | 206,366 |
def camel_to_snake(s: str) -> str:
"""Converts "CamelCase" to "snake_case"."""
return "".join(f"_{c}" if c.isupper() else c for c in s).strip("_").lower() | d6697877e5a00ce2bb1149dd6cf0da2b0230c9cd | 664,982 |
def shift_cftime_index(xobj, time_string, n, freq):
"""Shifts a ``CFTimeIndex`` over a specified number of time steps at a given
temporal frequency.
This leverages the handy ``.shift()`` method from ``xarray.CFTimeIndex``. It's a
simple call, but is used throughout ``climpred`` so it is documented here... | 18fd4e4b388f2006a59bbf5c20f3793028e2510c | 565,177 |
import binascii
def hex_to_sha(hex):
"""Takes a hex sha and returns a binary sha"""
assert len(hex) == 40, "Incorrect length of hexsha: %s" % hex
try:
return binascii.unhexlify(hex)
except TypeError as exc:
if not isinstance(hex, bytes):
raise
raise ValueError(exc.a... | ad3870706f523353d8d99455f64a90abf23b491a | 232,500 |
def get_mode_count(df):
"""
Computes the mode and the count of the mode from an array.
Args:
df - data fram with ONE column
Returns:
df_mode - mode of the column
df_mode_count - count for that mode
"""
# calculate the mode and its count from the input data fram (with one column)
... | 8712b6a351c6afdb328e94a633652af86a1f4eba | 18,159 |
def get_stop_times(feed, date=None):
"""
Return a subset of ``feed.stop_times``.
Parameters
----------
feed : Feed
date : string
YYYYMMDD date string restricting the output to trips active
on the date
Returns
-------
DataFrame
Subset of ``feed.stop_times``
... | 8e7df4ab933e5a1121ba943fb473fa51b9fc160c | 218,962 |
def get_pycache(source, names):
"""
gets all `__pycache__` directories available in names.
:param str source: source directory of contents.
:param list[str] names: name of all contents in source.
:rtype: list[str]
"""
return [name for name in names if '__pycache__' in name] | d1e325648e0353c8ece59a8448167ff16a0fb9a9 | 563,087 |
def doubles(counts):
"""Returns count of double occurrences."""
return (counts==2).sum() | 4ad63c65120c367ab7a9c7b10f9bb9272f3f99ff | 375,687 |
import json
def load_json(path):
"""Loads a JSON file and returns the data.
Args:
path: (str) a file path.
Returns:
(dict) the parsed data.
"""
with open(path, 'r') as fp:
ret = json.load(fp)
return ret | c547a8f794b0d2026e1f504d37565c0614ffbf7b | 324,696 |
def removekey(d, key):
"""This functions returns a copy of a dictionnary with a removed key
Parameters: d (dict): dictionnary
key: the key that must be deleted
Returns: copy of dictionnary d without the key
"""
r = dict(d)
del r[key]
return r | 45eeb1e56a38e065aeb2dba58acc098152ea0425 | 663,932 |
def str2ascii(string: str) -> list:
"""Convert a string to a list of ascii-codes"""
return [ord(i) for i in string] | a938b0c585e78a455721e9d17e8915b0769a025f | 698,310 |
import csv
def read_csv(input_file):
""" Read a csv as dict
Return participant dict with paired_site """
participant = {}
with open(input_file) as csv_file:
reader = csv.DictReader(csv_file)
try:
for row in reader:
if row["paired_site"] == "" or row["paire... | 2cd50be71d5fe11e13b582142553c6b3e5544b02 | 262,505 |
def compute_avg_headway(headways):
"""
Compute the average headways from the sum of all trips with the
specified headways.
:param headways: (list of int) list of headways
"""
if not headways:
return 0
frequency = sum([1 / headway for headway in headways])
return 1 / frequency | 2c0f034de54160aa899fb34bcc805dc3b5f90627 | 262,168 |
def get_basename(filename, extention):
"""
Get basename for a file contains format extension.
:param filename: (string) filename/path
:return: basename of a file without path or extension
"""
if extention in filename:
filename = filename.split("/")[-1]
base = filename.replace(ext... | 90133465b19330fbd999166115f2dc35b28c7e70 | 399,037 |
import six
def quote(s, *args):
"""Return quoted string even if it is unicode one.
:param s: string that should be quoted
:param args: any symbol we want to stay unquoted
"""
s_en = s.encode('utf8')
return six.moves.urllib.parse.quote(s_en, *args) | 14b72b9231a2d53f242136cb002dc2d189eca1aa | 61,297 |
def extract_version(txt):
"""This function tries to extract the version from the help text of any
program."""
words = txt.replace(",", " ").split()
version = None
for x in reversed(words):
if len(x) > 2:
if x[0].lower() == "v":
x = x[1:]
if "." in x an... | a9a7264942cf607d8faa5311c5bcfdac553d7929 | 436,743 |
def test_module(client):
"""
Returning 'ok' indicates that the integration works like it is supposed to. Connection to the service is successful.
Args:
client (Client): instance of the Client class
Returns:
'ok' if test passed, anything else will fail the test.
"""
try:
... | c52ef76a3db946974b2798f2b20ee3eb6388b72f | 97,240 |
def flatten_dict(nested_dict, sep=None):
"""
flatten_dict flattens a dictionary,
The flattened keys are joined using a separater which is default to '__'.
:param nested_dict: input nested dictionary to be flattened.
:type nested_dict: dict
:param sep: seperator for the joined keys, defaults to... | b7fe90a54df05ad8adc063be50d9dbe91a240a99 | 428,751 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.