content stringlengths 42 6.51k |
|---|
def add_pads_if_necessary(s):
"""adding bits to make an integer number of 64-bit blocks
"""
number_of_vacancy = len(s) % 64
need_pads = number_of_vacancy > 0
if need_pads:
for i in range(64 - number_of_vacancy):
s.append(0)
return s |
def stringDictToIntDict(dictionary):
"""
Converts dictionary keys into integers; non-integer keys won't be in
result.
:param dictionary:
:return:
"""
result = {}
for k in dictionary:
try:
result[int(k)] = dictionary[k]
except ValueError:
pass
r... |
def rivers_with_station(stations):
"""Returns a set with the names of the rivers with monitoring stations"""
# Build empty set
rivers = set()
# Add the river of every station, and duplicates are removed automatically
for station in stations:
rivers.add(station.river)
return rivers |
def ensure_tuple_or_list(obj):
"""Given an object, wrap into a tuple if not list or tuple
"""
if isinstance(obj, (list, tuple)):
return obj
return (obj,) |
def github(path):
"""
:param path: relative (to the root) path of a file in the glottolog data repository
:return: URL to a file in Glottolog's data repository on GitHub
"""
return 'https://github.com/glottolog/glottolog/blob/master/{0}'.format(path) |
def skip_nulls_rec(dict_obj):
"""
removes dict key/val pairs recursively where value is None,
not needed/wanted(?) in the JSON
:param o: needs to be dict
:return: the trimed dict, or exceptionally the value if it wasn't a dict
"""
if not isinstance(dict_obj, dict):
return dict_obj
... |
def isEmbeddedInOtherArc(arc, arcs, startIndex=0, stopIndex=-1):
"""
Check whether an arc is embedded within another arc between two indices.
"""
isEmbedded = False
testArcs = []
for testArc in arcs:
if (testArc[0] >= startIndex
and testArc[-1] <= stopIndex
... |
def dmc_task2str(domain_name, task_name):
"""Convert domain_name and task_name to a string suitable for environment_kwargs"""
return '%s-%s-v0' % (domain_name, task_name) |
def findprev(layer, cols):
"""For a particular column in a particular layer, find the next earlier
column in the layer that contains a node
"""
found = -1
pos = cols - 1
while (pos >= 0) and (found < 0):
if layer[pos] == 1:
found = pos
pos = pos - 1
return found |
def format(value):
"""
_format_
format a value as python
keep parameters simple, trust python...
"""
if isinstance(value, (str, bytes)):
value = "\'%s\'" % value
return str(value) |
def get_custom_db(firmware_version, _db):
"""Get db of device if yours doesn't exists."""
if _db:
if firmware_version in _db:
return _db[firmware_version]
return None |
def getGamePgnUrl(game_id):
"""Returns lichess url for game with `game_id` id"""
URL_TEMPLATE = "https://en.lichess.org/game/export/{}.pgn"
return URL_TEMPLATE.format(game_id) |
def revnum_to_revref(rev, old_marks):
"""Convert an hg revnum to a git-fast-import rev reference (an SHA1
or a mark)"""
return old_marks.get(rev) or b":%d" % (rev + 1) |
def is_iterable(obj):
""" Return True if `obj` is iterable
"""
try:
iter(obj)
except TypeError:
return False
return True |
def trsp(m):
"""Transpose of matrix"""
M = len(m[0])
n = len(m)
T = [n*[0] for i in range(M)]
for i in range(n):
for j in range(M):
T[j][i] = m[i][j]
return T |
def parse_cmu_seg_line(line, prepend_reco_to_spk=False):
"""This line parses a 'line' from the CMU automatic segmentation for
recording.
The CMU segmentation has the following format:
<file> <channel> <speaker> <start-time> <end-time> <condition>
We force the channel to be 1 and take the file-id to... |
def deps(value=None):
"""Returns empty for deps table requests since this app doesn't use them."""
del value # Unused.
return '' |
def edges_to_rings(edges):
"""
Rings creation from pairs of edges.
:param edges: set of (i,j) pairs representing edges of the alpha-shape.
(i,j) are the indices in the points array
:return: closed rings created from the edges
"""
edges_list = list(edges)
rings = []
while len(edges_l... |
def get_slide_id(full_filename:str) -> str:
"""get slide id
Get slide id from the slideviewer full file name. The full_filename in
the slideview csv is of the format: year;HOBS_ID;slide_id.svs
for example: 2013;HobS13-283072057510;1435197.svs
Args:
full_filename (str): full filename o... |
def count(s, *args):
"""count(s, sub[, start[,end]]) -> int
Return the number of occurrences of substring sub in string
s[start:end]. Optional arguments start and end are
interpreted as in slice notation.
"""
return s.count(*args) |
def checksum(byte_array):
"""
Checks whether a byte array has a valid checksum or not.
:param byte_array: of length 4 e.g. b'\x19\x10\x00\x29'
:return: True for valid checksum, False or failed communication
"""
if len(byte_array) == 4:
if (sum(byte_array[0:3]) % 256) == byte_array[3]:
... |
def merge_next(letters: list, positions: list) -> list:
"""Given a list of letter positions, merge each letter with its next neighbor.
>>> merge_next(['a', 'b', 'o', 'v', 'o' ], [0, 2])
['ab', '', 'ov', '', 'o']
>>> # Note: because it operates on the original list passed in, the effect is not cummulativ... |
def to_kebab(value: str) -> str:
""" snake_case to kebab-case """
try:
return value.replace('_', '-')
except Exception as e:
raise Exception(e) |
def word_count(sentence):
"""Return JSON with words from sentence and count of they."""
result = {}
word = ""
for char in sentence.lower():
if char.isalnum():
word += char
else:
if len(word):
if result.get(word):
result[word] +=... |
def validate_fail_under(num_str):
"""Fail under value from args.
Should be under 100 as anything over 100 will be converted to 100.
Args:
num_str (str): string representation for integer.
Returns:
Any[float,int]: minimum of 100 or converted num_str
"""
try:
value = in... |
def remove_blank_lines(string):
""" Removes all blank lines in @string
-> #str without blank lines
"""
return "\n".join(line
for line in string.split("\n")
if len(line.strip())) |
def pf_potential(phi):
""" Phase field potential. """
return 0.25*(1.-phi**2)**2 |
def get_possible_sgrp_suf(sgrp_nr):
"""
determine possible space group suffix. Multiple suffixes might be possible
for one space group due to different origin choice, unique axis, or choice
of the unit cell shape.
Parameters
----------
sgrp_nr : int
space group number
... |
def where_in(name, value, big_range):
"""Determines which bucket of big_range 'value' lies in."""
bottom = big_range[0]
top = big_range[1]
step = big_range[2]
i = 0
bot_range = bottom + i * step
while bot_range < top:
bot_range = bottom + i * step
top_range = bottom + (i + 1... |
def inverse_points(points):
"""
Generate the points for the inverse CDF of a distribution.
Takes the points for the CDF and transforms them into the points for
the inverse. Due to the discrete nature of the function, the x and
y coordinates must be re-paired such that the inverse function is
d... |
def introspection_email(request):
"""Returns the email to be returned by the introspection endpoint."""
return request.param if hasattr(request, 'param') else None |
def merge_adjacent(numbers, indicator='..', base=0):
""" Merge adjacent numbers in an iterable of numbers.
Parameters:
numbers (list): List of integers or numeric strings.
indicator (str): Delimiter to indicate generated ranges.
base (int): Passed to the `int()` conversi... |
def _altitude_factor(alt_units_in: str, alt_units_out: str) -> float:
"""helper method for convert_altitude"""
factor = 1.0
# units to feet
if alt_units_in == 'm':
factor /= 0.3048
elif alt_units_in == 'ft':
pass
elif alt_units_in == 'kft':
factor *= 1000.
else:
... |
def separate_bags(bags):
"""
Seperates the positive and negative bags
takes a list of bags as input and returns list of indices for positive and negative bags
pos, neg=separate_bags(bags)
"""
#random.shuffle(bags)
pos_bags=[]
neg_bags=[]
for ind in range (len(bags)):
if bags... |
def parse_line(line):
"""Takes a string of two comma seperated integers. Returns the integers to
the caller.
"""
x, n = line.strip().split(',')
return int(x), int(n) |
def in_array(key: str, array: list):
"""Return True if given key exists in given array"""
if key in array:
return True
else:
return False |
def CleanFloat(number, locale = 'en'):
"""\
Return number without decimal points if .0, otherwise with .x)
"""
try:
if number % 1 == 0:
return str(int(number))
else:
return str(float(number))
except:
return number |
def capitalize_first_letter(string):
"""Capitalize first letter of words"""
return_string = ""
split_array = string.split(" ")
for s in split_array:
return_string = return_string + " " + s.capitalize()
return return_string.strip() |
def objective_limit(energy, limit):
"""
The algorithm stops as soon as the current objective function value
is less or equal then limit.
"""
if energy <= limit :
return True
else :
return False |
def reduce(f, seq):
""" takes a lambda function f and
runs thrue it with the given values list seq
"""
if not seq:
return 0
elif isinstance(seq[0], list):
return reduce(f, seq[0])
elif len(seq) == 1:
return seq[0]
else:
return f(seq[0], reduce(f, seq[1:])) |
def update_dtypes_dict(dtypes_dict: dict) -> dict:
"""
Task to update dtypes_dictionary that will be stored in the schema. It's required due to workaround Pandas to_parquet bug connected with mixed dtypes in object
Args:
dtypes_dict (dict): Data types dictionary inferenced by Visions
Returns:
... |
def _sbe(exp,synapses):
"""
Subcellular binary expression model for chemical synapses
Paramters:
----------
exp : Expression object
synapses: dict
Synapse data
"""
lus = {}
for cell in synapses:
lus[cell] = [0.,0.]
for cont in synapses[cell]:
adj... |
def pressure(v, t, n):
"""Hoddie"""
k = 1.38e-23 # boltzmann constant
return n * k * t / v |
def check_accounts(msg: dict) -> int:
"""
Returns the number of accounts to
process.
The incoming msg is a string that contains the
Account Id, Groups and Account name
"""
accounts = 0
print(f"DEBUG --- check_account msg parameter {msg}")
if msg != "":
accounts = len(msg[0])
... |
def sortable_date(date_fr):
""" '23/12/1977' -> ('1977', '12', '23') """
return tuple(reversed(date_fr.split("/"))) |
def bestAlgorithm_(sequencingChemistries):
"""
Identify the (de novo) consensus algorithm we expect to deliver
the best results, given the sequencing chemistries represented in
an alignment file.
We key off the sequencing chemistries as follows:
- Just RS chemistry data? Then use quiver (at l... |
def _get_formatted_timestamp(app_type):
"""Different services required different date formats - return the proper format here"""
if app_type.startswith('duo'):
return 1505316432
if app_type.startswith('onelogin'):
return '2017-10-10T22:03:57Z'
if app_type.startswith('gsuite') or app_type... |
def round_(value, digits=0):
"""Rounds a number to the nearest whole number."""
return round(value, digits) |
def bubble_sort(a):
"""
Sorts the list 'a' using Bubble sort algorithm
>>> from pydsa import bubble_sort
>>> a = [3, 4, 2, 1, 12, 9]
>>> bubble_sort(a)
[1, 2, 3, 4, 9, 12]
"""
for k in range(len(a)):
flag = 0
for i in range(0, len(a)-k-1):
if(a[i] > a[i+1]):... |
def set_direction_of_pp(pp):
""" order direction of path so that we always go the same way and can define locations
order going clock wise from posterior so that left is ~25% and right is ~75% (top view)
"""
pplength = len(pp)
p25 = pp[int(0.25*pplength)]
p75 = pp[int(0.75*pplength)]
... |
def minThresholdClassify(sim_vec_dict, sim_thres):
"""Method to classify the given similarity vector dictionary with regard to
a given similarity threshold (in the range 0.0 to 1.0), where record pairs
that have all their similarities (of all attributes compared) with at
least this threshold are cl... |
def compute_FLOP(nl, nL, sl, ml):
"""
compute the flop of the model (operation complex)
:param nl: channel(filter) num of the input
:param nL: filter num
:param sl: kernel size
:param ml: output spacial size(length)
:return: the flop estimation
"""
return nl * nL * sl * sl * ml * ml |
def flatten_list(alist, howdeep=1):
"""Flattens nested sequences."""
if howdeep > 0:
newlist = []
for nested in alist:
try:
newlist.extend(nested)
except TypeError:
newlist.append(nested)
howdeep -= 1
alist = flatten_list(ne... |
def _left_parser(tab, i):
"""Helper function to build google observations
"""
coord = []
tab_ = tab[i]["left_team_positions"]
for list_ in tab_:
for value in list_:
coord.append(value)
return coord |
def required_props(props):
"""Pull names of required props from the props object.
Parameters
----------
props: dict
Returns
-------
list
List of prop names (str) that are required for the Component
"""
return [prop_name for prop_name, prop in list(props.items()) if prop["re... |
def bytesToStr(filename):
"""Return str for a bytes filename.
"""
return filename.decode("utf8", errors="backslashreplace") |
def smart_split(item, split_key=':'):
"""
split string in first matching with key
:param item: string which contain field_name:value or field_name:[00:00:00 TO 01:00:00]
:param split_key: key, which we use to split string
:return:
"""
split_index = item.find(split_key)
return [item[0:spl... |
def to_latex(var):
"""Returns a latex representation for a given variable string name.
Parameters
----------
var : string
One of the variable names used in the bicycleparameters package.
Returns
-------
latex : string
A string formatting for pretty LaTeX math print.
""... |
def filter_queryset_real_organization(queryset, auto_generated: bool):
""" Filters a given REST framework queryset for real (not auto generated) organizations.
Only keeps organizations that are or not auto generated.
Args:
queryset: A queryset containing elements
auto_generated (bool): Whe... |
def get_custom_endpoints(origin_endpoints, offset=0):
"""
origin_endpoint: ip:port
user_define_endpoint: ip:(port+offset)
"""
assert origin_endpoints != None
paddle_user_define_endpoints_list = []
for ip_port in origin_endpoints.split(","):
ip = ip_port.split(":")[0]
port = i... |
def indent(text: str, spaces: int):
"""
Prepend every line of the specified text with a set number of spaces. Line endings are
preserved.
"""
prefix = " " * spaces
return "".join(prefix + t for t in text.splitlines(keepends=True)) |
def textToFloat(text, defaultFloat):
"""Converts text to a float by using an eval and returns the float.
If something goes wrong, returns defaultFloat.
"""
try:
returnFloat = float(text)
except Exception:
returnFloat = defaultFloat
return returnFloat |
def split_python_text(text):
"""
splits first '#ifdef PYTHON_SETUP ... #endif python section from text.
Returns:
(pythontext, nonpythontext)
"""
s = text.split('#ifdef PYTHON_SETUP', 1)
if len(s) < 2:
return (None, text)
(beforep, afterp) = s
(pythontext, aftere) = afte... |
def MakeArgs(l):
"""['-a', '', 'abc', ''] -> '-a abc'"""
return " ".join(filter(None, l)) |
def normalize_url(url):
"""Adds trailing slash if necessary."""
if not url.endswith('/'):
return '%(url)s/' % {'url': url}
else:
return url |
def sum_of_n_even_nums(i):
"""Calculate sum of first i even numbers"""
count = i
num = 0
ans = 0
iterator = 1
while iterator <= count:
if num % 2 == 0:
ans = ans + num
iterator = iterator + 1
num = num + 1
else:
num = num + 1
re... |
def _is_valid_transformer(transformer_name):
"""Determine if transformer should be tested or not."""
return transformer_name != 'IdentityTransformer' and 'Dummy' not in transformer_name |
def format_time(seconds):
"""Returns a short string human readable duration (5 chars)
Args:
seconds (float)
"""
if seconds < 1:
return f"{seconds:.3f}s"[1:] # e.g. .652s
if seconds < 10:
return f"{seconds:.2f}s" # e.g. 5.21s
if seconds < 100:
r... |
def get_always_bytes(path: str) -> bytes:
"""
Returns bytes
"""
if path:
pass
return b"\x04\x00" |
def most_frequent(data):
"""
determines the most frequently occurring string in the sequence.
"""
# your code here
print([(data.count(x),x) for x in data])
return max([(data.count(x),x) for x in data])[1] |
def _coerce_field_name(field_name, field_index):
"""
Coerce a field_name (which may be a callable) to a string.
"""
if callable(field_name):
if field_name.__name__ == '<lambda>':
return 'lambda' + str(field_index)
return field_name.__name__
return field_name |
def marital_status_from_string(str):
"""Convert marital status to one of ['single', 'partner', 'married', 'separated', 'widowed']."""
marital_status_dict = {
'Single': 'single',
'Significant other': 'partner',
'Life Partner': 'partner',
'Married': 'married',
'Divorced': '... |
def rfam_problems(status):
"""
Create a list of the names of all Rfam problems.
"""
ignore = {"has_issues", "messages", "has_issue", "id"}
problems = sorted(n for n, v in status.items() if v and n not in ignore)
return problems or ["none"] |
def get_spc_info(spc_dct_i):
""" convert species dictionary to species_info array
"""
err_msg = ''
props = ['ich', 'chg', 'mul']
for i, prop in enumerate(props):
if prop in spc_dct_i:
props[i] = spc_dct_i[prop]
else:
err_msg = prop
if err_msg:
prin... |
def _dict_clean(d):
""" Replace None with empty string in dict
Args:
d (dict): dictionary
Returns:
dict
"""
result = {}
for key, value in d.items():
if value is None:
value = ''
result[key] = value
return result |
def _find_replacement(key, kwargs):
"""Finds a replacement for key that doesn't collide with anything in kwargs."""
key += '_'
while key in kwargs:
key += '_'
return key |
def update_upper_bound(update_model, Ediffs, thresholds, upper_bound,
scaling_factor, upper_bound_limit):
"""Update upper bound
Args:
update_model (bool) : True : update model.
False: do not update model.
Ediffs (dict) :... |
def cluster_points(cluster_name: str) -> list:
"""
Return points composing the cluster, removing brackets and hyphen from cluster name,
e.g. ((a)-(b))-(c) becomes [a, b, c].
:param cluster_name: name of the cluster.
:return: points forming the cluster.
"""
return cluster_name.replace("(", "... |
def initialize_object_list(inp, cls):
""" Utility function to return list of objects from a valid input (a single object or list of objects where each object is of the class ``cls``).
If invalid input ``None`` is returned.
:param inp: Input.
:paramtype inp: (list, cls) or cls
:param cls: ... |
def queryBeforeTraverse(container, app_handle):
"""Find __before_traverse__ hook objects, given an 'app_handle'.
Returns a list of (priority, object) pairs."""
btr = getattr(container, '__before_traverse__', {})
objects = []
for k in btr.keys():
if k[1] == app_handle:
objects.ap... |
def get_attributes_threshold(alist, decreasing_factor, min_activity_count=1, max_activity_count=25):
"""
Get attributes cutting threshold
Parameters
----------
alist
Sorted attributes list
decreasing_factor
Decreasing factor of the algorithm
min_activity_count
Minimu... |
def square(x):
"""
funciton documentation
can we have your liver then?
"""
return x ** 2 # square |
def numberOfSteps(num):
"""
:type num: int
:rtype: int
"""
res = num
count = 0
while num != 0:
if num % 2 == 0:
res = num / 2
count += 1
num = res
else:
res = num - 1
count += 1
num = res
return count |
def _translate_ip_xml_json(ip):
"""
Convert the address version to int.
"""
ip = dict(ip)
version = ip.get('version')
if version:
ip['version'] = int(version)
if ip.get('type'):
ip['type'] = ip.get('type')
if ip.get('mac_addr'):
ip['mac_addr'] = ip.get('mac_addr')... |
def suma_total(monto=0):
""" Calcula la suma total """
calculo_suma = 20
calculo_suma += monto
return calculo_suma |
def split_dataset_sizes(stream_list, split_sizes):
"""Splits with different sizes
Args:
stream_list (list): list of stream path
split_sizes (list): batch size per worker
"""
out = []
start = 0
total = sum(split_sizes)
for split_size in split_sizes[:-1]:
num = int(spl... |
def stripDrive(path):
"""Strips off leading "drive:foo" specification from path, if present
"""
# strip any drives off the front of the filename
first, *rest = path.strip("/").split("/")
return "/".join([first.split(":").pop(), *rest]) |
def merge_dict(destination, source, path=None):
"""merges source into destination"""
if path is None:
path = []
for key in source:
if key in destination:
if isinstance(destination[key], dict) and isinstance(source[key], dict):
merge_dict(destination[key], source[key], path + [str(key)])
... |
def get_dict_value(key, data):
"""Return data[key] with improved KeyError."""
try:
return data[key]
except (KeyError, TypeError):
raise KeyError("No key [%s] in [%s]" % (key, data)) |
def left_top_submatrices(left_top_tuple: tuple, n_half: int) -> tuple:
"""
Calculates the coordinates of the top left corner of the four submatrices
and returns these as four tuples of two coordinates
"""
left, top = left_top_tuple
M00 = (left, top)
M01 = (left + n_half, top)
M10 = (left... |
def match_fields(exp_fields, fields):
"""
Check field names and values match the expected ones.
- exp_fields:
A list of dictionaries with field name/value pairs.
- fields:
SPARKL event fields as returned by the listener.
[
{'attr': {'name':'n',... |
def parse_state_line(line):
"""
Parse a line from a Tor state line and return the data that we should plot in the histogram
For example if it's (CircuitBuildTimeBin 342 4) return (342, 342, 342, 342)
"""
items = line.split()
# We only use CircuitBuildTimeBin lines
if len(items) < 1:
... |
def mu_air(BP,RH,TC):
"""Returns the inverse 1/e penetration depth [mm-1] as a function of
barometric pressure (BP) in torr, relative humidity (RH) in %, and
temperature in Celsius. The expressions below were derived from equations
and tabulated data found in:
https://en.wikipedia.org/wiki/Antoi... |
def _diff(state_data, resource_object):
"""helper method to compare salt state info with the PagerDuty API json structure,
and determine if we need to update.
returns the dict to pass to the PD API to perform the update, or empty dict if no update.
"""
objects_differ = None
for k, v in state_d... |
def create_text(text, hashtags):
"""Returns a solid string containing the entire text of the posting
Parameters:
text (string): text of your posting
hashtags (list): list of hashtags e.g. from get_random_hashtags()
Returns:
string that contains the posting
"""
output = text + '\n.\n.\n.... |
def determine_sentiment(delta):
"""Returns 1 for positive sentiment, 0 otherwise"""
if delta > 0:
return 1
else:
return 0 |
def get_es_substitutions (num):
"""Return Spanish substitutions for plurals"""
subs = [{'n': '', 's': ''},
{'n':'n', 's':'s'}][(num > 1)]
subs['num'] = num
return subs |
def create_filepath_template(intemp: str, output_1partition: bool):
"""Process the user input's template to a python string that allows us to pass variables' value
to get the correct file name.
There are three variables:
1. `{auto}`: an auto-incremental ID of the new partition
2. `{stem}`: ... |
def inverse_ip(ip_v4):
""" Inverse ip order a.b.c.d -> d.c.b.a """
tmp = ip_v4.split(".")
tmp.reverse()
return ".".join(tmp) |
def to_bytes(value):
"""
Convert int to a byte
Args:
The int to convert
Return:
The byte value
Exception:
If value is not a byte
"""
if not isinstance(value, int):
raise TypeError('Value is type %s, but needs to be an int'
% type(value)... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.