content stringlengths 35 416k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
def pagination_for(context, current_page, page_var="page", exclude_vars=""):
"""
Include the pagination template and data for persisting querystring
in pagination links. Can also contain a comma separated string of
var names in the current querystring to exclude from the pagination
links, via the ``... | 77996effe11d35cff3b7e279533f331e1b2d05e0 | 20,459 |
def _place_caravanserai(new_map, size):
"""
Find a 3x3 region of desert near but not on the east or south edges.
"""
# find a space to fit it along the eastern edge,
# starting from the north
found_y = -1
rows = 0
for y in range(2, 19):
cols = 0
for x in range(19 - size, ... | bc35e27eff0ab564f3ea7d0eb8f5a3d1973797b0 | 20,460 |
import re
def suggest_name(ctx, wheel_name):
"""
Guess Debian package name from a wheel name and a python implementation.
:param wheel_name: Name of the distribution wheel
:type wheel_name: str
:return: Package name
"""
prefix = {2: "python", 3: "python3"}[ctx.python_version.major]
b... | b64d190c8e9417af81808d4ec7b0b51661aaa3ca | 20,461 |
import re
def _simplify_sql(sql):
"""
Simplify SQL statements to make them easier on the eye and shorter for the stats.
"""
sql = " ".join(sql.split("\n"))
sql = re.sub(r" +", " ", sql)
sql = re.sub(r"SELECT .*? FROM", "SELECT FROM", sql)
sql = re.sub(r"INSERT INTO (.*?) \(.*", r"INSERT I... | 9bd29bd0d455ffa7a35d8d46be8372e7a84c3964 | 20,462 |
def parse_gff_comment(comment):
"""Parse the comment."""
fields = {}
for fld in comment.split(";"):
tokens = fld.split("=", 1)
if len(tokens) == 2:
fields[tokens[0]] = tokens[1]
return fields | 2300fa3cab8507597d0c6d9fd2033f6f4a364e1d | 20,464 |
import hmac
def compute_signature(payload: bytes, secret: bytes, algo: str = 'sha256') -> str:
"""
Computes the HMAC signature of *payload* given the specified *secret* and the given hashing *algo*.
# Parmeters
payload: The payload for which the signature should be computed.
secret: The secret string that ... | 0f62326eaadddb569c661a59a60284062c348b2e | 20,467 |
def serialize(instance):
"""Return instanceect data in easily serializeable format."""
title = instance.title
if not instance.title:
title = ''
firstname = instance.firstname
if not instance.firstname:
firstname = ''
lastname = instance.lastname
if not instance.lastname:
... | f197143c115342997343aafb4724140bfe977e5a | 20,468 |
def all_true(results):
"""
return all results with true values
"""
return filter(lambda r: r.value, results) | f67b2e669c226cbedc4e62aebe43c5da35b8d43f | 20,471 |
import re
def remove_emoji(text: str) -> str:
"""
A function to remove emojis using regex
:param text: An input text.
:return: A text with removed emojis
"""
emoji_pattern = re.compile(pattern="["
u"\U0001F600-\U0001F64F" # emoticons
... | d30f2ae6ec3be04d4665329e1cba0cef47509751 | 20,474 |
import requests
import sys
def find_record_id(account_num, domain_id, headers, url, target_domain):
""" Parse through all records under our domain, to find our A record's ID. """
all_records = requests.get(url + '/domains/{}/records'.format(domain_id), headers=headers)
for record in all_records.json()['... | c0a93f9489c15dc127eac6ce2788297a189faac5 | 20,475 |
def format_data_for_ai(data):
"""EDITABLE. This function task is to prepare input data for AI model."""
formated_data = [[
data["pacman"]["x"],
data["pacman"]["y"],
data["ghost_1"]["x"],
data["ghost_1"]["y"],
data["ghost_2"]["x"],
data["ghost_2"]["y"],
dat... | 72b50f2a96828c0a50df7f1bf13f5a70a60e45c6 | 20,476 |
from typing import Any
import jinja2
def _render_jinja2(filename: str, **kwargs: Any) -> str:
"""Returns a rendered template.
Args:
filename: Template filename.
**kwargs: Template environment.
"""
with open(filename, 'r') as fh:
template = jinja2.Template(fh.read(), autoescape=False)
return tem... | 1a48edc8fb829e9a12bfd678756c995afaf5c9bd | 20,478 |
def min_value_node(node):
"""
Binary Search Tree min value node
Complexity: O(HEIGHT)
Find the node with the minimum value in a
binary search tree.
"""
while node.left is not None:
node = node.left
return node | 4d423183f0da3fd5d0bc66c84f26353543f8e406 | 20,479 |
from typing import List
def word_list(list_: str) -> List[str]:
"""Load word list."""
with open(list_, "r") as f:
return f.readlines() | 7c7ec4824d81d9f1e9e64af62dbfdf2d7bc2eaa1 | 20,480 |
def parse_option_list_string(option_list, delimiter=None):
"""Convert the given string to a dictionary of options.
Each pair must be of the form 'k=v', the delimiter seperates the
pairs from each other not the key from the value.
:param option_list: A string representation of key value pairs.
:typ... | fe6a440e004552b418d151ac6f3de9a08fe3e787 | 20,481 |
def _read_exact(read, size):
"""
Read exactly size bytes with `read`, except on EOF
:Parameters:
- `read`: The reading function
- `size`: expected number of bytes
:Types:
- `read`: ``callable``
- `size`: ``int``
:return: The read bytes
:rtype: ``str``
"""
if size <... | 4520c977812bd77365dd1b5a445697b3435d1d19 | 20,483 |
def filtering_results(results, book_type, number):
"""
results = list of dictionnary
Filter the results to getspecific type trade / issue
include omnibus and compendium in trades
and number
Based on comic title
"""
assert book_type in ["trade", "issue", None] , "Choose between 'trade' o... | 1d762f30703ce90ed750493889189e7ac3ae8872 | 20,485 |
def genericIntListValidator(values, validValues):
"""
Generic. (Added at version 2.)
"""
if not isinstance(values, (list, tuple)):
return False
valuesSet = set(values)
validValuesSet = set(validValues)
if valuesSet - validValuesSet:
return False
for value in values:
if not isinstance(value, int):
retur... | 5f20046435947ae811547646851b3964702ffeea | 20,486 |
import math
def circular_easein(pos):
"""
Easing function for animations: Circular Ease In
"""
return 1 - math.sqrt(1 - (pos * pos)) | 89d69147f1f273740683597c9076319d75c080a5 | 20,487 |
import re
def char_length(character, letter_spacing=0):
"""Return the max width of a character by looking at its longest line.
:param character: The character array from the font face
:param letter_spacing: The user defined letter spacing
:returns: The length of a longest line in a character
"""
... | 32650f0d21e597810225dad25513b0107d6551e1 | 20,488 |
def return_first_col_action(row, action_dict):
"""
Default action function for tables. This function returns the first data column value for the row of
data. Used by the **TABLE_RETURN_FIRST_VAL** action.
:param List row: the data associated with the selected row
:param Dict action_dict: the di... | 57e5975fd60cabff2bc135a6e0b88b34d3f70211 | 20,489 |
from typing import List
from typing import Dict
def aggregate_action_stats(action_stats: List[Dict[str, int]]) -> Dict[str, int]:
"""Aggregate statistics by returning largest value observed for each of the tweet reactions (reply, retweet, favorite)."""
action_stat = {}
for key in action_stats[0].keys():
... | 2e43e186c6f6ba58ce7ba0ff883bdb61783c896b | 20,490 |
import re
import logging
def append_to_csl_item_note(csl_item, text='', dictionary={}):
"""
Add information to the note field of a CSL Item.
In addition to accepting arbitrary text, the note field can be used to encode
additional values not defined by the CSL JSON schema, as per
https://github.com... | b3f43adacba1dca3749e048fe94b5cf0b2c15e3b | 20,491 |
def GetTitle( text ):
"""Given a bit of text which has a form like this:
'\n\n Film Title\n \n (OmU)\n '
return just the film title.
"""
pp = text.splitlines()
pp2 = [p.strip() for p in pp if len(p.strip()) >= 1]
return pp2[0] | d152282610072fa88c7a45a3aca2991b6fedb79c | 20,493 |
def get_time_in_seconds(timeval, unit):
"""
Convert a time from 'unit' to seconds
"""
if 'nyear' in unit:
dmult = 365 * 24 * 3600
elif 'nmonth' in unit:
dmult = 30 * 24 * 3600
elif 'nday' in unit:
dmult = 24 * 3600
elif 'nhour' in unit:
dmult = 3600
elif '... | 4fbcbf16e7a51e046e267a0cafad090049357cae | 20,494 |
def index():
""" render svg graph """
return 'Hello! Go to /table/ or /graph/.' | a2e5be8ca5a33078497315dcdf850df0d7c7a6fb | 20,495 |
def get_columns(document):
"""
Return a list of tuples, each tuple containing column name and type
"""
# tags = document.tags.to_dict()
tags = document.tags
names = list(tags.keys())
types = list(tags.values())
columns = []
for field, value in zip(names, types):
try:
... | c227815fcf1d2b3815b951c90397a7e89c43cc1c | 20,496 |
def drawPins(input_data, xpins, ypins, map):
"""
Draw pins on input_data
Inputs:
- input_data: np.array of size (C, H, W) (all zeros to start with)
- xpins & ypins: np.array of x-coordinates and y-coordinates for all pins
e.g., [x1, x2 ... xm] and [y1, y2, ... ym] for ... | 1d5796cc2a009e8e5993cbc374525c46b08a1a61 | 20,497 |
def _get_tile_grid_dict(tile_grid, tile_reading):
"""Create Dictionary with row and column for each tile in a grid.
Args:
tile_grid (tuple): Rows and columns of tile grid in well.
tile_reading (str): Reading method of microscope: horizontal,
horizontal_serp, vertical, vertical_s... | 5eb2a9c3c6e058e23bbb4c36f6229c4289d2803f | 20,498 |
import re
def slugify(input: str) -> str:
"""Converts Foo Bar into foo-bar, for use wih anchor links."""
input = re.sub(r"([() ]+)", "-", input.lower())
return re.sub(r"-$", "", input) | c08acb689783e382ce58ca886f2971aa4f42c763 | 20,500 |
def start_with_qu(string):
"""判断一个字符串是否含有'qu'
:type string: Str
"""
if len(string) < 2:
return False
bool1 = string[0] == 'Q' or string[0] == 'q'
bool2 = string[1] == 'U' or string[1] == 'u'
return bool1 and bool2 | 98774403ee984ffe7eb15ead9176acc45b16e763 | 20,501 |
def clean_music_name(music_name):
"""
DESCRIPTION: clean extra info from music name
INPUT: music_name (str)
OUTPUT: music_name (str)
"""
breaking_substring_list = [
' (feat.',
' (with',
]
for substring in breaking_substring_list:
if substring in music_name:
... | 20d3ce462f9f3a3fb08bc57ff707a30788c40a75 | 20,502 |
import six
def _changes(plays):
"""
Find changes in ansible return data
"""
changes = {}
for play in plays["plays"]:
task_changes = {}
for task in play["tasks"]:
host_changes = {}
for host, data in six.iteritems(task["hosts"]):
if data["chang... | 3c7206054db159d417aaaf617da1a7c197448c62 | 20,503 |
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
cur_dt = {}
max_len = 0 # history max_len
inn_len = 0 # current round max_len
last_check_point = 0
for seq, ch in enumerate(s):
if ch not in cur_dt:
cur_dt[ch] = seq
inn_len += 1
... | 95fa88cc5ef6fb55397f78cd59ba0556abc04ae8 | 20,504 |
def create_hash(ls):
"""
Examples:
>>> create_hash([1,2,3])
{1: None, 2: None, 3: None}
"""
return dict.fromkeys(ls) | 9b27921f0058331119856d27ca7b3565bbfe9673 | 20,505 |
def enemies_flied(enemies) -> bool:
"""Проверяет, добрались ли enemy balls до нижнего края экрана."""
for enemy in enemies.sprites():
if enemy.cx <= enemy.radius:
return True
return False | f069f5df813acbb61b65b0010905351f24f97502 | 20,506 |
def check_input_stream_count(expected_number_of_streams):
"""
Decorator for Tool._execute that checks the number of input streams
:param expected_number_of_streams: The expected number of streams
:return: the decorator
"""
def stream_count_decorator(func):
def func_wrapper(*args, **kwa... | 96dfdc8f85d70dee1ac44f01f95dd07eb3725261 | 20,508 |
import math
def calculate_base_day_duration(game):
"""Calculate the base day length."""
base_day_length = math.sqrt(2 * game.nb_alive_players)
base_day_length = math.ceil(base_day_length) + 1
base_day_length = base_day_length * 60
return base_day_length | 00f49a75f7b9772bed5358fe05f7ddf9b35d2f93 | 20,510 |
def guess_shape_and_submatrix_shape(dic):
"""
Guess the data shape and the shape of the processed data submatrix.
"""
if 'procs' not in dic: # unknow dimensionality and shapes
return None, None
procs = dic['procs']
if 'SI' not in procs or 'XDIM' not in procs:
return None, None ... | 00cf28f558b8af1253abb90df4312ff5c594d8e3 | 20,511 |
def contains_tokens(pattern):
"""Test if pattern is a list of subpatterns."""
return type(pattern) is list and len(pattern) > 0 | 10436114b1eb1b9e3f5c85bf30ec0813d999d101 | 20,513 |
def _is_iter(lst):
"""Is ``lst`` an iterator?
"""
try:
return list(lst)
except: # pragma: nocover
return False | 8296cb9741046e51db19368c93a9e901cef0b717 | 20,514 |
def remove_overlap_vector(original, to_remove, buff=0):
"""remove the to_remove CutVector from original CutVector"""
for row in to_remove:
original = original[(original[:, 1]<row[0]-buff) | (original[:, 0]>row[1]+buff)]
return original | 63dc22963da5660874d372093dea15c1cf3003ef | 20,515 |
def get_phase(answer: str) -> str:
"""Возвращает фазовое состояние на английском, как указано в GET запросе."""
while True:
if answer.lower() == 'газ':
return 'vapor'
elif answer.lower() == 'жидкость':
return 'liquid'
else:
answer = input('Неправильный... | 260d2eda627c0f788679af7af7a922cfb0a40ae4 | 20,516 |
import argparse
def parse_args() -> argparse.Namespace:
"""Parse user command line arguments."""
parser = argparse.ArgumentParser(
description='Merge given files on command-line.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('infiles', metavar='file', type=s... | 0b66396e75548b8552a3743d693b4bdbc248e8a0 | 20,519 |
def resolve_bibitem(bibitem, referenced_pubs):
"""Returns the publication object for the first referenced publication
whose bibliographic data matches to text in the bibitem.
.. note:: This is pretty hacky
Parameters
----------
bibitem : unicode
The bibitem string (e.g., \bibitem text)... | 19f6bde3936ec5c8ec534f119ca2c2f05e232ab4 | 20,520 |
def _patched_cast(self, db_type):
""" a workaround for icontains searches on LOB fields """
# see https://code.djangoproject.com/ticket/11580
if db_type and db_type.endswith('LOB'):
return "DBMS_LOB.SUBSTR(%s,2000,1)"
else:
return "%s" | 75e283c9a68f793550a13b384f4478b1397dd594 | 20,521 |
def solve(equation, answer):
""" Solve equation and check if user is correct or incorrect """
splitted = equation.split('=')
left = splitted[0].strip().replace('?', str(answer)).replace('x', '*')
right = splitted[1].strip().replace('?', str(answer)).replace('x', '*')
try:
if right.isdigit(... | 78f0eced818e681ed8d8c5e0e4667a101a7ffd4e | 20,522 |
def replace_parameters(templatefile, outfile, replacements, delims='=', commentchar=None, endchars = '\n', warn=False):
""" looks for strings of the form:
PARAM delim VALUE, if delim is a single str
and if PARAM is in replacements, writes a new line:
PARAM delim replacements[PARAM], otherwise does the replacem... | cd5fc328adfdc1ea9aef9a320e7e7b9dab07f39d | 20,526 |
import os
def _has_api_credentials():
"""
Test for API credentials
"""
client_id = os.environ.get('GOOGLE_OAUTH_CLIENT_ID')
client_secret = os.environ.get('GOOGLE_OAUTH_CONSUMER_SECRET')
salt = os.environ.get('AUTHOMATIC_SALT')
return bool(client_id and client_secret and salt) | 097c785c39ccb19cbf02284016b1bd1e2a104bce | 20,528 |
import re
def contig_to_vcf_chrom(contig_name):
"""
Annotations may reference more complex contig names than the VCF does in its CHROM column.
This function maps any given annotation's contig name to its corresponding VCF CHROM name.
"""
return re.sub(r'\W.+$', '', contig_name) | 080437ff504fd695742917542072f11405606a83 | 20,529 |
import pwd
import pwd
import os
def expanduser(path):
"""Expand ~ and ~user constructions. If user or $HOME is unknown,
do nothing."""
if not path.startswith('~'):
return path
i = path.find('/', 1)
if i < 0:
i = len(path)
if i == 1:
if 'HOME' not in os.environ:
... | f08a1b3a6f2075a66804a8890dc71fe5d98f94f5 | 20,530 |
import os
def list_files_in_dir(dir_path):
"""
返回目录下的所有文件名和绝对路径
[
{
"key": "file name",
"path": "abs path"
},
...
]
"""
lists = []
for f in os.listdir(dir_path):
f_path = os.path.join(dir_path, f)
if os.path.isfile(f_path):
... | 4d72c52f74a20de7b297e47da6e1d3e7bd19aa0f | 20,531 |
import os
import shutil
def send_to_logbook(fileName, location='lcls'):
""" Copies an xml file into the logbook 'new entry' directory. """
path = os.path.join("/u1/", location, "physics/logbook/data")
try:
shutil.copy(fileName + ".png", path)
except IOError:
print("Copying thumbnail failed!")
try:
shutil.c... | 9b7f7e72be4070fec6d8917a690f7278235fcd78 | 20,532 |
def create_prefix(nick, user, host):
""" Builds a prefix based on the nick, user, and host provided. This is essentially the
opposite to parse_prefix().
>>> create_prefix("Jack", "~jack", "000-000-000-000.example.com")
'Jack!~jack@000-000-000-000.example.com'
"""
return "{0}!{1}@{2}... | 9ad4cba5ab057faee06d0cf284e00c2c4d563623 | 20,533 |
def getJoint(df, attrs): # P(attrs)
"""Calcule la distribution de probabilité jointe de plusieurs attributs.
Parameters
----------
attrs : list of str
Les noms de l'ensemble d'attributs en question.
Returns
-------
probas : pandas.Series
La distribution de probabilité join... | e2668e99cd7a1dd1568033f3fd0270828fd33f40 | 20,534 |
def burg(sample_list, coefficient_number):
"""
Computes Linear Prediction coefficients via Burg method from a list of samples.
"""
p = sum(sample ** 2 for sample in sample_list)
a0 = p / len(sample_list)
b1 = sample_list[:len(sample_list) - 1]
b2 = sample_list[1:]
aa = [0.0 for i in r... | 3ccb83d43be659022fe85b3f10a73031030c21dd | 20,535 |
import numpy as np
def sigmoid(values, gain, shift):
"""
Map values with sigmoid function to range [0,1].
Y(t) = 1/(1 + exp(-gain*(values - shift))
"""
tiny = 0.000000001
# Make sure argument is a numpy array
if type(values) != np.ndarray:
values = np.array(values)
return 1... | aadbdbf8a3f5d8657cc7c9b24056e1a47baeb4a6 | 20,536 |
def from_last_seen_data(last_branch, last_node):
"""
nodeID / num_nodes
nodeID / nodes_left
nodes_left, best_integer, best_bound, itCnt, gap, has_incumbent, num_nodes
objective / best_integer, best_bound / objective, best_bound / best_integer
open_nodes_len, open_nodes_max, open_nodes_min, open_... | 5cc58b21f1f6ef776d14154aea4b7f6c8693de99 | 20,539 |
def load_model_configurations(model):
"""
Arguments:
model: A SSD model with PriorBox layers that indicate the
parameters of the prior boxes to be created.
Returns:
model_configurations: A dictionary of the model parameters.
"""
model_configurations = []
for layer in mod... | 34c6efbca820bd5461b2e5aeb2c7b30184afa250 | 20,540 |
from numpy.linalg import det, svd
import numpy
def is_mirror_image(X, Y):
"""
Check if two configurations X and Y are mirror images
(i.e. their optimal superposition involves a reflection).
@param X: n x 3 input vector
@type X: numpy array
@param Y: n x 3 input vector
@type Y: numpy array... | 62a384fc8d0a70990d1b5da1da80c9d43a6290a2 | 20,541 |
def user_case(mocker, user, case, org):
"""Fake UserCase instance."""
instance = mocker.Mock()
instance.user = user
instance.case = case
instance.organisation = org
return instance | 5d6de334a8ac690156204e81a6c2db53a71ea1d6 | 20,543 |
import re
def camel_to_snake(text):
"""
Will convert CamelCaseStrings to snake_case_strings. Examples:
>>> camel_to_snake('CamelCase')
'camel_case'
>>> camel_to_snake('CamelCamelCase')
'camel_camel_case'
>>> camel_to_snake('Camel2Camel2Case')
'camel2_camel2_case'
>>> camel_to_snak... | d7216ab1a35c189abf67bfb475486ce05dcba560 | 20,544 |
def get_hex(binary_str):
"""
Returns the hexadecimal string literal for the given binary string
literal input
:param str binary_str: Binary string to be converted to hex
"""
return "{0:#0{1}x}".format(int(binary_str, base=2), 4) | fe2784e58d61e577bcc66ed1bd3d2c02c1e7fda0 | 20,545 |
from typing import List
from typing import Any
def second_pass(tokens: List[Any]) -> List[Any]:
"""
Examples
--------
>>> second_pass([3, "[", "abc", "]", 4, "[", "ab", "]", "c"])
[3, ['abc'], 4, ['ab'], 'c']
>>> second_pass([10, '[', 'a', ']'])
[10, ['a']]
>>> second_pass([3, '[', 2... | 478d3e5f5a8adb642110b50dd6268c1c43c254c8 | 20,546 |
def crossval_split_a2d2(imgs_paths: list, masks_paths: list, fold=5):
"""
Splits images and masks by two sets: train and validation by folds with a small stratification by categories 'uu', 'um' and 'umm'.
Possible value for 'fold' is: 1, 2, 3, 4, 5
params:
imgs_paths : li... | 58d273d4093b8c56d9398c159bbcd06aae6c6742 | 20,547 |
def quickSortIntersection(dataList, keyList, discardList):
"""
quickSortIntersection recursively sorts the list of values usinga
quick sort algorithm.
"""
if len(keyList) <= 1:
return keyList
else:
lessData = []
lessKey = []
moreData = []
moreKey = []
... | a92bdd1fc0a380c529fab435eb4c63a24aea2c24 | 20,548 |
import logging
def default_end_point(message):
"""
All Requests whose request type is not determined are routed to this method
Primary function is to log the request received
:param request: request received by the server
:type request: `Message`
:param response: response to send back
:typ... | 03986710696281da3b51c26d6f477ce12016add5 | 20,550 |
import glob
def is_file_exists(file_name: str) -> bool:
"""
Checks if a file exists.
:param file_name: file name to check
:return: True, if the file exists, False otherwise
"""
return len(glob.glob(file_name)) == 1 | 7e8da1f544d40d53f9329e4453e198022330f01c | 20,551 |
def fix_lon(lon):
"""
Fixes negative longitude values.
Parameters
----------
lon: ndarray like or value
Input longitude array or single value
"""
if lon < 0:
lon += 360.
return lon | ab11dca9279399179242537c86cf0af85eedb60e | 20,552 |
def is_unique_chars_v3(str):
"""
If not allowed to use additional data structures, we can compare every character of the string to every other
character of the string.
This will take 0(n ** 2) time and 0(1) space
"""
for char1 in str:
occurrence = 0
for char2 in str:
... | 5c524ffa29b7cdc9d43619da7b299ae0f90d443c | 20,554 |
def split_canonical(canonical):
"""
Split a canonical into prefix and suffix based on value sign #
:param canonical: the canonical to split
:return: prefix and suffix
"""
if '#' not in canonical:
return canonical, ''
if canonical.startswith('#'):
return '', canonical[1:].str... | 1a3f7e17cfcc9fa88d63d72fced5435c92f2ea10 | 20,555 |
def decode_content(content, encoding=None):
"""
解码内容为文本
:param content: 二进制内容
:param encoding: 编码
:return: 文本
"""
try:
if encoding is not None:
return content.decode(encoding)
return content.decode('utf-8')
except Exception:
try:
return con... | b257a3c925cb9474ee4842a6fa063a92f72c6cd7 | 20,556 |
import argparse
def build_arg_parser():
"""
:return: argparse.ArgumentParser() filled with the standard arguments for a training session.
Might need to be enhanced for some models.
"""
parser = argparse.ArgumentParser()
parser.add_argument('--data', type=str, default='./dat... | eb9552b23407506ba95ed55c15337a8840013d83 | 20,559 |
import re
def regularize_number(item):
"""
transform the item into hexadecimal like "0xff"
:param item:
:return:
"""
item = "".join(filter(str.isalnum, item)) # remove the non-number and non-alpha
item = re.sub(r'[g-z]|[G-Z]', "", item) # remove the illegal alpha except for (a-f,A-F)
... | 809fec6708f4e7c8e43fda2c533bfbe49bc4ac83 | 20,560 |
import six
def force_bytes(s):
"""Force to a bytes type (not unicode)"""
if issubclass(type(s), six.binary_type):
return s
if issubclass(type(s), six.text_type):
return s.encode("utf-8")
return ValueError(s) | 4dc6216a0078268e49557d79f083a18031273c45 | 20,561 |
from typing import Dict
def polymerize(template: str, rules: Dict[str, str]) -> str:
"""Polymerize a new polymer according to the given `template` and `rules`"""
new_polymer: str = ""
for i in range(len(template) - 1):
pair = template[i : i + 2]
insertion = rules[pair]
new_polymer ... | 8cfbf233f2fc31f1326c0ab6e4676e5ad0a725ab | 20,562 |
def _format_yaml_load(data):
"""
Reinsert '\n's that have been removed fom comments to make file more readable
:param data: string to format
:return: formatted string
"""
# ptr = 0
# cptr = data[ptr:].find('comment: ')
data = data.replace("\n", "\n\n")
return data | 499cd37a75d9badb4ba3bc853a6152036bc2e66b | 20,564 |
def TransformName(r, undefined=''):
"""Returns a resorce name from an URI.
Args:
r: JSON-serializable object.
undefined: Returns this value if the resource cannot be formatted.
Returns:
A project name for selfLink from r.
"""
if r:
try:
return r.split('/')[-1]
except AttributeError... | 6dafaa2b3f0e187fc9bc9238e3a7a06a895675fd | 20,565 |
def get_username(request):
"""Returns the username from request."""
# Retrieve the username either from a cookie (when logging out) or
# the authenticated user.
username = "not-login"
if hasattr(request, "user"):
username = request.user.username
if request.session.get('staff', False)... | cdd19d9715c20a4bc7f20be9b53926b87be671da | 20,566 |
import hashlib
def get_methods():
"""
Returns a list of methods to use while hashing, along with
constructers for the hashing algorithm
"""
return [
{
"algorithm": "md5",
"hasher": hashlib.md5()
}, {
"algorithm": "sha1",
"hasher": ha... | df655768d4680d211512a95bfc0b27ac0114a82c | 20,567 |
def extractTitles(df):
"""EXTRACT title column with Mr,Mrs,Miss,Master or rare
Note: this function was adapted from an idea posted online. Please see README file for reference [2]"""
df['Title'] = df.Name.str.extract(' ([A-Za-z]+)\.', expand=False)
#Collapse less frequent titles into larger groups f... | 24a121688d783733f969be10ea470159e3becd55 | 20,568 |
def west_valley(parcels):
"""
Dummy for presence in West Valley.
"""
in_wv = parcels['mpa'].isin([
'AV', 'BU', 'EL', 'GB', 'GL', 'GO', 'LP', 'PE', 'SU', 'TO', 'WI', 'YO'
])
return (parcels['is_MC'] & in_wv).astype(int) | fec326f82b21acb0cab670904b76096f84445e4d | 20,569 |
import os
from pathlib import Path
def get_data_home():
"""
DATA_HOME is determined using environment variables.
The top priority is the environment variable $DICODILE_DATA_HOME which is
specific to this package.
Else, it falls back on XDG_DATA_HOME if it is set.
Finally, it defaults to $HOME/... | 8339be48914e3fc23b688404e7c375159f526d82 | 20,571 |
import sys
def sanitized_argv(cli_args=None):
"""
Return a list of arguments where -- may have been inserted.
By default argparse gets confused with commands like the following:
$ remote foo --yay bar
The '--yay' argument should be sent to the "foo" command, but argparse
interprets it as a "r... | 008924908784339e2149253b82deef898feedc05 | 20,573 |
from typing import Dict
def _run_compatibility_patches(json_data: Dict) -> Dict:
"""Patch the incoming JSON to make it compatible.
Over time the structure of the JSON information used to dump a workflow
has changed. These patches are to guarantee that an old workflow is
compliant with the new structu... | e00776cfb89499fbac71cf867830fc00631ac600 | 20,575 |
def unsplit_to_tensors(tuples):
"""Get original tensor list from list of tensor tuples.
Args:
tuples: list of tensor tuples.
Returns:
original tensor list.
"""
return [t for tup in tuples for t in tup] | b5df94421ade286ef5ff9bb4c422c5201babe36f | 20,577 |
def getLivenessPoints(liveness):
"""
histogram points for the liveness plot. It will be used for a plot like:
^
| *
| * * *
| ** ***
| *********
+-------------->
schedule index.
For example, if the livenesses are [3,5], the points will be,
[[0,3],[1,3]... | 84dac2f0b29c957727354dd06863820c2e56a866 | 20,578 |
from urllib.parse import urlencode
def oauth_url(client_id, permissions=None, server=None, redirect_uri=None):
"""A helper function that returns the OAuth2 URL for inviting the bot
into servers.
Parameters
-----------
client_id : str
The client ID for your bot.
permissions : :class:`P... | bf7ca1957153ff938334927744804891010c0c26 | 20,579 |
def extract_locations_and_object_types(
object_info,
classification=False,
object_types_format="name"
):
"""
:param object_info:
:param classification:
:param object_types_format:
:return:
"""
locations = [obj.location for obj in object_info]
if classification:... | 75d7baf7f7235c485732f5f2183719599e25004b | 20,580 |
def git(orig): # pragma: no cover
""" most git commands play nicer without a TTY """
cmd = orig.bake(_tty_out=False)
return cmd | 9c14f8ff4bd7a74112e2400af02657ac49ffa2bd | 20,582 |
def get_menu_item(menu):
"""
Asks user to choose a menu item from one of the menu
dictionaries defined above
Args:
(dictionary) menu - dict of menu items
Returns:
(str) selection - key of menu item chosen
"""
while True:
print('------------')
print('Menu Optio... | 603ed07bdd7b51d9539cb0251f42e0affc1001cf | 20,584 |
def _sharded_checkpoint_pattern(process_index, process_count):
"""Returns the sharded checkpoint prefix."""
return f"shard-{process_index:05d}-of-{process_count:05d}_checkpoint_" | 67e0b91b8b3ac9d5c69ec662f6c7931c90a79225 | 20,587 |
def size(self, dim=None):
"""
Size function
"""
if dim is not None:
return self.shape[dim]
else:
return self.shape | fac1bde25485088f2aab93a4b27e249f694762b1 | 20,588 |
def parse_arg(arg):
"""
Parses arguments for convenience. Argument can be a
csv list ('a,b,c'), a string, a list, a tuple.
Returns a list.
"""
# handle string input
if type(arg) == str:
arg = arg.strip()
# parse csv as tickers and create children
if ',' in arg:
... | 45154fbdd9b6ecfafebbee0ec47a6185799a773a | 20,589 |
from typing import Dict
def tags_string(tags: Dict[str, str]) -> str:
"""
tags_string generates datadog format tags from dict.
"""
s = ''
for k in tags:
s += f'{k}:{tags[k]},'
return s.rstrip(',') | 203243c2960bb4fb75a832bc014484aa5b9dec9c | 20,590 |
def _process_cdf_array(cdf_array, process_type="long"):
"""Reshapes a CDF array for training/plotting.
Converts shape of cdf array between (n_eval, n_obs) (wide format)
and (n_eval*n_obs,) (long format).
Args:
cdf_array: (np.ndarray) Input cdf_array in long / wide format.
shape... | 2a2b9b5b038b23f4668c9ac25b3671aeb4f6d1c4 | 20,591 |
from typing import Optional
from typing import Set
def user_string(
prompt: str, allow_empty: bool = False, disallowed_values: Optional[Set[str]] = None
) -> str:
"""Get a string from the user"""
value = input(f"{prompt} ").strip()
while True:
if disallowed_values is not None and next(
... | 11f303242500db99828e8c6edb1f9543baaee3f7 | 20,592 |
def if_elif_else(value, condition_function_pair):
"""
Apply logic if condition is True.
Parameters
----------
value : anything
The initial value
condition_function_pair : tuple
First element is the assertion function, second element is the
logic function to execute if a... | ff2e5315e3bf7ad3e8fa23941e17d7052d6e3ebc | 20,593 |
import functools
def compose(*functions):
"""Compose a list of function to one."""
return functools.reduce(lambda f, g: lambda x: f(g(x)), functions,
lambda x: x) | 7173b623c587a08d43e097a79cabb3f861026a4d | 20,594 |
from typing import List
def nest_list(inp: list, col_cnt: int) -> List[list]:
"""Make a list of list give the column count."""
nested = []
if len(inp) % col_cnt != 0:
raise ValueError("Missing value in matrix data")
for i in range(0, len(inp), col_cnt):
sub_list = []
for n in r... | 6d3ca2e2d4cb61d68279ffad773831a7ba203eae | 20,595 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.