content stringlengths 42 6.51k |
|---|
def fizzbuzz(n):
"""
Return an array containing the numbers from 1 to N, where N is the parametered value. N will never be less than 1.
:param n: an integer value.
:return: an array with integers and FizzBuzz.
"""
final = []
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
... |
def indicator_object(passed_keywords: dict) -> dict: # pylint: disable=R0912 # noqa: C901
"""Create a properly formatted single indicator payload.
{
"action": "string",
"applied_globally": true,
"description": "string",
"expiration": "2021-10-22T10:40:39.372Z",
"host_groups": [
... |
def create_psql_connection_string(user: str, db_name: str,
host: str, password: str):
"""
this function is responsible for creating the psql connection string
username: str = username of server
password: str = password of server
host: str = name of server
db... |
def year_month_sorter(x: str):
"""
:param x: year_month_day splited by '-'
"""
y, m = x.split('-')
return [int(y), int(m)] |
def isSubKey(potentialParent:str, potentialChild:str) -> bool:
"""
## Example
`isSubKey("Rocket", "Rocket.name")` -> True
`isSubKey("SimControl", "Rocket.name")` -> False
"""
if potentialParent == "":
# All keys are children of an empty key
return True
pLeng... |
def to_sorted_subfloats(float_string):
"""Convert string of floats into sorted list of floats.
This function will first remove the "TSH" and turn the string of
floats into the list of floats and sort them from smaller to larger.
Args:
float_string (str): A string begin with "TSH" and followed ... |
def _residue_key_func(node):
"""
Creates a residue "key" for a node. Keys should be identical only for nodes
that are part of the same residue.
"""
attrs = 'mol_idx', 'chain', 'resid', 'resname'
return tuple(node.get(attr) for attr in attrs) |
def strip_off_fasta_suffix(s):
"""
e.g. "bin_abc.fasta" --> "bin_abc", or "bin_def.fna" --> "bin_def"
:param s: string to strip fasta suffix off of
:return: string without fasta suffix
"""
print('strip off fasta suffix for {}'.format(s))
try:
if ".fasta" in s:
return s.r... |
def add_arg_to_cmd(cmd_list, param_name, param_value, is_bool=False):
"""
@cmd_list - List of cmd args.
@param_name - Param name / flag.
@param_value - Value of the parameter
@is_bool - Flag is a boolean and has no value.
"""
if is_bool is False and param_value is not None:
cmd_list.... |
def proctime(d):
"""
Convers D to an integer in seconds
Args:
d (str): Duration
Returns:
int: Time in seconds of duration
"""
t = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800}
suffix = d[-1]
d = int(d[:-1])
d = d * t[suffix]
return d |
def fibab(n, a, b):
"""
This is general fib method.
"""
if n<2:
return 1
else:
return a*fibab(n-1,a,b) + b*fibab(n-2,a,b) |
def sum_of_square_difference(v1, v2):
"""Calculates the sum of the square differences of the components of
v1 and v2 vectors.
"""
sum = 0.0
for c1, c2 in zip(v1, v2):
t = c1 - c2
sum += t * t
return sum |
def fix_number_value(query_toks, query_toks_no_value):
"""
There is something weird in the dataset files - the `query_toks_no_value` field anonymizes all values,
which is good since the evaluator doesn't check for the values. But it also anonymizes numbers that
should not be anonymized: e.g. LIMIT 3 bec... |
def argmax(l: list) -> int:
"""
numpy.argmax()
"""
return l.index(max(l)) |
def _ArgsException(key):
"""
Dump Response of required attribute not exist exception.
:param key: the missed required attribute
:return: response string
"""
return r"""{"return": "Missed required attribute: %s", "code": "Failed"}""" % key |
def make_ewma_metric(metric, alpha):
""" Format the name of an EWMA metric. """
return f"{metric}-ewma-alpha{alpha}" |
def trim_method_name(full_name):
"""
Extract method/function name from its full name,
e.g., RpcResponseResolver.resolveResponseObject -> resolveResponseObject
Args:
full_name (str): Full name
Returns:
str: Method/Function name
"""
point_pos = full_name.rfind('.')
if po... |
def shift_scale_theta(theta):
"""
input: between -1 (-180-deg) and +1 (+180 deg)
output: between 0 (-180-deg) and +1 (+180 deg)
"""
return (theta+1)/2 |
def iou(box1, box2):
"""
Implements the intersection over union (IoU) between box1 and box2
Arguments:
box1 -- first box, list object with coordinates (box1_x1, box1_y1, box1_x2, box_1_y2)
box2 -- second box, list object with coordinates (box2_x1, box2_y1, box2_x2, box2_y2)
"""
# Assig... |
def sorted_list_difference(expected, actual):
"""Finds elements in only one or the other of two, sorted input lists.
Returns a two-element tuple of lists. The first list contains those
elements in the "expected" list but not in the "actual" list, and the
second contains those elements in the "a... |
def ensure_str(string):
"""Convert string into str (bytes) object."""
if not isinstance(string, str):
return string.encode('utf-8')
return string |
def _get_errors(exc):
""" Partial method to extract exception messages to list
"""
if hasattr(exc, 'message'):
errors = exc.messages
else:
errors = [str(exc)]
return errors |
def greet(name):
"""
dynamic route, URL variable default
variable name in URL
:param name:
:return:
"""
# redirect('/hello')
return f'<h1>Hello, {name}!</h1>' |
def _triplet(seq: str, i: int):
"""Return the ith triplet in a sequence.
Args:
seq (str): Sequence
i (int): 0-based index of the target triplet.
Returns:
str: The target triplet.
"""
start = i * 3
return seq[start:start+3] |
def href_to_basename(href, ow=None):
"""
From the bookcontainer API. There's a typo until Sigil 0.9.5.
"""
if href is not None:
return href.split('/')[-1]
return ow |
def pythonic(name):
""" Tries and creates pythonic name from c names """
name = str(name)
to_lower = lambda x: x if x == x.lower() else "_" + x.lower()
return name[0].lower() + ''.join([to_lower(u) for u in name[1:]]) |
def _FindRuleForRegister(cfi_row, reg):
"""Returns the postfix expression as string for a given register.
Breakpad CFI row format specifies rules for unwinding each register in postfix
expression form separated by space. Each rule starts with register name and a
colon. Eg: "CFI R1: <rule> R2: <rule>".
"""
... |
def str_quote(str_, quote='"'):
"""Utility to add quotes to strings, but just stringify non-strings."""
if isinstance(str_, str):
return str_.join((quote, quote))
else:
return str(str_) |
def backtracer(frame):
"""
Gets the full stack backtrace
"""
backtrace = []
curr_frame = frame
while curr_frame is not None:
backtrace.append(
{
'address': '0x%8x' % curr_frame.pc(),
'function': '%s' % curr_frame.name()
}
)... |
def elp(x):
"""These mathematical expressions use infix notation,
here the operator (e.g., +, -, *, or /)
appears in between the operands (numbers).
Python includes many ways to form compound expressions.
hoe tf do u write doc string
"""
return 3 + 5; |
def get_sleep_time(humidity):
"""
Determines how long to humidify based on some arbitrary thresholds.
:param humidity:
:return:
"""
if humidity > 65:
return None
elif humidity > 55:
return 10
else:
return 20 |
def fft_phase(numbers, offset=0):
"""Perform a phase of flawed frequency transmission."""
output = [0 for __ in numbers]
if offset > len(numbers) // 2:
num_sum = sum(numbers[offset:])
for n in range(offset, len(numbers)):
output[n] = num_sum % 10
num_sum -= numbers[n... |
def compute_dl_target(location, lpp_source, nim_lpp_sources):
"""
Compute suma DL target based on lpp source name.
When the location is empty, set the location path to
/usr/sys/inst.images
Check if a lpp_source NIM resource already exist and check the path is
the same
When the locat... |
def sort(arr):
"""
sort
:param arr:
:return:
"""
l = len(arr)
for i in range(0, l):
for j in range(i+1, l):
if arr[i] >= arr[j]:
tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
return arr |
def find_scenario_string_from_number(scenario):
"""
Find a string to represent a scenario from it's number (or None in the case of baseline).
Args:
scenario: The scenario value or None for baseline
Returns:
The string representing the scenario
"""
if scenario == 0:
retu... |
def return_geojson(lat,lng,increment):
"""returns geojson box around lat and lng"""
geojson_geometry = { # (lng,lat)
"type": "Polygon",
"coordinates": [
[
[
lng+increment,
lat+increment
],
[
lng+increment,
lat-increment
],
[
lng-increment,
lat-increment
... |
def linesin(line, piece, space):
""" given a line size, piece and space size
return number of pieces that will fit it and
0-based starting position as a tuple"""
# [ ] test edge case - no pieces are fit
pieces, rem = divmod(line+space, piece+space)
if pieces == 0:
return (0, 0)
if rem == 0:
... |
def explode(pkt, *args):
"""This function takes a dict object and explodes it into the tuple requested.
It returns None for any value it doesn't find.
The only error it throws is if args is not defined.
Example:
pkt = {'a':0, 'b':1}
0, 1, None = pdcomm.explode(pkt,... |
def net_specs(name, input_shape, num_classes, dtype='float'):
"""Returns object with net specs."""
return {
'name': name,
'input_shape': input_shape,
'num_classes': num_classes,
'dtype': dtype
} |
def strip_password(password):
"""Strip the trailing and leading whitespace.
Returns:
String
"""
return password.strip() |
def ir(some_value):
""" Because opencv wants integer pixel values"""
return int(round(some_value)) |
def readUntilNull(s):
"""
Read a string until a null is encountered
returns (string up to null , remainder after null)
"""
item = s.split(b'\0' , 1)
if len(item) == 1:
return (item[0] , None)
else:
return (item[0] , item[1]) |
def square_root_2param_t0_fit(t, a, t0):
"""t^1/2 fit w/ 2 params: slope a and horizontal shift t0."""
return a*(t-t0)**(0.5) |
def is_ip_v4_address_valid(ip_v4_address: str) -> bool:
"""
print "Valid IP address" If IP is valid.
or
print "Invalid IP address" If IP is invalid.
>>> is_ip_v4_address_valid("192.168.0.23")
True
>>> is_ip_v4_address_valid("192.255.15.8")
False
>>> is_ip_v4_address_valid("172.100... |
def pre_process_request_data(request_data):
"""
Pro process request database.
"""
if request_data is None:
request_data = dict()
return request_data |
def is_stdin(fn):
"""Return true if file is stdin"""
return fn in ['-', 'stdin'] |
def isAESround(rnd, aes_rounds):
"""
Return True if rnd is an AES round.
"""
return rnd == 0 or (((rnd + 1) % (aes_rounds + 1)) != 0) |
def round_income(x):
"""
Round income to the lower 10000th
Intput:
- income
Output:
- lower 10000th of the income. Return 0 if the income
is less than 30,000 or more than 120,000
"""
for y in range(30, 130, 10):
if x >= y*1000 and x < (y+10)*1000:
return y*1000
... |
def remove_prefix(_input, prefix):
"""Remove all prefixes in the input.
:param: _input: the input
:param: prefix: the prefix
"""
if not prefix.endswith(":"):
prefix += ':'
if not _input:
return _input
if isinstance(_input, str):
if _input.startswith(prefix):
... |
def complete_check(input_board: list) -> bool:
"""Checks if someone wins"""
# Linear
for board_line in input_board:
if board_line.count("X") == 5:
return True
# Vertical
for num in range(5):
column = []
for board_line in input_board:
colu... |
def climb_stairs(n: int) -> int:
"""
Args:
n: number of steps of staircase
Returns:
Distinct ways to climb a n step staircase
Raises:
AssertionError: n not positive integer
"""
fmt = "n needs to be positive integer, your input {}"
assert isinstance(n, int) and n > 0, ... |
def has_required(*argv):
"""For list of object/attrib pairs confirm that the objects have the attrib."""
npairs = len(argv) // 2
for i in range(npairs):
if not argv[2 * i + 1] in argv[2 * i]:
return False
return True |
def avg_Q(q_S, q_D):
"""
Return total transmission capacity of station, assuming transmission evenly distributed over hexagonal cell
Parameters
----------
q_S : float
channel capacity in Mbps
q_D : float
data demand rate in Mbps
Returns
... |
def parsePersonName(value):
"""
"""
if not value:return None
return value.lower() |
def _dict_fix_dates(my_dict):
"""Convert values of keys 'start_date' and 'end_date' to proper ISO
date format."""
for key, val in my_dict.items():
if key in ['start_date', 'end_date']:
my_dict[key] = val.isoformat()
return my_dict |
def get_strand(start, end):
"""
Checks the start and end coordinates of a sequence and returns -1 if the
sequence comes from the 3' strand and 1 if it comes from the 5' strand
start: An integer indicating sequence start location
end: An integer indicating sequence end location
"""
# -
... |
def efp_directory_size(directory_name):
""""Decode the directory name in the format NNNk to a numeric size, where NNN is a number string"""
try:
if directory_name[-1] == 'k':
return int(directory_name[:-1])
except ValueError:
pass
return 0 |
def which(program):
"""Returns the path to a program to make sure it exists"""
import os
def is_exe(fp):
return os.path.isfile(fp) and os.access(fp, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in... |
def format_name(name):
"""
format parameter names for output
:param name: Cleans a name for output
:return:
"""
name = name.replace("_", " ")
return name |
def lose(power_pellet_active, touching_ghost):
"""
:param power_pellet_active: bool - does the player have an active power pellet?
:param touching_ghost: bool - is the player touching a ghost?
:return: bool
"""
if not power_pellet_active and touching_ghost:
return True
else:
... |
def prime (num):
"""prime: calclates whether or not a number is prime
Args:
num (int): Number to be evaluated as prime
Returns:
bool: True if the number is prime, False otherwise
Note: prime() is mostly for internal use, for a more user User-friendly version,
see prime_out()
"""
co... |
def find_data_list(data):
"""Take an object and find the first list
"""
if isinstance(data, list):
# List, cool
return data
if not isinstance(data, dict):
raise Exception("Loaded data type that we don't know what to do with")
print("Loaded dict with keys: %s" % data.keys())
... |
def make_batches(size, batch_size):
"""Function extracted from Keras - check keras.engine.training_utils
for the original version.
Returns a list of batch indices (tuples of indices).
# Arguments
size: Integer, total size of the data to slice into batches.
batch_size: Integer, batch siz... |
def _get_angle(range_index, range_len, angle_increment):
"""
Return the angle of the range index.
"""
lidar_angle = (range_index - (range_len / 2)) * angle_increment
steering_angle = lidar_angle / 2
return steering_angle |
def load_secrets_file(path: str, path_startswith: bool) -> str:
"""Read the file from the given path, which should be a Docker-secrets-like file.
If path_startswith=True, only load the file if the path given starts with "/" or "./".
"""
if not path_startswith or (path.startswith("/") or path.startswith(... |
def image_output_size(input_shape, size, stride, padding):
"""Calculate the resulting output shape for an image layer with the specified options."""
if len(size) > 2 and input_shape[3] != size[2]:
print("Matrix size incompatible!")
height = size[0]
width = size[1]
out_depth = size[3] if le... |
def binary_search(data, target, low, high):
"""Return True if target is found in indicated portion of a Python list.
The search only considers the portion from data[low] to data[high] inclusive.
"""
if low > high:
return False # interval is empty; no match
else:
mid = (low... |
def _find_top_snp(sign_snp_data, ld_block_size, is_get_biggest=True):
"""
:param sign_snp_data: A 2D array: [[xpos1, yvalue1, text1], [xpos2, yvalue2, text2], ...]
"""
top_snp = []
tmp_cube = []
for i, (_x, _y, text) in enumerate(sign_snp_data):
if i == 0:
tmp_cube.append([_... |
def interpret_box(box):
"""Expand given tuple for box specification to 4-tuple form.
The returned values are normalized to be of type float, even if
corresponding values of the input weren't originally.
Examples:
>>> interpret_box( (1,2,3,4) )
(1.0, 2.0, 3.0, 4.0)
>>> interpret_b... |
def handle_entity_status(input_li, dv_li):
"""Handles DV360 entity status field by applying changes from the input_li to the dv_li
Args:
input_li: object representing the input line item from the input data
dv_li: object representing the DV360 line item
Returns: List of strings representing the fields to... |
def find_hotkey(text, attrs, hotkey='hotkey'):
"""
Given an urwid (text, attr) tuple (as returned by, e.g.
:meth:`Text.get_text`), returns the first character of the text matching
the *hotkey* attribute, or ``None`` if no character matches.
"""
ix = 0
for attr, rl in attrs:
if attr =... |
def _order_from_regexp(items, order_regexps):
"""Re-order list given regular expression listed by priorities
Example:
--------
>>> _order_from_regexp(["aA", "aZ", "bb", "bY", "cZ", "aY"], [".*Y", ".*Z"])
['bY', 'aY', 'aZ', 'cZ', 'aA', 'bb']
"""
import re
ordered = list()
for order_r... |
def get_gml(geo_json):
"""
Get polygon in GML format given GeoJSON
:param geo_json:
:return:
"""
gml = "<gml:Polygon srsName=\"http://www.opengis.net/gml/srs/epsg.xml#4326\" " \
"xmlns:gml=\"http://www.opengis.net/gml\">\n " \
" <gml:outerBoundaryIs>\n <gml:LinearRing>\... |
def safe_repr(obj, short=False):
"""
Helper class to provide backport support for `assertIn` and `assertIsInstance`
for python 2.6
"""
MAX_LENGTH = 80
try:
result = repr(obj)
except Exception:
result = object.__repr__(obj)
if not short or len(result) < MAX_LENGTH:
... |
def merge(dict1, dict2):
"""
Recursive merge dictionaries.
:param dict1: Base dictionary to merge.
:param dict2: Dictionary to merge on top of base dictionary.
:return: Merged dictionary
"""
for key, val in dict1.items():
if isinstance(val, dict):
dict2_node = dict2.setd... |
def _get_proxymodules_to_import(connection):
"""
used in _add_functions, _get_generated_proxies, and _remove_functions to get
modules to import proxies from.
"""
if connection and connection.ProxiesNS:
modules = connection.ProxiesNS
return [modules.filters, modules.sources, modules.w... |
def load_files(filenames, func_load):
"""Load a list of score files and return a list of tuples of (neg, pos)
Parameters
----------
filenames : :any:`list`
list of file paths
func_load :
function that can read files in the list
Returns
-------
:any:`list`: [(neg,pos)]... |
def reindent(string):
"""
Add the same tab size for each line in a block of lines
:param string: String, Line to apply the format. Ie,
"
This is a block
of lines with
different tabs
"
:return: String, Line with format. Ie,
"
Thi... |
def human_tidy(agents, self_state, self_name, cube):
"""
@param agents:
@param self_state:
@param self_name:
@param cube:
@return:
@ontology_type cube: Cube
"""
return [("human_pick_cube", cube), ("human_drop_cube",)] |
def _add_dicts(*dicts):
"""
Creates a new dict with a union of the elements of the arguments
"""
result = {}
for d in dicts:
result.update(d)
return result |
def toLower(s):
"""
toLower :: str -> str
Convert a letter to the corresponding lower-case letter, if any. Any other
character is returned unchanged.
"""
return s.lower() |
def formatannotation(annotation, base_module=None):
"""
This is taken from Python 3.7's inspect.py; the only change is to
add documentation.
INPUT:
- ``annotation`` -- annotation for a function
- ``base_module`` (optional, default ``None``)
This is only relevant with Python 3, so the doct... |
def extract_datetime_str(line):
"""
Construct string containing recon-all stage date and time from a log string,
for easier parsing with datetime functions.
(Example: "#@# STAGE_NAME Sun Nov 14 12:31:34 UTC 2021" --> "Nov 14 2021 12:31:34")
:param str line: line in recon-surf.log containing recon-a... |
def lint_py_check_per_line_filter(_repo, cf):
"""
Run multiple line-by-line checks
"""
if not cf or cf.binary or cf.deleted:
return False
with open(cf.name, 'r') as f:
firstline = f.readline()
if not cf.name.endswith(".py") and not 'python' in firstline:
_repo.log.info("... |
def orientation(p, q, r):
"""
Finds the orientation of an ordered set of vertices(p, q, r).
p: First vertex represented as a tuple.
q: Second vertex represented as a tuple.
r: Third vertex represented as a tuple.
returns:
0 : Collinear points
1 : Clockwise points
2 : Counterclockw... |
def to_color(category):
"""Map each category color a good distance away from each other on the HSV color space."""
import colorsys
v = (category - 1) * (137.5 / 360)
return colorsys.hsv_to_rgb(v, 1, 1) |
def is_string_matched_in_regular_expression_objects(string, regex_objects):
""" param regex_objects contains regular expression objects compiled from patterns
searches string for any occurence of each regex_object
"""
for regex_object in regex_objects:
if regex_object.search(string):
... |
def get_qual(fsize):
"""Returns file size qualifier"""
if (fsize > 2**30):
fsize = fsize/(2**30)
qual = 'Giga'
elif (fsize > 2**20):
fsize = fsize/(2**20)
qual = 'Mega'
elif (fsize > 2**10):
fsize = fsize/(2**10)
qual = 'Kilo'
else: qual = ''
fs... |
def _process(proc_data):
"""
Final processing to conform to the schema.
Parameters:
proc_data: (List of Dictionaries) raw structured data to process
Returns:
List of Dictionaries. Structured data to conform to the schema.
"""
# nothing more to process
return proc_data |
def _make_plural(s):
"""Poor man 'plural' version for now"""
if s.endswith('repository'):
return s.replace('repository', 'repositories')
else:
return s + 's' |
def timeout_cmd(cmd, timeout):
"""Return a command line prefaced with a timeout wrappers and stdout/err unbuffered."""
return 'timeout -sKILL %us stdbuf -o0 -e0 %s' % (timeout, cmd) |
def canon_decimal(msg):
"""
Format is:
139,99,22
Handler for messages that income as decimal, as opposed to hex
"""
m = msg.strip().split(",")
newmsg = [int(x) for x in m]
return newmsg |
def find_offensive_plays(possible_plays):
"""
Helper function used to find offensive plays in possible plays.
:param possible_plays: list of cards possible to be played
:return: list of cards which can be played as attack
"""
offensive_plays = []
for card in possible_plays:
if card =... |
def _remove_suffix_apple(path):
"""
Strip off .so or .dylib.
>>> _remove_suffix_apple("libpython.so")
'libpython'
>>> _remove_suffix_apple("libpython.dylib")
'libpython'
>>> _remove_suffix_apple("libpython3.7")
'libpython3.7'
"""
if path.endswith(".dylib"):
return path[:... |
def max_consecutive_ones(x):
# e.g. x= 95 (1101111)
"""
Steps
1. x & x<<1 --> 1101111 & 1011110 == 1001110
2. x & x<<1 --> 1001110 & 0011100 == 0001100
3. x & x<<1 --> 0001100 & 0011000 == 0001000
4. x & x<<1 --> 0001000 & 0010000 == 0000000
:param x:
:return:
"""... |
def _divide_and_round(a, b):
"""divide a by b and round result to the nearest integer
When the ratio is exactly half-way between two integers,
the even integer is returned.
"""
q, r = divmod(a, b)
r *= 2
greater_than_half = r > b if b > 0 else r < b
if greater_than_half or r == b and q ... |
def splglob_simple(pattern):
""" Return a splglob that either matches a full path or match a simple file """
if "/" not in pattern:
# Assume we've been given a simple file name: app.conf, *.tgz
pattern = "^.../{}$".format(pattern)
else:
pattern = "^{}$".format(pattern)
return p... |
def is_ascii(str_data: str) -> bool:
"""Checks if string contains only ascii chars.
Necessary because python 3.6 does not have a str.isascii() method.
Parameters
----------
str_data : str
string to check if it contains only ascii characters
Returns
-------
bool
True if... |
def get_imdb_string(number):
"""Convert number to IMDB_ID String.
Args:
number: number entered
Returns:
imdb_id string
"""
return 'tt{arg}'.format(arg=str(int(number)).zfill(7)) |
def get_list_of_tuples(list_given: list) -> list:
"""
:param list_given: List containing List
:return: List containing tuple
"""
list_result = []
for list_item in list_given:
list_result.append(tuple(list_item))
return list_result |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.