content
stringlengths
42
6.51k
def ensure_unique_app_labels(installed_apps): """Checks that INSTALLED APPS contains unique app labels.""" retval = () for val in installed_apps: if val in retval: continue retval += (val, ) return retval
def convert_to_cplus_type(json_type): """ convert the json type to c++ type :param json_type: the json type :return: c++ type. """ if json_type in ["boolean"]: return "bool" if json_type in ["number"]: return "double" if json_type in ["integer"]: return...
def widget_type(field): """ (stolen from django-widget-tweaks) Returns field widget class name (in lower case). """ if hasattr(field, 'field') and hasattr(field.field, 'widget') and field.field.widget: widget_name = field.field.widget.__class__.__name__.lower() if widget_name == "groupe...
def convert_to_float(number): """ converts the input to a float """ try: return float(number) except: return None
def returnCoords(coordString: str) -> tuple: """ Converts Human-Readable Coordinates into 2D list index coordString - The user-input string, such as "A3" or "C2". This is not case-sensetive. """ y = coordString[0].lower() columnCheck = {"a":0, "b":1, "c":2} x = int(coordString[1])-1 y =...
def personString(person): """ Print a nice person string e.g. mr#bryce#d#mecum#mecum@nceas.ucsb.edu """ person_strings = [] for field in person: person_strings.append("%s:%s" % (field, person[field])) return "#".join(person_strings)
def _findString(text, s, i): """Helper function of findString, which is called recursively until a match is found, or it is clear there is no match.""" # Find occurrence i2 = text.find(s, i) if i2 < 0: return -1 # Find newline (if none, we're done) i1 = text.rfind("\n", 0, i2) ...
def selection_sort(array): """ Selection sort sorts an array by placing the minimum element element at the beginning of an unsorted array. :param array A given array :return the given array sorted """ length = len(array) for i in range(0, length): min_index = i ...
def map_init(interface, params): """Intialize random number generator with given seed `params.seed`.""" import random import numpy as np random.seed(params['seed']) np.random.seed(params['seed']) return params
def rsa_crt_dmp1(private_exponent: int, p: int) -> int: """ Compute the CRT private_exponent % (p - 1) value from the RSA private_exponent (d) and p. """ return private_exponent % (p - 1)
def has_one_of_attributes(node,*args) : """ Check whether one of the listed attributes is present on a (DOM) node. @param node: DOM element node @param args: possible attribute names @return: True or False @rtype: Boolean """ if len(args) == 0 : return None if isinstance(args[0], (tuple, list)) : rargs = a...
def check_infos(data, infos, required_infos=None): """Implement infos verification logic.""" if required_infos is False or required_infos is None: return data if required_infos is True: return data, infos assert isinstance(required_infos, (tuple, list)) for required, actual in zip(re...
def filetostr(filename): """ filetostr """ try: with open(filename, "r") as stream: return stream.read() except: return None
def getDASDV(rawEMGSignal): """ Get the standard deviation value of the the wavelength.:: DASDV = sqrt( (1 / (N-1)) * sum((x[i+1] - x[i])**2 ) for i = 1 --> N - 1 * Input: * raw EMG Signal * Output: * DASDV :param rawEMGSignal: the raw EMG signal ...
def positions(width): """Helper function returning a list describing the bit positions. Bit positions greater than 99 are truncated to 2 digits, for example, 100 -> 00 and 127 -> 27.""" return ['{0:2}'.format(i)[-2:] for i in reversed(range(width))]
def upgrade_link(link: str) -> str: """ The permalink returned by praw used to have no trailing slash. We use this old version in the json file add one here to make sure it's consistent with new poems. """ if link and link[-1] != "/": link += "/" return link
def create_mapping(dico): """ Create a mapping (item to ID / ID to item) from a dictionary. Items are ordered by decreasing frequency. """ sorted_items = sorted(dico.items(), key=lambda x: (-x[1], x[0])) id_to_item = {i: v[0] for i, v in enumerate(sorted_items)} item_to_id = {v: k for...
def _get_distance(x1, y1, x2, y2): """ :return: distance between points """ return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
def merge(L, R): """Merge lists.""" A = [] while len(L) > 0 and len(R) > 0: if L[0] >= R[0]: A.append(R.pop(0)) elif L[0] < R[0]: A.append(L.pop(0)) return A + L + R
def get_parts(line): """ Split each rule line into its constituent parts >>> get_parts('1-3 a: abcde') ('1-3 a', 'abcde') >>> get_parts('1-3 b: cdefg') ('1-3 b', 'cdefg') >>> get_parts('2-9 c: ccccccccc') ('2-9 c', 'ccccccccc') :param line: :return: """ p1, p2 = line.sp...
def bus(informed_entity): """Decodes informed entity objects for buses and returns a human-readable route name Args: informed_entity (dict): The informed entity object returned by the API for a bus route Returns: str: The route ID """ route_id = informed_entity["route_id"] # h...
def swap_coordinates(geo, idx1, idx2): """ Swap the order of the coordinates of two atoms in a molecular geometry. :param geo: molecular geometry :type geo: automol molecular geometry data structure :param idx1: index for one atom to swap coordinates :type idx1: int :param i...
def get_video_daytime(file_name): """Extracts daytime when video was recorded from filename Arguments: file_name: The name of the file returns hour, minutes of the video when it was recorded """ if file_name.find("FrontColor") != -1: time_stop_pos = file_name.find("FrontColor") ...
def generalized_lorentzian(x, p): """ Generalized Lorentzian function. Parameters ---------- x: numpy.ndarray non-zero frequencies p: iterable p[0] = peak centeral frequency p[1] = FWHM of the peak (gamma) p[2] = peak value at x=x0 p[3] = power coeffici...
def hex_nring(npos): """Return the number of rings in a hexagonal layout. For a hexagonal layout with a given number of positions, return the number of rings. Args: npos (int): The number of positions. Returns: (int): The number of rings. """ test = npos - 1 nrings = ...
def check_tag(textline, tag1, tag2): """ helper function return (found tag[or None], is tab-separated) """ l = textline.strip() if l.startswith(tag1 + " "): return tag1, False elif l.startswith(tag2 + " "): return tag2, False if l.startswith(tag1 + "\t"): return tag1,...
def dec_2_bx(VALUE, ALPHABET, BASE, EXCEL_MODE): """ Convert from decimal to base X """ if VALUE == 0: return ALPHABET[0] AMOUNT_PLACES = 0 while pow(BASE, AMOUNT_PLACES) <= VALUE: AMOUNT_PLACES += 1 RETURN_VALUE = "" for i in range(AMOUNT_PLACES - 1, -1, -1): for j in range(BASE, -1, -1):...
def collapse_topology(topology): """ Collapse a topology into a compact representation. """ # make a copy so that we don't do this in place topology_full = list(topology) compact = [] counter = 0 domain = topology_full.pop(0) while topology_full: next_domain = topology_ful...
def PO2_Calc(KO2, tO2, Kr, I, qO2): """ Calculate PO2. :param KO2: oxygen valve constant [kmol.s^(-1).atm^(-1)] :type KO2 : float :param tO2: oxygen time constant [s] :type tO2 : float :param Kr: modeling constant [kmol.s^(-1).A^(-1)] :type Kr : float :param I: cell load current [A]...
def round_down_to_power_of_two(n): """Returns the nearest power-of-two less than or equal to n.""" for i in range(30, 0, -1): p = 1 << i if p <= n: return p return -1
def idle_time(boutlist, idle_threshold=15): """Takes list of times of bouts in seconds, returns idle time in seconds, i.e. time spent without bout for longer than idle_threshold in seconds. """ idle_time = 0 for i in range(0, len(boutlist) - 2): inter_bout_time = boutlist[i + 1] - boutlist[...
def alias_phased_obs_with_phase(x, y, start, end): """ :param x: a list containing phases :param y: a list containing observations :param start: start phase :param end: end phase :return: aliased phases and observations """ x = [float(n) for n in x] y = [float(n) for n in y] if...
def transform_seed_objects(objects): """Map seed objects to state format.""" return {obj['instance_id']: { 'initial_player_number': obj['player_number'], 'initial_object_id': obj['object_id'], 'initial_class_id': obj['class_id'], 'created': 0, 'created_x': obj['x'], ...
def is_float(element): """ Check if an element is a float or not """ try: float(element) return True except (ValueError, TypeError): return False
def get_prof_diff(t, e): """ Calcuate difference between profiles """ try: diff = t - float(e) except TypeError: return None return diff
def make_binary_tree(fn, args, **kwargs): """Takes a function/class that takes two positional arguments and a list of arguments and returns a binary tree of results/instances. >>> make_binary_tree(UnionMatcher, [matcher1, matcher2, matcher3]) UnionMatcher(matcher1, UnionMatcher(matcher2, matcher3)) ...
def is_free(amount): """ Explit zero amounts are interpreted as Free! """ return ( amount == 0 or amount == 0.00 or amount == '0' or amount == '0.00' )
def quaternion_solution_check(qn1, qn): """ Summary: Returns most likely quaternion solution between the positive rotation angle and is negative rotation angle. Metric to decide is the minimization of the L2 norm. Parameters: * q is a quaternion of [q0, q1, q2, q3] in a list * qn1 ...
def get_property_by_name(exchange, name): """Get property object with name ``name`` from exchange ``exchange``. Returns an empty dictionary if the property named ``name`` is not found.""" for prop in exchange.get("properties", []): if prop['name'] == name: return prop return {}
def merge(dict1,dict2): """ Returns a new dictionary merging (joining keys) dict1 and dict2. If a key appears in only one of dict1 or dict2, the value is the value from that dictionary. If it is in both, the value is the sum of values. Example: merge({'a':1,'b':2},{'b':3,'c':4}) returns ...
def ref_mode(mode): """ Defines reference pixels for different imaging modes. Parameters: mode - string containing imaging mode. Returns: xref, yref - Floating point reference pixel coordinates """ xref, yref = 692.5, 511.5 xref_slit, yre...
def clean_line(line): """ Remove \ufeff\r characters Remove \t \n \r Remove additional characters """ return line.replace('\ufeff\r', '').replace('\t', ' ').replace('\n', '').\ replace('\r', '').replace('(', '').replace(')', '').replace("'", '').\ replace('"', '').replace(',', ''...
def findKey(dict_, search): """Find a key in a dictionary. Uses '#text' format to help with the xml dictionaries. Args: dict_: Haystack to search for. search: Needle; key to search for. Returns: Value of dict_[search] """ data = {} if len(dict_) > 0: ...
def form_results_name(code, llx, lly): """ Form valid dataset name using OpenTopography bulk raster naming convention for EarthScope data. """ return code + str(llx) + '_' + str(lly) + '_results.tif'
def _build_udf_resources(resources): """ :type resources: sequence of :class:`UDFResource` :param resources: fields to be appended. :rtype: mapping :returns: a mapping describing userDefinedFunctionResources for the query. """ udfs = [] for resource in resources: udf = {resource...
def merge_dictionaries(dic1, dic2): """Merge two dictionaries :param dic1 - dictionary :param dic2 - dictionary :return dictionary""" result = dic1.copy() for key in dic2: if key in dic1.keys(): result[key] += dic2[key] else: result[key] = dic2[key] re...
def AmOppCreditParts(exact, e87521, num, c00100, CR_AmOppRefundable_hc, CR_AmOppNonRefundable_hc, c10960, c87668): """ Applies a phaseout to the Form 8863, line 1, American Opportunity Credit amount, e87521, and then applies the 0.4 refundable rate. Logic corresponds to Form 8863, P...
def parse_first_smartystreets_result(result: list) -> dict: """ Given an address *result* from SmartyStreets geocoding API, parse the canonicalized address and lat/lng information of the first result to return as a dict. If address is invalid and response is empty, then returns a dict with empt...
def isPrompt(line, prompt_list): """ Determine if the target string contains a prompt. """ for prompt in prompt_list: if prompt in line: return True return False
def dot(vector1, vector2): """ :return: The dot (or scalar) product of the two vectors """ return vector1[0] * vector2[0] + vector1[1] * vector2[1]
def are_floats(items): """ detect if all items are floats """ for i in items: try: float(i) if i is not None and len(i) > 0 else None except ValueError: return False return True
def aggregate_ontology(entries): """ Group and count GO terms """ ret_ontology = dict() for entry in entries: ontology = entries[entry].ontology if not ontology or ontology[0].rstrip() == "": continue for term in ontology: if term not in ret_ontology: ...
def fold(dots, axis, line): """Return the dots folded along line, given axis x or y.""" if axis == 'x': return {(line - abs(line - x), y) for (x, y) in dots} else: return {(x, line - abs(line - y)) for (x, y) in dots}
def geodesic_ify(splitting_name, splitting_string, n_geodesic_steps=1): """Replace every appearance of R with several Rs""" Rs = " ".join(["R"]*n_geodesic_steps) new_name = splitting_name + " ({})".format(n_geodesic_steps) new_splitting_string = splitting_string.replace("R", Rs) return new_name, ne...
def is_dash_option(string): """Whether that string looks like an option >>> is_dash_option('-p') True """ return string[0] == '-'
def ParseFloat(num_str): """Parse number string to float.""" try: return float(num_str) except (ValueError, TypeError): return None
def median(v): """finds the 'middle-most' value of v""" n = len(v) sorted_v = sorted(v) midpoint = n // 2 if n % 2 == 1: # if odd, return the middle value return sorted_v[midpoint] else: # if even, return the average of the middle values lo = midpoint - 1 ...
def iterativeFactorial(num): """assumes num is a positive int returns an int, num! (the factorial of n) """ factorial = 1 while num > 0: factorial = factorial*num num -= 1 return factorial
def match_lists(list1, list2): """to find the number of matching items in each list use sets""" set1 = set(list1) set2 = set(list2) # set3 contains all items comon to set1 and set2 set3 = set1.intersection(set2) # return number of matching items return len(set3)
def dynamic_path(input_str): """Normalise dynamic paths""" # Not needed at the moment return input_str
def image_topic_basename(topic): """ A convenience method for stripping the endings off an image topic""" endings = ['compressed', 'encoding', 'image_raw'] for e in endings: if topic.endswith(e): return topic[:-1 * len(e)] return None
def check_key(dictionary, key, default_value): """ Returns the value assigned to the 'key' in the ini file. Parameters: dictionary : [dict] key : [string|int] default_value : [string] Output: Value assigned to the 'key' into the 'dictionary' (or default value if not ...
def handle_keyword_duplicates(item): """ Sometimes keywords are combined together: "Condition, Context, Effect: ..." In this case, we remove duplicates and preserve the first keyword match. """ values = item.values() unique_values = set(values) if len(values) == len(unique_values): ...
def nbsp(value): """ Avoid text wrapping in the middle of a phrase by adding non-breaking spaces where there previously were normal spaces. """ return value.replace(" ", "\xa0")
def get_pymofa_id(parameter_combination): """ Get a unique ID for a `parameter_combination` and ensemble index `i`. ID is of the form 'parameterID_index_ID.pkl' Parameters ---------- parameter_combination : tuple The combination of Parameters Returns ------- ID : string ...
def get_geoindex(geo_matrix, index_pixel): """ This functions returns geo-coordinates of pixels of image coordinate. Args: param geo_matrix: this is a tuple which tells us which image coordinate corresponds to which geo-coordinates. type geo_matrix: tuple of 2 list. ...
def map_bound(value, in_low, in_high, out_low, out_high): """map with high and low bound handling.""" result = None if value <= in_low: result = out_low else: if value >= in_high: result = out_high else: # http://stackoverflow.com/a/5650012/574981 ...
def cndexp(condition, truevalue, falsevalue): """Simulates a conditional expression known from C or Python 2.5. :Parameters: condition : any Tells what should be returned. truevalue : any Value returned if condition evaluates to True. falsevalue : any Value returned if...
def _convert_platform_to_omahaproxy_platform(platform): """Converts platform to omahaproxy platform for use in get_production_builds_info.""" platform_lower = platform.lower() if platform_lower == 'windows': return 'win' return platform_lower
def in_bisect(word_list, target): """ Takes a sorted word list and checks for presence of target word using bisection search""" split_point = (len(word_list) // 2) if target == word_list[split_point]: return True if len(word_list) <= 1: return False if target < word_list[sp...
def check_detection_overlap(gs, dd): """ Evaluates if two detections overlap Paramters --------- gs: list Gold standard detection [start,stop] dd: list Detector detection [start,stop] Returns ------- overlap: bool Whether two events overlap. """ ov...
def manhattan_distance(pos1: tuple, pos2: tuple): """ Compute Manhattan distance between two points :param pos1: Coordinate of first point :param pos2: Coordinate of second point :return: Manhattan distance between two points """ distance = 0 for ind in range(len(pos1)): distance...
def get_partial_dict(prefix, dictionary, container_type=dict): """Given a dictionary and a prefix, return a Bunch, with just items that start with prefix The returned dictionary will have 'prefix.' stripped so:: get_partial_dict('prefix', {'prefix.xyz':1, 'prefix.zyx':2, 'xy':3}) would return...
def parse_phot_header(header): """ Parses my photometry file header. Parameters ---------- header : dictionary The header of photometry file. """ keys = header['key'] vals = header['val'] parsed_header = {} for key, val in zip(keys, vals): parsed_header[key.low...
def rough_calibration(pis, mission): """Make a rough conversion betwenn PI channel and energy. Only works for NICER, NuSTAR, and XMM. Parameters ---------- pis: float or array of floats PI channels in data mission: str Mission name Returns ------- energies : float ...
def flatten_corner(corner_kick, game_id): """Flatten the schema of a corner kick.""" ck_id = corner_kick[0] ck_data = corner_kick[1] return {'game_id': game_id, 'ck_id': ck_id, 'time_of_event(min)': (ck_data['t']['m'] + (ck_data['t']['s'] / 60 )), # 'assist': ck_data...
def add_filter_names(headerlist, filter_names, filter_labels, filters): """ Add a set of filter header labels (i.e. niriss_f090w_magnitude for example) to a list, by matching filter names. Parameters ---------- headerlist : list An existing (possibly empty) list to hold the header strin...
def transform_y(transform, y_value): """Applies a transform matrix to a y coordinate.""" return int(round(y_value * transform[1][1]))
def mod(x, y): """ Mappable modulo function :param x: First number :param y: Second number :return: x % y """ return [a % b for a, b in zip(x, y)]
def get_soloed_layers(layers): """Given a list of layers, return only those that are soloed :param layers: list of layers to filter :type layers: list :return: list of layers that are soloed. :rtype: list """ return [layer for layer in layers if layer.get_soloed()]
def fix_frequency(frequency): """ Fixes the frequency format for RS-UV3. """ return frequency.replace('.', '')
def clean_list(string): """ Strip optional characters from list for sake of comparing in tests :param string: the list to clean represented as a string :return: the clean list a as a string """ return string.replace('[', '').replace(']', '').strip()
def extract_prefix(kwords): """Converts dict of {w:count} to {w[:-1]:{w[-1]:count}}""" result = {} for w, count in kwords.items(): prefix = w[:-1] suffix = w[-1] if prefix not in result: result[prefix] = {} curr = result[prefix] if suffix not in curr: ...
def cubic_easeinout(pos): """ Easing function for animations: Cubic Ease In & Out """ if pos < 0.5: return 4 * pos * pos * pos fos = (2 * pos) - 2 return 0.5 * fos * fos * fos + 1
def _unescape_xml(xml): """ Replace escaped xml symbols with real ones. """ return xml.replace('&lt;', '<').replace('&gt;', '>').replace('&quot;', '"')
def calcp(kA, kV, cosphi = 1.0): """ Calculates three phase P in MW from kA and kV takes an optional input power factor""" return 3**(0.5) * kV * kA * cosphi
def filter(inputstr): """ Trim last character and replace NaN,Inf by a high value (1000) """ _str = inputstr[0][:-1] return _str.replace('NaN', '2147483647').replace('Inf', '2147483647')
def _is_sld(model_info, id): """ Return True if parameter is a magnetic magnitude or SLD parameter. """ if id.startswith('M0:'): return True if '_pd' in id or '.' in id: return False for p in model_info.parameters.call_parameters: if p.id == id: return p.type ...
def _human_readable(size_in_bytes): """Convert an integer number of bytes into human readable form E.g. _human_readable(500) == 500B _human_readable(1024) == 1KB _human_readable(11500) == 11.2KB _human_readable(1000000) == """ if size_in_byt...
def dict_to_cidr(obj): """ Take an dict of a Network object and return a cidr-formatted string. :param obj: Dict of an Network object """ return '%s/%s' % (obj['network_address'], obj['prefix_length'])
def wrap_list(item): """ Returns an object as a list. If the object is a list, it is returned directly. If it is a tuple or set, it is returned as a list. If it is another object, it is wrapped in a list and returned. """ if item is None: return [] elif isinstance(item, list): ...
def add_to_visited_places(x_curr: int, y_curr: int, visited_places: dict) -> dict: """ Visited placs[(x,y)] contains the number of visits that the cell (x,y). This updates the count to the current position (x_curr, y_curr) Args: x_curr: x coordinate of current position y_curr: ...
def get_renamed_pool5_vgg_weight_keys(curr_model_state_dict, saved_ckpt_dict): """ Load backbone conv weights from a traditional bernoulli dropout model into an x* heteroscedastic dropout model. These weights extend from conv1 to pool5. """ updated_dict = {} for model_key in curr_model_state_dic...
def is_written_by(author_first_name, author_last_name, paper): """ Judge whether the scraped paper belongs to the given author or not. :param author_first_name: string. First name of the author. :param author_last_name: string. Last name of the author. :param paper: string. arXiv papers sc...
def _manage_words(words, save_to=None): """just return or write to file""" if save_to is None: return words with open(save_to, 'w+') as file: file.write('\n'.join(words))
def tol(shots): """Numerical tolerance to be used in tests.""" if shots == 0: # analytic expectation values can be computed, # so we can generally use a smaller tolerance return {"atol": 0.01, "rtol": 0} # for non-zero shots, there will be additional # noise and stochastic effec...
def sst_freeze_check(insst, sst_uncertainty=0.0, freezing_point=-1.80, n_sigma=2.0): """ Compare an input SST to see if it is above freezing. :param insst: the input SST :param sst_uncertainty: the uncertainty in the SST value, defaults to zero :param freezing_point: the freezing point of the w...
def get_decision(data, decision_value): """Get the decision depending the decision value.""" result = map(lambda x : x > 0.6, data) return list(result)
def safe_filename(name): """Utility function to make a source short-name safe as a filename.""" name = name.replace("/", "-") return name
def attrs(gid='.', tid=None, exn=None, gbio=None, tbio=None, iid=None, bio=None): """Make gtf attributes (to shorten line).""" attrs_ = ['gene_id "{}"'.format(gid)] if tid: # transcript_id attrs_.append('transcript_id "{}"'.format(tid)) if exn: # exon number attrs_.append('exon_number ...