content stringlengths 42 6.51k |
|---|
def phpencode(text, quotechar="'"):
"""convert Python string to PHP escaping
The encoding is implemented for
U{'single quote'<http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single>}
and U{"double quote"<http://www.php.net/manual/en/language.types.string.php#languag... |
def _is_oom_error(error: RuntimeError) -> bool:
"""Check whether a runtime error was caused by insufficient memory."""
message = error.args[0]
# CUDA out of memory
if 'CUDA out of memory.' in message:
return True
# CPU out of memory
if "[enforce fail at CPUAllocator.cpp:64] . DefaultCP... |
def centroid_points_xy(points):
"""Compute the centroid of a set of points lying in the XY-plane.
Warning
-------
Duplicate points are **NOT** removed. If there are duplicates in the
sequence, they should be there intentionally.
Parameters
----------
points : list of list
A seq... |
def get_runner_image_name(base_images_project, test_image_suffix):
"""Returns the runner image that should be used, based on
|base_images_project|. Returns the testing image if |test_image_suffix|."""
image = f'gcr.io/{base_images_project}/base-runner'
if test_image_suffix:
image += '-' + test_image_suffix
... |
def possible_records(games):
"""
Input:
games (integer) - number of games played
Returns:
records (list of tuples) - list of possible records as tuples
"""
records = list()
for i in range(games + 1):
records.append((i, games-i))
return records |
def map_type(name, field_name):
"""Maps a Sysmon event type to VAST type."""
if name == "string":
return "string"
elif name == "integer":
if field_name == "SourcePort" or field_name == "DestinationPort":
return "port"
return "count"
elif name == "date":
return... |
def cal_sort_key( cal ):
"""
Sort key for the list of calendars: primary calendar first,
then other selected calendars, then unselected calendars.
(" " sorts before "X", and tuples are compared piecewise)
"""
if cal["selected"]:
selected_key = " "
else:
selected_key = "X"
... |
def add_padding(main_str, padding_str, padding_length, inverse_padding=False):
"""
Add padding to a string either in the beginning or at the end
Args:
main_str (str): string to add padding to
padding_str (str): padding character as string
padding_length (int): the length of the fina... |
def is_predicate(data: str) -> bool:
""" Returns true if predicate is a logical form predicate. """
return '@' in data |
def transcript_dna_char(base_char: str) -> str:
"""
A function that transcripts DNA char into mRNA char
returns '' when base char is not one of DNA bases
:param base_char:
:return rna_char:
"""
dict_of_dna_to_rna_base = {'A': 'U',
'T': 'A',
... |
def two_decimals(number: float):
"""Return passed number to two decimal places if not an integer,
otherwise return number as an integer,"""
if (number - int(number) != 0):
return "%.2f" % number
else:
return "%d" % number |
def multiples_of_3_5(stop):
"""Compute sum numbers in range(1, stop) if numbers are divided by 3 or 5."""
# numbers divided by 3 or 5 make cycle length 15.
frames = (stop - 1) // 15
# multiples for the first frame: 1-15
multiples = [i for i in range(1, 15 + 1) if i % 3 == 0 or i % 5 == 0]
frame_... |
def s_to_timestep(snapshot_years, snapshot_idx):
"""Convert snapshot index position to timestep.
Args:
snapshot_years (list): list of snapshot years.
snapshot_idx (int): index of snapshot
Returns:
snapshot_timestep (int): timestep of the snapshot
"""
return snapshot_years[s... |
def _CopyExcluding(er, s, t):
"""Return a copy of er, excluding all those involving triangles s and t.
Args:
er: list of (weight, e, tl, tr) - see _ERGraph
s: 3-tuple of int - a triangle
t: 3-tuple of int - a triangle
Returns:
Copy of er excluding those with tl or tr == s or t
"... |
def is_palindrome(values: list) -> bool:
"""
Function to check if a list of values is a palindrome
"""
begin, end = [0, len(values) - 1]
while begin < end:
if values[begin] != values[end]:
return False
begin, end = [begin + 1, end - 1]
return True |
def _get_version(version_info):
"""
converts version info tuple to version string
example:
(0, 1, 2, 'beta', 3) returns '0.1.2-beta.3'
"""
vi = [str(e) for e in version_info]
suffix = ''
if len(vi) > 3:
suffix = '-' + '.'.join(vi[3:])
version = '.'.join(vi[:3]) + suffix
... |
def digital_root(n):
"""
Return the sum of the digits of n, repeated until one digit remains.
This happens to be the same as n % 9 except in the case where the
modulus is 0 and n is greater than 0, in which case the result
is 9.
"""
if n == 0:
return 0
mn = n % 9
return 9 if... |
def format_allocation(nr_nodes, nr_timestamps, allocation_matrix, start_matrix):
"""Generate the formatted dict of the allocation of tasks.
Args:
nr_nodes (int): Number of fog nodes.
nr_timestamps (int): Number of timestamps.
allocation_matrix (matrix): Matrix showing the time allocated... |
def retrieve_components(command):
""" split the command and return its arguments """
command = command.strip(' ')
command = command.split(' ')
first_component = command.pop(0)
if first_component == "message" or first_component == "private":
return [command[0], ' '.join(command[1:])]
elif first_compone... |
def find_anchor_s(min_id_ind,
selected_x,
selected_y,
selected_z):
"""
Finds the anchor S using the minimum ID of selected anchors.
S anchor must be the anchor which has the minimum ID in selected
anchors.
"""
s_x = selected_x[min_id_ind]
... |
def _get_db_subnet_group_arn(region, current_aws_account_id, db_subnet_group_name):
"""
Return an ARN for the DB subnet group name by concatenating the account name and region.
This is done to avoid another AWS API call since the describe_db_instances boto call does not return the DB subnet
group ARN.
... |
def transpose(v, offset):
"""
transpose vector format midi
discards note that go out of the 0, 127 range
"""
t = []
for note in v:
if note >= 128:
tn = note + offset
if tn >= 128 and tn < 256:
t.append(tn)
else:
t.append(note)
... |
def sample_from_config(opt):
"""
Applies the distributions specified in the opt.json file
"""
output = {}
for param, dist in opt.items():
if isinstance(dist, dict):
output[param] = sample_from_config(dist)
elif isinstance(dist, list):
output[param] = [sample_... |
def rowSum(mtx):
"""Return all row-sums as a list"""
try:
for i in range(0, len(mtx)):
assert len(mtx[i]) == len(mtx[i-1]) # check whether each list has the same length.
res = list()
for j in range(0, len(mtx[0])):
tmp = 0
for i in range(0, l... |
def CheckHeader(inFile):
"""
IN: Data to search
Out: Bool and file offset where NULL was found
Description:
This function attempts to determine if the PlugX input file is XOR encoded. XOR encoded files typically have the key in the 1st 10 bytes followed by a NULL
If we don't have a NULL... |
def convert_strlist_intlist(str_labels):
"""
This fucntion is used to change str_type labels
to index_labels
params: string type list labels <list>
int_labels: int type list labels <list>
search_index: string type unique lables <list>
```python
>>> a = ["a", "b", "c", "c"]
>>> in... |
def edge_direction(point0, point1):
"""
:return: A vector going from point0 to point1
"""
return point1[0] - point0[0], point1[1] - point0[1] |
def toStr(pipeObj):
"""Create string representation of pipeline object."""
res = ''
lastIdx = len(pipeObj) - 1
for i, c in enumerate(pipeObj):
if c[0]:
res += c[0] + '('
res += toStr(c[1])
res += ')'
else:
res += c[1]
if i != lastId... |
def get_time_slot(hour, minute):
"""
Computes time_slot id for given hour and minute. Time slot is corresponding to 15 minutes time
slots in reservation table on Sutka pool web page (https://www.sutka.eu/en/obsazenost-bazenu).
time_slot = 0 is time from 6:00 to 6:15, time_slot = 59 is time from 20:45 to 21:00.
:pa... |
def _sigdigs(number, digits):
"""Round the provided number to a desired number of significant digits
:arg: number = A floating point number
digits = How many significant digits to keep
:returns: A formatted currency string rounded to 'digits' significant digits.
Example: _sigdigs(123... |
def binary_search(data, target, low, high):
"""Return True if target is found in indicated portion of a Python list.
The search only consider the portion from dat[low] to data[high] inclusive.
"""
if low > high:
return False
else:
mid = (low + high) // 2
if target =... |
def sensitivity_analysis_payload(
variable_parameter_name="",
variable_parameter_range="",
variable_parameter_ref_val="",
output_parameter_names=None,
):
"""format the parameters required to request a sensitivity analysis in a specific JSON"""
if output_parameter_names is None:
output_pa... |
def hex_to_char(s):
"""Return the unicode string of the 5-digit hex codepoint."""
return chr(int(s[1:], 16)) |
def explode(screen):
"""Convert a string representing a screen
display into a list of lists."""
return [list(row) for row in screen.split('\n')] |
def getConfiguration(basePath, recursive=False):
"""Retrieves Tags from the Gateway as Python dictionaries.
These can be edited and then saved back using system.tag.configure.
Args:
basePath (str): The starting point where the Tags will be
retrieved. This can be a folder containing, an... |
def add_probabilities(q_0, dfa, T, g_pos):
"""
This code adds probabilities to each of the intermediate nodes.
"""
pos_prob = {}
total = {}
for tau,g in T:
q = q_0
q_vis = set([q])
if q not in pos_prob:
pos_prob[q] = 0
for sigma in tau:
if (q,sigma) in dfa:
q = dfa[(q,sigma)]
q_vis.add(q)
... |
def last(path):
"""Returns a last "part" of a path.
Examples:
last('abc/def/ghi') => 'ghi'
last('abc') => 'abc'
last('') => ''
"""
if '/' not in path:
return path
return path.split('/')[-1] |
def v2_tail(sequence, n):
"""Return the last n items of given sequence.
This fixes the previous problem...
"""
if n == 0:
return []
return list(sequence[-n:]) |
def make_name(estimator):
"""Helper function that returns the name of estimator or the given string
if a string is given
"""
if estimator is not None:
if isinstance(estimator, str):
estimator_name = estimator
else:
estimator_name = estimator.__class__.__name__
else:
estimator... |
def select_case(select_key, next_state):
"""
This method returns select case for a parser
:param select_key: the action to read registers
:type select_key: str
:param next_state: the next state associated with the select key
:type next_state: str
:returns: str -- the code in plain text
... |
def create_numeric_classes(lithologies):
"""Creates a dictionary mapping lithologies to numeric code
Args:
lithologies (iterable of str): Name of the lithologies
"""
my_lithologies_numclasses = dict([(lithologies[i], i) for i in range(len(lithologies))])
return my_lithologies_numcla... |
def as_float(prod):
"""Convert json values to float and sum all production for a further use"""
prod['total'] = 0.0
if isinstance(prod, dict) and 'yuk' not in prod.keys():
for prod_type, prod_val in prod.items():
prod[prod_type] = float(prod_val)
prod['total'] += prod[prod_ty... |
def _gen_key_value(size: int) -> bytes:
"""Returns a fixed key_value of a given size."""
return bytes(i for i in range(size)) |
def email_bodies(emails):
"""Return a list of email text bodies from a list of email objects"""
body_texts = []
for eml in emails:
body_texts.extend(list(eml.walk())[1:])
return body_texts |
def _to_pydim(key: str) -> str:
"""Convert ImageJ dimension convention to Python/NumPy."""
pydims = {
"Time": "t",
"slice": "pln",
"Z": "pln",
"Y": "row",
"X": "col",
"Channel": "ch",
}
if key in pydims:
return pydims[key]
else:
return... |
def create_geocoding_api_request_str(street, city, state,
benchmark='Public_AR_Census2010',
vintage='Census2010_Census2010',
layers='14',
format='json') -> str:
"""Crea... |
def ignore_headers(headers_to_sign):
"""
Ignore headers.
"""
# Excerpts from @lsegal -
# https://github.com/aws/aws-sdk-js/issues/659#issuecomment-120477258
#
# User-Agent:
#
# This is ignored from signing because signing this causes problems
# with generating pre-sign... |
def num_range(n):
"""Generate a range of numbers around the target number `n` to use in a multiple choice question. """
return range(n - 32, n + 32) |
def remove_right_side(argument: str) -> str:
"""Removes default arguments so their names can be used as values or names of variables."""
return argument.split("=")[0] |
def clean(dictionary: dict):
"""
Used to remove inconsistencies such as word_tag1-tag2
Converts it to word_tag1 and word_tag2
"""
clean_dictionary = dictionary.copy()
for key, value in dictionary.items():
word, tag = key.split('_', 1)
tag_parts = tag.split('-')
if len(tag... |
def to_xml_elem(key, val):
"""
Returns an xml element of type key containing the val, unless val is the
empty string when it returns a closed xml element.
:param key: The xml tag of the element to generate. This must be a valid xml tag
name because it is not escaped.
:param val: The value o... |
def _star_passthrough(args):
""" this is so we can give a zipped iterable to func """
# args[0] is function, args[1] is positional args, and args[2] is kwargs
return args[0](*(args[1]), **(args[2])) |
def set_default_hyperparameters(path, taskname):
"""
Retrieve the default hyperparameters to be set for a primitive.
Parameters ---------
path: Python path of a primitive
"""
hyperparams = None
# Set hyperparameters for specific primitives
if path == 'd3m.primitives.data_c... |
def parse_weight(weight):
"""Validate node weight and format to float.
Args:
weight (str,int,float): node weight.
Returns:
float: Correctly formatted node weight.
Raises:
ValueError: Invalid node weight.
"""
try:
weight = float(weight)
except TypeError:
... |
def remove_file_from_tree(tree, file_path):
"""Remove a file from a tree.
Args:
tree
A list of dicts containing info about each blob in a tree.
file_path
The path of a file to remove from a tree.
Returns:
The provided tree, but with the item matching the s... |
def split_one(value, key):
"""
Returns the value turned into a list.
"""
return value.split(key, 1) |
def list_same(list_a, list_b):
"""
Return the items from list_b that are also on list_a
"""
result = []
for item in list_b:
if item in list_a:
result.append(item)
return result |
def get_actions(op):
"""Return the operation's array of actions."""
return op.get('metadata', {}).get('pipeline').get('actions', []) |
def GetMeta(result: dict, meta: str = '') -> list:
"""Extract from input and return a list of 2nd arg selectable of cwd user error exitcode"""
output = []
if not result: return output
if isinstance(result, dict) and 'metadata' in result: # these works only for AliEn responses
meta_opts_list = m... |
def sami_params(php, os):
"""Configuration parameters for sami with their default values"""
return {'KRW_CODE': '/code',
'SAMI_CONFIG': '/etc/sami/sami.conf.php',
'SAMI_PROJECT_TITLE': 'API Documentation',
'SAMI_SOURCE_DIR': 'src',
'SAMI_BUILD_DIR': 'docs/build/ht... |
def __is_int(s):
"""Helper function that returns whether or not a string textually
represents an integer."""
try:
int(s)
return True
except ValueError:
return False |
def _decode_edge_attributes(color):
""" decode vertex attributes from integer values
scheme:
bond order <=> tens place
parity <=> ones place (None->0, False->1, True->2)
"""
id2 = color // 10
color -= id2 * 10
id1 = color // 1
order = id2
assert id1 in (0, 1, 2)... |
def find_min(L):
"""find min uses a loop to return the minimum of L.
Argument L: a nonempty list of numbers.
Return value: the smallest value in L.
"""
result = L[0]
for x in L:
if x < result:
result = x
return result |
def get_swaps(path):
"""Take a list (path) between an end qbit index and
a start qbit index. Return a list of tuples (replacement_gates)
which correspond to the set of swap gates required to swap the
end qbit with the start qbit"""
replacement_gates = []
for i in range(len(path) - 1): # itera... |
def factors_of_a_number(num: int) -> list:
"""
>>> factors_of_a_number(1)
[1]
>>> factors_of_a_number(5)
[1, 5]
>>> factors_of_a_number(24)
[1, 2, 3, 4, 6, 8, 12, 24]
>>> factors_of_a_number(-24)
[]
"""
return [i for i in range(1, num + 1) if num % i == 0] |
def clean_date(date):
"""
Perform date format cleanup
"""
months = {'Jan': 'January',
'Feb': 'February',
'Mar': 'March',
'Apr': 'April',
'Mai': 'May',
'May': 'May',
'Jun': 'June',
'Jul': 'July',
... |
def _make_divisible(v, divisor, width=1, min_value=None):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
... |
def getFirst(text, signs):
"""
Returns a position of the sign which occurs the first.
@param {string} text.
@param {Array.<string>} signs.
@return {number} Position.
"""
positions = [text.find(sign) for sign in signs if text.find(sign) != -1]
if positions:
retur... |
def label_name(condition):
"""format label name
"""
label = condition.replace("_", " ").split(".")
label[0] = label[0].capitalize()
return " ".join(label) |
def purity(first_clustering, second_clustering):
"""
Returns the purity of the given two clusterings
:param first_clusterings: a set of iterables to compare
:param second_clusterings: a set of iterables to compare
:return: the purity index as a double
"""
summation = 0
for cluster in ... |
def export(number):
"""Exports a python float to a Java long to avoid loss of precision."""
from struct import pack, unpack
return str(unpack("<q",pack("<d", number))[0]) + "L" |
def get_wait_time(authenticated):
"""Get the wait time based on whether EUtils API use is authenticated or not.
Parameters
----------
authenticated : bool
Whether EUtils API use is authenticated.
Returns
-------
float
Wait time to use between API calls, in seconds.
Not... |
def distance(a, b):
""" Manthatten distance """
(ax, ay) = a
(bx, by) = b
return abs(ax - bx) + abs(ay - by) |
def flatten_list(lst):
"""
lst: list of lists
retuns flattened list
"""
return [item for sublist in lst for item in sublist] |
def __one_forward_open(x, y, c, l):
"""convert coordinates to zero-based, both strand, open/closed coordinates.
Parameters are from, to, is_positive_strand, length of contig.
"""
x -= 1
y -= 1
if not c: x, y = l - y, l - x
return x, y |
def number_of_bits_needed_to_communicates_no_compressed(nb_devices:int, d: int) -> int:
"""Computing the theoretical number of bits used for a single way when using compression (with Elias encoding)."""
return nb_devices * d * 32 |
def IsAioNode(tag):
"""Returns True iff tag represents an AIO node."""
return tag.startswith('aio_nodes.') |
def addBroadcastBits( iAdr, bitCount ):
"""
iAdr is 32 bit integer
bitCount is integer.
"""
# set the broadcast values
for idx in range( 32-bitCount ):
iAdr = iAdr | (1 << idx)
return iAdr |
def _rpm_long_size_hack(hdr, size):
""" Rpm returns None, for certain sizes. And has a "longsize" for the real
values. """
return hdr[size] or hdr['long' + size] |
def delete_cancelled_events(events):
"""
Function to delete all cancelled events.
:param events: all elements of `DayStudyEvents`
:type events: list
:return: list of available events
:rtype: list
"""
return [
event for event in events
if not event["IsCancelled"]
] |
def surf_vol(length, girth):
"""Calculate the surface volume of an animal from its length and girth
Args
----
length: float or ndarray
Length of animal (m)
girth: float or ndarray
Girth of animal (m)
Returns
-------
surf:
Surface area of animal (m^2)
vol: fl... |
def _decimal_lshift_exact(n, e):
""" Given integers n and e, return n * 10**e if it's an integer, else None.
The computation is designed to avoid computing large powers of 10
unnecessarily.
>>> _decimal_lshift_exact(3, 4)
30000
>>> _decimal_lshift_exact(300, -999999999) # returns None
""... |
def true_dict(data_dict):
"""
Converts string to boolean type
"""
for key in data_dict:
if str(data_dict[key]).lower() == "true":
data_dict[key] = True
elif str(data_dict[key]).lower() == "false":
data_dict[key] = False
elif type(data_dict[key]) is dict:
... |
def prob_from_crowd_label_cheat(l, uncer):
"""
return the prob of true label
from the crowd label and workers agreements
"""
agree = uncer[0] * 1.0 / (uncer[0] + uncer[1])
if l == 0:
if uncer[0] == 5: return (0.99, 0.01)
elif uncer[0] == 4: return (0.95, 0.05)
else: retur... |
def _isint(x):
"""returns True if x is an int, False otherwise"""
try:
int(x)
return True
except:
return False |
def _get(elements, index):
"""Return element at the given index or None."""
return None if index is None else elements[index] |
def insertion_sort(nums):
"""Insertion Sort"""
if not nums:
return None
for i in range(1, len(nums)):
value = nums[i]
j = i - 1
while j >= 0:
if nums[j] > value:
nums[j + 1] = nums[j]
else:
break
j = j - 1
... |
def string_bits(str_):
"""Return a string with every other character.
:return: Return a string with the odd characters removed.
"""
return str_[::2] |
def split_uri(uri, mod_attr_sep='::'):
"""Split given URI into a tuple of (protocol, module URI, attribute chain).
@param mod_attr_sep: the separator between module name and attribute name.
"""
uri_parts = uri.split(mod_attr_sep, 1)
if len(uri_parts) == 2:
mod_uri, attr_chain = uri_parts
... |
def parse_float_string(obj: str) -> float:
"""Parse a string as `float`. Throws a ValueError if `obj` is not a string.
Args:
obj : the string to parse
"""
if not isinstance(obj, str):
raise ValueError()
return float(obj) |
def get_nmods(mod_seq, mod_str="Oxidation"):
"""
Get the number of modifications.
Args:
mod_seq: str, modified sequence string
mod_str: str, modification string to count
Returns:
int, number of mods seen in mod_seq
"""
return mod_seq.count(mod_str) |
def numeric(s):
"""
:param s:
:return:
"""
try:
return int(s)
except ValueError:
try:
return float(s)
except ValueError:
return s |
def _format2_out_to_abstract(format2_step, run=None):
"""Convert Format2 'out' list for step into CWL abstract 'out' list."""
cwl_out = []
if "out" in format2_step:
out = format2_step.get("out")
if isinstance(out, dict):
for out_name in out.keys():
# discard PJA i... |
def to_int(text):
"""
extract digits from text
"""
return ''.join([char for char in text if char.isdigit()]) |
def cleaned_up(review):
"""
Clean up a review.
:param review: Review to clean up.
:return: Cleaned up review.
"""
# Remove the Beer Buddy suffix.
suffix = '---Rated via Beer Buddy for iPhone'
if review.endswith(suffix):
review = review[:len(review) - len(suffix)]
# Remove mu... |
def nested_sum(ints):
"""Returns the sum of a nested list of integers
ints: nested list of integers
"""
int_sum = 0
# Recursive case, if ints is a list then add together the sum of each element
# of ints (whether or not that element happens to be a list). Calling nested
# sum on each element... |
def merge(ll, rl):
"""Merge given two lists while sorting the items in them together
:param ll: List one (left list)
:param rl: List two (right list)
:returns: Sorted, merged list
"""
res = []
while len(ll) != 0 and len(rl) != 0:
if ll[0] < rl[0]:
res.append(ll.pop(0))
... |
def mapSeries(requestContext, seriesList, mapNode):
"""
Short form: ``map()``
Takes a seriesList and maps it to a list of seriesList. Each seriesList has the
given mapNode in common.
.. note:: This function is not very useful alone. It should be used with :py:func:`reduceSeries`
.. code-block:: none
... |
def get_volume_reduction_method(config_file):
"""
Checks in the configuration file if a volume reduction method has been set.
Parameters
----------
config_file: dict
The configuration file used in the parent code.
Returns
-------
algorithm: str
Volume reduction algorith... |
def statsboardDataConsistent(statsboardData, hostsOnStage):
"""
Utility function validates the consistency of statsboard data fetched
for use by service add ons.
:param statsboardData:
:param hostsOnStage:
:return:
"""
numHostsOnCurrStage = len(hostsOnStage)
if not statsboardData:
... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.