code
stringlengths
51
2.38k
docstring
stringlengths
4
15.2k
def to_string(self, obj): try: converted = [str(element) for element in obj] string = ','.join(converted) except TypeError: string = str(obj) return string
Picks up an object and transforms it into a string, by coercing each element in an iterable to a string and then joining them, or by trying to coerce the object directly
def color(nickname): _hex = md5(nickname).hexdigest()[:6] darken = lambda s: str(int(round(int(s, 16) * .7))) return "rgb(%s)" % ",".join([darken(_hex[i:i+2]) for i in range(6)[::2]])
Provides a consistent color for a nickname. Uses first 6 chars of nickname's md5 hash, and then slightly darkens the rgb values for use on a light background.
def post(self, ddata, url=SETUP_ENDPOINT, referer=SETUP_ENDPOINT): headers = HEADERS.copy() if referer is None: headers.pop('Referer') else: headers['Referer'] = referer if 'csrfmiddlewaretoken' not in ddata.keys(): ddata['csrfmiddlewaretoken'] = self....
Method to update some attributes on namespace.
def int_check(*args, func=None): func = func or inspect.stack()[2][3] for var in args: if not isinstance(var, numbers.Integral): name = type(var).__name__ raise ComplexError( f'Function {func} expected integral number, {name} got instead.')
Check if arguments are integrals.
def stem_singular_word(self, word): context = Context(word, self.dictionary, self.visitor_provider) context.execute() return context.result
Stem a singular word to its common stem form.
def index_missing_documents(self, documents, request=None): log.info('Trying to index documents of type `{}` missing from ' '`{}` index'.format(self.doc_type, self.index_name)) if not documents: log.info('No documents to index') return query_kwargs = dict...
Index documents that are missing from ES index. Determines which documents are missing using ES `mget` call which returns a list of document IDs as `documents`. Then missing `documents` from that list are indexed.
def _print_err(*args): if not CFG.debug: return if not args: return encoding = 'utf8' if os.name == 'posix' else 'gbk' args = [_cs(a, encoding) for a in args] f_back = None try: raise Exception except: f_back = sys.exc_traceback.tb_frame.f_back f_name = f_...
Print errors. *args list, list of printing contents
def iss_spi_divisor(self, sck): _divisor = (6000000 / sck) - 1 divisor = int(_divisor) if divisor != _divisor: raise ValueError('Non-integer SCK divisor.') if not 1 <= divisor < 256: error = ( "The value of sck_divisor, {}, " "is no...
Calculate a USBISS SPI divisor value from the input SPI clock speed :param sck: SPI clock frequency :type sck: int :returns: ISS SCK divisor :rtype: int
def _get_history_minute_window(self, assets, end_dt, bar_count, field_to_use): try: minutes_for_window = self.trading_calendar.minutes_window( end_dt, -bar_count ) except KeyError: self._handle_minute_history_out_of_b...
Internal method that returns a dataframe containing history bars of minute frequency for the given sids.
def _get_value_from_match(self, key, match): value = match.groups(1)[0] clean_value = str(value).lstrip().rstrip() if clean_value == 'true': self._log.info('Got value of "%s" as boolean true.', key) return True if clean_value == 'false': self._log.info...
Gets the value of the property in the given MatchObject. Args: key (str): Key of the property looked-up. match (MatchObject): The matched property. Return: The discovered value, as a string or boolean.
def get(cls, scope=None): if scope is None: scope = cls.default if isinstance(scope, string_types) and scope in cls._keywords: return getattr(cls, scope) return scope
Return default or predefined URLs from keyword, pass through ``scope``.
def load(self, carddict): self.code = carddict["code"] if isinstance(self.code, text_type): self.code = eval(self.code) self.name = carddict["name"] self.abilities = carddict["abilities"] if isinstance(self.abilities, text_type): self.abilities = eval(self...
Takes a carddict as produced by ``Card.save`` and sets this card instances information to the previously saved cards information.
def list(self, cart_glob=['*.json']): carts = [] for glob in cart_glob: if not glob.endswith('.json'): search_glob = glob + ".json" else: search_glob = glob for cart in juicer.utils.find_pattern(Constants.CART_LOCATION, search_glob): ...
List all carts
def get_weights(self, data, F): beta = np.var(data) trans_F = F.T.copy() W = np.zeros((self.K, data.shape[1])) if self.weight_method == 'rr': W = np.linalg.solve(trans_F.dot(F) + beta * np.identity(self.K), trans_F.dot(data)) else: ...
Calculate weight matrix based on fMRI data and factors Parameters ---------- data : 2D array, with shape [n_voxel, n_tr] fMRI data from one subject F : 2D array, with shape [n_voxel,self.K] The latent factors from fMRI data. Returns ------- ...
def key_description(self): "Return a description of the key" vk, scan, flags = self._get_key_info() desc = '' if vk: if vk in CODE_NAMES: desc = CODE_NAMES[vk] else: desc = "VK %d"% vk else: desc = "%s"% self.key...
Return a description of the key
def _sumterm_prime(lexer): tok = next(lexer) if isinstance(tok, OP_or): xorterm = _xorterm(lexer) sumterm_prime = _sumterm_prime(lexer) if sumterm_prime is None: return xorterm else: return ('or', xorterm, sumterm_prime) else: lexer.unpop_token...
Return a sum term' expression, eliminates left recursion.
def module(command, *args): if 'MODULESHOME' not in os.environ: print('payu: warning: No Environment Modules found; skipping {0} call.' ''.format(command)) return modulecmd = ('{0}/bin/modulecmd'.format(os.environ['MODULESHOME'])) cmd = '{0} python {1} {2}'.format(modulecmd, co...
Run the modulecmd tool and use its Python-formatted output to set the environment variables.
def removeDefaultAttributeValue(node, attribute): if not node.hasAttribute(attribute.name): return 0 if isinstance(attribute.value, str): if node.getAttribute(attribute.name) == attribute.value: if (attribute.conditions is None) or attribute.conditions(node): node.rem...
Removes the DefaultAttribute 'attribute' from 'node' if specified conditions are fulfilled Warning: Does NOT check if the attribute is actually valid for the passed element type for increased preformance!
def IOC_TYPECHECK(t): result = ctypes.sizeof(t) assert result <= _IOC_SIZEMASK, result return result
Returns the size of given type, and check its suitability for use in an ioctl command number.
def _options_request(self, url, **kwargs): request_kwargs = { 'method': 'OPTIONS', 'url': url } for key, value in kwargs.items(): request_kwargs[key] = value return self._request(**request_kwargs)
a method to catch and report http options request connectivity errors
def gc(self): gc = len([base for base in self.seq if base == 'C' or base == 'G']) return float(gc) / len(self)
Find the frequency of G and C in the current sequence.
def goal(self, goal_name, count=1): for enrollment in self._get_all_enrollments(): if enrollment.experiment.is_displaying_alternatives(): self._experiment_goal(enrollment.experiment, enrollment.alternative, goal_name, count)
Record that this user has performed a particular goal This will update the goal stats for all experiments the user is enrolled in.
def rgevolve_leadinglog(self, scale_out): self._check_initial() return rge.smeft_evolve_leadinglog(C_in=self.C_in, scale_high=self.scale_high, scale_in=self.scale_in, scale_out=scale_out)
Compute the leading logarithmix approximation to the solution of the SMEFT RGEs from the initial scale to `scale_out`. Returns a dictionary with parameters and Wilson coefficients. Much faster but less precise that `rgevolve`.
def app_profile( self, app_profile_id, routing_policy_type=None, description=None, cluster_id=None, allow_transactional_writes=None, ): return AppProfile( app_profile_id, self, routing_policy_type=routing_policy_type, ...
Factory to create AppProfile associated with this instance. For example: .. literalinclude:: snippets.py :start-after: [START bigtable_create_app_profile] :end-before: [END bigtable_create_app_profile] :type app_profile_id: str :param app_profile_id: The ID of ...
def optimal_parameters(reconstruction, fom, phantoms, data, initial=None, univariate=False): r def func(lam): return sum(fom(reconstruction(datai, lam), phantomi) for phantomi, datai in zip(phantoms, data)) tol = np.finfo(phantoms[0].space.dtype).resolution ...
r"""Find the optimal parameters for a reconstruction method. Notes ----- For a forward operator :math:`A : X \to Y`, a reconstruction operator parametrized by :math:`\theta` is some operator :math:`R_\theta : Y \to X` such that .. math:: R_\theta(A(x)) \approx x. The optimal c...
def identify(fn): return ( fn.__globals__['__name__'], getattr(fn, '__qualname__', getattr(fn, '__name__', '')) ) def __init__(self, fn): self.validate_function(fn) self.configured = False self.has_backup_plan = False if sel...
returns a tuple that is used to match functions to their neighbors in their resident namespaces
def ln_growth(eqdata, **kwargs): if 'outputcol' not in kwargs: kwargs['outputcol'] = 'LnGrowth' return np.log(growth(eqdata, **kwargs))
Return the natural log of growth. See also -------- :func:`growth`
def stop(self, id): path = partial(_path, self.adapter) path = path(id) return self._delete(path)
stop the tracker.
def get_content_models(cls): concrete_model = base_concrete_model(ContentTyped, cls) return [m for m in apps.get_models() if m is not concrete_model and issubclass(m, concrete_model)]
Return all subclasses of the concrete model.
def space_acl(args): r = fapi.get_workspace_acl(args.project, args.workspace) fapi._check_response_code(r, 200) result = dict() for user, info in sorted(r.json()['acl'].items()): result[user] = info['accessLevel'] return result
Retrieve access control list for a workspace
def _safe_name(file_name, sep): file_name = stringify(file_name) if file_name is None: return file_name = ascii_text(file_name) file_name = category_replace(file_name, UNICODE_CATEGORIES) file_name = collapse_spaces(file_name) if file_name is None or not len(file_name): return ...
Convert the file name to ASCII and normalize the string.
def get(self, label, default=None): if label in self.index: loc = self.index.get_loc(label) return self._get_val_at(loc) else: return default
Returns value occupying requested label, default to specified missing value if not present. Analogous to dict.get Parameters ---------- label : object Label value looking for default : object, optional Value to return if label not in index Return...
def actions(obj, **kwargs): if 'exclude' in kwargs: kwargs['exclude'] = kwargs['exclude'].split(',') actions = obj.get_actions(**kwargs) if isinstance(actions, dict): actions = actions.values() buttons = "".join("%s" % action.render() for action in actions) return '<div class="action...
Return actions available for an object
def action_draft(self): for rec in self: if not rec.state == 'cancelled': raise UserError( _('You need to cancel it before reopening.')) if not (rec.am_i_owner or rec.am_i_approver): raise UserError( _('You are not a...
Set a change request as draft
def _prepare_corerelation_data(self, X1, X2, start_voxel=0, num_processed_voxels=None): num_samples = len(X1) assert num_samples > 0, \ 'at least one sample is needed for correlation computation' num_voxels1 = X1[0...
Compute auto-correlation for the input data X1 and X2. it will generate the correlation between some voxels and all voxels Parameters ---------- X1: a list of numpy array in shape [num_TRs, num_voxels1] X1 contains the activity data filtered by ROIs and prepared...
def render(self): if not self.available(): return "" mtool = api.get_tool("portal_membership") member = mtool.getAuthenticatedMember() roles = member.getRoles() allowed = "LabManager" in roles or "Manager" in roles self.get_failed_instruments() if allo...
Render the viewlet
def get_owner_ids_value(self, obj): return [ user.pk for user in get_users_with_permission(obj, get_full_perm('owner', obj)) ]
Extract owners' ids.
def gray2bin(G): return farray([G[i:].uxor() for i, _ in enumerate(G)])
Convert a gray-coded vector into a binary-coded vector.
def status(cls): return cls.json_get('%s/status' % cls.api_url, empty_key=True, send_key=False)
Retrieve global status from status.gandi.net.
def dirty(self): return not os.path.exists(self.cachename) or \ (os.path.getmtime(self.filename) > os.path.getmtime(self.cachename))
True if the cache needs to be updated, False otherwise
def get_my_ip(): ip = subprocess.check_output(GET_IP_CMD, shell=True).decode('utf-8')[:-1] return ip.strip()
Returns this computers IP address as a string.
def sum(self): return self._constructor(self.values.sum(axis=self.baseaxes, keepdims=True))
Compute the sum across records.
def classify_catalog(catalog): components = [] islands = [] simples = [] for source in catalog: if isinstance(source, OutputSource): components.append(source) elif isinstance(source, IslandSource): islands.append(source) elif isinstance(source, SimpleSourc...
Look at a list of sources and split them according to their class. Parameters ---------- catalog : iterable A list or iterable object of {SimpleSource, IslandSource, OutputSource} objects, possibly mixed. Any other objects will be silently ignored. Returns ------- components : ...
def wrap_io_os_err(e): msg = '' if e.strerror: msg = e.strerror if e.message: msg = ' '.join([e.message, msg]) if e.filename: msg = ': '.join([msg, e.filename]) return msg
Formats IO and OS error messages for wrapping in FSQExceptions
def get_userinfo(self): wanted_fields = ["name", "mobile", "orgEmail", "position", "avatar"] userinfo = {k: self.json_response.get(k, None) for k in wanted_fields} return userinfo
Method to get current user's name, mobile, email and position.
def _format_postconditions(postconditions: List[icontract._Contract], prefix: Optional[str] = None) -> List[str]: if not postconditions: return [] result = [] if prefix is not None: result.append(":{} ensures:".format(prefix)) else: result.append(":ensures:") for postconditio...
Format postconditions as reST. :param postconditions: postconditions of a function :param prefix: prefix to be prepended to ``:ensures:`` directive :return: list of lines describing the postconditions
def register_instance(self, instance, allow_dotted_names=False): self.instance = instance self.allow_dotted_names = allow_dotted_names
Registers an instance to respond to XML-RPC requests. Only one instance can be installed at a time. If the registered instance has a _dispatch method then that method will be called with the name of the XML-RPC method and its parameters as a tuple e.g. instance._dispatch('add',...
def _get_url_hashes(path): urls = _read_text_file(path) def url_hash(u): h = hashlib.sha1() try: u = u.encode('utf-8') except UnicodeDecodeError: logging.error('Cannot hash url: %s', u) h.update(u) return h.hexdigest() return {url_hash(u): True for u in urls}
Get hashes of urls in file.
def merge_paths(paths, weights=None): G = make_paths(paths, weights=weights) G = reduce_paths(G) return G
Zip together sorted lists. >>> paths = [[1, 2, 3], [1, 3, 4], [2, 4, 5]] >>> G = merge_paths(paths) >>> nx.topological_sort(G) [1, 2, 3, 4, 5] >>> paths = [[1, 2, 3, 4], [1, 2, 3, 2, 4]] >>> G = merge_paths(paths, weights=(1, 2)) >>> nx.topological_sort(G) [1, 2, 3, 4]
def _calc_new_threshold(self, score): if self.threshold_mode == 'rel': abs_threshold_change = self.threshold * score else: abs_threshold_change = self.threshold if self.lower_is_better: new_threshold = score - abs_threshold_change else: new...
Determine threshold based on score.
def locked(path, timeout=None): def decor(func): @functools.wraps(func) def wrapper(*args, **kwargs): lock = FileLock(path, timeout=timeout) lock.acquire() try: return func(*args, **kwargs) finally: lock.release() ...
Decorator which enables locks for decorated function. Arguments: - path: path for lockfile. - timeout (optional): Timeout for acquiring lock. Usage: @locked('/var/run/myname', timeout=0) def myname(...): ...
def atlasdb_reset_zonefile_tried_storage( con=None, path=None ): with AtlasDBOpen(con=con, path=path) as dbcon: sql = "UPDATE zonefiles SET tried_storage = ? WHERE present = ?;" args = (0, 0) cur = dbcon.cursor() res = atlasdb_query_execute( cur, sql, args ) dbcon.commit() ...
For zonefiles that we don't have, re-attempt to fetch them from storage.
def Terminate(self): self.lock.acquire() try: for bucket in self.connections.values(): try: for conn in bucket: conn.lock() try: conn.Close() except Exception: pass conn.release() except Exception: pass self.connections = {} finally: self.lock.relea...
Close all open connections Loop though all the connections and commit all queries and close all the connections. This should be called at the end of your application. @author: Nick Verbeck @since: 5/12/2008
def end_namespace(self, prefix): del self._ns[prefix] self._g.endPrefixMapping(prefix)
Undeclare a namespace prefix.
def place(self, value): if (value is not None) and (not value in DG_C_PLACE): raise ValueError("Unrecognized value for place: '%s'" % value) self.__place = value
Set the place of articulation of the consonant. :param str value: the value to be set
def set_handler(self, language, obj): if obj is None: if language in self._handlers: del self._handlers[language] else: self._handlers[language] = obj
Define a custom language handler for RiveScript objects. Pass in a ``None`` value for the object to delete an existing handler (for example, to prevent Python code from being able to be run by default). Look in the ``eg`` folder of the rivescript-python distribution for an example scri...
def logged_command(cmds): "helper function to log a command and then run it" logger.info(' '.join(cmds)) os.system(' '.join(cmds))
helper function to log a command and then run it
def extras_msg(extras): if len(extras) == 1: verb = "was" else: verb = "were" return ", ".join(repr(extra) for extra in extras), verb
Create an error message for extra items or properties.
def accept_key(pki_dir, pub, id_): for key_dir in 'minions', 'minions_pre', 'minions_rejected': key_path = os.path.join(pki_dir, key_dir) if not os.path.exists(key_path): os.makedirs(key_path) key = os.path.join(pki_dir, 'minions', id_) with salt.utils.files.fopen(key, 'w+') as f...
If the master config was available then we will have a pki_dir key in the opts directory, this method places the pub key in the accepted keys dir and removes it from the unaccepted keys dir if that is the case.
def save(self, session_file, verbose=False): PARAMS={"file":session_file} response=api(url=self.__url+"/save", PARAMS=PARAMS, verbose=verbose) return response
Saves the current session to an existing file, which will be replaced. If this is a new session that has not been saved yet, use 'save as' instead. :param session_file: The path to the file where the current session must be saved to. :param verbose: print more
def list_sku_versions(access_token, subscription_id, location, publisher, offer, sku): endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/providers/Microsoft.Compute/', 'locations/', location, '...
List available versions for a given publisher's sku. Args: access_token (str): A valid Azure authentication token. subscription_id (str): Azure subscription id. location (str): Azure data center location. E.g. westus. publisher (str): VM image publisher. E.g. MicrosoftWindowsServer....
def namespace_to_dict(obj): if isinstance(obj, (argparse.Namespace, optparse.Values)): return vars(obj) return obj
If obj is argparse.Namespace or optparse.Values we'll return a dict representation of it, else return the original object. Redefine this method if using other parsers. :param obj: * :return: :rtype: dict or *
def add_barplot(self): cats = OrderedDict() cats['n_nondups'] = {'name': 'Non-duplicates'} cats['n_dups'] = {'name': 'Duplicates'} pconfig = { 'id': 'samblaster_duplicates', 'title': 'Samblaster: Number of duplicate reads', 'ylab': 'Number of reads' ...
Generate the Samblaster bar plot.
def space_labels(document): for label in document.xpath('.//bold'): if not label.text or not re.match('^\(L?\d\d?[a-z]?\):?$', label.text, re.I): continue parent = label.getparent() previous = label.getprevious() if previous is None: text = parent.text or '' ...
Ensure space around bold compound labels.
def field_types(self): if self._field_types is None: self._field_types = FieldTypeList(self._version, assistant_sid=self._solution['sid'], ) return self._field_types
Access the field_types :returns: twilio.rest.autopilot.v1.assistant.field_type.FieldTypeList :rtype: twilio.rest.autopilot.v1.assistant.field_type.FieldTypeList
def get_fields(model_class): return [ attr for attr, value in model_class.__dict__.items() if issubclass(type(value), (mongo.base.BaseField, mongo.EmbeddedDocumentField)) ]
Pass in a mongo model class and extract all the attributes which are mongoengine fields Returns: list of strings of field attributes
def visit(self, node): method = getattr(self, 'visit_' + node.expr_name, self.generic_visit) try: return method(node, [self.visit(child) for child in reversed(list(node))]) except (VisitationError, UndefinedLabel): raise except self.unwrapped_exceptions: ...
See the ``NodeVisitor`` visit method. This just changes the order in which we visit nonterminals from right to left to left to right.
def on_ok(self, sender): logger.debug("in on_ok with sender %s" % sender) if sender == self.ion_task and not self.transfer_done: ion_structure = self.ion_task.get_final_structure() self.ioncell_task._change_structure(ion_structure) self.transfer_done = True ...
This callback is called when one task reaches status S_OK. If sender == self.ion_task, we update the initial structure used by self.ioncell_task and we unlock it so that the job can be submitted.
def compile_delete(self, query): table = self.wrap_table(query.from__) if isinstance(query.wheres, list): wheres = self._compile_wheres(query) else: wheres = "" if query.joins: joins = " %s" % self._compile_joins(query, query.joins) sql = "...
Compile a delete statement into SQL :param query: A QueryBuilder instance :type query: QueryBuilder :return: The compiled update :rtype: str
def dir_import_table(self): import_header = list(self.optional_data_directories)[1] import_offset = self.resolve_rva(import_header.VirtualAddress) i = 0 while True: offset = import_offset + i*Import_DirectoryTable.get_size() idt = Import_DirectoryTable(self.stream...
import table is terminated by a all-null entry, so we have to check for that
def set_contents_from_filename(self, filename, headers=None, replace=True, cb=None, num_cb=10, policy=None, md5=None, reduced_redundancy=False, encrypt_key=False): fp = open(filename, 'rb') self.set_...
Store an object in S3 using the name of the Key object as the key in S3 and the contents of the file named by 'filename'. See set_contents_from_file method for details about the parameters. :type filename: string :param filename: The name of the file that you want to put onto S3...
def link_href(self, rel): link = self.link(rel) if (link is not None): link = link['href'] return(link)
Look for link with specified rel, return href from it or None.
def to_frequencyseries(self, delta_f=None): from pycbc.fft import fft if not delta_f: delta_f = 1.0 / self.duration tlen = int(1.0 / delta_f / self.delta_t + 0.5) flen = int(tlen / 2 + 1) if tlen < len(self): raise ValueError("The value of delta_f (%s) wo...
Return the Fourier transform of this time series Parameters ---------- delta_f : {None, float}, optional The frequency resolution of the returned frequency series. By default the resolution is determined by the duration of the timeseries. Returns ------- ...
def fw_retry_failures_delete(self): for tenant_id in self.fwid_attr: try: with self.fwid_attr[tenant_id].mutex_lock: fw_data = self.get_fw_by_tenant_id(tenant_id) if fw_data is None: LOG.info("No FW for tenant %s", tenan...
This routine is called for retrying the delete cases.
def _find_plugin_dir(module_type): for install_dir in _get_plugin_install_dirs(): candidate = os.path.join(install_dir, module_type) if os.path.isdir(candidate): return candidate else: raise PluginCandidateError( 'No plugin found for `{}` module in paths:\n{}'.for...
Find the directory containing the plugin definition for the given type. Do this by searching all the paths where plugins can live for a dir that matches the type name.
def subs2seqs(self) -> Dict[str, List[str]]: subs2seqs = collections.defaultdict(list) nodes = find(self.find('sequences'), 'node') if nodes is not None: for seq in nodes: subs2seqs['node'].append(strip(seq.tag)) return subs2seqs
A |collections.defaultdict| containing the node-specific information provided by XML `sequences` element. >>> from hydpy.auxs.xmltools import XMLInterface >>> from hydpy import data >>> interface = XMLInterface('single_run.xml', data.get_path('LahnH')) >>> series_io = interface....
def main(reactor): control_ep = UNIXClientEndpoint(reactor, '/var/run/tor/control') tor = yield txtorcon.connect(reactor, control_ep) state = yield tor.create_state() print("Closing all circuits:") for circuit in list(state.circuits.values()): path = '->'.join(map(lambda r: r.id_hex, circuit...
Close all open streams and circuits in the Tor we connect to
def result_type(*arrays_and_dtypes): types = {np.result_type(t).type for t in arrays_and_dtypes} for left, right in PROMOTE_TO_OBJECT: if (any(issubclass(t, left) for t in types) and any(issubclass(t, right) for t in types)): return np.dtype(object) return np.result_type(...
Like np.result_type, but with type promotion rules matching pandas. Examples of changed behavior: number + string -> object (not string) bytes + unicode -> object (not unicode) Parameters ---------- *arrays_and_dtypes : list of arrays and dtypes The dtype is extracted from both numpy a...
def mouseDown(self, button): log.debug('mouseDown %s', button) self.buttons |= 1 << (button - 1) self.pointerEvent(self.x, self.y, buttonmask=self.buttons) return self
Send a mouse button down at the last set position button: int: [1-n]
def backward(self, loss): with mx.autograd.record(): if isinstance(loss, (tuple, list)): ls = [l * self._scaler.loss_scale for l in loss] else: ls = loss * self._scaler.loss_scale mx.autograd.backward(ls)
backward propagation with loss
def isSquare(matrix): try: try: dim1, dim2 = matrix.shape except AttributeError: dim1, dim2 = _np.array(matrix).shape except ValueError: return False if dim1 == dim2: return True return False
Check that ``matrix`` is square. Returns ======= is_square : bool ``True`` if ``matrix`` is square, ``False`` otherwise.
def write(self, data, params=None, expected_response_code=204, protocol='json'): headers = self._headers headers['Content-Type'] = 'application/octet-stream' if params: precision = params.get('precision') else: precision = None if protocol ==...
Write data to InfluxDB. :param data: the data to be written :type data: (if protocol is 'json') dict (if protocol is 'line') sequence of line protocol strings or single string :param params: additional parameters for the request, d...
def GetUnscannedSubNode(self): if not self.sub_nodes and not self.scanned: return self for sub_node in self.sub_nodes: result = sub_node.GetUnscannedSubNode() if result: return result return None
Retrieves the first unscanned sub node. Returns: SourceScanNode: sub scan node or None if not available.
def addAnnotationsSearchOptions(parser): addAnnotationSetIdArgument(parser) addReferenceNameArgument(parser) addReferenceIdArgument(parser) addStartArgument(parser) addEndArgument(parser) addEffectsArgument(parser) addPageSizeArgument(parser)
Adds common options to a annotation searches command line parser.
def guess_pygments_highlighter(filename): try: from pygments.lexers import get_lexer_for_filename, get_lexer_by_name except Exception: return TextSH root, ext = os.path.splitext(filename) if ext in custom_extension_lexer_mapping: try: lexer = get_lexer_by_name...
Factory to generate syntax highlighter for the given filename. If a syntax highlighter is not available for a particular file, this function will attempt to generate one based on the lexers in Pygments. If Pygments is not available or does not have an appropriate lexer, TextSH will be returned in...
def close_cursor(self, cursor_id, address=None): warnings.warn( "close_cursor is deprecated.", DeprecationWarning, stacklevel=2) if not isinstance(cursor_id, integer_types): raise TypeError("cursor_id must be an instance of (int, long)") self._clos...
DEPRECATED - Send a kill cursors message soon with the given id. Raises :class:`TypeError` if `cursor_id` is not an instance of ``(int, long)``. What closing the cursor actually means depends on this client's cursor manager. This method may be called from a :class:`~pymongo.cursor.Curs...
def analyse(self, path_and_filename, pattern): with open(path_and_filename) as handle: content = handle.read() loc = content.count('\n') + 1 com = 0 for match in re.findall(pattern, content, re.DOTALL): com += match.count('\n') + 1 retu...
Find out lines of code and lines of comments. Args: path_and_filename (str): path and filename to parse for loc and com. pattern (str): regex to search for line commens and block comments Returns: int, int: loc and com for given file.
def fist() -> Histogram1D: import numpy as np from ..histogram1d import Histogram1D widths = [0, 1.2, 0.2, 1, 0.1, 1, 0.1, 0.9, 0.1, 0.8] edges = np.cumsum(widths) heights = np.asarray([4, 1, 7.5, 6, 7.6, 6, 7.5, 6, 7.2]) + 5 return Histogram1D(edges, heights, axis_name="Is this a fist?", title=...
A simple histogram in the shape of a fist.
def get_prefix(self, form, name): return '{form_prefix}{prefix_name}-{field_name}'.format( form_prefix=form.prefix + '-' if form.prefix else '', prefix_name=self.prefix_name, field_name=name)
Return the prefix that is used for the formset.
def connection(self, connection): if connection is not None: connection.subscribe("capacity", self._on_capacity_data) connection.default_return_capacity = True if self._connection is not None: connection.unsubscribe("capacity", self._on_capacity_data) self._co...
Change the dynamo connection
def parse(input_: Union[str, FileStream], source: str) -> Optional[str]: error_listener = ParseErrorListener() if not isinstance(input_, FileStream): input_ = InputStream(input_) lexer = jsgLexer(input_) lexer.addErrorListener(error_listener) tokens = CommonTokenStream(lexer) tokens.fill...
Parse the text in infile and save the results in outfile :param input_: string or stream to parse :param source: source name for python file header :return: python text if successful
def parse(self, data): self.validate_packet(data) packet_length = data[0] packet_type = data[1] sub_type = data[2] sequence_number = data[3] command_type = data[4] transceiver_type = data[5] transceiver_type_text = _MSG1_RECEIVER_TYPE.get(data[5]) ...
Parse a 13 byte packet in the Status format. :param data: bytearray to be parsed :type data: bytearray :return: Data dictionary containing the parsed values :rtype: dict
def setup_app(app, api): api.add_resource( KnwKBAllResource, '/api/knowledge' ) api.add_resource( KnwKBResource, '/api/knowledge/<string:slug>' ) api.add_resource( KnwKBMappingsResource, '/api/knowledge/<string:slug>/mappings' ) api.add_resourc...
setup the resources urls.
def handlePosition(self, msg): self.log_msg("position", msg) contract_tuple = self.contract_to_tuple(msg.contract) contractString = self.contractString(contract_tuple) self.registerContract(msg.contract) if msg.account not in self._positions.keys(): self._positions[ms...
handle positions changes
def _update(self, rules: list): self._rules = rules to_store = '\n'.join( rule.config_string for rule in rules ) sftp_connection = self._sftp_connection with sftp_connection.open(self.RULE_PATH, mode='w') as file_handle: file_handle.write(to_st...
Updates the given rules and stores them on the router.
def update_pulled_fields(instance, imported_instance, fields): modified = False for field in fields: pulled_value = getattr(imported_instance, field) current_value = getattr(instance, field) if current_value != pulled_value: setattr(instance, field, pulled_value) ...
Update instance fields based on imported from backend data. Save changes to DB only one or more fields were changed.
def prepare_soap_body(self, method, parameters, namespace): tags = [] for name, value in parameters: tag = "<{name}>{value}</{name}>".format( name=name, value=escape("%s" % value, {'"': "&quot;"})) tags.append(tag) wrapped_params = "".join(tags) if...
Prepare the SOAP message body for sending. Args: method (str): The name of the method to call. parameters (list): A list of (name, value) tuples containing the parameters to pass to the method. namespace (str): tThe XML namespace to use for the method. ...
def during(f): def decorator(g): @wraps(g) def h(*args, **kargs): tf = Thread(target=f, args=args, kwargs=kargs) tf.start() r = g(*args, **kargs) tf.join() return r return h return decorator
Runs f during the decorated function's execution in a separate thread.
def render(self): f1 = self._format_alignment(self._alignment[0], self._alignment[1]) f2 = self._format_alignment(self._alignment[1], self._alignment[0]) return f1, f2
Returns a tuple of HTML fragments rendering each element of the sequence.
def send(self, url, **kwargs): if self.config.server_url: return super(DjangoClient, self).send(url, **kwargs) else: self.error_logger.error("No server configured, and elasticapm not installed. Cannot send message") return None
Serializes and signs ``data`` and passes the payload off to ``send_remote`` If ``server`` was passed into the constructor, this will serialize the data and pipe it to the server using ``send_remote()``.