content stringlengths 39 14.9k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
def lower_column(col):
""" Return column from lower level tables if possible
>>> metadata = sa.MetaData()
>>> s = sa.Table('accounts', metadata,
... sa.Column('name', sa.String),
... sa.Column('amount', sa.Integer),
... sa.Column('id', sa.Integer, primary... | c204ab8dcf63abd50ff0213f1f72043b0102f43d | 687,675 |
def tuple_replace(tup, *pairs):
"""Return a copy of a tuple with some elements replaced.
:param tup: The tuple to be copied.
:param pairs: Any number of (index, value) tuples where index is the index
of the item to replace and value is the new value of the item.
"""
tuple_list = list(tup)
... | ec4c52d3257bd33b31a45e3d298aa934436efb4a | 687,680 |
import re
def separate_words(name):
"""Convenience function for inserting spaces into CamelCase names."""
return re.sub(r"(.)([A-Z])", r"\1 \2", name) | a2c2db19d9eddf94edd846f0752ca237cb99e441 | 687,685 |
def first_is_not(l, v):
"""
Return first item in list which is not the specified value.
If all items are the specified value, return it.
Parameters
----------
l : sequence
The list of elements to be inspected.
v : object
The value not to be matched.
Example:
-------... | c514e22338060a71c46fd1cc39ae7f9f273de158 | 687,686 |
def default_str(str_, default_str):
"""Returns :attr:`str_` if it is not `None` or empty, otherwise returns
:attr:`default_str`.
Args:
str_: A string.
default_str: A string.
Returns:
Either :attr:`str_` or :attr:`default_str`.
"""
if str_ is not None and str_ != "":
... | c0277b5a91a26c0fdb01c98d0e91b3a9c7604285 | 687,687 |
def _get_morphometry_data_suffix_for_surface(surf):
"""
Determine FreeSurfer surface representation string.
Determine the substring representing the given surface in a FreeSurfer output curv file. For FreeSurfer's default surface 'white', the surface is not represented in the output file name pattern. For ... | fcc9a4611bd056a7159d88e5b8706efa40e2ab67 | 687,688 |
import math
def format_duration(seconds: float) -> str:
"""Formats duration in seconds into hours/minutes/seconds."""
if seconds < 60:
return f'{seconds:.0f}s'
elif seconds < 3600:
minutes = math.floor(seconds / 60)
seconds -= minutes * 60
return f'{minutes}m{seconds:.0f}s'
else:
hours = m... | 0d14b1dcf389501c21ea1f14f0808b926a952c44 | 687,690 |
def save_as(user):
"""
Returns a callback which saves the model as the user that you
pass in
"""
def user_save(instance):
instance.save(user)
return user_save | e86e29369e2306eee3189b32db87457c13ace8b5 | 687,698 |
def get_test_count(test_data):
"""
Args:
test_data: json of test data
Returns:
int: total test count
"""
return int(test_data.get("testsuites").get("testsuite").get("@tests")) | a65bb6bdccd789609b1ce25faf2740f1de6b1f78 | 687,699 |
def map_diameter(c: int) -> float:
""" Compute the diameter """
return 1 / 3 * (c + 1) * (c - 1) | fb3f19478901f40b52af11c6d200f125d3716112 | 687,700 |
def label_set_match(object_labels, returned_labels):
"""Return True if at least one of the object labels is contained in at least one of the returned labels"""
for o in object_labels:
for r in returned_labels:
if o.lower() in r.lower():
return True
return False | 86e0b1e73fb9c370fc8a25a89dca4476abd2f3b2 | 687,705 |
def chain_value(row, attribute_ids):
"""
Join all the values of attributes to get a identifier
:param row: a row of the table, e.g. a list of attribute values
:param attribute_ids: a set of attribute
:return: a string consists of joint value
"""
result = []
for attribute_id in sorted(att... | 22c303f9286ae46cc96eccb556178edd2e5d180e | 687,710 |
def shared_data_volume_container_path(sdv, sdvkey):
# type: (dict, str) -> str
"""Get shared data volume container path
:param dict sdv: shared_data_volume configuration object
:param str sdvkey: key to sdv
:rtype: str
:return: container path
"""
return sdv[sdvkey]['container_path'] | e9a3455a71f6862039ab092ccc5ede46a58c71f4 | 687,711 |
def isYes(string):
"""Returns True if the string represents a yes, False, if it represents
no, and another string if it represents something else"""
value = string.strip().lower()
if value in ['yes', 'always', 'on', 'true']:
return True
if value in ['no', 'never', 'off', 'false', 'null']:
... | 3c8d5e54daf02fe5add3202cb4d3d0942a43616e | 687,715 |
def _add_new_line_if_none(s: str):
"""Since graphviz 0.18, need to have a newline in body lines.
This util is there to address that, adding newlines to body lines
when missing."""
if s and s[-1] != "\n":
return s + "\n"
return s | aed3582be53cf403601a125cebc436041257a0d9 | 687,716 |
def build_from_cfg(name, cfg, registry, default_args=None):
"""Build a module from config dict.
Args:
name (str): Name of the object
cfg (addict): Config dict of the object
registry (:obj:`Registry`): The registry to search the type from.
default_args (dict, optional): Default i... | 8eb9b9717764ae1c1ca121a59b466ef7c2d72f70 | 687,718 |
import glob
def getFeatures(dirSrc, extFilesSrc, lsGenres):
""" Reads samples in form of multiple text files and returns a pair of
feature matrix X and target vector y.
dirSrc: Absolute path to directory of source text files
extFilesSrc: File extension of source text files (e.g. "txt")
... | 47b0ddbe36e34788ba3eed20aebc8852a36e8245 | 687,719 |
import torch
def truncated_normal(size, std):
"""
Pytorch does not have a truncated normal function to we manually make one
in order to cut the dependancy on tensorflow
Modified Version of: https://discuss.pytorch.org/t/implementing-truncated-normal-initializer/4778/20
"""
mean = 0
tensor ... | 0064f5a5c97809072b9493f823667d4036921d50 | 687,723 |
def compare_equal(compare_data): # pylint: disable=redefined-outer-name
"""
Returns a function which checks that a given data is equal to the stored reference.
"""
return lambda data, tag=None: compare_data(lambda x, y: x == y, data, tag) | 45215759bb4aac2a9fe304e75c40e3ff70acd787 | 687,725 |
def type_validator(property_type):
"""Create validator that requires specific type.
Args:
property_type: The type of the validator.
Returns:
Validator that only accepts values of a specific type.
"""
def type_validator_impl(value):
if not isinstance(value, property_type):
raise TypeError('... | 8ba043af22a133ed5bd613bf7a926912d4053275 | 687,726 |
def prod(lst):
"""Product of list elements."""
if len(lst) == 0:
return 0
x = lst[0]
for v in lst[1:]:
x *= v
return x | 156de3b3b9689460a4a84bcd4e868a2888299d09 | 687,730 |
def get_agent_location_from_maze_string(mazeString=str):
"""[Get the location of the agent from the maze]
Args:
mazeString ([str], optional): [string of the maze location]. Defaults to str.
Returns:
[tuple]: [location of the maze]
"""
mazeString = [list(x.strip()) for x in mazeStri... | 62e15c2d4b90283649fa25cc4b07d657eed2ea8b | 687,731 |
def decompose_path(path):
"""
Break a path down into individual parts
Parameters
----------
path : string
Path to variable on the
Returns
-------
structure : tuple of strings
Tuple of split apart path
"""
return tuple((path_entry for path_entry in path.split('/')... | 09d33183fbd4020f85bfc2e178dc387064262bde | 687,732 |
from typing import OrderedDict
def groupby(items, by):
"""
Group items using a function to derive a key.
:param items:
The items to group
:param by:
A lambda function to create a key based on the item
:return:
an Ordered dict
"""
result = OrderedDict()
for it... | 21b10c5fb4ba795cd3a43f380c8cdb823b3c2bfe | 687,734 |
def check_textbound_overlap(anns):
"""
Checks for overlap between the given TextBoundAnnotations.
Returns a list of pairs of overlapping annotations.
"""
overlapping = []
for a1 in anns:
for a2 in anns:
if a1 is a2:
continue
if a2.start < a1.end a... | e10618fc2cc1642ffd295ed59f939a120094254a | 687,735 |
def det3x3(matrix):
"""
Calculate a determinant of a 3x3 matrix. Should be usually substituted
by `numpy.linalg.det`, but is indispensable for matrices with uncertainties.
:param matrix: 3x3 array/matrix which allows for 2d indexing
:type matrix: numpy.ndarray or uncertainties.unumpy.matrix
:re... | cf212c2e8710a2f4c60b091eede77b5b1c8b78b7 | 687,736 |
def get_counts(filename):
"""
reads the .counts_edit file and extracts the counts
:param filename: counts file (original or simulated)
:return: list of counts
"""
with open(filename, "r") as counts_file:
counts = []
for line in counts_file:
line = line.strip()
... | 2bb9914fbca82550e121f0dcb5c55f6817da6c91 | 687,739 |
def _get_human_bytes(num_bytes):
"""Return the given bytes as a human friendly KB, MB, GB, or TB string
thanks https://stackoverflow.com/questions/12523586/python-format-size-application-converting-b-to-kb-mb-gb-tb
"""
num_bytes = float(num_bytes)
one_kb = float(1024)
one_mb = float(one_kb ** 2)... | b48a74b619734df839db752a247d5e6fcd6da05e | 687,745 |
import re
def get_brute(brute_file, mini=1, maxi=63, banned='[^a-z0-9_-]'):
"""
Generates a list of brute-force words based on length and allowed chars
"""
# Read the brute force file into memory
with open(brute_file, encoding="utf8", errors="ignore") as infile:
names = infile.read().split... | 57ace4ffcb311c232e9c275e9e033d155beb4316 | 687,748 |
def get_post_from_row(post_row):
"""
extracts the post body from a post row
"""
return post_row.find("td").find("div", class_="content") | e2e75320d4b801cd76da3027b958ccced28f34ba | 687,749 |
def get_status(query_type, command, execution_status, status_dict):
""" Get the status code of a command according to a query
Args:
query_type (str): Type of status query, chunk in this case
command (str): Command name
execution_status: State of the last given command execution
... | 78c28790a2824696e54f1556c9fb32e6ee972e47 | 687,751 |
def get_all_individuals_to_be_vaccinated_by_area(
vaccinated_compartments, non_vaccination_state, virus_states, area
):
"""Get sum of all names of species that have to be vaccinated.
Parameters
----------
vaccinated_compartments : list of strings
List of compartments from which individuals ... | 45ba235d14df08455304ce20d7b39e590b93e872 | 687,753 |
def get_lt(hdr):
"""Obtain the LTV and LTM keyword values.
Note that this returns the values just as read from the header,
which means in particular that the LTV values are for one-indexed
pixel coordinates.
LTM keywords are the diagonal elements of MWCS linear
transformation matrix, while LTV... | 8bbe18f09c82c0273ac177b5b1090bd8b490dd8e | 687,754 |
import pathlib
def data_file(module, *comps):
"""Return Path object of file in the data directory of an app."""
return pathlib.Path(module.__file__).parent.joinpath('..', 'data', *comps) | 1ee38b920eacb1ac90ee260c73242fdf5d7db98f | 687,755 |
def depend_on_proj_props(target, source, env):
""" Emitter which adds a dependency for the project properties file """
#sys.stderr.write("depend_on_proj_props called\n")
#sys.stderr.flush()
return (target, source + [env['XISE_PY_PROPFILE']]) | f0ea4c5aa0a6958e71dd051a6aff08cf9318d136 | 687,756 |
from bs4 import BeautifulSoup
from typing import List
from typing import Dict
import re
def parse_row(row: BeautifulSoup, columns: List[str], table_id: str) -> Dict[str, str]:
"""Takes a BeautifulSoup tag corresponding to a single row in an HTML table as input,
along with an ordered list of normalized column ... | 06dd895243f614f2a0d6fb3b40fc857ae50274ae | 687,758 |
def ses(count, plural='s', singular=''):
"""
ses is pronounced "esses".
Return a string suffix that indicates a singular sense if
the count is 1, and a plural sense otherwise.
So, for example:
log.info("%d item%s found", items, utils.ses(items))
would log:
... | f0fe352df642ba442e620bbc5aefee613747588b | 687,761 |
import math
def fpart(x):
"""Returns the fractional part of x."""
return x - math.floor(x) | 022ef9a11356e24a9cfb218640b67ff2b11fe58a | 687,769 |
def host_dict(host):
"""Convert a host model object to a result dict"""
if host:
return host.state
else:
return {} | 5cbc0471f87deb2214f1649fba23b093641f84d9 | 687,770 |
def get_total_results(results):
"""Return the totalResults object from a Google Analytics API request.
:param results: Google Analytics API results set
:return: Number of results
"""
if results['totalResults']:
return results['totalResults'] | 3cc5604017ebb9eab98f2f99f1045c13cd1402f1 | 687,771 |
from typing import List
def _format_results(results: list) -> List[dict]:
"""
Args:
results: a list of results with the following format:
['"<hash>","<anchor_hash>",<score>']
Returns: a list of dictionaries containing the hash and score
"""
formatted_res = []
# Remove last e... | 2ce02ae0a5181c94a89b209735c1bba80deeae76 | 687,772 |
def textarea(
rows="",
span=2,
placeholder="",
htmlId=False,
inlineHelpText=False,
blockHelpText=False,
focusedInputText=False,
required=False,
disabled=False,
prepopulate=False):
"""
*Generate a textarea - TBS style*
**Key Arg... | 50956f0296e275385acc2e12ea063074c32887f9 | 687,774 |
def _construct_target_subscription(_target_id, _target_type='board'):
"""This function constructs a dictionary for an individual subscription target to be used in a payload.
.. versionadded:: 3.5.0
:param _target_id: The unique identifier for the target (e.g. Node ID)
:type _target_id: str
:param ... | df66023aedc3306c8ffaa9f9dbccdb3811fc660b | 687,775 |
def monkey_patch(cls):
""" Return a method decorator to monkey-patch the given class. """
def decorate(func):
name = func.__name__
func.super = getattr(cls, name, None)
setattr(cls, name, func)
return func
return decorate | a9fa473ab70e609740750e9347d61d1184689159 | 687,777 |
def pil_to_flatten_data(img):
"""
Convert data from [(R1, G1, B1, A1), (R2, G2, B2, A2)] to [R1, G1, B1, A1, R2, G2, B2, A2]
"""
return [x for p in img.convert('RGBA').getdata() for x in p] | 3c6dbd84a5f664e2301c72f85fd19ecd0824ecaa | 687,778 |
def count_numeric(df):
"""
Returns the number of numeric variables in the dataset.
Parameters:
df (pandas DataFrame): Dataset to perform calculation on
Returns:
counter_numeric (int): Number of numeric variables in df
"""
counter_numeric = 0
for i in range(len... | eb31507163a3193ae1c67ed282f344aad50b2b22 | 687,782 |
def bahai_major(date):
"""Return 'major' element of a Bahai date, date."""
return date[0] | 248c0d317db55f2ad1148b91a2058cac66da664f | 687,783 |
def check_alpha_signs(svm):
"""Returns the set of training points that violate either condition:
* all non-support-vector training points have alpha = 0
* all support vectors have alpha > 0
Assumes that the SVM has support vectors assigned, and that all training
points have alpha values assi... | ab3868cc5b4f954d6c6158e804d19c95834704a9 | 687,786 |
import random
def luck(n=2):
""" gives 1 chance out of n (default: 2) to return True """
assert n > 1
return bool(random.randint(0, n-1)) | 1d0990ca1586d865e8da57ec4fff8715738c8b90 | 687,788 |
def convert_dict(my_dict):
"""Convert dictionaries from Netmiko format to NAPALM format."""
new_dict = {}
for k, v in my_dict.items():
new_dict[k] = v
hostname = new_dict.pop('host')
new_dict['hostname'] = hostname
device_type = new_dict.pop('device_type')
new_device_type = device_... | 13431c74534199ee73ed7a75ae34885872495f91 | 687,789 |
def bspline(p, j, x):
"""
Return the value at x in [0,1[ of the B-spline with integer nodes of degree p with support starting at j.
Implemented recursively using the [De Boor's Algorithm](https://en.wikipedia.org/wiki/De_Boor%27s_algorithm)
.. math::
B_{i,0}(x) := \left\{
\begin{matrix... | b634c3e09936074e50c919aa9bfad0b5c6b00411 | 687,790 |
def dict_partial_from_keys(keys):
"""Return a function that constructs a dict with predetermined keys."""
def dict_partial(values):
return dict(zip(keys, values))
return dict_partial | b738face4fa1874ab8ad1072c84afcde6f63b239 | 687,792 |
def create_accounttax_sample(account_id, **overwrites):
"""Creates a sample accounttax resource object for the accounttax samples.
Args:
account_id: int, Merchant Center ID these tax settings are for.
**overwrites: dictionary, a set of accounttax attributes to overwrite
Returns:
A new accountt... | e3a105b2ee2c798d80746104ef242f5c4b3e53c0 | 687,793 |
import shlex
def split_args(line):
"""Version of shlex.split that silently accept incomplete strings.
Parameters
----------
line : str
The string to split
Returns
-------
[str]
The line split in separated arguments
"""
lex = shlex.shlex(line, posix=True)
lex.w... | 19837bf875d108f96349f848f6f3605795469a40 | 687,794 |
def delete_cluster(dataproc, project, region, cluster):
"""Delete the cluster."""
print('Tearing down cluster.')
result = dataproc.delete_cluster(
project_id=project, region=region, cluster_name=cluster)
return result | e5a1a2b97b36e2860de508aa8daedb7928f044e6 | 687,797 |
def ReshapeShortFat(original):
"""
ReshapeShortFat(original)
Reshapes the image numpy array from original shape
to the ShortFat single row shape and captures
shape info in the output:
channel_0, channel_1, channel_2
functionally performs original.reshape(1, x*y*z) to create single... | a1f02dfb3e675011bf08bf8261355217df25006d | 687,799 |
def get_class_prob(predictions_dict):
"""Get true and predicted targets from predictions_dict.
Parameters
----------
predictions_dict : dict
Dict of model predictions. Must contain "target_true" and "target_pred" keys with corresponding
dict values of the form class_label : probability.... | d5876043d1bb2fcea25bf1892fd0b35f07e98db9 | 687,800 |
def generate_video_url(video_id:str)->str:
"""Takes video Id and generates the video url
example https://www.youtube.com/watch?v=e3LqeN0e0as
"""
return f'https://www.youtube.com/watch?v={video_id}' | f48759a223ac831c46d51e4c56ce098de54425b5 | 687,801 |
import base64
def base64url_encode(msg):
"""
Encode a message to base64 based on JWT spec, Appendix B.
"Notes on implementing base64url encoding without padding"
"""
normalb64 = base64.urlsafe_b64encode(msg)
return normalb64.rstrip(b'=') | 315b0b0e5041da30c3bfd97de1759f8de8cd4ea3 | 687,803 |
def num_parameters(model):
"""
Returns the number of parameters in the given model
Parameters
----------
model : torch.nn.Module
the model to count the parameters of
Returns
-------
int
the number of parameters in the given model
"""
n = 0
for p in model.pa... | cb4a2fe4f383c0765f560a1fbc8e7cc404b1eca0 | 687,805 |
def simplex_dimension(simplex):
"""
Get the dimension of a simplex.
:param simplex: Simplex defined by a list of vertex indices.
:type simplex: List[int]
:return: Dimension of the simplex.
"""
return len(simplex) - 1 | 4aef0e6d1af41cc6e1d4e1977625ec1f0476c2b9 | 687,807 |
def ssXXsuffix( i ):
"""Turns an integer into an ssXX ending between .ss01 and .ss20, e.g. 5 -> '.ss05'."""
i = i%21 # max 20
if not i: # if 0
i = 1
return ".calt.ss%.2d" % ( i ) | 3c2961e8e085c86dcc0cb7b950d1a4fe94de9ba8 | 687,810 |
def decode_rgb565(val):
"""Decode a RGB565 uint16 into a RGB888 tuple."""
r5 = (val & 0xf800) >> 11
g6 = (val & 0x7e0) >> 5
b5 = val & 0x1f
return (
int((r5 * 255 + 15) / 31),
int((g6 * 255 + 31) / 63),
int((b5 * 255 + 15) / 31)
) | 0c9a67016df686eb23282de74f663493caa305a9 | 687,811 |
from typing import OrderedDict
def quant_params_vec2dict(keys, vals, search_clipping=False):
"""
Convert the vector(s) created by quant_params_dict2vec to a dictionary of quantization parameters that
the post-training quantizer API can digest
"""
res = OrderedDict()
for idx, k in enumerate(key... | facdcaea5ae0504017d51a7d61c682322f70d23f | 687,812 |
from bs4 import BeautifulSoup
def clean_data(data):
"""
Strip HTML and clean spaces
"""
data = BeautifulSoup(data, "lxml").text.strip()
data = ' '.join(data.split()).encode("utf-8")
return data | 78034485428aaf8e2d70f02fa3d3011332bea407 | 687,815 |
import pkg_resources
def get_template(name):
"""
Look for 'name' in the vr.runners.templates folder. Return its contents.
>>> import six
>>> tmpl = get_template('base_image.lxc')
>>> isinstance(tmpl, six.string_types)
True
"""
path = 'templates/' + name
b_stream = pkg_resources.r... | 75d95692b9f9e386c219650165256462ec736762 | 687,818 |
def service_exists(keystone, service_name):
""" Return True if service already exists"""
return service_name in [x.name for x in keystone.services.list()] | f885e8a3df9b9f20f39a97ae1a481eed26694bc4 | 687,819 |
def material_type(rec):
"""Determine material type for record (arg1).
Returns:
A string, one of BK (books), CF (computer files), MP
(maps), MU (music), CR (continuing resource), VM (visual
materials), MX (mixed materials)
"""
l = rec[0]
# Book: Leader/06 (Type of record) co... | 21f6ae1ca85c7db62f4d6d868ffc32a1adce3197 | 687,831 |
def cookies_string_to_dict(cookies_string):
"""
Transform cookies which is type of string to the type of dict
"""
if not cookies_string or cookies_string == '':
raise ValueError("Invalid blank param of cookies_string !")
if not isinstance(cookies_string, str):
raise TypeError("Inval... | 300fdd5adb189ac025727bfe2d08947aac87dac4 | 687,834 |
def classname(cls):
"""Return the name of a class"""
return cls.__name__ | d907d4c78e4300dbc0ba0a185fd9b5ca85f11511 | 687,839 |
def normalize_lang(lang):
"""Normalize input languages string
>>> from pyams_utils.i18n import normalize_lang
>>> lang = 'fr,en_US ; q=0.9, en-GB ; q=0.8, en ; q=0.7'
>>> normalize_lang(lang)
'fr,en-us;q=0.9,en-gb;q=0.8,en;q=0.7'
"""
return lang.strip() \
.lower() \
... | d3ce6b111bdf5cad98f643955631dd9389a6d849 | 687,840 |
def doi_to_directory(doi):
"""Converts a doi string to a more directory-friendly name
Parameters
----------
doi : string
doi
Returns
-------
doi : string
doi with "/" and ":" replaced by "-" and "-" respectively
"""
return doi.replace("/", "-").replace(":", "-") | 3bc20f48fbc1048da0324a5053561c685ae66b98 | 687,845 |
def get_soil_texture_superclass_id(superclass: str):
"""Get soil texture superclass ID.
Parameters
----------
superclass : str
Superclass from {L, S, T, U}.
Returns
-------
int
ID of superclass
"""
superclass_dict = {"L": 0, "S": 1, "T": 2, "U": 3}
return super... | 0fa642ed21ae0d46374fbb408e0640e34ff823f5 | 687,848 |
from datetime import datetime
def write_fn(period: datetime = datetime.now(), prefix: str = 'xbrlrss', ext: str = '.xml') -> str:
"""Write the filename with pattern prefix-YYYY-MM-.xml
Args:
period (datetime, optional): Date from which year and month come. Defaults to datetime.now().
prefix (... | 02df0ac8300fb6adf06c0b6b10daa993f8cc57bc | 687,855 |
def fullName(first: str, middle: str, last: str) -> str:
"""
Compose parts of a name into a full name.
"""
if middle:
return f"{first} {middle}. {last}"
else:
return f"{first} {last}" | 1cd1fb8fedbbe090794cb1571fc47c62923d1f56 | 687,857 |
from typing import List
from typing import Tuple
def write_inertia(inertia: List[Tuple[int, int]]):
"""
Given an inertia decomposition as returned by `compute_inertia`, create the string
version that is latex displayable.
Example:
>>> write_inertia([(2, 1), (2, 3), (1, 2), (1, 1)])
'(... | ceb62482209ce7a68cade8f7c673c52594f68dcc | 687,862 |
def xyz_to_zyx(data, x=0, y=0, z=0):
"""
Reverses dimensions of matrix.
Assumes data is not flattened. If it is flattened, pass in y, x, z parameters
:param data:
:param y:
:param x:
:param z:
"""
x = data.shape[0] if x == 0 else x
y = data.shape[1] if y == 0 else y
z... | 508000b8a76917170269e98392b3a88b0ba9ecd9 | 687,863 |
def lowercase(raw_text: str) -> str:
"""
>>> lowercase("This is NeW YoRk wIth upPer letters")
'this is new york with upper letters'
"""
return raw_text.lower() | d486c8c6d1f15cc80038bd67994067464cdbbda5 | 687,864 |
def create_obj(d):
"""Create an object from a dict"""
return type('D', (object,), d)() | 5a95b32ed5f7e7b47919da3da590d13c80901d91 | 687,866 |
def get_option(name,project,default=None):
"""
Get an option flag from the project structure, returning None if
the flag is not defined.
"""
if 'options' not in project:
return default
if name not in project['options']:
return default
return project['options'][name] | 6e59ca528c5cdfaf0a1fb126d91df663686c8a06 | 687,867 |
import re
def read_segmentation(word_boundary_path):
"""
Args:
word_boundary_paths (list): list of paths to word boundary files
Returns:
segmentation_dict (dict):
key (string): utt_index
value (list): list of [start_frame, end_frame, word]
"""
segmentation_d... | 3f13c702e457bacc6d470d0529269339a0247f8d | 687,868 |
import re
def fixSlashes(url):
"""
Removes double slashes in URLs, and ensures the protocol is doubleslahed
"""
# insert missing protocol slashes
p = re.compile( '(:/)([^/])' )
url = p.subn( r'\1/\2', url)[0]
# strip any double slashes excluding ://
p = re.compile( '([^:])//' )
... | 8213ff86e85d49a829f4eff8627a97c33e984a9e | 687,871 |
import secrets
import binascii
def generate_consul_key(unique=False):
"""
Generate a consul key.
https://www.consul.io/docs/security/encryption
Key generated per the following description:
https://github.com/hashicorp/consul/blob/b3292d13fb8bbc8b14b2a1e2bbae29c6e105b8f4/command/keygen/keygen.go
... | 645a5a37fe7a9ceb46b60fd209ded7f0a9c8222a | 687,873 |
def find_index(token, low, high, features): # binary search to find element index in list
"""
# Binary search helper method
# helps finding token index
# with O(log n) time whereas
# np.where() will need O(n)
"""
if high >= low:
mid = int((high + low) / 2)
if features[mid] ... | 181443fa1a95419c28e357b646ecf98e00cdeeaf | 687,875 |
def _calc_target_shape(target_range):
"""Returns the shape of the target image."""
return tuple((target_range[:, 1] - target_range[:, 0]).astype(int)) | b62c079653cd3ed4fd16eee09179b72d2ce6e2a8 | 687,876 |
def _convert(x):
"""Generically convert strings to numbers.
:param x: string that maybe represents a number
:returns: value
:rtype: string, float, or int
"""
try:
return int(x)
except ValueError:
try:
return float(x)
except ValueError:
return... | 14ac5fdee202adabc8aac927bac9a36ca0b3d806 | 687,878 |
def is_imported_from_same_module(the_class: str, imported_name: str) -> bool:
"""
Is the class imported from another module?
:param the_class: the class object itself
:param imported_name: name of the imported class
:return: true if the class was imported from another module
"""
return ".".... | 713ddc8b012c70c6261c91f5224b83788c951455 | 687,880 |
def checkPrefix(s, prefix):
"""
Return a pair (hasPrefix, rest).
If prefix is a prefix of s:
hasPrefix is True
rest is everything after the prefix
Else:
hasPrefix is False
rest is s
"""
if s.startswith(prefix):
return (True, s[len(prefix):])
else:
... | f3f552ae3f4b0c5b8cc6a58259fd61a8b8147092 | 687,882 |
import io
import wave
def buffer_to_wav(buffer: bytes) -> bytes:
"""Wraps a buffer of raw audio data (16-bit, 16Khz mono) in a WAV"""
with io.BytesIO() as wav_buffer:
wav_file: wave.Wave_write = wave.open(wav_buffer, mode="wb")
with wav_file:
wav_file.setframerate(16000)
... | 1112c080c11696cbb5f54b9fe49fcf7b8c45e966 | 687,883 |
import torch
def discriminator_loss(logits_r,logits_m,logits_f,logits_f_prime):
"""
Computes the discriminator loss described in the homework pdf
using the bce_loss function.
Inputs:
- logits_r: PyTorch Variable of shape (N,) giving scores for the real data.
- logits_f: PyTorch Variable o... | 5d2a5ea19d1818f2043310fa20be9efdc27e98fe | 687,884 |
import toml
def parse_toml_file(file_object):
"""Parses toml data using a file-like object to a dictionary.
Args:
file_object: (file-like object) file like object instantiated from a toml formatted file
Returns:
A dictionary with parsed toml data fields.
"""
return toml.load(file... | 5fe70fec0d35ef5cfc124c0d8ea3c185b84a8e54 | 687,887 |
def escape_delimit(s):
"""
escapes bytes 0x7e, 0x7d, 0x11 (XON), 0x13 (XOFF)
0x7e and 0x7d will be escaped to 0x7d 0x5e and 0x7d 0x5d
0x11 and 0x13 will be escaped to 0x7d 0x31 and 0x7d 0x33
0x7e - packet start/end
0x7d - escape character. escapes the following byte by inverting bit 5.
exa... | 4828e77a2dc14f32d3a2865fb37564a1b7147132 | 687,890 |
def csv_file(tmp_path):
"""Generate a csv file for test purposes
Args:
tmp_path: temporary area to use to write files
Returns:
path to the csv file
"""
tmp_path.mkdir(exist_ok=True)
csv_file_path = tmp_path / "file.csv"
csv_file_path.write_text("x,y\n0,0\n1,1\n")
return csv... | 2b2df6bb396d7bf0e18d9d427f0c81ed30a050e8 | 687,891 |
from typing import Tuple
def calculate_center_coords(
cell_index: Tuple[int, int], cell_size: Tuple[int, int]
) -> Tuple[int, int]:
"""Calculate cell center coordinates.
:param cell_index: selected cell index
:param cell_size: given cell size (height, width)
:return: given cell center coordinates... | c510f55695770a7e3effcb82dd2e59c9a1cd36bc | 687,892 |
def cleanup_comment(comment):
"""Given a dictionary of a comment record, return a new dictionary for
output as JSON."""
comm_data = comment
comm_data["id"] = str(comm_data["_id"])
comm_data["user"] = str(comm_data["user"])
comm_data["post"] = str(comm_data["post"])
comm_data["created"] = s... | 54354dfe276911e00b84518310f188378708caab | 687,897 |
def tag2String(tag):
"""Takes a tag placement, and turns it into a verbose string"""
return ("Tag with ID #%d, was placed @ (%f,%f), facing %f deg" %
(tag['id'], tag['x'], tag['y'], tag['th_deg'])) | 5eac5e77228e81efe9968475c85b9d79ab5a2960 | 687,900 |
def get_level_size(slide, level):
"""Returns the dimensions of a level
"""
return slide.level_dimensions[level] | cb3279b82331152edc8d477ca842f035881621fe | 687,903 |
import torch
def regularization_loss(params, weight):
"""Compute regularization loss.
Args:
params: iterable of all parameters
weight: weight for the regularization term
Returns:
the regularization loss
"""
l2_reg = 0
for param in params:
l2_reg += torch.norm(param)
return weight * l2_... | bee4f75d3782e9c3ba059d0de0cfb6819abd493d | 687,906 |
def format_network_speed(raw_bps=0):
""" Formats a network speed test to human readable format """
fmt = ['b/s', 'Kb/s', 'Mb/s', 'Gb/s']
index = 0
speed = raw_bps
while speed > 1024:
index += 1
speed /= 1024
return "%0.2f %s" % (speed, fmt[index]) | 9d716c135feb862ac1be6d522987302716fec9b1 | 687,909 |
def derivative_from_polycoefficients(coeff, loc):
"""
Return derivative of a polynomial of the form
f(x) = coeff[0] + coeff[1]*x + coeff[2]*x**2 + ...
at x = loc
"""
derivative = 0.
for n, c in enumerate(coeff):
if n == 0:
continue
derivative += n*c*loc**(n-... | 29991fa0bccb29af85376759ce8c212606c027b2 | 687,912 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.