content
stringlengths
39
9.28k
sha1
stringlengths
40
40
id
int64
8
710k
def dedupe_list(l): """Remove duplicates from a list preserving the order. We might be tempted to use the list(set(l)) idiom, but it doesn't preserve the order, which hinders testability.""" result = [] for el in l: if el not in result: result.append(el) return result
272fd6201fef908dd6a36ff01ef4087bc159f7ba
331,369
def convert_to_html(model): """ Takes a model instance and produces an HTML table that represents the results in a nice way for us to look at. Parameters ---------- model : dict Dictionary of layers and their properties as a list in the form of [Vp, Vs, rho, thickness, top] Returns...
f5ecc9677f7bfc0692e7f58b3dc6ce4f1955ee44
653,002
def isSpaceFree(board, move): """Return True if the move is free on the provided board.""" return board[move] == ' '
8524c869fc8d014de229633748be0da3c9180713
181,042
def fixtureid_cpcs(fixture_value): """ Return a fixture ID to be used by pytest, for fixtures returning a list of CPCs. Parameters: * fixture_value (list of zhmcclient.Cpc): The list of CPCs. """ cpc_list = fixture_value assert isinstance(cpc_list, list) cpcs_str = ','.join([cpc.n...
864d76898452e74841497253e7799e763aac3c0b
122,498
def get_renaming(mappers, year): """Get original to final column namings.""" renamers = {} for code, attr in mappers.items(): renamers[code] = attr['df_name'] return renamers
33197b5c748b3ecc43783d5f1f3a3b5a071d3a4e
705,784
def convert_to_lower(x): """Convert string to lower case :param x: string :return: string """ x = str(x) x = str.lower(x) return x
310a9603a572830c0c5b9fcee573abe88a466d7f
410,587
def calculate_max_series(series): """ Calculate the maximum of a series :param series: list of values where we want to know the maximum of. :return max(series): highest value in the series """ assert type(series) is list and len(series) != 0 return max(series)
de8ba05f580e2de4ca6e0737e5db61352bf1e266
650,919
import re def is_image(url: str) -> bool: """If a image is a valid image or not. Examples ________ >>> url = "https://i.imgur.com/a/MzgxNjI.jpg" >>> is_image(url) True >>> url = "https://www.google.com" >>> is_image(url) False Parameters ---------- url : str T...
f1978247c8262ce9c5cb842d300fefb1b9067f99
524,454
import torch def split_tensor_pos(tensor): """ Splits tensor into positive and negative terms """ zeros_like = torch.zeros_like(tensor) pos_tensor = torch.max(tensor, zeros_like) neg_tensor = torch.min(tensor, zeros_like) return pos_tensor, neg_tensor
e3f1b013949b38a88185aeaaec45cb60891753a2
640,685
def register_sql(spark, files, schema=None, sep=None, table_name="table", return_count=False): """ Register a list of files as a SQL temporary view. parameters: - files is overloaded: can be one file path or list of file paths. - spark: pyspark.sql.SparkSession - table_name: this is how we will...
f7f79211c2ad0dc8591dfdca7823e595f86ef035
630,642
def curvature(x, dfunc, d2func, *args, **kwargs): """ Curvature of function: `f''/(1+f'^2)^3/2` Parameters ---------- x : array_like Independent variable to evalute curvature dfunc : callable Function giving first derivative of function f: f', to be called `dfunc(x, *ar...
892629c780a142a0c974fd9d859a28fe5a66a96e
247,509
def _fits_section_header(section_name): """ Blank fits header cards for a section header. As in drizzle, one blank line, a line with the section name as a comment, then one more blank. """ return [('', '', ''), ('', '/ ' + section_name, ''), ('', '', '')]
4d7ce0175e40706505bed15e2fc1f5d4f2d27dac
87,464
def readme_contents_section_exists(path: str) -> bool: """ Given a README.md path, checks to see if there is a Contents section """ try: with open(path, 'r') as f: return '## Contents' in f.read() except FileNotFoundError: return False
7353613b5e74f3343c8214073b00919a32a692b0
677,558
def format_query(query): """Replaces spaces for %20""" return "%20".join(query.split())
8512752574e34ae72e5bd6a6da68283a710c347d
345,004
def hdx_acap_formatter(raw): """Formats raw pandas.DataFrame - Drops 'pcode', 'admin_level_name', 'alternative source' columns - Orders Columns Arguments: raw (pandas.DataFrame): from hdx_acap_connector Returns: pandas.DataFrame """ data = raw.copy() data.columns = [column.low...
2372f7b340ed6e7f25283baeb457cabbfef930ca
494,018
def string2bool(allegedstring): """ Tries to return a boolean from a string input if possible, else it just returns the original item, allegedly a string. """ if allegedstring.lower().startswith('t'): return True elif allegedstring.lower().startswith('f'): return False elif a...
a5a8598f0d0c2e79a27f7ae9a4555894b2ccf9ac
62,765
import torch def build_design_tensor(item_thetas, individual_assignment): """ Given a sequence of angles representing objects on S^1, and assignments of those objects to individuals, creates a design tensor of the appropriate dimensions. :param torch.Tensor item_thetas: a tensor of dimension batc...
16dfa55a98b04e67ba68e6b50d4e0ff53a38d5e3
580,403
def _calc_num_simulated_obs_meeting_a_condition(simulated_y, condition): """ Calulates the number of simulated observations where `y == 1` and `condition == True`. Parameters ---------- simulated_y : 1D ndarray of ints in `{0, 1}`. Denotes the simulated outcomes. condition : 1D ndar...
f64f8467a641a9e8e18748bad9f43d9f6b4995db
341,196
import pickle def load_model(path): """Loads the saved BaseModel and returns the reconstructed object. Parameters ---------- path: string Location of the saved model. Returns ------- model: BaseModel Saved BaseModel/child class instance.""" with open(path, 'rb') as f:...
4ccfbefcb1813152e4397901f240be8755091fee
158,434
import hashlib def file_hash(file_path): """ Compute the md5 hash of a file """ digest = hashlib.md5() with open(file_path, 'rb') as fp: for chunk in iter(lambda: fp.read(4096), b''): digest.update(chunk) return digest.hexdigest()
490501eac96ebe14f07a36365985ca1b1bed9215
132,461
import mimetypes def guess_mimetype(path): """Define a mimetypes.guess_type() wrapper that returns just the type and also handles webp. """ if path.endswith('.webp'): return 'image/webp' return mimetypes.guess_type(path)[0]
a6f42de3f8f550119f20e496cd4f70eeb7a49239
523,284
from typing import Any def success_fixture(mocker: Any) -> None: """Return fixture for success method used by status endpoint.""" mocked_success = mocker.patch('medtagger.api.core.business.success') mocked_success.return_value = {'success': True} return mocked_success
d4fa3f54e4602dede36b159838c9f6feee4e4836
515,698
def ranktokelv(rankine): """ This function converts Rankine to kelvin, with Rankine as parameter.""" kelvin = rankine / 1.8 return kelvin
c604ea03047afb8dfffc96ef580b1520c7ae52e1
511,353
def f(x): """ Target function for the perceptron learning algorithm. I've chosen the NAND gate, but any function is okay, with the caveat that the algorithm won't terminate if ``f`` cannot be computed by a perceptron.""" return int(not (x[0] and x[1]))
2713e720a09b707bab1ad7678d5d20d636926f13
394,895
def get_passed_objects(token): """ Returns a tuple (which you can pass as args) of passed objects """ contents = token.split_contents() return tuple(contents[1:])
4e609ae4f0edc01e5635ab397d4bec5a2f91e786
147,842
def select_trajectories(panel, items=None): """Select trajectories of certain items in a panel. Parameters ---------- panel : pandas Panel A panel of trajectories, with time on the major axis, items denoting different trajectories, and features on the minor axis. items : iterable o...
5b7b9d408bd1dcd3d213282f4bb1f938a93d1ef6
163,280
def get_env(fuzzing_language, build): """Returns an environment for building. The environment is returned as a list and is suitable for use as the "env" parameter in a GCB build step. The environment variables are based on the values of |fuzzing_language| and |build.""" env_dict = { 'FUZZING_LANGUAGE': ...
d7681862b118731557dc79dcf51f3ee12d387568
238,410
import logging def MergeDictionaryValues(old_dict, new_dict): """Attempts to merge the given dictionaries. Warns if a key exists with different values in both dictionaries. In this case, the new_dict value trumps the previous value. Args: old_dict: Existing dictionary. new_dict: New dictionary. R...
2836386f78363a1a0cede442ac0d6e81e3cd71e4
695,263
def chunk_data(data, size): """Creates a list of chunks of the specified `size`. Args: data (list): self-explanatory. size (int): desired size of each chunk, the last one can be <= `size`. Returns: coll (list): lists of lists. """ coll = [] start_indx = 0 while star...
3d7547d747c308e575eca4bb21c2a1316f8fbdc8
292,121
import random def encourage() -> str: """Returns a random encouragement message""" return random.choice([ "Please try again.", "Give it another try.", "Let's try it again.", "Try it again; next time's the charm!", "Don't give up now, try it one more time.", "But no need to fret...
55928dcad07a871b5fc214f1fa631bc359b61dcf
265,203
def generalized_fibonacci_sequence(n, p): """Compute the generalized Fibonacci sequence. Args: n: An `int`. The number of terms to compute. Must be >= 2. p: An `int`. The number of the generalized sequence. Must be >= 1. If `p` is 1, the sequence is the standard Fibonacci sequence. Returns: A ...
2b7b2c5e2f7102166deb4f81b37e950fa900a17e
642,605
def get_weighted_mean(items=[{}]): """ When scores are grouped we multiply first each score x by its frequency f and divide the sum of products xf by total frequency N weighted_mean = summation(x*f) / n x = score f = frequency n = total frequency """ total_freq = 0 total ...
9796b704aab21a53f582d7f0602784cec27bd145
331,524
def is_transactional_goal(domain_goal: dict): """Checks if a domain goal is transactional. For MultiWOZ this is equivalent to the presence of a ``'book'`` field in the domain goal. Parameters ---------- domain_goal Domain goal containing constraints and information requests. See `compute_ig...
a10f143db07e396e9e85050010ee873349110e1e
170,403
def clean(features): """ Sanitize the dataset for analysis by centering the means to 0 and creating unit variance """ means = features.mean(axis=0) sd = features.std(axis=0) cleaned = features for j in range(features.shape[1]): cleaned.T[j] = [(i-means[j])/sd[j] for i in cleaned.T[j]...
06c025af9d2ce587cad1effacf6286505f1f71c5
506,620
def unreduceConfig(cls, stream): """Create a `~lsst.pex.config.Config` from a stream. Parameters ---------- cls : `lsst.pex.config.Config`-type A `lsst.pex.config.Config` type (not an instance) that is instantiated with configurations in the ``stream``. stream : file-like object, `s...
ae65d6930b34bb32866c23417f4e0a65d6cec06b
203,200
def remainder(num1: int, num2: int) -> int: """Find the remainder from num1 and num2.""" if all((num1 > 0, num2 >= 1,)): return num1 % num2 raise ValueError("Check number inputs. ")
3ac3e106cfb315ac53a38b839cc28002ae97e00b
155,566
def merge(a, b): """ Recursively merges hash b into a so that keys from b take precedence over keys from a See: https://github.com/ansible/ansible/blob/6787fc70a643fb6e2bdd2c6a6202072d21db72ef/lib/ansible/utils/vars.py """ # if a is empty or equal to b, return b if a == {} or a == b: re...
478c7ef98d793526b157ec353b740a9e1d21c011
468,935
def create_client_from_parsed_globals(session, service_name, parsed_globals, overrides=None): """Creates a service client, taking parsed_globals into account Any values specified in overrides will override the returned dict. Note that this override occurs after 'region...
2e3dc30f5b212ea4a812de33069025450b50ba2b
470,065
import json def error(status, message): """ Return an error message """ return { "statusCode": status, "body": json.dumps({"message": message}) }
73b8f78f8858736998f2db24ad61dbd32764e0df
321,436
def key_strings_from_class(klass): """ Returns the set of visible fields from a class, presumably a BaseModel subclass. """ return set(x for x in klass.__fields__.keys())
57b5858f9f16ff44a60e62f3f40b7de67af74107
256,983
import re def remove_color_tags(s): """Clean string and remove color tags from string""" return re.sub("<[^>]*>", "", s)
389072bf8a2ad7dbf28ed985e0504dbe6cda04eb
362,382
def types_args_one_port(port, formatter): """ Extract from a port: 1. a list of "type arg"s 2. a list of args :param port: :param formatter: a string formatter with {0} and/or {1} where {0} is the port name, and {2} is an arg ID. :return: """ types_args = [] args = [] for i ...
c10a379aab76ff2101f4353c5421ede62c1473cb
81,199
import math def data_to_sorted_xy(data, logx): """ Return a list of (x, y) pairs with distinct x values and sorted by x value. Enter: data: a list of (x, y) or [x, y] values. logx: True to return (log10(x), y) for each entry. Exit: data: the sorted list with unique x values. """ i...
7e84a3f684dc9a82bf5fd48256e5f5c18a5eedb6
13,870
def sort_dict(dict_unsorted): """Sort dictionary by values in reverse order.""" dict_sorted = sorted( dict_unsorted.items(), key=lambda dict_sort: dict_sort[1], reverse=True ) return dict_sorted
1b9be6dcf620e9b9c0074aa5fe03091d23e1b87d
93,063
def paginated_id_to_names(slack, method, key, **kwargs): """Call the paginated method via slack, return a dict of id: name.""" cursor = True mapping = {} while cursor: if cursor is True: api_return = slack.api_call(method, **kwargs) else: api_return = slack.api_c...
097fd00c4d78d12ea2d1b5067927e9d09eef2c0d
176,346
def end_chat(input_list): """ End chat Parameters ---------- input_list : list List containing 'quit' to end chat. Returns ------- True or False : boolean Boolean assures whether to end chat based on whether the input contains 'quit'. """ if 'quit' in in...
bcf96aa2d3dc43c1d8464b9775c7450ebcf7984b
665,045
def shift_row(matrix): """ This function is used for AES encryption. The first row is unchanged; The bytes in second, third and forth row are shifted over one, two and three bytes to the left respectively. :param matrix: A 4x4 matrix which composed by 4 lists. The lists only accept numeric objects. ...
6a5dfe1398db1582663fe26d2cb3c4d860d8dd00
202,800
def train_vae(model, input_tensor, opt, batch_size, loss_fn = None): """ Wrapper function to make a forward pass through a VAE model. Assumes that the model class contains a variational loss function, if this is not the case it can be provided using the loss_fn arg. Params ------ m...
1a93bb7f2e39e34975170aa90bd035c388f6fa26
425,129
def filter_document(nodes) -> str: """ Returns the text content of the specified nodes """ text = "" for element in nodes: text = text + element.text_content() return text
122cd81005f9b27d7945a3d51aad474e92a2968a
327,309
import re def regex(pattern): """Return compiled regular expression pattern.""" return re.compile(pattern)
e8fa8d3d8a8745d0dc6a2c71b4a8cba2c5af16b9
308,506
import itertools def args_combinations(*args, **kwargs): """ Given a bunch of arguments that are all a set of types, generate all possible possible combinations of argument type args is list of type or set of types kwargs is a dict whose values are types or set of types """ def asset(v): ...
8e90ce285322bd17a97e4bdb75e230f7015f4b2d
702,598
def index_range(keyword, header_list): """ This function takes in a keyword and a list of column header names and creates a list of indices that contains the specified keyword. :param keyword: A header or part of a header that you are interested in. :param header_list: A list of column header names...
b4efaab899362108406ad61b75dabcfc2bc92752
469,009
import re def parse_changelog(filename, version): """Parse Debian syntax changelog files and return last date. Args: filename: filename of changelog Returns: date in the format day-of-week, dd month yyyy hh:mm:ss +zzzz or -1 """ check_line = False with open(filename, 'r') as c...
2896d0a3ce02668b9977c9a89d6fe87d32b15325
505,637
def pick(d, *args): """ Pick some keys on a given dictionary. :param d: the dict :param args: the keys to pick """ res = {} getitem = d.getlist if hasattr(d, "getlist") else d.__getitem__ for key in args: if key in d: res[key] = getitem(key) return res
db040245574ef05faa596fcf6accbd50587f834f
87,929
import logging def create_logger(e_handler_name, t_handler_name): """ Wrapper to create logger for errors and training records :param e_handler_name: filepath to logger as string :param t_handler_name: filepath to logger as string :return: logger object """ log = logging.getLogger() l...
a0720407fd410781456b531ed36de4a232ff5ae2
260,495
def get_output_labels(opts, preds): """Convert predictions to labels.""" processor = opts["pass_in"][0] labels = processor.get_labels() output_labels = [] for prediction in preds: output_labels.append(labels[prediction]) return output_labels
8121107c0e894335925546d9970297f6f72a7bbf
100,121
def predict(title:str): """ Naive Baseline: Always predicts "fake" """ return {'prediction': 'fake'}
8cca0e47294dcb6b5c4bfb3d29c2e98a674266cc
482,373
def split_filter(string_to_split, delimiter): """ Create a custom Jinja filter to use it in the Jinja2 template. Same functionality as the split function in the Python language :param string_to_split: the string to split :type string_to_split: string :param delimiter: the delimiter to split the...
2648854cdb8173c49748cee94ba97c7186642a71
492,850
def sample_c(_rb): """ Force sample from ChainerRL PrioritizedReplayBuffer """ def sample(n): _rb.memory.wait_priority_after_sampling = False return _rb.sample(n) return sample
d83d6d8d115acb5cee953a7a9b24ca45e4548e0e
158,846
def arg_max(list_input): """ Return the index of the biggest element on the array :param list_input: Input List :return: index biggest element """ biggest_element = max(list_input) idx_max = list_input.index(biggest_element) return idx_max
667640534f947ee6b198a094add2a1c9fdc67c82
188,604
import re import string def clean_str(inp_str: str) -> str: """Removes non printable characters from the string Reference: https://stackoverflow.com/a/52540226 Arguments: inp_str {str} -- The input string, to be cleaned. Returns: {str} -- The cleaned string """ return re...
36a42eacc070a32b41934b9cdf55d8d59b1b4182
121,690
import copy def derive(coefficients): """ Applies one derivation of given coefficients list of polynomial function. :param coefficients: polynomial function represented as a coefficients list :return coefficients: the new derived polynomial function represented as a coefficients list """ if no...
ee0c4e7a672d1dbd1fc85692eecce6c61fb04a6a
329,782
def get_headers(sql, prefix=None): """Returns a list of headers in an sql select string Parameters ---------- sql : str SQL to get definition from. prefix : str (optional) Characters to prefix to each header. Returns ------- list List of headers. Example...
034ddcec32b6c36d5360777c75e5ae21c07a1821
493,664
def ord_mrv(csp): """ Return the variable with the minimum remaining value (MRV). """ # ANCHOR: alt version 1 # variables = csp.get_all_unasgn_vars() # dom_sizes = [var.cur_domain_size() for var in variables] # min_index = dom_sizes.index(min(dom_sizes)) # return variables[min_index] ...
7d574fe1d389f3493c171871b53d3555c2865b42
323,871
import math def get_points_dist(pt1, pt2): """Returns the distance between a pair of points. Parameters ---------- pt1 : libpysal.cg.Point A point. pt2 : libpysal.cg.Point The other point. Returns ------- dist : float The distance between ``pt1`` and ...
d4033334963673c2f33c2c170d8eaba4c0b6a77e
233,718
def parse_report_filter_values(request, reports): """Given a dictionary of GET query parameters, return a dictionary mapping report names to a dictionary of filter values. Report filter parameters contain a | in the name. For example, request.GET might be { "crash_report|operating_...
217a7bfdeb65952637774ebefb6ae0ea7a0d991c
9,834
def exponent(attrs, inputs, proto_obj): """Elementwise exponent of input array.""" return 'exp', attrs, inputs
78c9fb18246ca70126f193000fe432a85e2da08f
250,890
def utilization (arrv): """Returns observed utilization of all queues in arrv.""" max_d = 0 tot_time = [0] * arrv.num_queues() for evt in arrv: max_d = max (evt.d, max_d) tot_time[evt.qid] += evt.s return [ x / max_d for x in tot_time ]
18ed74b8e34f6411899f131e415c2e34de099a25
237,769
def get_indexes(table, col, v): """ Returns indexes of values _v_ in column _col_ of _table_. """ li = [] start = 0 for row in table[col]: if row == v: index = table[col].index(row, start) li.append(index) start = index + 1 return li
fd585a908477dd3632f2dbc3f450c1601157e928
675,575
def visualThreshold(npArray, value): """ Thresholds all pixel values greater than 0 in numPy array and sets them to given value Arguments: npArray: A numpy array value: int to set all values greater than zero to Returns: Numpy array """ #npArray = npArray.astype('uint8') #print(mito_anno) npArra...
f3ba99e36f670f65f7911acf5fc30df51aeff422
572,797
from ssl import _create_unverified_context from urllib.error import HTTPError, URLError from urllib.request import urlopen def url_is_alive(url: str) -> object: """ Checks that a given URL is reachable """ try: return urlopen(url, context=_create_unverified_context()) except HTTPError: ...
fa651fd941bc27b907491ad75ab5940331de75e9
123,912
def getTrackingScriptString(jobid=None): """ For injecting tracking script into a web page. jobid -> current jobid. Adds "jobid" custom variable to events and page views on this page. """ customVarString = "" if jobid is not None: customVarString = "_gaq.push(['_setCustomVar',1...
fca5d972a2950b17c639bb49237db40f55b91531
494,225
def get_fits_from_sample(cells, datatype, ct, tr): """ Returns the fits for the given datatype, celltype and treatment. :param cells: dict of cells :param datatype: str, datatype :param ct: str, celltype :param tr: str, treatment :return: dict of fits """ sample = cells[ct][tr] ...
b76946f29743561ec7c4a0d4080ea62e56d9644d
229,580
def create_table_name( table_type: str, command_id: str, context_id: str, node_id: str ) -> str: """ Creates and returns in lower case a table name with the format <tableType>_<commandId>_<contextId>_<nodeId> """ if table_type not in {"table", "view", "merge"}: raise TypeError(f"Table type i...
15083f04ccab3a0d11da7c6617cc8023e66d0452
447,089
def _intersect_point2_circle(P, C): """ Returns True if point P lies with circle C. @type P : Point2 instance @type C : Circle instance """ return abs(P - C.c) <= C.r
58522af7eed88e90c6377be5393da82d8ff99529
20,329
def getarg(args, index): """ Helper to retrieve value from command line args """ return args[index] if index < len(args) else None
256de43dda23c9f613c61a8dca456a3d507ca332
29,384
def security_plugin_label(context): """Security plug-in name adapter""" return context.title
3f5e415c88774e80e323a025eb3fc4055b4e4af2
556,217
from typing import Any def do_nothing(var: Any) -> Any: """ Does nothing but return what it's given. Mainly for default callbacks. :param var: Any :return: Returns var that was given. """ return var
cee85cdceb60c30043f1e2d72dfe36fa351aa343
426,040
def is_valid_algorithm(algorithm: str) -> bool: """check if the input algorithm is valid :param algorithm: algorithm name :type algorithm: str :return: True if the algorithm is valid :rtype: bool """ available_algorithms = [ 'Histogram Equalization', 'Contrast Stretching', ...
433aa8da6b0d2f7d4fc1c410754a0067f2644c78
176,655
import re def get_qdyn_compiler(configure_log): """Return the name the Fortran compiler that QDYN was compiled with""" with open(configure_log) as in_fh: for line in in_fh: if line.startswith("FC"): m = re.search(r'FC\s*:\s*(.*)', line) if m: ...
71e0b9e448664a3a5df11587d63574cef22e0511
681,419
def get_prefix(id: str) -> str: """Drop the final component of a qualified name (e.g. ('x.y' -> 'x').""" return id.rsplit('.', 1)[0]
ffe781d328e9d07fc7b044c9ff19de15a4799815
490,665
def apply_intervals(list_: list, intervals: list) -> list: """ Apply slice lists in a list. >>> list_with_elements = ['spam', 'eggs', 'foo', 'bar'] >>> intervals = [0, 2, 3] >>> apply_intervals(list_with_elements, intervals) ['spam', 'foo', 'bar'] """ return [list_[interval] for inter...
617c6ef925605394565de2c3c07ce5d7e233f75a
558,974
def createBox(box): """ create a box filter from the input list "box" this filter should accept a list of length len(box) and return a simple convolution of it. the meaning of this box filter is as follows: for each element the input list l, multiple l[i] by box[i] sum the results of all of ...
ad6fe6b3c1d2f778a5477225c609cb76da55f4e0
229,314
from typing import Sequence def listify(seq: Sequence) -> str: """Convert sequence to a conventional string list Args: seq (Sequence): Sequence object Returns: str: the conventional string list """ fnl = "" for option in seq: fnl += str(option) + ", " return fnl.s...
85587ceab6a732eff3a5805478a3bf42d977da25
340,800
import torch def reduce(tensor: torch.Tensor, reduction: str) -> torch.Tensor: """Reduces the given tensor using a specific criterion. Args: tensor (torch.Tensor): input tensor reduction (str): string with fixed values [elementwise_mean, none, sum] Raises: ValueError: when the re...
a77edd7f9a8486a8fd604b9a35c2ecfe28d43c8c
702,271
def rectangle_crop_img(src, x_start, x_end, y_start, y_end): """ Getting the parts needed in an image by clipping an image with a rectangle. Image ( 0, 0 ) points in the upper left corner of the image, x_start and x_end determine the height of the image, y_start and y_end determine the width of the ...
40eb71921db0f6d7f30221c8f07309a11daa4b2a
89,827
def label(label, units): """ Combines a label string and units string together in the form 'label [units]' unless one of the other is empty. """ string = r''+label if len(units)>0: string = string + ' [' + units + ']' return string
4eaef8d176d7d462503509df9b081c53da741195
236,148
def open_group(group, path): """Creates or loads the subgroup defined by `path`.""" if path in group: return group[path] else: return group.create_group(path)
b67b7cd5e49a6274a1dea68159f859cd7eb8c40e
457,162
def sum_series(n, n0=0, n1=1): """ Compute the nth value of a summation series. :param n0=0: value of zeroth element in the series :param n1=1: value of first element in the series This function should generalize the fibonacci() and the lucas(), so that this function works for any first two nu...
abd9fcf4f74500b03c0c3fdd00ca2cb57553dcb5
590,452
import re def begins_with_pattern(string, pattern): """ Check if a string begins with a pattern. Args: string (str) pattern (str or regular expression) Returns: bool """ x = re.match(pattern, string) if x is None: return False return True
48c8198c2cc00d57d06201e1a1058b9d9a43fbf7
355,526
def parse_args(args): """ This parses the arguments and returns a tuple containing: (args, command, command_args) For example, "--config=bar start --with=baz" would return: (['--config=bar'], 'start', ['--with=baz']) """ index = None for arg_i, arg in enumerate(args): if not a...
6e02216d7ff1af7677e5df956a508d7b80af43dd
680,619
def lookup(source, *keys, **kw): """Successively looks up each key, returning the default keyword arg if a dead end is reached.""" fallback = kw.get('default') try: for key in keys: source = source[key] return source except (KeyError, AttributeError, TypeError): return fallback
a7fefc33fb92a4b9fc9ca1231a11a6b998407681
503,186
def is_attorney(descr): """ If a string description is a attorney description """ return any(map(lambda s: s in descr.upper(), ('ATTORNEY','ATTNY')))
aa45fcada75e74abb9dee686d2b662f527f0f883
599,176
def list_of_elem(elem, length): """return a list of given length of given elements""" return [elem for i in range(length)]
d72bdab16a541714b2a0a781a3077d40e309e9f7
10,872
def calc_total_income(row): """ 计算累计收益 :param row: dataframe行 :return 累计收益 """ # 场景分析: 累计收益 = 当日市值+卖出金额-成本 return row["share"]*row["price"]+row["sell"]-row["cost"]
c73d7e5b0b34ada73b0fe92701c3fd40a483eeaa
191,792
def rgbfloat2rgbint(i): """ Convert a rgb float value (0.0-1.0) to a rgb integer value (0-255). Args: i (int): Float value from 0.0 to 1.0 Returns: int: Integer values from 0 to 255 """ return int(255 * i)
cad7b5f8ee0227a9ef02d9b1ce7703562a1632a2
486,149
def discoverSubclasses(cls): """Returns a list of all classes derived from the the supplied base class. Implementation based on ubuntu's answer to related question on StackOverflow [1]_. Parameters ---------- cls: type The base to search from Returns ------- list...
ff406074ff5269a5ac7f5fb746460b6b0a01e109
146,423
def broadcast(dct): """Convenience function for broadcasting a dictionary of lists. Parameters ---------- dct : dict Input parameter ranges. All parameter ranges must have only 1 or N values. Returns ------- list List of N dictionaries; the input parameters with N values ar...
85d8e32b0cf784b3975bd033b0a56d9df0641de1
577,671
import random def random_text(text_length=32): """Produces random characters of specified length for use in password or text""" possible_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!#$%()*+-/:;<=>?@[\]^_`{|}~' text = '' for _ in range(text_length): text += random.Sys...
fa2f60c7e8c461e4af1f5ba2a8886f1ef382ca0e
130,662
def merge_similarities(oldsims, newsims, clip=None): """ Merge two precomputed similarity lists, truncating the result to clip most similar items. """ if oldsims is None: result = newsims or [] elif newsims is None: result = oldsims else: result = sorted(oldsims + newsims...
02f243c4a9b3f0c7032f06f62b9ae1b94c6bfa84
110,846