content stringlengths 39 14.9k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
import pickle
def load_pickle(filename):
"""Load Pickfle file"""
filehandler = open(filename, 'rb')
return pickle.load(filehandler) | f93b13616f94c31bc2673232de14b834a8163c5f | 970 |
def is_str_str_dict(x):
"""Tests if something is a str:str dictionary"""
return isinstance(x, dict) and all(
isinstance(k, str) and isinstance(v, str) for k, v in x.items()
) | ce6230714c0526764f2cc67e4dedf598acd28169 | 982 |
def _ensureListLike(item):
"""
Return the item if it is a list or tuple, otherwise add it to a list and
return that.
"""
return item if (isinstance(item, list) or isinstance(item, tuple)) \
else [item] | 1c602a1fcf8dd6a5b4583264e63e38747f5b0d50 | 983 |
import io
def get_file_from_gitlab(gitpkg, path, ref="master"):
"""Retrieves a file from a Gitlab repository, returns a (StringIO) file."""
return io.StringIO(gitpkg.files.get(file_path=path, ref=ref).decode()) | 7eccad01a538bdd99651b0792aff150f73e82cdd | 984 |
def count_disordered(arr, size):
"""Counts the number of items that are out of the expected
order (monotonous increase) in the given list."""
counter = 0
state = {
"expected": next(item for item in range(size) if item in arr),
"checked": []
}
def advance_state():
state... | bb708e7d862ea55e81207cd7ee85e634675b3992 | 986 |
def every(n_steps):
"""Returns True every n_steps, for use as *_at functions in various places."""
return lambda step: step % n_steps == 0 | 02fc6bc59fa6f223b681539baeae32c40bd9577e | 991 |
def calc_batch_size(num_examples, batches_per_loop, batch_size):
"""Reduce the batch size if needed to cover all examples without a remainder."""
assert batch_size > 0
assert num_examples % batches_per_loop == 0
while num_examples % (batch_size * batches_per_loop) != 0:
batch_size -= 1
return batch_size | 3c394813a98a8414645f633a519001937247e8b0 | 992 |
def has_admin_access(user):
"""Check if a user has admin access."""
return user == 'admin' | d178861bee504f6f3026c9e495d56cc8d2d7c3d3 | 993 |
def flatten_mock_calls(mock):
"""
Flatten the calls performed on a particular mock object,
into a list of calls with arguments.
"""
result = []
for call in mock.mock_calls:
call = list(call)
call_name = call[0]
if '.' in str(call_name):
call_name = str(call_na... | 7c41025382f4ca25db1ccd328e9eb17e1d72a01a | 995 |
def update_not_existing_kwargs(to_update, update_from):
"""
This function updates the keyword aguments from update_from in
to_update, only if the keys are not set in to_update.
This is used for updated kwargs from the default dicts.
"""
if to_update is None:
to_update = {}
to_update... | a66de151e6bc6d8f5b2f1b0ff32e30d2c8cb5277 | 996 |
from typing import List
def add_multiple_package(package_list: List[str]) -> str:
"""
Generate latex code to add multiple package to preamble
:param package_list: List of package to add in preamble
"""
usepackage_command_list = []
for package in package_list:
usepackage_command_list.a... | 90bdd0a521c094d92c35ef92e62d6b43f6b135b4 | 1,002 |
def commit_veto(environ, status, headers):
"""Veto a commit.
This hook is called by repoze.tm in case we want to veto a commit
for some reason. Return True to force a rollback.
By default we veto if the response's status code is an error code.
Override this method, or monkey patch the instancemeth... | 9fc96fe8cdbedde20cb325e189b71d9df94cf176 | 1,004 |
import torch
def one_hot(y, num_dim=10):
"""
One Hot Encoding, similar to `torch.eye(num_dim).index_select(dim=0, index=y)`
:param y: N-dim tenser
:param num_dim: do one-hot labeling from `0` to `num_dim-1`
:return: shape = (batch_size, num_dim)
"""
one_hot_y = torch.zeros(y.size(0), num_d... | 694bfea18ecbb5c5737e0d38c0aa0f5f52a82a55 | 1,007 |
def extend_dict(x, *y):
"""Similar to Object.assign() / _.extend() in Javascript, using
'dict.update()'
Args:
x (dict): the base dict to merge into with 'update()'
*y (dict, iter): any number of dictionary or iterable key/value
pairs to be sequentially merged into 'x'. Skipped i... | f10a5bc7d5ed3646e6a9f8f9535a16bd800c7fcd | 1,012 |
def make_general_csv_rows(general_csv_dict):
"""
Method for make list of metrics from general metrics dict.
Rows using in general metrics writer
:param general_csv_dict: dict with all metrics
:type general_csv_dict: dict
:return: all metrics as rows
:rtype: list
"""
rows = []
f... | 45ca165d312b39cd0b7088e0bcbfb402a92e7e2b | 1,015 |
def get_speakable_timestamp(timestamp):
"""Return a 'speakable' timestamp, e.g. 8am, noon, 9pm, etc."""
speakable = f"{timestamp.strftime('%I').lstrip('0')} {timestamp.strftime('%p')}"
if speakable == '12 PM':
return 'noon'
elif speakable == '12 AM':
return 'midnight'
return speakab... | 0b724686ebd5d3152d9017dc456d2945c78be0ee | 1,016 |
import torch
def _featurize(inputs,model):
"""
Helper function used to featurize exemplars before feeding into
buffer.
"""
with torch.no_grad():
# Forward pass
outputs = model(*inputs).detach() #Featurize raw exem
return outputs | 191fd1b362f38309a35618284fcf3f1910a06bd6 | 1,017 |
def make_file_prefix(run, component_name):
"""
Compose the run number and component name into string prefix
to use with filenames.
"""
return "{}_{}".format(component_name, run) | 73ef37d75d9e187ee49ee058958c3b8701185585 | 1,022 |
import base64
def multibase_b64decode(data):
"""
Follow forge's base64 urlsafe encode convention to decode string
Args:
data(string): encoded string
Returns: bytes
Examples:
>>> multibase_b64decode('aGVsbG8')
b'hello'
"""
if isinstance(data, str):
data =... | fdbc0f937e33d7994737a3a515973598cac3debd | 1,025 |
import hashlib
def sha1_file(filename):
"""
Return the hex string representation of the SHA1 checksum of the filename
"""
s = hashlib.sha1()
with open(filename, "rb") as f:
for line in f:
s.update(line)
return s.hexdigest() | b993ac9f025d69124962905f87b1968617bb33f5 | 1,032 |
def read_from_file(file_path):
"""
Read a file and return a list with all the lines in the file
"""
file_in_list = []
with open(file_path, 'r') as f:
for line in f.readlines():
file_in_list.append(line)
return file_in_list | 5fef3a3f50528c1a9786451666ae7e43be282bf9 | 1,033 |
import sqlite3
def cn(DB):
"""Return the cursor and connection object."""
conn = sqlite3.connect(DB)
c = conn.cursor()
return (c,conn) | 76abbec283d45732213f8b94031242146cdb4ee0 | 1,043 |
import copy
def partially_matched_crossover(random, mom, dad, args):
"""Return the offspring of partially matched crossover on the candidates.
This function performs partially matched crossover (PMX). This type of
crossover assumes that candidates are composed of discrete values that
are permutations... | b0d5132cf4ca14095f3d7c637cb50db3fe37d244 | 1,044 |
import re
def regex_trim(input, regex, replace=''):
"""
Trims or replaces the regex match in an input string.
input (string): the input string to search for matches
regex (string): regex to match
replace (string - optional): a string to replace any matches with. Defaults to trimming the match.
"""
return re.s... | 169bfaa0d2bfd7a1f32c1e05a63b41993f82bf4b | 1,045 |
def prepare_definitions(defs, prefix=None):
"""
prepares definitions from a dictionary
With a provided dictionary of definitions in key-value pairs and builds them
into an definition list. For example, if a dictionary contains a key ``foo``
with a value ``bar``, the returns definitions will be a li... | ddc6d14cc18f8afba766efee65ab365df1d226c2 | 1,048 |
def extract_job_url(job):
"""
parse the job data and extract the str for the URL of the job posted
params:
job str: html str representation from bs4
returns:
url str: relative URL path of the job ad
"""
return job.a["href"] | 7517badcc2814e641c04a8f880353d897d434b7f | 1,049 |
import random
def check_random_state(seed):
"""
Turn seed into a random.Random instance
If seed is None, return the Random singleton used by random.
If seed is an int, return a new Random instance seeded with seed.
If seed is already a Random instance, return it.
Otherwise raise ValueError.
... | 347481de01f4a3bba59bc9a2c484c10d4857e1e2 | 1,055 |
def get_user_playlists(spotipy_obj, username):
"""Gets and returns all Spotify playlists owned by the username specified.
Parameters:
spotipy_obj: Spotipy object
username: Spotify username
Returns:
List of dictionaries, each dictionary a Spotify playlist object.
"""
# Gra... | 90c06e0ddd91a7a84f4d905dd9334f9b4c27f890 | 1,060 |
def compute_median_survival_time(times, surv_function):
"""
Computes a median survival time estimate by looking for where the survival
function crosses 1/2.
Parameters
----------
times : 1D numpy array
Sorted list of unique times (in ascending order).
surv_function : 1D numpy array... | 22103bc705acb791c0937a403aa9c34e9145e1c2 | 1,063 |
def aggregate_ant(data, sub_num, response_type="full"):
"""
Aggregate data from the ANT task.
Calculates various summary statistics for the ANT task for a given subject.
Parameters
----------
data : dataframe
Pandas dataframe containing a single subjects trial data for the task.
su... | be01651d450560a5c36bc6240025fe59352d6347 | 1,064 |
def display_ordinal_value(glyph: str):
"""Displays the integer value of the given glyph
Examples:
>>> display_ordinal_value('🐍')\n
128013
>>> display_ordinal_value('G')\n
71
>>> display_ordinal_value('g')\n
103
"""
return ord(glyph) | 7daa53180023bfec2968308d463ac615a83a4e55 | 1,072 |
import re
def tokens(s):
"""Return a list of strings containing individual words from string s.
This function splits on whitespace transitions, and captures apostrophes
(for contractions).
>>> tokens("I'm fine, how are you?")
["I'm", 'fine', 'how', 'are', 'you']
"""
words = re.findall(r... | aee0b6fad2f9107c893496f1f3807e80c9d2e44b | 1,079 |
def potatoes(p0, w0, p1):
"""
- p1/100 = water1 / water1 + (1 - p0/100) * w0
=> water1 = w0 * p1/100 * (1 - p0/100) / (1 - p1/100)
- dry = w0 * (1 - p0/100)
- w1 = water1 + dry = w0 * (100 - p0) / (100 - p1)
Example:
98/100 = water1 / water1 + (1- 99/100) * 100
water1 = 49
w1 = 49... | f2955a58db3a48c64b6acc4980e663f33332aeea | 1,084 |
def relacao(lista):
"""Crie uma função que recebe uma lista de números reais e retorna uma outra lista de tamanho 3 em que
(i) o primeiro elemento é a quantidade de números maiores que zero,
(ii) o segundo elemento é a quantidade de números menores que zero e
(iii) o último elemento é a quantidade de... | 39e45d8221d5d5b7322ebec5aa3f761d9e2ef413 | 1,087 |
def german_weekday_name(date):
"""Return the german weekday name for a given date."""
days = [u'Montag', u'Dienstag', u'Mittwoch', u'Donnerstag', u'Freitag', u'Samstag', u'Sonntag']
return days[date.weekday()] | 7d2919c61438ec913abe38cccd924bb69f866655 | 1,089 |
def identity_func(x):
"""The identify (a.k.a. transparent) function that returns it's input as is."""
return x | 06e0296c338d68663aa87d08b21f84919be3f85e | 1,090 |
def make_choice_validator(
choices, default_key=None, normalizer=None):
"""
Returns a callable that accepts the choices provided.
Choices should be provided as a list of 2-tuples, where the first
element is a string that should match user input (the key); the
second being the value associat... | 65ac672f16a1031a9051bc4f6769c6b1b88db727 | 1,091 |
def gen_event_type_entry_str(event_type_name, event_type, event_config):
"""
return string like:
{"cpu-cycles", PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES},
"""
return '{"%s", %s, %s},\n' % (event_type_name, event_type, event_config) | ca89c19b45f182b8a7ae74ab76f3f42bddf46811 | 1,092 |
from pathlib import Path
def get_force_charge() -> str:
"""
Gets the command object for the force charge command
Returns:
The command object as a json string
"""
force_charge = Path('force_charge.json').read_text()
return force_charge | c67277c62664419c3b4a19ae57ea6de027c60416 | 1,093 |
def _non_string_elements(x):
"""
Simple helper to check that all values of x are string. Returns all non string elements as (position, element).
:param x: Iterable
:return: [(int, !String), ...]
"""
problems = []
for i in range(0, len(x)):
if not isinstance(x[i], str):
p... | 974715622949157693084823a52a88973b51d100 | 1,095 |
def get_filename_pair(filename):
"""
Given the name of a VASF data file (*.rsd) or parameter file (*.rsp) return
a tuple of (parameters_filename, data_filename). It doesn't matter if the
filename is a fully qualified path or not.
- assumes extensions are all caps or all lower
"""
param_file... | f6eb5a64cf472f230c5806447d9c2ee8ae43a71d | 1,097 |
def addBenchmark(df):
"""Add benchmark to df."""
# Compute the inverse of the distance
distance_inv = (1. / df.filter(regex='^distance*', axis=1)).values
# Extract the value at the nearest station
values = df.filter(regex='value_*', axis=1)
# Compute the benchmark
numer = (distance_inv * ... | 62c63215d622c46bed8200f97ad55b985e2beb20 | 1,100 |
def is_file_like(f):
"""Check to see if ```f``` has a ```read()``` method."""
return hasattr(f, 'read') and callable(f.read) | 9eee8c8f4a6966d1db67fb4aa9149e2fbd390fb9 | 1,101 |
def check_protocol(protocol):
"""
Check if a given protocol works by computing the qubit excitation probabilities
"""
qubit_weight = {}
qubit_weight[protocol[0][0][0]] = 1.0
for pair_set in protocol:
for i, j, p in pair_set:
qubit_weight[j] = qubit_weight[i] * (1.0 - p)
... | 8b9d0a8e329a340718d37bc79066be4a05cf2d20 | 1,102 |
def choose_first_not_none(*args):
""" Choose first non None alternative in args.
:param args: alternative list
:return: the first non None alternative.
"""
for a in args:
if a is not None:
return a
return None | fe3efba85251161cd0a6ecb50583cc443cd04dc0 | 1,103 |
def add_dict(dct1, dct2):
"""Returns a new dictionaries where the content of the dictionaries `dct1`
and `dct2` are merged together."""
result = dct1.copy()
result.update(dct2)
return result | eba785e4d00534e94c1bdde413603d64e18aac05 | 1,105 |
def mapdict(itemfunc, dictionary):
"""
Much like the builtin function 'map', but works on dictionaries.
*itemfunc* should be a function which takes one parameter, a (key,
value) pair, and returns a new (or same) (key, value) pair to go in
the dictionary.
"""
return dict(map(itemfunc, diction... | 1f0573410f82acb1f3c06029cf4bfaccd295e1ac | 1,110 |
def get_classpath(obj):
"""
Return the full module and class path of the obj. For instance,
kgof.density.IsotropicNormal
Return a string.
"""
return obj.__class__.__module__ + "." + obj.__class__.__name__ | bf986e2b27dd8a216a2cc2cdb2fb2b8a83b361cc | 1,112 |
def _get_should_cache_fn(conf, group):
"""Build a function that returns a config group's caching status.
For any given object that has caching capabilities, a boolean config option
for that object's group should exist and default to ``True``. This
function will use that value to tell the caching decora... | 7a11124c640bfb3ced28e2d9395593b70dc85a0a | 1,128 |
def horizontal_move(t, h_speed=-2/320):
"""Probe moves horizontally at h_speed [cm/s]"""
return 0.*t, h_speed*t, 2/16 + 0*t | d9cf0e5b968e7d8319b7f63f7d1d7a4666484ad3 | 1,134 |
def categories_report(x):
"""Returns value counts report.
Parameters
----------
x: pd.Series
The series with the values
Returns
-------
string
The value counts report.
str1 = False 22 | True 20 | nan 34
str2 = False (22) | True (20) | nan (34)
... | 695ccd73ee73a13e92edbdf0eb242121d136ddbb | 1,135 |
def euler(step, y0):
"""
Implements Euler's method for the differential equation dy/dx = 1/(2(y-1)) on the interval [0,4]
"""
x = [0]
index_x = 0
while x[index_x] < 4:
x.append(x[index_x] + step)
index_x += 1
index_y = 0
y = [y0]
def yprime(y):
yprime = 1 / (2... | 89c6e6409a1c43ce4766507fba2f401bb01cfbb8 | 1,142 |
import logging
def update_softwaretitle_packages(api, jssid, pkgs):
"""
Update packages of software title
:param jssid: Patch Software Title ID
:param pkgs: dict of {version: package, ...}
:returns: None
"""
logger = logging.getLogger(__name__)
data = api.get(f"patchs... | 0acb3dfbff0e85a2e8a876d5e5d484c4d1e52068 | 1,143 |
from typing import Sequence
def _table(*rows: Sequence) -> str:
"""
>>> _table(['a', 1, 'c', 1.23])
'|a|1|c|1.23|'
>>> _table(['foo', 0, None])
'|foo|||'
>>> print(_table(['multiple', 'rows', 0], ['each', 'a', 'list']))
|multiple|rows||
|each|a|list|
"""
return '\n'.join([
... | d566da2ad9240e73b60af00d3e4b4e25607234b4 | 1,146 |
import difflib
def lines_diff(lines1, lines2):
"""Show difference between lines."""
is_diff = False
diffs = list()
for line in difflib.ndiff(lines1, lines2):
if not is_diff and line[0] in ('+', '-'):
is_diff = True
diffs.append(line)
return is_diff, diffs | 50916d46871980fadfd854dc698481a4b0f35834 | 1,150 |
import re
def countBasesInFasta(fastaFile):
"""
Given a fasta file, return a dict where the number of records and
the total number of bases are given by 'records' and 'bases' respectively.
"""
recordRE = re.compile(r'^>')
whiteSpaceRE = re.compile(r'\s+')
total_bases = 0
total_seqs = 0... | 45eaa5b8d36b4bae6b97bb29fdead1efc0aed8c2 | 1,156 |
def linear_search(iterable, item):
"""Returns the index of the item in the unsorted iterable.
Iterates through a collection, comparing each item to the target item, and
returns the index of the first item that is equal to the target item.
* O(n) time complexity
* O(1) space complexity
Args:
iterable:... | bdbd7e70cea79deef1375648bde61067df1d2221 | 1,158 |
def create_MD_tag(reference_seq, query_seq):
"""Create MD tag
Args:
reference_seq (str) : reference sequence of alignment
query_seq (str) : query bases of alignment
Returns:
md_tag(str) : md description of the alignment
"""
no_change = 0
md = []
for ref_base, query_ba... | 4b711521d00af132e8e29fe4fc44785b985c2607 | 1,159 |
import re
def calc_word_frequency(my_string, my_word):
"""Calculate the number of occurrences of a given word in a given string.
Args:
my_string (str): String to search
my_word (str): The word to search for
Returns:
int: The number of occurrences of the given word in the given st... | 15ff723dd2ff089fb12cccb38283f1f75e37079d | 1,160 |
import hashlib
def intmd5(source: str, nbytes=4) -> int:
"""
Generate a predictive random integer of nbytes*8 bits based on a source string.
:param source:
seed string to generate random integer.
:param nbytes:
size of the integer.
"""
hashobj = hashlib.md5(source.encode())
retu... | c03eb99a67af00a4a081423ecca3a724111514e1 | 1,161 |
def _partition_at_level(dendrogram, level) :
"""Return the partition of the nodes at the given level
A dendrogram is a tree and each level is a partition of the graph nodes.
Level 0 is the first partition, which contains the smallest snapshot_affiliations, and the best is len(dendrogram) - 1.
The higher the level ... | b179127076c386480c31a18a0956eb30d5f4ef2a | 1,167 |
def _format_stages_summary(stage_results):
"""
stage_results (list of (tuples of
(success:boolean, stage_name:string, status_msg:string)))
returns a string of a report, one line per stage.
Something like:
Stage: <stage x> :: SUCCESS
Stage: <stage y> :: FAILED
Stage: <s... | 2f5c757342e98ab258bdeaf7ffdc0c5d6d4668ca | 1,174 |
import socket
def canonical_ipv4_address(ip_addr):
"""Return the IPv4 address in a canonical format"""
return socket.inet_ntoa(socket.inet_aton(ip_addr)) | edacc70ccc3eef12030c4c597c257775d3ed5fa4 | 1,177 |
def make_chained_transformation(tran_fns, *args, **kwargs):
"""Returns a dataset transformation function that applies a list of
transformations sequentially.
Args:
tran_fns (list): A list of dataset transformation.
*args: Extra arguments for each of the transformation function.
**kw... | 5f24e030df74a0617e633ca8f8d4a3954674b001 | 1,179 |
def generate_outlier_bounds_iqr(df, column, multiplier=1.5):
"""
Takes in a dataframe, the column name, and can specify a multiplier (default=1.5). Returns the upper and lower bounds for the
values in that column that signify outliers.
"""
q1 = df[column].quantile(.25)
q3 = df[column].quantile(.... | 7f096d5f5cf2417cbc161713715a39560efd140a | 1,182 |
import math
from PIL import ImageColor
def indexedcolor(i, num, npersat=15, lightness=60):
"""Returns an rgb color triplet for a given index, with a finite max 'num'.
Thus if you need 10 colors and want to get color #5, you would call this with (5, 10).
The colors are "repeatable".
"""
nsats = int... | 418a875bc8ae50ce21f9667f46718863ba0f55e3 | 1,185 |
def dot_to_dict(values):
"""Convert dot notation to a dict. For example: ["token.pos", "token._.xyz"]
become {"token": {"pos": True, "_": {"xyz": True }}}.
values (iterable): The values to convert.
RETURNS (dict): The converted values.
"""
result = {}
for value in values:
path = res... | a2c56a01b179d27eabc728d6ff2ec979885d5feb | 1,186 |
from pathlib import Path
def delta_path(base_path: Path, item_path: Path, new_base_path: Path) -> Path:
"""
Removes a base path from an item, and appends result to a new path
:param base_path: The :py:class:`pathlib.Path` to be removed from `item_path`
:param item_path: The :py:class:`pathlib.Path` t... | ec531a011e36f053a8092525faae2047f5f66ccc | 1,189 |
import random
def describe_current_subtask(subtask, prefix=True):
"""
Make a 'natural' language description of subtask name
"""
to_verb = {"AnswerQuestion": "answering a question",
"ArmGoal": "moving my arm",
"DemoPresentation": "giving a demo",
"Find": "fi... | 628c699201c26242bd72c6066cba07cce54b14ca | 1,197 |
import re
def parse_date(deadline_date):
"""
Given a date in the form MM/DD/YY or MM/DD/YYYY, returns
the integers MM, DD, and YYYY (or YY) in this order.
"""
deadline_split = re.split('\\/|\\-', deadline_date)
return int(deadline_split[0]), int(deadline_split[1]), int(deadline_split[2]) | 0ded6bccce8437aad61cfa5ff121c5ed0595849b | 1,199 |
import re
def get_file_name(part):
"""get file name using regex from fragment ID"""
return re.findall(r"='(.*\-[a-z]+).*", part)[0] | 30c8867d8e14b04c593359f1c16d9bf324711ba0 | 1,201 |
def black_color_func(word, font_size, position, orientation,
random_state=None, **kwargs):
"""Make word cloud black and white."""
return("hsl(0,100%, 1%)") | d5e874a4f62d30abcba29476d0ba7fc3a31b0ca6 | 1,210 |
def detect_label_column(column_names):
""" Detect the label column - which we display as the label for a joined column.
If a table has two columns, one of which is ID, then label_column is the other one.
"""
if (column_names and len(column_names) == 2 and "id" in column_names):
return [c fo... | 40524e7ed0878316564ad8fd66a2c09fc892e979 | 1,211 |
def table(custom_headings, col_headings_formatted, rows, spec):
"""
Create a LaTeX table
Parameters
----------
custom_headings : None, dict
optional dictionary of custom table headings
col_headings_formatted : list
formatted column headings
rows : list of lists of cell-str... | 0ca28fce26fc7476aa5b88a621c5476ae8d381ce | 1,213 |
from typing import List
def split_to_sublists(initial_list:list, n:int, strict:bool=True) -> List[list]:
"""Takes a list and splits it into sublists of size n
Parameters
----------
initial_list : list
The initial list to split into sublists
n : int
The size of each sublist
s... | fcca74f9814020c99aaf8b31f092ca3ca9533216 | 1,215 |
def sectionize(parts, first_is_heading=False):
"""Join parts of the text after splitting into sections with headings.
This function assumes that a text was splitted at section headings,
so every two list elements after the first one is a heading-section pair.
This assumption is used to join sections wi... | 402832d55268dc808888f94b95e3a1c991394041 | 1,217 |
def byte_compare(stream_a, stream_b):
"""Byte compare two files (early out on first difference).
Returns:
(bool, int): offset of first mismatch or 0 if equal
"""
bufsize = 16 * 1024
equal = True
ofs = 0
while True:
b1 = stream_a.read(bufsize)
b2 = stream_b.read(bufsi... | 59adfe50fefdb79edd082a35437018d4b954ec75 | 1,218 |
import re
def is_regex(param):
"""
判断参数是否是合法正则表达式字符串
:param param: {String} 参数
:return: {Boolean} 是否是合法正则表达式
"""
try:
re.compile(param)
return True
except re.error:
return False | 6a3ee33e68e33d3557db546beadc005235360080 | 1,219 |
def min_count1(lst):
"""
Get minimal value of list, version 1
:param lst: Numbers list
:return: Minimal value and its count on the list
"""
if len(lst) == 0:
return []
count = 0
min_value = lst[0]
for num in lst:
if num == min_value:
count += 1
el... | b441d0a37534909e9a990b91a953d4022698c04b | 1,220 |
def exactly_one_topping(ketchup, mustard, onion):
"""Return whether the customer wants exactly one of the three available toppings
on their hot dog.
"""
return True if int(ketchup) + int(mustard) + int(onion) == 1 else False | 214c95d35c116993dc78740d5d16b874122960ed | 1,221 |
def strip_line_endings(data: list) -> list:
"""Removes line endings(\n). Removes item if only contains \n."""
return [i.rstrip("\n") for i in data if i != "\n"] | 5383b1bc3884395459ca63b6f15c0a1091eaaaf0 | 1,222 |
def taxon_id(_):
"""
Always returns 10090, the mouse taxon id.
"""
return 10090 | 117fe7f8d56eb9be4ee2b0f4d782b806576faedf | 1,225 |
def getLogisticModelNames(config):
"""
Get the names of the models present in the configobj
Args:
config: configobj object defining the model and its inputs.
Returns:
list: list of model names.
"""
names = []
lmodel_space = config
for key, value in lmodel_space.items():... | f7f82b12eb50a58c92970b5c2a8f99eb01945523 | 1,227 |
def mp0(g0):
"""Return 0th order free energy."""
return g0.sum() | 5aa3580fec1322bd7b4e357ec6bee4d52fae592e | 1,228 |
import socket
import fcntl
import struct
def get_ip_address(dev="eth0"):
"""Retrieves the IP address via SIOCGIFADDR - only tested on Linux."""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(s.fileno(),0x8915,struct.pack('256s', dev[:15]))[20:24])... | 96f59f17937543ed9cd4652af4703eaf975b8069 | 1,234 |
def gen_fov_chan_names(num_fovs, num_chans, return_imgs=False, use_delimiter=False):
"""Generate fov and channel names
Names have the format 'fov0', 'fov1', ..., 'fovN' for fovs and 'chan0', 'chan1', ...,
'chanM' for channels.
Args:
num_fovs (int):
Number of fov names to create
... | 417490259c42a52c58aab418fbb63185602e6750 | 1,240 |
def flatten_list(a_list, parent_list=None):
"""Given a list/tuple as entry point, return a flattened list version.
EG:
>>> flatten_list([1, 2, [3, 4]])
[1, 2, 3, 4]
NB: The kwargs are only for internal use of the function and should not be
used by the caller.
"""
if parent_list... | dd6c9c66a370e65744ede40dfdc295b0ec63379a | 1,243 |
def unpad_pkcs7(data):
"""
Strips PKCS#7 padding from data.
Raises ValueError if padding is invalid.
"""
if len(data) == 0:
raise ValueError("Error: Empty input.")
pad_value = data[-1]
if pad_value == 0 or pad_value > 16:
raise ValueError("Error: Invalid padding.")
for i ... | 27e59b8a880c130997f19814135c09cb6e94354d | 1,247 |
import re
def remove_multispaces(text):
""" Replace multiple spaces with only 1 space """
return [re.sub(r' +', " ",word) for word in text] | 0b87f6a4b0d49931b3f4bec6f9c313be05d476f0 | 1,252 |
def num_fixed_points(permutation):
"""
Compute the number of fixed points (elements mapping to themselves) of a permutation.
:param permutation: Permutation in one-line notation (length n tuple of the numbers 0, 1, ..., n-1).
:return: Number of fixed points in the permutation.
.. rubric:: Examples... | 124713cd4c90988c43630a74881e7107ff748682 | 1,257 |
def iscode(c):
"""
Tests if argument type could be lines of code,
i.e. list of strings
"""
if type(c) == type([]):
if c:
return type(c[0]) == type('')
else:
return True
else: return False | e60da6c05922ff1e67db15fa4caa1500a8f470c7 | 1,259 |
import re
def split_bucket(s3_key):
"""
Returns the bucket name and the key from an s3 location string.
"""
match = re.match(r'(?:s3://)?([^/]+)/(.*)', s3_key, re.IGNORECASE)
if not match:
return None, s3_key
return match.group(1), match.group(2) | 6b854bdc9d105643a9fa528e6fefd19672451e63 | 1,261 |
def create_dictionary(names, months, years, max_sustained_winds, areas_affected, updated_damages, deaths):
"""Create dictionary of hurricanes with hurricane name as the key and a dictionary of hurricane data as the value."""
hurricanes = dict()
num_hurricanes = len(names)
for i in range(num_hurricanes):
hur... | 5a27d5349113f29d2af55df27a2ee2c2cc524549 | 1,266 |
def generate_file_prefix(bin_params):
""" Use the bin params to generate a file prefix."""
prefix = "bin_"
for j in range(0, len(bin_params)):
if (j + 1) % 2 != 0:
prefix += str(bin_params[j]) + "-"
else:
prefix += str(bin_params[j]) + "_"
return prefix | cc058a64fcab77f6a4794a8bf7edb1e0e86c040c | 1,270 |
def create_scale(tonic, pattern, octave=1):
"""
Create an octave-repeating scale from a tonic note
and a pattern of intervals
Args:
tonic: root note (midi note number)
pattern: pattern of intervals (list of numbers representing
intervals in semito... | f9337289fda2e1b08cd371d3e91cc5a23c9c9822 | 1,276 |
def get_subgraph_pos(G, pos):
""" Returns the filtered positions for subgraph G. If subgraph = original graph then pos will be returned.
Parameters
----------
G : nx.Graph
A graph object.
Pos : dict
A dictionary with nodes as keys and positions as values.
Example
-------... | ca7fc389cc51aaace7a751f2107fe5cfbfd22e6c | 1,280 |
def pds3_label_gen_date(file):
"""Returns the creation date of a given PDS3 label.
:param path: File path
:type path: str
:return: Creation date
:rtype: str
"""
generation_date = "N/A"
with open(file, "r") as f:
for line in f:
if "PRODUCT_CREATION_TIME" in line:
... | c2877fa9246dd0c12c6ea47635ab248dc038b179 | 1,283 |
def harmony(*args):
"""
Takes an arbitrary number of floats and prints their harmonic
medium value. Calculation is done with formula:
number_of_args \ (1 \ item1 + 1 \ item2 + ...)
Args:
*args (tuple): number of arguments with a type: float, integer
Returns:
float: harmonic... | bc66276b3ef27ef0bfd059afa8ca7afd5d9cbb82 | 1,284 |
def pack(pieces=()):
"""
Join a sequence of strings together.
:param list pieces: list of strings
:rtype: bytes
"""
return b''.join(pieces) | ffd0852a16c6292f921e5cf205301171e3a96fd3 | 1,286 |
import inspect
def getsource(obj,is_binary=False):
"""Wrapper around inspect.getsource.
This can be modified by other projects to provide customized source
extraction.
Inputs:
- obj: an object whose source code we will attempt to extract.
Optional inputs:
- is_binary: whether the obje... | 9e97a030c695b9ea50d27abc5253e47be7d4c06a | 1,292 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.