content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
from typing import AnyStr
import codecs
def auto_encode(string: AnyStr, encoding: str = "utf-8", errors: str = "strict") -> bytes:
"""Lookup a encoder and encode the string if it is bytes, else return it
untouched if it's already in bytes (for utf). If its an int, etc, it'll try
to wrap it in bytes for you.
:par... | ff5854e843f718adaec728ac11083cfa22604e9e | 73,544 |
def merge_pages(key, pages):
"""Merge boto3 paginated results into single list.
Args:
key (str): the key used to gather results from each page.
pages (list): a list of pages of typically description dictionaries.
Returns:
list: a single flat list containing results of all pages.
... | 56ffe16f66e6a4d98c7e742d582f62a88d00a4e3 | 395,783 |
def convert_points_from_homogeneous(points):
"""Converts 3D points from the homogeneous coordinate
Args:
points (numpy.ndarray): The points to convert. It should a 2D array with
shape 4 x num_points.
Returns:
numpy.ndarray: Non-homogeneous points. A 2D array with shape
... | 96a21ae2d03f96f459e29978bb04ec779cbc5194 | 432,798 |
def parsemsg(s):
"""
Breaks a message from an IRC server into its prefix, command, and arguments.
"""
prefix = ''
trailing = []
if s[0] == ':':
prefix, s = s[1:].split(' ', 1)
if s.find(' :') != -1:
s, trailing = s.split(' :', 1)
args = s.split()
args.append(t... | 0c553a57c82547ea68d9978580d657c8d432f5d2 | 327,768 |
def get_elements_by(xmldoc, *args, **kwargs):
"""Searches xml for nodes with provided tag, attributes (args) and
attribute key-val pairs (kwargs)
Args:
xmldoc (xml.dom.minidom): minidom object (returned by minidom.parse())
tag (str, optional): name of tag to search for
*args (str, o... | f9dc65fd48c5911430489adfef06b9943e562e83 | 397,072 |
def get_events(headers, data):
""" Build a list of dictionaries that have the detail and what that detail "contains".
Args:
headers (list): Headers for these type of events (Provides the order and expected output)
data (dict): Event data to lookup
Returns:
list: list of dictionary ... | 3b4ea233fa48deb193c26b74cd53352d6e5c875c | 392,467 |
def parse_plot_set(plot_set_string):
"""
Given one of the string arguments to the --plot-sets option, parse out a
data structure representing which conditions ought to be compared against
each other, and what those comparison plots/tables should be called.
The syntax of a plot set is [title:]c... | 1df83681aa3110dfd9302bd7918f15dfbfa497ab | 706,518 |
def check_for_security_updates_using_scheduled_task(api, configuration, api_version, api_exception):
""" Creates a scheduled task that checks for security updates.
The scheduled task runs immediately after it is created, and is deleted thereafter.
:param api: The Deep Security API modules.
:param c... | 06b77568781624075fb8d8b4ad765d3e9b616bce | 196,087 |
def name_and_age(name, age):
"""Returns a string stating the person's age."""
if age >= 0:
return name + " is " + str(age) + " years old."
else:
return "Error: Invalid age" | 87e3320324261cf56c3dbb8150d01d95846c7454 | 360,582 |
def table_name(table, column):
"""Compute the table name
table (string) : the originate table's name
column : the column name
return : string
"""
return "{0}__{1}_agg".format(table, column) | 3d247d424f558d22559df2dbc9ac9e5e0fa2768f | 118,664 |
def to_hex_string(string: str) -> str:
"""Converts UTF-8 string into its hex representation
:param string: str
The string to convert to hex
:return:
Hex representation of the given string
"""
return string.encode('utf-8').hex() | 62b9b71af31bccdde136aa6d2dabbb2ee3df2ea7 | 25,169 |
from typing import List
def group_anagrams(strs: List[str]) -> List[List[str]]:
"""
LeetCode 49: Group Anagrams
Given an array of strings, group anagrams together.
"""
str_map = {}
res = []
for s in strs:
temp = ''.join(sorted(s))
if temp not in str_map:
str_map... | 847b448f194d798bbc5f201c2864cd1dd0d6c40c | 586,147 |
def generate_dot(dc):
"""
Generates a dot format graph of docker-compose depends_on between services
:param dict dc: Docker comppose configuration loaded as a python dict
:rtype: string
"""
lines = []
lines.append("digraph docker {")
for service_name, service in dc["services"].items():
... | 6b9fcfbb939e779593b1e559065ab7daa1a4ba28 | 654,919 |
def set_search_csrf(session):
"""Extract the required CSRF token.
LinkedIn's search function requires a CSRF token equal to the JSESSIONID.
"""
csrf_token = session.cookies['JSESSIONID'].replace('"', '')
session.headers.update({'Csrf-Token': csrf_token})
return session | 1c506f6c7039b4908cadd772c2e7a289b7d18ae3 | 598,727 |
from typing import List
def str_in_list_non_case_sensitive(string: str, list_of_strings: List[str]) -> bool:
"""
>>> str_in_list_non_case_sensitive('aba',['abc','cde'])
False
>>> str_in_list_non_case_sensitive('aBa',['abc','Aba'])
True
"""
string = string.lower()
list_of_strings = [my_... | dd71eec9b75feb3b81ee4458a94a94b9a21cd398 | 403,757 |
def getattribute(objeto, name: str):
"""Returns the attribute matching passed name."""
# Get internal dict value matching name.
value = objeto.__dict__.get(name)
if not value:
# Raise AttributeError if attribute value not found.
return None
# Return attrib... | 8235c0c7440d0712d2978cfd72ca9529ba7b80b6 | 528,587 |
def quick_exponent_with_mod(base, power, modulo):
"""Compute quickly the exponent within a given modulo range.
Will apply a modulo with the specified base at every iteration of the
exponentiation algorithm, making sure the result is in the given range."""
# 'powers' will be a list of the base with powe... | 404373115f14b3d751c9bb7fb77b101aa7a7bd70 | 55,160 |
def get_schc_conf(config):
"""Return a dict of which values are taken from the config instance."""
dict_config = {}
for n in config.__dir__():
dict_config[n] = getattr(config, n)
return dict_config | 9f66d470dde02c0c1c2a12425bc976f03c5392ba | 193,795 |
def _calculate_pycls_momentum(
alpha: float, total_batch_size: int, max_epoch: int, update_period: int
):
"""pycls style momentum calculation which uses a relative model_ema to decouple momentum with
other training hyper-parameters e.g.
* training epochs
* interval to update ema
* b... | 1928ac4d50e1e2f3acd5b187ef31639e309dfa21 | 518,279 |
def format_numeric_column(column):
"""
Format fn for simple numeric columns.
Returns column (zero-indexed) +1 to give 1-indexed label.
"""
return column + 1 | ff8393d4fb13d9742d3bc636d4720237596b26fe | 595,692 |
import itertools
def flatten(list2d):
"""
flatten nested lists into a single list
"""
return list(itertools.chain.from_iterable(list2d)) | 4df26e37e252c8ddc9bc6794ac66612aa005ef87 | 203,880 |
from typing import Iterable
from typing import Generator
def flatten(collection: Iterable[Iterable]) -> Generator:
"""Flatten list of lists in one plane list"""
return (item for sublist in collection for item in sublist) | 4ae959b5e8446209a3156bfe62e4c46ef4dc55db | 341,692 |
def gen_new_coordinates_from_change_in_coordinates(old_coordinates, change_in_coordinates):
"""Calculate new coordinates given coordinates(lat,lon) and change_in_coordinates(lat,lon)."""
return tuple(map(lambda x, y: x + y, old_coordinates, change_in_coordinates)) | cb12e51e7f7526038758bb1229c6fc8a815949e1 | 432,364 |
def _gt_get_all_nodes(self):
"""
Get all the nodes in this tree
"""
return self.tree.get_all_nodes() | 92793d01b0f4a0c347a7e5a717d3bf760b27288b | 59,960 |
import random
def choice(seq):
""" Choose a random element from a non-empty sequence.
(Based on the Python 2.x code for random.choice, and used for deterministic results across
platforms as Python 3.x changed the way random.choice worked.)
"""
return seq[int(random.random() * len(seq))] | d7fe586187bd873e62f2b25b43491ff2d28544ca | 325,701 |
import traceback
def _extract_stack(count=5):
"""
Helper function to extract the function stack.
:param count: Number of stack(last most) to extract.
:return: function names as string.
"""
result = ''
for t in traceback.extract_stack()[:count]:
result += '{0}->'.format(t[2])
re... | ca507781ad4a63fbc7d9f567135a1c797990272d | 279,964 |
def order_cols(df, cols):
"""Put columns in cols first, followed by rest of columns"""
rest = [col for col in df.columns if col not in cols]
df = df[cols + rest]
return df | 269e9ecaf976e89af0784daf7052cfd3fe494e27 | 575,667 |
def check_error(http):
"""
Checks for http errors (400 series) and returns True if an error exists.
Parameters
----------
http : addinfourl whose fp is a socket._fileobject
Returns
-------
has_error : bool
"""
err = http.code
if 400<= err < 500:
print("HTTP error {}... | 20ba1426758e68b196708ec3260ff1f3a86c2820 | 684,573 |
def max_lt(seq, val):
"""
Return greatest item in seq for which item < val applies.
None is returned if seq was empty or all items in seq were >= val.
>>> max_lt([3, 6, 7, 11], 10)
7
>>> max_lt((5, 9, 12, 13), 12)
9
"""
idx = len(seq)-1
while idx >= 0:
if seq[idx] < val... | 0a6152a852c5e2c1421214d876ec6945ac42512b | 532,484 |
def f(value):
"""Format a float value to have 4 digits after the decimal point"""
return "{0:.4f}".format(value) | 1c4107a8f9c6a3b8e450a0c45647dc51816160ba | 195,667 |
def neighbour_coordinates(grid, point_coords):
"""Returns list of coordinates for neighbours of the given point,
within the bounds of the grid"""
grid_width = len(grid[0])
grid_height = len(grid)
point_row, point_col = point_coords
neighbour_coords = [
(point_row, point_col - 1),
... | 614e936c0992b734ec47c61405fda64b2a0ad348 | 604,418 |
def format_line(data, linestyle):
"""Formats a list of elements using the given line style"""
return linestyle.begin + linestyle.sep.join(data) + linestyle.end | 6a49a80f876ffe8a8f38e6e987051a0247858c6c | 19,148 |
from pathlib import Path
from typing import Optional
def resolve_absolute_offset(
dataset_path: Path, offset: str, target_path: Optional[Path] = None
) -> str:
"""
Expand a filename (offset) relative to the dataset.
>>> external_metadata_loc = Path('/tmp/target-metadata.yaml')
>>> resolve_absolut... | cbeb2dfb6fea41d5a995ada529d964357c4e9fb9 | 653,737 |
def find_single_number(arr: list[int]) -> int:
"""Finds the single number in a non-empty array of integers where every number appears exactly twice, except for one which appears exactly once.
Complexity:
Time: O(n)
Space: O(1)
Args:
arr: array of numbers with all but a single eleme... | a10f5f8cbb49968073c4a28d4eca883c128fb542 | 642,233 |
def num_active_calls(log, ad):
"""Get the count of current active calls.
Args:
log: Log object.
ad: Android Device Object.
Returns:
Count of current active calls.
"""
calls = ad.droid.telecomCallGetCallIds()
return len(calls) if calls else 0 | a6674df1e8e539478db6ab1a640fbce1cf0b6b4c | 701,482 |
def string_repr(source_string):
"""Return a nice visual representation of the given
SourceString and all its properties.
Any property that isn't populated (e.g. tags
or developer comment) will be omitted.
"""
return (
'[green]"{string}"[end]\n'
'{key}'
'{context}'
... | b3261e9731445819173ab4a12e4f189a157660b8 | 232,209 |
def dice_similarity_coefficient(inter, union):
"""Computes the dice similarity coefficient.
Args:
inter (iterable): iterable of the intersections
union (iterable): iterable of the unions
"""
return 2 * sum(inter) / (sum(union) + sum(inter)) | ae58310528b7c24b7289cb3bcf76c72745c8bacc | 678,115 |
def get_local_node_mapping(tree, last_tree, spr):
"""
Determine the mapping between nodes in local trees across ARG.
A maps across local trees until it is broken (parent of recomb node).
This method assumes tree and last_tree share the same node naming
and do not contain intermediary nodes (i.e. si... | b30a95bb0c23fc00005de9474da19e781bc0485e | 26,043 |
import math
def computePairN(att_name, att_value, data):
"""
Sub-function to compute number of pairs that input value > * used in Pairwise oracle
Attributes:
att_name: sensitive attribute name
att_value: value of protected group of above attribute
data: dataframe that stored the d... | 495fdc105ab0aebf39464dffc50aa1a641973394 | 117,806 |
import json
def load_mapping(filename):
"""
Load mapping dict from file.
:param filename: mapping file name
:return: dictionary of key to one-hot encoding; number of keys
"""
f = open(filename)
mapping = json.load(f)
return mapping, len(mapping) | e87b9c968bf12e9eb6a8480e56b06256f4496bef | 269,694 |
def pvct(pv: float, compr_total: float):
""" Pore Volume times Total Compressibility
Parameters
---
pv : float
pore volume
compr_total : float
total compressibility
Return
pvct : float
pore volume total compressibility
"""
return pv*compr_total | 31c84e4dc94cb2f1c78c9e26ba02cec4c81f0800 | 38,022 |
def s_and(*args):
"""Logical and."""
result = True
for i in args:
result = result and i
if not result:
break
return result | 022f3a14e0430210a636828daf2056c653107c58 | 107,088 |
from typing import OrderedDict
def dictify(od):
"""Recursively replace OrderedDict with dict"""
if isinstance(od, OrderedDict):
return dict((k, dictify(v)) for k, v in od.items())
else:
return od | 886fc727dba02f9029945d5efa947d7573c3df19 | 624,913 |
def simple_format(num):
"""Takes a number and returns the simplest format for the number removing
all trailing 0's and the '.' if it's the trailing character.
>>> simple_format(123)
'123'
>>> simple_format(123.0)
'123'
>>> simple_format(123.01100)
'123.011'
"""
return ('%f' % nu... | 1c8b27aa2f2d5059babe440a40fa1aeb067a59ec | 446,350 |
from typing import OrderedDict
def dict_sort(in_dict: dict)-> OrderedDict:
"""
sort dict by values, 给字典排序(依据值大小)
Args:
in_dict: dict, eg. {"游戏:132, "旅游":32}
Returns:
OrderedDict
"""
in_dict_sort = sorted(in_dict.items(), key=lambda x:x[1], reverse=True)
return OrderedDict(i... | b32187f78861c2f04c377030ecc45b1892ace708 | 674,947 |
import math
def mutual_information(co_oc, oi, oj, n):
"""
:param co_oc: Number of co occurrences of the terms oi and oj in the corpus
:param oi: Number of occurrences of the term oi in the corpus
:param oj: Number of occurrences of the term oi in the corpus
:param n: Total number of words in the c... | 76c27295c7e757282573eab71f2bb7cfd3df74cb | 2,906 |
def apply_rot_to_vec(rot, vec, unstack=False):
"""Multiply rotation matrix by a vector."""
if unstack:
x, y, z = [vec[:, i] for i in range(3)]
else:
x, y, z = vec
return [rot[0][0] * x + rot[0][1] * y + rot[0][2] * z,
rot[1][0] * x + rot[1][1] * y + rot[1][2] * z,
rot[2][0] * x + rot... | 6e119bbac62f1ced346f6ac46abd69058aedf698 | 557,893 |
def get_cutoff_class(sample):
"""
Identify which set of cutoffs to use when classifying a given variant call
Arguments
---------
sample : namedtuple('Metrics')
Row from variant metrics table
Returns
-------
(source, svtype) : (str, str)
Key to cutoffs table
"""
... | f4b6f37f912c638bf9fd70644658a699a4c051b6 | 564,072 |
import re
def original_image_extender(pipeline_index,
finder_image_urls,
extender_image_urls=[],
*args, **kwargs):
"""
Example:
http://media-cache-ec0.pinimg.com/70x/50/9b/bd/509bbd5c6543d473bc2b49befe75f4c6.jpg
http:/... | 19c4c2f61cf027acbb5ffc75cd72bd2cc802cfab | 650,595 |
def read_to_whitespace(fptr, track):
"""
Read, skipping over white space. Then read all non-whitespace until one
character of white space is consumed. Return the non-whitespace that was
read. If the end of the file is encountered, return any non-whitespace if
available, or an empty bytes string.
... | 80d246f578af932830169306816acb5f0d6bfffb | 670,190 |
def field_add(field, vector):
""" Add the vector to the field.
Parameters
----------
field: array (x, y, z, 3)
an image of vectors.
vector: array (3, )
the vector that will be added to the field.
Returns
-------
field: array (x, y, z, 3)
the incremented image of... | 06387b1c72b97a926c3e72a83f3c033921d013d7 | 97,439 |
from typing import Tuple
from typing import Mapping
from typing import Counter
def create_mappings(dataset_path: str) -> Tuple[Mapping, Mapping]:
"""Creates separate mappings to indices for entities and relations."""
# counters to have entities/relations sorted from most frequent
entity_counter = Counter(... | e1724c485a4cef659d27f75b6e597892f54201af | 192,694 |
import json
def response(message, status_code):
"""Returns a dictionary object for an API Gateway Lambda integration
response.
:param message: Message for JSON body of response
:type message: str or dict
:param int status_code: HTTP status code of response
:rtype: dict
"""
if isinst... | 4928c22b409e7d81f6b287758eb36e7583ba029d | 494,603 |
import math
def _estimate_group_size(encoding_size):
"""
Given an encoding size (e.g. 128 B), estimate the number of encodings that will likely
be under 100MiB in data including blocks. Note this is hopefully very conservative
in estimating the average number of blocks each record is in.
"""
n... | 7983795c0c6f21212882c888649596d028c6cecf | 461,003 |
def nrow(data):
"""
Return the amount of rows in `data`.
This is a useful shorthand for `data.nrow` in contexts where you don't have
direct access to the data frame in question, e.g. in group-by-aggregate
>>> data = di.DataFrame.read_csv("data/listings.csv")
>>> data.group_by("hood").aggregate... | 6246110df7ff356183258d5232e369ec632c1a1a | 336,143 |
def find_ballot(ballot_num, unique_ballot_manifest):
"""
Find ballot among all the batches
Input
-----
ballot_num : int
a ballot number that was sampled
unique_ballot_manifest : dict
ballot manifest with unique IDs across batches
Returns
-------
tuple : (original_b... | f7edb4e8d553d30684d608a44c58a675b10d7e9b | 124,722 |
def mass_aspect(self, truncate_ell=max):
"""Compute the Bondi mass aspect of the AsymptoticBondiData.
The Bondi mass aspect is given by
M = -ℜ{ψ₂ + σ ∂ₜσ̄}
Note that the last term is a product between two fields. If, for example, these both have
ell_max=8, then their full product would have ... | 828a70adf812111ccd232a53e4947231e4da87d8 | 427,315 |
def itemize(x):
"""
Extract item from a list/tuple with only one item.
>>> itemize([3])
3
>>> itemize([3, 2, 1])
[3, 2, 1]
>>> itemize([])
[]
:param list|tuple x: An indexable collection
:return: Return item in collection if there is only one, else
returns the co... | b88473b3ee5e417b9dd50bb066a08998790c09df | 544,174 |
def aggregate(collection, pipeline):
"""Executes an aggregation on a collection.
Args:
collection: a `pymongo.collection.Collection` or
`motor.motor_tornado.MotorCollection`
pipeline: a MongoDB aggregation pipeline
Returns:
a `pymongo.command_cursor.CommandCursor` or
... | 03ea889ea23fb81c6a329ee270df2ac253e90d69 | 5,002 |
def invert_dictionary(aDict):
"""Transforms a dict so that keys become values and values become keys"""
return {v: k for k, v in aDict.items()} | a4ec169b2f8d0857d4b22191c8823b17a03fc954 | 631,073 |
def phie_gaymard(phid, phin):
"""Estimate the effective porosity using Gaymard-Poupon [1]_ method.
Parameters
----------
phid : array_like
Density porosity (porosity calculated using density log)
phin : int, float
Neutron porosity (porosity calculated using neutron log)
Returns... | 57c59836310baa180b42b265112dc283774759d8 | 455,779 |
def as_flag(b):
"""Return bool b as a shell script flag '1' or '0'"""
if b:
return '1'
return '0' | 3caf3136b4845b7c6dec2e704744314101215399 | 268,543 |
from typing import OrderedDict
import random
def _downsample_dict(full_dict, num_samples):
"""Returns a down-sampled dictionary with a given number of samples in
each value list.
Args:
full_dict: A dictionary with lists as values to down-sample.
num_samples: The number of elements in each... | 4072a0f3cd1755b65a9070089d618992f76a582f | 194,810 |
from typing import Union
import copy
import collections
def dls_sort(orig: Union[dict, list, set]) -> Union[dict, list]:
"""
Given a nested dictionary, set or list, return a sorted version. Note that lists aren't sorted (they merely retain
their original order). Original data is unchanged.
:param or... | 5d77cf35b35d5f297ea16edfc472d87ce9030076 | 391,113 |
def get_util2d_shape_for_layer(model, layer=0):
"""
Define nrow and ncol for array (Util2d) shape of a given layer in
structured and/or unstructured models.
Parameters
----------
model : model object
model for which Util2d shape is sought.
layer : int
layer (base 0) for whic... | a56e00698b6d498800b895e83c84bed5b2ccc09d | 686,234 |
import re
def remove_pii_phone(text: str, replacement: str = '--PHONE--') -> str:
"""
Pass a string and return the text string with phone numbers removed
Parameters
----------
text : str
The text to replace phone numbers in
replacement : str
The text to replace phone numbers w... | 148b0564d2bcc0cae8de8d8f4e4c50fcdd221fc0 | 369,993 |
def normURLPath(path):
"""
Normalise the URL path by resolving segments of '.' and '..'.
"""
segs = []
pathSegs = path.split('/')
for seg in pathSegs:
if seg == '.':
pass
elif seg == '..':
if segs:
segs.pop()
else:
seg... | 78abcc6c1cce17e5678baac10af96db508b3d57a | 647,490 |
def requests_length(requests):
"""Total number of pageviews across all sessions."""
return len([r for r in requests if 'is_pageview' in r and r['is_pageview'] == 'true']) | 8980ebdf8fe71aa5ffcf3bcc54920d1b0c83cf65 | 53,410 |
import io
def write_pxi(filename, definitions):
"""
Write a cython include file (.pxi), `filename`, with the definitions in the
`definitions` mapping.
"""
with io.open(filename, mode='w', encoding='utf-8') as pxi_file:
for name, val in definitions.items():
pxi_file.write(u"DEF ... | 0b2c892afc3d639b0d48169bfbe35917c5494110 | 265,485 |
def init_tdl(model, inputs, labels, device):
"""
Initialize TDLs using input and label data.
:param model: NARX model
:param inputs: inputs
:param labels: labels
:param device: device type
:return: input, labels, input TDL, output TDL
"""
max_delay_size = max(model.input_delay_siz... | fcec1fd661e8fe2ccf8cab76ae1399a0fe2b0b87 | 159,591 |
from typing import Any
def is_num(num: Any) -> bool:
"""Function that tests if object can be converted to number
A function that takes any input and detects if it is a number by
attempting to convert the input to a float. This function catches
convertable digit string cases.
Source:
https://... | d74a40b1cf5c2dee6b8001b7fc937a546ea55bc0 | 593,359 |
def count_crickMAX(args):
"""Count the number of sequences in the Crick fasta file"""
with open(args.crick, 'r') as crick_in:
count = 0
for line in crick_in:
if line.startswith('>'):
count +=1
return count | c4937613b917107f74aa6658719d3e6e243eebd2 | 32,289 |
def _get_nn_layers(spec):
"""
Returns a list of neural network layers if the model contains any.
Parameters
----------
spec: Model_pb
A model protobuf specification.
Returns
-------
[NN layer]
list of all layers (including layers from elements of a pipeline
"""
... | b992a9fab6b5e042fece06ae384f784f7ec38719 | 615,149 |
def get_func_id(func):
"""Get a quasi-identifier of a given function."""
return func.__qualname__ | 8aba6fdb53d5c1ba89e0e4443b919070516224fa | 641,397 |
from typing import Dict
def decode_internal_tx(raw_internal_tx: bytes) -> Dict:
"""
Decodes bytes representation of an internal transaction into a dictionary.
Args:
raw_internal_tx: Bytes representing an internal transaction.
Returns:
Internal transaction in dictionary form.
"""
... | 44ab2b062d121f025a5bcde8ce48a49c576a758e | 457,935 |
def _ros_plot_pos(row, censorship, cohn):
"""
ROS-specific plotting positions.
Computes the plotting position for an observation based on its rank,
censorship status, and detection limit index.
Parameters
----------
row : pandas.Series or dict-like
Full observation (row) from a cen... | 5111aa3223e1a2111c106b8711edc54cee9f878f | 352,995 |
import re
def is_requirements_txt(filename) -> bool:
"""Check if filename is of requirements.txt format
:param filename: filename
:return: True if filename is in requirements.txt format
"""
return True if re.match(r'.*requirements.*\.txt$', filename) else False | 01a67f29b8da728ef21b17844a5eb2005b1e5b23 | 214,561 |
from typing import Dict
from typing import List
import random
def get_shared_chores(chores: Dict, chore_group: List) -> List:
"""
Find chores that are shared and should be assigned to a pool of several users.
:param chores: All chores
:param chore_group: List of the categories that should be shared
... | c45c7c24517922082e9740dc31da2e27e083d46b | 501,687 |
def A_tot_wkend_days_init(M, i, t, e):
"""
Initialize number of weekend days worked in the i'th pattern, tour type t,
and weekend type e.
:param M: Model
:param i: weekend worked pattern
:param t: tour type
:param e: weekend type (1=Sun and Sat, 2=Fri and Sat)
:return: number of weekend... | f1ee32a30bd4a89c30148c3d0cdf95da6c5836ed | 620,954 |
def exc_info(space):
"""Return the (type, value, traceback) of the most recent exception
caught by an except clause in the current stack frame or in an older stack
frame."""
operror = space.getexecutioncontext().sys_exc_info()
if operror is None:
return space.newtuple([space.w_None,space.w_None,spac... | 39a1515476267863832434ca738241bf40fa8812 | 282,290 |
def top_n_filter(peak_set, n=40):
"""Keep only the top `n` most abundant peaks in the spectrum.
Parameters
----------
peak_set : :class:`Iterable` of :class:`~.PeakBase`
The peaks to filter
n : int, optional
The maximum number of peaks to retain, by default 40
Returns
-----... | fc12934ccfc6163903f6a8286a3dc9f5219ad484 | 487,481 |
def get_basic_authorization(request):
"""Gets basic auth username and password
Parameters
----------
request : A django request obj
Returns
-------
auth_username : base64 decoded
auth_password : base64 decoded
"""
http_auth = request.META.get('HTTP_AUTHORIZATION')
if ... | 3ea69f5b8a296bce9e03397b279d88ae577f795b | 423,394 |
def all_indices(s, substr):
"""
Find all indices in ``s`` where ``substr`` begins.
Returns results in list.
"""
indices = []
i = s.find(substr)
while i >= 0:
indices.append(i)
i = s.find(substr, i+1)
return indices | 93d199f07f923ff89c87a642edb6ec3cfb99dfbb | 552,927 |
def ints_to_rgb(r, g=None, b=None):
"""Convert ints in the [0...255] range to the standard [0...1] range.
Parameters:
:r:
The Red component value [0...255]
:g:
The Green component value [0...255]
:b:
The Blue component value [0...255]
Returns:
The color as an (r, g, b) tuple in... | d24e336f13beb3538428724ce56532f1efb8fb6f | 618,197 |
def in_static_dir(filepath, static_dirs):
"""See if filepath is contained within a directory contained in
static_dirs."""
for directory in static_dirs:
if filepath.startswith(directory):
return True
else:
return False | 4f5c7357cefb6de3a3a5cd53fc8e154b12e7c418 | 490,857 |
def _create_query_dict(query_text):
"""
Create a dictionary with query key:value definitions
query_text is a comma delimited key:value sequence
"""
query_dict = dict()
if query_text:
for arg_value_str in query_text.split(','):
if ':' in arg_value_str:
arg_valu... | 2e4478bdf110911d4ca9fcc6c409aab3504a0b8a | 8,510 |
import binascii
def shinglify(clean_text):
"""
Generates list of 'shingles': crc sums of word subsequences of default length
:param clean_text: cleaned text to calculate shingles sequence.
:return: shingles sequence
"""
shingle_length = 3
result = []
for idx in range(len(clean_text) - ... | aedd17f3eed48aaf1cbeb0bbe34103d594d6428b | 669,687 |
def layer_sizes(X, Y):
"""
设置网络结构
Args:
X: 输入的数据
Y: 输出值
Return:
n_x: 输入层节点数
n_h: 隐藏层节点数
n_y: 输出层节点数
"""
n_x = X.shape[0] # 输入层大小(节点数)
n_h = 4
n_y = Y.shape[0] # 输出层大小(节点数)
return (n_x, n_h, n_y) | 62f6d0f3a1cb772091fce3657d3f158bc86bd5a1 | 262,377 |
def label_continues_or_empty(seq, previous_label):
"""Returns True if seq continues previous_label or 'O' (that is no new
tags starting)."""
seq = list(seq) # copy
if previous_label.startswith('B-'):
continuation_label = 'I-' + previous_label[2:]
elif previous_label.startswith('I-'):
... | d3a3c198ff8ba1a6e9be60920ab0cff380f9a5e0 | 278,215 |
import socket
def get_host_name() -> str:
"""Return the name of the current host"""
return socket.gethostname() | 70958a59576a20da2fc053f8ff1e8d7f81bf1ba4 | 143,575 |
def binary_scores_from_counts(ntp, nfp, ntn, nfn):
"""
Precision, recall, and F1 scores from counts of TP, FP, TN, FN.
Example usage:
p, r, f1 = binary_scores_from_counts(*map(len, error_sets))
"""
prec = ntp / float(ntp + nfp) if ntp + nfp > 0 else 0.0
rec = ntp / float(ntp + nfn) if n... | cbdff276e5ee765c5154f2632b5a2edb35b72984 | 329,272 |
def plural(word: str, i: int) -> str:
"""Returns either "word" or "words" based on the number."""
return f"{word}{'' if i == 1 else 's'}" | 14efa098cc0a3af809194a5a88b806e6fc9c7b1a | 325,081 |
import random
import string
def rndstr(size=16, alphabet=""):
"""
Returns a string of random ascii characters or digits
:param size: The length of the string
:return: string
"""
rng = random.SystemRandom()
if not alphabet:
alphabet = string.ascii_letters[0:52] + string.digits
... | 03325a4ff88300d42dcbd7b1b58ba001e3581b5a | 392,941 |
def build_table(x,
perceiver,
cache,
cache_emb,
topk,
return_images=False,
index_dir=None):
"""
Maps each image to a linearized row in a table. Each entry in a row
is delimited by "|". Each entry comes from the t... | 5822dbe6f54a0d582bc19785cf9b1d49a4ac5eda | 677,960 |
def FindRecentBuilds(ab_client, branch, target,
build_type='submitted',
build_attempt_status=None,
build_successful=None):
"""Queries for the latest build_ids from androidbuild.
Args:
ab_client: The androidbuild API client.
branch: The name of ... | 089dcb868be50d60dbebe912fe8f68dc73a28c40 | 102,366 |
import torch
def monte_carlo_pose_loss(pose_sample_logweights, cost_target):
"""
Args:
pose_sample_logweights: Shape (mc_samples, num_obj)
cost_target: Shape (num_obj, )
Returns:
Tensor: Shape (num_obj, )
"""
loss_tgt = cost_target
loss_pred = torch.logsumexp(pose_samp... | 93f599ecc04178fe982d88ce39d0bd846534e4fb | 152,970 |
def get_rules(view):
"""Get auto-popup scope rule."""
rules = view.settings().get("color_helper.scan")
return rules if rules is not None and rules.get("enabled", False) else None | 1e04d93514785af0bb569c6b7ea7a8b2458c0175 | 590,529 |
def count_bags_inside(bag_name, rules):
"""Count total number of bags in bag_name."""
if not rules[bag_name]:
return 0
count = 0
for inside_bag, number_of_bags in rules[bag_name].items():
count += (number_of_bags + number_of_bags
* count_bags_inside(inside_bag, rules))
... | 68dcc6e6b400e1d4bc9c8fdd6ecf71a24caedaee | 171,272 |
def comma_separated_positions(text):
"""
Start and end positions of comma separated text items.
Commas and trailing spaces should not be included.
>>> comma_separated_positions("ABC, 2,3")
[(0, 3), (5, 6), (7, 8)]
"""
chunks = []
start = 0
end = 0
for item in text.split(","):
... | 6203ac8839be7718742426474ec093cdce5b58c3 | 101,123 |
def geometry_mismatch(shape1, shape2):
"""Compute relative mismatch of two shapes"""
return shape1.symmetric_difference(shape2).area / shape1.union(shape2).area | 0e56b8c8d8d030dd63d2176ad3f336d85951244b | 476,590 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.