content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
import struct
def get_rgba_str(src):
"""Convert cairo surface data to RGBA."""
lmax = len(src) / 4
argb = list(struct.unpack("=%dL" % lmax, src))
rgba = [(((d & 0x0ffffff) << 8) + ((d >> 24) & 0x0ff)) for d in argb]
return struct.pack(">%dL" % lmax, *rgba) | ca1c94193be349b83b9e99a1d76ccbde1e251db7 | 244,568 |
import struct
def of_slicer(remaining_data):
"""Slice a raw bytes into OpenFlow packets"""
data_len = len(remaining_data)
pkts = []
while data_len > 3:
length_field = struct.unpack('!H', remaining_data[2:4])[0]
if data_len >= length_field:
pkts.append(remaining_data[:length... | 77bd851e4b3fc1111c5142fab1439820c165989b | 686,253 |
import socket
import struct
def format_ipv4(ip, port=None):
"""Format an ipv4 address into a human-readable string.
Args:
ip (varies): the ip address as extracted in an LTTng event.
Either an integer or a list of integers, depending on the
tracer version.
port (int, optional)... | 9b34a59376962d0c095b39bc846e546fa75250a4 | 514,157 |
import six
def range_join(numbers, to_str=False, sep=",", range_sep=":"):
"""
Takes a sequence of positive integer numbers given either as integer or string types, and
returns a sequence 1- and 2-tuples, denoting either single numbers or inclusive start and stop
values of possible ranges. When *to_str... | c1b2d10ec1b47fa5c917fccead2ef8d5fc506370 | 1,981 |
def containing_block(code, idx, delimiters=['()','[]','{}'], require_delim=True):
"""
Find the code block given by balanced delimiters that contains the position ``idx``.
INPUT:
- ``code`` - a string
- ``idx`` - an integer; a starting position
- ``delimiters`` - a list of strings (default: [... | a23d584d9890ae24b17ab0fdd48b9327b94a8f2a | 373,527 |
def groups_balanced(arg):
"""
Match [, {, and ( for balance
>>> groups_balanced("(a) and (b)")
True
>>> groups_balanced("((a) and (b))")
True
>>> groups_balanced("((a) and (b)")
False
>>> groups_balanced(" [a] and [b] ")
True
>>> groups_balanced("((a) and [(b)])")
True... | cef90251e5dfe9f3be17af062d6042df6505bb0c | 81,223 |
import math
def entropy(U, N, weight=None):
""" Returns information entropy of the assignment
Args:
U: assignment {class_id: [members,],}
N: number of objects
weight: weight factor {object_id: weight},]
"""
H = 0
for class_id, members in U.items():
size = len(membe... | 5437b3dd843fbc82b3f26852f3525f02fb1e95ae | 613,104 |
from typing import Optional
def verify_input_date_format(date: Optional[str]) -> Optional[str]:
"""
Make sure a date entered by the user is in the correct string format (with a Z at the end).
Args:
date (str): Date string given by the user. Can be None.
Returns:
str: Fixed date in th... | 7e8e17df937693d6cf53aaff4018df1ff95fb1fc | 133,326 |
def apply_polynomial(coeff, x):
"""Given coefficients [a0, a1, ..., an] and x, compute f(x) = a0 + a1*x + ... + ai*x**i +
... + an*x**n.
Args:
coeff (list(int)): Coefficients [a0, a1, ..., an].
x (int): Point x to on which to evaluate the polynomial.
Returns:
int: f(x) = a0 + a... | 08bc04ca19b9289b726fec0b9ad832470709270d | 665,654 |
def check_has_backup_this_month(year, month, existing_backups):
"""
Check if any backup in existing_backups matches year and month.
"""
for backup in existing_backups:
if backup.createdYear == year and backup.createdMonth == month:
return True
return False | ade68262f374f2e56ba2f60c6e3689a50b87d7a0 | 618,536 |
import math
def rule_of_thumb(m,alpha=0.92,s=0.20):
"""
Return number of precincts to audit for given margin m.
m = margin of victory a fraction of total votes cast
Roughly 92\% confidence rate.
"""
return 1.0/m * (- 2.0 * s * math.log(alpha)) | 4acf633d7b57893ee78c74636027212703ac10fa | 328,548 |
def match_module(names):
"""
line is a stripped Fortran statement. (no comment or
white space at beginning or end.) if it contains the beginning of
a module definition, return the module name in upper case
"""
if len(names) < 2: return ""
if names[0] == "MODULE" and names[1] != "PROCEDURE":... | 9793112985c15448362636dd8c4c947fc79e455e | 159,662 |
def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to underscores.
"""
value = value.strip().lower()
value = value.replace(',', '')
value = value.replace(' ', '_')
return value | ac006d287c9efaef3ee8d210a71681536ce84c17 | 271,856 |
def _get_mapping_dict(ch_names: list[str]) -> dict:
"""Create dictionary for remapping channel types.
Arguments
---------
ch_names : list
Channel names to be remapped.
Returns
-------
remapping_dict : dict
Dictionary mapping each channel name to a channel type.
"""
... | 782cda9c43749f71241dbef65f5654eafd7e07f4 | 43,066 |
def is_vlan(v):
"""
Check value is valid VLAN ID
>>> is_vlan(1)
True
>>> is_vlan(-1)
False
>>> is_vlan(4095)
True
>>> is_vlan(4096)
False
>>> is_vlan("g")
False
"""
try:
v = int(v)
return 1 <= v <= 4095
except ValueError:
return False | 48c8fddc1e733ac3697c14b3d78641863dd84fdd | 294,398 |
import torch
def create_random_binary_mask(features):
"""
Creates a random binary mask of a given dimension with half of its entries
randomly set to 1s.
:param features: Dimension of mask.
:return: Binary mask with half of its entries set to 1s, of type torch.Tensor.
"""
mask = torch.zeros... | 4f98743fc27d4abec219b9317dd16e9c61a0eac9 | 682,738 |
def _pep8_violations(report_file):
"""
Returns the list of all PEP 8 violations in the given report_file.
"""
with open(report_file) as f:
return f.readlines() | d7b399e8d7b67a1ed22feea74bed2a0755396d09 | 181,603 |
def int_def(value, default=0):
"""Parse the value into a int or return the default value.
Parameters
----------
value : `str`
the value to parse
default : `int`, optional
default value to return in case of pasring errors, by default 0
Returns
-------
`int`
the p... | b4394805007c9dcd8503d3062f76aa6840672604 | 659,272 |
def new(num_buckets=256):
"""Initializes a Map with the given number of buckets."""
aMap = []
for i in range(0,num_buckets):
aMap.append([])
return aMap | 7d4496ce082950343ee43e1ce97a196361f04e47 | 525,732 |
def list_from_i_to_n(i,n):
"""
make list [i,i+1,...,n]
for example:
list_from_i_to_n(3,7) => [3,4,5,6,7]
list_from_i_to_n(4,6) => [4,5,6]
"""
result = []
for j in range(i,n+1):
result = result + [j]
return result | 308a30dde3f74726755343a34a78d9a9c949cb8e | 106,412 |
def iob_scheme(tags, iobes=True):
""" Transform tag sequence without any scheme into IOB or IOBES scheme """
iob = []
tags = ["O"] + tags + ["O"]
for i in range(1, len(tags) - 1):
prev_tag, tag, next_tag = tags[i - 1: i + 2]
if not tag == "O":
if tag not in [prev_tag, next_... | 8c7c39eafab90b07864e18315fb391417d3dcf81 | 250,963 |
def get_corpus(tokens):
""" Combine the list of tokens into a string
:return: a long string that combines all the tokens together
"""
corpus = ' '.join(tokens)
return corpus | 5a728c231a81462d09bdab64b48e7d3a1fac76cc | 448,747 |
import torch
def get_plan_feats(plans, scene_tensor, agent_tensor):
"""
Returns location coordinates, map and agent features for a given batch of plans
Inputs
plans: Sequences of row and column values on grid. shape: (Batchsize, horizon, 2)
scene_tensor: Tensor of scene features: (Batchsize, C_s,... | d90b74fc96e1547ac1f1f3f6f160bf9805701738 | 316,296 |
def discouont_rewards(raw_rewards, gamma):
"""
Discount rewards in episode
:param raw_rewards: raw rewards
:param gamma: discount rate
:return: discounted rewards
"""
R = 0
rewards = []
for r in reversed(raw_rewards):
R = r + gamma * R
rewards.append(R)
rewards = ... | 564428678e9ecb923926651a46d583827d11690c | 151,971 |
def get_missing_object(wizard, obj_dict, display_name, fallback_func):
"""Get a prerequisite object.
The ``obj_dict`` here is typically a mapping of objects known by the
Wizard. If it is empty, the ``fallback_func`` is used to create a new
object. If it has exactly 1 entry, that is used implicitly. If ... | 739a453a42c902f2a3dcbe89360fba78a2557cf4 | 279,557 |
def strip_prefix_from_items(prefix, items):
"""Strips out the prefix from each of the items if it is present.
Args:
prefix: the string for that you wish to strip from the beginning of each
of the items.
items: a list of strings that may or may not contain the prefix you want
to strip out.
Re... | ce509d99b410ee07469cbdb3250cd8768cfd5aed | 391,750 |
def import_name(name):
"""
Import an object by its qualified name.
Can be used to load a module, submodule, class, function, or other
object, including in situations where the regular `import` statement
would fail.
Parameters
----------
name : str
The fully qualified name of th... | ca87ea4f6e3305657a36e9864416e6e5d797ca46 | 438,830 |
def nop(stack):
""" no-operation. Returns stack unchanged. """
return stack | 6a5cc77693adf325e6945e6548873f4d2af7b10a | 273,427 |
def finish_present_answer_feedback(question_id):
"""
Feedback when the user tries to answer another question, when he is in mid of answering a question.
:param question_id: Q-id of the question, you are currently answering
:return: Feedback text
"""
return "Please complete answering present que... | 49d330e2becdb85e616f0c7af8ce76e590a389e5 | 605,317 |
import json
def get_counts_from_json(year):
""" Reads a json file containing total phrase and doc freqs for each year and returns the total
no. of phrases in the year in the arguments, and the total no. of documents in the year."""
# Read the Json file which has the monthly total phrases and documents --... | 456918bd5beede3e0f51af3e10603fe05888e773 | 232,089 |
def isOnGrid(p, grid_size):
"""
Check if position `p` is valid for the grid.
"""
return p[0] >= 0 and p[1] >= 0 and p[0] < grid_size and p[1] < grid_size | 43ade6c302fb7f2c0b6c743a00e351eaed21a6e5 | 134,433 |
from xml.dom.minidom import parse
import typing
def parse_element(stream: typing.BinaryIO):
"""Parse the content of the PSML file to determine the element.
:param stream: a filelike object with the binary content of the file.
:return: the symbol of the element following the IUPAC naming standard.
"""... | 639d4770cf73955204590c516040eeefbce51af3 | 397,065 |
def upload_media(request, form_cls, up_file_callback, instance=None, **kwargs):
"""
Uploads media files and returns a list with information about each media:
name, url, thumbnail_url, width, height.
Args:
* request object
* form class, used to instantiate and validate form for upload
* call... | 3e8fa5429cfd9f19b69525a4d7d97d414fb02820 | 662,369 |
def create_expr_string(clip_level_value):
"""Create the expression arg string to run AFNI 3dcalc via Nipype.
:type clip_level_value: int
:param clip_level_value: The integer of the clipping threshold.
:rtype: str
:return The string intended for the Nipype AFNI 3dcalc "expr" arg inputs.
"""
... | 37b8219a16eff829cc80cbc3da282a565ad06ded | 159,209 |
import re
def split_url_at_version_tag(url):
"""Split a DataONE REST API URL.
Return: BaseURL, version tag, path + query E.g.:
http://dataone.server.edu/dataone/mn/v1/objects/mypid ->
'http://dataone.server.edu/dataone/mn/', 'v1', 'objects/mypid'
"""
m = re.match(r"(.*?)(/|^)(v[123])(/|$)(.*... | 42ba4ca3a1b746012e90db0f5fb046374cb5e0e1 | 195,081 |
import re
def get_glyphorder_cps_and_truncate(glyphOrder):
"""This scans glyphOrder for names that correspond to a single codepoint
using the 'u(ni)XXXXXX' syntax. All names that don't match are moved
to the front the glyphOrder list in their original order, and the
list is truncated. The ones that do match... | 7763b704d45d5a05a829effec3dc49c447c4a71d | 160,292 |
def ocr_url(client, image_url):
"""
Calls Azure Computer Vision API to read the contents of a file via a public url
Returns the result object
"""
recognize_printed_results = client.batch_read_file(image_url, raw=True)
return recognize_printed_results | 76b70d9b7ce815fa352dd54043e77253ee40a6f4 | 165,021 |
import click
def color_setup_options(f):
"""Create common options for handing color scales"""
f = click.option('--normalized_by_size', '-nbs',
help='Normalize the quantity used for color function by the number of atoms in each frame.',
show_default=True, default=False... | 06b1d483ecc100eb55b4483d055c26062d4dc917 | 362,623 |
import re
def alias(s):
"""
Generate alias for s.
Aliases contain only numbers, lowercase letters, and underscores.
Any groups of invalid characters will be replaced by one underscore.
"""
return re.sub('[^a-z0-9]+', '_', s.lower()) | 1320f543950d46e8298dc4354aeaa0c48f675be9 | 162,930 |
def get_int_df(adata, lr, use_label, sig_interactions, title):
"""Retrieves the relevant interaction count matrix."""
no_title = type(title) == type(None)
if type(lr) == type(None): # No LR inputted, so just use all
int_df = (
adata.uns[f"lr_cci_{use_label}"]
if sig_interact... | 957d6d400189d4804406dfdbf20aa7a196f04e67 | 169,845 |
def convertDictToValueNameList(valueDict):
"""Returns a list of name/value dictionaries. Not recursive. Scrubs empties."""
return [
{"name": k, "value": valueDict[k]} for k in valueDict if valueDict[k]
] | 6e587d3b2eb88c60038128686d96890b2a02bd07 | 560,132 |
def get_importmappings(mappings):
"""Build list of importmappings
Args:
mappings: dict<string,string> src: dst
Returns
list<string> in the form 'M{src}={dst}`
"""
if not mappings:
return []
return ["M%s=%s" % (src, dst) for src, dst in mappings.items()] | b0afa33ddd6a9ed3a91f7219a982713af7eae92a | 382,385 |
def command_condition(*args):
"""
Parameters
----------
args: list
containing boolean values for:
if a command has been specified explicitly
if the dbms is oracle
if the os is posix
if windows authentication has been specified
Returns
---... | 47755a875109896676f995a52e8da7899aff0a81 | 458,234 |
def check_if_tuple_exists(cursor, table_name, kennziffer, year):
"""
Checks if the tuple is already in te DB.
:param cursor: for the db to check
:param table_name: name of the table to check in
:param kennziffer: this value is checked
:param year: this value is checked
:return: True if the t... | ae6bf3c4dab0e98ef01c0e1fcee7de351f66b0cd | 626,026 |
def calculate_distance_between_colors(color1, color2):
""" Takes 2 color tupes and returns the average between them
"""
return ((color1[0] + color2[0]) / 2, (color1[1] + color2[1]) / 2, (color1[2] + color2[2]) / 2) | 17143102cd8f7270009d692d707c51c1b84b4528 | 452,270 |
def Hex8_shape_function_loc(xi_vec,node_local_coords): #Test function written
"""Compute the value of the shape function at xi_vec
for a node with local coordinates node_local_coords"""
xi1,xi2,xi3 = xi_vec
nc1,nc2,nc3 = node_local_coords
return 0.125*(1+xi1*nc1)*(1+xi2*nc2)*(1+xi3*nc3) | 11fe45ef2ce6d2fc1d4145875397a00fae486d98 | 515,866 |
def invert_edge_predicate(edge_predicate):
"""Build an edge predicate that is the inverse of the given edge predicate.
:param edge_predicate: An edge predicate
:type edge_predicate: (pybel.BELGraph, BaseEntity, BaseEntity, str) -> bool
:rtype: (pybel.BELGraph, BaseEntity, BaseEntity, str) -> bool
"... | d218705e7c4a5101ddde487171f9c7007cd4dfde | 155,535 |
def get_number_of_arrays(vtk_polydata):
"""Returns the names and number of arrays for a vtkPolyData object
Args:
vtk_polydata (vtkPolyData): object / line to investigate the array.
Returns:
count (int): Number of arrays in the line.
names (list): A list of names of the arrays.
... | 7aae492d37a8312e083161b1824a26407af15296 | 141,655 |
def to_string(obj):
"""Convert input to string"""
if isinstance(obj, str):
pass
elif isinstance(obj, bytes):
obj = obj.decode()
return obj | 023917319cde93a8b95444f1f57f588c7224404d | 373,717 |
def triangle_area(a, b, c):
"""Returns the area of a triangle using Heron's Formula."""
# Get the semiperimeter
s = (a+b+c)/2
return (s*(s-a)*(s-b)*(s-c))**(1/2) | 50bb74f6c43ee4e23c22a28108b4d4e0ffb1b069 | 451,775 |
import binascii
def npbytearray2hexstring(npbytearray, prefix="0x"):
"""Convert a NumPy array of uint8 dtype into a hex string.
Example:
npbytearray2hexstring(array([15, 1], dtype=uint8)) = "0x0f01"
"""
return prefix + binascii.hexlify(bytearray(npbytearray)).decode("utf-8") | 0d2dd571b25261774d7aad5156dbdb2bf9ec6357 | 664,985 |
def merge(df1, df2, monthly=False):
"""
Merge two dataframes
:param df1: (pd.DataFrame) - dataframe to merge
:param df2: (pd.DataFrame) - another dataframe to merge
:param monthly: (boolean) - whether to groupby month first
:return merged_df: (pd.DataFrame) - combined df1 and df2
"""
if... | 04cf5cfeaa7207be4807c96843f35cfb1fd1b0ac | 380,576 |
def dot(v1,v2):
"""Dot product of two vectors"""
n = len(v1)
prod = 0
if n == len(v2):
for i in range(n):
prod += v1[i]*v2[i]
return prod | 569ad317cf8d875a0c0592a576c3987b02b54f95 | 673,683 |
def interpret(text):
"""
Decode text to allow escape sequences (e.g. \n or \t) input by the user
to be processed as expected. Important for the -f and -s options.
"""
if text:
return bytes(text, "utf-8").decode("unicode_escape")
else:
return None | 799423e5784f20b500848b41c43f735571cb0986 | 372,809 |
def length(string):
"""returns the length of string.
"""
return len(string) | 8af2fa272039bad4a8fa1d0158895f537aeddf8c | 292,826 |
def strpos(string, search_val, offset = 0):
"""
Returns the position of `search_val` in `string`, or False if it doesn't exist. If `offset` is defined, will start looking if `search_val` exists after the `offset`.
"""
try:
return string[offset:].index(search_val) + offset
except ValueError:... | f4c1d5226ae56201d276d7cd3b4b29edd799b10e | 285,740 |
def is_child(child, parent):
"""
Check if the child class is inherited from the parent.
Args:
child: child class
parent: parent class
Returns:
boolean
"""
for base in child.__bases__:
if base is parent:
return True
for base in child.__bases__:
... | df72d8e754dab6db2f0d2edc0f399ab23b2cb6d1 | 70,672 |
def build_trainset_surprise(dataset):
"""
Build a training set from a Surprise Dataset so that
it can be used for model training.
"""
trainset = dataset.build_full_trainset()
return trainset | 0c43284c11654e4bfce7f52caf21e01652be80bd | 600,984 |
def get_last_folder_as_id(value):
"""Use the last part of the directories as test identifier."""
if isinstance(value, (tuple,)):
return value[-1]
return value | e1e90424f1c06ccf7d4a58d89fd0a24eb668144a | 315,702 |
def string_to_tuple(version):
"""Convert version as string to tuple
Example:
>>> string_to_tuple("1.0.0")
(1, 0, 0)
>>> string_to_tuple("2.5")
(2, 5)
"""
return tuple(map(int, version.split("."))) | b987fbd25ef46839b24ab32282fc3403588a473c | 164,819 |
def get_reverse_bits(bytes_array):
"""
Reverse all bits in arbitrary-length bytes array
"""
num_bytes = len(bytes_array)
formatstring = "{0:0%db}" % (num_bytes * 8)
bit_str = formatstring.format(int.from_bytes(bytes_array, byteorder='big'))
return int(bit_str[::-1], 2).to_bytes(num_bytes, by... | c4c64624a9fab5d9c564b8f781885a47984f1eaf | 23,577 |
def get_sim_name(obj):
"""
Get an in-simulation object name: if ``obj`` has attribute
``__sim_name__``, it will be returned, otherwise ``__name__``
standard attribute.
Args:
obj: an object to get name of
Returns: object name
"""
if hasattr(obj, '__sim_name__') and obj.__sim_nam... | 80c7b29047f09d5ca1f3cdb2d326d1f2e75d996b | 39,082 |
def LIFR(conf):
"""Get LIFR Color code from config"""
return conf.get_color("colors", "color_lifr") | a7ea2f4303acb28ef9a8851a7cc72d2b9eee9406 | 202,474 |
def join_positions(pos1, pos2):
"""Merge two positions and return as a list of strings
pos1: iterable object containing the first positions data
pos2: iterable object containing the second positions data
Example:
>>> join_positions('ABCD','1234')
['A1', 'B2', 'C3', 'D4']
"""
... | a8b430898ec807d74974ef27ebc6fe04349caa09 | 131,802 |
import random
def randtour(n):
"""Construct a random tour of size 'n'."""
sol = list(range(n)) # set solution equal to [0,1,...,n-1]
random.shuffle(sol) # place it in a random order
return sol | 267da3ed15924f8bb5505e3d881714e4d1917bd4 | 505,907 |
def get_functions(template):
"""
Extracts functions and environment variables from a template
This returns a dict with the function name as the key and a list of variable names
as value.
"""
# Extract variables from the Globals section
try:
global_variables = list(template["Globals... | bbfb158c62a7f764041fe78804fd523fbc4b553a | 438,056 |
def allocated_ticket(user):
"""Returns True if the user has any allocated tickets."""
return user.raffleticket_set.count() > 0 | fda884e79500b0b34d58d4224032e06aa87a8df4 | 234,934 |
import copy
def run_hook(name, workspace, hooks):
"""Runs all hooks added under the give name.
Parameters
----------
name - str
Name of the hook to invoke
workspace - dict
Workspace that the hook functions operate on
hooks - dict of lists
Mapping with hook names and ... | e6caa07125a0e9d52ac3047d54537e3ef8a9361d | 284,975 |
from typing import Optional
def get_pr_number(branch: str) -> Optional[int]:
"""Retrieve the pull request number from the branch name."""
if 'pull/' in branch:
parts = branch.split('/')
return int(parts[parts.index('pull') + 1])
return None | f6467a0ead306ff13474761228d83c39d4734425 | 305,149 |
from random import randint
def rand_colour() -> int:
"""Gets a random colour."""
return randint(0, 0xFFFFFF) | 764445570f16c60bad4ba6aa17977dcb09cb02a3 | 562,820 |
import glob
def get_photos(folder_path, pics_extension):
"""Function create a list of photos from given folder"""
pics_path = folder_path + "/*." + pics_extension
return glob.glob(pics_path) | 3d32d5b173d74a2425f1229ee5e45784c1283053 | 687,453 |
import collections
def tail(n, seq):
""" The last n elements of a sequence
>>> tail(2, [10, 20, 30, 40, 50])
[40, 50]
See Also:
drop
take
"""
try:
return seq[-n:]
except (TypeError, KeyError):
return tuple(collections.deque(seq, n)) | 4cf7fa4301f1989ea16a9d653d083fd4edb6bc76 | 630,917 |
def get_ros_type(type_string):
"""Parses the type_string to get package and type"""
parts = type_string.split("/")
if len(parts) != 2:
raise Exception("Type lookup requires two parts, split by a slash: Package and type")
package = parts[0]
ros_type = parts[1]
return (package, ros_type) | f52f1bdd71bb08724d155e9ae7ec6764740e21da | 290,779 |
def format_duration(seconds):
"""Format a duration as ``[hours:]minutes:seconds``.
Parameters
----------
seconds : int
Duration in seconds.
Returns
-------
str
"""
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
if hours > 0:
ret... | b5b612f0bf613ec429d8570265531560cb03ba7d | 391,200 |
def normalize_sql_str(string):
"""Normalizes the format of a SQL string for string comparison."""
string = string.lower()
while ' ' in string:
string = string.replace(' ', ' ')
string = string.strip()
string = string.replace('( ', '(').replace(' )', ')')
string = string.replace(' ;', ';')
string = s... | 4696d5758a3ba20c92e7c0128a71f98cd76333f9 | 381,826 |
import io
def slurp(path, encoding='UTF-8'):
"""
Reads file `path` and returns the entire contents as a unicode string
By default assumes the file is encoded as UTF-8
Parameters
----------
path : str
File path to file on disk
encoding : str, default `UTF-8`, optional
Enco... | 1a0697b7c2858308a2ad4844d92620d3e7206a02 | 72,098 |
def fact_imp(n: int) -> int:
""" Factorielle de n, n! = 1 * 2 * .. * n."""
f: int = 1
for i in range(2, n+1):
f = f * i # f *= i est aussi possible
return f | 792fc9df08f2df0a6118ac37067f2f06f8471f9d | 448,294 |
from typing import Any
import torch
def unsqueeze_scalar_tensor(data: Any) -> Any:
""" Un-squeezes a 0-dim tensor. """
if isinstance(data, torch.Tensor) and data.dim() == 0:
data = data.unsqueeze(0)
return data | 9af53ffa3d0a100672a3060969627fb6547f6a23 | 476,812 |
def _get_all_no_id(cursor, table_name, table_id, print_string, verbose, **kwargs):
"""Gets all elements from a table as a dictionary, excluding id"""
cursor.execute("""SELECT * FROM """ + table_name)
values = []
for row in cursor:
val = dict(zip(row.keys(), row))
val.pop(table_id)
... | 4b2160b945128d30bc4ad15ab2b3ca9543e43645 | 569,014 |
import torch
def one_hot_vector(length, index, device=torch.device('cpu')):
"""
Create a one-hot-vector of a specific length with a 1 in the given index.
Args:
length: Total length of the vector.
index: Index of the 1 in the vector.
device: Torch device (GPU or CPU) to load the ve... | 88dca8d63792b5a8eb58abbf12eb2800e28b3264 | 22,686 |
import re
def construct_filename(fn_format, **kwargs):
"""
Function that takes a string with n-number of format placeholders (e.g. {0]})
and uses the values from n-kwargs to populate the string.
"""
kwarg_num = len(kwargs)
fn_var_num = len(re.findall(r"\{.*?\}", fn_format))
if kwarg_num !=... | 83edcb1703814d891af6d13577b87efdce2fd02c | 457,713 |
def changePlayer(player):
"""Returns the opposite player given any player"""
if player == "X":
return "O"
else:
return "X" | 118d4c7ad04c16edebb1c8b829462327cccc4235 | 628,830 |
def _header_dict(project_id, auth_token):
"""
Create a header dict from the project ID and auth token
"""
return {
"accept": "application/json",
"project": str(project_id),
"Authorization": "Bearer " + str(auth_token),
} | 9b13f5a3472d51303fd554e35a7462ae83a7c860 | 489,492 |
def get_review_rating(soup):
""" Analyze "soup" to extract rating review.
Args:
soup -- bs4.BeautifulSoup from http request of book url.
Return:
rating review
"""
str_to_number = {"One": 1, "Two": 2, "Three": 3, "Four": 4, "Five": 5}
p = soup.find("p", {"class": "star-rating"})
... | e4fa5e0ca2c290cfba952a1c3237da3cf6aa991e | 231,639 |
def get_ngram_universe(sequence, n):
"""
Computes the universe of possible ngrams given a sequence. Where n is equal to the length of the sequence, the resulting number represents the sequence universe.
Example
--------
>>> sequence = [2,1,1,4,2,2,3,4,2,1,1]
>>> ps.get_ngram_universe(sequence, 3)
64
"""
# if... | 3dbfe1822fdefb3e683b3f2b36926b4bb066468f | 705,907 |
def set_fig_in_pt_set(rectangle, value, points):
"""
General routine to set tiles found in points to value
"""
if not points:
return rectangle
rectangle[points[0][0]][points[0][1]] = value
set_fig_in_pt_set(rectangle, value, points[1:])
return rectangle | 319dac7f6d14fc1e46073cce7e574937ca0a323c | 427,962 |
def mate_after(aln):
""" Check if mate is after (to the right) of aln
Alignment A is after alignment B if A appears after B in a sorted BAM
alignment. If A and B are on different chromosomes, the reference ID is
compared.
Args:
aln (:obj:`pysam.AlignedSegment`): An aligned segment
Ret... | 890a069bb54340e41579d532966bb608c52bcab6 | 384,961 |
def composite_identity(f, g):
"""
Return a function with one parameter x that returns True if f(g(x)) is
equal to g(f(x)). You can assume the result of g(x) is a valid input for f
and vice versa.
>>> add_one = lambda x: x + 1 # adds one to x
>>> square = lambda x: x**2
>>> b1 = compo... | df89773a3a01bf7d8b3ba5e33ec4866a0f365026 | 645,843 |
from datetime import datetime
def get_date(dt):
"""Return datetime from string."""
return datetime.strptime(dt[:-1] if dt.endswith('Z') else dt,
"%Y-%m-%dT%H:%M:%S.%f") | 937f40766f3b224ca9430538ff08ec14f9711788 | 269,558 |
def get_contributors(raw):
"""
Extract contributors.
Only contributors tagged as belonging
to any of the valid roles will be extracted.
@param raw: json object of a Libris edition
@type raw: dictionary
"""
valid_roles = ["author", "editor",
"translator", "illustrator... | 18e8b3737643e825b7e94a75603883086f5a37d1 | 45,672 |
def make_revisit_location_translations(query_metadata_table):
"""Return a dict mapping location revisits to the location being revisited, for rewriting."""
location_translations = dict()
for location, _ in query_metadata_table.registered_locations:
location_being_revisited = query_metadata_table.ge... | 17b9d431c340b46b972a9e5248d354396c512bd9 | 332,995 |
def get_default_ylabel(args):
"""Compute default ylabel for commandline args"""
label = ''
if args.transform == '':
label = args.metric
else:
label = args.transform + '(' + args.metric + ')'
if args.relative_to is not None:
label += ' relative to %s' % args.relative_to
re... | ccf414395536a64414b973ca0f3c30b2342cfa1b | 298,402 |
def sign(x):
""" Returns the sign of float x.
"""
if abs(x) == x:
return 1.
else:
return -1. | 24d5440a869e012be7f76527dc5459a792e7c28b | 365,930 |
def get_emissions_info(df, feature_1, feature_2):
"""Get CO2 emissions data as absolute and relative numbers as well
as per dwelling (mean).
Parameters
----------
df : pandas.DataFrame
Dataframe from which to retrieve emission data.
Needs to have columns named CO2_EMISSIONS_CURRENT... | 7786e31db22914a1ba250d9270cd6ff066543bfd | 224,340 |
def CalculateNewPassRate(existing_pass_rate, existing_iterations,
incoming_pass_rate, incoming_iterations):
"""Incorporates a new pass rate into an exsting one.
Args:
existing_pass_rate (float): The pass rate to merge into.
exisitng_iterations (int): The number of iterations used t... | 551ec00d10a8ce30f4ad04cab6bf65cc17f8f55f | 301,793 |
def AUC_analysis(AUC):
"""
Analysis AUC with interpretation table.
:param AUC: area under the ROC curve
:type AUC : float
:return: interpretation result as str
"""
try:
if AUC == "None":
return "None"
if AUC < 0.6:
return "Poor"
if AUC >= 0.6 ... | 433583977c2dc0a077f050691d31f17582d947a2 | 343,929 |
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 identity(x):
"""Transparent function, returning what's been input"""
return x | 8e443dfaaf1c0f0e9b07aaf8cf631a1dd5137b19 | 693,291 |
def ok(b):
"""
:returns: 'ok' if b is True, else, return 'error'.
"""
if b:
return "ok"
return "error" | 806e0dee4d4183c57bd72c2bc9166bd067172561 | 305,380 |
def _scalars_match(scalar_name1: str, scalar_name2: str) -> bool:
"""Return whether the two input scalars are considered to be the same.
For now, two input scalars are considered to be the same if they're the same scalar, if
one is ID and the other is String, or if one is ID and the other is Int. This may ... | fd4412278f9804eb8852f8c1a831f252ac55fa9e | 490,132 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.