content stringlengths 35 416k | sha1 stringlengths 40 40 | id int64 0 710k |
|---|---|---|
import re
def getencoding(path):
"""Detect encoding string from the leading two lines.
:param str path: pathname of the source file
:rtype: str or None
:return: encoding str or None
"""
coding = re.compile(r"coding[:=]\s*(\w)+")
with open(path, encoding="ascii") as in_:
for _ in (... | 8976b094b54d9af60f6ecd29bd077bb4040cb2ab | 16,215 |
import asyncio
def in_async_call(loop, default=False):
"""Whether this call is currently within an async call"""
try:
return loop.asyncio_loop is asyncio.get_running_loop()
except RuntimeError:
# No *running* loop in thread. If the event loop isn't running, it
# _could_ be started ... | 9ed888e0d8ee27d18c843a184071d9596880c039 | 16,216 |
def list_format(items, fmt):
"""format each item in a list"""
out = []
for i in items:
out.append(fmt.format(i))
return out | bf4824ee8726457ace69bedf1532610f3efca2c1 | 16,217 |
def dictlist_lookup(dictlist, key, value):
"""
From a list of dicts, retrieve those elements for which <key> is <value>.
"""
return [el for el in dictlist if el.get(key)==value] | 94e90b29c8f4034357be2b8683115f725c24b374 | 16,218 |
import sys
def contentSplit(content, fragmentation_len=1000, offset_str="\n\n"):
"""
由于企业微信消息发送接口限制单条内容长度为2048字节,因此需对告警内容进行分片,多次发送
:param content: 告警内容
:param fragmentation_len: 分片长度,默认值1000
:return: 以列表形式返回所有分片
为使分片后的消息看起来更连贯,对分片后的内容进行判断,如果不是一条完全的告警,则往前偏移,直至找到完整一条的告警为止
完整的一条告警是以\n\n分隔的
... | 9bb3f865882758db1d0f7cca73373cb2793ee577 | 16,220 |
def df_to_geojson(df, geometry, coordinates, properties):
"""
Convert a pandas DataFrame to a GeoJson dictionary.
Parameters
----------
df : pandas DataFrame
DataFrame containing the geojson's informations.
geometry : String
The type of the geometry (Point, Polygon, etc.).
c... | 75fa808dca18b89e2e259425b9b9e67bb8ac2415 | 16,221 |
def get_bytes(block_nums):
"""
Takes an array of block integers and turns them back into a bytes object.
Decodes using base 256.
:param block_nums: Blocks (list of ints)
:return: Original data (bytes)
"""
message = []
for block in block_nums:
block_text = []
while bloc... | 429325cd37a3f821b751237659626907eb19d4a9 | 16,222 |
def check_positive_int(string):
"""Convert a string to integer and check if it's positive.
Raise an error if not. Return the int.
"""
if int(string) > 0:
return int(string)
else:
raise RuntimeError("Integer must be positive.") | 1e32994a2d9d58b1361a9228b78210398c19c94e | 16,225 |
def clamp_value(my_value, min_value, max_value):
"""Limit value by min_value and max_value."""
return max(min(my_value, max_value), min_value) | f3478b08f3e2e38ba3459d487adc9c462468c32e | 16,226 |
import argparse
def usage():
"""
Parses arguments and displays usage information on screen
"""
parser = argparse.ArgumentParser(
description="Rfam fasta export tool", epilog='')
# group required arguments together
req_args = parser.add_argument_group("required arguments")
req_ar... | a3c749a3310beb0fc97ee900bbaa866e636b847a | 16,227 |
def _buses_with_gens(gens):
"""
Return a list of buses with generators
"""
buses_with_gens = list()
for gen_name, gen in gens.items():
if not gen['bus'] in buses_with_gens:
buses_with_gens.append(gen['bus'])
return buses_with_gens | 5cf9e918a55e140053bceb538cd1f15b331c253d | 16,229 |
import hashlib
def get_key(model_params, mw_range, bins):
"""
Generate a hash key based on the model parameters used and the fited
mw_range and number of bins.
"""
hasher = hashlib.md5()
hasher.update(bytes(str(model_params), 'ASCII'))
hasher.update(bytes(str(mw_range), 'ASCII'))
hashe... | 50fe5d13bf9f897c76ee144c576212ec483e1948 | 16,230 |
def list_merge_values(d, d2, in_place=True):
"""
Parameters
----------
d
d2
in_place : bool, optional (default is True)
Do the update in-place.
Examples
--------
>>> list_merge_values({1:[2]}, {1:[3]})
{1: [2, 3]}
Returns
-------
"""
d3 = d.copy() if n... | 2474d00e61ce3590b9a28590b436ef5e7c7cc470 | 16,231 |
from typing import Any
import hashlib
def md5(item: Any) -> str:
"""Compute the MD5 hash of an object.
Args:
item:
The object to compute the MD5 hash on.
Returns:
The MD5 hash of the object.
"""
return hashlib.md5(str(item).encode()).hexdigest() | 5589eb0a38b9f099a7f4b418d719b9c089110d5a | 16,237 |
from typing import List
from typing import Tuple
import os
import logging
def path_renaming(path_lst: List[str], search_value: str, new_value: str, renaming: bool = False) -> Tuple[int, int, List[str]]:
""" List of filtered files and directories are renamed and their names
returned. Furthermore, the number fo... | 15e2026260daed5cd631cec3ef49c88263f3ca3d | 16,240 |
import shlex
def parse_value(s: str) -> str:
"""
>>> parse_value('"a"')
'a'
>>> parse_value('"CentOS Linux"')
'CentOS Linux'
>>> parse_value('Hello World')
'Hello World'
"""
tokens = list(shlex.shlex(s, posix=True))
if len(tokens) == 1:
return tokens[0]
return s | 0e3c0e84ed3fa71dceafab85405f8eda8ce0a0ac | 16,241 |
import os
def get_label_name(image_path):
"""Get label name from image path"""
items = image_path.split(os.path.sep)
if len(items) < 2:
raise ValueError("invalid image path:{}".format(image_path))
return items[-2] | 8be4f52f377f2c58e4dd84f3067a5c389443c426 | 16,243 |
def SortLists(sortbylist,otherlists,reverse=False):
"""This function sorts lists similar to each list being a column
of data in a spreadsheet program and choosing one column to sort
by.
The sortbylist is the column or list that you wish to sort by and
otherlists is a list of the other lists or colu... | 132678fa35f008e4f987ffca57e729840438ca72 | 16,244 |
def P_bremsstrahlung(k, Te, ne):
"""
W m^3
"""
return 1.53e-38 * Te**0.5 * (k + 1)**2 | 11959d1616bdc25bea26f4f4329447d8e570f162 | 16,246 |
from typing import Dict
def gen_tag_list(db_config: Dict):
"""
Generate tag list from tag string
"""
if 'tags' not in db_config:
return []
return db_config['tags'].split(',') | f612f5cc85e0f61041d65c6782d3a427b6538a95 | 16,247 |
from typing import Any
def is_json_string(value: Any) -> bool:
"""Check if the provided string looks like json."""
# NOTE: we do not use json.loads here as it is not strict enough
return isinstance(value, str) and value.startswith("{") and value.endswith("}") | 16418c37fc1d348e4d1e5805c485f181e514d537 | 16,249 |
def flux2zhr(flux, pop_index=2.0):
"""Eqn 41 from (Koschak 1990b)
Parameters
----------
flux : float [1000^-1 km^-2 h^-2]
"""
r = pop_index
flux = flux / 1000.0
zhr = (flux * 37200.0) / ( (13.1*r - 16.45) * (r - 1.3)**0.748 )
return zhr | 945f49d3429989e5914b5753e2d2fa3610172ec6 | 16,253 |
def get_filediff_encodings(filediff, encoding_list=None):
"""Return a list of encodings to try for a FileDiff's source text.
If the FileDiff already has a known encoding stored, then it will take
priority. The provided encoding list, or the repository's list of
configured encodingfs, will be provided a... | 5c951bbc6266daa058de58bdeb1382ea6dc93ada | 16,254 |
def get_group_command_url(environment: str,
enterprise_id: str,
group_id: str) -> str:
"""
Build and return pipeline url for scapi endpoint
:param environment:
:param enterprise_id:
:param group_id:
:return: Url
"""
url = f'https://{env... | 34402dbe263ddd5cd4b04b247beb09701f2b2cc6 | 16,256 |
def mutate_query_params(request, mutations):
"""
Return a mutated version of the query string of the request.
The values of the `mutations` dict are interpreted thus:
* `None`, `False`: Remove the key.
* Any other value: Replace with this value.
:param request: A HTTP request.
:type reque... | 215a79ce82859eb415454876c1c80cdfdb34d7f6 | 16,257 |
import math
import random
def Move(Location, xT):
"""
This function will return the new location (as a tuple) of a particle after it has
isotropically scattered
Location: Location of particle before scattering
xT: Total cross section of medium
"""
# Sample distance to collision
Distan... | 5902de845db37e5848fbf64954f971fcdce60c89 | 16,258 |
def get_color(col, color):
"""Function needed for backwards compatibility with the old "col" argument in
plt functions. It returns the default color 'C0' if both arguments are None.
If 'color' is not None, it always uses that. If 'color' is None and
'col' is an integer, it returns the corresponding 'CN... | b4e7cbeacca0e730cb2fa5f320318da83cabfc1a | 16,261 |
import copy
def remove_hidden_options(config, whitelist):
"""
Remove any hidden options not whitelisted
"""
for entry in copy.copy(config):
for func in entry:
if func.startswith("_") and func not in whitelist:
config.remove(entry)
return config | 73435c65559c7982c591e23d46b58048ac101d8c | 16,262 |
from datetime import datetime
def played_at_to_date(s):
"""Return played at string as date
'2022-01-09T19:17:33.720Z',
"""
return datetime.strptime(s, "%Y-%m-%dT%H:%M:%S.%fZ") | 2db8b82cc875275b8a40fd7e6ab752a7b03e221d | 16,263 |
import re
def GetAge(description):
"""
:param description: A description possibly containing the borrower's age.
:return: The borrower's age as an int, if not found, returns None.
"""
match = re.findall(
"([1-9][0-9])( |\-)(years old|years of age|year old|year\-old)",
description)
... | 46609ee32c5145e3492c6f314d6324a252a3845b | 16,264 |
def arg_is_natural_num(arg):
"""Return whether the string arg contains a natural number.
>>> arg_is_natural_num('123')
True
>>> arg_is_natural_num('0')
True
>>> arg_is_natural_num('-1')
False
>>> arg_is_natural_num('1.5')
False
>>> arg_is_natural_num('foo2')
False
>>> ar... | 545a127121c168be54f140f42cfbbd6b5ef7eef3 | 16,265 |
def _eigenvals(mat,roundto):
"""
Returns the eigenvalues using QR algorithm
"""
assert mat.isSquare and mat.d0>=2, "Can't find eigenvalues for non-square matrices"
if mat.d0==2:
d=mat.det
tr=mat.matrix[0][0]+mat.matrix[1][1]
return list(set([(tr+(tr**2 - 4*d)**(1/2))/2,(tr-(... | e1f448602625ff144337b8997a2fefbe2d74ab16 | 16,266 |
def stations_by_river(stations):
"""Creates a dictionary of stations that are located on the same river. The river is the key for this dictionary"""
river_dictionary = {}
for i in stations:
station_list = []
for j in stations:
if i.river == j.river:
station_list.a... | cf6d324c10ecb756dfa4c506adbbd16316a0d500 | 16,267 |
def _shuffle_leaky(all_ratings_df, n_train):
"""Shuffles and splits the ratings allowing overlap in the ref sentences."""
all_ratings_df = all_ratings_df.sample(frac=1, random_state=555)
all_ratings_df = all_ratings_df.reset_index(drop=True)
train_ratings_df = all_ratings_df.iloc[:n_train].copy()
dev_ratings_... | a7db1e2f8730ce6501824fa0aaf775667a9665e6 | 16,269 |
def merge_response_func(func, key):
"""
Use this decorator to set a new merging
response function to HTTP endpoints
candidate function must have the following signature
and be childs of BaseApi:
```
def merge_some_function(self, response, rison_args):
```... | 277d7a5b477d3d52aac7a8d93e5cc5468d36279c | 16,271 |
def Mimir_header_handler(header):
"""Makes some small modifications to the header as its read in"""
# Copy the header for manipulation
outHeader = header.copy()
# Make the ADU units lower case so that astropy.units can handle them.
outHeader['BUNIT'] = header['BUNIT'].strip().lower()
# Set the... | cecbd339b366fbd67243719bf161a9b9fc370b51 | 16,272 |
def real_letter(character, key):
""" Afla caracterul """
if character.isalpha():
character = ord(character)-key
if character < ord('a'):
character = ord('z') - abs(ord('a') - character) + 1
return chr(character)
else:
return character | 24290f3e7c970f8643034f3c88d06b4369695b16 | 16,273 |
def revert_gr(admin_mc, request):
"""Ensures gr was reverted to previous state, regardless of test results
"""
def _cleanup(old_gr):
def revert():
reverted_gr = admin_mc.client.update_by_id_global_role(
id=old_gr.id,
displayName=old_gr.name,
... | 2e109bee2796d1ee8dfe5a7fe60e2748ae70b084 | 16,274 |
def get_margin(length):
"""Add enough tabs to align in two columns"""
if length > 23:
margin_left = "\t"
chars = 1
elif length > 15:
margin_left = "\t\t"
chars = 2
elif length > 7:
margin_left = "\t\t\t"
chars = 3
else:
margin_left = "\t\t\t\t"... | c50b1253a737d787f696216a22250fb2268216ab | 16,275 |
def unique_list(it):
"""
Create a list from an iterable with only unique element and where
the order is preserved.
Parameters
----------
it : iterable
Items should be hashable and comparable
Returns : list
All items in the list is unique and the order of the ite... | 5122b3fdb7a489df856ad6ff4a17da58a99a8160 | 16,277 |
def f4(x):
"""Evaluate the estimate x**4+x**3+x**2+x."""
return x*(x*(x*x+x)+x)+x | 6a5e258d77992e8c15a6dddb04627a7d5c8467d9 | 16,278 |
import os
def get_files(directory, exts=('.torrent',)):
"""Get the shallowest set of files with valid extensions in a directory
structure.
If no valid files are found in a directory, search all subdirectories. If
a valid file is found in a directory, no subdirectories will be searched.
Parameter... | 5a704db10242e64996a6bc1783c3e62aca39214b | 16,279 |
import re
def _is_private_name(name):
""" Return true if the given variable name is considered private.
Parameters
----------
name : str
Variable name to check
"""
# e.g. __name__ is considered public.
is_reserved_public_name = re.match(r"__[a-zA-Z0-9_]+__$", name) is not None
... | 1232f6a52ffc9d07d7ed286771c3c75bc80db8b7 | 16,280 |
import os
def find_file_in_dirs(path, dirs):
"""
Search for `path` in the list of directories `dirs`.
Return the first expansion that matches an existing file.
"""
if os.path.isabs(path):
return path
for d in dirs:
if d == '.':
f = path
else:
d ... | a7314850fdb68382a370a4fd193c92ff3af1fc48 | 16,281 |
import shlex
def shell_join(argv, delim=" "):
"""Join strings together in a way that is an inverse of `shlex` shell parsing into `argv`.
Basically, if the resulting string is passed as a command line argument then `sys.argv` will equal `argv`.
Parameters
----------
argv : list(str)
List ... | d58fb2d899bc1f72adf0dd22bdc55ddc9ffeecfb | 16,282 |
def inverted_index_add(inverted, doc_id, doc_index):
"""
Add Invertd-Index doc_index of the document doc_id to the
Multi-Document Inverted-Index (inverted),
using doc_id as document identifier.
{word:{doc_id:[locations]}}
"""
for word, locations in doc_index.items():
indices = in... | 4ef0cad9892a09dfaeb730487f413836ba72b7d3 | 16,283 |
import numpy
def draw_mask_over_image(image: numpy.ndarray, mask: numpy.ndarray, color=None):
"""
Draws the mask over the image. All the pixels marked as True will be overwritten in the original image wih a
certain colour.
Args:
image: A numpy array, representing the RGB image in [0, 1] range... | 6a2db79dcd47559be685e832e851018edf09c8c9 | 16,287 |
def append(xs, ys):
"""
Adds all the elements of xs and ys and returns a combined list.
"""
return xs + ys | e629ece09214a88465d780bb82f2b6f1a82af18a | 16,289 |
from json import dumps
def json_formatter(data):
"""Method returns parsing result in json format.
"""
return dumps(data, sort_keys=True, indent=4, separators=(',', ': ')) | 08614e5d49be595642d751bf48171be3b2a5b62b | 16,290 |
def get_fert_weight(npk, required_nitrogen):
"""
:param required_nitrogen: this is the required lbs of nitrogen required for the application
:return: the amount of product required for an application
This method calculates the required amount of product for an application.
"""
product_nitrogen ... | ac174ec16fe1db68be192bc621a9b6e21b3fddba | 16,291 |
def get_keyfunc(cols, schema, nulls_are_smaller=False):
"""
Return a function that maps a row to a tuple of some of its columns values
"""
def key(row):
"""
Returns a tuple designed for comparisons based on a row
Each requested columns is mapped to two columns:
- The fi... | 35d722aaf0eec26f9a17e4b29dbcbd2db5095dc0 | 16,293 |
import re
def get_all_text(elem):
"""Returns all texts in the subtree of the lxml.etree._Element, which is
returned by Document.cssselect() etc.
"""
text = ''.join(elem.itertext())
return re.sub(r'\s+', ' ', text).strip() | 1c8e2e8743147fd3b09488445d8578a89f5346d7 | 16,294 |
def count_offspring(cycle: int, days: int) -> int:
"""Counts how many offspring will be spawn over a given period"""
if cycle >= days:
return 1
if cycle == 0:
return count_offspring(8, days - 1) + count_offspring(6, days - 1)
return count_offspring(cycle - 1, days - 1) | 4cc57ab978289828d074c728bfbfdd8d51ecf5d9 | 16,295 |
def hashable(obj):
""" Convert obj to a hashable obj.
We use the value of some fields from Elasticsearch as keys for dictionaries. This means
that whatever Elasticsearch returns must be hashable, and it sometimes returns a list or dict."""
if not obj.__hash__:
return str(obj)
return obj | bf59d420da20f6df5121fb25a1ce8c5c7ce210c7 | 16,296 |
def _collect_mexp_symbols(value):
"""Get symbols in a math expression.
:param value: The math expression.
:rtype : list of str
:return: A list of symbols.
"""
# Get symbols.
fsm = value.free_symbols
# Initialize symbol list.
ret = []
# Pop symbols.
while len(fsm) != 0:... | c031212e3af5bd485dc572c4cf9bb31cfc2be3db | 16,297 |
def _inBoundaries(value, bounds):
"""
Tell if an integer value is within a range.
Parameters
----------
value : int
A value to be tested.
bounds : int | tuple(int, int)
Low boundary of the range, or a tuple of the form (low, high+1).
Returns
-------
boolean
... | dcc9fcf993ee7b25bb0f6b8cb8acea7b0474fad5 | 16,298 |
def has_neighbor(prop1: str, prop2: str) -> str:
"""
Builds the statements required for: The person who `prop1` has a neighbor who `prop2`.
For example, "The Norwegian lives next to the Blue house."
We can solve this by doing a disjunction of all satisfying possibilities. Because we've
previously s... | d63dd6e09ce822cdcd87970dd205ee34857ad8f4 | 16,300 |
import numpy
def isInsideContour(p, xc, yc):
"""
Check if a point is inside closed contour by summing the
@param p point (2d array)
@param xc array of x points, anticlockwise and must close
@param yc array of y points, anticlockwise and must close
@return True if p is inside, False otherwise... | d86813df1abf3d446b11c1e2a1a55b996888b5b5 | 16,302 |
from typing import Counter
def build_vocab(data, min_token_instances, verbose=True):
""" Builds a set that contains the vocab. Filters infrequent tokens. """
token_counter = Counter()
for img in data:
for region in img['regions']:
if region['tokens'] is not None:
token_... | 20f2ee90abb9e2fd370e434752736a1a02d80399 | 16,303 |
def convert_command_results(header, msg, flag):
"""
转换管理命令结果集
"""
if flag:
return '%s:\n' \
'Errno: %s\n' \
'%s\n\n' % (header, msg[0], msg[1])
else:
res = '%s:\n' % header
affect_rows, headers, columns = msg[0], msg[1], msg[2]
_c, _r = ... | 6ce65100b212ceda2113115187b41d73e3b7be8f | 16,305 |
import os
def addAssets(proj,name,typ,component):
"""
asset을 등록한다. component,assembly정보에 맞춰 각각 등록한다.
"""
if component == "assembly":
os.system("/lustre/INHouse/CentOS/bin/csi3 -add item -project %s -name %s -type asset -assettype %s -assettags %s,assembly" % (proj, name, typ, typ))
elif component == "component... | f1fa6ee2156b38105ddae950a13cefb7782b2f2e | 16,306 |
def bl_little_mock(url, request):
"""
Mock for bundle lookup, small output.
"""
littlebody = b'<?xml version="1.0" encoding="UTF-8"?><availableBundlesResponse version="1.0.0" sessionId="7762de1a-909c-4afb-88fe-57acb69e9049"><data authEchoTS="1366644680359"><status code="0"><friendlyMessage>Success</frie... | 0b35d1e30c32b89351341103ba9013ca95c0ba2e | 16,307 |
from typing import Optional
def _transform_op(op: Optional[str]) -> str:
"""Transforms an operator for MongoDB compatibility.
This method takes an operator (as specified in a keyword argument) and transforms it
to a ``$`` prefixed camel case operator for MongoDB.
"""
if op is None:
return... | 6168e84387ade258b2907445e5d3536228053521 | 16,309 |
def generate_title(input_file):
"""Return title fragment."""
title = input_file['title']
subtitle = input_file['subtitle']
return '# {title}\n*{subtitle}*<br>'.format(title=title, subtitle=subtitle) | 64150f960a0ee560f6bec600c8a632970afb45f1 | 16,311 |
import os
def is_git_repo(directory):
"""
Determine if a folder is a git repo
Checks the 'git status' message for error
"""
files = os.listdir(directory)
if '.git' in files:
return True
return False | 9e33250083e7691d2c930e56819ec7bf3df39212 | 16,312 |
import requests
def get_bitstamp_ticker(symbol='btcusd'):
"""
doc:
https://www.bitstamp.net/api/
limit: 600 / 10 min
"""
main_path = 'https://www.bitstamp.net'
info_path = '/api/v2/ticker/{0}/'.format(symbol)
r = requests.get(main_path + info_path)
data_json = r.json()
retu... | f8af2dfbf39a0a0eb6d98d0891647a6cc749ca9e | 16,314 |
import sys
def install(cls):
"""Class decorator for installation on sys.meta_path."""
sys.meta_path.append(cls)
return cls | 02771d8ca6a87f149264523a82abd5b64d696395 | 16,317 |
def stop_server(proc):
"""
Stop server process.
proc: ShellProc
Process of server to stop.
"""
return proc.terminate(timeout=10) | c126ca840b56407f1eea8af943f4602532df13df | 16,318 |
import functools
def replace_exception(raised, to_raise):
"""
Parametrized decorator for replacing exception class or tuple of
classes ``raised`` by ``to_raise`` called with the previously raised
exception as its sole argument.
"""
def decorator(func):
@functools.wraps(func)
de... | 9cd63635a8b0185ec382f26ab5c75f12a2287390 | 16,319 |
def _v3_to_v2_catalog(catalog):
"""Convert a catalog to v2 format.
X_SERVICE_CATALOG must be specified in v2 format. If you get a token
that is in v3 convert it.
"""
v2_services = []
for v3_service in catalog:
# first copy over the entries we allow for the service
v2_service = {... | a7cb008306e7a8c0bdd9ce7dec2c733c50087839 | 16,320 |
def batched_input(func):
"""Decorator to mark an actor method as accepting only a single input.
By default methods accept a batch.
"""
func.ray_serve_batched_input = True
return func | e97c2df57c4eb9cfe003545224cc8c942f27d845 | 16,321 |
def compose(left, right):
"""compose(left, right)-->left.compose(right)"""
return left.compose(right) | a279b3d30a1bde94f181d3f94e349c34e49473bc | 16,322 |
def create_subnet_raw(conn, subnet_name, parent_network_id, subnet_cidr, gateway):
"""creates a subnet without checking for existing subnets"""
os_subnet = conn.network.create_subnet(
name=subnet_name,
network_id=parent_network_id,
ip_version='4',
cidr=subnet_cidr,
gatewa... | f88fd35401ec84af39f5256ea117a4c00f22574c | 16,323 |
def TMTOND(N, LX, X):
"""
trace mode to nd array
"""
Y = X.reshape(N, LX)
return Y | df79cd727919ba628862c93225aef7a23c85a6ca | 16,324 |
def next_node_i(edgeweights, node, next_node):
"""
find the next node's index in the edgeweights
file so the cost can be calculated
"""
for i in range(len(edgeweights[node])):
if edgeweights[node][i][0] == next_node:
return i | 4a0486e7f862c9036d51ac5ed2107a45ff9af664 | 16,325 |
def get_oct(value):
"""
Convert an integer number to an octal string.
"""
try:
return oct(value)
except:
return value | 0e07b02f7a727c5942c1f6f1d7d1ca12506328f4 | 16,327 |
import torch
def compute_distance_histograms(a, dx, b, dy):
"""
Computes the squared distance between histograms of distance of two
mm-spaces. The histograms are obtained by normalising measures to be
probabilities.
Parameters
----------
a: torch.Tensor of size [Batch, size_X]
Input m... | 7758c15cea89d9eabba2fbf95decd6252bec532d | 16,329 |
def gather_3rd(params, indices):
"""Special case of tf.gather_nd where indices.shape[-1] == 3
Check https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/gather_nd
for details.
Args:
params: Tensor, shaped [B, d_1, ..., d_i], di >= 2
indices: LongTensor, shaped [B, i_1, ... | 948acdbb83b283cc0bef743d679e7d30fbaa0ad6 | 16,330 |
def apply_date_format(in_data):
""" This routine adds the default UTC zone format to the input date time string
If a timezone (strting with + or -) is found, all the following chars
are replaced by +00, otherwise +00 is added.
Note: if the input zone is +02:00 no date conversion is done,
at th... | 2a9cd63f1b0a341c3c859d80b538af5ecf56d21d | 16,332 |
def find_base_match(char, matrix):
"""Return list of coordinates wherein char matched inside matrix.
Args:
char (str): A single-length string
matrix (list): A list containing lines of string.
row_length (int): An integer which represents the height of the matrix.
column_length (... | 836a84b672365908cf81f848f1111840f613dbea | 16,335 |
def startswith(text, starts):
"""
{% load startswith %}
{{ var | startswith: "foo" }}
"""
return text.startswith(starts) if isinstance(text, str) else False | 088437bc630ceb310e85b3995d24dc51aa65860c | 16,337 |
def near(array, value):
"""Find the nearest point within the array and return its index"""
idx = (abs(array - value)).argmin()
return idx | 0e683e8a94a54cd4e53f6d147ca8d4d907ec84c2 | 16,338 |
import time
def bench_from_sample( distribution, sample, n=1000 ):
"""Bench the training of a probability distribution."""
tic = time.time()
for i in range(n):
distribution.summarize( sample )
return time.time() - tic | c9d97badda06a9b5340dc360e1ba7d043a6e2c29 | 16,339 |
def dominant_clade(idx, prev_cutoff=1):
"""
Which is the dominant clade?
"""
for k in sorted(idx, key=lambda item: item[0], reverse=True):
if idx[k][0] < prev_cutoff:
return None
else:
return k | 3961f1bf35522a0226da5bf4b52b612a7c44a5bb | 16,340 |
def packal_username(author):
"""Format usernames for Packal."""
user = author.lower()
user = user.replace(" ", "-")
return user | c8f9c5ab67deb95775a787a5cb8cceab67ea73f1 | 16,341 |
def map_args(args):
"""used to filter arguments passed in on the command line that should also
be passed as keyword args to make_map"""
arg_set = set(['starting_year', 'ending_year', 'ranking_algorithm',
'similarity_algorithm', 'filtering_algorithm',
'number_of_terms', ... | df5bc2599a732c9f75db6e833dc672f135f9a012 | 16,343 |
def _get_all_nearest_neighbors(method, structure):
"""Get the nearest neighbor list of a structure
Args:
method (NearNeighbor) - Method used to compute nearest neighbors
structure (IStructure) - Structure to study
Returns:
Output of `method.get_all_nn_info(structure)`
"""
re... | 5e1e33c7b06951933d8603a75006c6895b742293 | 16,345 |
def mul_inv(a):
"""
:type a: int
"""
if a == 0:
a = 0x10000
result = pow(a, 0x10001 - 2, 0x10001)
return result | 63c1e617600fb93cff3a601668aab1a3ce5d3be6 | 16,350 |
import zlib
def _compute_crc(file_obj):
"""To minimize memory use, compute the CRC in chunks."""
crc = 31415 # can initialize to any value
while True:
content = file_obj.read(1048576) # 1M at a time
if not content:
break
crc = zlib.crc32(content, crc)
r... | 3963c560819160636f947f9b5834b69eab666816 | 16,352 |
def rolling_average(n, data):
"""
Function to calculate an n day rolling average given a list of covid data pts
@param n: an int representing the number of points to average into the past
@param data: a list of dictionaries representing covid data pts
@return: the n day death, death increase, infec... | ba02b006176d8b37ed53e5f2cd46c64a92b7dcc2 | 16,353 |
def compute_avg_negation_difference(sent_and_contexts1, sent_and_contexts2):
"""
We compare each context in document 1 to each context in document 2
the difference in negations is abs(# negations in context 1 - # negations in context 2)
Summing up the differences in negations between all the contexts a... | 9aecc7a42126da2dde770e218105b88d00a98c3b | 16,354 |
def checkPW(md5str: str, USER_PASSWD: str):
"""检查本地数据库中密码和计算后的密码是否一致
:param md5str: 计算后的密码(字符串)
:param USER_PASSWD: 本地的存的密码(字符串)
:return: bool
"""
return md5str == USER_PASSWD | 2bbe3864493700ff907b00ff9f82c45723e1a2d7 | 16,355 |
def get_list_of_existing_certificates(ct_collection):
"""
We don't want to re-download data that we already have.
Therefore, we get the list of known crt_sh_ids from the database.
"""
existing_ids = []
results = ct_collection.find({'crt_sh_min_id': {"$exists": True}}, {'crt_sh_min_id': 1})
... | 9ada67bba79db41cdbe64d708f6adce2589b5cf9 | 16,356 |
def count_letters(word,find):
"""
Example function with types documented in the docstring.
Ce code doit retourner le nombre d'occurences d'un caractère passé en paramètre dans un mot donné également
Parameters
----------
param1 : str
Le 1er paramètre est une chaine de caractères
param2 : char
Le 2ème para... | 327e6b4fd99d03b27473b9620d6147909d0cf6e4 | 16,357 |
import socket
import struct
def ip2num(ipStr):
"""Converts ipv4 string representation to local number format"""
return struct.unpack("!I", socket.inet_aton(ipStr))[0] | e11e713a14cf6d285e0659efbbe3b6567084aea7 | 16,359 |
def one_space(value):
""""Removes empty spaces, tabs and new lines from a string and adds a space between words when necessary.
Example:
>>> from phanterpwa.tools import one_space
>>> one_space(" My long \r\n text. \tTabulation, spaces, spaces. ")
'My long text. Ta... | ff3ce941437186c734bd2815fff9d23ba581bec7 | 16,360 |
def small_push_dir(tmpdir):
"""Create a small pg data directory-alike"""
contents = 'abcdefghijlmnopqrstuvwxyz\n' * 10000
push_dir = tmpdir.join('push-from').ensure(dir=True)
push_dir.join('arbitrary-file').write(contents)
# Construct a symlink a non-existent path. This provoked a crash
# at o... | 6f3fa0f6b291f4d18a8f33624ad55045c45b7261 | 16,361 |
def _matching_award(cursor, load_object):
""" Try to find an award for this transaction to belong to by unique_award_key"""
find_matching_award_sql = "select id from awards where generated_unique_award_id = '{}'".format(
load_object["transaction_fpds"]["unique_award_key"]
)
cursor.execute(find_m... | 5bd95f293a7683b8f9ec53ecccc43fb8055c5486 | 16,362 |
import os
def check_wand_binaries_exist(package_path: str) -> bool:
"""
Check if the binaries neccessary to run the DeepSparse Engine are present
"""
arch_path = os.path.join(package_path, "arch.bin")
print("Checking to see if", arch_path, "exists..", os.path.exists(arch_path))
return os.path.... | 53d4e13c371f71aedfae7b17bf95a3646fad9505 | 16,363 |
def create_cmnd(
analysis, sim_start_date, sim_end_date, use_sim_manager, attributes, no_close
):
"""Create a command string for automatic processing."""
args = []
if use_sim_manager:
args.append("UseSimManager")
if sim_start_date:
args.append(f"SimStartDate {sim_start_date[0]} {si... | b95fc466a9c61a125bf09d6fc5dd77bf4575756d | 16,365 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.