seed
stringlengths
1
14k
source
stringclasses
2 values
def stations_by_river(stations): """Returns a dictionary mapping the names of rivers with a list of their monitoring stations""" # Creates a dictionary of rivers that map to a list of their monitoring stations rivers = dict() for station in stations: # Adds names of monitoring stations into the list under each river if station.river is not None: if station.river in rivers.keys(): rivers[station.river].append(station.name) else: rivers[station.river] = [station.name] else: pass # Sorts the lists of monitoring stations alphabetically for river in rivers.keys(): rivers[river].sort() return rivers
bigcode/self-oss-instruct-sc2-concepts
def create_field_matching_dict(airtable_records, value_field, key_field = None, swap_pairs = False): """Uses airtable_download() output to create a dictionary that matches field values from the same record together. Useful for keeping track of relational data. If second_field is `None`, then the dictionary pairs will be {<record id>:value_field}. Otherwise, the dictionary pairx will be {key_field:value_field}. If swap_pairs is True, then dictionary pairs will be {value_field:<record id>(or key_field)}. """ airtable_dict = {} for airtable_record in airtable_records: if key_field == None: key = airtable_record['id'] else: key = airtable_record['fields'].get(key_field) value = airtable_record['fields'].get(value_field) if swap_pairs: airtable_dict.update({key : value}) else: airtable_dict.update({value : key}) return airtable_dict
bigcode/self-oss-instruct-sc2-concepts
def gen_rect(t, b, l, r): """ :param t: top latitude :param b: bottom latitude :param l: left longitude :param r: right longitude :return: GeoJSON rect with specified borders """ ret = { 'type': 'Feature', 'properties': {}, 'geometry': { 'type': 'Polygon', 'coordinates': [[[l, t], [r, t], [r, b], [l, b], [l, t]]] } } return ret
bigcode/self-oss-instruct-sc2-concepts
import copy def exact_to_1st_order_model(model): """Convert model training on exact augmented objective to model training on 1st order approximation. """ model_1st = copy.deepcopy(model) model_1st.approx = True model_1st.feature_avg = True model_1st.regularization = False return model_1st
bigcode/self-oss-instruct-sc2-concepts
def kmp(S): """Runs the Knuth-Morris-Pratt algorithm on S. Returns a table F such that F[i] is the longest proper suffix of S[0...i] that is also a prefix of S[0...i]. """ n = len(S) if n == 0: return [] F = [0] * n for i in range(1, n): k = F[i - 1] while k > 0 and S[k] != S[i]: k = F[k - 1] if S[k] == S[i]: k += 1 F[i] = k return F
bigcode/self-oss-instruct-sc2-concepts
def quat_real(quaternion): """Return real part of quaternion. >>> quat_real([3, 0, 1, 2]) 3.0 """ return float(quaternion[0])
bigcode/self-oss-instruct-sc2-concepts
def calc_plan(plan, beam_set, norm): """Calculate and normalize treatment plan. Parameters ---------- plan : connect.connect_cpython.PyScriptObject Current treatment plan. beam_set : connect.connect_cpython.PyScriptObject Current beam set. norm : (str, float, float) Region of interest, dose, and volume used for normalization. Returns ------- int RayStation exit status: - 0: success - 1: normalization failed - 2: optimization failed """ # Calculate plan plan.PlanOptimizations[0].ResetOptimization() try: plan.PlanOptimizations[0].RunOptimization() except: return 2 # Normalize plan try: beam_set.NormalizeToPrescription( RoiName=norm[0], DoseValue=norm[1], DoseVolume=norm[2], PrescriptionType='DoseAtVolume') return 0 except: return 1
bigcode/self-oss-instruct-sc2-concepts
import hashlib def sha256sum(body): """Get a SHA256 digest from a string.""" h = hashlib.sha256() if body: h.update(body) return h.hexdigest()
bigcode/self-oss-instruct-sc2-concepts
from typing import Optional from typing import List import pkg_resources def get_version( package_name: str, raise_on_error: bool, alternate_package_names: Optional[List[str]] = None, ) -> Optional[str]: """ :param package_name: The name of the full package, as it would be imported, to get the version for :type package_name: str :param raise_on_error: True to raise an error if package is not installed or couldn't be imported, False to return None :type raise_on_error: bool :param alternate_package_names: List of alternate names to look for the package under if package_name is not found. Useful for nightly builds. :type alternate_package_names: Optional[List[str]] :return: the version of the desired package if detected, otherwise raises an error :rtype: str """ current_version: Optional[str] = None version_err = None try: current_version = pkg_resources.get_distribution(package_name).version except Exception as err: version_err = err if version_err and alternate_package_names: next_package = alternate_package_names.pop() return get_version(next_package, raise_on_error, alternate_package_names) if version_err and raise_on_error: raise ImportError( f"error while getting current version for {package_name}: {version_err}" ) return current_version if not version_err else None
bigcode/self-oss-instruct-sc2-concepts
def get_simulation(open_rocket_helper, ork_file_path, i_simulation): """Return the simulation with the given index from the .ork file. :arg open_rocket_helper: Instance of ``orhelper.Helper()`` :raise IndexError: If `i_simulation` is negative or >= the number of simulations in the given .ork file """ doc = open_rocket_helper.load_doc(ork_file_path) n_simulations = doc.getSimulationCount() if i_simulation < 0 or i_simulation >= n_simulations: raise IndexError( "Simulation index is out of bounds!\n" + "i_simulation = {}\n".format(i_simulation) + "n_simulations = {}\n".format(doc.getSimulationCount())) sim = doc.getSimulation(i_simulation) print("Load simulation number {} called {}.".format( i_simulation, sim.getName())) return sim
bigcode/self-oss-instruct-sc2-concepts
def within_time_period(t, time_period): """ Check if time is in the time period Argument: t = given time in format (half, min, sec) time_period = tuple of (start_time, end_time) in format (half, min, sec) Return: boolean """ start_time = time_period[0] end_time = time_period[1] assert start_time[0] == end_time[0], "TIME PERIOD NOT IN THE SAME HALF !" if t[0] == start_time[0]: t_sec = t[1]*60+t[2] start_sec = start_time[1]*60 + start_time[2] end_sec = end_time[1]*60 + end_time[2] if t_sec >= start_sec and t_sec <= end_sec: return True return False
bigcode/self-oss-instruct-sc2-concepts
def compute_agreement_score(arcs1, arcs2, method, average): """Agreement score between two dependency structures Parameters ---------- arcs1: list[(int, int, str)] arcs2: list[(int, int, str)] method: str average: bool Returns ------- float """ assert len(arcs1) == len(arcs2) if method == "joint": shared_arcs = set(arcs1) & set(arcs2) score = float(len(shared_arcs)) elif method == "independent": score = 0.0 dep2head1 = {d: (h, r) for h, d, r in arcs1} dep2head2 = {d: (h, r) for h, d, r in arcs2} for dep in dep2head1.keys(): head1, rel1 = dep2head1[dep] head2, rel2 = dep2head2[dep] if head1 == head2: score += 0.5 if rel1 == rel2: score += 0.5 else: raise Exception("Never occur.") if average: score = float(score) / len(arcs1) return score
bigcode/self-oss-instruct-sc2-concepts
def read_image(file_path): """ Read an image file """ with open(file_path, "rb") as ifile: return ifile.read()
bigcode/self-oss-instruct-sc2-concepts
def manhattan_distance(params): """ Manhattan distance from current position to target Args: current (tuple): x, y coordinates of the current position target (tuple): x, y coordinates of the target Returns: (float): Manhattan distance from current position to target """ current, target, solution = params if not solution: return -100 dist = abs(target[0] - current[0]) + abs(target[1] - current[1]) target_reached = dist == 0 return -dist + (100 * target_reached)
bigcode/self-oss-instruct-sc2-concepts
def row_normalize(x): """ Scale a matrix such that each row sums to one """ return x / x.sum(axis=1)[:, None]
bigcode/self-oss-instruct-sc2-concepts
import re def error_has_gloss(value): """Checks if the value has at least a hyphen followed by two capital letters""" return bool(re.match(r'.*\-[A-Z]{2,}', value))
bigcode/self-oss-instruct-sc2-concepts
def argmin(arr, f): """Return the index, i, in arr that minimizes f(arr[i])""" m = None i = None for idx, item in enumerate(arr): if item is not None: if m is None or f(item) < m: m = f(item) i = idx return i
bigcode/self-oss-instruct-sc2-concepts
def divide_toward_zero(x, y): """Divides `x` by `y`, rounding the result towards zero. The division is performed without any floating point calculations. For exmaple: divide_toward_zero(2, 2) == 1 divide_toward_zero(1, 2) == 0 divide_toward_zero(0, 2) == 0 divide_toward_zero(-1, 2) == 0 divide_toward_zero(-2, 2) == -1 Args: x (int): The numerator of the division. y (int): The denominator of the division. Returns: The int result rounded towards 0. """ return (x // y) if (x * y) > 0 else ((x + (-x % y)) // y)
bigcode/self-oss-instruct-sc2-concepts
def comma_join(fields): """ Converts everything in the list to strings and then joins them with commas. """ return ",".join(map(str, fields))
bigcode/self-oss-instruct-sc2-concepts
def sort_x_by_y(x, y): """Sort the iterable x by the order of iterable y""" x = [x for (_, x) in sorted(zip(y, x))] return x
bigcode/self-oss-instruct-sc2-concepts
import psycopg2 def _execute_psyco(command, **kwargs): """ executes a postgres commandline through psycopg2 :param command: A psql command line as a str :param kwargs: will be forwarded to psycopg2.connect """ # Note: Ubuntu 18.04 uses "peer" as the default postgres configuration # which allows connections only when the unix user matches the database user. # This restriction no longer applies for IPv4/v6-based connection, # when specifying host=localhost. if kwargs.get('host') is None: kwargs['host'] = 'localhost' output = None with psycopg2.connect(**kwargs) as conn: conn.autocommit = True with conn.cursor() as cursor: cursor.execute(command) if cursor.description is not None: output = cursor.fetchall() # see http://initd.org/psycopg/docs/usage.html#with-statement conn.close() return output
bigcode/self-oss-instruct-sc2-concepts
def pair(s1, s2, track_id_pairs): """Returns pairs of tracks, i.e., tracks that can be compared. s1 -- all tracks of sensor 1. s2 -- all tracks of sensor 2. track_id_pairs -- ID pairs of tracks of s1 and s2. """ pairs = [] # collect available ids of the sensor's tracks for tid1, tid2 in track_id_pairs: try: t1 = s1[tid1] t2 = s2[tid2] pairs.append([t1, t2]) except Exception as e: # tracks may vanish from view, ignore # (pair gets unused at some point) pass return pairs
bigcode/self-oss-instruct-sc2-concepts
import functools def lsp_rpc(f): """A decorator for LanguageServerProtocol-methods. This wrapper filters out calls that are made before initializing the server and after shutdown and returns an error message instead. This decorator should only be used on methods of LanguageServerProtocol-objects as it expects the first parameter to be a the `self`-reference of this object. All LSP-methods should be decorated with this decorator except initialize and exit """ @functools.wraps(f) def wrapper(*args, **kwargs): try: self = args[0] except IndexError: self = kwargs['self'] if self.shared.shutdown: return {'code': -32600, 'message': 'language server already shut down'} elif not self.shared.initialized: return {'code': -32002, 'message': 'language server not initialized'} else: return f(*args, **kwargs) return wrapper
bigcode/self-oss-instruct-sc2-concepts
import json from typing import OrderedDict def validate_payload(payload): """Validate that the payload is of type OrderedDict. If the payload is of type str, then it assumes that the string is able to be parsed via json. Args: payload (str or OrderedDict): Payload object Returns: OrderedDict: Original payload object as an OrderedDict. """ if isinstance(payload, str): payload = json.JSONDecoder( object_pairs_hook=OrderedDict ).decode(payload) if not isinstance(payload, OrderedDict): raise TypeError("Payload must be of type OrderedDict.") return payload
bigcode/self-oss-instruct-sc2-concepts
def clean_cluster_seq_id(id): """Returns a cleaned cd-hit sequence id The cluster file has sequence ids in the form of: >some_id... """ return id[1:-3]
bigcode/self-oss-instruct-sc2-concepts
def longVal(x): """ longVal(x): if 'x' is a z3 constant (i.e. function of arity 0) whose value is an integer, then return that integer as a python long else return 'None'""" if(hasattr(x, 'as_long')): return x.as_long() elif(hasattr(x, 'numerator_as_long')): if(x.denominator_as_long() == 1): return x.numerator_as_long() return None
bigcode/self-oss-instruct-sc2-concepts
def bytes_to_str(bytes, base=2, precision=0): """Convert number of bytes to a human-readable format Arguments: bytes -- number of bytes base -- base 2 'regular' multiplexer, or base 10 'storage' multiplexer precision -- number of decimal places to output Returns: Human-readable string such as '1.32M' """ if base == 2: multiplexer = 1024 elif base == 10: multiplexer = 1000 else: return None # raise error precision_string = '%.' + str(precision) + 'f' mebi_convert = True if bytes >= (multiplexer ** 4): terabytes = float(bytes / (multiplexer ** 4)) output = (precision_string % terabytes) + 'T' elif bytes >= (multiplexer ** 3): gigabytes = float(bytes / (multiplexer ** 3)) output = (precision_string % gigabytes) + 'G' elif bytes >= (multiplexer ** 2): megabytes = float(bytes / (multiplexer ** 2)) output = (precision_string % megabytes) + 'M' elif bytes >= (multiplexer ** 1): kilobytes = float(bytes / (multiplexer ** 1)) output = (precision_string % kilobytes) + 'K' else: output = (precision_string % float(bytes)) + 'B' mebi_convert = False # mebibytes and gibibytes all those weird HDD manufacturer terms if base == 10 and mebi_convert: num, base = output[:-1], output[-1] output = num + base.lower() + 'B' return output
bigcode/self-oss-instruct-sc2-concepts
import textwrap def _wrap(content, indent_level): """wrap multiple lines keeping the indentation""" indent = ' ' * indent_level wrap_opt = { 'initial_indent': indent, 'subsequent_indent': indent, } lines = [] for paragraph in content.splitlines(): if not paragraph: lines.append('') continue lines.extend(textwrap.wrap(paragraph, **wrap_opt)) return lines
bigcode/self-oss-instruct-sc2-concepts
def fwd_slash(file_path): """Ensure that all slashes are '/' Args: file_path (st|Path): The path to force '/' Returns: (str): Formatted path """ return str(file_path).replace("\\", "/")
bigcode/self-oss-instruct-sc2-concepts
def flatten_sub(sub, game_id): """Flatten the schema of a sub""" sub_id = sub[0] sub_data = sub[1] return {'game_id': game_id, 'sub_id': sub_id, 'sub_type': sub_data['type'], 'time_of_event(min)': (sub_data['t']['m'] + (sub_data['t']['s'] / 60 )), 'team_id': sub_data['team'], 'player_off': float(sub_data['offId']), 'player_on': float(sub_data['inId']) }
bigcode/self-oss-instruct-sc2-concepts
def remove_by_idxs(ls, idxs): """Remove list of indexes from a target list at the same time""" return [i for j, i in enumerate(ls) if j not in idxs]
bigcode/self-oss-instruct-sc2-concepts
def gcd(a, b): """ Find GCD(a, b).""" # GCD(a, b) = GCD(b, a mod b). while b != 0: # Calculate the remainder. remainder = a % b # Calculate GCD(b, remainder). a = b b = remainder # GCD(a, 0) is a. return a
bigcode/self-oss-instruct-sc2-concepts
def _max_factor(n, factor, max_size): """ Return the largest factor within the provided max; e.g., the most images of size n thet can fit in max_size """ if max_size is None or n * factor <= max_size: return factor return max_size // n
bigcode/self-oss-instruct-sc2-concepts
from typing import Tuple import math def get_new_coordinates(curr_x: float, curr_y: float, angle: float, speed: float) -> Tuple[float, float]: """ Works out the next x, y coordinate given the current coordinate, an angle (from the x axis in radians) and the speed (i.e distance of travel). :param curr_x: :param curr_y: :param angle: :param speed: :return: coordinate """ new_x = curr_x + speed * math.cos(angle) new_y = curr_y + speed * math.sin(angle) return new_x, new_y
bigcode/self-oss-instruct-sc2-concepts
def move(cm, from_start, from_end, insert_pos): """ Move rows from_start - from_end to insert_pos in-place. Examples -------- >>> cm = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 0, 1], [2, 3, 4, 5]]) >>> move(cm, 1, 2, 0) array([[5, 6, 4, 7], [9, 0, 8, 1], [1, 2, 0, 3], [3, 4, 2, 5]]) """ assert insert_pos < from_start or insert_pos > from_end if insert_pos > from_end: p_new = (list(range(from_end + 1, insert_pos + 1)) + list(range(from_start, from_end + 1))) else: p_new = (list(range(from_start, from_end + 1)) + list(range(insert_pos, from_start))) # print(p_new) p_old = sorted(p_new) # swap columns cm[:, p_old] = cm[:, p_new] # swap rows cm[p_old, :] = cm[p_new, :] return cm
bigcode/self-oss-instruct-sc2-concepts
def evaluate_training_result(env, agent): """ Evaluates the performance of the current DQN agent by using it to play a few episodes of the game and then calculates the average reward it gets. The higher the average reward is the better the DQN agent performs. :param env: the game environment :param agent: the DQN agent :return: average reward across episodes """ total_reward = 0.0 episodes_to_play = 100 for i in range(episodes_to_play): trajectories, _ = env.run(is_training=True) # calculate reward episode_reward = 0.0 for ts in trajectories[0]: # print( # 'State: {}, Action: {}, Reward: {}, Next State: {}, Done: {}'. # format(ts[0], ts[1], ts[2], ts[3], ts[4])) episode_reward += ts[2] total_reward += episode_reward average_reward = total_reward / episodes_to_play return average_reward
bigcode/self-oss-instruct-sc2-concepts
import re def checkBadFormat(pattern, id): """ Returns False if the format looks okay, True if it's not a match """ if (id == id): if re.match(pattern, id): return(False) else: return(True)
bigcode/self-oss-instruct-sc2-concepts
def always_https(url): """ ensures that urls are always using HTTPS :param url: An URL (as string) :type url: str :return: The passed in URL with HTTPS :rtrype: str """ if not url.startswith('http'): return url elif url.startswith('https'): return url else: https_url = url.replace('http://', 'https://') return https_url
bigcode/self-oss-instruct-sc2-concepts
def get_default_living(rows, cols): """If number of intitial living cells is not specified for the game, calculate as a function of grid size.""" return round((rows * cols) / 4)
bigcode/self-oss-instruct-sc2-concepts
def convert_temperature(val, old_scale="fahrenheit", new_scale="celsius"): """ Convert from a temperatuure scale to another one among Celsius, Kelvin and Fahrenheit. Parameters ---------- val: float or int Value of the temperature to be converted expressed in the original scale. old_scale: str Original scale from which the temperature value will be converted. Supported scales are Celsius ['Celsius', 'celsius', 'c'], Kelvin ['Kelvin', 'kelvin', 'k'] or Fahrenheit ['Fahrenheit', 'fahrenheit', 'f']. new_scale: str New scale from which the temperature value will be converted. Supported scales are Celsius ['Celsius', 'celsius', 'c'], Kelvin ['Kelvin', 'kelvin', 'k'] or Fahrenheit ['Fahrenheit', 'fahrenheit', 'f']. Raises ------- NotImplementedError if either of the scales are not one of the requested ones. Returns ------- res: float Value of the converted temperature expressed in the new scale. """ # Convert from 'old_scale' to Kelvin if old_scale.lower() in ['celsius', 'c']: temp = val + 273.15 elif old_scale.lower() in ['kelvin', 'k']: temp = val elif old_scale.lower() in ['fahrenheit', 'f']: temp = 5.0 * (val - 32) / 9.0 + 273.15 else: raise AttributeError( f'{old_scale} is unsupported. Celsius, Kelvin and Fahrenheit are supported') # and from Kelvin to 'new_scale' if new_scale.lower() in ['celsius', 'c']: result = temp - 273.15 elif new_scale.lower() in ['kelvin', 'k']: result = temp elif new_scale.lower() in ['fahrenheit', 'f']: result = (temp - 273.15) * 9.0 / 5.0 + 32 else: raise AttributeError( f'{new_scale} is unsupported. Celsius, Kelvin and Fahrenheit are supported') return result
bigcode/self-oss-instruct-sc2-concepts
def percent(num, div, prec=2): """ Returns the percentage of num/div as float Args: num (int): numerator div (int): divisor prec (None, int): rounding precision Returns: p (float) p = 100 * num/div """ num = float(num) div = float(div) if div == 0: return 0.0 # no division by zero p = round(100 * num / div, prec) return p
bigcode/self-oss-instruct-sc2-concepts
def slurp(path): """Returns the contents of the file at the given path.""" f = None try: f = open(path) return f.read() finally: if f: f.close()
bigcode/self-oss-instruct-sc2-concepts
def get_paralogs_data(paral_file): """Extract paralogous projections.""" if paral_file is None: return set() paral_proj = [] with open(paral_file, "r") as f: paral_proj = set(x.rstrip() for x in f.readlines()) return paral_proj
bigcode/self-oss-instruct-sc2-concepts
import json import requests def get_onboard_certificates(clearpass_fqdn, access_token, username): """Get all valid certificates (not revoked or expired) for user""" url = "https://{}/api/certificate".format(clearpass_fqdn) queryfilter = {'mdps_user_name': username, 'is_valid':'true'} payload = {'filter':json.dumps(queryfilter),'calculate_count':'true'} headers = {'Authorization':'Bearer {}'.format(access_token), "Content-Type": "application/json"} try: r = requests.get(url, headers=headers, params=payload) r.raise_for_status() json_response = r.json() except Exception as e: print(e) exit(1) certs = [(i['id'], i['ca_id']) for i in json_response['_embedded']['items']] return certs
bigcode/self-oss-instruct-sc2-concepts
import time def generate_stats_table(stats: dict) -> str: """Function to generate md table with questions stats. Args: stats: Stats dict. { "category": { "title" str, "cnt": int, } } Returns: Md table string. """ cnt_total = sum([v['cnt'] for v in stats.values()]) header = f"""## Questions categories *Total number of questions as of {time.strftime('%Y-%m-%d', time.gmtime())}*: **{cnt_total}** """ table_body = "\n".join([f"|[{v['title']}](questions/{k}/)|{v['cnt']}|" for k, v in stats.items()]) return f"""{header}\n |Category|Number of questions| |:-:|-:| {table_body}"""
bigcode/self-oss-instruct-sc2-concepts
def connex(data,K,L): """ Return the list of members of the connex component containing the elements originaly in L. Parameters: ----------- data: pandas dataframe data must contain at least one column named neighbors. K: list The list of members of the connex component so far. L: list The starting element, then the list of members of the connex component reached so far minus the ones that are in K. Returns: -------- K: list The junctions in the connex component containing the elements originaly in L. """ if L == []: K.sort() return K M = [] for elem in L: M += data.at[elem,'neighbors_list'] N = [] for elem2 in M: if not (elem2 in K): N += [elem2] N = list(set(N)) L = N K += L return connex(data,K,L)
bigcode/self-oss-instruct-sc2-concepts
def get_variant_id(variant_dict): """Build a variant id The variant id is a string made of CHROM_POS_REF_ALT The alt field for svs needs some massage to work downstream. Args: variant_dict (dict): A variant dictionary Returns: variant_id (str) """ chrom = variant_dict['CHROM'] pos = variant_dict['POS'] ref = variant_dict['REF'] #There are several symbols in structural variant calls that make #things hard. We will strip those symbols bad_chars = "<>[]:" alt = ''.join(c for c in variant_dict['ALT'] if c not in bad_chars) return '_'.join([chrom,pos,ref,alt])
bigcode/self-oss-instruct-sc2-concepts
import random def mcpi_samples(n): """ Compute the number of points in the unit circle out of n points. """ count = 0 for i in range(n): x, y = random.random(), random.random() if x*x + y*y <= 1: count += 1 return count
bigcode/self-oss-instruct-sc2-concepts
def batchify(batch): """Gather a batch of individual examples into one batch.""" questions = [ex['question'] for ex in batch] question_tokens = [ex['question_tokens'] for ex in batch] answers = [ex['answer'] for ex in batch] answer_tokens = [ex['answer_tokens'] for ex in batch] if 'answer_tokens' in batch[0] else None docs_truth = [ex['doc_truth'] for ex in batch] if 'doc_truth' in batch[0] else None return questions, question_tokens, answers, answer_tokens, docs_truth
bigcode/self-oss-instruct-sc2-concepts
def lv_unpack(txt): """ Deserializes a string of the length:value format :param txt: The input string :return: a list og values """ txt = txt.strip() res = [] while txt: l, v = txt.split(":", 1) res.append(v[: int(l)]) txt = v[int(l):] return res
bigcode/self-oss-instruct-sc2-concepts
def tmp_data_directory(tmp_path_factory): """Creates temporary directory and returns its path. """ return str(tmp_path_factory.mktemp("getdera"))
bigcode/self-oss-instruct-sc2-concepts
def _Mabove(dat, surf_th): """Return total gas mass above threshold density surf_th.""" surf = dat.surf M = surf.where(surf>surf_th).sum()*dat.domain['dx'][0]*dat.domain['dx'][1] return M.values[()]
bigcode/self-oss-instruct-sc2-concepts
import string import operator def getFisrtCharThatAppearsOnce(myString): """ Get the first char that appears once in the provided string. Only alphabetic chars are considered. """ myString = "".join(myString.lower().split()) charDict = {key:[0, 0] for key in string.ascii_lowercase} for pos, char in enumerate(myString): charDict[char][0] += 1 charDict[char][1] = pos charDict = {key:values for key, values in charDict.items() if values[0] == 1} sortedCharDict = sorted(charDict.items(), key=operator.itemgetter(1)) strOut = sortedCharDict[0][0] if sortedCharDict else False return strOut
bigcode/self-oss-instruct-sc2-concepts
def _strip_quotes(str_q): """ Helper function to strip off the ' or " off of a string """ if str_q[0] == str_q[-1] and str_q.startswith(("'", '"')): return str_q[1:-1] return str_q
bigcode/self-oss-instruct-sc2-concepts
def inverse2d(a): """ Returns the matrix inverse of 2x2 matrix "a". :param a: The original 2x2 matrix. :return: Its matrix inverse. """ result = [[0, 0], [0, 0]] det_a = (a[1][1] * a[0][0]) - (a[0][1] * a[1][0]) for a_row in range(len(result)): for a_col in range(len(result[a_row])): result[a_row][a_col] = round(a[a_row][a_col] * (1/det_a), 6) buffer = result[1][1] result[0][1] *= -1 result[1][0] *= -1 result[1][1] = result[0][0] result[0][0] = buffer return result
bigcode/self-oss-instruct-sc2-concepts
def number2binary(v, dynamic_padding=False, padded_length=None): """ Convert an integer value to the equivalent string of 1 and 0 characters. """ s = "" while v: s = [ "0", "1" ][v & 1] + s v >>= 1 if dynamic_padding: w = 4 while w < len(s): w <<= 1 else: w = len(s) if padded_length is None else padded_length return "0"*(w-len(s)) + s
bigcode/self-oss-instruct-sc2-concepts
def propset_dict(propset): """Turn a propset list into a dictionary PropSet is an optional attribute on ObjectContent objects that are returned by the VMware API. You can read more about these at: | http://pubs.vmware.com/vsphere-51/index.jsp | #com.vmware.wssdk.apiref.doc/ | vmodl.query.PropertyCollector.ObjectContent.html :param propset: a property "set" from ObjectContent :return: dictionary representing property set """ if propset is None: return {} return {prop.name: prop.val for prop in propset}
bigcode/self-oss-instruct-sc2-concepts
def strip_and_split(line): """ Helper function which saves a few lines of code elsewhere :param line: :return: """ line = line.strip().split() stripped_line = [subline.strip() for subline in line] return stripped_line
bigcode/self-oss-instruct-sc2-concepts
import re def first_upper(s): """Capitalizes the first letter, leaves everything else alone""" return re.sub('([a-zA-Z])', lambda x: x.groups()[0].upper(), s, 1)
bigcode/self-oss-instruct-sc2-concepts
def get_repo_information(repo_obj): """ Using the current repository, we obtain information on the repository, which includes - owner_name - repo_name Requires for there to be a remote named "origin" in the current local repo clone Returns a dict with said values. If it cannot find both, returns an empty dict """ url = repo_obj.remotes["origin"].url colon_find = url.find(":") dot_r_find = url.rfind(".") if colon_find != -1 and dot_r_find != -1: url = url[colon_find + 1 : dot_r_find] results = url.split("/") return {"owner_name": results[0], "repo_name": results[1]} return dict()
bigcode/self-oss-instruct-sc2-concepts
import itertools def to_combinations(list_of_objs: list): """ Create array of all combinations of a list of length n of the shape (1, 2, ..., n) :param list_of_objs: :return: Combinations """ combinations = [] for i in range(2, len(list_of_objs)): combinations.extend(itertools.combinations(list_of_objs, i)) return combinations
bigcode/self-oss-instruct-sc2-concepts
def fmt(x, pos): """ A utility function to improve the formatting of plot labels """ a, b = '{:.2e}'.format(x).split('e') b = int(b) return r'${} \times 10^{{{}}}$'.format(a, b)
bigcode/self-oss-instruct-sc2-concepts
def is_binary(plist_path): """Checks if a plist is a binary or not.""" result = False with open(plist_path, 'rb') as _f: for _block in _f: if b'\0' in _block: result = True break return result
bigcode/self-oss-instruct-sc2-concepts
def validate_ppoi_tuple(value): """ Validates that a tuple (`value`)... ...has a len of exactly 2 ...both values are floats/ints that are greater-than-or-equal-to 0 AND less-than-or-equal-to 1 """ valid = True while valid is True: if len(value) == 2 and isinstance(value, tuple): for x in value: if x >= 0 and x <= 1: pass else: valid = False break else: valid = False return valid
bigcode/self-oss-instruct-sc2-concepts
def dec2dec(dec): """ Convert sexegessimal RA string into a float in degrees. Parameters ---------- dec : str A string separated representing the Dec. Expected format is `[+- ]hh:mm[:ss.s]` Colons can be replaced with any whit space character. Returns ------- dec : float The Dec in degrees. """ d = dec.replace(':', ' ').split() if len(d) == 2: d.append('0.0') if d[0].startswith('-') or float(d[0]) < 0: return float(d[0]) - float(d[1]) / 60.0 - float(d[2]) / 3600.0 return float(d[0]) + float(d[1]) / 60.0 + float(d[2]) / 3600.0
bigcode/self-oss-instruct-sc2-concepts
import torch def cosine_similarity(x1, x2): """Calculates cosine similarity of two tensor.""" dist = torch.sum(torch.multiply(x1, x2), dim=-1) dist = dist / (torch.linalg.norm(x1, dim=-1) * torch.linalg.norm(x2, dim=-1)) return dist
bigcode/self-oss-instruct-sc2-concepts
from typing import List def generate_board( row: int, column: int, ) -> List[List[str]]: """ Generate a new board in a row * column manner. Parameters ---------- row: int A number that indicate how many row should be generated. column: int A numebr that indicated how many column should be generated. Returns ------- board: List[List[str]] 2D array containing all the game detail, including column header, row header and placed buildings. """ board = [] # * Preparation of the column header * # ordinal = ord('A') header_list = [' '] + [chr(ordinal + i) for i in range(column)] board.append(header_list) # * Preparation for each row * # for i in range(1, row + 1): row_list = [str(i)] + [' ' for _ in range(column)] board.append(row_list) return board
bigcode/self-oss-instruct-sc2-concepts
def cpd_int(p, r, t, n=12): """ Calculate compound interest :param p: (float) - principal :param r: (float) - annual interest rate :param t: (int) - time(years) :param n: (int) - number of times interest is compounded each year :return a: (float) - compound interest """ a = p * (1 + r / n) ** (n * t) return a
bigcode/self-oss-instruct-sc2-concepts
import re def clean_newline(line): """Cleans string so formatting does not cross lines when joined with \\n. Just looks for unpaired '`' characters, other formatting characters do not seem to be joined across newlines. For reference, discord uses: https://github.com/Khan/simple-markdown/blob/master/simple-markdown.js """ match = None for match1 in re.finditer(r'(`+)\s*([\s\S]*?[^`])\s*\1(?!`)', line): match = match1 idx = match.end() if match else 0 line = line[:idx] + line[idx:].replace('`', '\`') return line
bigcode/self-oss-instruct-sc2-concepts
def lu_decomposition(matrix_in, q=0): """ LU-Factorization method using Doolittle's Method for solution of linear systems. Decomposes the matrix :math:`A` such that :math:`A = LU`. The input matrix is represented by a list or a tuple. If the input matrix is 1-dimensional, i.e. a list or tuple of integers and/or floats, then the second function argument ``q`` must be bigger than zero. If the input matrix is 2-dimensional, i.e. list of lists of integers and/or floats, then there is no need to input ``q`` as it will be automatically computed. :param matrix_in: Input matrix (must be a square matrix) :type matrix_in: list, tuple :param q: matrix size (not used if the input matrix is 2-dimensional) :type q: int :return: a tuple containing matrices (L,U) :rtype: tuple """ if not isinstance(q, int): raise TypeError("Matrix size must be an integer") if q < 0: raise ValueError("Matrix size should be bigger than zero") # Flag for converting return values into 1-dimensional list convert_res = False if q > 0: # Check if the 1-dimensional input matrix is a square matrix if len(matrix_in) != q ** 2: raise ValueError("The input matrix must be a square matrix") # Convert 1-dimensional matrix to 2-dimensional matrix_a = [[0.0 for _ in range(q)] for _ in range(q)] for i in range(0, q): for j in range(0, q): matrix_a[i][j] = matrix_in[j + (q * i)] # The input is 1-dimensional, so the return values should be convert_res = True else: matrix_a = matrix_in # Check if the 2-dimensional input matrix is a square matrix q = len(matrix_a) for idx, m_a in enumerate(matrix_a): if len(m_a) != q: raise ValueError("The input must be a square matrix. " + "Row " + str(idx + 1) + " has a size of " + str(len(m_a)) + ".") # Initialize L and U matrices matrix_u = [[0.0 for _ in range(q)] for _ in range(q)] matrix_l = [[0.0 for _ in range(q)] for _ in range(q)] # Doolittle Method for i in range(0, q): for k in range(i, q): # Upper triangular (U) matrix matrix_u[i][k] = float(matrix_a[i][k] - sum([matrix_l[i][j] * matrix_u[j][k] for j in range(0, i)])) # Lower triangular (L) matrix if i == k: matrix_l[i][i] = 1.0 else: matrix_l[k][i] = float(matrix_a[k][i] - sum([matrix_l[k][j] * matrix_u[j][i] for j in range(0, i)])) matrix_l[k][i] /= float(matrix_u[i][i]) # Prepare and return the L and U matrices if convert_res: m_u = [] m_l = [] for upper, lower in zip(matrix_u, matrix_l): m_u.extend(upper) m_l.extend(lower) return m_l, m_u return matrix_l, matrix_u
bigcode/self-oss-instruct-sc2-concepts
def count_user_type(data_list): """ Conta os tipos de usuário nos registros de uma lista. Argumentos: data_list: Lista de registros contendo o tipo do usuário em uma das colunas. Retorna: Número de usuários 'Subscriber' e 'Customer', nesta ordem. """ subscriber = 0 customer = 0 for sample in data_list: if sample[-3] == 'Subscriber': subscriber += 1 elif sample[-3] == 'Customer': customer += 1 return [subscriber, customer]
bigcode/self-oss-instruct-sc2-concepts
def index_api_data(parsed_json, id_field): """Transform a list of dicts into a dict indexed by one of their fields. >>> index_api_data([{'id': 'eggs', 'val1': 42, 'foo': True}, ... {'id': 'spam', 'val1': 1, 'foo': True}], 'id') {'eggs': {'val1': 42, 'foo': True}, 'spam': {'val1': 1, 'foo': True}} >>> index_api_data([{'id': 'eggs', 'val1': 42, 'foo': True}, ... {'id': 'spam', 'val1': 1, 'foo': True}], 'val1') {1: {'foo': True, 'id': 'spam'}, 42: {'foo': True, 'id': 'eggs'}} """ transformed_dict = {} for attr in parsed_json: # make a copy of the attr dict # remove id field: if not id_field in attr: raise RuntimeError("Field '{}' not found in json object".format( id_field)) id_val = attr[id_field] if id_val in transformed_dict: raise RuntimeError("Identifier '{}' found more than once in json " "object".format(id_val)) # make a copy of the sub-dictionary without the id field attr_dict = dict(attr) del attr_dict[id_field] transformed_dict[id_val] = attr_dict return transformed_dict
bigcode/self-oss-instruct-sc2-concepts
def client_factory(client_cls, dispatcher, settings): """Shared logic to instantiate a configured torque client utility.""" torque_url = settings.get('torque.url') torque_api_key = settings.get('torque.api_key') return client_cls(dispatcher, torque_url, torque_api_key)
bigcode/self-oss-instruct-sc2-concepts
from typing import Tuple import torch from typing import List def encode_supervisions( supervisions: dict, subsampling_factor: int ) -> Tuple[torch.Tensor, List[str]]: """ Encodes Lhotse's ``batch["supervisions"]`` dict into a pair of torch Tensor, and a list of transcription strings. The supervision tensor has shape ``(batch_size, 3)``. Its second dimension contains information about sequence index [0], start frames [1] and num frames [2]. The batch items might become re-ordered during this operation -- the returned tensor and list of strings are guaranteed to be consistent with each other. """ supervision_segments = torch.stack( ( supervisions["sequence_idx"], supervisions["start_frame"] // subsampling_factor, supervisions["num_frames"] // subsampling_factor, ), 1, ).to(torch.int32) indices = torch.argsort(supervision_segments[:, 2], descending=True) supervision_segments = supervision_segments[indices] texts = supervisions["text"] texts = [texts[idx] for idx in indices] return supervision_segments, texts
bigcode/self-oss-instruct-sc2-concepts
from bs4 import BeautifulSoup def fetch_links_from_html(html_doc): """ Given a blob of HTML, this function returns a list of PDF links """ soup = BeautifulSoup(html_doc) pdf_attachments = [] for link in soup.findAll('a'): value = link.get('href') if "http" in value: pdf_url = value else: pdf_url = "https://www.gov.uk" + value pdf_attachments.append(pdf_url) return pdf_attachments
bigcode/self-oss-instruct-sc2-concepts
import torch def depth_map_to_3d_torch(depth, cam_K, cam_W): """Derive 3D locations of each pixel of a depth map. Args: depth (torch.FloatTensor): tensor of size B x 1 x N x M with depth at every pixel cam_K (torch.FloatTensor): tensor of size B x 3 x 4 representing camera matrices cam_W (torch.FloatTensor): tensor of size B x 3 x 4 representing world matrices Returns: loc3d (torch.FloatTensor): tensor of size B x 3 x N x M representing color at given 3d locations mask (torch.FloatTensor): tensor of size B x 1 x N x M with a binary mask if the given pixel is present or not """ depth = torch.from_numpy(depth) cam_K = torch.from_numpy(cam_K) cam_W = torch.from_numpy(cam_W) N, M = depth.size() device = depth.device # Turn depth around. This also avoids problems with inplace operations depth = -depth.permute(1,0) zero_one_row = torch.tensor([[0., 0., 0., 1.]]) zero_one_row = zero_one_row.expand(1, 4).to(device) # add row to world mat cam_W = torch.cat((cam_W, zero_one_row), dim=0) # clean depth image for mask # upperlimit = 1.e+10 upperlimit = float("Inf") mask = (depth.abs() != upperlimit).float() depth[depth == upperlimit] = 0 depth[depth == -1*upperlimit] = 0 # 4d array to 2d array k=N*M d = depth.reshape(1,N * M) # create pixel location tensor px, py = torch.meshgrid([torch.arange(0, N), torch.arange(0, M)]) px, py = px.to(device), py.to(device) p = torch.cat(( px.expand(px.size(0), px.size(1)), (M - py).expand(py.size(0), py.size(1)) ), dim=0) p = p.reshape(2, py.size(0) * py.size(1)) p = (p.float() / M * 2) # create terms of mapping equation x = P^-1 * d*(qp - b) P = cam_K[:2, :2].float().to(device) q = cam_K[2:3, 2:3].float().to(device) b = cam_K[:2, 2:3].expand(2, d.size(1)).to(device) Inv_P = torch.inverse(P).to(device) rightside = (p.float() * q.float() - b.float()) * d.float() x_xy = torch.matmul(Inv_P, rightside) # add depth and ones to location in world coord system x_world = torch.cat((x_xy, d, torch.ones_like(d)), dim=0) # derive loactoion in object coord via loc3d = W^-1 * x_world Inv_W = torch.inverse(cam_W) Inv_W_exp = Inv_W.expand(4, 4) loc3d = torch.matmul(Inv_W_exp, x_world.double()) loc3d = loc3d.reshape(4, N, M) loc3d = loc3d[:3,:,:].to(device) mask = mask.to(device) loc3d = loc3d.view(3, N * M) return loc3d, mask
bigcode/self-oss-instruct-sc2-concepts
from typing import Iterable def flatten_iterable(its: Iterable, deep: bool = False) -> list: """ flatten instance of Iterable to list Notes: 1. except of str, won't flatten 'abc' to 'a', 'b', 'c' demo: [[[1], [2], [3]], 4] if deep is True: flatten to [1, 2, 3, 4] if deep is False, flatten to [[1], [2], [3], 4]. """ res = [] for it in its: if isinstance(it, str): res.append(it) elif isinstance(it, Iterable): if deep: res += flatten_iterable(it, True) else: res.extend(it) else: res.append(it) return res
bigcode/self-oss-instruct-sc2-concepts
def find_frequency_bandwidth(frequency, simulation_parameters): """ Finds the correct bandwidth for a specific frequency from the simulation parameters. """ simulation_parameter = 'channel_bandwidth_{}'.format(frequency) if simulation_parameter not in simulation_parameters.keys(): KeyError('{} not specified in simulation_parameters'.format(frequency)) bandwidth = simulation_parameters[simulation_parameter] return bandwidth
bigcode/self-oss-instruct-sc2-concepts
def isLoopClockwise(loop): """Gets if a loop of line segments is clockwise Parameters ---------- loop : List or np array of shape (-1, 2, 2) -1 number of line segments, [startPoint, endPoint], [x,y] Returns ------- bool Note ------- https://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-points-are-in-clockwise-order """ s = 0 for i in loop: s += (i[1][0] - i[0][0]) * (i[1][1] + i[0][1]) return s > 0
bigcode/self-oss-instruct-sc2-concepts
def is_smile_inside_face(smile_coords, face_coords): """Function to check if the smile detected is inside a face or not Args: smile_coords (list): list of smaile coordinates of form [x, y, (x+w), (y+h)] face_coords (list): list of face coordinates of form [x, y, (x+w), (y+h)] Returns: bool: True if smile is inside of face bounding box, else False """ sx1, sy1, sx2, sy2 = smile_coords fx1, fy1, fx2, fy2 = face_coords # If top-left plate corner is inside the face if fx1 < sx1 and fy1 < sy1: # If bottom-right plate corner is inside the face if sx2 < fx2 and sy2 < fy2: # The entire smile is inside the face. return True else: # Some part of the smile is outside the face. return False else: # whole smile is outside the face. return False
bigcode/self-oss-instruct-sc2-concepts
def get_max_drawdown_from_series(r): """Risk Analysis from asset value cumprod way Parameters ---------- r : pandas.Series daily return series """ # mdd = ((r.cumsum() - r.cumsum().cummax()) / (1 + r.cumsum().cummax())).min() mdd = (((1 + r).cumprod() - (1 + r).cumprod().cummax()) / ((1 + r).cumprod().cummax())).min() return mdd
bigcode/self-oss-instruct-sc2-concepts
import math def dist_to_line(line, point): """ Finds a point's distance from a line of infinite length. To find a point's distance from a line segment, use dist_to_line_seg instead. line: ((lx0,ly0), (lx1,ly1)) Two points on the line point: (px, py) The point to find the distance from returns: the distance between the point and the line """ x1,y1 = line[0] x2,y2 = line[1] x3,y3 = point # where on line the perpendicular is u = ( ((x3-x1)*(x2-x1) + (y3-y1)*(y2-y1)) / (math.pow(x1-x2,2) + math.pow(y1-y2,2)) ) # intersection point x = x1 + u*(x2-x1) y = y1 + u*(y2-y1) dist = math.sqrt(math.pow(x-x3,2)+math.pow(y-y3,2)) return dist
bigcode/self-oss-instruct-sc2-concepts
def personal_best(scores): """ Return the highest score in scores. param: list of scores return: highest score in scores """ return max(scores)
bigcode/self-oss-instruct-sc2-concepts
def identify_value_block(block: dict) -> str: """Given a key block, find the ID of the corresponding value block.""" return [x for x in block["Relationships"] if x["Type"] == "VALUE"][0]["Ids"][0]
bigcode/self-oss-instruct-sc2-concepts
import logging def mergeDoc(existing_doc, new_doc): """ existing_doc is merged with new_doc. Returns true/false if existing_doc is modified. """ records = existing_doc.setdefault("records", []) if 'records' not in new_doc: return False isModified = False for new_record in new_doc['records']: if new_record not in records: records.append(new_record) isModified = True logging.info("# merged records %d " % len(records)) # Merge images. images = existing_doc.setdefault("images", []) for new_image in new_doc['images']: if new_image not in images: images.append(new_image) isModified = True logging.info("# merged images %d " % len(images)) # Merge sources. sources = existing_doc.setdefault("sources", []) for new_source in new_doc['source']: if new_source not in sources: sources.append(new_source) isModified = True logging.info("# merged sources %d " % len(sources)) return isModified
bigcode/self-oss-instruct-sc2-concepts
def convert_mip_type_to_python_type(mip_type: str): """ Converts MIP's types to the relative python class. The "MIP" type that this method is expecting is related to the "sql_type" enumerations contained in the CDEsMetadata. """ type_mapping = { "int": int, "real": float, "text": str, } if mip_type not in type_mapping.keys(): raise KeyError( f"MIP type '{mip_type}' cannot be converted to a python class type." ) return type_mapping.get(mip_type)
bigcode/self-oss-instruct-sc2-concepts
from typing import Sequence from typing import Tuple import math def quaternion_to_euler(quat: Sequence[float]) -> Tuple[float,float,float]: """ Convert WXYZ quaternion to XYZ euler angles, using the same method as MikuMikuDance. Massive thanks and credit to "Isometric" for helping me discover the transformation method used in mmd!!!! :param quat: 4x float, W X Y Z quaternion :return: 3x float, X Y Z angle in degrees """ w, x, y, z = quat # pitch (y-axis rotation) sinr_cosp = 2 * ((w * y) + (x * z)) cosr_cosp = 1 - (2 * ((x ** 2) + (y ** 2))) pitch = -math.atan2(sinr_cosp, cosr_cosp) # yaw (z-axis rotation) siny_cosp = 2 * ((-w * z) - (x * y)) cosy_cosp = 1 - (2 * ((x ** 2) + (z ** 2))) yaw = math.atan2(siny_cosp, cosy_cosp) # roll (x-axis rotation) sinp = 2 * ((z * y) - (w * x)) if sinp >= 1.0: roll = -math.pi / 2 # use 90 degrees if out of range elif sinp <= -1.0: roll = math.pi / 2 else: roll = -math.asin(sinp) # fixing the x rotation, part 1 if x ** 2 > 0.5 or w < 0: if x < 0: roll = -math.pi - roll else: roll = math.pi * math.copysign(1, w) - roll # fixing the x rotation, part 2 if roll > (math.pi / 2): roll = math.pi - roll elif roll < -(math.pi / 2): roll = -math.pi - roll roll = math.degrees(roll) pitch = math.degrees(pitch) yaw = math.degrees(yaw) return roll, pitch, yaw
bigcode/self-oss-instruct-sc2-concepts
import jinja2 def load_jinja( path, file, vrf_name, bandwidth, packet_size, ref_packet_size, time_interval, ipp4_bps, ipp2_bw_percent, ipp0_bw_percent, interface, ): """Use Jinja templates to build the device configuration Args: device (`obj`): Device object vrf_name (`str`): Vrf name to be used in configuration bandwidth (`int`): In bps, bandwidth for traffic flow packet_size (`int`): Config packet size ref_packet_size (`int`): Refrenced packet size time_interval (`float`): In seconds, used for calculating bc ipp4_bps (`int`): In bps, bandwidth for IPP4 traffic ipp2_bw_percent (`int`): In percents, bandwidth for IPP2 traffic ipp0_bw_percent (`int`): In percents, bandwidth for IPP0 traffic interface (`str`): Where to apply the configured policies Returns: out """ env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath=path)) template = env.get_template(file) out = template.render( vrf_name=vrf_name, bandwidth=bandwidth, packet_size=packet_size, ref_packet_size=ref_packet_size, time_interval=time_interval, ipp4_bps=ipp4_bps, ipp2_bw_percent=ipp2_bw_percent, ipp0_bw_percent=ipp0_bw_percent, interface=interface, ) return out
bigcode/self-oss-instruct-sc2-concepts
async def ladders(database, platform_id): """Get ladders for a platform.""" query = "select id as value, name as label from ladders where platform_id=:platform_id" return list(map(dict, await database.fetch_all(query, values={'platform_id': platform_id})))
bigcode/self-oss-instruct-sc2-concepts
import hashlib def get_unique_str(seed: str) -> str: """Generate md5 unique sting hash given init_string.""" return hashlib.md5(seed.encode("utf-8")).hexdigest()
bigcode/self-oss-instruct-sc2-concepts
from typing import List def get_characters_from_file(file_path: str) -> List[str]: """ Opens the specified file and retrieves a list of characters. Assuming each character is in one line. Characters can have special characters including a space character. Args: file_path (str): path to the file Returns: List[str]: List of character names """ characters = [] with open(file_path, 'r') as characters_file: characters = [ # Remove leading/trailing spaces character_name.strip() for character_name in characters_file.readlines() # It may contain empty lines or comments if character_name.strip() and character_name[0] != '#' ] return characters
bigcode/self-oss-instruct-sc2-concepts
def _get_edge(layer_idx_start, layer_idx_end): """ Returns a tuple which is an edge. """ return (str(layer_idx_start), str(layer_idx_end))
bigcode/self-oss-instruct-sc2-concepts
def _fasta_slice(fasta, seqid, start, stop, strand): """ Return slice of fasta, given (seqid, start, stop, strand) """ _strand = 1 if strand == '+' else -1 return fasta.sequence({'chr': seqid, 'start': start, 'stop': stop, \ 'strand': _strand})
bigcode/self-oss-instruct-sc2-concepts
def set_coordinates(atoms, V, title="", decimals=8): """ Print coordinates V with corresponding atoms to stdout in XYZ format. Parameters ---------- atoms : list List of atomic types V : array (N,3) matrix of atomic coordinates title : string (optional) Title of molecule decimals : int (optional) number of decimals for the coordinates Return ------ output : str Molecule in XYZ format """ N, D = V.shape fmt = "{:2s}" + (" {:15."+str(decimals)+"f}")*3 out = list() out += [str(N)] out += [title] for i in range(N): atom = atoms[i] atom = atom[0].upper() + atom[1:] out += [fmt.format(atom, V[i, 0], V[i, 1], V[i, 2])] return "\n".join(out)
bigcode/self-oss-instruct-sc2-concepts
from typing import Dict def strip_leading_underscores_from_keys(d: Dict) -> Dict: """ Clones a dictionary, removing leading underscores from key names. Raises ``ValueError`` if this causes an attribute conflict. """ newdict = {} for k, v in d.items(): if k.startswith('_'): k = k[1:] if k in newdict: raise ValueError(f"Attribute conflict: _{k}, {k}") newdict[k] = v return newdict
bigcode/self-oss-instruct-sc2-concepts
from typing import List from typing import Optional from typing import Tuple def split_by(items: List[str], separator: Optional[str] = None) -> Tuple[List[str], List[str]]: """If the separator is present in the list, returns a 2-tuple of - the items before the separator, - all items after the separator. If the separator isn't present, returns a tuple of - (the original list, []) """ if separator is None: separator = '--' try: idx = items.index(separator) return items[0:idx], items[idx + 1:] except ValueError: return (items, [])
bigcode/self-oss-instruct-sc2-concepts
def value_of_ace(hand_value): """ :param hand_value: int - current hand value. :return: int - value of the upcoming ace card (either 1 or 11). """ if hand_value + 11 > 21: value = 1 else: value = 11 return value
bigcode/self-oss-instruct-sc2-concepts
def _parse_ports(ports_text): """ Handle the case where the entry represents a range of ports. Parameters ---------- ports_text: str The text of the given port table entry. Returns ------- tuple A tuple of all ports the text represents. """ ports = ports_text.split('-') try: if len(ports) == 2: ports = tuple(range(int(ports[0]), int(ports[1]) + 1)) else: ports = (int(ports[0]),) except ValueError: return () return ports
bigcode/self-oss-instruct-sc2-concepts
import socket import struct def long2ip(l): """Convert big-endian long representation of IP address to string """ return socket.inet_ntoa(struct.pack("!L", l))
bigcode/self-oss-instruct-sc2-concepts
from bs4 import BeautifulSoup def get_soup(html): """ Get the Beautiful Soup tree from HTML. """ # return BeautifulSoup(req.content, "html.parser") # return BeautifulSoup(req.text, "html5lib") # Haven't tested this yet return BeautifulSoup(html, "html.parser")
bigcode/self-oss-instruct-sc2-concepts