content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
def get_all_subclasses(klass):
"""Return all subclasses of a class
Use Python introspection to list all subclasses of the given class.
This function do not stop to the direct children, but walk the whole
inheritance tree.
:param klass: the root class to use for introspection
:type klass: T
... | 056f1cd12255bfb6fff38d25a29923c6be43cc0f | 615,063 |
def num_words(tokens):
"""Given list of words, return no. of words (int)"""
return len(tokens) | 89388c467380803e834d2ef287d33d17b882d666 | 19,208 |
import base64
def convert_to_base64(image_file):
"""Open image and convert it to base64"""
print('Converting Image to Base64')
with open(image_file, 'rb') as f:
return base64.b64encode(f.read()).decode('utf-8') | 8013b297db36f71eadf9b6cdec685f984abf2a1e | 610,897 |
def cc(g):
"""
>>> graph = [[1, 4], [0], [3, 6, 7], [2, 7], [0, 8, 9], [], [2, 10], \
[2, 3, 10, 11], [4, 9], [4, 8], [6, 7, 11], [10, 7]]
>>> cc(graph)
3
"""
def dfs(g, t, seen):
for v in g[t]:
if v not in seen:
seen.add(v)
df... | 07ba602a45635df2d6eab79398c9408ce6b0e736 | 369,309 |
def project_value(value, src_min, src_max, dst_min, dst_max):
"""project value from source interval to destination interval"""
scaled = float(value - src_min) / float(src_max - src_min)
return dst_min + (scaled * (dst_max - dst_min)) | 29356e3df08986eb21c7cc44f4039d72d1c5558b | 138,375 |
import yaml
def load_config(path="config/default.ymal") -> dict:
"""
Load and parses a YAML configuration file
:param path: path to YAML configuration file
:return: configuration dictionary
"""
with open(path, 'r') as ymlfile:
cfg = yaml.safe_load(ymlfile)
return cfg | 59417b3e6f727244cee51558f064d371dd86d911 | 599,910 |
def _trim_ds(ds, epochs):
"""Trim a Dataset to account for rejected epochs.
If no epochs were rejected, the original ds is rturned.
Parameters
----------
ds : Dataset
Dataset that was used to construct epochs.
epochs : Epochs
Epochs loaded with mne_epochs()
"""
if len(e... | 0e08273d86188b8572d0b26feea992771694feb8 | 668,317 |
def str_manipulation(s):
"""
This function turns all the alphabet into lower case.
----------------------------------------------------------------------------
:param s: (str) the word that user input.
:return: ans (str), the word with the lower case.
"""
ans = ''
for ch in s:
if ch.isupper():
ans += ch.lo... | 61a91a4bbcafe2ffd913bad9055cbba115bd8311 | 117,966 |
def string_to_elements(string):
"""
:string: elements separated by colon as in s1:s2:s3
Return list of elements
"""
ss = string.split(':')
elements = []
for s in ss:
if s:
elements.append(bytes.fromhex(s))
return elements | e1fe186d0e4b8f4b5ebc7fcc548a24be734189d9 | 53,663 |
def bytes_to_string(bytes):
"""
It generates a string with a proper format to represent bytes.
Parameters
----------
bytes : int
A quantity of bytes
Returns
-------
size_str : str
The string representing the number of bytes with a proper format
"""
kilobytes = b... | 3e3a2946e02d0c7f5ed347da265ee329bab0f577 | 592,481 |
def convert_qty2gram(qty = {'val': '', 'unit': ''}):
"""
Convert OFF quantity to a standard quantity (in grams)
Args:
qty (dict): OFF quantity value and unit
Returns:
dict with value converted to grams (if possible) and
two new keys:
std: True if value could be convert... | c7f6a5cacf6ee7a799e06dfee9d2e14706acb9da | 195,222 |
from typing import Dict
def market_is_active(market: Dict) -> bool:
"""
Return True if the market is active.
"""
# "It's active, if the active flag isn't explicitly set to false. If it's missing or
# true then it's true. If it's undefined, then it's most likely true, but not 100% )"
# See http... | f8c113e2e3cf08906aa38cddbf89cb555096012e | 623,993 |
def file_length(file_obj):
"""
Returns the length in bytes of a given file object.
Necessary because os.fstat only works on real files and not file-like
objects. This works on more types of streams, primarily StringIO.
"""
file_obj.seek(0, 2)
length = file_obj.tell()
file_obj.seek(0)
... | 052254469e4a20e4b510cec52434ec88614d92c1 | 605,480 |
import inspect
def get_func_name() -> str:
"""Return calling function name."""
func_name = inspect.currentframe().f_back.f_code.co_name # type: ignore
return func_name | a48175ee7919e8744e39ebb196bce9827d8814ad | 238,415 |
def subset_BEA_Use(df, attr):
"""
Function to modify loaded BEA table based on data in the FBA method yaml
:param df: df, flowbyactivity format
:param attr: dictionary, attribute data from method yaml for activity set
:return: modified BEA dataframe
"""
commodity = attr['clean_parameter']
... | c3fbb7fc07e0bac81501f57c4f557b360601e3fb | 620,564 |
def getShapes(node):
"""Returns the shape of the dagNode
Arguments:
node (dagNode): The input node to search the shape
Returns:
list: The shapes of the node
"""
return node.listRelatives(shapes=True) | 290710ebfbc43830e847a441cb59beedf4aa7989 | 593,119 |
def cycle_length(k: int) -> int:
"""
Computes the repeated cycle length of the decimal expansion of 1/k.
e.g.
1/6 = 0.1(6) -> 1
1/7 = 0.(142857) -> 6
For k not equal to a multiple of 2 or 5,
1/k has a cycle of d digits if 10^d == 1 mod k = 0
"""
while k % 2 == 0:
k //= 2 #... | f4516683928174fa1e730074c40892e7a56ac0e4 | 57,694 |
def to_time_weighted(G, speed):
"""
Convert from distance weighted network to time weighted network.
Parameters
----------
:param G: networkx.MultiDiGraph
transport network with length attribute.
:param speed: float
speed in Km/h.
Parameters
----------
:return: netw... | f72f5284e528f1d103adf008c3c202029bc0d0ff | 150,173 |
def subtree_has_tag(block, tagname):
"""
Check if the doctree node contains a particular tag in its subtree.
Args:
block: A block that has to be checked
tagname: The searched tag
Returns: True if the subtree contains a node with the tagname or False otherwise.
"""
for node in blo... | d6fcf7c9315bfa88ed8d81afd4b0d48506a9816c | 448,506 |
def in_or_none(x, L):
"""Check if item is in list of list is None."""
return (L is None) or (x in L) | bc3e4ef5a8daf7669e7430940e361d4c7ec1a240 | 11,825 |
def remove_index(a, index):
"""Remove element at index of a sequence and return it as a list"""
a = list(a)
a.pop(index)
return a | a8987c36b18592852f9ec8223bedd293a265e95b | 234,039 |
def question2(a):
"""
Given a string a, find the longest palindromic substring contained in a.
:param a: initial string
:return: longest palindromic substring in a
"""
if a == None:
return None
elif len(a) < 2:
return a
biggest = (0, "")
for num in range(len(a)):
... | f8ff81316f5249733592cd03291828998a1ae6b0 | 438,297 |
def _SplitLineIntoRegions(line, uncovered_blocks):
"""Returns a list of regions for a line of code.
The structure of the output is as follows:
[
{
'covered': True/False # Whether this region is actually covered.
'text': string # The source text for this region.
}
]
The regions in the out... | 03cd4443595af38d102b5cb015dbde7251059d5e | 299,181 |
import re
def camelcase_to_snakecase(value: str) -> str:
"""
Convert a string from snake_case to camelCase.
>>> camelcase_to_snakecase('')
''
>>> camelcase_to_snakecase('foo')
'foo'
>>> camelcase_to_snakecase('fooBarBaz')
'foo_bar_baz'
>>> camelcase_to_snakecase('foo_bar_baz')
... | 05fe02739e8152bc64ab35bd842162b5d7c3ab4c | 704,075 |
def make_row_dict(row):
"""
Takes in a DataFrame row (Series),
and return a dictionary with the row's index as key,
and the row's values as values.
{col1_name: col1_value, col2_name: col2_value}
"""
ind = row[row.notnull()].index
values = row[row.notnull()].values
# to transformation... | e5eb63e5c56466cfc27756782bf9842a30801110 | 469,337 |
import requests
def get_content_from_url(url):
"""Get the content of the page from the URL as a string"""
r = requests.get(url)
return r.text | c8f3f65490d4ae32b03b0567882f792a18db51dd | 596,265 |
def _clean_values(values):
"""
Clean values to the state that can be used in Sheets API
:type values: list
:param values: Row values to clean
:rtype: list
:return: Cleaned values, in the same order as given in function argument
"""
return [value if value is not None else '' for value i... | 64556e79cfe019189b4df303bc26cd25a8980b14 | 94,633 |
def bindVarCompare(a, b):
"""
_bindVarCompare_
Bind variables are represented as a tuple with the first element being the
variable name and the second being it's position in the query. We sort on
the position in the query.
"""
if a[1] > b[1]:
return 1
elif a[1] == b[1]:
... | 3a5534115c4501e532bf5df3f93d2bc18ab6958d | 149,129 |
import torch
def pdist(sample_1, sample_2, eps=1e-5):
"""Compute the matrix of all squared pairwise distances. Code
adapted from the torch-two-sample library (added batching).
You can find the original implementation of this function here:
https://github.com/josipd/torch-two-sample/blob/master/torch_t... | 0e4eba793d19963e3afab3452c7ce981f8eccf57 | 359,602 |
def HeadLossCurcuit(List_Head):
"""
Head Loss Circuit is a function which calculates the head loss around a closed ventilation circuit.
Accepts input of a list (Junctions From-To) and Head Losses, in Water
A closed-circuit head loss is calculate and returned as a percentage (%)
Returns a Tuple of (H... | 47d0ab51ab1204579985dfe5fb2f1010ee6e6111 | 132,931 |
def get_freejoint(element):
"""Retrieves the free joint of a body. Returns `None` if there isn't one."""
if element.tag != 'body':
return None
elif hasattr(element, 'freejoint') and element.freejoint is not None:
return element.freejoint
else:
joints = element.find_all('joint', immediate_children_on... | bcd0980b28f6e2f5b45b854912f9e0916ee732b4 | 299,172 |
def get_null_term_str(data, encoding):
"""
extract null terminated string from data
:param data: data buffer
:param encoding: encoding used to convert bytes to str
:return: string
"""
# temp = data[:]
if 0 in data:
temp = bytes(data[:data.index(0)])
else:
temp = bytes... | 8c660261209419d284f36d1880e8f6f556b6d757 | 179,344 |
import glob
def matching_pathnames(paths):
"""Get list of matching pathnames for the given list of glob patterns."""
results = []
for p in paths:
results.extend(glob.glob(p, recursive=True))
return results | da83979d49304a37aa093b4240a7dfd52bdecaf7 | 547,552 |
def generate_bins() -> list:
"""
Generate color bins.
:return: List of bins
"""
h_bins = [(x / 10.0, (x + 1) / 10.0) for x in range(0, 10)]
h_bins[-1] = (h_bins[-1][0], 1.1)
s_bins = [(0.0, 0.333), (0.333, 0.666), (0.666, 1.1)]
l_bins = [(0.0, 0.333), (0.333, 0.666), (0.666, 1.1)]
bi... | fa207596bc915f83145964d6a07b5140fb021e5d | 680,128 |
import itertools
import operator
def invert_otm_mapping(mapping):
"""Inverts a one-to-many mapping."""
# Create a list of inverted (v, k) tuples.
tuples = (itertools.zip_longest(v, [k], fillvalue=k)
for k, v in mapping.items() if len(v) > 0) # yapf: disable
tuples = itertools.chain.from... | e4d3d0449f7b7e87ff00447228653788805a5a6d | 588,316 |
def get_pgstattuple_schema_name(curs):
"""
Getting the schema where the pgstattuple extension is installed
"""
query = """
select
n.nspname::text,
e.extversion::numeric
from pg_catalog.pg_extension e
join pg_catalog.pg_namespace as n on
n.oid... | 4223ae2fcdbb25314b7c523d78c95b3e510de1cf | 557,358 |
def get_missing_mlflow_experiment_tracker_error() -> ValueError:
"""Returns a detailed error that describes how to add an MLflow experiment
tracker component to your stack."""
return ValueError(
"The active stack needs to have a MLflow experiment tracker "
"component registered to be able to... | 572fe73e420480566066ed97e71a6acc13304c19 | 168,991 |
import collections
def _compile_job_list(job_iterator):
"""
Make one unified list of jobs from a job iterator.
Identify jobs by their `description` or index in the `job_iterator`.
Keep the last one in the `job_iterator` for each job identifier.
Remove jobs missing the required fields.
"""
... | f41383b913164dfdde898b7c23eb0f561beb9496 | 429,081 |
def test_cache_memoize_arg_normalization(cache):
"""
Test taht cache.memoize() normalizes argument ordering for positional and keyword
arguments.
"""
@cache.memoize(typed=True)
def func(a, b, c, d, **kargs):
return (a, b, c, d)
for args, kargs in (
((1, 2, 3, 4), {"e": 5}),... | 170069c8af7f9debe94029018f8df8bdaf31c01d | 647,313 |
def trim(inp_str, symbol):
""" Trim a string up to a symbol
Parameters
----------
inp_str : string
input string
symbol : string
symbol to split on
Returns
-------
out : string
trimmed output
"""
if isinstance(inp_str, str) and isinstance(symbol, str):
... | b884937fb36a330d08cd445150f48759382b357b | 269,009 |
def map_type_list_field(old_type):
"""
This function maps the list type into individual field type which can contain
the individual values of the list.
Mappings
- list:reference <table> --> refererence <table>
- list:integer --> integer
- list:string --> string
... | 496e238c48ed72d0713b4b17a89b9133e3cef8e0 | 113,579 |
import yaml
def _yaml_to_dict(yaml_string):
"""
Converts a yaml string to dictionary
Args:
yaml_string: String containing YAML
Returns:
Dictionary containing the same object
"""
return yaml.safe_load(yaml_string) | c7de0c860028d17302cd4d07e20c3215503b977b | 4,444 |
def _stacktrace_beginning(stacktrace, size):
""" Gets the first `size` bytes of the stacktrace """
if len(stacktrace) <= size:
return stacktrace
return stacktrace[:size] | cfebf615dd129b89da77e1843437788659043a52 | 505,869 |
def transform_df_to_numbers(df):
"""
Subsitutes difficulties with numbers to work with them in a better way, from 1 to 3
:param df: Dataframe to transform to numbers to
:return transformed datafarme
"""
mapping = {'LOW': 1, 'MEDIUM': 2, 'HIGH': 3, 'undef': -1}
df = df.replace({'physDiffic... | c59e3aecb35b49b9a56681c31835872d8eab7317 | 509,857 |
def _translate_attachment_summary_view(_context, vol):
"""Maps keys for attachment summary view."""
d = {}
storage_pool_id = vol['id']
# NOTE(justinsb): We use the storage_pool id as the id of the attachment object
d['id'] = storage_pool_id
d['storage_pool_id'] = storage_pool_id
d['server_id'... | b5ed8754d544dd16cc325c7cc15b74c97c59e64a | 612,666 |
import typing
def hour_to_day(hour: int) -> typing.Tuple[int, int]:
"""Converts from a simulation hour to the pair (day, hour)."""
day = int(hour // 24)
hour = int(hour % 24)
return day, hour | 138bcf18020cf0c20c4d90303ffdaf9e518a7bfe | 256,475 |
import hashlib
def md5(data: str):
"""
generate md5 hash of utf-8 encoded string.
"""
return hashlib.md5(data.encode("utf-8")).hexdigest() | 3a7274c72228247e71b0e4668b42328506040d3c | 139,921 |
def tuple_to_string(tuple_in: tuple) -> str:
"""Converts an RGB tuple to a latex-xcolor compatible string"""
return ", ".join([f"{val:6.3f}" for val in tuple_in]) | adec820901c4c3fb86dc74e2a404f2bc03f5d7f3 | 343,036 |
def interpret_numbers(user_range):
"""
:param user_range: A string specifying a range of numbers. Eg.
interpret_numbers('4-6')==[4,5,6]
interpret_numbers('4,6')==[4,6]
interpret_numbers('4,6-9')==[4,6,7,8,9]
:return: A list of integers, or None if the input is not numberic
"""
... | dc3a156bdb392e8a54edf95fc4182dfd5965010a | 25,716 |
def one(s):
"""Get one element of a set"""
return next(iter(s)) | 45ce1607e5d4b6bf2fc53cfc2da5602ccdc83910 | 657,591 |
def eval_multiple(exprs,**kwargs):
"""Given a list of expressions, and keyword arguments that set variable
values, returns a list of the evaluations of the expressions.
This can leverage common subexpressions in exprs to speed up running times
compared to multiple eval() calls.
"""
for e in exp... | 2bc90dacb972d3315168638a4ea99f9cfbb13830 | 702,136 |
def kinase_families(klifs_metadata, kinase_group=None):
"""
Get all kinase families for a kinase group.
Parameters
----------
kinase_group : None or str
Kinase group name (default is None, i.e. all kinase groups are selected).
Returns
-------
list of str
Kinase family n... | 483c050c3f27dcd20c0ad90197c501b8fa2c64ce | 215,343 |
def get_isotopic_abundance_product(components):
"""
Estimates the abundance of a molecule based on the abundance of the isotopic
components.
Returns
-------
:class:`float`
Notes
------
This is essentially a simplistic activity model.
Isotopic abundances from periodictable are i... | 554db46ae3ba43fab780e7c6458efa79d1927697 | 119,425 |
def flatten_dict(data, parent_name="", sep=".", key_converter=None, skip_key_check=None):
"""
Flattens a dictionary to a single layer with child keys separated by `sep` charactor
Example:
input:
parent_name = "root"
data = {
"parent_obj": {
"child_obj": {
... | 4d171e842d04a94f5610d36798519450f9bd674b | 562,309 |
def make_message(options):
"""Returns formatted string describing `options` dict."""
return 'Options: ' + ', '.join('%s=%s' % (k, options[k]) for k in options) | a778ad4eeef6d0c7cc48160e21a91811077626ec | 219,392 |
def refix(val, p_in="", p_out=""):
"""
Convert between different SI unit prefixes. Available options are:
:code:`'T'` Terra
:code:`'G'` Giga
:code:`'M'` Mega
:code:`'k'` Kilo
:code:`'m'` Milli
:code:`'mu'` Micro
:code:`'n'` Nano
:code:`'p'` Pico
Parameters
---... | 2667ee7ef0df622bbb81285cbbb0a3f67be2d01d | 442,903 |
def write_outfile(lines_list,
filename,
writestyle = "x"):
"""
=================================================================================================
write_outfile(lines_list, filename, writestyle)
This function is meant to take a list of strings and a... | 7c4a113985de4ae85c67f3e1676b7bc55306869f | 423,787 |
def skipfile(filename):
"""Read a skip file, return a list of integers."""
with open(filename) as f:
data = f.readlines()
return map(int, map(str.strip, data)) | a51dad2f3ad7f2322f8ad8095eb32b4560f5814d | 542,890 |
def is_collection(v):
"""
Decide if a variable contains multiple values and therefore can be
iterated, discarding strings (single strings can also be iterated, but
shouldn't qualify)
"""
# The 2nd clause is superfluous in Python 2, but (maybe) not in Python 3
# Therefore we use 'str' instead... | e51ee293566e0be9f7143524abb055da0e35671e | 24,170 |
def is_method_of(method, obj):
"""Return True if *method* is a method of *obj*.
*method* should be a method on a class instance; *obj* should be an instance
of a class.
"""
# Check for both 'im_self' (Python < 3.0) and '__self__' (Python >= 3.0).
cls = obj.__class__
mainObj = getattr(method... | 554ab48effb7ce996846192786ce2141abf671a4 | 699,838 |
import click
def print_result(result):
"""
Print successful result to the terminal.
"""
return click.echo(result) | fb34e935ee4f501136d4837d41c8accc4d9b0c91 | 608,031 |
def parse_command(cmd_str):
"""
# the line has one word for the command and n pairs that go to key, value (separator is space)
:param cmd_str: string with name of command and pairs of params and values
:return: cmd : str (name of the command)
cmd_par: dictionary {par_name: str(par_value)} wi... | ac48d05bcd88c7eb5e04cedeb26c5d5278bbc3bd | 26,382 |
import json
def load_json_from_string(string):
"""Load schema from JSON string"""
try:
json_data = json.loads(string)
except ValueError as e:
raise ValueError('Given string is not valid JSON: {}'.format(e))
else:
return json_data | 66f96373a8e02bf69289e5e4594ac319906475f5 | 5,839 |
def check_read(read):
"""
Helper function to decide what reads should
be keep when parsing alignment file with `pysam`.
Parameters
----------
read : AlignedSegment
read from alignment file parsed with `pysam`.
Returns
-------
bool
True/False if read should be inclu... | d8cdec3eae4b3c85831b82c4f89eca1896bfe28b | 447,458 |
def create_headers_for_request(token):
"""Create a header dict to be passed to the api.
:param token: token string coming from the api
:return: a dict containing all the headers for a request
"""
return {
'X-Auth-Token': token,
'Content-Type': 'application/json',
'Accept': '... | c52e560125b00195d9811b00579281a75ecd8edf | 217,514 |
def coord2num(i,j,N):
"""
return number given coordinate
start indices at 0
"""
return (i+1)+N*j | efa489c8b72b2b00bcc92f92d8dd59b1fff3cd71 | 239,478 |
def _normalize_sizes(sizes):
"""
Checks whether all the sizes are either slices or not. Transforms
slices into their sizes.
"""
out = []
ns = 0
for size in sizes:
if isinstance(size, slice):
size = size.stop - size.start
ns += 1
else:
size... | 0ad4d2ffec0e80e435ec59226c815a5b1737de14 | 338,760 |
def readLinesFromFile(filename):
"""
Returns the read file as a list of strings.
Each element of the list represents a line.
On error it returns None.
"""
try:
with open(filename, "r") as input_file:
lines = input_file.readlines()
return lines
except EnvironmentE... | e80d17cbb1039fd8f87da5bedee28c470ba697f6 | 379,962 |
import random
def choose(population, weights=None, k=None):
"""
Chooses k times from the given population with an optional weighted probability.
:param population: the population to chose from
:param weights: the weights attached to each population element
:param k: the amount of times to chose
... | 4a78dde05dba4f9774ae64f0b85bd89e61204b89 | 32,477 |
def _has_attr(node, attr):
"""
Given a ``node`` and an ``attr``, check if the ``attr`` exists on the
``node``.
:param node: Node
:type node: :class:`~nuke.Node`
:param attr: UUID attribute
:type attr: str
"""
if attr in node.knobs():
return True
return False | 2e750ceb795c1e4803dddb43cd3a18f0150a25fb | 545,737 |
def number_to_digits(num):
"""Return sorted list of digits in number."""
return sorted(int(ch) for ch in str(num)) | cc09154db0f587da5042bc06236e793411a0c5ab | 634,048 |
from typing import Union
from pathlib import Path
from typing import Optional
from typing import List
def find_wav_files(path_to_dir: Union[Path, str]) -> Optional[List[Path]]:
"""Find all wav files in the directory and its subtree.
Args:
path_to_dir: Path top directory.
Returns:
List con... | 3a780eb6fffd750a1aa7425578b98f61e38ffb6c | 522,219 |
import six
import networkx as nx
def nx_ascii_tree(graph, key=None):
"""
Creates an printable ascii representation of a directed tree / forest.
Args:
graph (nx.DiGraph): each node has at most one parent (
i.e. graph must be a directed forest)
key (str): if specified, uses this... | c0e8146f13216714d873e6cab91e0e57ad6e57dd | 529,672 |
import random
def random_bits(n: int) -> str:
"""
Returns a binary string with `n` bits.
Args:
n (int): Number of bits.
Returns:
bits (str): Binary string of random bits.
"""
return f"0b{''.join([str(random.randint(0, 1)) for _ in range(n)])}" | 344b7b66c534cb28cb17927646f82872b258edc3 | 526,563 |
def sextractor_output(fname):
"""SExtractor detection FITS table name from FITS image name"""
return fname.replace(".fits",".fits.stars") | 5001f163a64531a7c005f204878ae58c37c27595 | 87,718 |
def n_coeffs_from_ell_max(ell_max):
"""Returns the number of coefficients for an SWSFT with max degree ell_max."""
return (ell_max + 1)**2 | 2c173b7f0c2365ddde5136fa3d3984468e0339bf | 667,831 |
def know(possible_dates):
"""A person knows the birthdate if they have exactly one possible
date."""
return len(possible_dates) == 1 | 53c445b1579b9a6ea72a0428a7b1c6b820e34e8c | 575,826 |
def setattr_validate_property(traito, traitd, obj, name, value):
"""Validates then assigns a value to a specified property trait attribute"""
validated = traitd.c_attrs.validate(traitd, obj, name, value)
result = traitd.c_attrs.post_setattr(traito, traitd, obj, name, validated)
return result | bfed5c7f6ce6e02c25c297d9abf2c107e98f501b | 129,198 |
def copy_vals(params):
"""Save the values and stderrs of params in a temporary dict."""
tmp_params = {}
for para_key in params:
tmp_params[para_key] = (params[para_key].value,
params[para_key].stderr)
return tmp_params | 9b4cd635d861840046584fb3feb3f060a0a39030 | 528,642 |
import math
def sizeof_fmt(size, suffix='B'):
"""
Return a human-readable string representation of a filesize
Arguments:
size -- size in bytes
"""
try:
size = int(size)
except ValueError:
return None
if size <= 0:
return '0 %s' % suffix
size_name = ('', 'K... | ba8da544c29296e55aca30787026cb519bdbe75e | 575,739 |
def basic_sequence(piece_dict):
"""
Create a basic sequence with the given pieces.
Keyword arguments:
piece_dict -- The pieces dictionary with the quantity of each piece.
"""
sequence = []
# Iterate over the pieces original dict
for piece, number in piece_dict.items():
if nu... | df95990188b8b24275bd0608645c7efcfa0aa8f9 | 588,118 |
from typing import List
def max_crossing_sum(lst: List[int], mid: int, n: int) -> int:
"""
Parameter <mid> is the floor middle index of <lst>.
Parameter <n> is the length of the input list <lst>.
Pre: <lst> is a list of integers and len(lst) >= 2.
Post: returns the maximum contiguous crossing sum ... | 3d873907cb7ed0c14152ec3c2e92a742bd52aa85 | 1,820 |
def sortable(obj):
"""Returns True if *obj* is sortable else returns False."""
try:
sorted([obj, obj])
return True
except TypeError:
return False | f6df95acb95a15ab57d19c275e0a0e3fb2479ede | 192,645 |
def rounder(x, ndigits):
"""Round a number, or sequence of numbers, to a specified number of decimal digits
Args:
x (None, float, complex, list): The number or sequence of numbers to be rounded. If the
argument is None, then None will be returned.
ndigits (int): The number of decima... | 17d02f934e0ac62070010a3524885b5cb25afb36 | 619,758 |
def build_connections(movies, participants, role, threshold):
"""
Build connections between movies and participants (cast and directors).
The `role` filed in the `attributes` will be constructed with corresponding
pointers, and the nodes that are not connected to any movie will be removed.
Parameters
-----... | 601606f4c45a404c0bcbc6fcb56a7be9e7d2f495 | 569,325 |
def varchar(length):
"""
Factory for a character length validator of the specified length.
"""
length = int(length)
def char_length_validator(string):
"""
Validate a string ensuring that it doesn't exceed a maximum length.
"""
if string is None:
return
... | a0d498c26528f1f0e7156a7b2fda632db1d65682 | 52,064 |
def _listify(element):
""" Ensures elements are contained in a list """
# either it is a list already, or it is None
if type(element)==type([]) or element==None:
return element
else:
return [element] | e4f5fac2af9a23e4595db4a4c01f2108beb07ef6 | 387,280 |
def pythag(x, y, find=0):
"""
Pythogream Theorem.
cos**2 + adj**2 = hyp**2
x= Adjacent y= Opposite , find hypotneuse.
:param x:
:param y:
:param find:
:return: <float>
"""
if not isinstance(x, float) and not isinstance(x, int):
raise ValueError("Please input only float/ ... | 51df31b44c08914e4c96c844af0b7007ce53c65f | 383,569 |
def get_object(o,
names
):
"""
Command used to get the object o.name1.name2.name3 where name1, name2, name3 are provided in `names`
It is located here so that it can be pickled and sent over the wire
:param o:
:param names:
:return:
"""
result = o
for n... | 5a47556e2a549962b8c32ea365d5cc6d9ad25215 | 259,435 |
def clip(x, min_val, max_val):
"""
Clips x between min_ and max_val.
Args:
x (float): The input to be clipped.
min_val (float): The min value for x.
max_val (float): The max value for x.
Returns:
float: The clipped value.
"""
return max(min_val, min(x, max_val)) | 9060f990e89eef1ce40b5b8e97bcf465c8f28ef2 | 460,224 |
def get_words_from_string(line):
"""
Return a list of the words in the given input string,
converting each word to lower-case.
Input: line (a string)
Output: a list of strings
(each string is a sequence of alphanumeric characters)
"""
word_list = [] # accumulates wo... | b00825a5801b7fe92c58f1caeb6f2d1e02d3a9d2 | 447,127 |
def watt_hours(watts, hours):
"""Multiplies watts times the hours used"""
return watts * hours | 38e367ef25754fa47212b2459571fa6c3e6625d2 | 599,111 |
import torch
def simple_inter(box1, box2):
"""
Simple intersection among bounding boxes.
:param box1: bounding boxes coordinates.
:param box2: bounding boxes coordinates.
:return: intersection among pair of bounding boxes.
"""
top_left_i = torch.max(box1[..., :2], box2[..., :2])
bot_ri... | 3efe0232849ee827ca9af2f3bb9dc2675ba07e33 | 424,157 |
def get_inputs( filename ):
"""
The input file contains rules and messages separated by a blank line.
"""
with open( filename, 'r') as input_file:
raw_data = input_file.read().split('\n\n')
rules = raw_data[0].splitlines()
messages = raw_data[1].splitlines()
return rules, messages | cd277aac4928413d77af6f842644f929b736a027 | 176,296 |
def remove_empty_parameters(data):
"""Accepts a dictionary and returns a dict with only the key, values where the values are not None."""
return {key: value for key, value in data.items() if value is not None} | 4448556ffd3fe20a651986396c6b724ce6157f4f | 632,716 |
def _intersect(rect1, rect2):
"""
Check whether two rectangles intersect.
:param rect1, rect2: a rectangle represented with a turple(x,y,w,h,approxPoly_corner_count)
:return whether the two rectangles intersect
"""
# check x
x_intersect = False
if rect1[0] <= rect2[0] and rect2[0] - rect... | 24892f225ff2794e8f1f37714ba69c724dec4651 | 87,706 |
from typing import List
def find(arr: List[int], key: int) -> int:
"""Modified version of binary search: one half of the array must be already sorted.
Therefore check the sorted half to decide whether to search the left or right half.
This takes O(lg n) time and O(1) space.
"""
lo = 0
hi = len... | b4a49cc73009b27344fc403a79c26130339b5d04 | 644,082 |
def createFile(name, paramToValue, numberOfCycles=1):
"""Creates a dictionary storing file information"""
File = {'name': name,
'paramToValue': paramToValue,
'numberOfCycles': numberOfCycles}
return File | 55294fb81152977ff0262c78888fb08986234a0a | 658,451 |
def __convert_sec_to_time(seconds):
"""Convert sec to time format"""
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return hour, minutes, seconds | 01f859b8b622b6bb403ffa79b2a7688b39bc07be | 263,677 |
def generate_sample_fov_tiling_entry(coord, name):
"""Generates a sample fov entry to put in a sample fovs list for tiling
Args:
coord (tuple):
Defines the starting x and y point for the fov
name (str):
Defines the name of the fov
Returns:
dict:
... | c8e2e0886f9e5e67398726053a977c32b33cbcc7 | 287,828 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.