content stringlengths 42 6.51k |
|---|
def _int_from_bool(boolean_value):
"""Converts a boolean value to an integer of 0 or 1."""
if boolean_value:
return 1
return 0 |
def _mesh_export_format_from_filename(filename):
"""
Determine a mesh output format based on a file name.
Determine a mesh output format based on a file name. This inspects the file extension.
Parameters
----------
filename: string
A file name, may start with a full path. Examples: 'br... |
def convert_option_name_to_environment_variable_name(option_name: str) -> str:
"""Convert given option name to uppercase, replace hyphens with underscores, and add "BARRIER_" prefix."""
return f"BARRIER_{option_name.upper().replace('-', '_')}" |
def non_FA(alt_img_root):
"""Wrapper for the ``tbss_non_FA`` command.
e.g.: ``tbss_non_FA("L2")``
"""
return ["tbss_non_FA", alt_img_root] |
def get_compatible_name(name_str):
"""
Converts given string to a valid Maya string
:param name_str: str
:return: str
"""
return ''.join([c if c.isalnum() else '_' for c in name_str]) |
def csnl_to_list(csnl_str):
"""Converts a comma-separated and new-line separated string into
a list, stripping whitespace from both ends of each item. Does not
return any zero-length strings.
"""
s = csnl_str.replace('\n', ',')
return [it.strip() for it in s.split(',') if it.strip()] |
def is_route_bfs(graph, node1, node2):
"""Determine if there is a route between two nodes."""
queue = [node1]
visited_nodes = set()
while queue:
current_node = queue.pop(0)
for child_node in graph[current_node]:
if child_node not in visited_nodes:
if child_nod... |
def sort_string(text: str) -> str:
"""Sort string punctuation, lowercase and then uppercase."""
return ''.join(sorted([x for x in text], key=str.swapcase)) |
def recall_at_n(asins: list, predicted_asins: list) -> float:
"""
Args:
asins ([list]):
predicted_asins ([list]):
"""
# number of relevant items
set_actual = set(asins)
set_preds = set(predicted_asins)
num_relevant = len(set_actual.intersection(set_preds))
# calculating... |
def find_char(char, secret, lst):
"""Takes a character from given string,
shifts it secret times and return the
character from lst depending on case of
the taken character."""
pos = lst.index(char)
offset = (pos + secret) % 26
ch = lst[offset]
return ch |
def make_null_array(n):
"""
Return list of n empty lists.
"""
a = []
for i in range(n):
a.append([])
return(a) |
def signbit(x):
""" Returns 1 if x<0"""
return int(x < 0) |
def bisect_right(a, x, lo=0, hi=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e <= x, and all e in
a[i:] have e > x. So if x already appears in the list, a.insert(x) will
insert just after the rightmost x already t... |
def total_intake_calories(bmr, activity_level):
"""
Calculate the Total Intake Calories based
on activity level
:param bmr: value from bmr function
:param activity_level: reference level, 0 - sedentary, 1 - low, 2 - moderate, 3 - high
:return: average target intake calories
"""
r... |
def func_h(b, n, p, x_i):
"""
b_(i+1) = func_g(b_i, x_i)
"""
if x_i % 3 == 2:
return (b + 1) % n
elif x_i % 3 == 0:
return 2*b % n
elif x_i % 3 == 1:
return b
else:
print("[-] Something's wrong!")
return -1 |
def tuple_diff(t1, t2):
"""Elementwise addition of two tuples"""
return tuple( t1[i] - t2[i] for i in range(len(t1)) ) |
def parse_masked_mac(s): # pylint: disable=invalid-name
"""
parse mac and mask string. (xx:xx:..:xx/yy:yy:...:yy)
"""
items = s.split("/", 1)
if len(items) == 1:
return items[0], "ff:ff:ff:ff:ff:ff"
elif len(items) == 2:
return items[0], items[1]
else:
raise SyntaxErr... |
def _ShouldGenerateVM(options):
"""Returns true if we will need a VM version of our images."""
# This is a combination of options.vm and whether or not we are generating
# payloads for vm testing.
return options.vm and (options.basic_suite or options.full_suite) |
def correct(old_txt):
"""
correct() sets to 0 the atom valence indication from the mol block in old_text and
returns the corrected text
"""
new_lines = []
# initialize list of corrected lines in old_txt
old_lines = old_txt.split('\n')
# get lines from old_txt
creator_line = old_lines[1].strip()
# line 2, indexed... |
def separate(sep, extractions):
"""Extracts information from an existing dictionary and returns a list of
seperate dictionaries.
"""
extracted = []
for k in extractions:
tmp_d = {}
tmp_d[k]=sep[k]
extracted.append(tmp_d)
return extracted |
def lcm(num1, num2):
"""Returns the lowest common multiple of two given integers."""
temp_num = num1
while (temp_num % num2) != 0:
temp_num += num1
return temp_num |
def error_analysis(err_train, err_test, threshold=0.2):
"""
Using a threshold to flag when there is over/under-fitting.
Default: 0.20
"""
if (err_test - err_train) > threshold:
print("WARNING: OVER-fitting")
return False
if err_train > threshold:
print("WARNING: U... |
def reverse(text: str):
"""
Returns the `text` in reverse order.
"""
return text[::-1] |
def skipExport(item, all, metadataProperty):
"""
Determine whether a particular item should be exported as part of this export run.
:param item: the item we're checking
:param all: whether or not we're exporting all or recent items
:param metadataProperty: the metadata property that acts as a flag ... |
def nested_dict_to_list_of_tuples(d, separator='.', extra_nesting=False):
""" Take a 2-level nested dict of dict to list of tuples.
to preserve some structure, the keys from the two levels are
joined with the separator string.
If extra_nesting is true, the inner items are wrapped in an extr... |
def in_nested_list(my_list, item):
"""
Determines if an item is in my_list, even if nested in a lower-level list.
"""
if item in my_list:
return True
else:
return any(in_nested_list(sublist, item) for sublist in my_list if isinstance(sublist, list)) |
def normalize_path(path):
"""
Normalizes provided path to: /something/something/something
:param path: string path to normalize
:return: string normalized path
"""
while path[len(path) - 1] == ' ' or path[len(path) - 1] == '/':
path = path[:len(path) - 1]
retu... |
def _build_key(host, port):
"""Build a key based upon the hostname or address supplied."""
return "%s:%s" % (host, port) |
def minRemoveToMakeValid(s):
"""
:type s: str
:rtype: str
"""
opens, closes = [], []
for i in range(len(s)):
c = s[i]
if c == '(':
opens.append(i)
elif c == ')':
if len(opens) == 0:
closes.append(i)
else:
... |
def to_str(text, session=None):
"""
Try to decode a bytestream to a python str, using encoding schemas from settings
or from Session. Will always return a str(), also if not given a str/bytes.
Args:
text (any): The text to encode to bytes. If a str, return it. If also not bytes, convert
... |
def group_instance_count_json(obj):
"""
Test lifecycle parser
>>> group_instance_count_json(json.loads('{ "AutoScalingGroups": [ { "Instances": [ { "LifecycleState": "InService" } ] } ] }'))
1
"""
if not obj['AutoScalingGroups']:
return 0
instances = obj['AutoScalingGroups'][0]['Ins... |
def lfff_name(lt):
"""Create mch-filename for icon ctrl run for given leadtime.
Args:
lt (int): leadtime
Returns:
str: filename of icon output simulation in netcdf, following mch-convention
"""
hour = int(lt) % 24
day = (int(lt) - hour) // 24
remaining_s = round((lt - int(... |
def set_pairing_bit_on_device_type(pairing_bit: bool, device_type: int):
"""
Shifts the pairing bit (True = 1, False = 0) 7 bits to the left and adds
the device type
"""
return (pairing_bit << 7) + device_type |
def cmp_dicts(dict1, dict2):
"""
Returns True if dict2 has all the keys and matching values as dict1.
List values are converted to tuples before comparing.
"""
result = True
for key, v1 in dict1.items():
result, v2 = key in dict2, dict2.get(key)
if result:
v1... |
def unpack_libraries(libraries):
""" Given a list of libraries returns url """
if libraries:
return libraries[0].get('url') |
def isstr(obj):
"""
isstr
"""
return isinstance(obj, str) |
def convert_to_signed_int_16_bit(hex_str):
"""
Utility function to convert a hex string into a 16 bit signed hex integer value
:param hex_str: hex String
:return: signed 16 bit integer
"""
val = int(hex_str, 16)
if val > 0x7FFF:
val = ((val+0x8000) & 0xFFFF) - 0x8000
return val |
def calc_share_ratios(list_prio, cores):
""" Basic Shares calculator"""
# Shares calculation logic
max_shares_per_app = 100
# collect all high priority apps
shares_per_app = None
if len(list_prio) <= cores:
total_shares = sum([r[2] for r in list_prio])
shares_per_app = [r[2]/tota... |
def dissoc_deep(obj, *ks):
""" Return a copy of obj without k """
from copy import deepcopy
obj = deepcopy(obj)
for k in ks:
try: # pragma: no cover
del obj[k]
except (KeyError, IndexError): # pragma: no cover
pass
return obj |
def ex_euclid(a, b):
"""
The extended Euclidean algorithm yields the
gcd of inputs a and b, and also two numbers
x and y such that a*x + b*y = gcd(a,b).
"""
last_remainder = a
current_remainder = b
last_s = 1
current_s = 0
last_t = 0
current_t = 1
while current_remainder... |
def filter_fn(filename):
"""Filter interesting coverage files.
>>> filter_fn('z3c.coverage.__init__.cover')
True
>>> filter_fn('z3c.coverage.tests.cover')
False
>>> filter_fn('z3c.coverage.tests.test_foo.cover')
False
>>> filter_fn('z3c.coverage.ftests.test_b... |
def reverse(insts):
"""
Reverse the order of instances.
This function should be passed to :meth:`InstanceSet.select` without any
argument.
"""
return list(reversed(insts)) |
def is_valid_xvg(ext):
""" Checks if file is XVG """
formats = ['xvg']
return ext in formats |
def parse_headers(arg):
"""Parse headers argument"""
if not arg:
return None
return dict(val.split(':', 1) for val in arg.split(',')) |
def term_precision(reference, test):
"""
Given a set of reference values and a set of test values, return
the fraction of test values that appear in the reference set.
In particular, return card(``reference`` intersection ``test``)/card(``test``).
If ``test`` is empty, then return None.
:type r... |
def transpose_loggraph(loggraph_dict):
"""Transpose the information in the CCP4-parsed-loggraph dictionary
into a more useful structure."""
columns = loggraph_dict["columns"]
data = loggraph_dict["data"]
results = {}
# FIXME column labels are not always unique - so prepend the column
# nu... |
def format_seconds(s):
"""
Format a seconds value into a human-readable form
"""
years, s = divmod(s, 31556952)
min, s = divmod(s, 60)
h, min = divmod(min, 60)
d, h = divmod(h, 24)
return '%sy, %sd, %sh, %sm, %ss' % (years, d, h, min, s) |
def _normalize(line):
"""
Deal with lines in which spaces are used rather than tabs.
"""
import re
SPC = re.compile(' +')
return re.sub(SPC, '\t', line) |
def get_function_object(obj):
"""
Objects that wraps function should provide a "__numba__" magic attribute
that contains a name of an attribute that contains the actual python
function object.
"""
attr = getattr(obj, "__numba__", None)
if attr:
return getattr(obj, attr)
return ob... |
def combine_counts(counts1, counts2):
"""
Combine two counts dictionaries.
"""
ret = counts1
for key, val in counts2.items():
if key in ret:
ret[key] += val
else:
ret[key] = val
return ret |
def so_to_dict(sqlobj):
"""Convert SQLObject to a dictionary based on columns."""
d = {}
if sqlobj is None:
return d # stops recursion
for name in sqlobj.sqlmeta.columns.keys():
d[name] = getattr(sqlobj, name)
d['id'] = sqlobj.id # id must be added explicitly
if sqlobj._inheritab... |
def parse_performance_timing(p_timing):
"""Changes performance timing results to deltas."""
ns = p_timing["navigationStart"]
return {k: v - ns if v else 0 for k, v in p_timing.items()} |
def int_to_alpha(num):
"""
Encode an integer (0-26) into alpha characters, useful for sequences of
axes/figures.
Parameters
-----------
int : :class:`int`
Integer to encode.
Returns
---------
:class:`str`
Alpha-encoding of a small integer.
"""
alphas = [chr(... |
def validate_number(x) -> bool:
"""Validates that the input is a number"""
if x.isdigit():
return True
elif x == "":
return True
else:
return False |
def to_mb(val, update_interval=None):
"""Convert bytes to MB with 2 decimals"""
tmp = 1
if update_interval:
tmp = 1/update_interval
return "{:0.2f}".format((val / 1024 / 1024) * tmp) |
def allcap_differential(words):
"""
Check whether just some words in the input are ALL CAPS
:param list words: The words to inspect
:returns: `True` if some but not all items in `words` are ALL CAPS
"""
is_different = False
allcap_words = 0
for word in words:
if word.isupper():
... |
def ccw(x, y, xm, ym, nx, ny):
"""
Determines if redrawn points are counter clockwise in cross-section
Parameters
----------
x : x-coordinates of cross-section
y : y-coordinates of cross-section
xm : x[i-1]
ym : y[i-1]
nx : new x-coordinate
ny : new y-coordinate
Returns
-------
ccw : Array of bools indic... |
def ArgumentCountException(function: str, args: int, count: int) -> bool:
"""
Checks if the correct number of arguments were given to a specific command.
:param function: The display name given to the function making the call.
:param args: The number of arguments passed into the command.
:param coun... |
def _is_boolish(b):
"""
Check if b is a 1, 0, True, or False.
"""
if (b == 1) or (b == 0) or (b == True) or (b == False):
return True
else:
return False |
def test_if_between(a, b, test_val):
"""Returns True is test_val is between a and b"""
if a < b:
return a <= test_val <= b
else:
return b <= test_val <= a |
def run_args_from_settings(settings):
"""
Build valid list of run arguments for spk_run.py based on a settings dict.
"""
# basic settings
run_args = [
settings["script"],
settings["mode"],
settings["representation"],
settings["dataset"],
settings["dbpath"],
... |
def count_leading(data, char):
"""Count number of character (char) in the beginning of string (data) """
for i in range(len(data)):
if data[i] != char:
return i
return len(data) |
def calculate_distance(a, b):
"""Helper function to calculate the distance between node a and b."""
(x1, y1) = a
(x2, y2) = b
return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 |
def cut_lines_to_n(string, n = 200):
"""
Takes a string and breaks it into lines with <= n characters per line.
Useful because psql queries have to have < 212 chars per line.
"""
out = []
lines = string.split("\n")
for line in lines:
newline = ""
words = line.split(" ")
... |
def services_by_user(account, days_back, user_arn, user_name):
""" Returns query for eventsource (service) / actions performed by user. """
query_string = f"""SELECT DISTINCT eventsource, eventname FROM behold
WHERE account = '{account}'
AND (useridentity.arn = '{user_arn}')
AND from_iso... |
def filter_current_symbol(view, point, symbol, locations):
"""
Filter the point specified from the list of symbol locations. This
results in a nicer user experience so the current symbol doesn't pop up
when hovering over a class definition. We don't just skip all class and
function definitions for the sake of lang... |
def solve(N):
"""Solve an individual problem."""
if N==0:
return 0
elif N<=4:
return 1
else:
return (N-4)//2 + 1 + N%2 |
def get_topic(domain_class: type) -> str:
"""
Returns a string describing a class.
:param domain_class: A class.
:returns: A string describing the class.
"""
return (
domain_class.__module__
+ "#"
+ getattr(domain_class, "__qualname__", domain_class.__name__)
) |
def l_out(l_in: int, padding: int, dilation: int, kernel: int, stride: int) -> int:
"""
Determine the L_out of a 1d-CNN model given parameters for the 1D CNN
:param l_in: length of input
:param padding: number of units to pad
:param dilation: dilation for CNN
:param kernel: kernel size for CNN
... |
def b(s, encoding="utf-8"):
""" bytes/str/int/float type to bytes """
if isinstance(s, bytes):
return s
elif isinstance(s, (str, int ,float)):
return str(s).encode(encoding)
else:
raise TypeError(type(s)) |
def reduce(l):
"""
Args:
Rangelist: generic list of tuples (can be either float or int tuple)
Return:
Reduced int list based on average value of every tuple in input tuple list
"""
result = []
for s in l:
midVal = abs(float(s[0]) - float(s[1])) / 2.0
result.append... |
def count_hosts(meetings):
""" Dedicated fuction to only count
specific hosts
"""
# Create an empty dictionary for hosts
host_dict = dict()
for item in meetings['items']:
# Check if the host is already counted
if item['hostEmail'] not in host_dict.keys():
host_dic... |
def pop_listtype(args, fname, ptype):
""" Reads argument of type from args """
if len(args) > 0 and isinstance(args[0], list):
if args[0][0] == fname:
p = ptype()
p.parse(args.pop(0)[1:])
return p
return None |
def convert_trunc(raw):
"""
Convert the turnc value to a tuple of (True, False) to indicate if it is 5'
or 3' truncated.
"""
if isinstance(raw, tuple):
return raw
if raw == "no":
return (False, False)
if raw == "5'":
return (True, False)
if raw == "3'":
r... |
def organiser_name(meta):
"""Get name from organiser text."""
v = meta.get('organiser') or ''
assert type(v) in (dict, str), f"{type(v)} found"
if type(v) == dict:
return v.get('name', '')
return v |
def meter_reading(m3,dl):
"""
Parameters:
-----------
m3: int
dl: list of int
Return:
-------
vol_m3: float
volume in m3 from the dial and gauges values
/!\ The nature of gauges means the gauge will show the next value early
converting requires to take 1 away from digit if digi... |
def note_to_freq(notenum):
"""Converts a MIDI note number to a frequency.
See https://en.wikipedia.org/wiki/MIDI_Tuning_Standard
"""
return (2.0 ** ((notenum - 69) / 12.0)) * 440.0 |
def round2int(value, boundary=-1):
"""Function that is used to obtain rounding value
:param value: float32, an original value
:param boundary: int, an object that represents the boundary of rounded value
:return:
"""
if value - int(value) >= 0.5:
if boundary == -1 or (int(value)+1) <= ... |
def get_most_popular_talks_by_views(videos):
"""Return the pycon video list sorted by viewCount"""
return sorted(videos, key = lambda x: int(x.metrics['viewCount']), reverse=True)
pass |
def read_file(filename, **kwargs):
"""Read file and return contents as string"""
if not filename:
return
MODE = kwargs.get('mode', 'r')
content = ''
with open(filename, MODE) as reader:
content = reader.read()
return content |
def get_processed_row(data, key):
"""
Utility function will pick either the custom overriden value or the default
value for the row.
"""
if data[key+'_r']:
return data[key+'_r']
else:
return data[key] |
def false_negative(y_true, y_pred):
"""Function to false negative
Arguments:
y_true {list} -- list of true values
y_pred {list} -- list of predicted values
"""
fn = 0
for yt, yp in zip(y_true, y_pred):
if yt == 1 and yp == 0:
fn += 1
return fn |
def remove_redundant(group):
"""
:param group:
:return:
"""
new_group = []
for j in group:
new = []
for i in j:
if i not in new:
new.append(i)
new_group.append(new)
return new_group |
def build_authenticate_header(realm=''):
"""Optional WWW-Authenticate header (401 error)"""
return {'WWW-Authenticate': 'OAuth realm="%s"' % realm} |
def total_amount_needed(amount, duration, frequency):
"""Calculates the total amount of the drug that is needed to send home with the client.
Params:
amount(Float) - Could be in mls or the number of tablets per dose.
duration(Integer) - The number of days to give the medication for
frequency(Intege... |
def get_image_ranges(experiments):
"""Get image ranges for a list of experiments (including scanless exp.)"""
# Note, if set to 1,1,for scanless experiments then first batch offset in
# _calculate_batch_offsets is zero below, bad!
return [e.sequence.get_image_range() if e.sequence else (0, 0) for e in e... |
def make_friends_list(json_data: dict) -> list:
"""
Returns a list of tuples: [(screen_name, location), ...]
from json data (.json object)
>>> make_friends_list({\
"users": [\
{\
"id": 22119703,\
"id_str": "22119703",\
"name": "Niki Jennings",\
... |
def targets_key(measurement_uuid: str, agent_uuid: str) -> str:
"""The name of the file containing the targets to probe."""
return f"targets__{measurement_uuid}__{agent_uuid}.csv" |
def add_integers(*args: int) -> int:
"""Sum integers.
Arguments:
*args: One or more integers.
Returns:
Sum of integers.
Raises:
TypeError: No argument was passed or a passed argument is not of type
`int`.
Example:
>>> add_integers(3, 4)
7
"... |
def valid_heart_json(input_json):
"""
Checks to make sure input JSON is valid and contains the appropriate keys.
Will also check to make sure the number of dates passed in matches the number
of readings.
Returns True if all checks passed, False otherwise.
"""
if(type(input_json) is dict):
... |
def create_confusion_matrix(row):
"""
Reports the number of false positives, false negatives, true positives, and true negatives
to describe the performance of the model
Args:
row: A row in the input dataframe
Returns:
Accuracy variable(variable): Returns the accuracy measure that th... |
def compute_auto_betweenclass_weighting(tests, betweenclass_weighting=True):
"""
Finds the automatic weighting used between classes of test, e.g., a
"top level" Bonferroni correction, or no correction, etc.
"""
if betweenclass_weighting:
betweenclass_weighting = {test: 1 / len(tests) for te... |
def Naive_Tokenizer(sentence):
"""
Tokenizes sentence, naively splitting by space only.
This is only for cleaning, a real tokenizer is suppossed to be applied
later in the process.
"""
return sentence.split() |
def fibonacci(n):
"""
Generates the first n Fibonacci numbers.
Adopted from: https://docs.python.org/3/tutorial/modules.html
"""
result = []
a, b = 0, 1
while len(result) < n:
result.append(b)
a, b = b, a + b
return result |
def isqrt(n):
"""Returns the integer square root of n; i.e. r=isqrt(n) is the greatest
integer such that r**2<=n. Code taken directly from "Integer square root in
python" at http://stackoverflow.com/a/15391420."""
x = n
y = (x + 1) // 2
while y < x:
x = y
y = (x + n // x) // 2
... |
def print_execution_time(start, end):
"""Helper function to print execution times properly formatted."""
hours, rem = divmod(end-start, 3600)
minutes, seconds = divmod(rem, 60)
return ("{:0>2}:{:0>2}:{:0>2}".format(int(hours),int(minutes),int(seconds))) |
def is_same_class(obj, a_class):
"""checks if an object is exactly an instance of a given class"""
return (type(obj) is a_class) |
def dec_to_float(dec):
"""
Decimal to Float
"""
if dec:
return float(dec)
else:
return None |
def is_new(urn):
""" returns the last segment of the given urn"""
idx = urn.find("$");
if idx >= 0 :
return True
else:
return False |
def state2int(points_earned, distance_reward):
"""
Helper function returns total reward for snake_game
ate_food -> reward=2
no_colition -> reward=distance_reward
colision -> reward=-2
:param points_earned: Points earned reported by SnakeGame
:param distance_reward: Calculated... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.