positive
stringlengths
100
30.3k
anchor
stringlengths
1
15k
def get_pod_by_uid(uid, podlist): """ Searches for a pod uid in the podlist and returns the pod if found :param uid: pod uid :param podlist: podlist dict object :return: pod dict object if found, None if not found """ for pod in podlist.get("items", []): try: if pod["metadata"]["uid"] == uid: return pod except KeyError: continue return None
Searches for a pod uid in the podlist and returns the pod if found :param uid: pod uid :param podlist: podlist dict object :return: pod dict object if found, None if not found
def bind_topic_exchange(self, exchange_name, routing_key, queue_name): """ 绑定主题交换机和队列 :param exchange_name: 需要绑定的交换机名 :param routing_key: :param queue_name: 需要绑定的交换机队列名 :return: """ self._channel.queue_declare( queue=queue_name, auto_delete=True, durable=True, ) self._channel.exchange_declare( exchange=exchange_name, exchange_type='topic', auto_delete=True, ) self._channel.queue_bind( exchange=exchange_name, queue=queue_name, routing_key=routing_key )
绑定主题交换机和队列 :param exchange_name: 需要绑定的交换机名 :param routing_key: :param queue_name: 需要绑定的交换机队列名 :return:
async def stop(self): """ Stops playback from lavalink. .. important:: This method will clear the queue. """ await self.node.stop(self.channel.guild.id) self.queue = [] self.current = None self.position = 0 self._paused = False
Stops playback from lavalink. .. important:: This method will clear the queue.
def split_comments(comments): """Split COMMENTS into flag comments and other comments. Flag comments are those that begin with '#,', e.g. '#,fuzzy'.""" flags = [] other = [] for c in comments: if len(c) > 1 and c[1] == ',': flags.append(c) else: other.append(c) return flags, other
Split COMMENTS into flag comments and other comments. Flag comments are those that begin with '#,', e.g. '#,fuzzy'.
def normalizeSequence(sequence): """ normalize sequence by subtracting the mean and :param sequence: a list of data samples :param considerDimensions: a list of dimensions to consider :return: normalized sequence """ seq = np.array(sequence).astype('float64') meanSeq = np.mean(seq) stdSeq = np.std(seq) seq = (seq - np.mean(seq)) / np.std(seq) sequence = seq.tolist() return sequence, meanSeq, stdSeq
normalize sequence by subtracting the mean and :param sequence: a list of data samples :param considerDimensions: a list of dimensions to consider :return: normalized sequence
def _mock_request(self, **kwargs): """ A mocked out make_request call that bypasses all network calls and simply returns any mocked responses defined. """ model = kwargs.get('model') service = model.service_model.endpoint_prefix operation = model.name LOG.debug('_make_request: %s.%s', service, operation) return self.load_response(service, operation)
A mocked out make_request call that bypasses all network calls and simply returns any mocked responses defined.
def start(self, timeout=None): """Install the server on its IOLoop, optionally starting the IOLoop. Parameters ---------- timeout : float or None, optional Time in seconds to wait for server thread to start. """ if self._running.isSet(): raise RuntimeError('Server already started') self._stopped.clear() # Make sure we have an ioloop self.ioloop = self._ioloop_manager.get_ioloop() self._ioloop_manager.start() # Set max_buffer_size to ensure streams are closed # if too-large messages are received self._tcp_server = tornado.tcpserver.TCPServer( self.ioloop, max_buffer_size=self.MAX_MSG_SIZE) self._tcp_server.handle_stream = self._handle_stream self._server_sock = self._bind_socket(self._bindaddr) self._bindaddr = self._server_sock.getsockname() self.ioloop.add_callback(self._install) if timeout: return self._running.wait(timeout)
Install the server on its IOLoop, optionally starting the IOLoop. Parameters ---------- timeout : float or None, optional Time in seconds to wait for server thread to start.
def choose_ancestral_states_mppa(tree, feature, states, force_joint=True): """ Chooses node ancestral states based on their marginal probabilities using MPPA method. :param force_joint: make sure that Joint state is chosen even if it has a low probability. :type force_joint: bool :param tree: tree of interest :type tree: ete3.Tree :param feature: character for which the ancestral states are to be chosen :type feature: str :param states: possible character states in order corresponding to the probabilities array :type states: numpy.array :return: number of ancestral scenarios selected, calculated by multiplying the number of selected states for all nodes. Also modified the get_personalized_feature_name(feature, ALLOWED_STATES) feature of each node to only contain the selected states. :rtype: int """ lh_feature = get_personalized_feature_name(feature, LH) allowed_state_feature = get_personalized_feature_name(feature, ALLOWED_STATES) joint_state_feature = get_personalized_feature_name(feature, JOINT_STATE) n = len(states) _, state2array = get_state2allowed_states(states, False) num_scenarios = 1 unresolved_nodes = 0 num_states = 0 # If force_joint == True, # we make sure that the joint state is always chosen, # for this we sort the marginal probabilities array as [lowest_non_joint_mp, ..., highest_non_joint_mp, joint_mp] # select k in 1:n such as the correction between choosing 0, 0, ..., 1/k, ..., 1/k and our sorted array is min # and return the corresponding states for node in tree.traverse(): marginal_likelihoods = getattr(node, lh_feature) marginal_probs = marginal_likelihoods / marginal_likelihoods.sum() if force_joint: joint_index = getattr(node, joint_state_feature) joint_prob = marginal_probs[joint_index] marginal_probs = np.hstack((np.sort(np.delete(marginal_probs, joint_index)), [joint_prob])) else: marginal_probs = np.sort(marginal_probs) best_k = n best_correstion = np.inf for k in range(1, n + 1): correction = np.hstack((np.zeros(n - k), np.ones(k) / k)) - marginal_probs correction = correction.dot(correction) if correction < best_correstion: best_correstion = correction best_k = k num_scenarios *= best_k num_states += best_k if force_joint: indices_selected = sorted(range(n), key=lambda _: (0 if n == joint_index else 1, -marginal_likelihoods[_]))[:best_k] else: indices_selected = sorted(range(n), key=lambda _: -marginal_likelihoods[_])[:best_k] if best_k == 1: allowed_states = state2array[indices_selected[0]] else: allowed_states = np.zeros(len(states), dtype=np.int) allowed_states[indices_selected] = 1 unresolved_nodes += 1 node.add_feature(allowed_state_feature, allowed_states) return num_scenarios, unresolved_nodes, num_states
Chooses node ancestral states based on their marginal probabilities using MPPA method. :param force_joint: make sure that Joint state is chosen even if it has a low probability. :type force_joint: bool :param tree: tree of interest :type tree: ete3.Tree :param feature: character for which the ancestral states are to be chosen :type feature: str :param states: possible character states in order corresponding to the probabilities array :type states: numpy.array :return: number of ancestral scenarios selected, calculated by multiplying the number of selected states for all nodes. Also modified the get_personalized_feature_name(feature, ALLOWED_STATES) feature of each node to only contain the selected states. :rtype: int
def sniff(count=0, store=1, offline=None, prn = None, lfilter=None, L2socket=None, timeout=None, stop_callback=None, *arg, **karg): """Sniff packets sniff([count=0,] [prn=None,] [store=1,] [offline=None,] [lfilter=None,] + L2ListenSocket args) -> list of packets Select interface to sniff by setting conf.iface. Use show_interfaces() to see interface names. count: number of packets to capture. 0 means infinity store: wether to store sniffed packets or discard them prn: function to apply to each packet. If something is returned, it is displayed. Ex: ex: prn = lambda x: x.summary() lfilter: python function applied to each packet to determine if further action may be done ex: lfilter = lambda x: x.haslayer(Padding) offline: pcap file to read packets from, instead of sniffing them timeout: stop sniffing after a given time (default: None) L2socket: use the provided L2socket stop_callback: Call every loop to determine if we need to stop the capture """ c = 0 if offline is None: log_runtime.info('Sniffing on %s' % conf.iface) if L2socket is None: L2socket = conf.L2listen s = L2socket(type=ETH_P_ALL, *arg, **karg) else: s = PcapReader(offline) lst = [] if timeout is not None: stoptime = time.time()+timeout remain = None while 1: try: if timeout is not None: remain = stoptime-time.time() if remain <= 0: break if stop_callback and stop_callback(): break try: p = s.recv(MTU) except PcapTimeoutElapsed: continue if p is None: break if lfilter and not lfilter(p): continue if store: lst.append(p) c += 1 if prn: r = prn(p) if r is not None: print(r) if count > 0 and c >= count: break except KeyboardInterrupt: break s.close() return plist.PacketList(lst,"Sniffed")
Sniff packets sniff([count=0,] [prn=None,] [store=1,] [offline=None,] [lfilter=None,] + L2ListenSocket args) -> list of packets Select interface to sniff by setting conf.iface. Use show_interfaces() to see interface names. count: number of packets to capture. 0 means infinity store: wether to store sniffed packets or discard them prn: function to apply to each packet. If something is returned, it is displayed. Ex: ex: prn = lambda x: x.summary() lfilter: python function applied to each packet to determine if further action may be done ex: lfilter = lambda x: x.haslayer(Padding) offline: pcap file to read packets from, instead of sniffing them timeout: stop sniffing after a given time (default: None) L2socket: use the provided L2socket stop_callback: Call every loop to determine if we need to stop the capture
def norm(x, mu, sigma=1.0): """ Scipy norm function """ return stats.norm(loc=mu, scale=sigma).pdf(x)
Scipy norm function
def request(self, method, url, params=None, **aio_kwargs): """Make a request to provider.""" oparams = { 'oauth_consumer_key': self.consumer_key, 'oauth_nonce': sha1(str(RANDOM()).encode('ascii')).hexdigest(), 'oauth_signature_method': self.signature.name, 'oauth_timestamp': str(int(time.time())), 'oauth_version': self.version, } oparams.update(params or {}) if self.oauth_token: oparams['oauth_token'] = self.oauth_token url = self._get_url(url) if urlsplit(url).query: raise ValueError( 'Request parameters should be in the "params" parameter, ' 'not inlined in the URL') oparams['oauth_signature'] = self.signature.sign( self.consumer_secret, method, url, oauth_token_secret=self.oauth_token_secret, **oparams) self.logger.debug("%s %s", url, oparams) return self._request(method, url, params=oparams, **aio_kwargs)
Make a request to provider.
def encode(self): """ Just iterate over the child elements and append them to the current element :return: the encoded element :rtype: xml.etree.ElementTree.Element """ element = ElementTree.Element( self.name, attrib={'type': FieldConstants.ARRAY}, ) for item in self.value: element.append(item.encode()) return element
Just iterate over the child elements and append them to the current element :return: the encoded element :rtype: xml.etree.ElementTree.Element
def updatewhere(clas,pool_or_cursor,where_keys,**update_keys): "this doesn't allow raw_keys for now" # if clas.JSONFIELDS: raise NotImplementedError # todo(awinter): do I need to make the same change for SpecialField? if not where_keys or not update_keys: raise ValueError setclause=','.join(k+'=%s' for k in update_keys) whereclause=' and '.join(eqexpr(k,v) for k,v in where_keys.items()) q='update %s set %s where %s'%(clas.TABLE,setclause,whereclause) vals = tuple(update_keys.values()+where_keys.values()) commit_or_execute(pool_or_cursor,q,vals)
this doesn't allow raw_keys for now
def set_idlesleep(self, idlesleep): """ Sets CPU idle sleep time value. :param idlesleep: idle sleep value (integer) """ is_running = yield from self.is_running() if is_running: # router is running yield from self._hypervisor.send('vm set_idle_sleep_time "{name}" 0 {idlesleep}'.format(name=self._name, idlesleep=idlesleep)) log.info('Router "{name}" [{id}]: idlesleep updated from {old_idlesleep} to {new_idlesleep}'.format(name=self._name, id=self._id, old_idlesleep=self._idlesleep, new_idlesleep=idlesleep)) self._idlesleep = idlesleep
Sets CPU idle sleep time value. :param idlesleep: idle sleep value (integer)
def _check_above_value_float(string, minimum): """ Checks that the number parsed from the string is above a minimum. This is used on compulsory numeric fields. If the value is not above the minimum an exception is thrown. :param string: the field value :param minimum: minimum value """ value = float(string) if value < minimum: message = 'The Numeric Field value should be above %s' % minimum raise pp.ParseException(message)
Checks that the number parsed from the string is above a minimum. This is used on compulsory numeric fields. If the value is not above the minimum an exception is thrown. :param string: the field value :param minimum: minimum value
def generic(self, input_string, **kwargs): """ return a generic filename for a given dataset and component """ kwargs_copy = self.base_dict.copy() kwargs_copy.update(**kwargs) kwargs_copy['dataset'] = kwargs.get('dataset', self.dataset(**kwargs)) kwargs_copy['component'] = kwargs.get( 'component', self.component(**kwargs)) self._replace_none(kwargs_copy) return input_string.format(**kwargs_copy)
return a generic filename for a given dataset and component
def visualize_conv_activations(activation, name): """Visualize activations for convolution layers. Remarks: This tries to place all activations into a square. Args: activation: tensor with the activation [B,H,W,C] name: label for tensorboard Returns: image of almost all activations """ import math with tf.name_scope('visualize_act_' + name): _, h, w, c = activation.get_shape().as_list() rows = [] c_per_row = int(math.sqrt(c)) for y in range(0, c - c_per_row, c_per_row): row = activation[:, :, :, y:y + c_per_row] # [?, H, W, 32] --> [?, H, W, 5] cols = tf.unstack(row, axis=3) # [?, H, W, 5] --> 5 * [?, H, W] row = tf.concat(cols, 1) rows.append(row) viz = tf.concat(rows, 2) tf.summary.image('visualize_act_' + name, tf.expand_dims(viz, -1))
Visualize activations for convolution layers. Remarks: This tries to place all activations into a square. Args: activation: tensor with the activation [B,H,W,C] name: label for tensorboard Returns: image of almost all activations
def parse_xml(self, node): """ Parse an Object from ElementTree xml node :param node: ElementTree xml node :return: self """ def read_points(text): """parse a text string of float tuples and return [(x,...),...] """ return tuple(tuple(map(float, i.split(','))) for i in text.split()) self._set_properties(node) # correctly handle "tile objects" (object with gid set) if self.gid: self.gid = self.parent.register_gid(self.gid) points = None polygon = node.find('polygon') if polygon is not None: points = read_points(polygon.get('points')) self.closed = True polyline = node.find('polyline') if polyline is not None: points = read_points(polyline.get('points')) self.closed = False if points: x1 = x2 = y1 = y2 = 0 for x, y in points: if x < x1: x1 = x if x > x2: x2 = x if y < y1: y1 = y if y > y2: y2 = y self.width = abs(x1) + abs(x2) self.height = abs(y1) + abs(y2) self.points = tuple( [(i[0] + self.x, i[1] + self.y) for i in points]) return self
Parse an Object from ElementTree xml node :param node: ElementTree xml node :return: self
def lfprob (dfnum, dfden, F): """ Returns the (1-tailed) significance level (p-value) of an F statistic given the degrees of freedom for the numerator (dfR-dfF) and the degrees of freedom for the denominator (dfF). Usage: lfprob(dfnum, dfden, F) where usually dfnum=dfbn, dfden=dfwn """ p = betai(0.5*dfden, 0.5*dfnum, dfden/float(dfden+dfnum*F)) return p
Returns the (1-tailed) significance level (p-value) of an F statistic given the degrees of freedom for the numerator (dfR-dfF) and the degrees of freedom for the denominator (dfF). Usage: lfprob(dfnum, dfden, F) where usually dfnum=dfbn, dfden=dfwn
def _compute_surface_areas(self, cell_ids): """For each edge, one half of the the edge goes to each of the end points. Used for Neumann boundary conditions if on the boundary of the mesh and transition conditions if in the interior. """ # Each of the three edges may contribute to the surface areas of all # three vertices. Here, only the two adjacent nodes receive a # contribution, but other approaches (e.g., the flat cell corrector), # may contribute to all three nodes. cn = self.cells["nodes"][cell_ids] ids = numpy.stack([cn, cn, cn], axis=1) half_el = 0.5 * self.edge_lengths[..., cell_ids] zero = numpy.zeros([half_el.shape[1]]) vals = numpy.stack( [ numpy.column_stack([zero, half_el[0], half_el[0]]), numpy.column_stack([half_el[1], zero, half_el[1]]), numpy.column_stack([half_el[2], half_el[2], zero]), ], axis=1, ) return ids, vals
For each edge, one half of the the edge goes to each of the end points. Used for Neumann boundary conditions if on the boundary of the mesh and transition conditions if in the interior.
def _readintle(self, length, start): """Read bits and interpret as a little-endian signed int.""" ui = self._readuintle(length, start) if not ui >> (length - 1): # Top bit not set, number is positive return ui # Top bit is set, so number is negative tmp = (~(ui - 1)) & ((1 << length) - 1) return -tmp
Read bits and interpret as a little-endian signed int.
def start(self): """ Start trapping WINCH signals and resizing the PTY. This method saves the previous WINCH handler so it can be restored on `stop()`. """ def handle(signum, frame): if signum == signal.SIGWINCH: self.pty.resize() self.original_handler = signal.signal(signal.SIGWINCH, handle)
Start trapping WINCH signals and resizing the PTY. This method saves the previous WINCH handler so it can be restored on `stop()`.
def reassemble(self, blueprint, fields, documents): """ Reassemble the given set of fields for a list of pre-assembed documents. NOTE: Reassembly is done in place, since the data you send the method should be JSON type safe, if you need to retain the existing document it is recommended that you copy them using `copy.deepcopy`. """ # Reset the blueprint blueprint.reset() # Reassemble the documents for document in documents: blueprint.reassemble(fields, document)
Reassemble the given set of fields for a list of pre-assembed documents. NOTE: Reassembly is done in place, since the data you send the method should be JSON type safe, if you need to retain the existing document it is recommended that you copy them using `copy.deepcopy`.
def as_variable(obj, name=None) -> 'Union[Variable, IndexVariable]': """Convert an object into a Variable. Parameters ---------- obj : object Object to convert into a Variable. - If the object is already a Variable, return a shallow copy. - Otherwise, if the object has 'dims' and 'data' attributes, convert it into a new Variable. - If all else fails, attempt to convert the object into a Variable by unpacking it into the arguments for creating a new Variable. name : str, optional If provided: - `obj` can be a 1D array, which is assumed to label coordinate values along a dimension of this given name. - Variables with name matching one of their dimensions are converted into `IndexVariable` objects. Returns ------- var : Variable The newly created variable. """ from .dataarray import DataArray # TODO: consider extending this method to automatically handle Iris and if isinstance(obj, DataArray): # extract the primary Variable from DataArrays obj = obj.variable if isinstance(obj, Variable): obj = obj.copy(deep=False) elif isinstance(obj, tuple): try: obj = Variable(*obj) except (TypeError, ValueError) as error: # use .format() instead of % because it handles tuples consistently raise error.__class__('Could not convert tuple of form ' '(dims, data[, attrs, encoding]): ' '{} to Variable.'.format(obj)) elif utils.is_scalar(obj): obj = Variable([], obj) elif isinstance(obj, (pd.Index, IndexVariable)) and obj.name is not None: obj = Variable(obj.name, obj) elif isinstance(obj, (set, dict)): raise TypeError( "variable %r has invalid type %r" % (name, type(obj))) elif name is not None: data = as_compatible_data(obj) if data.ndim != 1: raise MissingDimensionsError( 'cannot set variable %r with %r-dimensional data ' 'without explicit dimension names. Pass a tuple of ' '(dims, data) instead.' % (name, data.ndim)) obj = Variable(name, data, fastpath=True) else: raise TypeError('unable to convert object into a variable without an ' 'explicit list of dimensions: %r' % obj) if name is not None and name in obj.dims: # convert the Variable into an Index if obj.ndim != 1: raise MissingDimensionsError( '%r has more than 1-dimension and the same name as one of its ' 'dimensions %r. xarray disallows such variables because they ' 'conflict with the coordinates used to label ' 'dimensions.' % (name, obj.dims)) obj = obj.to_index_variable() return obj
Convert an object into a Variable. Parameters ---------- obj : object Object to convert into a Variable. - If the object is already a Variable, return a shallow copy. - Otherwise, if the object has 'dims' and 'data' attributes, convert it into a new Variable. - If all else fails, attempt to convert the object into a Variable by unpacking it into the arguments for creating a new Variable. name : str, optional If provided: - `obj` can be a 1D array, which is assumed to label coordinate values along a dimension of this given name. - Variables with name matching one of their dimensions are converted into `IndexVariable` objects. Returns ------- var : Variable The newly created variable.
def _run_amber(paired, work_dir, lenient=False): """AMBER: calculate allele frequencies at likely heterozygous sites. lenient flag allows amber runs on small test sets. """ amber_dir = utils.safe_makedir(os.path.join(work_dir, "amber")) out_file = os.path.join(amber_dir, "%s.amber.baf" % dd.get_sample_name(paired.tumor_data)) if not utils.file_exists(out_file) or not utils.file_exists(out_file + ".pcf"): with file_transaction(paired.tumor_data, out_file) as tx_out_file: key = "germline_het_pon" het_bed = tz.get_in(["genome_resources", "variation", key], paired.tumor_data) cmd = ["AMBER"] + _get_jvm_opts(tx_out_file, paired.tumor_data) + \ ["-threads", dd.get_num_cores(paired.tumor_data), "-tumor", dd.get_sample_name(paired.tumor_data), "-tumor_bam", dd.get_align_bam(paired.tumor_data), "-reference", dd.get_sample_name(paired.normal_data), "-reference_bam", dd.get_align_bam(paired.normal_data), "-ref_genome", dd.get_ref_file(paired.tumor_data), "-bed", het_bed, "-output_dir", os.path.dirname(tx_out_file)] if lenient: cmd += ["-max_het_af_percent", "1.0"] try: do.run(cmd, "PURPLE: AMBER baf generation") except subprocess.CalledProcessError as msg: if not lenient and _amber_allowed_errors(str(msg)): return _run_amber(paired, work_dir, True) for f in os.listdir(os.path.dirname(tx_out_file)): if f != os.path.basename(tx_out_file): shutil.move(os.path.join(os.path.dirname(tx_out_file), f), os.path.join(amber_dir, f)) return out_file
AMBER: calculate allele frequencies at likely heterozygous sites. lenient flag allows amber runs on small test sets.
def bfd(self, **kwargs): """Configure BFD for Interface. Args: name (str): name of the interface to configure (230/0/1 etc) int_type (str): interface type (gigabitethernet etc) tx (str): BFD transmit interval in milliseconds (300, 500, etc) rx (str): BFD receive interval in milliseconds (300, 500, etc) multiplier (str): BFD multiplier. (3, 7, 5, etc) delete (bool): True if BFD configuration should be deleted. Default value will be False if not specified. get (bool): Get config instead of editing config. (True, False) callback (function): A function executed upon completion of the method. The only parameter passed to `callback` will be the ``ElementTree`` `config`. Returns: Return value of `callback`. Raises: KeyError: if `tx`, `rx`, or `multiplier` is not passed. Examples: >>> import pynos.device >>> switches = ['10.24.39.230'] >>> auth = ('admin', 'password') >>> for switch in switches: ... conn = (switch, '22') ... with pynos.device.Device(conn=conn, auth=auth) as dev: ... output = dev.interface.bfd(name='230/0/4', rx='300', ... tx='300', multiplier='3', int_type='tengigabitethernet') ... output = dev.interface.bfd(name='230/0/4', rx='300', ... tx='300', multiplier='3', ... int_type='tengigabitethernet', get=True) ... output = dev.interface.bfd(name='230/0/4', rx='300', ... tx='300', multiplier='3', ... int_type='tengigabitethernet', delete=True) """ int_type = str(kwargs.pop('int_type').lower()) kwargs['name'] = str(kwargs.pop('name')) kwargs['min_tx'] = kwargs.pop('tx') kwargs['min_rx'] = kwargs.pop('rx') kwargs['delete'] = kwargs.pop('delete', False) callback = kwargs.pop('callback', self._callback) valid_int_types = ['gigabitethernet', 'tengigabitethernet', 'fortygigabitethernet', 'hundredgigabitethernet'] if int_type not in valid_int_types: raise ValueError('int_type must be one of: %s' % repr(valid_int_types)) kwargs['int_type'] = int_type bfd_tx = self._bfd_tx(**kwargs) bfd_rx = self._bfd_rx(**kwargs) bfd_multiplier = self._bfd_multiplier(**kwargs) if kwargs.pop('get', False): return self._get_bfd(bfd_tx, bfd_rx, bfd_multiplier) config = pynos.utilities.merge_xml(bfd_tx, bfd_rx) config = pynos.utilities.merge_xml(config, bfd_multiplier) return callback(config)
Configure BFD for Interface. Args: name (str): name of the interface to configure (230/0/1 etc) int_type (str): interface type (gigabitethernet etc) tx (str): BFD transmit interval in milliseconds (300, 500, etc) rx (str): BFD receive interval in milliseconds (300, 500, etc) multiplier (str): BFD multiplier. (3, 7, 5, etc) delete (bool): True if BFD configuration should be deleted. Default value will be False if not specified. get (bool): Get config instead of editing config. (True, False) callback (function): A function executed upon completion of the method. The only parameter passed to `callback` will be the ``ElementTree`` `config`. Returns: Return value of `callback`. Raises: KeyError: if `tx`, `rx`, or `multiplier` is not passed. Examples: >>> import pynos.device >>> switches = ['10.24.39.230'] >>> auth = ('admin', 'password') >>> for switch in switches: ... conn = (switch, '22') ... with pynos.device.Device(conn=conn, auth=auth) as dev: ... output = dev.interface.bfd(name='230/0/4', rx='300', ... tx='300', multiplier='3', int_type='tengigabitethernet') ... output = dev.interface.bfd(name='230/0/4', rx='300', ... tx='300', multiplier='3', ... int_type='tengigabitethernet', get=True) ... output = dev.interface.bfd(name='230/0/4', rx='300', ... tx='300', multiplier='3', ... int_type='tengigabitethernet', delete=True)
def UpdateHuntObject(self, hunt_id, start_time=None, **kwargs): """Updates the hunt object by applying the update function.""" hunt_obj = self.ReadHuntObject(hunt_id) delta_suffix = "_delta" for k, v in kwargs.items(): if v is None: continue if k.endswith(delta_suffix): key = k[:-len(delta_suffix)] current_value = getattr(hunt_obj, key) setattr(hunt_obj, key, current_value + v) else: setattr(hunt_obj, k, v) if start_time is not None: hunt_obj.init_start_time = hunt_obj.init_start_time or start_time hunt_obj.last_start_time = start_time hunt_obj.last_update_time = rdfvalue.RDFDatetime.Now() self.hunts[hunt_obj.hunt_id] = hunt_obj
Updates the hunt object by applying the update function.
def _fix_logging_shortcuts(cls): """ Fix standard logging shortcuts to correctly report logging module. This is only useful if you: - actually use %(name) and care about it being correct - you would still like to use the logging.info() etc shortcuts So basically you'd like to write this: import logging logging.info("hello") Instead of this: import logging LOG = logging.getLogger(__name__) LOG.info("hello") """ if cls.is_using_format("%(pathname)s %(filename)s %(funcName)s %(module)s"): logging._srcfile = cls._logging_snapshot._srcfile else: logging._srcfile = None logging.logProcesses = cls.is_using_format("%(process)d") logging.logThreads = cls.is_using_format("%(thread)d %(threadName)s") def getframe(): return sys._getframe(4) def log(level, msg, *args, **kwargs): """Wrapper to make logging.info() etc report the right module %(name)""" name = get_caller_name() logger = logging.getLogger(name) try: logging.currentframe = getframe logger.log(level, msg, *args, **kwargs) finally: logging.currentframe = ORIGINAL_CF def wrap(level, **kwargs): """Wrap corresponding logging shortcut function""" original = getattr(logging, logging.getLevelName(level).lower()) f = partial(log, level, **kwargs) f.__doc__ = original.__doc__ return f logging.critical = wrap(logging.CRITICAL) logging.fatal = logging.critical logging.error = wrap(logging.ERROR) logging.exception = partial(logging.error, exc_info=True) logging.warning = wrap(logging.WARNING) logging.info = wrap(logging.INFO) logging.debug = wrap(logging.DEBUG) logging.log = log
Fix standard logging shortcuts to correctly report logging module. This is only useful if you: - actually use %(name) and care about it being correct - you would still like to use the logging.info() etc shortcuts So basically you'd like to write this: import logging logging.info("hello") Instead of this: import logging LOG = logging.getLogger(__name__) LOG.info("hello")
def _save_files(self, data, dtype_out_time): """Save the data to netcdf files in direc_out.""" path = self.path_out[dtype_out_time] if not os.path.isdir(self.dir_out): os.makedirs(self.dir_out) if 'reg' in dtype_out_time: try: reg_data = xr.open_dataset(path) except (EOFError, RuntimeError, IOError): reg_data = xr.Dataset() reg_data.update(data) data_out = reg_data else: data_out = data if isinstance(data_out, xr.DataArray): data_out = xr.Dataset({self.name: data_out}) data_out.to_netcdf(path, engine='netcdf4', format='NETCDF3_64BIT')
Save the data to netcdf files in direc_out.
def calculate_v(nfs): """Calculates V(n+1/n) values. Useful for establishing the quality of your normalization regime. See Vandesompele 2002 for advice on interpretation. :param DataFrame nfs: A matrix of all normalization factors, produced by `calculate_all_nfs`. :return: a Series of values [V(2/1), V(3/2), V(4/3), ...]. """ v = [] if (nfs.columns != range(1, nfs.columns[-1]+1)).any(): raise ValueError("Column names invalid in nf_v_frame") for i in nfs.columns[:-1]: v.append(std(log2(nfs[i]/nfs[i+1]), ddof=1)) return pd.Series(v, index=nfs.columns[:-1])
Calculates V(n+1/n) values. Useful for establishing the quality of your normalization regime. See Vandesompele 2002 for advice on interpretation. :param DataFrame nfs: A matrix of all normalization factors, produced by `calculate_all_nfs`. :return: a Series of values [V(2/1), V(3/2), V(4/3), ...].
def from_config(config): """ Creates new Parameters from ConfigMap object. :param config: a ConfigParams that contain parameters. :return: a new Parameters object. """ result = Parameters() if config == None or len(config) == 0: return result for (key, value) in config.items(): result.put(key, value) return result
Creates new Parameters from ConfigMap object. :param config: a ConfigParams that contain parameters. :return: a new Parameters object.
def preprocess(*_unused, **processors): """ Decorator that applies pre-processors to the arguments of a function before calling the function. Parameters ---------- **processors : dict Map from argument name -> processor function. A processor function takes three arguments: (func, argname, argvalue). `func` is the the function for which we're processing args. `argname` is the name of the argument we're processing. `argvalue` is the value of the argument we're processing. Examples -------- >>> def _ensure_tuple(func, argname, arg): ... if isinstance(arg, tuple): ... return argvalue ... try: ... return tuple(arg) ... except TypeError: ... raise TypeError( ... "%s() expected argument '%s' to" ... " be iterable, but got %s instead." % ( ... func.__name__, argname, arg, ... ) ... ) ... >>> @preprocess(arg=_ensure_tuple) ... def foo(arg): ... return arg ... >>> foo([1, 2, 3]) (1, 2, 3) >>> foo("a") ('a',) >>> foo(2) Traceback (most recent call last): ... TypeError: foo() expected argument 'arg' to be iterable, but got 2 instead. """ if _unused: raise TypeError("preprocess() doesn't accept positional arguments") def _decorator(f): args, varargs, varkw, defaults = argspec = getargspec(f) if defaults is None: defaults = () no_defaults = (NO_DEFAULT,) * (len(args) - len(defaults)) args_defaults = list(zip(args, no_defaults + defaults)) if varargs: args_defaults.append((varargs, NO_DEFAULT)) if varkw: args_defaults.append((varkw, NO_DEFAULT)) argset = set(args) | {varargs, varkw} - {None} # Arguments can be declared as tuples in Python 2. if not all(isinstance(arg, str) for arg in args): raise TypeError( "Can't validate functions using tuple unpacking: %s" % (argspec,) ) # Ensure that all processors map to valid names. bad_names = viewkeys(processors) - argset if bad_names: raise TypeError( "Got processors for unknown arguments: %s." % bad_names ) return _build_preprocessed_function( f, processors, args_defaults, varargs, varkw, ) return _decorator
Decorator that applies pre-processors to the arguments of a function before calling the function. Parameters ---------- **processors : dict Map from argument name -> processor function. A processor function takes three arguments: (func, argname, argvalue). `func` is the the function for which we're processing args. `argname` is the name of the argument we're processing. `argvalue` is the value of the argument we're processing. Examples -------- >>> def _ensure_tuple(func, argname, arg): ... if isinstance(arg, tuple): ... return argvalue ... try: ... return tuple(arg) ... except TypeError: ... raise TypeError( ... "%s() expected argument '%s' to" ... " be iterable, but got %s instead." % ( ... func.__name__, argname, arg, ... ) ... ) ... >>> @preprocess(arg=_ensure_tuple) ... def foo(arg): ... return arg ... >>> foo([1, 2, 3]) (1, 2, 3) >>> foo("a") ('a',) >>> foo(2) Traceback (most recent call last): ... TypeError: foo() expected argument 'arg' to be iterable, but got 2 instead.
def script_file(self): """ Returns the startup script file for this VPCS VM. :returns: path to startup script file """ # use the default VPCS file if it exists path = os.path.join(self.working_dir, 'startup.vpc') if os.path.exists(path): return path else: return None
Returns the startup script file for this VPCS VM. :returns: path to startup script file
def delete_items(self, url, container, container_object=None): """Deletes an objects in a container. :param url: :param container: """ headers, container_uri = self._return_base_data( url=url, container=container, container_object=container_object ) return self._deleter(uri=container_uri, headers=headers)
Deletes an objects in a container. :param url: :param container:
def _add_conflicting_arguments(self): """It's too dangerous to use `-y` and `-r` together.""" group = self._parser.add_mutually_exclusive_group() group.add_argument( '-y', '--yes', '--yeah', action='store_true', help='execute fixed command without confirmation') group.add_argument( '-r', '--repeat', action='store_true', help='repeat on failure')
It's too dangerous to use `-y` and `-r` together.
def offering(self): """ Deprecated. Use course and run independently. """ warnings.warn( "Offering is no longer a supported property of Locator. Please use the course and run properties.", DeprecationWarning, stacklevel=2 ) if not self.course and not self.run: return None elif not self.run and self.course: return self.course return "/".join([self.course, self.run])
Deprecated. Use course and run independently.
def num_nodes(tree): """Determine the number of nodes in a tree""" if tree.is_leaf: return 1 else: return 1 + num_nodes(tree.left_child) + num_nodes(tree.right_child)
Determine the number of nodes in a tree
def reply(self, user, msg, errors_as_replies=True): """Fetch a reply from the RiveScript brain. Arguments: user (str): A unique user ID for the person requesting a reply. This could be e.g. a screen name or nickname. It's used internally to store user variables (including topic and history), so if your bot has multiple users each one should have a unique ID. msg (str): The user's message. This is allowed to contain punctuation and such, but any extraneous data such as HTML tags should be removed in advance. errors_as_replies (bool): When errors are encountered (such as a deep recursion error, no reply matched, etc.) this will make the reply be a text representation of the error message. If you set this to ``False``, errors will instead raise an exception, such as a ``DeepRecursionError`` or ``NoReplyError``. By default, no exceptions are raised and errors are set in the reply instead. Returns: str: The reply output. """ return self._brain.reply(user, msg, errors_as_replies)
Fetch a reply from the RiveScript brain. Arguments: user (str): A unique user ID for the person requesting a reply. This could be e.g. a screen name or nickname. It's used internally to store user variables (including topic and history), so if your bot has multiple users each one should have a unique ID. msg (str): The user's message. This is allowed to contain punctuation and such, but any extraneous data such as HTML tags should be removed in advance. errors_as_replies (bool): When errors are encountered (such as a deep recursion error, no reply matched, etc.) this will make the reply be a text representation of the error message. If you set this to ``False``, errors will instead raise an exception, such as a ``DeepRecursionError`` or ``NoReplyError``. By default, no exceptions are raised and errors are set in the reply instead. Returns: str: The reply output.
def get_file_from_s3(job, s3_url, encryption_key=None, write_to_jobstore=True): """ Downloads a supplied URL that points to an unencrypted, unprotected file on Amazon S3. The file is downloaded and a subsequently written to the jobstore and the return value is a the path to the file in the jobstore. """ work_dir = job.fileStore.getLocalTempDir() filename = '/'.join([work_dir, os.path.basename(s3_url)]) # This is common to encrypted and unencrypted downloads download_call = ['curl', '-fs', '--retry', '5'] # If an encryption key was provided, use it to create teh headers that need to be injected into # the curl script and append to the call if encryption_key: key = generate_unique_key(encryption_key, s3_url) encoded_key = base64.b64encode(key) encoded_key_md5 = base64.b64encode( hashlib.md5(key).digest() ) h1 = 'x-amz-server-side-encryption-customer-algorithm:AES256' h2 = 'x-amz-server-side-encryption-customer-key:{}'.format(encoded_key) h3 = 'x-amz-server-side-encryption-customer-key-md5:{}'.format(encoded_key_md5) download_call.extend(['-H', h1, '-H', h2, '-H', h3]) # This is also common to both types of downloads download_call.extend([s3_url, '-o', filename]) try: subprocess.check_call(download_call) except subprocess.CalledProcessError: raise RuntimeError('Curl returned a non-zero exit status processing %s. Do you' % s3_url + 'have premssions to access the file?') except OSError: raise RuntimeError('Failed to find "curl". Install via "apt-get install curl"') assert os.path.exists(filename) if write_to_jobstore: filename = job.fileStore.writeGlobalFile(filename) return filename
Downloads a supplied URL that points to an unencrypted, unprotected file on Amazon S3. The file is downloaded and a subsequently written to the jobstore and the return value is a the path to the file in the jobstore.
def write_out(self, output): """Banana banana """ for page in self.walk(): ext = self.project.extensions[page.extension_name] ext.write_out_page(output, page)
Banana banana
def main(): """ NAME plot_magmap.py DESCRIPTION makes a color contour map of desired field model SYNTAX plot_magmap.py [command line options] OPTIONS -h prints help and quits -f FILE specify field model file with format: l m g h -fmt [pdf,eps,svg,png] specify format for output figure (default is png) -mod [arch3k,cals3k,pfm9k,hfm10k,cals10k_2,shadif14k,cals10k] specify model for 3ka to 1900 CE, default is cals10k -alt ALT; specify altitude in km, default is sealevel (0) -age specify date in decimal year, default is 2016 -lon0: 0 longitude for map, default is 0 -el: [D,I,B,Br] specify element for plotting -cm: [see https://matplotlib.org/users/colormaps.html] specify color map for plotting (default is RdYlBu) """ cmap = 'RdYlBu' date = 2016. if not ccrs: print("-W- You must intstall the cartopy module to run plot_magmap.py") sys.exit() dir_path = '.' lincr = 1 # level increment for contours if '-WD' in sys.argv: ind = sys.argv.index('-WD') dir_path = sys.argv[ind+1] if '-h' in sys.argv: print(main.__doc__) sys.exit() if '-fmt' in sys.argv: ind = sys.argv.index('-fmt') fmt = sys.argv[ind+1] if fmt == 'jpg': print('jpg not a supported option') print(main.__doc__) sys.exit() else: fmt = 'png' if '-cm' in sys.argv: ind = sys.argv.index('-cm') cmap = sys.argv[ind+1] if '-el' in sys.argv: ind = sys.argv.index('-el') el = sys.argv[ind+1] else: el = 'B' if '-alt' in sys.argv: ind = sys.argv.index('-alt') alt = sys.argv[ind+1] else: alt = 0 if '-lon0' in sys.argv: ind = sys.argv.index('-lon0') lon_0 = float(sys.argv[ind+1]) else: lon_0 = 0 if '-mod' in sys.argv: ind = sys.argv.index('-mod') mod = sys.argv[ind+1] ghfile = '' elif '-f' in sys.argv: ind = sys.argv.index('-f') ghfile = sys.argv[ind+1] mod = 'custom' date = '' else: mod, ghfile = 'cals10k', '' if '-age' in sys.argv: ind = sys.argv.index('-age') date = float(sys.argv[ind+1]) if '-alt' in sys.argv: ind = sys.argv.index('-alt') alt = float(sys.argv[ind+1]) else: alt = 0 # doesn't work correctly with mod other than default Ds, Is, Bs, Brs, lons, lats = pmag.do_mag_map( date, mod=mod, lon_0=lon_0, alt=alt, file=ghfile) ax = plt.axes(projection=ccrs.Mollweide(central_longitude=lon_0)) ax.coastlines() xx, yy = meshgrid(lons, lats) if mod == 'custom': str_date = 'Custom' else: str_date = str(date) if el == 'B': levmax = Bs.max()+lincr levmin = round(Bs.min()-lincr) levels = np.arange(levmin, levmax, lincr) plt.contourf(xx, yy, Bs, levels=levels, cmap=cmap, transform=ccrs.PlateCarree(central_longitude=lon_0)) plt.title('Field strength ($\mu$T): '+ str_date) if el == 'Br': levmax = Brs.max()+lincr levmin = round(Brs.min()-lincr) plt.contourf(xx, yy, Brs, levels=np.arange(levmin, levmax, lincr), cmap=cmap, transform=ccrs.PlateCarree(central_longitude=lon_0)) plt.title('Radial field strength ($\mu$T): '+ str_date) if el == 'I': levmax = Is.max()+lincr levmin = round(Is.min()-lincr) plt.contourf(xx, yy, Is, levels=np.arange(levmin, levmax, lincr), cmap=cmap, transform=ccrs.PlateCarree(central_longitude=lon_0)) plt.contour(xx, yy, Is, levels=np.arange(-80, 90, 10), colors='black', transform=ccrs.PlateCarree(central_longitude=lon_0)) plt.title('Field inclination: '+ str_date) if el == 'D': plt.contourf(xx, yy, Ds, levels=np.arange(-180, 180, 10), cmap=cmap, transform=ccrs.PlateCarree(central_longitude=lon_0)) plt.contour(xx, yy, Ds, levels=np.arange(-180, 180, 10), colors='black') # cs=m.contourf(x,y,Ds,levels=np.arange(-180,180,10),cmap=cmap) # cs=m.contourf(x,y,Ds,levels=np.arange(-180,180,10),cmap=cmap) # m.contour(x,y,Ds,levels=np.arange(-180,180,10),colors='black') plt.title('Field declination: '+ str_date) cbar = plt.colorbar(orientation='horizontal') figname = 'geomagnetic_field_' + str_date + '.'+fmt plt.savefig(figname) print('Figure saved as: ', figname)
NAME plot_magmap.py DESCRIPTION makes a color contour map of desired field model SYNTAX plot_magmap.py [command line options] OPTIONS -h prints help and quits -f FILE specify field model file with format: l m g h -fmt [pdf,eps,svg,png] specify format for output figure (default is png) -mod [arch3k,cals3k,pfm9k,hfm10k,cals10k_2,shadif14k,cals10k] specify model for 3ka to 1900 CE, default is cals10k -alt ALT; specify altitude in km, default is sealevel (0) -age specify date in decimal year, default is 2016 -lon0: 0 longitude for map, default is 0 -el: [D,I,B,Br] specify element for plotting -cm: [see https://matplotlib.org/users/colormaps.html] specify color map for plotting (default is RdYlBu)
def _cache_translation(translation, timeout=cache.default_timeout): """ Store a new translation in the cache. """ if not appsettings.PARLER_ENABLE_CACHING: return if translation.master_id is None: raise ValueError("Can't cache unsaved translation") # Cache a translation object. # For internal usage, object parameters are not suited for outside usage. fields = translation.get_translated_fields() values = {'id': translation.id} for name in fields: values[name] = getattr(translation, name) key = get_translation_cache_key(translation.__class__, translation.master_id, translation.language_code) cache.set(key, values, timeout=timeout)
Store a new translation in the cache.
def print_results(cls, stdout, stderr): """Print linter results and exits with an error if there's any.""" for line in stderr: print(line, file=sys.stderr) if stdout: if stderr: # blank line to separate stdout from stderr print(file=sys.stderr) cls._print_stdout(stdout) else: print(':) No issues found.')
Print linter results and exits with an error if there's any.
def fuzz_string(seed_str, runs=100, fuzz_factor=50): """A random fuzzer for a simulated text viewer application. It takes a string as seed and generates <runs> variant of it. :param seed_str: the string to use as seed for fuzzing. :param runs: number of fuzzed variants to supply. :param fuzz_factor: degree of fuzzing = 1 / fuzz_factor. :return: list of fuzzed variants of seed_str. :rtype: [str] """ buf = bytearray(seed_str, encoding="utf8") variants = [] for _ in range(runs): fuzzed = fuzzer(buf, fuzz_factor) variants.append(''.join([chr(b) for b in fuzzed])) logger().info('Fuzzed strings: {}'.format(variants)) return variants
A random fuzzer for a simulated text viewer application. It takes a string as seed and generates <runs> variant of it. :param seed_str: the string to use as seed for fuzzing. :param runs: number of fuzzed variants to supply. :param fuzz_factor: degree of fuzzing = 1 / fuzz_factor. :return: list of fuzzed variants of seed_str. :rtype: [str]
def trim(self): """Clear not used counters""" for key, value in list(iteritems(self.counters)): if value.empty(): del self.counters[key]
Clear not used counters
def command(state, args): """Reset anime watched episodes.""" args = parser.parse_args(args[1:]) aid = state.results.parse_aid(args.aid, default_key='db') query.update.reset(state.db, aid, args.episode)
Reset anime watched episodes.
def without(self, *values): """ Return a version of the array that does not contain the specified value(s). """ if self._clean.isDict(): newlist = {} for i, k in enumerate(self.obj): # if k not in values: # use indexof to check identity if _(values).indexOf(k) is -1: newlist.set(k, self.obj[k]) else: newlist = [] for i, v in enumerate(self.obj): # if v not in values: # use indexof to check identity if _(values).indexOf(v) is -1: newlist.append(v) return self._wrap(newlist)
Return a version of the array that does not contain the specified value(s).
def get_changes(self, fixer=str.lower, task_handle=taskhandle.NullTaskHandle()): """Fix module names `fixer` is a function that takes and returns a `str`. Given the name of a module, it should return the fixed name. """ stack = changestack.ChangeStack(self.project, 'Fixing module names') jobset = task_handle.create_jobset('Fixing module names', self._count_fixes(fixer) + 1) try: while True: for resource in self._tobe_fixed(fixer): jobset.started_job(resource.path) renamer = rename.Rename(self.project, resource) changes = renamer.get_changes(fixer(self._name(resource))) stack.push(changes) jobset.finished_job() break else: break finally: jobset.started_job('Reverting to original state') stack.pop_all() jobset.finished_job() return stack.merged()
Fix module names `fixer` is a function that takes and returns a `str`. Given the name of a module, it should return the fixed name.
def get_header_dict(response, header): """ returns a dictionary of the cache control headers the same as is used by django.utils.cache.patch_cache_control if there are no Cache-Control headers returns and empty dict """ def dictitem(s): t = s.split('=', 1) if len(t) > 1: return (t[0].lower(), t[1]) else: return (t[0].lower(), True) if response.has_header(header): hd = dict([dictitem(el) for el in cc_delim_re.split(response[header])]) else: hd= {} return hd
returns a dictionary of the cache control headers the same as is used by django.utils.cache.patch_cache_control if there are no Cache-Control headers returns and empty dict
def _backlog(self, data): """Find all the datagrepper messages between 'then' and 'now'. Put those on our work queue. Should be called in a thread so as not to block the hub at startup. """ try: data = json.loads(data) except ValueError as e: self.log.info("Status contents are %r" % data) self.log.exception(e) self.log.info("Skipping backlog retrieval.") return last = data['message']['body'] if isinstance(last, str): last = json.loads(last) then = last['timestamp'] now = int(time.time()) retrieved = 0 for message in self.get_datagrepper_results(then, now): # Take the messages from datagrepper and remove any keys that were # artificially added to the message. The presence of these would # otherwise cause message crypto validation to fail. message = fedmsg.crypto.utils.fix_datagrepper_message(message) if message['msg_id'] != last['msg_id']: retrieved = retrieved + 1 self.incoming.put(dict(body=message, topic=message['topic'])) else: self.log.warning("Already seen %r; Skipping." % last['msg_id']) self.log.info("Retrieved %i messages from datagrepper." % retrieved)
Find all the datagrepper messages between 'then' and 'now'. Put those on our work queue. Should be called in a thread so as not to block the hub at startup.
def colorize(bg, base, fg, *text): """ colorize(bg, base, fg, *text) """ # All argument types must be str. rtext = [str(f) for f in text] return COLORIZE_FORMAT.format( _to_int(bg), _to_int(base), _to_int(fg), ''.join(rtext) )
colorize(bg, base, fg, *text)
def get_arp_output_arp_entry_interface_name(self, **kwargs): """Auto Generated Code """ config = ET.Element("config") get_arp = ET.Element("get_arp") config = get_arp output = ET.SubElement(get_arp, "output") arp_entry = ET.SubElement(output, "arp-entry") ip_address_key = ET.SubElement(arp_entry, "ip-address") ip_address_key.text = kwargs.pop('ip_address') interface_name = ET.SubElement(arp_entry, "interface-name") interface_name.text = kwargs.pop('interface_name') callback = kwargs.pop('callback', self._callback) return callback(config)
Auto Generated Code
def verify_hash_type(self): ''' Verify and display a nag-messsage to the log if vulnerable hash-type is used. :return: ''' if self.config['hash_type'].lower() in ['md5', 'sha1']: log.warning( 'IMPORTANT: Do not use %s hashing algorithm! Please set ' '"hash_type" to sha256 in Salt %s config!', self.config['hash_type'], self.__class__.__name__ )
Verify and display a nag-messsage to the log if vulnerable hash-type is used. :return:
def set_session(self, autocommit=None, readonly=None): """Sets one or more parameters in the current connection. :param autocommit: Switch the connection to autocommit mode. With the current version, you need to always enable this, because :meth:`commit` is not implemented. :param readonly: Switch the connection to read-only mode. """ props = {} if autocommit is not None: props['autoCommit'] = bool(autocommit) if readonly is not None: props['readOnly'] = bool(readonly) props = self._client.connection_sync(self._id, props) self._autocommit = props.auto_commit self._readonly = props.read_only self._transactionisolation = props.transaction_isolation
Sets one or more parameters in the current connection. :param autocommit: Switch the connection to autocommit mode. With the current version, you need to always enable this, because :meth:`commit` is not implemented. :param readonly: Switch the connection to read-only mode.
def peer_list(self): """ GET /network/peers Use the Network APIs to retrieve information about the network of peer nodes comprising the blockchain network. ```golang message PeersMessage { repeated PeerEndpoint peers = 1; } message PeerEndpoint { PeerID ID = 1; string address = 2; enum Type { UNDEFINED = 0; VALIDATOR = 1; NON_VALIDATOR = 2; } Type type = 3; bytes pkiID = 4; } message PeerID { string name = 1; } ``` :return: json body of the network peers info """ res = self._get(self._url("/network/peers")) return self._result(res, True)
GET /network/peers Use the Network APIs to retrieve information about the network of peer nodes comprising the blockchain network. ```golang message PeersMessage { repeated PeerEndpoint peers = 1; } message PeerEndpoint { PeerID ID = 1; string address = 2; enum Type { UNDEFINED = 0; VALIDATOR = 1; NON_VALIDATOR = 2; } Type type = 3; bytes pkiID = 4; } message PeerID { string name = 1; } ``` :return: json body of the network peers info
def run(pipeline, input_gen, options={}): """ Run a pipeline over a input generator >>> # if we have a simple component >>> from reliure.pipeline import Composable >>> @Composable ... def print_each(letters): ... for letter in letters: ... print(letter) ... yield letter >>> # that we want to run over a given input: >>> input = "abcde" >>> # we just have to do : >>> res = run(print_each, input) a b c d e it is also possible to run any reliure pipeline this way: >>> import string >>> pipeline = Composable(lambda letters: (l.upper() for l in letters)) | print_each >>> res = run(pipeline, input) A B C D E """ logger = logging.getLogger("reliure.run") t0 = time() res = [output for output in pipeline(input_gen, **options)] logger.info("Pipeline executed in %1.3f sec" % (time() - t0)) return res
Run a pipeline over a input generator >>> # if we have a simple component >>> from reliure.pipeline import Composable >>> @Composable ... def print_each(letters): ... for letter in letters: ... print(letter) ... yield letter >>> # that we want to run over a given input: >>> input = "abcde" >>> # we just have to do : >>> res = run(print_each, input) a b c d e it is also possible to run any reliure pipeline this way: >>> import string >>> pipeline = Composable(lambda letters: (l.upper() for l in letters)) | print_each >>> res = run(pipeline, input) A B C D E
def compile(self, compass): """ Calls the compass script specified in the compass extension with the paths provided by the config.rb. """ try: output = subprocess.check_output( [compass.compass_path, 'compile', '-q'], cwd=self.base_dir) os.utime(self.dest, None) compass.log.debug(output) except OSError, e: if e.errno == errno.ENOENT: compass.log.error("Compass could not be found in the PATH " + "and/or in the COMPASS_PATH setting! " + "Disabling compilation.") compass.disabled = True else: raise e
Calls the compass script specified in the compass extension with the paths provided by the config.rb.
def match(self, path, **kw): ''' path - str (urlencoded) ''' m = self._pattern.match(path) if m: kwargs = m.groupdict() # convert params for url_arg_name, value_urlencoded in kwargs.items(): conv_obj = self._url_params[url_arg_name] unicode_value = unquote(value_urlencoded) if isinstance(unicode_value, six.binary_type): # XXX ?? unicode_value = unicode_value.decode('utf-8', 'replace') try: kwargs[url_arg_name] = conv_obj.to_python(unicode_value, **kw) except ConvertError as err: logger.debug('ConvertError in parameter "%s" ' 'by %r, value "%s"', url_arg_name, err.converter.__class__, err.value) return None, {} return m.group(), kwargs return None, {}
path - str (urlencoded)
def tostring(element): """Serialize an element and its child nodes to a string""" rv = [] def serializeElement(element): if not hasattr(element, "tag"): if element.docinfo.internalDTD: if element.docinfo.doctype: dtd_str = element.docinfo.doctype else: dtd_str = "<!DOCTYPE %s>" % element.docinfo.root_name rv.append(dtd_str) serializeElement(element.getroot()) elif element.tag == comment_type: rv.append("<!--%s-->" % (element.text,)) else: # This is assumed to be an ordinary element if not element.attrib: rv.append("<%s>" % (element.tag,)) else: attr = " ".join(["%s=\"%s\"" % (name, value) for name, value in element.attrib.items()]) rv.append("<%s %s>" % (element.tag, attr)) if element.text: rv.append(element.text) for child in element: serializeElement(child) rv.append("</%s>" % (element.tag,)) if hasattr(element, "tail") and element.tail: rv.append(element.tail) serializeElement(element) return "".join(rv)
Serialize an element and its child nodes to a string
def UserAcceptance( matchList, recursiveLookup = True, promptComment = None, promptOnly = False, xStrOverride = "to skip this selection" ): """ Prompt user to select a entry from a given match list or to enter a new string to look up. If the match list is empty user must enter a new string or exit. Parameters ---------- matchList : list A list of entries which the user can select a valid match from. recursiveLookup : boolean [optional: default = True] Allow user to enter a new string to look up. promptComment : string [optional: default = None] Add an additional comment on the end of the prompt message. promptOnly : boolean [optional: default = False] Set to true if match list is expected to be empty. In which case the presence of an empty match list will not be mentioned and user will be expected to enter a new response to look up. xStrOverride : string [optional: default = "to skip this selection"] Override the string for 'x' response. This can be used if the behaviour of the 'x' response is changed. Returns ---------- string or None Either a entry from matchList, another valid response or a new string to look up. If match list is empty and recursive lookup is disabled or if the user response is 'x' this will return None. """ matchString = ', '.join(matchList) if len(matchList) == 1: goodlogging.Log.Info("UTIL", "Match found: {0}".format(matchString)) prompt = "Enter 'y' to accept this match or e" elif len(matchList) > 1: goodlogging.Log.Info("UTIL", "Multiple possible matches found: {0}".format(matchString)) prompt = "Enter correct match from list or e" else: if promptOnly is False: goodlogging.Log.Info("UTIL", "No match found") prompt = "E" if not recursiveLookup: return None if recursiveLookup: prompt = prompt + "nter a different string to look up or e" prompt = prompt + "nter 'x' {0} or enter 'exit' to quit this program".format(xStrOverride) if promptComment is None: prompt = prompt + ": " else: prompt = prompt + " ({0}): ".format(promptComment) while(1): response = goodlogging.Log.Input('UTIL', prompt) if response.lower() == 'exit': goodlogging.Log.Fatal("UTIL", "Program terminated by user 'exit'") if response.lower() == 'x': return None elif response.lower() == 'y' and len(matchList) == 1: return matchList[0] elif len(matchList) > 1: for match in matchList: if response.lower() == match.lower(): return match if recursiveLookup: return response
Prompt user to select a entry from a given match list or to enter a new string to look up. If the match list is empty user must enter a new string or exit. Parameters ---------- matchList : list A list of entries which the user can select a valid match from. recursiveLookup : boolean [optional: default = True] Allow user to enter a new string to look up. promptComment : string [optional: default = None] Add an additional comment on the end of the prompt message. promptOnly : boolean [optional: default = False] Set to true if match list is expected to be empty. In which case the presence of an empty match list will not be mentioned and user will be expected to enter a new response to look up. xStrOverride : string [optional: default = "to skip this selection"] Override the string for 'x' response. This can be used if the behaviour of the 'x' response is changed. Returns ---------- string or None Either a entry from matchList, another valid response or a new string to look up. If match list is empty and recursive lookup is disabled or if the user response is 'x' this will return None.
def moresane_by_scale(self, start_scale=1, stop_scale=20, subregion=None, sigma_level=4, loop_gain=0.1, tolerance=0.75, accuracy=1e-6, major_loop_miter=100, minor_loop_miter=30, all_on_gpu=False, decom_mode="ser", core_count=1, conv_device='cpu', conv_mode='linear', extraction_mode='cpu', enforce_positivity=False, edge_suppression=False, edge_offset=0, flux_threshold=0, neg_comp=False, edge_excl=0, int_excl=0): """ Extension of the MORESANE algorithm. This takes a scale-by-scale approach, attempting to remove all sources at the lower scales before moving onto the higher ones. At each step the algorithm may return to previous scales to remove the sources uncovered by the deconvolution. INPUTS: start_scale (default=1) The first scale which is to be considered. stop_scale (default=20) The maximum scale which is to be considered. Optional. subregion (default=None): Size, in pixels, of the central region to be analyzed and deconvolved. sigma_level (default=4) Number of sigma at which thresholding is to be performed. loop_gain (default=0.1): Loop gain for the deconvolution. tolerance (default=0.75): Tolerance level for object extraction. Significant objects contain wavelet coefficients greater than the tolerance multiplied by the maximum wavelet coefficient in the scale under consideration. accuracy (default=1e-6): Threshold on the standard deviation of the residual noise. Exit main loop when this threshold is reached. major_loop_miter (default=100): Maximum number of iterations allowed in the major loop. Exit condition. minor_loop_miter (default=30): Maximum number of iterations allowed in the minor loop. Serves as an exit condition when the SNR does not reach a maximum. all_on_gpu (default=False): Boolean specifier to toggle all gpu modes on. decom_mode (default='ser'): Specifier for decomposition mode - serial, multiprocessing, or gpu. core_count (default=1): In the event that multiprocessing, specifies the number of cores. conv_device (default='cpu'): Specifier for device to be used - cpu or gpu. conv_mode (default='linear'): Specifier for convolution mode - linear or circular. extraction_mode (default='cpu'): Specifier for mode to be used - cpu or gpu. enforce_positivity (default=False): Boolean specifier for whether or not a model must be strictly positive. edge_suppression (default=False): Boolean specifier for whether or not the edges are to be suprressed. edge_offset (default=0): Numeric value for an additional user-specified number of edge pixels to be ignored. This is added to the minimum suppression. OUTPUTS: self.model (no default): Model extracted by the algorithm. self.residual (no default): Residual signal after deconvolution. """ # The following preserves the dirty image as it will be changed on every iteration. dirty_data = self.dirty_data scale_count = start_scale while not (self.complete): logger.info("MORESANE at scale {}".format(scale_count)) self.moresane(subregion=subregion, scale_count=scale_count, sigma_level=sigma_level, loop_gain=loop_gain, tolerance=tolerance, accuracy=accuracy, major_loop_miter=major_loop_miter, minor_loop_miter=minor_loop_miter, all_on_gpu=all_on_gpu, decom_mode=decom_mode, core_count=core_count, conv_device=conv_device, conv_mode=conv_mode, extraction_mode=extraction_mode, enforce_positivity=enforce_positivity, edge_suppression=edge_suppression, edge_offset=edge_offset, flux_threshold=flux_threshold, neg_comp=neg_comp, edge_excl=edge_excl, int_excl=int_excl) self.dirty_data = self.residual scale_count += 1 if (scale_count>(np.log2(self.dirty_data.shape[0]))-1): logger.info("Maximum scale reached - finished.") break if (scale_count>stop_scale): logger.info("Maximum scale reached - finished.") break # Restores the original dirty image. self.dirty_data = dirty_data self.complete = False
Extension of the MORESANE algorithm. This takes a scale-by-scale approach, attempting to remove all sources at the lower scales before moving onto the higher ones. At each step the algorithm may return to previous scales to remove the sources uncovered by the deconvolution. INPUTS: start_scale (default=1) The first scale which is to be considered. stop_scale (default=20) The maximum scale which is to be considered. Optional. subregion (default=None): Size, in pixels, of the central region to be analyzed and deconvolved. sigma_level (default=4) Number of sigma at which thresholding is to be performed. loop_gain (default=0.1): Loop gain for the deconvolution. tolerance (default=0.75): Tolerance level for object extraction. Significant objects contain wavelet coefficients greater than the tolerance multiplied by the maximum wavelet coefficient in the scale under consideration. accuracy (default=1e-6): Threshold on the standard deviation of the residual noise. Exit main loop when this threshold is reached. major_loop_miter (default=100): Maximum number of iterations allowed in the major loop. Exit condition. minor_loop_miter (default=30): Maximum number of iterations allowed in the minor loop. Serves as an exit condition when the SNR does not reach a maximum. all_on_gpu (default=False): Boolean specifier to toggle all gpu modes on. decom_mode (default='ser'): Specifier for decomposition mode - serial, multiprocessing, or gpu. core_count (default=1): In the event that multiprocessing, specifies the number of cores. conv_device (default='cpu'): Specifier for device to be used - cpu or gpu. conv_mode (default='linear'): Specifier for convolution mode - linear or circular. extraction_mode (default='cpu'): Specifier for mode to be used - cpu or gpu. enforce_positivity (default=False): Boolean specifier for whether or not a model must be strictly positive. edge_suppression (default=False): Boolean specifier for whether or not the edges are to be suprressed. edge_offset (default=0): Numeric value for an additional user-specified number of edge pixels to be ignored. This is added to the minimum suppression. OUTPUTS: self.model (no default): Model extracted by the algorithm. self.residual (no default): Residual signal after deconvolution.
def validate(self): """Could this config be used to send a real email?""" missing = [] for k, v in self._map.items(): attr = getattr(self, k, False) if not attr or attr == CONFIG_PLACEHOLDER: missing.append(v) if missing: return "Missing or invalid config values: {}".format( ", ".join(sorted(missing)) )
Could this config be used to send a real email?
def update_account_api_key(self, account_id, api_key, body, **kwargs): # noqa: E501 """Update API key details. # noqa: E501 An endpoint for updating API key details. **Example usage:** `curl -X PUT https://api.us-east-1.mbedcloud.com/v3/accounts/{accountID}/api-keys/{apiKey} -d '{\"name\": \"TestApiKey25\"}' -H 'content-type: application/json' -H 'Authorization: Bearer API_KEY'` # noqa: E501 This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass asynchronous=True >>> thread = api.update_account_api_key(account_id, api_key, body, asynchronous=True) >>> result = thread.get() :param asynchronous bool :param str account_id: Account ID. (required) :param str api_key: The ID of the API key to be updated. (required) :param ApiKeyUpdateReq body: New API key attributes to be stored. (required) :return: ApiKeyInfoResp If the method is called asynchronously, returns the request thread. """ kwargs['_return_http_data_only'] = True if kwargs.get('asynchronous'): return self.update_account_api_key_with_http_info(account_id, api_key, body, **kwargs) # noqa: E501 else: (data) = self.update_account_api_key_with_http_info(account_id, api_key, body, **kwargs) # noqa: E501 return data
Update API key details. # noqa: E501 An endpoint for updating API key details. **Example usage:** `curl -X PUT https://api.us-east-1.mbedcloud.com/v3/accounts/{accountID}/api-keys/{apiKey} -d '{\"name\": \"TestApiKey25\"}' -H 'content-type: application/json' -H 'Authorization: Bearer API_KEY'` # noqa: E501 This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass asynchronous=True >>> thread = api.update_account_api_key(account_id, api_key, body, asynchronous=True) >>> result = thread.get() :param asynchronous bool :param str account_id: Account ID. (required) :param str api_key: The ID of the API key to be updated. (required) :param ApiKeyUpdateReq body: New API key attributes to be stored. (required) :return: ApiKeyInfoResp If the method is called asynchronously, returns the request thread.
def add_transformers(line): '''Extract the transformers names from a line of code of the form from __experimental__ import transformer1 [,...] and adds them to the globally known dict ''' assert FROM_EXPERIMENTAL.match(line) line = FROM_EXPERIMENTAL.sub(' ', line) # we now have: " transformer1 [,...]" line = line.split("#")[0] # remove any end of line comments # and insert each transformer as an item in a list for trans in line.replace(' ', '').split(','): import_transformer(trans)
Extract the transformers names from a line of code of the form from __experimental__ import transformer1 [,...] and adds them to the globally known dict
def create(self, name, sources=None, destinations=None, services=None, action='allow', log_options=None, authentication_options=None, connection_tracking=None, is_disabled=False, vpn_policy=None, mobile_vpn=False, add_pos=None, after=None, before=None, sub_policy=None, comment=None, **kw): """ Create a layer 3 firewall rule :param str name: name of rule :param sources: source/s for rule :type sources: list[str, Element] :param destinations: destination/s for rule :type destinations: list[str, Element] :param services: service/s for rule :type services: list[str, Element] :param action: allow,continue,discard,refuse,enforce_vpn, apply_vpn,forward_vpn, blacklist (default: allow) :type action: Action or str :param LogOptions log_options: LogOptions object :param ConnectionTracking connection_tracking: custom connection tracking settings :param AuthenticationOptions authentication_options: options for auth if any :param PolicyVPN,str vpn_policy: policy element or str href; required for enforce_vpn, use_vpn and apply_vpn actions :param bool mobile_vpn: if using a vpn action, you can set mobile_vpn to True and omit the vpn_policy setting if you want this VPN to apply to any mobile VPN based on the policy VPN associated with the engine :param str,Element sub_policy: sub policy required when rule has an action of 'jump'. Can be the FirewallSubPolicy element or href. :param int add_pos: position to insert the rule, starting with position 1. If the position value is greater than the number of rules, the rule is inserted at the bottom. If add_pos is not provided, rule is inserted in position 1. Mutually exclusive with ``after`` and ``before`` params. :param str after: Rule tag to add this rule after. Mutually exclusive with ``add_pos`` and ``before`` params. :param str before: Rule tag to add this rule before. Mutually exclusive with ``add_pos`` and ``after`` params. :param str comment: optional comment for this rule :raises MissingRequiredInput: when options are specified the need additional setting, i.e. use_vpn action requires a vpn policy be specified. :raises CreateRuleFailed: rule creation failure :return: the created ipv4 rule :rtype: IPv4Rule """ rule_values = self.update_targets(sources, destinations, services) rule_values.update(name=name, comment=comment) if isinstance(action, Action): rule_action = action else: rule_action = Action() rule_action.action = action if not rule_action.action in self._actions: raise CreateRuleFailed('Action specified is not valid for this ' 'rule type; action: {}'.format(rule_action.action)) if rule_action.action in ('apply_vpn', 'enforce_vpn', 'forward_vpn'): if vpn_policy is None and not mobile_vpn: raise MissingRequiredInput('You must either specify a vpn_policy or set ' 'mobile_vpn when using a rule with a VPN action') if mobile_vpn: rule_action.mobile_vpn = True else: try: vpn = element_resolver(vpn_policy) # VPNPolicy rule_action.vpn = vpn except ElementNotFound: raise MissingRequiredInput('Cannot find VPN policy specified: {}, ' .format(vpn_policy)) elif rule_action.action == 'jump': try: rule_action.sub_policy = element_resolver(sub_policy) except ElementNotFound: raise MissingRequiredInput('Cannot find sub policy specified: {} ' .format(sub_policy)) #rule_values.update(action=rule_action.data) log_options = LogOptions() if not log_options else log_options if connection_tracking is not None: rule_action.connection_tracking_options.update(**connection_tracking) auth_options = AuthenticationOptions() if not authentication_options \ else authentication_options rule_values.update( action=rule_action.data, options=log_options.data, authentication_options=auth_options.data, is_disabled=is_disabled) params = None href = self.href if add_pos is not None: href = self.add_at_position(add_pos) elif before or after: params = self.add_before_after(before, after) return ElementCreator( self.__class__, exception=CreateRuleFailed, href=href, params=params, json=rule_values)
Create a layer 3 firewall rule :param str name: name of rule :param sources: source/s for rule :type sources: list[str, Element] :param destinations: destination/s for rule :type destinations: list[str, Element] :param services: service/s for rule :type services: list[str, Element] :param action: allow,continue,discard,refuse,enforce_vpn, apply_vpn,forward_vpn, blacklist (default: allow) :type action: Action or str :param LogOptions log_options: LogOptions object :param ConnectionTracking connection_tracking: custom connection tracking settings :param AuthenticationOptions authentication_options: options for auth if any :param PolicyVPN,str vpn_policy: policy element or str href; required for enforce_vpn, use_vpn and apply_vpn actions :param bool mobile_vpn: if using a vpn action, you can set mobile_vpn to True and omit the vpn_policy setting if you want this VPN to apply to any mobile VPN based on the policy VPN associated with the engine :param str,Element sub_policy: sub policy required when rule has an action of 'jump'. Can be the FirewallSubPolicy element or href. :param int add_pos: position to insert the rule, starting with position 1. If the position value is greater than the number of rules, the rule is inserted at the bottom. If add_pos is not provided, rule is inserted in position 1. Mutually exclusive with ``after`` and ``before`` params. :param str after: Rule tag to add this rule after. Mutually exclusive with ``add_pos`` and ``before`` params. :param str before: Rule tag to add this rule before. Mutually exclusive with ``add_pos`` and ``after`` params. :param str comment: optional comment for this rule :raises MissingRequiredInput: when options are specified the need additional setting, i.e. use_vpn action requires a vpn policy be specified. :raises CreateRuleFailed: rule creation failure :return: the created ipv4 rule :rtype: IPv4Rule
def smooth(self): """ Read/write boolean specifying whether to use curve smoothing to form the line connecting the data points in this series into a continuous curve. If |False|, a series of straight line segments are used to connect the points. """ smooth = self._element.smooth if smooth is None: return True return smooth.val
Read/write boolean specifying whether to use curve smoothing to form the line connecting the data points in this series into a continuous curve. If |False|, a series of straight line segments are used to connect the points.
def outcomes(self, outcomes): """ Setter for _outcomes field See property. :param dict outcomes: Dictionary outcomes[outcome_id] that maps :class:`int` outcome_ids onto values of type :class:`rafcon.core.state_elements.logical_port.Outcome` :raises exceptions.TypeError: if outcomes parameter has the wrong type :raises exceptions.AttributeError: if the key of the outcome dictionary and the id of the outcome do not match """ if not isinstance(outcomes, dict): raise TypeError("outcomes must be of type dict") if [outcome_id for outcome_id, outcome in outcomes.items() if not isinstance(outcome, Outcome)]: raise TypeError("element of outcomes must be of type Outcome") if [outcome_id for outcome_id, outcome in outcomes.items() if not outcome_id == outcome.outcome_id]: raise AttributeError("The key of the outcomes dictionary and the id of the outcome do not match") old_outcomes = self.outcomes self._outcomes = outcomes for outcome_id, outcome in outcomes.items(): try: outcome.parent = self except ValueError: self._outcomes = old_outcomes raise # aborted and preempted must always exist if -1 not in outcomes: self._outcomes[-1] = Outcome(outcome_id=-1, name="aborted", parent=self) if -2 not in outcomes: self._outcomes[-2] = Outcome(outcome_id=-2, name="preempted", parent=self) # check that all old_outcomes are no more referencing self as there parent for old_outcome in old_outcomes.values(): if old_outcome not in iter(list(self._outcomes.values())) and old_outcome.parent is self: old_outcome.parent = None
Setter for _outcomes field See property. :param dict outcomes: Dictionary outcomes[outcome_id] that maps :class:`int` outcome_ids onto values of type :class:`rafcon.core.state_elements.logical_port.Outcome` :raises exceptions.TypeError: if outcomes parameter has the wrong type :raises exceptions.AttributeError: if the key of the outcome dictionary and the id of the outcome do not match
def get_key(self, section, key): """ Gets key value from settings file. :param section: Current section to retrieve key from. :type section: unicode :param key: Current key to retrieve. :type key: unicode :return: Current key value. :rtype: object """ LOGGER.debug("> Retrieving '{0}' in '{1}' section.".format(key, section)) self.__settings.beginGroup(section) value = self.__settings.value(key) LOGGER.debug("> Key value: '{0}'.".format(value)) self.__settings.endGroup() return value
Gets key value from settings file. :param section: Current section to retrieve key from. :type section: unicode :param key: Current key to retrieve. :type key: unicode :return: Current key value. :rtype: object
def check_proxy_setting(): """ If the environmental variable 'HTTP_PROXY' is set, it will most likely be in one of these forms: proxyhost:8080 http://proxyhost:8080 urlllib2 requires the proxy URL to start with 'http://' This routine does that, and returns the transport for xmlrpc. """ try: http_proxy = os.environ['HTTP_PROXY'] except KeyError: return if not http_proxy.startswith('http://'): match = re.match('(http://)?([-_\.A-Za-z]+):(\d+)', http_proxy) #if not match: # raise Exception('Proxy format not recognised: [%s]' % http_proxy) os.environ['HTTP_PROXY'] = 'http://%s:%s' % (match.group(2), match.group(3)) return
If the environmental variable 'HTTP_PROXY' is set, it will most likely be in one of these forms: proxyhost:8080 http://proxyhost:8080 urlllib2 requires the proxy URL to start with 'http://' This routine does that, and returns the transport for xmlrpc.
def set(self, key, value): """ Saves the input with the given key in the section that was passed to the constructor. If either the section or the key are not found, they are created. Does nothing if the given value is None. :type key: str :param key: The key for which to define a value. :type value: str|None :param value: The value that is defined, or None. :rtype: str|None :return: The given value. """ if value is None: return None self.parser.set(self.section, key, value) # Unfortunately ConfigParser attempts to write a string to the file # object, and NamedTemporaryFile uses binary mode. So we nee to create # the tempfile, and then re-open it. with NamedTemporaryFile(delete=False) as tmpfile: pass with codecs.open(tmpfile.name, 'w', encoding='utf8') as fp: self.parser.write(fp) self.file.close() shutil.move(tmpfile.name, self.file.name) self.file = open(self.file.name) return value
Saves the input with the given key in the section that was passed to the constructor. If either the section or the key are not found, they are created. Does nothing if the given value is None. :type key: str :param key: The key for which to define a value. :type value: str|None :param value: The value that is defined, or None. :rtype: str|None :return: The given value.
def subspace_index(self, little_endian_bits_int: int ) -> Tuple[Union[slice, int, 'ellipsis'], ...]: """An index for the subspace where the target axes equal a value. Args: little_endian_bits_int: The desired value of the qubits at the targeted `axes`, packed into an integer. The least significant bit of the integer is the desired bit for the first axis, and so forth in increasing order. Returns: A value that can be used to index into `target_tensor` and `available_buffer`, and manipulate only the part of Hilbert space corresponding to a given bit assignment. Example: If `target_tensor` is a 4 qubit tensor and `axes` is `[1, 3]` and then this method will return the following when given `little_endian_bits=0b01`: `(slice(None), 0, slice(None), 1, Ellipsis)` Therefore the following two lines would be equivalent: args.target_tensor[args.subspace_index(0b01)] += 1 args.target_tensor[:, 0, :, 1] += 1 """ return linalg.slice_for_qubits_equal_to(self.axes, little_endian_bits_int)
An index for the subspace where the target axes equal a value. Args: little_endian_bits_int: The desired value of the qubits at the targeted `axes`, packed into an integer. The least significant bit of the integer is the desired bit for the first axis, and so forth in increasing order. Returns: A value that can be used to index into `target_tensor` and `available_buffer`, and manipulate only the part of Hilbert space corresponding to a given bit assignment. Example: If `target_tensor` is a 4 qubit tensor and `axes` is `[1, 3]` and then this method will return the following when given `little_endian_bits=0b01`: `(slice(None), 0, slice(None), 1, Ellipsis)` Therefore the following two lines would be equivalent: args.target_tensor[args.subspace_index(0b01)] += 1 args.target_tensor[:, 0, :, 1] += 1
def multiply(a, b, prim=0x11b, field_charac_full=256, carryless=True): '''A slow multiply method. This method gives the same results as the other __mul__ method but without needing precomputed tables, thus it can be used to generate those tables. If prim is set to 0 and carryless=False, the function produces the result of a standard multiplication of integers (outside of a finite field, ie, no modular reduction and no carry-less operations). This procedure is called Russian Peasant Multiplication algorithm, which is just a general algorithm to multiply two integers together. The only two differences that you need to account for when doing multiplication in a finite field (as opposed to just integers) are: 1- carry-less addition and substraction (XOR in GF(2^p)) 2- modular reduction (to avoid duplicate values in the field) using a prime polynomial ''' r = 0 a = int(a) b = int(b) while b: # while b is not 0 if b & 1: r = r ^ a if carryless else r + a # b is odd, then add the corresponding a to r (the sum of all a's corresponding to odd b's will give the final product). Note that since we're in GF(2), the addition is in fact an XOR (very important because in GF(2) the multiplication and additions are carry-less, thus it changes the result!). b = b >> 1 # equivalent to b // 2 a = a << 1 # equivalent to a*2 if prim > 0 and a & field_charac_full: a = a ^ prim # GF modulo: if a >= 256 then apply modular reduction using the primitive polynomial (we just substract, but since the primitive number can be above 256 then we directly XOR). return GF2int(r)
A slow multiply method. This method gives the same results as the other __mul__ method but without needing precomputed tables, thus it can be used to generate those tables. If prim is set to 0 and carryless=False, the function produces the result of a standard multiplication of integers (outside of a finite field, ie, no modular reduction and no carry-less operations). This procedure is called Russian Peasant Multiplication algorithm, which is just a general algorithm to multiply two integers together. The only two differences that you need to account for when doing multiplication in a finite field (as opposed to just integers) are: 1- carry-less addition and substraction (XOR in GF(2^p)) 2- modular reduction (to avoid duplicate values in the field) using a prime polynomial
def get_step_f(step_f, lR2, lS2): """Update the stepsize of given the primal and dual errors. See Boyd (2011), section 3.4.1 """ mu, tau = 10, 2 if lR2 > mu*lS2: return step_f * tau elif lS2 > mu*lR2: return step_f / tau return step_f
Update the stepsize of given the primal and dual errors. See Boyd (2011), section 3.4.1
def elbv2_load_balancer_arn_suffix(self, lookup, default=None): """ Args: lookup: the friendly name of the v2 elb to look up default: value to return in case of no match Returns: The shorthand fragment of the ALB's ARN, of the form `app/*/*` """ try: elb = self._elbv2_load_balancer(lookup) m = re.search(r'.+?(app\/[^\/]+\/[^\/]+)$', elb['LoadBalancerArn']) return m.group(1) except ClientError: return default
Args: lookup: the friendly name of the v2 elb to look up default: value to return in case of no match Returns: The shorthand fragment of the ALB's ARN, of the form `app/*/*`
def missing_optional_tagfiles(self): """ From v0.97 we need to validate any tagfiles listed in the optional tagmanifest(s). As there is no mandatory directory structure for additional tagfiles we can only check for entries with missing files (not missing entries for existing files). """ for tagfilepath in list(self.tagfile_entries().keys()): if not os.path.isfile(os.path.join(self.path, tagfilepath)): yield tagfilepath
From v0.97 we need to validate any tagfiles listed in the optional tagmanifest(s). As there is no mandatory directory structure for additional tagfiles we can only check for entries with missing files (not missing entries for existing files).
def schemaless_reader(fo, writer_schema, reader_schema=None): """Reads a single record writen using the :meth:`~fastavro._write_py.schemaless_writer` Parameters ---------- fo: file-like Input stream writer_schema: dict Schema used when calling schemaless_writer reader_schema: dict, optional If the schema has changed since being written then the new schema can be given to allow for schema migration Example:: parsed_schema = fastavro.parse_schema(schema) with open('file.avro', 'rb') as fp: record = fastavro.schemaless_reader(fp, parsed_schema) Note: The ``schemaless_reader`` can only read a single record. """ if writer_schema == reader_schema: # No need for the reader schema if they are the same reader_schema = None writer_schema = parse_schema(writer_schema) if reader_schema: reader_schema = parse_schema(reader_schema) return read_data(fo, writer_schema, reader_schema)
Reads a single record writen using the :meth:`~fastavro._write_py.schemaless_writer` Parameters ---------- fo: file-like Input stream writer_schema: dict Schema used when calling schemaless_writer reader_schema: dict, optional If the schema has changed since being written then the new schema can be given to allow for schema migration Example:: parsed_schema = fastavro.parse_schema(schema) with open('file.avro', 'rb') as fp: record = fastavro.schemaless_reader(fp, parsed_schema) Note: The ``schemaless_reader`` can only read a single record.
def add_binding(self, *keys, **kwargs): """ Decorator for annotating key bindings. :param filter: :class:`~prompt_toolkit.filters.CLIFilter` to determine when this key binding is active. :param eager: :class:`~prompt_toolkit.filters.CLIFilter` or `bool`. When True, ignore potential longer matches when this key binding is hit. E.g. when there is an active eager key binding for Ctrl-X, execute the handler immediately and ignore the key binding for Ctrl-X Ctrl-E of which it is a prefix. :param save_before: Callable that takes an `Event` and returns True if we should save the current buffer, before handling the event. (That's the default.) """ filter = to_cli_filter(kwargs.pop('filter', True)) eager = to_cli_filter(kwargs.pop('eager', False)) save_before = kwargs.pop('save_before', lambda e: True) to_cli_filter(kwargs.pop('invalidate_ui', True)) # Deprecated! (ignored.) assert not kwargs assert keys assert all(isinstance(k, (Key, text_type)) for k in keys), \ 'Key bindings should consist of Key and string (unicode) instances.' assert callable(save_before) if isinstance(filter, Never): # When a filter is Never, it will always stay disabled, so in that case # don't bother putting it in the registry. It will slow down every key # press otherwise. def decorator(func): return func else: def decorator(func): self.key_bindings.append( _Binding(keys, func, filter=filter, eager=eager, save_before=save_before)) self._clear_cache() return func return decorator
Decorator for annotating key bindings. :param filter: :class:`~prompt_toolkit.filters.CLIFilter` to determine when this key binding is active. :param eager: :class:`~prompt_toolkit.filters.CLIFilter` or `bool`. When True, ignore potential longer matches when this key binding is hit. E.g. when there is an active eager key binding for Ctrl-X, execute the handler immediately and ignore the key binding for Ctrl-X Ctrl-E of which it is a prefix. :param save_before: Callable that takes an `Event` and returns True if we should save the current buffer, before handling the event. (That's the default.)
def exists(name, **kwargs): ''' Check if a ZFS filesystem or volume or snapshot exists. name : string name of dataset type : string also check if dataset is of a certain type, valid choices are: filesystem, snapshot, volume, bookmark, or all. .. versionadded:: 2015.5.0 CLI Example: .. code-block:: bash salt '*' zfs.exists myzpool/mydataset salt '*' zfs.exists myzpool/myvolume type=volume ''' ## Configure command # NOTE: initialize the defaults opts = {} # NOTE: set extra config from kwargs if kwargs.get('type', False): opts['-t'] = kwargs.get('type') ## Check if 'name' of 'type' exists res = __salt__['cmd.run_all']( __utils__['zfs.zfs_command']( command='list', opts=opts, target=name, ), python_shell=False, ignore_retcode=True, ) return res['retcode'] == 0
Check if a ZFS filesystem or volume or snapshot exists. name : string name of dataset type : string also check if dataset is of a certain type, valid choices are: filesystem, snapshot, volume, bookmark, or all. .. versionadded:: 2015.5.0 CLI Example: .. code-block:: bash salt '*' zfs.exists myzpool/mydataset salt '*' zfs.exists myzpool/myvolume type=volume
def _inertia_from_labels(X, centers, labels): """Compute inertia with cosine distance using known labels. """ n_examples, n_features = X.shape inertia = np.zeros((n_examples,)) for ee in range(n_examples): inertia[ee] = 1 - X[ee, :].dot(centers[int(labels[ee]), :].T) return np.sum(inertia)
Compute inertia with cosine distance using known labels.
def progress_str(max_val, lbl='Progress: ', repl=False, approx=False, backspace=PROGGRESS_BACKSPACE): r""" makes format string that prints progress: %Xd/MAX_VAL with backspaces NOTE: \r can be used instead of backspaces. This function is not very relevant because of that. """ # string that displays max value max_str = six.text_type(max_val) if approx: # denote approximate maximum max_str = '~' + max_str dnumstr = six.text_type(len(max_str)) # string that displays current progress cur_str = '%' + dnumstr + 'd' # If user passed in the label if repl: _fmt_str = lbl.replace('<cur_str>', cur_str).replace('<max_str>', max_str) else: _fmt_str = lbl + cur_str + '/' + max_str if backspace: # put backspace characters into the progress string # (looks nice on normal terminals) #nBackspaces = len(_fmt_str) - len(dnumstr) + len(max_str) #backspaces = '\b' * nBackspaces #fmt_str = backspaces + _fmt_str # FIXME: USE CARAGE RETURN INSTEAD OF BACKSPACES fmt_str = '\r' + _fmt_str else: # FIXME: USE CARAGE RETURN INSTEAD OF BACKSPACES # this looks better on terminals without backspaces fmt_str = _fmt_str + '\n' return fmt_str
r""" makes format string that prints progress: %Xd/MAX_VAL with backspaces NOTE: \r can be used instead of backspaces. This function is not very relevant because of that.
def run_query(db, query): ''' Run SQL query and return result CLI Example: .. code-block:: bash salt '*' oracle.run_query my_db "select * from my_table" ''' if db in [x.keys()[0] for x in show_dbs()]: conn = _connect(show_dbs(db)[db]['uri']) else: log.debug('No uri found in pillars - will try to use oratab') # if db does not have uri defined in pillars # or it's not defined in pillars at all parse oratab file conn = _connect(uri=db) return conn.cursor().execute(query).fetchall()
Run SQL query and return result CLI Example: .. code-block:: bash salt '*' oracle.run_query my_db "select * from my_table"
def encode_record_with_schema_id(self, schema_id, record, is_key=False): """ Encode a record with a given schema id. The record must be a python dictionary. :param int schema_id: integer ID :param dict record: An object to serialize :param bool is_key: If the record is a key :returns: decoder function :rtype: func """ serialize_err = KeySerializerError if is_key else ValueSerializerError # use slow avro if schema_id not in self.id_to_writers: # get the writer + schema try: schema = self.registry_client.get_by_id(schema_id) if not schema: raise serialize_err("Schema does not exist") self.id_to_writers[schema_id] = self._get_encoder_func(schema) except ClientError: exc_type, exc_value, exc_traceback = sys.exc_info() raise serialize_err(repr(traceback.format_exception(exc_type, exc_value, exc_traceback))) # get the writer writer = self.id_to_writers[schema_id] with ContextStringIO() as outf: # Write the magic byte and schema ID in network byte order (big endian) outf.write(struct.pack('>bI', MAGIC_BYTE, schema_id)) # write the record to the rest of the buffer writer(record, outf) return outf.getvalue()
Encode a record with a given schema id. The record must be a python dictionary. :param int schema_id: integer ID :param dict record: An object to serialize :param bool is_key: If the record is a key :returns: decoder function :rtype: func
def find_close_value(self, LIST, value): ''' take a LIST and find the nearest value in LIST to 'value' ''' diff = inf for a in LIST: if abs(value - a) < diff: diff = abs(value - a) result = a return(result)
take a LIST and find the nearest value in LIST to 'value'
def create_job(cpu_width, time_height): """ :param cpu_width: number of cpus :param time_height: amount of time :return: the instantiated JobBlock object """ shell_command = stress_string.format(cpu_width, time_height) job = JobBlock(cpu_width, time_height) job.set_job(subprocess.call, shell_command, shell=True) return job
:param cpu_width: number of cpus :param time_height: amount of time :return: the instantiated JobBlock object
def wait_locally(self): """If you have run the query in a non-blocking way, call this method to pause until the query is finished.""" try: self.thread.join(sys.maxint) # maxint timeout so that we can Ctrl-C them except KeyboardInterrupt: print "Stopped waiting on job '%s'" % self.kwargs['job_name']
If you have run the query in a non-blocking way, call this method to pause until the query is finished.
def putData(self,data=None,exten=None): """ Now that we are removing the data from the object to save memory, we need something that cleanly puts the data array back into the object so that we can write out everything together using something like fits.writeto....this method is an attempt to make sure that when you add an array back to the .data section of the hdu it still matches the header information for that section ( ie. update the bitpix to reflect the datatype of the array you are adding). The other header stuff is up to you to verify. Data should be the data array exten is where you want to stick it, either extension number or a string like 'sci,1' """ if data is None: log.warning("No data supplied") else: extnum = _interpretExten(exten) ext = self._image[extnum] # update the bitpix to the current datatype, this aint fancy and # ignores bscale ext.header['BITPIX'] = _NUMPY_TO_IRAF_DTYPES[data.dtype.name] ext.data = data
Now that we are removing the data from the object to save memory, we need something that cleanly puts the data array back into the object so that we can write out everything together using something like fits.writeto....this method is an attempt to make sure that when you add an array back to the .data section of the hdu it still matches the header information for that section ( ie. update the bitpix to reflect the datatype of the array you are adding). The other header stuff is up to you to verify. Data should be the data array exten is where you want to stick it, either extension number or a string like 'sci,1'
def kasten96_lt(airmass_absolute, precipitable_water, aod_bb): """ Calculate Linke turbidity factor using Kasten pyrheliometric formula. Note that broadband aerosol optical depth (AOD) can be approximated by AOD measured at 700 nm according to Molineaux [4] . Bird and Hulstrom offer an alternate approximation using AOD measured at 380 nm and 500 nm. Based on original implementation by Armel Oumbe. .. warning:: These calculations are only valid for air mass less than 5 atm and precipitable water less than 5 cm. Parameters ---------- airmass_absolute : numeric airmass, pressure corrected in atmospheres precipitable_water : numeric precipitable water or total column water vapor in centimeters aod_bb : numeric broadband AOD Returns ------- lt : numeric Linke turbidity See also -------- bird_hulstrom80_aod_bb angstrom_aod_at_lambda References ---------- [1] F. Linke, "Transmissions-Koeffizient und Trubungsfaktor", Beitrage zur Physik der Atmosphare, Vol 10, pp. 91-103 (1922) [2] F. Kasten, "A simple parameterization of the pyrheliometric formula for determining the Linke turbidity factor", Meteorologische Rundschau 33, pp. 124-127 (1980) [3] Kasten, "The Linke turbidity factor based on improved values of the integral Rayleigh optical thickness", Solar Energy, Vol. 56, No. 3, pp. 239-244 (1996) :doi:`10.1016/0038-092X(95)00114-7` [4] B. Molineaux, P. Ineichen, N. O'Neill, "Equivalence of pyrheliometric and monochromatic aerosol optical depths at a single key wavelength", Applied Optics Vol. 37, issue 10, 7008-7018 (1998) :doi:`10.1364/AO.37.007008` [5] P. Ineichen, "Conversion function between the Linke turbidity and the atmospheric water vapor and aerosol content", Solar Energy 82, pp. 1095-1097 (2008) :doi:`10.1016/j.solener.2008.04.010` [6] P. Ineichen and R. Perez, "A new airmass independent formulation for the Linke Turbidity coefficient", Solar Energy, Vol. 73, no. 3, pp. 151-157 (2002) :doi:`10.1016/S0038-092X(02)00045-2` """ # "From numerically integrated spectral simulations done with Modtran # (Berk, 1989), Molineaux (1998) obtained for the broadband optical depth # of a clean and dry atmospshere (fictitious atmosphere that comprises only # the effects of Rayleigh scattering and absorption by the atmosphere gases # other than the water vapor) the following expression" # - P. Ineichen (2008) delta_cda = -0.101 + 0.235 * airmass_absolute ** (-0.16) # "and the broadband water vapor optical depth where pwat is the integrated # precipitable water vapor content of the atmosphere expressed in cm and am # the optical air mass. The precision of these fits is better than 1% when # compared with Modtran simulations in the range 1 < am < 5 and # 0 < pwat < 5 cm at sea level" - P. Ineichen (2008) delta_w = 0.112 * airmass_absolute ** (-0.55) * precipitable_water ** 0.34 # broadband AOD delta_a = aod_bb # "Then using the Kasten pyrheliometric formula (1980, 1996), the Linke # turbidity at am = 2 can be written. The extension of the Linke turbidity # coefficient to other values of air mass was published by Ineichen and # Perez (2002)" - P. Ineichen (2008) lt = -(9.4 + 0.9 * airmass_absolute) * np.log( np.exp(-airmass_absolute * (delta_cda + delta_w + delta_a)) ) / airmass_absolute # filter out of extrapolated values return lt
Calculate Linke turbidity factor using Kasten pyrheliometric formula. Note that broadband aerosol optical depth (AOD) can be approximated by AOD measured at 700 nm according to Molineaux [4] . Bird and Hulstrom offer an alternate approximation using AOD measured at 380 nm and 500 nm. Based on original implementation by Armel Oumbe. .. warning:: These calculations are only valid for air mass less than 5 atm and precipitable water less than 5 cm. Parameters ---------- airmass_absolute : numeric airmass, pressure corrected in atmospheres precipitable_water : numeric precipitable water or total column water vapor in centimeters aod_bb : numeric broadband AOD Returns ------- lt : numeric Linke turbidity See also -------- bird_hulstrom80_aod_bb angstrom_aod_at_lambda References ---------- [1] F. Linke, "Transmissions-Koeffizient und Trubungsfaktor", Beitrage zur Physik der Atmosphare, Vol 10, pp. 91-103 (1922) [2] F. Kasten, "A simple parameterization of the pyrheliometric formula for determining the Linke turbidity factor", Meteorologische Rundschau 33, pp. 124-127 (1980) [3] Kasten, "The Linke turbidity factor based on improved values of the integral Rayleigh optical thickness", Solar Energy, Vol. 56, No. 3, pp. 239-244 (1996) :doi:`10.1016/0038-092X(95)00114-7` [4] B. Molineaux, P. Ineichen, N. O'Neill, "Equivalence of pyrheliometric and monochromatic aerosol optical depths at a single key wavelength", Applied Optics Vol. 37, issue 10, 7008-7018 (1998) :doi:`10.1364/AO.37.007008` [5] P. Ineichen, "Conversion function between the Linke turbidity and the atmospheric water vapor and aerosol content", Solar Energy 82, pp. 1095-1097 (2008) :doi:`10.1016/j.solener.2008.04.010` [6] P. Ineichen and R. Perez, "A new airmass independent formulation for the Linke Turbidity coefficient", Solar Energy, Vol. 73, no. 3, pp. 151-157 (2002) :doi:`10.1016/S0038-092X(02)00045-2`
def Rz_to_lambdanu_jac(R,z,Delta=1.): """ NAME: Rz_to_lambdanu_jac PURPOSE: calculate the Jacobian of the cylindrical (R,z) to prolate spheroidal (lambda,nu) conversion INPUT: R - Galactocentric cylindrical radius z - vertical height Delta - focal distance that defines the spheroidal coordinate system (default: 1.) Delta=sqrt(g-a) OUTPUT: jacobian d((lambda,nu))/d((R,z)) HISTORY: 2015-02-13 - Written - Trick (MPIA) """ discr = (R**2 + z**2 - Delta**2)**2 + (4. * Delta**2 * R**2) dldR = R * (1. + (R**2 + z**2 + Delta**2) / nu.sqrt(discr)) dndR = R * (1. - (R**2 + z**2 + Delta**2) / nu.sqrt(discr)) dldz = z * (1. + (R**2 + z**2 - Delta**2) / nu.sqrt(discr)) dndz = z * (1. - (R**2 + z**2 - Delta**2) / nu.sqrt(discr)) dim = 1 if isinstance(R,nu.ndarray): dim = len(R) elif isinstance(z,nu.ndarray): dim = len(z) jac = nu.zeros((2,2,dim)) jac[0,0,:] = dldR jac[0,1,:] = dldz jac[1,0,:] = dndR jac[1,1,:] = dndz if dim == 1: return jac[:,:,0] else: return jac
NAME: Rz_to_lambdanu_jac PURPOSE: calculate the Jacobian of the cylindrical (R,z) to prolate spheroidal (lambda,nu) conversion INPUT: R - Galactocentric cylindrical radius z - vertical height Delta - focal distance that defines the spheroidal coordinate system (default: 1.) Delta=sqrt(g-a) OUTPUT: jacobian d((lambda,nu))/d((R,z)) HISTORY: 2015-02-13 - Written - Trick (MPIA)
def map_trips( feed: "Feed", trip_ids: List[str], color_palette: List[str] = cs.COLORS_SET2, *, include_stops: bool = True, ): """ Return a Folium map showing the given trips and (optionally) their stops. Parameters ---------- feed : Feed trip_ids : list IDs of trips in ``feed.trips`` color_palette : list Palette to use to color the routes. If more routes than colors, then colors will be recycled. include_stops : boolean If ``True``, then include stops in the map Returns ------- dictionary A Folium Map depicting the shapes of the trips. If ``include_stops``, then include the stops for each trip. Notes ------ - Requires Folium """ import folium as fl import folium.plugins as fp # Get routes slice and convert to dictionary trips = ( feed.trips.loc[lambda x: x["trip_id"].isin(trip_ids)] .fillna("n/a") .to_dict(orient="records") ) # Create colors n = len(trips) colors = [color_palette[i % len(color_palette)] for i in range(n)] # Initialize map my_map = fl.Map(tiles="cartodbpositron") # Collect route bounding boxes to set map zoom later bboxes = [] # Create a feature group for each route and add it to the map for i, trip in enumerate(trips): collection = feed.trip_to_geojson( trip_id=trip["trip_id"], include_stops=include_stops ) group = fl.FeatureGroup(name="Trip " + trip["trip_id"]) color = colors[i] for f in collection["features"]: prop = f["properties"] # Add stop if f["geometry"]["type"] == "Point": lon, lat = f["geometry"]["coordinates"] fl.CircleMarker( location=[lat, lon], radius=8, fill=True, color=color, weight=1, popup=fl.Popup(hp.make_html(prop)), ).add_to(group) # Add path else: # Path prop["color"] = color path = fl.GeoJson( f, name=trip, style_function=lambda x: { "color": x["properties"]["color"] }, ) path.add_child(fl.Popup(hp.make_html(prop))) path.add_to(group) # Direction arrows, assuming, as GTFS does, that # trip direction equals LineString direction fp.PolyLineTextPath( path, " \u27A4 ", repeat=True, offset=5.5, attributes={"fill": color, "font-size": "18"}, ).add_to(group) bboxes.append(sg.box(*sg.shape(f["geometry"]).bounds)) group.add_to(my_map) fl.LayerControl().add_to(my_map) # Fit map to bounds bounds = so.unary_union(bboxes).bounds bounds2 = [bounds[1::-1], bounds[3:1:-1]] # Folium expects this ordering my_map.fit_bounds(bounds2) return my_map
Return a Folium map showing the given trips and (optionally) their stops. Parameters ---------- feed : Feed trip_ids : list IDs of trips in ``feed.trips`` color_palette : list Palette to use to color the routes. If more routes than colors, then colors will be recycled. include_stops : boolean If ``True``, then include stops in the map Returns ------- dictionary A Folium Map depicting the shapes of the trips. If ``include_stops``, then include the stops for each trip. Notes ------ - Requires Folium
def image_get(fingerprint, remote_addr=None, cert=None, key=None, verify_cert=True, _raw=False): ''' Get an image by its fingerprint fingerprint : The fingerprint of the image to retrieve remote_addr : An URL to a remote Server, you also have to give cert and key if you provide remote_addr and its a TCP Address! Examples: https://myserver.lan:8443 /var/lib/mysocket.sock cert : PEM Formatted SSL Certificate. Examples: ~/.config/lxc/client.crt key : PEM Formatted SSL Key. Examples: ~/.config/lxc/client.key verify_cert : True Wherever to verify the cert, this is by default True but in the most cases you want to set it off as LXD normaly uses self-signed certificates. _raw : False Return the raw pylxd object or a dict of it? CLI Examples: ..code-block:: bash $ salt '*' lxd.image_get <fingerprint> ''' client = pylxd_client_get(remote_addr, cert, key, verify_cert) image = None try: image = client.images.get(fingerprint) except pylxd.exceptions.LXDAPIException: raise SaltInvocationError( 'Image with fingerprint \'{0}\' not found'.format(fingerprint) ) if _raw: return image return _pylxd_model_to_dict(image)
Get an image by its fingerprint fingerprint : The fingerprint of the image to retrieve remote_addr : An URL to a remote Server, you also have to give cert and key if you provide remote_addr and its a TCP Address! Examples: https://myserver.lan:8443 /var/lib/mysocket.sock cert : PEM Formatted SSL Certificate. Examples: ~/.config/lxc/client.crt key : PEM Formatted SSL Key. Examples: ~/.config/lxc/client.key verify_cert : True Wherever to verify the cert, this is by default True but in the most cases you want to set it off as LXD normaly uses self-signed certificates. _raw : False Return the raw pylxd object or a dict of it? CLI Examples: ..code-block:: bash $ salt '*' lxd.image_get <fingerprint>
def get_dtype_counts(self): """Get the counts of dtypes in this object. Returns: The counts of dtypes in this object. """ if hasattr(self, "dtype"): return pandas.Series({str(self.dtype): 1}) result = self.dtypes.value_counts() result.index = result.index.map(lambda x: str(x)) return result
Get the counts of dtypes in this object. Returns: The counts of dtypes in this object.
def track_to_ref(track, with_track_no=False): """Convert a mopidy track to a mopidy ref.""" if with_track_no and track.track_no > 0: name = '%d - ' % track.track_no else: name = '' for artist in track.artists: if len(name) > 0: name += ', ' name += artist.name if (len(name)) > 0: name += ' - ' name += track.name return Ref.track(uri=track.uri, name=name)
Convert a mopidy track to a mopidy ref.
def getvector(d,s): ''' Get a vector flds data. Parameters: ----------- d -- flds data. s -- key for the data. ''' return np.array([d[s+"x"],d[s+"y"],d[s+"z"]]);
Get a vector flds data. Parameters: ----------- d -- flds data. s -- key for the data.
def _EncodeString(self, string): """Encodes a string in the preferred encoding. Returns: bytes: encoded string. """ try: # Note that encode() will first convert string into a Unicode string # if necessary. encoded_string = string.encode( self._preferred_encoding, errors=self._encode_errors) except UnicodeEncodeError: if self._encode_errors == 'strict': logging.error( 'Unable to properly write output due to encoding error. ' 'Switching to error tolerant encoding which can result in ' 'non Basic Latin (C0) characters to be replaced with "?" or ' '"\\ufffd".') self._encode_errors = 'replace' encoded_string = string.encode( self._preferred_encoding, errors=self._encode_errors) return encoded_string
Encodes a string in the preferred encoding. Returns: bytes: encoded string.
def to_flags(value): """Return a (flags, ednsflags) tuple which encodes the rcode. @param value: the rcode @type value: int @raises ValueError: rcode is < 0 or > 4095 @rtype: (int, int) tuple """ if value < 0 or value > 4095: raise ValueError('rcode must be >= 0 and <= 4095') v = value & 0xf ev = long(value & 0xff0) << 20 return (v, ev)
Return a (flags, ednsflags) tuple which encodes the rcode. @param value: the rcode @type value: int @raises ValueError: rcode is < 0 or > 4095 @rtype: (int, int) tuple
def _add_line_segment(self, x, y): """Add a |_LineSegment| operation to the drawing sequence.""" self._drawing_operations.append(_LineSegment.new(self, x, y))
Add a |_LineSegment| operation to the drawing sequence.
def mk_auth_token(self, account, admin=False, duration=0): """ Builds an authentification token, using preauth mechanism. See http://wiki.zimbra.com/wiki/Preauth :param duration: in seconds defaults to 0, which means "use account default" :param account: an account object to be used as a selector :returns: the auth string """ domain = account.get_domain() try: preauth_key = self.get_domain(domain)['zimbraPreAuthKey'] except KeyError: raise DomainHasNoPreAuthKey(domain) timestamp = int(time.time())*1000 expires = duration*1000 return utils.build_preauth_str(preauth_key, account.name, timestamp, expires, admin)
Builds an authentification token, using preauth mechanism. See http://wiki.zimbra.com/wiki/Preauth :param duration: in seconds defaults to 0, which means "use account default" :param account: an account object to be used as a selector :returns: the auth string
def past(self, rev=None): """Return a Mapping of items at or before the given revision. Default revision is the last one looked up. """ if rev is not None: self.seek(rev) return WindowDictPastView(self._past)
Return a Mapping of items at or before the given revision. Default revision is the last one looked up.
def _get_version(): ''' Get the xbps version ''' version_string = __salt__['cmd.run']( [_check_xbps(), '--version'], output_loglevel='trace') if version_string is None: # Dunno why it would, but... return False VERSION_MATCH = re.compile(r'(?:XBPS:[\s]+)([\d.]+)(?:[\s]+.*)') version_match = VERSION_MATCH.search(version_string) if not version_match: return False return version_match.group(1).split('.')
Get the xbps version
def verify_oauth_token_and_set_current_user(): """Verify OAuth token and set current user on request stack. This function should be used **only** on REST application. .. code-block:: python app.before_request(verify_oauth_token_and_set_current_user) """ for func in oauth2._before_request_funcs: func() if not hasattr(request, 'oauth') or not request.oauth: scopes = [] try: valid, req = oauth2.verify_request(scopes) except ValueError: abort(400, 'Error trying to decode a non urlencoded string.') for func in oauth2._after_request_funcs: valid, req = func(valid, req) if valid: request.oauth = req
Verify OAuth token and set current user on request stack. This function should be used **only** on REST application. .. code-block:: python app.before_request(verify_oauth_token_and_set_current_user)