content stringlengths 39 9.28k | sha1 stringlengths 40 40 | id int64 8 710k |
|---|---|---|
def resolve_translation(instance, info, language_code):
"""Gets translation object from instance based on language code."""
return instance.translations.filter(language_code=language_code).first() | 79737c123e09760fb1514bbfad7d73c385a4309a | 16,676 |
import random
def rand_range_float(_range):
""" Generate a random float between a tuple range """
return round(random.uniform(_range[0], _range[1]), 2) | ba2273114a1f2d9fe1df4f30518372e591d9fe2b | 381,048 |
def _not_cal(not_sign, right):
"""
Reverse number
Args:
not_sign (bool): if has not sign
right (bool): right number
Returns:
bool
"""
if not_sign:
right = not right
return right | 4bc0b13d474dd1a80c439c693872b1b5e9e91bfb | 87,778 |
def replaceAll(text, replace_dict):
"""
Replace all the ``replace_dict`` keys by their associated item in ``text``.
"""
for i, j in replace_dict.items():
text = text.replace(i, j)
return text | 3cba9a96e70049d169c5a7d0eeedc2dca777e8cd | 120,256 |
def has_decreased(scores, in_last):
"""Return True iff the score in the last `in_last` descended."""
if in_last >= len(scores):
return True
last = scores[-(in_last + 1)]
for score in scores[-in_last:]:
if score < last:
return True
last = score
return False | 472f01b5573ecf965ab5b7c239904ac113cc0b67 | 30,962 |
def load_file_list(filename):
"""
Load a text file containing a list of filenames (or other strings) as a
Python list.
To obtain a list of DataFile objects, the result can easily be converted
as in the example:
>>> raw_biases = DataFileList(load_file_list('list_of_biases.txt'))
If the lis... | bbb49943b9d5ea6869be35d343ce0f7452d1f6d5 | 646,821 |
def _get_cols_length(table, cols_num):
"""Return the max length of every columns.
"""
cols_length = [0] * cols_num
for row in table:
for (i, col) in enumerate(row):
col_len = len(col)
if col_len > cols_length[i]:
cols_length[i] = col_len
return cols_le... | 02c852944d3d5efc50bec79adfe32f4519803c7a | 583,492 |
def get_snapshots_from_text(query_output):
""" Translate expanded snapshots from `cali-query -e` output into list of dicts
Takes input in the form
attr1=x,attr2=y
attr1=z
...
and converts it to `[ { 'attr1' : 'x', 'attr2' : 'y' }, { 'attr1' : 'z' } ]`
"""
... | f6bfb56329c04ee8e325b5bd82ea417901420a58 | 649,406 |
def get_band_index(band_name):
"""Get the write-index value for a Sentinel-2 image band
For bands 1 through 8, we return the band number. For 8A,
we return 9. For bands above 8A, we add one to the band
number.
Args:
band_name (str): the name of the band, e.g. "nir - 8A"
Return:
... | 16197e5303d259b3502cf20255cd68c514215e3c | 50,224 |
def normalized_age(age):
"""
Normalize age to [-1, 1]
"""
return age / 120 - 1 | 608b3aae05397210f50a28ee5697b2acd91cde4d | 271,038 |
def format_port(port):
"""Render port option."""
return '-p {}'.format(port) if port else '' | bb7dcdb474d11d2e7c9cf6f404b9b4db87321e36 | 157,052 |
def parse_bsub(output):
"""Parse bsub output and return job id.
:param output: stdout of bsub command
:type output: str
:returns: job id
:rtype: str
"""
for line in output.split("\n"):
if line.startswith("Job"):
return line.split()[1][1:-1] | 557377cbc28ba9e1bd516a3ce68132ced5b05b7b | 65,320 |
def sim_to_rgba(similarity, edge_color):
"""Convert similarity to RGBA.
Parameters
----------
similarity : float
Similarity between two answers.
edge_color : tuple
When the graph is plotted, this is the RGB color of the edge
i.e (0, 0, 1).
Returns
-------
tuple ... | 3b609dae4a8bb096333fbed76e9ea3d0ece92952 | 573,948 |
def fib(N):
"""
Avoid redoing the something over and over and store any Fib in a dictionary
to not do it again
Time: o(n)
"""
memo = {}
if N in memo:
return memo[N]
if N <= 2:
f = 1
else:
f = fib(N - 1) + fib(N - 2)
memo[N] = f
return f | c7461f0861844080c6a7bd2371a3644e4d48e441 | 401,632 |
def get_file_contents(filename):
"""
Return contents of a file
"""
with open(filename) as fd:
return fd.read() | b967f4676141afe877e5c33f315cc013b1cc49f6 | 264,445 |
def show_world(world):
"""
Shows the world parameter in the form a string instead of a list, being separated by line breaks (\n)
"""
rendered_world = ""
for line in range(len(world)):
rendered_world += ''.join(world[line])
rendered_world += "\n"
rendered_world = rendered_world.st... | 3b3fcc22e0626e08bdd16a392f3de572bbd3b412 | 324,677 |
import time
def elim_code_36(main_df):
"""
Remove undefined violations (code 36)
"""
start = time.time()
main_df = main_df[main_df['Violation Code']!=36].sort_values('Summons Number',ascending=False)
end = time.time()
print("Eliminate undefined violations time: ", end - start)
print(ma... | 533697c50f716ebd2a156d02f12e6e234f193e14 | 162,594 |
import torch
def sort_batch(seq_len):
"""Sorts torch tensor of integer indices by decreasing order."""
with torch.no_grad():
slens, sidxs = torch.sort(seq_len, descending=True)
oidxs = torch.sort(sidxs)[1]
return oidxs, sidxs, slens.tolist() | bb8861731974a5e561322d7f2cb73b1a0837664f | 119,705 |
def cat_arrays(vector_arrays):
"""Return a new |VectorArray| which a concatenation of the arrays in `vector_arrays`."""
vector_arrays = list(vector_arrays)
total_length = sum(map(len, vector_arrays))
cated_arrays = vector_arrays[0].empty(reserve=total_length)
for a in vector_arrays:
cated_ar... | eb7793aa45e031c374957803e135f5e605f25aa9 | 389,097 |
def unit_temp(ua):
"""
Computes the unit temperature in the given units
"""
return ua.MeV | 65d40ce710195f6111dedf0779199f2932ee3a7d | 543,558 |
def normalize_image(image):
"""Normalizes an image by dividing it by 255
Parameters
----------
image -- a numpy array representing an image
Returns
-------
Normalized image
"""
return image / 255 | b6927305c69c9da8e92503c8e29d7212804adfe3 | 52,917 |
def is_unsupported_size_mechanism_type(size_mechanism: str) -> bool:
"""Whether the given size mechanism is unknown/unsupported."""
return size_mechanism not in {
"fixed",
"len",
"len-in-bytes",
"ivi-dance",
"passed-in",
"passed-in-by-ptr",
"ivi-dance-with... | dd069872d58b9ead8e460d6802a3dc5b37331a6b | 151,910 |
import requests
def registry(url, resource='/registry'):
"""Retrieve the chip spec registry
Args:
url (str): protocol://host:port/path
resource (str): /registry/resource/path (default: /registry)
Returns:
list
Example:
>>> chipmunk.registry(url='http://host:port/path... | 5be4970ddeba9c1a087569a6601596c90e9cede3 | 321,748 |
import re
def regex_replace(value, regex, replace):
"""Replace every string matching the given regex with the replacement"""
return re.sub(regex, replace, value, flags=re.IGNORECASE) | b9b892041fe92399a5ff67c605451b1d406235d9 | 89,975 |
def lookupName(n, names):
"""Check if name is in list of names
Parameters
----------
n : str
Name to check
names : list
List of names to check in
Returns
-------
bool
Flag denoting if name has been found in list (True) or not (False)
"""
if n in names:... | 0fbb97e252f5daf9de52a946c206fa74395b01c6 | 4,924 |
def species_block(spc_dct):
""" Writes the species block of the mechanism file
:param spc_dct: dct containing the species data
:type spc_dct: dct {spc_name:data}
:return spc_str: str containing the species block
:rtype: str
"""
# Get the max species name length
max_len =... | 3ee94eeeb0d23531009a8f218dccbaf8553ae98f | 472,301 |
def column_lettering(colnum: int) -> str:
"""
Converts a zero-based column index into a spreadsheet-style column name
(A[0] to Z[25], then AA[26] to AZ[51], etc). Basically, it's almost base
26, but without a proper sense of zero (in that A is zero, but AA is 26).
"""
assert colnum >= 0
base... | ef92fae57f32cca77e2dfad3183dfcd1f002a002 | 146,009 |
def get_appendix_data(df, table):
"""
Given Appendix A DataFrame df and table name,
return Sequence, Start, and End numbers as lists.
"""
df = df[df['name'] == table]
return df['seq'].tolist(), df['start'].tolist(), df['end'].tolist() | 46ec1a9b5c0c065d9a261981efb5d7bb625971a9 | 648,951 |
def check_password(password, guess):
"""Takes two string variables: the password and the guess. Returns true if
they match, or false if they do not."""
return True if password == guess else False | dc49a94229ddf8db5c2594db735008cfee145b75 | 372,412 |
def get_times(ts_full, ts_system, len_state, sys_position, sys_length):
"""
This is a function specifically designed for TEDOPA systems. It calculates
the proper 'ts' and 'subsystems' input lists for :func:`tmps.evolve` from a
list of times where the full state shall be returned and a list of times
... | 26495149a867ed9b42da4c0db288d81c4db350dc | 33,877 |
from typing import Tuple
import asyncio
import threading
def setup_loop_in_thread() -> Tuple[asyncio.AbstractEventLoop, threading.Thread]:
"""Sets up a new asyncio event loop in a new thread, and runs it forever.
Returns:
A tuple containing the event loop and the thread.
"""
loop = asyncio.ne... | cd8e3c882cd50ddb7fbee4b0653c981c1c3bdb8a | 84,491 |
def secondary_sort(attr: str) -> tuple:
"""
Determine the secondary sort attribute and order when the
primary sort attribute has the same value.
Args:
attr (str): The primary sort attribute
Returns:
tuple: Secondary sort attribute and sort order
"""
if attr in ['wins', 'los... | 54c86b1856baca7f5f1748108bf974e91018a581 | 273,809 |
def colored_pixels(img, thresh):
"""
Find the positions of all pixels in the image that are considered dark
enough for text.
The image is assumed to be grayscale.
Args:
img (numpy.ndarray): Image to check.
thresh (int): Background threshold up to which a pixel is considered
... | c8a045245972e574df27f15219d5642b3e5cd02b | 74,612 |
def _trim(strings):
"""Remove leading and trailing whitespace from each string."""
return [x.strip() for x in strings] | a0c3c6352a27791f3477dae5f807e4d6727b3e6e | 236,837 |
def replace_repetitive_column_names(column_name, buildings):
"""
Returns column_name _unless_ it's one of a few special cases (building names, PIPE names, NODE names, srf names)
:param str column_name: the name of the column
:return: column_name or similar (for repetitive column names)
"""
if co... | 26a6deccee32d87ef99da90f37b3fb929475b0f2 | 443,576 |
def arithmetic_series(a: int, n: int, d: int = 1) -> int:
"""Returns the sum of the arithmetic sequence with parameters a, n, d.
a: The first term in the sequence
n: The total number of terms in the sequence
d: The difference between any two terms in the sequence
"""
return n * (2 * a + (n - 1... | 168f0b07cbe6275ddb54c1a1390b41a0f340b0a6 | 706,463 |
def order_tweets_by_polarity(tweets, positive_highest=True):
"""Sort the tweets by polarity, receives positive_highest which determines
the order. Returns a list of ordered tweets."""
return sorted(tweets, key=lambda x: x.polarity, reverse=positive_highest) | 1b98a8ff71e00f52465f395851c499368003afd1 | 134,623 |
import struct
def calcChecksum(data):
"""Calculate the checksum for an arbitrary block of data.
Optionally takes a 'start' argument, which allows you to
calculate a checksum in chunks by feeding it a previous
result.
If the data length is not a multiple of four, it assumes
it is to be padded with null byte.
... | 7678edec74457c74123ac3de1d1fe83008f67f31 | 512,074 |
def checkio(expression):
"""Check expression for correctly matched brackets."""
stack = []
closers = dict(zip(')]}', '([{'))
for char in expression:
# build up the stack with opening brackets
if char in '([{':
stack.append(char)
# tear down the stack when closing bra... | 73c2b9ec6c70735b822133dde07ffe4127ecc7e5 | 460,684 |
import re
def extract_shortwords(text, length=3):
"""Returns a list with all short words of a particular length
params:
length: Specify the length of words you want to remove
default is 3 letter word length
"""
token_words = re.split(r"\W+", text)
short_words_list = [i for i in token_wor... | 8f91a457be6cfc1d8dab82f160d76934928fd93b | 470,611 |
from datetime import datetime
def time_diff_close_enough(time1, time2, within_minutes):
"""Test whether two datetimes (TIMESTAMP's) are:
1. within a specified number of minutes of each other; and
2. within a specified number (the same number) of minutes of right now.
If both are true, then they are de... | 347cadf36008e4f483515215e8c0a66ea23e34f6 | 134,382 |
def mcastIp2McastMac(ip):
""" Convert a dot-notated IPv4 multicast address string into an multicast MAC address"""
digits = [int(d) for d in ip.split('.')]
return '01:00:5e:%02x:%02x:%02x' % (digits[1] & 0x7f, digits[2] & 0xff, digits[3] & 0xff) | 4713507e933e35b07fa917e04da431d71fe2b38f | 121,805 |
def get_openssl_url_version(version):
"""
Get the OpenSSL URL version part from the openssl version string.
"""
if version[:3] == "3.0":
return "3.0"
tokens = version.split(".")
if len(tokens) > 2:
return ".".join(tokens[:2] + [tokens[2][0],])
return version | 24ef49860a1e658a748b8a5c6dc39f0d38959bb0 | 593,087 |
def dot_product(vector1, vector2):
"""
Returns the dot product between two vectors. It can be
used as a helper function to calculate the multiplication
by two matrices.
"""
result = 0
for i in range(len(vector1)):
result += vector1[i] * vector2[i]
return result | e23cb2cfc480cf1ec0eea0fe3e915ee7bd20a8d3 | 416,871 |
import functools
def optional_parameter_decorator(f):
"""
A decorator for a decorator, allowing the decorator to be used both with
and without arguments applied.
Example:
------
@optional_parameter_decorator
def decorator(func, foo='bar'):
pass
which can now be used as
@... | dd92d18aa3c6159f6dfbfb31267e6a47c3dad237 | 320,595 |
def _generate_cache_key(ctx, spec_name, platform, configuration):
"""
Generate a composite key for (optional) spec_name, platform, and configuration
:param ctx: The context
:param spec_name: name of the spec (or None, use the current spec)
:param platform: the platform (or N... | 41cc6d6e66a4eba8cd74c81d13e8a1b0cd4623c3 | 90,989 |
from typing import List
def get_scores(true_pos: int, false_pos: int, false_neg: int) -> List[float]:
"""
calculate scores of performances with criteria of the computed confusion matrix : precision, recall or sensibility
and F1-score.
:param true_pos: number of correct detections
:type true_pos: ... | a917033018e551f55290f4f9ce20f90ba4c50ad1 | 580,988 |
def len_max_increasing_subsequence(n, l):
"""
l is a list containing a permutation of 1, ..., n.
Returns the maximal length of an increasing subsequence in l.
"""
d = [0] * n
# at position x, store length of maximal increasing subsequence ending in x
for i, x in enumerate(l):
try:
... | 5fc9aca6915d3a3e78e40c8386977aa8539b5f4f | 489,202 |
def genKgris(k):
"""
Calcule une liste de ``k`` teintes de gris allant du noir au blanc.
Paramètre:
k --> nombre de teintes (>=2)
La liste génére doit nécessairement commencer par la couleur noir (0,0,0)
et nécessairement terminer par la couleur blanc (255,255,255).
Les autres valeurs... | 3e480d5bba5f60e3448392da97c7d7738f5decad | 24,531 |
def get_threshold(rows, bands):
"""Approximate threshold from bandwidth and number of rows
:param rows: rows per band
:param bands: number of bands
:return: threshold value
:rtype: float
"""
return (1. / bands) ** (1. / rows) | 3c5a5e417e96797e18b571cb7c09597442cb5f44 | 26,886 |
def create_token_request_payload(auth_code, redirect_uri, optional_token_request_params):
"""
Construct payload for access token request
"""
token_request_payload = {
# Don't include client_id param: Verizon doesn't like it
'grant_type': 'authorization_code',
'code': auth_code,
... | d61708ec69913fcc52ec3538a240633716e36377 | 566,580 |
import torch
def scale_boxes(boxes, scale):
"""
Args:
boxes (tensor): A tensor of shape (B, 4) representing B boxes with 4
coords representing the corners x0, y0, x1, y1,
scale (float, float): The box scaling factor (w, h).
Returns:
Scaled boxes.
"""
w_half = (b... | 88e97c67d7fefa387c4794508368d3d20082a5dc | 414,953 |
def levenshtein_distance(str_a, str_b):
"""Calculate the Levenshtein distance between string a and b.
:param str_a: String - input string a
:param str_b: String - input string b
:return: Number - Levenshtein Distance between string a and b
"""
len_a, len_b = len(str_a), len(str_b)
if len_a ... | 89506ab9235a67cb581d703ad91d267e376ec24c | 522,483 |
from datetime import datetime
def reformat_subway_dates(date):
"""
The dates in our subway data are formatted in the format month-day-year.
The dates in our weather underground data are formatted year-month-day.
In order to join these two data sets together, we'll want the dates formatted
the sam... | a40aff606bc790e41b75b4588dbb9b4442510805 | 51,274 |
def w_lin_update(u, Lin_lhs, Lin_rhs):
"""Computes the update for the auxilary tumor dose distribution w_0 based on the current u
Parameters
----------
u : np.array of shape(None,)
Beamlet radiation distribution
Lin_lhs : np.array of shape(None, u.shape[0])
Stacked lhs of the const... | 9e8e5f8d1daac113a9673daaa1414f5b9b709af0 | 231,353 |
def _is_hd(release_name):
""" Determine if a release is classified as high-definition
:param release_name: release name to parse
:type release_name: basestring
:return: high-def status
:rtype: bool
"""
if "720" in release_name or "1080" in release_name:
return True
return False | eac085e5de4a5edd63d1960cb1b3a8ef79b7fd58 | 648,295 |
def generate_catalog_mags(instrument_mags, color, model):
"""
Generate catalog magnitudes from instrumental magnitudes
given a model that relates the two.
"""
return instrument_mags + model(color) | 0b39a7dae5eb1f573c62b25b7053acebf28e91d2 | 20,194 |
def llist(self, nl1="", nl2="", ninc="", lab="", **kwargs):
"""Lists the defined lines.
APDL Command: LLIST
Parameters
----------
nl1, nl2, ninc
List lines from NL1 to NL2 (defaults to NL1) in steps of NINC
(defaults to 1). If NL1 = ALL (default), NL2 and NINC are ignored
... | 8a6b258be0c34d930d01dd27791b3c2ec2708ec5 | 127,909 |
def get_z(coeffs, x, y):
"""
Calculate and return the height z given the coefficients and ordinates of ax + by + c = z
:param coeffs: numpy ndarray of coefficients a, b, c, in the order (c, a, b)
:param x: x value
:param y: y vakye
:return: the calculated z value
"""
as... | e414555b052deb02928859f71ede4d1f9a2efa09 | 621,294 |
def merge(values_1, values_2, labels, join='inner'):
"""Merge two dictionaries. The resulting dictionary will map key values to
dictionaries. Each nested dictionary has two elements, representing the
values from the respective merged dictionary. The labels for these elements
are defined by the labels ar... | 282b358c44a53a0f88afab5af8beb639c45b90d0 | 106,174 |
import re
def to_single_space(string):
"""Remove leading and trailing whitespace and replace newlines, tabs and
sequences of 2 or more space to one space.
"""
patt = re.compile(' {2,}')
return patt.sub(' ', string.strip().replace('\n', ' ').replace('\t', ' ')) | 1e4c5b18bd9c42019194e4f769888293c0bd90f9 | 559,750 |
def map_freqs(s):
"""
s é uma string não-vazia de caracteres
Retorna um dicionário que mapeia cada caractere em s que
é um dígito (0-9) ao número de vezes que ele ocorre em s.
"""
d = {}
for char in s:
if char.isdigit():
if char not in d:
d[char] = 1
... | 08150866dcb45e758cab9c0761061e14eba3fc76 | 75,171 |
import json
def read_proxy_config(file_name):
"""convert proxy file from json file to python data structure
Parameters
--------------
file_name : str
location of proxies json list file
Returns
-------------
object
datastructure representation of json file
"""
with... | 15ef78c1b5a67424d5cf1b3179fad1d884b80178 | 623,883 |
import json
def getJson(fnm):
"""Parse a json file as a dict"""
with open(fnm) as f:
return json.load(f) | 3d89138ba5615ec17e0cad025736da0a9a8838a2 | 491,600 |
def get_first_match(regex, directory):
"""
Checks if `directory` matches `regex` and returns the first match converted
to an integer. If it does not match -1 is returned.
Arguments:
- `regex`: Regular expression to be tested against
- `directory`: name of the directory under test
"""
r... | 958417a22f43045f0c2384cf2da1dec108fdc8b3 | 142,484 |
def get_ref_path(openapi_major_version):
"""Return the path for references based on the openapi version
:param int openapi_major_version: The major version of the OpenAPI standard
to use. Supported values are 2 and 3.
"""
ref_paths = {2: 'definitions',
3: 'components/schemas'}
... | f0e29f0e02cb38f503bad0e14b2516b5a2d2ac9f | 611,531 |
def MakeRanges(codes):
"""Turn a list like [1,2,3,7,8,9] into a range list [[1,3], [7,9]]"""
ranges = []
last = -100
for c in codes:
if c == last+1:
ranges[-1][1] = c
else:
ranges.append([c, c])
last = c
return ranges | f43e79a4d604d8ce322fdd1fc7c22bdf57725e60 | 615,933 |
import pathlib
def get_creation_timestamp(filename):
"""Get creation timestamp for a file"""
filen = pathlib.Path(filename)
assert filen.exists()
return filen.stat().st_ctime | aa8b8b1efa24c0449c7d53eb4bb15de4c51cbea2 | 566,673 |
from datetime import datetime
def date_slug(timestamp: datetime = datetime.now()) -> str:
"""Format current date stamp as a slug."""
return timestamp.strftime("%Y%m%d%H%M%S") | 527dd2266d9b004f6c062437e6426ccaaac19259 | 496,760 |
def GetHeaderGuard(path):
"""
Returns the header #define guard for the given file path.
This treats everything after the last instance of "src/" as being a
relevant part of the guard. If there is no "src/", then the entire path
is used.
"""
src_index = path.rfind('src/')
if src_index != -1:
guard = ... | 950ab5729325c096780e662aa225faaaf00a66b3 | 548,578 |
def parseInt(s, ret=0):
"""Parses a value as int."""
if not isinstance(s, str):
return int(s)
elif s:
if s[0] in "+-":
ts = s[1:]
else:
ts = s
if ts and all([_ in "0123456789" for _ in ts]):
return int(s)
return ret | a4f3f900c8010aad4dfd9ec4265cdff292053625 | 532,624 |
def decdeg2dms(dd):
""" Tansform decimal degrees into degrees minutes seconds
Argument:
dd (float): decimal angle
Returns:
degrees, minutes, seconds"""
negative = dd < 0
dd = abs(dd)
minutes, seconds = divmod(dd * 3600, 60)
degrees, minutes = divmod(minutes, 60)
i... | 7ef8af3156988fdc93e6287761dd82e428372550 | 418,797 |
import asyncio
def make_future(result) -> asyncio.Future:
"""Make future for value."""
f = asyncio.Future() # type: ignore
f.set_result(result)
return f | df8eaf3e7affbf0235127e50ea79e605c93668b1 | 150,384 |
def sum_numbers(first_int, second_int):
"""Returns the sum of the two integers"""
result = first_int + second_int
return result | eaf7d297e4f2043124f1b9864447d0af3691d19a | 402,646 |
def sub_field(k, v):
"""Return a nested dictionary with field keys k and value v."""
res = {}
field_d = res
fields = k.split('.')
for f in fields[:-1]:
field_d[f] = {}
field_d = field_d[f]
field_d[fields[-1]] = v
return res | 193869fdfaca84172c71ca935f5fdb312682b19e | 9,247 |
import copy
def remove_pad_sequences(sequences, pad_id=0):
"""Remove padding.
Parameters
-----------
sequences : list of list of int
All sequences where each row is a sequence.
pad_id : int
The pad ID.
Returns
----------
list of list of int
The processed seque... | 15657e0ce29774ec7bb84c9c16b8ffdd25a38567 | 622,337 |
from typing import Tuple
def calculate_number_of_valid_lines(lines: Tuple[str]) -> int:
"""
Note that this function does not count empty lines and headings.
:param lines: Result of a f.readlines() operation, i.e., a list of strings.
:return: The number of valid lines in this file
"""
line_coun... | 5fcd4fd514d9a095a9e9409ba512e693331dcfff | 222,766 |
def column_id(team_name: str, session: str) -> str:
"""
Create a unique column ID based on team and session identifier.
This is needed for the dash datatable.
"""
return team_name + "_" + session | c53b8fac617e17b311c405c083f6a3f5ced6f732 | 239,960 |
def read_flag_file(filename):
"""
Reads the flag file, ignoring comment lines
"""
with open(filename, "r") as myfile:
lines = myfile.read().splitlines()
# filter the lines
lines = [l for l in lines if l.startswith("-")]
return " ".join(lines) | d990347b56d7f85339eb0c0dbad925680010fd6f | 85,022 |
def add_weight_decay(model, weight_decay=1e-5, skip_list=()):
"""Splits param group into weight_decay / non-weight decay.
Tweaked from https://bit.ly/3dzyqod
:param model: the torch.nn model
:param weight_decay: weight decay term
:param skip_list: extra modules (besides BN/bias) to skip
:ret... | 786993f755adabf09b34a36bade30cbad71d76ce | 679,149 |
def has_valid_number_pieces(configuration, original_permutation):
"""
Checks if a certain chess configuration has the valid number of pieces
"""
pieces = set(original_permutation)
while len(pieces) > 0:
piece = pieces.pop()
if original_permutation.count(piece) != configuration.count(... | b5ea05de21b38b22dab87a162d87791c6b194299 | 419,205 |
def copy_digesters(digesters):
"""Returns copy of provided digesters since deepcopying doesn't work."""
result = {}
for hash_algorithm in digesters:
result[hash_algorithm] = digesters[hash_algorithm].copy()
return result | 56cd4c26d6c8cb5c5f82ac3420f1165bb8c8e9fc | 609,938 |
import requests
def _get_newest_entry(subreddit_name):
"""
Extracts newest subreddit entry from JSON RSS
:param str subreddit_name: Subreddit to get entry for
:return: Extracted rss data
:rtype: dict
"""
url = "https://www.reddit.com/r/{}/new/.json".format(subreddit_name)
headers = {... | 9e43739d631d9a4c253937f09dcabb20c779aefc | 199,201 |
import json
def parse_message(line: str) -> dict:
"""
Given a string that looks like json with
a 'msg' element in it, remove the msg element
and replace it with a set of keys and values
derived from expanding the msg field on spaces.
For example, a line string like:
{"one": "fish","two"... | 659f9666b7c135fabff59ff40770352964aad3d0 | 398,457 |
def parse_relation(fields):
"""
Assumes all relation are binary, argument names are discarded
:param fields: correspond to one Brat line seperated by tab
:return: relation id, relation name, arg1 and arg2
"""
rel, a1, a2 = fields[1].split(" ")
rel_id = fields[0]
return rel_id, rel, a1.sp... | 9e86f45d571e7b3de2e64645209a5854f145330e | 692,858 |
def upper_chars(string, indices):
"""
Make characters uppercase in string
:param string: string to modify
:param indices: character indice to change to uppercase
:return: uppercased string
"""
upper_string = "".join(c.upper() if i in indices else c for i, c in enumerate(string))
return ... | 409a0dc99649d1ee5a3e8def0fa618f89b43b949 | 598,990 |
def output_script_error(error, cfg):
"""
Cleanly outputs an error return from an Apps Script API call
"""
cfg['logger'].error('Script error', error_message=str(error['errorMessage']))
if 'scriptStackTraceElements' in error:
cfg['logger'].error('Script error stracktrace follows')
for ... | 62bbb1fc4cef44df56efbd7f0177f2a064f9c49c | 148,078 |
def find_missing(integers_list, start=None, limit=None):
""" Given a list of integers and optionally a start and an end
finds all integers from start to end that are not in the list
"""
start = start if start is not None else integers_list[0]
limit = limit if limit is not None else integers_list... | 317b60d7b9dd1fb6168ce2fd2bc00f03a69772a7 | 84,304 |
def get_nim_sum(state: tuple[int, ...]) -> int:
"""
Get the nim sum of a position. See https://www.archimedes-lab.org/How_to_Solve/Win_at_Nim.html
:param state: the state of the game
:return: the nim sum of the current position
"""
cur_sum = 0
for n in state:
cur_sum ^= n
return cur_sum | d1b3cf67d86fce56ffd69cb6ede438b8c2cc85f6 | 688,427 |
def flatten_spatial_dims(data):
"""Flatten all spatial dimensions of an input array.
Assumes input array has shape (time, vert, lat, lon).
"""
n_lon, n_lat, n_lev = data.shape[-1], data.shape[-2], data.shape[-3]
n_pt = n_lon*n_lat*n_lev
return data.reshape((-1, n_pt)) | 07be987d55b115c8e0e9977bc6731b50450cee1d | 297,889 |
from datetime import datetime
def createTrunk(name):
"""
Creates a trunk name for data in data_vault corresponding
to the current date.
Arguments:
name (str) : the name of the client.
Returns:
(*str) : the trunk to create in data_vault.
"""
date = datetime.no... | c4b1967468159cc13a551afcb142b05a510174ad | 23,153 |
def read_entries(fileobj):
"""
Reads environment variable assignments from a file-like object. Only lines that
contain an equal sign (=) and do not start with # (comments) are considered. Any
leading/trailing quotes around the value portion of the assignment are stripped.
"""
entries = {}
fo... | 3a74fd3855536389ef06ac2317b2bb52dd75ce52 | 351,367 |
def create_searching_by_message(searching_by: str):
"""
Creates a message informing the user how something is being searched
:param searching_by: A string saying how something is searched
:return:
A title cased string in the format of 'Searching: {searching_by}' minus
any `-` characters... | 6c695b491f3558f2bdddc8b880662aa706fb9395 | 128,771 |
def classname(class_object):
"""Returns the class name of the given object"""
return class_object.__class__.__name__ | f08c8bf90b0e5243c83d9d213ca208f50e27ab96 | 590,412 |
import unicodedata
def strip_symbols(s, pass_symbols=(u'й', u'Й', u'\n')):
""" Strip ugly unicode symbols from a string """
result = []
for char in s:
# Pass these symbols without processing
if char in pass_symbols:
result.append(char)
continue
for c in unic... | 223e2c0bb742bfaa9b5674584f700f9fcb0ff265 | 369,901 |
def get_names(soup):
"""Return a list of store location public names"""
storeid = soup.find(id="our-menu").find_all('option')
n = len(storeid)
names = [storeid[i].text for i in range(n)]
return names | 5eab999c9b7cfa90dc8e62d30e7a7e1b119dd157 | 342,788 |
def _fuzz_vhost_create_dev(client, socket, is_blk, use_bogus_buffer, use_valid_buffer, test_scsi_tmf, valid_lun):
"""Create a new device in the vhost fuzzer.
Args:
socket: A valid unix domain socket for the dev to bind to.
is_blk: if set, create a virtio_blk device, otherwise use scsi.
... | 1045e55eb6acc5eea690a0389475a7b0bfea9484 | 276,810 |
def suffixtonumber(suffix):
""" Given a set of ascii_lowercase values, get a base 26 number.
a = 0, ... z = 25, aa = 26, ...
"""
# int(base=26) doesn't quite work, since first ten ints are actually ints!
base36 = '0123456789abcdefghijklmnopqrstuvwxyz'
return int(''.join([base36[base36.index(b)-... | 08dcfab32f17a0fc52ccbb432513ab17cb174c3b | 165,970 |
def getDocComment(node):
"""
Returns the first doc comment of the given node.
"""
comments = getattr(node, "comments", None)
if comments:
for comment in comments:
if comment.variant == "doc":
return comment
return None | 4cc32f84d4e127880f9a137447e2d9401a6fbd6c | 644,668 |
import re
def get_ao3_identifiers(bookmarks):
"""
Given the output from Pinboard's /posts/all API, return all the unique AO3
identifiers. This is the numeric ID after /works.
e.g. The ID in https://archiveofourown.org/works/1160745 is 1160745
"""
saved_urls = [bk["href"] for bk in bookmarks]... | e1cfbf68b4e7296241e419794810afa61405154b | 393,074 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.