content stringlengths 42 6.51k |
|---|
def sklearn_model_2_file_name(result_dir):
"""Model file name for trained sklearn model"""
return '{0}/model_2.pkl'.format(result_dir) |
def _resolve_option(option_entity, selected_dish, selected_restaurant):
"""
Given an option entity that could have many potential resolved values (each representing a
unique customization option for a specific restaurant's dish), pick the most likely knowledge
base entry for the option. Here, we choose ... |
def _divides(div, n):
"""
returns True if and only if div divides n which is equivalent to
n = k*div with k an integer.
"""
return (n % div == 0) |
def clean_line( line ):
"""
clean and classify the line, return
tuple = ( cleaned_line, line_ref = str_ref, line_type ) # in flux check it out
"""
line_type = "tweet" # bad empty, retweet.... working on it
line = line.encode( encoding='UTF-8', errors='replace') # do this in the rea... |
def remove_suffix(string, suffix):
"""
This funtion removes the given suffix from a string, if the string
does indeed end with the prefix; otherwise, it returns the string
unmodified.
"""
# Special case: if suffix is empty, string[:0] returns ''. So, test
# for a non-empty suffix.
if suf... |
def number_to_pattern(index: int, k: int) -> str:
"""Convert an integer to its pattern representation
Arguments:
index {int} -- integer to convert to pattern
k {int} -- size of pattern
Returns:
str -- pattern
Example:
>>> number_to_pattern(11, 3)
'AGT'
"""
... |
def check_sites(cleaned_data):
"""
returns True if sites params same as parent or children objects (instance have same sites id as children and parent)
or if site params of instance have no sense (instance, parent or children have no sites params,)
return False if site params have not intersection wit... |
def get_expected_python_safety() -> dict:
"""expected python object"""
return {
"REQUIREMENTS_FILES": ["default-requirements.txt"],
"quick": {"REQUIREMENTS_FILES": ["small-requirements.txt"]},
"slow": {"REQUIREMENTS_FILES": ["big-requirements.txt"]},
} |
def IsLeapYear(someyear):
"""Adjust soome year is or not a leap year.
Args:
someyear: for adjust year.
Returns:
if it is a leap year, then True, else False.
"""
if (someyear % 4) == 0:
if (someyear % 100) == 0:
if (someyear % 400) == 0:
return True... |
def div_point(p, d):
"""
divide point p by d
"""
sp = []
for i in range(3):
sp.append(p[i]/d)
return sp |
def _percent_to_integer(percent: str) -> int:
"""
Internal helper for converting a percentage value to an integer
between 0 and 255 inclusive.
"""
return int(round(float(percent.split("%")[0]) / 100 * 255)) |
def resolve_blocks(source: str):
"""This is a dumb lexer that turns strings of text with code blocks (squigly
braces) into a single long string separated by semicolons. All code blocks are
converted to strings recursively with correct escaping levels. The resulting
string can be sent to break_commands t... |
def modify_data(data):
"""Modify notification data dictionary.
For easier use in templates, it joins the due_in and due today fields
together.
Args:
data (dict): notification data.
Returns:
dict: the received dict with some additional fields for easier traversal
in the notification template.
... |
def _parse_color_string(colors, n=None, r=False, start=0, stop=1):
"""
Parses strings that are formatted like the following:
'RdBu_r_start=0.8_stop=0.9_n=10'
'viridis_start0.2_r_stop.5_n20'
'Greens_start0_n15'
"""
if isinstance(colors, str):
color_settings = colors.split('_')
... |
def calc_prefix(_str, n):
"""
Return an n charaters prefix of the argument string of the form
'prefix...'.
"""
if len(_str) <= n:
return _str
return _str[: (n - 3)] + '...' |
def iterate(function,value):
"""Apply a function of a value repeatedly, until the value does not
change any more."""
new_value = function(value)
while new_value != value:
value = new_value
##debug("%s" % str(value).replace(" ",""))
new_value = function(value)
return value |
def unindent(lines):
"""
Remove common indentation from string.
Unlike doctrim there is no special treatment of the first line.
"""
try:
# Determine minimum indentation:
indent = min(len(line) - len(line.lstrip())
for line in lines if line)
except ValueErro... |
def filterEventList(events, LocationClient):
"""Take the fetched events list and compare if it is relevant to the location
returns filtered event list
"""
LocalEvents = [] # Will contain all the relevant future events after filtering by location [ datetime, 'summary']
for event in events:... |
def is_in_list(element, the_list):
"""
Prints boolean value of whether the element is in the list.
* element can be a singleton value or a list of values.
* the_list can be a single list or a list of lists.
"""
return any([element in the_list]) or any([element in row for row in the_list]) |
def str_for_containers(self):
"""
Nice printing for types and method containers.
Containers must have _container attribute containing all
elements to be printed.
"""
cont = getattr(self, '_container', None)
if cont is None:
return ''
res = ''
for child in cont:
... |
def convert_to_seconds(column_name, row):
"""converts the value in the column to a value in seconds
It will interpret the value as hours
"""
value = float(row[column_name])*60*60
return value |
def setr(registers, opcodes):
"""setr (set register) copies the contents of register A into register C.
(Input B is ignored.)"""
return registers[opcodes[1]] |
def LineStyleRev(argument):
"""Dictionary for reverse switching the line style arguement"""
switcher = {
'-' : 'Line',
'--':'Dash',
'-.' : 'DashDot',
':' : 'Dotted'
}
return switcher.get(argument, 'Line') |
def grouped_list_to_list(grouped):
"""
Takes a list of form [['group', 'item'], ...]
Returns ['# group', 'item', ...]
"""
result = []
group = ''
for pair in grouped:
if not pair[0] == group:
group = pair[0]
result.append('# ' + group)
result.append(pai... |
def combination_gen(ngrams):
""" Returns combinations for truncated expressions """
args = []
if len(ngrams) > 1:
for item1 in ngrams[0]:
for item2 in ngrams[1]:
if len(ngrams) == 2:
args.append([item1, item2])
if len(ngrams) == 3... |
def sum_of_smaller_squares_5(n: int) -> int:
"""Returns the sum of the squares of all the positive integers
smaller than n.
"""
return sum(num * num for num in range(n - 1, 0, -1)) |
def iscsi_portal_with_port(address):
"""Add default port 3260 to iSCSI portal
:param address: iSCSI portal without port
:return: iSCSI portal with default port 3260
"""
return "%(address)s:3260" % {"address": address} |
def ipaddress_to_asn_lookup(f_global_asndb_routeviews, flow_sa):
"""
Execute lookup AS number for IPs via route
"""
try:
prefix_lookup_result = f_global_asndb_routeviews.lookup(flow_sa)
flow_sa_origin_asn = prefix_lookup_result[0]
except:
flow_sa_origin_asn = None
retur... |
def gcd(x: int, y: int) -> int:
"""Greatest Common Divisor
Parameters:
x (int): Number
y (int): Number
Returns:
int: Result
"""
while y > 0:
x, y = y, x % y
return x |
def getattrs(obj, attr_names=[], alias={}):
"""Get attribute values as a dict.
obj can be any object.
attr_names should be attribute names.
alias is an attr_name:alias dict of attributes that should be renamed.
Good for pulling of initial form data out of models objects.
"""
return dict((a... |
def assign_survey(night, conf):
"""
Takes a desi production configuration (yaml) dictionary and determines
the survey corresponding to a given night based on the contents of the conf
dictionary, if psosible. Otherwise returns None.
Args:
night, int. The night you want to know the survey it ... |
def size_from_shape(shape):
"""Returns size from the shape sequence
"""
size=1
for d in shape: size*=d
return size |
def calc_inertialshapefactor(IA, IB, IC):
"""
Shape descriptor based on PMI
Cannot be calculated for plannar surfaces
"""
Si = IB / (IA*IC)
return Si |
def biground(value, base=5):
"""
>>> biground(7)
10
>>> biground(11.0)
15
"""
return int(base * round(float(value) / base)) + base
return f |
def ignore_gt(gt_anno, index, difficulty):
"""
Indicates whether to ignore GT sample
Args:
gt_anno [dict]: Ground truth annotation
index [int]: GT sample index
difficulty [int]: Difficulty index
Returns
ignore [bool]: Ignore flag
"""
# Compute ignore
ignore = ... |
def find_sqltype(val):
"""
Find sqlite data type which matches the type of `val`.
Parameters
----------
val : any python type
Returns
-------
sqltype : str
String with sql type which can be used to set up a sqlile table
"""
mapping = {\
type(None): 'NULL',
... |
def cnum1(s):
"""
x[y] -> y
z -> z
"""
p1 = s.find('[')
if p1 < 0:
return s
p2 = s.find(']')
return s[p1+1:p2] |
def from_sexagesimal(x, hours=False):
"""
Given string of the form "dd:mm:ss.sss" or "hh:mm:ss.sss" (when
hours=True), convert to decimal degrees.
"""
w = x.split(':')
w[0] = w[0].strip()
s = 1.
if w[0][0]=='-':
s=-1.
w[0] = w[0][1:]
y = 0.
c = 1.
for yy in w:... |
def reverseComplement(seq, alphabet='ACGT'):
"""
Returns the reverse complement of nucleic acid sequence input.
"""
compl= dict(zip('ACGTNRYWSMKBHDV', 'TGCANYRWSKMVDHB'))
return ''.join([compl[base]
for base in seq.upper().replace('U', 'T')])[::-1] |
def ensure_list(specifier):
"""
if specifier isn't a list or tuple, makes specifier into a list containing just specifier
"""
if not isinstance(specifier, list) and not isinstance(specifier, tuple):
return [specifier,]
return specifier |
def record_to_header(record, delimiter=None, name_only=False):
"""Builds a header for a given record."""
if name_only:
return record["name"]
fields = ["name", "organism", "scaffold", "start", "end"]
values = [str(record[field]) for field in fields]
if delimiter:
return delimiter.join... |
def first_or_default(items, default=None):
"""
Helper method to fetch first element of list (if exists)
"""
for item in items:
return item
return default |
def parse_instagram_video_embed(msg, width="200px", height="200px"):
""" need to put in proper video embed here """
video_url = msg['videos']['standard_resolution']['url']
picture_url = msg['images']['standard_resolution']['url']
video_str = """<video controls preload="none" width="{}" height="{}" poste... |
def get_url_from_formula(formula):
"""Return URL from the given HYPERLINK formula.
Args:
formula (str): HYPERLINK formula.
Returns:
str: Resource URL.
"""
return formula.split('",')[0][12:] |
def job(priority, properties):
"""Creates a new job with the given priority and properties."""
return {
"priority": priority,
"properties": properties,
} |
def get_header(headers, header_name):
"""
:return: The header with the given name as a tuple with the name and
value, or None if there was no such header.
"""
if headers is None:
return None
for name, value in headers.items():
if name.lower() == header_name.lower():
... |
def count_frequency(word_list):
"""
Count the frequency of each word in the list
by scanning the list of already encountered
words.
"""
L = []
for new_word in word_list:
for entry in L:
if new_word == entry[0]:
entry[1] = entry[1] + 1
break
else:
L.append([new_word, 1]... |
def complement(template_strand):
"""
Computes the complement strand of the given template strand.
Parameters
----------
template_strand : string
The template strand we are evaluating.
Returns
-------
complement_strand : string
The resulting string after finding the comp... |
def get_row_col(mouse_x, mouse_y):
""" Converts an x, y screen position into a row, col value. """
# Note: the top row is row=0 (bottom row=2), left col is col=0 (right col=2)
spacing_x = 86 + 8
spacing_y = 98 + 5
top_y = 50
left_x = 50
return (mouse_y - top_y) // spacing_y, (mouse_x - left_... |
def p1evl(x, coef, N):
"""
Evaluates the given polynomial of degree <tt>N</tt> at <tt>x</tt>.
Evaluates polynomial when coefficient of N is 1.0. Otherwise same as
<tt>polevl()</tt>.
Coefficients are stored in reverse order.
The function <tt>p1evl()</tt> assumes that <tt>coef[N] = 1.0</tt> and ... |
def class_description(cl, maxlength=80, desc_attribute="description"):
""" Returns a short description of a class, based on a
description class attribute (if present) and the class path:
Examples (maxlength = 45)::
"Short description (path.to.the.class)"
"This is a longer description (path... |
def cont_search_data(raw_data):
"""
Function for interactive search. Returns organization number and name if partial name is matches
:param raw_data: json data filtred by first letters
:return: list
"""
data_to_return = []
for el in range(len(raw_data['data'])):
data_to_return.append... |
def add_extension_to_file(file: str,
extension: str) -> str:
"""Add extension to a file.
:param file: File path to which to add an extension
:param extension: Extension to append to file (e.g., mp3)
:return: File with extension
"""
return file + "." + extension |
def flist(start, stop, step):
"""
Takes in:
start, stop, step = integers or floats
Returns:
zlist = list start to stop with step as increment
"""
# print('-flist has been called')
i = 0
zlist = [start]
while zlist[i] < stop:
nextvalue = zlist[i] + step
... |
def down_diagonal_contains_only_xs(board):
"""Check whether the going down diagonal contains only xs"""
for i in range(len(board)):
if board[i][i] != "X":
return False
return True |
def fixextensions(peeps, picmap, basedir="."):
"""replaces image names with ones that actually exist in picmap"""
fixed = [peeps[0].copy()]
missing = []
for i in range(1, len(peeps)):
name, ext = peeps[i][2].split(".", 1)
if (name in picmap):
fixed.append(peeps[i].copy())
... |
def convert(im_height, im_width, box):
"""
Converts the box from normalized coordinates to absolute pixel values.
Arguments:
im_height: int
The image height, in pixels
im_width: int
The image width, in pixels
box: (int, int, int, int)
(left, right, top, bot) box co... |
def get_policy_length(control_str_ids, n_control_steps):
"""
get the length of the policy. ASSUMPTION - PUMP controls are binary 1 BIT,
ORIFICE and WEIR are 3 BITS
returns: [int] the number of total control decisions in the policy
"""
pol_len = 0
for ctl_id in control_str_ids:
ctl... |
def update_labels_and_tags(dataset_id,
existing_labels_or_tags,
new_labels_or_tags,
overwrite_ok=False):
"""
Updates labels or tags in dataset if not set or needing to be updated
or overwrites existing labels or tags in the dat... |
def find_vswitch_name_for_id(k2_id, vswitch_map):
"""
Returns the vSwitch name for a given vSwitch K2 ID
:param k2_id: The K2 vSwitch ID
:param vswitch_map: The vSwitch map from the HMC Topo
:returns: vSwitch Name
"""
k2_obj = vswitch_map.get(k2_id)
if not k2_obj:
return None
... |
def image_check(name: str):
"""
A function that checks the string end for image file suffix.
Args:
name (str): the string to test the suffix for
Returns:
True: if the suffix suffix contains image extension
False; if the string suffix does not contain image extension
"""
... |
def _clamp(value, minx, maxx):
"""
Constrain a value between a minimum and a maximum.
If the value is larger than the maximum or lower than the minimum, the
maximum or minimum will be returned instead.
:param int value: The value to clamp.
:param int minx: The minimum the value can take.
:... |
def static_file(file_path):
"""
[This function will help in serving a static_file]
:param file_path [str]: [file path to serve as a response]
"""
return {
"type": "static_file",
"file_path": file_path,
# this is a hack for now
"body": "",
"status_code": "200... |
def offsetInRAM( dolOffset, sectionInfo ): # todo: write into dolInitializer method
""" Converts the given DOL offset to the equivalent location in RAM once the DOL file is loaded. """
ramAddress = -1
# Determine which section the DOL offset is in, and then get that section's starting offsets in both the dol and ... |
def read_excl_input(excl_file):
"""
reads in the exclude file
:param excl_file:
:return: list(excluded_epitopes)
"""
excluded_epitopes = []
if not excl_file is None:
with open(excl_file, "rU") as f:
excluded_epitopes = f.read().splitlines()
return excluded_epito... |
def adf_factor(x):
"""return an adjustment factor based on an fitted curve to the
strava gradient adjusted pace curve"""
coeff = [0.0017002, 0.02949656]
return coeff[0]*x**2 + coeff[1]*x + 1.0 |
def get_info_file_path(path):
"""
example:
path=/000001/000001493.png
returns /000001/000001493.gt_data.txt
"""
return path[:16] + '.gt_data.txt' |
def arb_gain(session, Type='Real64', RepCap='', AttrID=1250202, buffsize=0, action=['Get', '']):
"""[Arbitrary Gain <real64>]
Sets/Gets the Gain for the waveform. Allowable range of values depends upon connection type:
Single ended passive mode = 0.170 to 0.250 (best signal fidelity); Single ended ampl... |
def parseTheData(kubra_output_data_json2Dict):
"""
Parse the provided dict and return the epoch timestamp
"""
if 'updatedAt' in kubra_output_data_json2Dict.keys():
return kubra_output_data_json2Dict['updatedAt']
else:
return 0 |
def _check_similarity(similarity, length):
"""Check if measures are similarities or dissimilarities"""
if isinstance(similarity, bool):
similarity_list = [similarity] * length
elif isinstance(similarity, list) and all(isinstance(s, bool) for s in similarity) and len(similarity) == length:
si... |
def search_obj(bucketObj, searchList):
"""
Search function for s3 objects
input: s3 object in boto3, list of items
output: s3 object key list
"""
result=[]
for target in searchList:
for candidate in bucketObj.objects.all():
if str(candidate.key).find(target) > -1: # if ta... |
def max_key(words):
"""Return the item with the maximum key in words.
words: dictionary
"""
return words[max(words.keys())] |
def is_numeric(value):
""" check whether a value is numeric (could be float, int, or numpy numeric type) """
return hasattr(value, "__sub__") and hasattr(value, "__mul__") |
def extract_cands(mystr,candd):
""" extract candidate names from _-separated string, increment dict entries
"""
for c in mystr.split("_"):
if c in candd.keys():
candd[c] += 1
else:
candd[c] = 1
return candd |
def row_to_bbox_coordinates(row):
"""
Takes a row and returns a dictionary representing bounding
box coordinates: (center_x, center_y, width, height) e.g. {'x': 100, 'y': 120, 'width': 80, 'height': 120}
"""
return {'x': row['xMin'] + (row['xMax'] - row['xMin'])/2,
'width': (row['xMax'... |
def is_iterable(x):
"""Check if x is iterable"""
try:
iter(x)
return True
except:
return False |
def _only_keys(dict_ins, keys):
"""
Filters out unwanted keys of a dict.
"""
return {k: dict_ins[k] for k in dict_ins if k in keys} |
def flatten(contours):
"""\
Return a single contour that contains all the points in the given contours.
"""
return [segment for contour in contours for segment in contour] |
def binom_coeff(n):
"""
Calculate the binomial coefficient (n, 2), i.e. the number of distinct pairs possible
in a set of size n
:param n: size of set
:return: number of pairs
"""
return int(n * (n-1) / 2) |
def tf_sum(q, d, c):
"""
ID 1 for OHSUMED
"""
result = 0.0
for w in set(q) & set(d):
result += d[w]
return result |
def _heading_sort_order(msg_line):
"""
Custom first (i.e. generic False first), then by message i.e. by title
"""
custom = ' #### ' in msg_line[: 10] ## Custom all start with #### ...
return (not custom, msg_line) |
def ndistance(p1, p2):
"""
Calculate eucleidian distance between two points in N-dimensional space
NOTE: The two points must have the same number of
dimensions, thus having the same shape.
Points' dimension is the same as their index... |
def _get_dependencies(dependencies):
"""Method gets dependent modules
Args:
dependencies (dict): dependent modules
Returns:
list
"""
mods = []
for key, val in dependencies.items():
mods.append(val['package'])
return mods |
def _CheckSuccessful(response):
"""Checks if the request was successful.
Args:
response: An HTTP response that contains a mapping from 'status' to an
HTTP response code integer.
Returns:
True if the request was succesful.
"""
return "status" in response and 200 <= int(response["status"... |
def read_file(filepath):
"""Open and read file contents.
Parameters
----------
filepath : str
path to a file
Returns
-------
str
contents of file
Raises
------
IOError
if file does not exist
"""
file_data = None
with open(filepath, "r") as f... |
def expandUrlData(data):
"""
dict -> a param string to add to a url
"""
string = "?" # the base for any url
dataStrings = []
for i in data:
dataStrings.append(i+"="+data[i])
string += "&".join(dataStrings)
return string |
def separe(values, sep, axis=0):
"""
Separate values from separator or threshold.
:param values: list of values
:param sep: peparator value
:param axis: axis in each value
:return:lists of greater values, list of lesser values
"""
greater,lesser = [],[]
for i in values:
if i... |
def la_hoanthien(n):
"""Kiem tra so hoan thien"""
sum = 0
for i in range(1,n):
if n%i == 0:
sum+=i
if sum == n:
return True
return False |
def getTotalTime(segs, countMultipleSpkrs=True):
"""
This function counts the overall ground truth time after removing the collars. An option allows overlapping
speaker time to be double counted or not.
Inputs:
- segs: the list of ground truth and diarization segments after removing segments... |
def post_search_key(post):
"""Search key for post."""
return '{} {}'.format(post['title'], post['author']) |
def info2lists(info, in_place=False):
"""
Return info with:
1) `packages` dict replaced by a 'packages' list with indexes removed
2) `releases` dict replaced by a 'releases' list with indexes removed
info2list(info2dicts(info)) == info
"""
if 'packages' not in info and 'releases' not in i... |
def get_record_value(record):
"""Return a list of values for a hosted zone record."""
# test if record's value is Alias or dict of records
try:
value = [
":".join(
[
"ALIAS",
record["AliasTarget"]["HostedZoneId"],
... |
def _ainvhash(anum,num_of_arrows,dim):
"""Turns an arrow hash back into the array of arrow directions.
Args:
anum (int): The arrow hash number.
num_of_arrows (int): The number of arrows in the system.
dim (int): The number of directions the arrows can point.
Returns:
arrows... |
def set_binary_labels(input_examples, positive_label):
"""
Replaces the class labels with 1.0 or -1.0, depending on whether
the class label matches 'positive_label'.
Returns an array of tuples, where the first element is the label number
and the second is a path to the image file.
"""
exam... |
def drop_unadjusted_fields(mapping):
"""Drops all fields beyond mapping[0:12], except for the cigar and alignment type field.
Args:
mapping ([type]): [description]
"""
# print("mapping before drop:", mapping)
# Find the cigar and mapping-type fields.
keep_fields = list()
for field i... |
def num_to_int(num):
"""
Checks that a numerical value (e.g. returned by robot) is an integer and
not a float.
Parameters
----------
num : number to check
Returns
-------
integer : num cast to an integer
Raises
------
ValueError : if n is not an integer
"""
if ... |
def get_package(version, *args) -> str:
"""..."""
v = version.replace(".", "_")
return ".".join(["kuber", f"{v}", *args]) |
def bubble_sort(array):
""" do the bubble sort"""
array_len = len(array)
for i in range(array_len):
# Create a flag that will allow the function to
# terminate early if there's nothing left to sort
already_sorted = True
# Start looking at each item of the list one by one,
... |
def convert_priority(priority):
"""
Flips the priority from the interface version to the api version
In the user interface, 1 is most important and 4 is least important.
In the api, 4 is most important, and 1 is least important.
Args:
priority (int): The user inputted priority
Returns:
int
The API versio... |
def det_cat_fct_merge(contab_1, contab_2):
"""
Merge two contingency table objects.
Parameters
----------
contab_1: dict
A contingency table object initialized with
:py:func:`pysteps.verification.detcatscores.det_cat_fct_init`
and populated with
:py:func:`pysteps.verificatio... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.