positive
stringlengths
100
30.3k
anchor
stringlengths
1
15k
def create_target_group(name, protocol, port, vpc_id, region=None, key=None, keyid=None, profile=None, health_check_protocol='HTTP', health_check_port='traffic-port', health_check_path='/', health_check_interval_seconds=30, health_check_timeout_seconds=5, healthy_threshold_count=5, unhealthy_threshold_count=2): ''' Create target group if not present. name (string) - The name of the target group. protocol (string) - The protocol to use for routing traffic to the targets port (int) - The port on which the targets receive traffic. This port is used unless you specify a port override when registering the traffic. vpc_id (string) - The identifier of the virtual private cloud (VPC). health_check_protocol (string) - The protocol the load balancer uses when performing health check on targets. The default is the HTTP protocol. health_check_port (string) - The port the load balancer uses when performing health checks on targets. The default is 'traffic-port', which indicates the port on which each target receives traffic from the load balancer. health_check_path (string) - The ping path that is the destination on the targets for health checks. The default is /. health_check_interval_seconds (integer) - The approximate amount of time, in seconds, between health checks of an individual target. The default is 30 seconds. health_check_timeout_seconds (integer) - The amount of time, in seconds, during which no response from a target means a failed health check. The default is 5 seconds. healthy_threshold_count (integer) - The number of consecutive health checks successes required before considering an unhealthy target healthy. The default is 5. unhealthy_threshold_count (integer) - The number of consecutive health check failures required before considering a target unhealthy. The default is 2. returns (bool) - True on success, False on failure. CLI example: .. code-block:: bash salt myminion boto_elbv2.create_target_group learn1give1 protocol=HTTP port=54006 vpc_id=vpc-deadbeef ''' conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) if target_group_exists(name, region, key, keyid, profile): return True try: alb = conn.create_target_group(Name=name, Protocol=protocol, Port=port, VpcId=vpc_id, HealthCheckProtocol=health_check_protocol, HealthCheckPort=health_check_port, HealthCheckPath=health_check_path, HealthCheckIntervalSeconds=health_check_interval_seconds, HealthCheckTimeoutSeconds=health_check_timeout_seconds, HealthyThresholdCount=healthy_threshold_count, UnhealthyThresholdCount=unhealthy_threshold_count) if alb: log.info('Created ALB %s: %s', name, alb['TargetGroups'][0]['TargetGroupArn']) return True else: log.error('Failed to create ALB %s', name) return False except ClientError as error: log.error( 'Failed to create ALB %s: %s: %s', name, error.response['Error']['Code'], error.response['Error']['Message'], exc_info_on_loglevel=logging.DEBUG )
Create target group if not present. name (string) - The name of the target group. protocol (string) - The protocol to use for routing traffic to the targets port (int) - The port on which the targets receive traffic. This port is used unless you specify a port override when registering the traffic. vpc_id (string) - The identifier of the virtual private cloud (VPC). health_check_protocol (string) - The protocol the load balancer uses when performing health check on targets. The default is the HTTP protocol. health_check_port (string) - The port the load balancer uses when performing health checks on targets. The default is 'traffic-port', which indicates the port on which each target receives traffic from the load balancer. health_check_path (string) - The ping path that is the destination on the targets for health checks. The default is /. health_check_interval_seconds (integer) - The approximate amount of time, in seconds, between health checks of an individual target. The default is 30 seconds. health_check_timeout_seconds (integer) - The amount of time, in seconds, during which no response from a target means a failed health check. The default is 5 seconds. healthy_threshold_count (integer) - The number of consecutive health checks successes required before considering an unhealthy target healthy. The default is 5. unhealthy_threshold_count (integer) - The number of consecutive health check failures required before considering a target unhealthy. The default is 2. returns (bool) - True on success, False on failure. CLI example: .. code-block:: bash salt myminion boto_elbv2.create_target_group learn1give1 protocol=HTTP port=54006 vpc_id=vpc-deadbeef
def base_mortality_rate(self, index: pd.Index) -> pd.Series: """Computes the base mortality rate for every individual. Parameters ---------- index : A representation of the simulants to compute the base mortality rate for. Returns ------- The base mortality rate for all simulants in the index. """ return pd.Series(self.config.mortality_rate, index=index)
Computes the base mortality rate for every individual. Parameters ---------- index : A representation of the simulants to compute the base mortality rate for. Returns ------- The base mortality rate for all simulants in the index.
def schedule(func, time, channel="default"): """Run `func` at a later `time` in a dedicated `channel` Given an arbitrary function, call this function after a given timeout. It will ensure that only one "job" is running within the given channel at any one time and cancel any currently running job if a new job is submitted before the timeout. """ try: _jobs[channel].stop() except (AttributeError, KeyError): pass timer = QtCore.QTimer() timer.setSingleShot(True) timer.timeout.connect(func) timer.start(time) _jobs[channel] = timer
Run `func` at a later `time` in a dedicated `channel` Given an arbitrary function, call this function after a given timeout. It will ensure that only one "job" is running within the given channel at any one time and cancel any currently running job if a new job is submitted before the timeout.
def icon(self): """Get QIcon from wrapper""" if self._icon is None: self._icon = QIcon(self.pm()) return self._icon
Get QIcon from wrapper
def radiansBetween(self, other): ''' :param: other - Line subclass :return: float Returns the angle measured between two lines in radians with a range of [0, 2 * math.pi]. ''' # a dot b = |a||b| * cos(theta) # a dot b / |a||b| = cos(theta) # cos-1(a dot b / |a||b|) = theta # translate each line so that it passes through the origin and # produce a new point whose distance (magnitude) from the # origin is 1. # a = Point.unit(self.A, self.B) b = Point.unit(other.A, other.B) # in a perfect world, after unit: |A| = |B| = 1 # which is a noop when dividing the dot product of A,B # but sometimes the lengths are different. # # let's just assume things are perfect and the lengths equal 1. return math.acos(a.dot(b))
:param: other - Line subclass :return: float Returns the angle measured between two lines in radians with a range of [0, 2 * math.pi].
def GetMessages(self, formatter_mediator, event): """Determines the formatted message strings for an event object. Args: formatter_mediator (FormatterMediator): mediates the interactions between formatters and other components, such as storage and Windows EventLog resources. event (EventObject): event. Returns: tuple(str, str): formatted message string and short message string. Raises: WrongFormatter: if the event object cannot be formatted by the formatter. """ if self.DATA_TYPE != event.data_type: raise errors.WrongFormatter('Unsupported data type: {0:s}.'.format( event.data_type)) event_values = event.CopyToDict() read_receipt = event_values.get('read_receipt', None) if read_receipt is not None: event_values['read_receipt'] = ( self._READ_RECEIPT.get(read_receipt, 'UNKNOWN')) message_type = event_values.get('message_type', None) if message_type is not None: event_values['message_type'] = ( self._MESSAGE_TYPE.get(message_type, 'UNKNOWN')) return self._ConditionalFormatMessages(event_values)
Determines the formatted message strings for an event object. Args: formatter_mediator (FormatterMediator): mediates the interactions between formatters and other components, such as storage and Windows EventLog resources. event (EventObject): event. Returns: tuple(str, str): formatted message string and short message string. Raises: WrongFormatter: if the event object cannot be formatted by the formatter.
def fasta(self, key='vdj_nt', append_chain=True): ''' Returns the sequence pair as a fasta string. If the Pair object contains both heavy and light chain sequences, both will be returned as a single string. By default, the fasta string contains the 'vdj_nt' sequence for each chain. To change, use the <key> option to select an alternate sequence. By default, the chain (heavy or light) will be appended to the sequence name: >MySequence_heavy To just use the pair name (which will result in duplicate sequence names for Pair objects with both heavy and light chains), set <append_chain> to False. ''' fastas = [] for s, chain in [(self.heavy, 'heavy'), (self.light, 'light')]: if s is not None: c = '_{}'.format(chain) if append_chain else '' fastas.append('>{}{}\n{}'.format(s['seq_id'], c, s[key])) return '\n'.join(fastas)
Returns the sequence pair as a fasta string. If the Pair object contains both heavy and light chain sequences, both will be returned as a single string. By default, the fasta string contains the 'vdj_nt' sequence for each chain. To change, use the <key> option to select an alternate sequence. By default, the chain (heavy or light) will be appended to the sequence name: >MySequence_heavy To just use the pair name (which will result in duplicate sequence names for Pair objects with both heavy and light chains), set <append_chain> to False.
def level(self, value): """Build profile URI from level. Level should be an integer 0,1,2 """ self.compliance = self.compliance_prefix + \ ("%d" % value) + self.compliance_suffix
Build profile URI from level. Level should be an integer 0,1,2
def prepare_to_store(self, entity, value): """Prepare `value` for storage. Called by the Model for each Property, value pair it contains before handing the data off to an adapter. Parameters: entity(Model): The entity to which the value belongs. value: The value being stored. Raises: RuntimeError: If this property is required but no value was assigned to it. Returns: The value that should be persisted. """ if value is None and not self.optional: raise RuntimeError(f"Property {self.name_on_model} requires a value.") return value
Prepare `value` for storage. Called by the Model for each Property, value pair it contains before handing the data off to an adapter. Parameters: entity(Model): The entity to which the value belongs. value: The value being stored. Raises: RuntimeError: If this property is required but no value was assigned to it. Returns: The value that should be persisted.
def error(self, exc): """ log an error message: :param exc: exception message """ msg = 'trying to execute a step in the environment: \n' \ ' - Exception: %s' % exc if self.logger is not None: self.logger.error(msg) self.by_console(' ERROR - %s' % msg)
log an error message: :param exc: exception message
def processRequest(cls, ps, **kw): """invokes callback that should return a (request,response) tuple. representing the SOAP request and response respectively. ps -- ParsedSoap instance representing HTTP Body. request -- twisted.web.server.Request """ resource = kw['resource'] request = kw['request'] method = getattr(resource, 'soap_%s' % _get_element_nsuri_name(ps.body_root)[-1]) try: req,rsp = method(ps, request=request) except Exception, ex: raise return rsp
invokes callback that should return a (request,response) tuple. representing the SOAP request and response respectively. ps -- ParsedSoap instance representing HTTP Body. request -- twisted.web.server.Request
def transaction(self, compare, success=None, failure=None): """ Perform a transaction. Example usage: .. code-block:: python etcd.transaction( compare=[ etcd.transactions.value('/doot/testing') == 'doot', etcd.transactions.version('/doot/testing') > 0, ], success=[ etcd.transactions.put('/doot/testing', 'success'), ], failure=[ etcd.transactions.put('/doot/testing', 'failure'), ] ) :param compare: A list of comparisons to make :param success: A list of operations to perform if all the comparisons are true :param failure: A list of operations to perform if any of the comparisons are false :return: A tuple of (operation status, responses) """ compare = [c.build_message() for c in compare] success_ops = self._ops_to_requests(success) failure_ops = self._ops_to_requests(failure) transaction_request = etcdrpc.TxnRequest(compare=compare, success=success_ops, failure=failure_ops) txn_response = self.kvstub.Txn( transaction_request, self.timeout, credentials=self.call_credentials, metadata=self.metadata ) responses = [] for response in txn_response.responses: response_type = response.WhichOneof('response') if response_type in ['response_put', 'response_delete_range', 'response_txn']: responses.append(response) elif response_type == 'response_range': range_kvs = [] for kv in response.response_range.kvs: range_kvs.append((kv.value, KVMetadata(kv, txn_response.header))) responses.append(range_kvs) return txn_response.succeeded, responses
Perform a transaction. Example usage: .. code-block:: python etcd.transaction( compare=[ etcd.transactions.value('/doot/testing') == 'doot', etcd.transactions.version('/doot/testing') > 0, ], success=[ etcd.transactions.put('/doot/testing', 'success'), ], failure=[ etcd.transactions.put('/doot/testing', 'failure'), ] ) :param compare: A list of comparisons to make :param success: A list of operations to perform if all the comparisons are true :param failure: A list of operations to perform if any of the comparisons are false :return: A tuple of (operation status, responses)
def RegisterLateBindingCallback(target_name, callback, **kwargs): """Registers a callback to be invoked when the RDFValue named is declared.""" _LATE_BINDING_STORE.setdefault(target_name, []).append((callback, kwargs))
Registers a callback to be invoked when the RDFValue named is declared.
def on_timer(self, event): '''Main Loop.''' state = self.state self.loopStartTime = time.time() if state.close_event.wait(0.001): self.timer.Stop() self.Destroy() return # Check for resizing self.checkReszie() if self.resized: self.on_idle(0) # Get attitude information while state.child_pipe_recv.poll(): objList = state.child_pipe_recv.recv() for obj in objList: self.calcFontScaling() if isinstance(obj,Attitude): self.oldRoll = self.roll self.pitch = obj.pitch*180/math.pi self.roll = obj.roll*180/math.pi self.yaw = obj.yaw*180/math.pi # Update Roll, Pitch, Yaw Text Text self.updateRPYText() # Recalculate Horizon Polygons self.calcHorizonPoints() # Update Pitch Markers self.adjustPitchmarkers() elif isinstance(obj,VFR_HUD): self.heading = obj.heading self.airspeed = obj.airspeed self.climbRate = obj.climbRate # Update Airpseed, Altitude, Climb Rate Locations self.updateAARText() # Update Heading North Pointer self.adjustHeadingPointer() self.adjustNorthPointer() elif isinstance(obj,Global_Position_INT): self.relAlt = obj.relAlt self.relAltTime = obj.curTime # Update Airpseed, Altitude, Climb Rate Locations self.updateAARText() # Update Altitude History self.updateAltHistory() elif isinstance(obj,BatteryInfo): self.voltage = obj.voltage self.current = obj.current self.batRemain = obj.batRemain # Update Battery Bar self.updateBatteryBar() elif isinstance(obj,FlightState): self.mode = obj.mode self.armed = obj.armState # Update Mode and Arm State Text self.updateStateText() elif isinstance(obj,WaypointInfo): self.currentWP = obj.current self.finalWP = obj.final self.wpDist = obj.currentDist self.nextWPTime = obj.nextWPTime if obj.wpBearing < 0.0: self.wpBearing = obj.wpBearing + 360 else: self.wpBearing = obj.wpBearing # Update waypoint text self.updateWPText() # Adjust Waypoint Pointer self.adjustWPPointer() elif isinstance(obj, FPS): # Update fps target self.fps = obj.fps # Quit Drawing if too early if (time.time() > self.nextTime): # Update Matplotlib Plot self.canvas.draw() self.canvas.Refresh() self.Refresh() self.Update() # Calculate next frame time if (self.fps > 0): fpsTime = 1/self.fps self.nextTime = fpsTime + self.loopStartTime else: self.nextTime = time.time()
Main Loop.
def genl_send_simple(sk, family, cmd, version, flags): """Send a Generic Netlink message consisting only of a header. https://github.com/thom311/libnl/blob/libnl3_2_25/lib/genl/genl.c#L84 This function is a shortcut for sending a Generic Netlink message without any message payload. The message will only consist of the Netlink and Generic Netlink headers. The header is constructed based on the specified parameters and passed on to nl_send_simple() to send it on the specified socket. Positional arguments: sk -- Generic Netlink socket (nl_sock class instance). family -- numeric family identifier (integer). cmd -- numeric command identifier (integer). version -- interface version (integer). flags -- additional Netlink message flags (integer). Returns: 0 on success or a negative error code. """ hdr = genlmsghdr(cmd=cmd, version=version) return int(nl_send_simple(sk, family, flags, hdr, hdr.SIZEOF))
Send a Generic Netlink message consisting only of a header. https://github.com/thom311/libnl/blob/libnl3_2_25/lib/genl/genl.c#L84 This function is a shortcut for sending a Generic Netlink message without any message payload. The message will only consist of the Netlink and Generic Netlink headers. The header is constructed based on the specified parameters and passed on to nl_send_simple() to send it on the specified socket. Positional arguments: sk -- Generic Netlink socket (nl_sock class instance). family -- numeric family identifier (integer). cmd -- numeric command identifier (integer). version -- interface version (integer). flags -- additional Netlink message flags (integer). Returns: 0 on success or a negative error code.
def _resolve(self, path, migration_file): """ Resolve a migration instance from a file. :param migration_file: The migration file :type migration_file: str :rtype: orator.migrations.migration.Migration """ name = "_".join(migration_file.split("_")[4:]) migration_file = os.path.join(path, "%s.py" % migration_file) # Loading parent module parent = os.path.join(path, "__init__.py") if not os.path.exists(parent): with open(parent, "w"): pass load_module("migrations", parent) # Loading module mod = load_module("migrations.%s" % name, migration_file) klass = getattr(mod, inflection.camelize(name)) instance = klass() instance.set_connection(self.get_repository().get_connection()) return instance
Resolve a migration instance from a file. :param migration_file: The migration file :type migration_file: str :rtype: orator.migrations.migration.Migration
def reset_instance_attribute(self, instance_id, attribute): """ Resets an attribute of an instance to its default value. :type instance_id: string :param instance_id: ID of the instance :type attribute: string :param attribute: The attribute to reset. Valid values are: kernel|ramdisk :rtype: bool :return: Whether the operation succeeded or not """ params = {'InstanceId' : instance_id, 'Attribute' : attribute} return self.get_status('ResetInstanceAttribute', params, verb='POST')
Resets an attribute of an instance to its default value. :type instance_id: string :param instance_id: ID of the instance :type attribute: string :param attribute: The attribute to reset. Valid values are: kernel|ramdisk :rtype: bool :return: Whether the operation succeeded or not
def _log_message(self, level, freerun_entry, msg): """ method performs logging into log file and the freerun_entry """ self.logger.log(level, msg) assert isinstance(freerun_entry, FreerunProcessEntry) event_log = freerun_entry.event_log if len(event_log) > MAX_NUMBER_OF_EVENTS: del event_log[-1] event_log.insert(0, msg) self.freerun_process_dao.update(freerun_entry)
method performs logging into log file and the freerun_entry
def sendall_stderr(self, s): """ Send data to the channel's "stderr" stream, without allowing partial results. Unlike L{send_stderr}, this method continues to send data from the given string until all data has been sent or an error occurs. Nothing is returned. @param s: data to send to the client as "stderr" output. @type s: str @raise socket.timeout: if sending stalled for longer than the timeout set by L{settimeout}. @raise socket.error: if an error occured before the entire string was sent. @since: 1.1 """ while s: if self.closed: raise socket.error('Socket is closed') sent = self.send_stderr(s) s = s[sent:] return None
Send data to the channel's "stderr" stream, without allowing partial results. Unlike L{send_stderr}, this method continues to send data from the given string until all data has been sent or an error occurs. Nothing is returned. @param s: data to send to the client as "stderr" output. @type s: str @raise socket.timeout: if sending stalled for longer than the timeout set by L{settimeout}. @raise socket.error: if an error occured before the entire string was sent. @since: 1.1
def _dispatch(name, *args, **kwargs): """ Dispatch to apply. """ def outer(self, *args, **kwargs): def f(x): x = self._shallow_copy(x, groupby=self._groupby) return getattr(x, name)(*args, **kwargs) return self._groupby.apply(f) outer.__name__ = name return outer
Dispatch to apply.
def get_status(self): ''' Get the status of the report and its sub-reports. :rtype: str :return: report status ('passed', 'failed' or 'error') ''' status = self.get('status') if status == Report.PASSED: for sr_name in self._sub_reports: sr = self._sub_reports[sr_name] sr_status = sr.get_status() reason = sr.get('reason') if sr_status == Report.ERROR: self.error(reason) break if sr_status == Report.FAILED: self.failed(reason) break status = self.get('status') return status
Get the status of the report and its sub-reports. :rtype: str :return: report status ('passed', 'failed' or 'error')
def has_in_collaborators(self, collaborator): """ :calls: `GET /repos/:owner/:repo/collaborators/:user <http://developer.github.com/v3/repos/collaborators>`_ :param collaborator: string or :class:`github.NamedUser.NamedUser` :rtype: bool """ assert isinstance(collaborator, github.NamedUser.NamedUser) or isinstance(collaborator, (str, unicode)), collaborator if isinstance(collaborator, github.NamedUser.NamedUser): collaborator = collaborator._identity status, headers, data = self._requester.requestJson( "GET", self.url + "/collaborators/" + collaborator ) return status == 204
:calls: `GET /repos/:owner/:repo/collaborators/:user <http://developer.github.com/v3/repos/collaborators>`_ :param collaborator: string or :class:`github.NamedUser.NamedUser` :rtype: bool
def _get_kdjd(cls, df, n_days): """ Get the D of KDJ D = 2/3 × (prev. D) +1/3 × (curr. K) 2/3 and 1/3 are the smooth parameters. :param df: data :param n_days: calculation range :return: None """ k_column = 'kdjk_{}'.format(n_days) d_column = 'kdjd_{}'.format(n_days) df[d_column] = list(cls._calc_kd(df.get(k_column)))
Get the D of KDJ D = 2/3 × (prev. D) +1/3 × (curr. K) 2/3 and 1/3 are the smooth parameters. :param df: data :param n_days: calculation range :return: None
def main(): """Measure capnp serialization performance of a network containing a simple python region that in-turn contains a Random instance. """ engine.Network.registerPyRegion(__name__, SerializationTestPyRegion.__name__) try: _runTest() finally: engine.Network.unregisterPyRegion(SerializationTestPyRegion.__name__)
Measure capnp serialization performance of a network containing a simple python region that in-turn contains a Random instance.
def _advance_new_study_id(self): """ ASSUMES the caller holds the _doc_counter_lock ! Returns the current numeric part of the next study ID, advances the counter to the next value, and stores that value in the file in case the server is restarted. """ c = self._next_study_id self._next_study_id = 1 + c content = u'{"next_study_id": %d}\n' % self._next_study_id # The content is JSON, but we hand-rolled the string above # so that we can use it as a commit_msg self._write_master_branch_resource(content, self._id_minting_file, commit_msg=content, is_json=False) return c
ASSUMES the caller holds the _doc_counter_lock ! Returns the current numeric part of the next study ID, advances the counter to the next value, and stores that value in the file in case the server is restarted.
def countByValue(self): """Apply countByValue to every RDD.abs :rtype: DStream .. warning:: Implemented as a local operation. Example: >>> import pysparkling >>> sc = pysparkling.Context() >>> ssc = pysparkling.streaming.StreamingContext(sc, 0.1) >>> ( ... ssc ... .queueStream([[1, 1, 5, 5, 5, 2]]) ... .countByValue() ... .foreachRDD(lambda rdd: print(sorted(rdd.collect()))) ... ) >>> ssc.start() >>> ssc.awaitTermination(0.15) [(1, 2), (2, 1), (5, 3)] """ return self.transform( lambda rdd: self._context._context.parallelize( rdd.countByValue().items()))
Apply countByValue to every RDD.abs :rtype: DStream .. warning:: Implemented as a local operation. Example: >>> import pysparkling >>> sc = pysparkling.Context() >>> ssc = pysparkling.streaming.StreamingContext(sc, 0.1) >>> ( ... ssc ... .queueStream([[1, 1, 5, 5, 5, 2]]) ... .countByValue() ... .foreachRDD(lambda rdd: print(sorted(rdd.collect()))) ... ) >>> ssc.start() >>> ssc.awaitTermination(0.15) [(1, 2), (2, 1), (5, 3)]
def container_rename_folder(object_id, input_params={}, always_retry=False, **kwargs): """ Invokes the /container-xxxx/renameFolder API method. For more info, see: https://wiki.dnanexus.com/API-Specification-v1.0.0/Folders-and-Deletion#API-method%3A-%2Fclass-xxxx%2FrenameFolder """ return DXHTTPRequest('/%s/renameFolder' % object_id, input_params, always_retry=always_retry, **kwargs)
Invokes the /container-xxxx/renameFolder API method. For more info, see: https://wiki.dnanexus.com/API-Specification-v1.0.0/Folders-and-Deletion#API-method%3A-%2Fclass-xxxx%2FrenameFolder
def set_fc_volume(self, port_id, target_wwn, target_lun=0, boot_prio=1, initiator_wwnn=None, initiator_wwpn=None): """Set FibreChannel volume information to configuration. :param port_id: Physical port ID. :param target_wwn: WWN of target. :param target_lun: LUN number of target. :param boot_prio: Boot priority of the volume. 1 indicates the highest priority. :param initiator_wwnn: Virtual WWNN for initiator if necessary. :param initiator_wwpn: Virtual WWPN for initiator if necessary. """ port_handler = _parse_physical_port_id(port_id) fc_target = elcm.FCTarget(target_wwn, target_lun) fc_boot = elcm.FCBoot(boot_prio=boot_prio, boot_enable=True) fc_boot.add_target(fc_target) port = self._find_port(port_handler) if port: port_handler.set_fc_port(port, fc_boot, wwnn=initiator_wwnn, wwpn=initiator_wwpn) else: port = port_handler.create_fc_port(fc_boot, wwnn=initiator_wwnn, wwpn=initiator_wwpn) self._add_port(port_handler, port)
Set FibreChannel volume information to configuration. :param port_id: Physical port ID. :param target_wwn: WWN of target. :param target_lun: LUN number of target. :param boot_prio: Boot priority of the volume. 1 indicates the highest priority. :param initiator_wwnn: Virtual WWNN for initiator if necessary. :param initiator_wwpn: Virtual WWPN for initiator if necessary.
async def on_raw_301(self, message): """ User is away. """ target, nickname, message = message.params info = { 'away': True, 'away_message': message } if nickname in self.users: self._sync_user(nickname, info) if nickname in self._pending['whois']: self._whois_info[nickname].update(info)
User is away.
def get_jvm_options(self): """Return the options to run this JVM with. These are options to the JVM itself, such as -Dfoo=bar, -Xmx=1g, -XX:-UseParallelGC and so on. Thus named because get_options() already exists (and returns this object's Pants options). """ ret = [] for opt in self.get_options().options: ret.extend(safe_shlex_split(opt)) if (self.get_options().debug or self.get_options().is_flagged('debug_port') or self.get_options().is_flagged('debug_args')): debug_port = self.get_options().debug_port ret.extend(arg.format(debug_port=debug_port) for arg in self.get_options().debug_args) return ret
Return the options to run this JVM with. These are options to the JVM itself, such as -Dfoo=bar, -Xmx=1g, -XX:-UseParallelGC and so on. Thus named because get_options() already exists (and returns this object's Pants options).
def QA_util_stamp2datetime(timestamp): """ datestamp转datetime pandas转出来的timestamp是13位整数 要/1000 It’s common for this to be restricted to years from 1970 through 2038. 从1970年开始的纳秒到当前的计数 转变成 float 类型时间 类似 time.time() 返回的类型 :param timestamp: long类型 :return: 类型float """ try: return datetime.datetime.fromtimestamp(timestamp) except Exception as e: # it won't work ?? try: return datetime.datetime.fromtimestamp(timestamp / 1000) except: try: return datetime.datetime.fromtimestamp(timestamp / 1000000) except: return datetime.datetime.fromtimestamp(timestamp / 1000000000)
datestamp转datetime pandas转出来的timestamp是13位整数 要/1000 It’s common for this to be restricted to years from 1970 through 2038. 从1970年开始的纳秒到当前的计数 转变成 float 类型时间 类似 time.time() 返回的类型 :param timestamp: long类型 :return: 类型float
def idfreader1(fname, iddfile, theidf, conv=True, commdct=None, block=None): """read idf file and return bunches""" versiontuple = iddversiontuple(iddfile) # import pdb; pdb.set_trace() block, data, commdct, idd_index = readidf.readdatacommdct1( fname, iddfile=iddfile, commdct=commdct, block=block) if conv: convertallfields(data, commdct, block) # fill gaps in idd ddtt, dtls = data.dt, data.dtls if versiontuple < (8,): skiplist = ["TABLE:MULTIVARIABLELOOKUP"] else: skiplist = None nofirstfields = iddgaps.missingkeys_standard( commdct, dtls, skiplist=skiplist) iddgaps.missingkeys_nonstandard(block, commdct, dtls, nofirstfields) # bunchdt = makebunches(data, commdct) bunchdt = makebunches_alter(data, commdct, theidf) return bunchdt, block, data, commdct, idd_index, versiontuple
read idf file and return bunches
def load(stream): """Parse the LHA document and produce the corresponding Python object. Accepts a string or a file-like object.""" if isinstance(stream, str): string = stream else: string = stream.read() tokens = tokenize(string) return parse(tokens)
Parse the LHA document and produce the corresponding Python object. Accepts a string or a file-like object.
def update_notification_list(self, apps=None, schema_editor=None, verbose=False): """Updates the notification model to ensure all registered notifications classes are listed. Typically called from a post_migrate signal. Also, in tests you can register a notification and the Notification class (not model) will automatically call this method if the named notification does not exist. See notification.notify() """ Notification = (apps or django_apps).get_model("edc_notification.notification") # flag all notifications as disabled and re-enable as required Notification.objects.all().update(enabled=False) if site_notifications.loaded: if verbose: sys.stdout.write( style.MIGRATE_HEADING("Populating Notification model:\n") ) self.delete_unregistered_notifications(apps=apps) for name, notification_cls in site_notifications.registry.items(): if verbose: sys.stdout.write( f" * Adding '{name}': '{notification_cls().display_name}'\n" ) try: obj = Notification.objects.get(name=name) except ObjectDoesNotExist: Notification.objects.create( name=name, display_name=notification_cls().display_name, enabled=True, ) else: obj.display_name = notification_cls().display_name obj.enabled = True obj.save()
Updates the notification model to ensure all registered notifications classes are listed. Typically called from a post_migrate signal. Also, in tests you can register a notification and the Notification class (not model) will automatically call this method if the named notification does not exist. See notification.notify()
def domain(self, expparams): """ Returns a list of ``Domain``s, one for each input expparam. :param numpy.ndarray expparams: Array of experimental parameters. This array must be of dtype agreeing with the ``expparams_dtype`` property, or, in the case where ``n_outcomes_constant`` is ``True``, ``None`` should be a valid input. :rtype: list of ``Domain`` """ return [IntegerDomain(min=0,max=n_o-1) for n_o in self.n_outcomes(expparams)]
Returns a list of ``Domain``s, one for each input expparam. :param numpy.ndarray expparams: Array of experimental parameters. This array must be of dtype agreeing with the ``expparams_dtype`` property, or, in the case where ``n_outcomes_constant`` is ``True``, ``None`` should be a valid input. :rtype: list of ``Domain``
def __find_hidden_analyses(self, docs): """ Jätab meelde, millised analüüsid on nn peidetud ehk siis mida ei tule arvestada lemmade järelühestamisel: *) kesksõnade nud, dud, tud mitmesused; *) muutumatute sõnade sõnaliigi mitmesus; *) oleviku 'olema' mitmesus ('nad on' vs 'ta on'); *) asesõnade ainsuse-mitmuse mitmesus; *) arv- ja asesõnade vaheline mitmesus; Tagastab sõnastiku peidetud analüüse sisaldanud sõnade asukohtadega, iga võti kujul (doc_index, word_index); """ hidden = dict() nudTudLopud = re.compile('^.*[ntd]ud$') for d in range(len(docs)): for w in range(len(docs[d][WORDS])): word = docs[d][WORDS][w] if ANALYSIS in word and len(word[ANALYSIS]) > 1: # # 1) Kui enamus analüüse on nud/tud/dud analüüsid, peida mitmesus: # kõla+nud //_V_ nud, // kõla=nud+0 //_A_ // kõla=nud+0 //_A_ sg n, // kõla=nud+d //_A_ pl n, // nudTud = [ nudTudLopud.match(a[ROOT]) != None or \ nudTudLopud.match(a[ENDING]) != None \ for a in word[ANALYSIS] ] if nudTud.count( True ) > 1: hidden[(d, w)] = 1 # # 2) Kui analyysidel on sama lemma ja puudub vormitunnus, siis peida mitmesused ära: # Nt kui+0 //_D_ // kui+0 //_J_ // # nagu+0 //_D_ // nagu+0 //_J_ // lemmas = set([ a[ROOT] for a in word[ANALYSIS] ]) forms = set([ a[FORM] for a in word[ANALYSIS] ]) if len(lemmas) == 1 and len(forms) == 1 and (list(forms))[0] == '': hidden[(d, w)] = 1 # # 3) Kui 'olema'-analyysidel on sama lemma ning sama l6pp, peida mitmesused: # Nt 'nad on' vs 'ta on' saavad sama olema-analyysi, mis jääb mitmeseks; endings = set([ a[ENDING] for a in word[ANALYSIS] ]) if len(lemmas) == 1 and (list(lemmas))[0] == 'ole' and len(endings) == 1 \ and (list(endings))[0] == '0': hidden[(d, w)] = 1 # # 4) Kui asesõnadel on sama lemma ja lõpp, peida ainsuse/mitmuse mitmesus: # Nt kõik+0 //_P_ sg n // kõik+0 //_P_ pl n // # kes+0 //_P_ sg n // kes+0 //_P_ pl n // postags = set([ a[POSTAG] for a in word[ANALYSIS] ]) if len(lemmas) == 1 and len(postags) == 1 and 'P' in postags and \ len(endings) == 1: hidden[(d, w)] = 1 # # 5) Kui on sama lemma ja lõpp, peida arv- ja asesõnadevaheline mitmesus: # Nt teine+0 //_O_ pl n, // teine+0 //_P_ pl n, // # üks+l //_N_ sg ad, // üks+l //_P_ sg ad, // if len(lemmas) == 1 and 'P' in postags and ('O' in postags or \ 'N' in postags) and len(endings) == 1: hidden[(d, w)] = 1 return hidden
Jätab meelde, millised analüüsid on nn peidetud ehk siis mida ei tule arvestada lemmade järelühestamisel: *) kesksõnade nud, dud, tud mitmesused; *) muutumatute sõnade sõnaliigi mitmesus; *) oleviku 'olema' mitmesus ('nad on' vs 'ta on'); *) asesõnade ainsuse-mitmuse mitmesus; *) arv- ja asesõnade vaheline mitmesus; Tagastab sõnastiku peidetud analüüse sisaldanud sõnade asukohtadega, iga võti kujul (doc_index, word_index);
async def create_source_event_stream( schema: GraphQLSchema, document: DocumentNode, root_value: Any = None, context_value: Any = None, variable_values: Dict[str, Any] = None, operation_name: str = None, field_resolver: GraphQLFieldResolver = None, ) -> Union[AsyncIterable[Any], ExecutionResult]: """Create source even stream Implements the "CreateSourceEventStream" algorithm described in the GraphQL specification, resolving the subscription source event stream. Returns a coroutine that yields an AsyncIterable. If the client provided invalid arguments, the source stream could not be created, or the resolver did not return an AsyncIterable, this function will throw an error, which should be caught and handled by the caller. A Source Event Stream represents a sequence of events, each of which triggers a GraphQL execution for that event. This may be useful when hosting the stateful subscription service in a different process or machine than the stateless GraphQL execution engine, or otherwise separating these two steps. For more on this, see the "Supporting Subscriptions at Scale" information in the GraphQL spec. """ # If arguments are missing or incorrectly typed, this is an internal developer # mistake which should throw an early error. assert_valid_execution_arguments(schema, document, variable_values) # If a valid context cannot be created due to incorrect arguments, this will throw # an error. context = ExecutionContext.build( schema, document, root_value, context_value, variable_values, operation_name, field_resolver, ) # Return early errors if execution context failed. if isinstance(context, list): return ExecutionResult(data=None, errors=context) type_ = get_operation_root_type(schema, context.operation) fields = context.collect_fields(type_, context.operation.selection_set, {}, set()) response_names = list(fields) response_name = response_names[0] field_nodes = fields[response_name] field_node = field_nodes[0] field_name = field_node.name.value field_def = get_field_def(schema, type_, field_name) if not field_def: raise GraphQLError( f"The subscription field '{field_name}' is not defined.", field_nodes ) # Call the `subscribe()` resolver or the default resolver to produce an # AsyncIterable yielding raw payloads. resolve_fn = field_def.subscribe or context.field_resolver resolve_fn = cast(GraphQLFieldResolver, resolve_fn) # help mypy path = add_path(None, response_name) info = context.build_resolve_info(field_def, field_nodes, type_, path) # `resolve_field_value_or_error` implements the "ResolveFieldEventStream" algorithm # from GraphQL specification. It differs from `resolve_field_value` due to # providing a different `resolve_fn`. result = context.resolve_field_value_or_error( field_def, field_nodes, resolve_fn, root_value, info ) event_stream = await cast(Awaitable, result) if isawaitable(result) else result # If `event_stream` is an Error, rethrow a located error. if isinstance(event_stream, Exception): raise located_error(event_stream, field_nodes, response_path_as_list(path)) # Assert field returned an event stream, otherwise yield an error. if isinstance(event_stream, AsyncIterable): return cast(AsyncIterable, event_stream) raise TypeError( f"Subscription field must return AsyncIterable. Received: {event_stream!r}" )
Create source even stream Implements the "CreateSourceEventStream" algorithm described in the GraphQL specification, resolving the subscription source event stream. Returns a coroutine that yields an AsyncIterable. If the client provided invalid arguments, the source stream could not be created, or the resolver did not return an AsyncIterable, this function will throw an error, which should be caught and handled by the caller. A Source Event Stream represents a sequence of events, each of which triggers a GraphQL execution for that event. This may be useful when hosting the stateful subscription service in a different process or machine than the stateless GraphQL execution engine, or otherwise separating these two steps. For more on this, see the "Supporting Subscriptions at Scale" information in the GraphQL spec.
def _case_stmt(self, stmt: Statement, sctx: SchemaContext) -> None: """Handle case statement.""" self._handle_child(CaseNode(), stmt, sctx)
Handle case statement.
def wrap_kwargs_to_initdict(init_kwargs_fn: InitKwargsFnType, typename: str, check_result: bool = True) \ -> InstanceToInitDictFnType: """ Wraps a function producing a ``KwargsDict``, making it into a function producing an ``InitDict``. """ def wrapper(obj: Instance) -> InitDict: result = init_kwargs_fn(obj) if check_result: if not isinstance(result, dict): raise ValueError( "Class {} failed to provide a kwargs dict and " "provided instead: {}".format(typename, repr(result))) return kwargs_to_initdict(init_kwargs_fn(obj)) return wrapper
Wraps a function producing a ``KwargsDict``, making it into a function producing an ``InitDict``.
def primal_and_adjoint_for_tracing(self, node): """Build the primal and adjoint of a traceable function. Args: node: ast.Call node of a function we wish to trace, instead of transform Returns: primal: new ast.Assign node to replace the original primal call adjoint: new ast.Assign node using the VJP generated in primal to calculate the adjoint. """ primal_template = grads.primals[tracing.Traceable] adjoint_template = grads.adjoints[tracing.Traceable] # Prep to_pack = node.args target = ast_.copy_node(self.orig_target) vjp = quoting.quote(self.namer.unique('%s_grad' % node.func.id)) tmp = create.create_temp(quoting.quote('tmp'), self.namer) assert len(node.keywords) == 0 # Full replacement of primal # TODO: do we need to set 'pri_call' on this? primal = template.replace( primal_template, namer=self.namer, result=target, fn=node.func, tmp=tmp, vjp=vjp, args=gast.Tuple(elts=to_pack, ctx=gast.Load())) # Building adjoint using the vjp generated with the primal dto_pack = gast.Tuple( elts=[create.create_temp_grad(arg, self.namer) for arg in to_pack], ctx=gast.Store()) adjoint = template.replace( adjoint_template, namer=self.namer, result=target, vjp=vjp, dargs=dto_pack) return primal, adjoint
Build the primal and adjoint of a traceable function. Args: node: ast.Call node of a function we wish to trace, instead of transform Returns: primal: new ast.Assign node to replace the original primal call adjoint: new ast.Assign node using the VJP generated in primal to calculate the adjoint.
def _location_purge_all(delete=False, verbosity=0): """Purge all data locations.""" if DataLocation.objects.exists(): for location in DataLocation.objects.filter(Q(purged=False) | Q(data=None)): location_purge(location.id, delete, verbosity) else: logger.info("No data locations")
Purge all data locations.
def deny(cls, action, **kwargs): """Deny the given action need. :param action: The action to deny. :returns: A :class:`invenio_access.models.ActionNeedMixin` instance. """ return cls.create(action, exclude=True, **kwargs)
Deny the given action need. :param action: The action to deny. :returns: A :class:`invenio_access.models.ActionNeedMixin` instance.
def revnet_164_cifar(): """Tiny hparams suitable for CIFAR/etc.""" hparams = revnet_cifar_base() hparams.bottleneck = True hparams.num_channels = [16, 32, 64] hparams.num_layers_per_block = [8, 8, 8] return hparams
Tiny hparams suitable for CIFAR/etc.
def run_tm_noise_experiment(dim = 2048, cellsPerColumn=1, num_active = 40, activationThreshold=16, initialPermanence=0.8, connectedPermanence=0.50, minThreshold=16, maxNewSynapseCount=20, permanenceIncrement=0.05, permanenceDecrement=0.00, predictedSegmentDecrement=0.000, maxSegmentsPerCell=255, maxSynapsesPerSegment=255, seed=42, num_samples = 1, num_trials = 1000, sequence_length = 20, training_iters = 1, automatic_threshold = False, noise_range = range(0, 100, 5)): """ Run an experiment tracking the performance of the temporal memory given noise. The number of active cells and the dimensions of the TM are fixed. We track performance by comparing the cells predicted to be active with the cells actually active in the sequence without noise at every timestep, and averaging across timesteps. Three metrics are used, correlation (Pearson's r, by numpy.corrcoef), set similarity (Jaccard index) and cosine similarity (using scipy.spatial.distance.cosine). The Jaccard set similarity is the canonical metric used in the paper, but all three metrics tend to produce very similar results. Typically, this experiment is run to test the influence of activation threshold on noise tolerance, with multiple different thresholds tested. However, this experiment could also be used to examine the influence of factors such as sparsity and sequence length. Output is written to tm_noise_{threshold}}.txt, including sample size. We used three different activation threshold settings, 8, 12 and 16, mirroring the parameters used in the Poirazi neuron model experiment. """ if automatic_threshold: activationThreshold = min(num_active/2, maxNewSynapseCount/2) minThreshold = min(num_active/2, maxNewSynapseCount/2) for noise in noise_range: print noise for trial in range(num_trials): tm = TM(columnDimensions=(dim,), cellsPerColumn=cellsPerColumn, activationThreshold=activationThreshold, initialPermanence=initialPermanence, connectedPermanence=connectedPermanence, minThreshold=minThreshold, maxNewSynapseCount=maxNewSynapseCount, permanenceIncrement=permanenceIncrement, permanenceDecrement=permanenceDecrement, predictedSegmentDecrement=predictedSegmentDecrement, maxSegmentsPerCell=maxSegmentsPerCell, maxSynapsesPerSegment=maxSynapsesPerSegment, )#seed=seed) datapoints = [] canonical_active_cells = [] for sample in range(num_samples): data = generate_evenly_distributed_data_sparse(dim = dim, num_active = num_active, num_samples = sequence_length) datapoints.append(data) for i in range(training_iters): for j in range(data.nRows()): activeColumns = set(data.rowNonZeros(j)[0]) tm.compute(activeColumns, learn = True) tm.reset() current_active_cells = [] for j in range(data.nRows()): activeColumns = set(data.rowNonZeros(j)[0]) tm.compute(activeColumns, learn = True) current_active_cells.append(tm.getActiveCells()) canonical_active_cells.append(current_active_cells) tm.reset() # Now that the TM has been trained, check its performance on each sequence with noise added. correlations = [] similarities = [] csims = [] for datapoint, active_cells in zip(datapoints, canonical_active_cells): data = copy.deepcopy(datapoint) apply_noise(data, noise) predicted_cells = [] for j in range(data.nRows()): activeColumns = set(data.rowNonZeros(j)[0]) tm.compute(activeColumns, learn = False) predicted_cells.append(tm.getPredictiveCells()) similarity = [(0.+len(set(predicted) & set(active)))/len((set(predicted) | set(active))) for predicted, active in zip (predicted_cells[:-1], active_cells[1:])] dense_predicted_cells = convert_cell_lists_to_dense(2048*32, predicted_cells[:-1]) dense_active_cells = convert_cell_lists_to_dense(2048*32, active_cells[1:]) correlation = [numpy.corrcoef(numpy.asarray([predicted, active]))[0, 1] for predicted, active in zip(dense_predicted_cells, dense_active_cells)] csim = [1 - cosine(predicted, active) for predicted, active in zip(dense_predicted_cells, dense_active_cells)] correlation = numpy.nan_to_num(correlation) csim = numpy.nan_to_num(csim) correlations.append(numpy.mean(correlation)) similarities.append(numpy.mean(similarity)) csims.append(numpy.mean(csim)) correlation = numpy.mean(correlations) similarity = numpy.mean(similarities) csim = numpy.mean(csims) with open("tm_noise_{}.txt".format(activationThreshold), "a") as f: f.write(str(noise)+", " + str(correlation) + ", " + str(similarity) + ", " + str(csim) + ", " + str(num_trials) + "\n")
Run an experiment tracking the performance of the temporal memory given noise. The number of active cells and the dimensions of the TM are fixed. We track performance by comparing the cells predicted to be active with the cells actually active in the sequence without noise at every timestep, and averaging across timesteps. Three metrics are used, correlation (Pearson's r, by numpy.corrcoef), set similarity (Jaccard index) and cosine similarity (using scipy.spatial.distance.cosine). The Jaccard set similarity is the canonical metric used in the paper, but all three metrics tend to produce very similar results. Typically, this experiment is run to test the influence of activation threshold on noise tolerance, with multiple different thresholds tested. However, this experiment could also be used to examine the influence of factors such as sparsity and sequence length. Output is written to tm_noise_{threshold}}.txt, including sample size. We used three different activation threshold settings, 8, 12 and 16, mirroring the parameters used in the Poirazi neuron model experiment.
def _SetCompleted(self): """Atomically marks the breakpoint as completed. Returns: True if the breakpoint wasn't marked already completed or False if the breakpoint was already completed. """ with self._lock: if self._completed: return False self._completed = True return True
Atomically marks the breakpoint as completed. Returns: True if the breakpoint wasn't marked already completed or False if the breakpoint was already completed.
def set_hyperparams(self, new_params): """Sets the free hyperparameters to the new parameter values in new_params. Parameters ---------- new_params : :py:class:`Array` or other Array-like, (len(:py:attr:`self.free_params`),) New parameter values, ordered as dictated by the docstring for the class. """ new_params = scipy.asarray(new_params, dtype=float) if len(new_params) == len(self.free_params): if self.enforce_bounds: for idx, new_param, bound in zip(range(0, len(new_params)), new_params, self.free_param_bounds): if bound[0] is not None and new_param < bound[0]: new_params[idx] = bound[0] elif bound[1] is not None and new_param > bound[1]: new_params[idx] = bound[1] self.params[~self.fixed_params] = new_params else: raise ValueError("Length of new_params must be %s!" % (len(self.free_params),))
Sets the free hyperparameters to the new parameter values in new_params. Parameters ---------- new_params : :py:class:`Array` or other Array-like, (len(:py:attr:`self.free_params`),) New parameter values, ordered as dictated by the docstring for the class.
def unique(values): """ Hash table-based unique. Uniques are returned in order of appearance. This does NOT sort. Significantly faster than numpy.unique. Includes NA values. Parameters ---------- values : 1d array-like Returns ------- numpy.ndarray or ExtensionArray The return can be: * Index : when the input is an Index * Categorical : when the input is a Categorical dtype * ndarray : when the input is a Series/ndarray Return numpy.ndarray or ExtensionArray. See Also -------- Index.unique Series.unique Examples -------- >>> pd.unique(pd.Series([2, 1, 3, 3])) array([2, 1, 3]) >>> pd.unique(pd.Series([2] + [1] * 5)) array([2, 1]) >>> pd.unique(pd.Series([pd.Timestamp('20160101'), ... pd.Timestamp('20160101')])) array(['2016-01-01T00:00:00.000000000'], dtype='datetime64[ns]') >>> pd.unique(pd.Series([pd.Timestamp('20160101', tz='US/Eastern'), ... pd.Timestamp('20160101', tz='US/Eastern')])) array([Timestamp('2016-01-01 00:00:00-0500', tz='US/Eastern')], dtype=object) >>> pd.unique(pd.Index([pd.Timestamp('20160101', tz='US/Eastern'), ... pd.Timestamp('20160101', tz='US/Eastern')])) DatetimeIndex(['2016-01-01 00:00:00-05:00'], ... dtype='datetime64[ns, US/Eastern]', freq=None) >>> pd.unique(list('baabc')) array(['b', 'a', 'c'], dtype=object) An unordered Categorical will return categories in the order of appearance. >>> pd.unique(pd.Series(pd.Categorical(list('baabc')))) [b, a, c] Categories (3, object): [b, a, c] >>> pd.unique(pd.Series(pd.Categorical(list('baabc'), ... categories=list('abc')))) [b, a, c] Categories (3, object): [b, a, c] An ordered Categorical preserves the category ordering. >>> pd.unique(pd.Series(pd.Categorical(list('baabc'), ... categories=list('abc'), ... ordered=True))) [b, a, c] Categories (3, object): [a < b < c] An array of tuples >>> pd.unique([('a', 'b'), ('b', 'a'), ('a', 'c'), ('b', 'a')]) array([('a', 'b'), ('b', 'a'), ('a', 'c')], dtype=object) """ values = _ensure_arraylike(values) if is_extension_array_dtype(values): # Dispatch to extension dtype's unique. return values.unique() original = values htable, _, values, dtype, ndtype = _get_hashtable_algo(values) table = htable(len(values)) uniques = table.unique(values) uniques = _reconstruct_data(uniques, dtype, original) return uniques
Hash table-based unique. Uniques are returned in order of appearance. This does NOT sort. Significantly faster than numpy.unique. Includes NA values. Parameters ---------- values : 1d array-like Returns ------- numpy.ndarray or ExtensionArray The return can be: * Index : when the input is an Index * Categorical : when the input is a Categorical dtype * ndarray : when the input is a Series/ndarray Return numpy.ndarray or ExtensionArray. See Also -------- Index.unique Series.unique Examples -------- >>> pd.unique(pd.Series([2, 1, 3, 3])) array([2, 1, 3]) >>> pd.unique(pd.Series([2] + [1] * 5)) array([2, 1]) >>> pd.unique(pd.Series([pd.Timestamp('20160101'), ... pd.Timestamp('20160101')])) array(['2016-01-01T00:00:00.000000000'], dtype='datetime64[ns]') >>> pd.unique(pd.Series([pd.Timestamp('20160101', tz='US/Eastern'), ... pd.Timestamp('20160101', tz='US/Eastern')])) array([Timestamp('2016-01-01 00:00:00-0500', tz='US/Eastern')], dtype=object) >>> pd.unique(pd.Index([pd.Timestamp('20160101', tz='US/Eastern'), ... pd.Timestamp('20160101', tz='US/Eastern')])) DatetimeIndex(['2016-01-01 00:00:00-05:00'], ... dtype='datetime64[ns, US/Eastern]', freq=None) >>> pd.unique(list('baabc')) array(['b', 'a', 'c'], dtype=object) An unordered Categorical will return categories in the order of appearance. >>> pd.unique(pd.Series(pd.Categorical(list('baabc')))) [b, a, c] Categories (3, object): [b, a, c] >>> pd.unique(pd.Series(pd.Categorical(list('baabc'), ... categories=list('abc')))) [b, a, c] Categories (3, object): [b, a, c] An ordered Categorical preserves the category ordering. >>> pd.unique(pd.Series(pd.Categorical(list('baabc'), ... categories=list('abc'), ... ordered=True))) [b, a, c] Categories (3, object): [a < b < c] An array of tuples >>> pd.unique([('a', 'b'), ('b', 'a'), ('a', 'c'), ('b', 'a')]) array([('a', 'b'), ('b', 'a'), ('a', 'c')], dtype=object)
def download(name, options): """ download a file or all files in a directory """ dire = os.path.dirname(name) # returns the directory name fName = os.path.basename(name) # returns the filename fNameOnly, fExt = os.path.splitext(fName) dwn = 0 if fileExists(fName, dire) and not fileExists((fNameOnly + '.srt'), dire): # skip if already downloaded if file_downloaded(download_file(fName, options.timeout, dire), fName, options.verbose): dwn += 1 elif dirExists(name): for filename in os.listdir(name): if options.recursive: dwn += download(os.path.join(name, filename), options) else: if file_downloaded(download_file(filename, options.timeout, name), filename, options.verbose): dwn += 1 return dwn
download a file or all files in a directory
def main(): """Main""" import sys import h5py handler = logging.StreamHandler(sys.stderr) formatter = logging.Formatter(fmt=_DEFAULT_LOG_FORMAT, datefmt=_DEFAULT_TIME_FORMAT) handler.setFormatter(formatter) handler.setLevel(logging.DEBUG) LOG.setLevel(logging.DEBUG) LOG.addHandler(handler) platform_name = "NOAA-20" # platform_name = "Suomi-NPP" viirs = ViirsRSR('M1', platform_name) filename = os.path.join(viirs.output_dir, "rsr_viirs_{0}.h5".format(platform_name)) with h5py.File(filename, "w") as h5f: h5f.attrs['description'] = 'Relative Spectral Responses for VIIRS' h5f.attrs['platform_name'] = platform_name h5f.attrs['sensor'] = 'viirs' h5f.attrs['band_names'] = VIIRS_BAND_NAMES for chname in VIIRS_BAND_NAMES: viirs = ViirsRSR(chname, platform_name) grp = h5f.create_group(chname) grp.attrs['number_of_detectors'] = len(viirs.rsr.keys()) # Loop over each detector to check if the sampling wavelengths are # identical: det_names = viirs.rsr.keys() wvl = viirs.rsr[det_names[0]]['wavelength'] wvl, idx = np.unique(wvl, return_index=True) wvl_is_constant = True for det in det_names[1:]: det_wvl = np.unique(viirs.rsr[det]['wavelength']) if not np.alltrue(wvl == det_wvl): LOG.warning( "Wavelngth arrays are not the same among detectors!") wvl_is_constant = False if wvl_is_constant: arr = wvl dset = grp.create_dataset('wavelength', arr.shape, dtype='f') dset.attrs['unit'] = 'm' dset.attrs['scale'] = 1e-06 dset[...] = arr # Loop over each detector: for det in viirs.rsr: det_grp = grp.create_group(det) wvl = viirs.rsr[det]['wavelength'][ ~np.isnan(viirs.rsr[det]['wavelength'])] rsp = viirs.rsr[det]['response'][ ~np.isnan(viirs.rsr[det]['wavelength'])] wvl, idx = np.unique(wvl, return_index=True) rsp = np.take(rsp, idx) LOG.debug("wvl.shape: %s", str(wvl.shape)) det_grp.attrs[ 'central_wavelength'] = get_central_wave(wvl, rsp) if not wvl_is_constant: arr = wvl dset = det_grp.create_dataset( 'wavelength', arr.shape, dtype='f') dset.attrs['unit'] = 'm' dset.attrs['scale'] = 1e-06 dset[...] = arr dset = det_grp.create_dataset('response', rsp.shape, dtype='f') dset[...] = rsp
Main
def dict2dzn( objs, declare=False, assign=True, declare_enums=True, wrap=True, fout=None ): """Serializes the objects in input and produces a list of strings encoding them into dzn format. Optionally, the produced dzn is written on a file. Supported types of objects include: ``str``, ``int``, ``float``, ``set``, ``list`` or ``dict``. List and dict are serialized into dzn (multi-dimensional) arrays. The key-set of a dict is used as index-set of dzn arrays. The index-set of a list is implicitly set to ``1 .. len(list)``. Parameters ---------- objs : dict A dictionary containing the objects to serialize, the keys are the names of the variables. declare : bool Whether to include the declaration of the variable in the statements or just the assignment. Default is ``False``. assign : bool Whether to include assignment of the value in the statements or just the declaration. declare_enums : bool Whether to declare the enums found as types of the objects to serialize. Default is ``True``. wrap : bool Whether to wrap the serialized values. fout : str Path to the output file, if None no output file is written. Returns ------- list List of strings containing the dzn-encoded objects. """ log = logging.getLogger(__name__) vals = [] enums = set() for key, val in objs.items(): if _is_enum(val) and declare_enums: enum_type = type(val) enum_name = enum_type.__name__ if enum_name not in enums: enum_stmt = stmt2enum( enum_type, declare=declare, assign=assign, wrap=wrap ) vals.append(enum_stmt) enums.add(enum_name) stmt = stmt2dzn(key, val, declare=declare, assign=assign, wrap=wrap) vals.append(stmt) if fout: log.debug('Writing file: {}'.format(fout)) with open(fout, 'w') as f: for val in vals: f.write('{}\n\n'.format(val)) return vals
Serializes the objects in input and produces a list of strings encoding them into dzn format. Optionally, the produced dzn is written on a file. Supported types of objects include: ``str``, ``int``, ``float``, ``set``, ``list`` or ``dict``. List and dict are serialized into dzn (multi-dimensional) arrays. The key-set of a dict is used as index-set of dzn arrays. The index-set of a list is implicitly set to ``1 .. len(list)``. Parameters ---------- objs : dict A dictionary containing the objects to serialize, the keys are the names of the variables. declare : bool Whether to include the declaration of the variable in the statements or just the assignment. Default is ``False``. assign : bool Whether to include assignment of the value in the statements or just the declaration. declare_enums : bool Whether to declare the enums found as types of the objects to serialize. Default is ``True``. wrap : bool Whether to wrap the serialized values. fout : str Path to the output file, if None no output file is written. Returns ------- list List of strings containing the dzn-encoded objects.
def random_variant(variants, weights): """ A generator that, given a list of variants and a corresponding list of weights, returns one random weighted selection. """ total = 0 accumulator = [] for w in weights: total += w accumulator.append(total) r = randint(0, total - 1) yield variants[bisect(accumulator, r)]
A generator that, given a list of variants and a corresponding list of weights, returns one random weighted selection.
def _get_entity_id_if_container_metric(self, labels): """ Checks the labels indicate a container metric, then extract the container id from them. :param labels :return str or None """ if CadvisorPrometheusScraperMixin._is_container_metric(labels): pod = self._get_pod_by_metric_label(labels) if is_static_pending_pod(pod): # If the pod is static, ContainerStatus is unavailable. # Return the pod UID so that we can collect metrics from it later on. return self._get_pod_uid(labels) return self._get_container_id(labels)
Checks the labels indicate a container metric, then extract the container id from them. :param labels :return str or None
def parse_host(entity, default_port=DEFAULT_PORT): """Validates a host string Returns a 2-tuple of host followed by port where port is default_port if it wasn't specified in the string. :Parameters: - `entity`: A host or host:port string where host could be a hostname or IP address. - `default_port`: The port number to use when one wasn't specified in entity. """ host = entity port = default_port if entity[0] == '[': host, port = parse_ipv6_literal_host(entity, default_port) elif entity.endswith(".sock"): return entity, default_port elif entity.find(':') != -1: if entity.count(':') > 1: raise ValueError("Reserved characters such as ':' must be " "escaped according RFC 2396. An IPv6 " "address literal must be enclosed in '[' " "and ']' according to RFC 2732.") host, port = host.split(':', 1) if isinstance(port, string_type): if not port.isdigit() or int(port) > 65535 or int(port) <= 0: raise ValueError("Port must be an integer between 0 and 65535: %s" % (port,)) port = int(port) # Normalize hostname to lowercase, since DNS is case-insensitive: # http://tools.ietf.org/html/rfc4343 # This prevents useless rediscovery if "foo.com" is in the seed list but # "FOO.com" is in the ismaster response. return host.lower(), port
Validates a host string Returns a 2-tuple of host followed by port where port is default_port if it wasn't specified in the string. :Parameters: - `entity`: A host or host:port string where host could be a hostname or IP address. - `default_port`: The port number to use when one wasn't specified in entity.
def _verify_same_spaces(self): """Verifies that all the envs have the same observation and action space.""" # Pre-conditions: self._envs is initialized. if self._envs is None: raise ValueError("Environments not initialized.") if not isinstance(self._envs, list): tf.logging.warning("Not checking observation and action space " "compatibility across envs, since there is just one.") return # NOTE: We compare string representations of observation_space and # action_space because compositional classes like space.Tuple don't return # true on object comparison. if not all( str(env.observation_space) == str(self.observation_space) for env in self._envs): err_str = ("All environments should have the same observation space, but " "don't.") tf.logging.error(err_str) # Log all observation spaces. for i, env in enumerate(self._envs): tf.logging.error("Env[%d] has observation space [%s]", i, env.observation_space) raise ValueError(err_str) if not all( str(env.action_space) == str(self.action_space) for env in self._envs): err_str = "All environments should have the same action space, but don't." tf.logging.error(err_str) # Log all action spaces. for i, env in enumerate(self._envs): tf.logging.error("Env[%d] has action space [%s]", i, env.action_space) raise ValueError(err_str)
Verifies that all the envs have the same observation and action space.
def decode(self, binSequence): """decodes a binary sequence to return a string""" try: binSeq = iter(binSequence[0]) except TypeError, te: binSeq = binSequence ret = '' for b in binSeq : ch = '' for c in self.charToBin : if b & self.forma[self.charToBin[c]] > 0 : ch += c +'/' if ch == '' : raise KeyError('Key %d unkowom, bad format' % b) ret += ch[:-1] return ret
decodes a binary sequence to return a string
def satisfy_custom_matcher(self, args, kwargs): """Return a boolean indicating if the args satisfy the stub :return: Whether or not the stub accepts the provided arguments. :rtype: bool """ if not self._custom_matcher: return False try: return self._custom_matcher(*args, **kwargs) except Exception: return False
Return a boolean indicating if the args satisfy the stub :return: Whether or not the stub accepts the provided arguments. :rtype: bool
def get_arg_or_attr(self, name, default=None): """Returns flow argument, as provided with sitegate decorators or attribute set as a flow class attribute or default.""" if name in self.flow_args: return self.flow_args[name] try: return getattr(self, name) except AttributeError: return default
Returns flow argument, as provided with sitegate decorators or attribute set as a flow class attribute or default.
def update_views(self): """Update stats views.""" # Call the father's method super(Plugin, self).update_views() # Add specifics informations # Alert for i in self.stats: ifrealname = i['interface_name'].split(':')[0] # Convert rate in bps ( to be able to compare to interface speed) bps_rx = int(i['rx'] // i['time_since_update'] * 8) bps_tx = int(i['tx'] // i['time_since_update'] * 8) # Decorate the bitrate with the configuration file thresolds alert_rx = self.get_alert(bps_rx, header=ifrealname + '_rx') alert_tx = self.get_alert(bps_tx, header=ifrealname + '_tx') # If nothing is define in the configuration file... # ... then use the interface speed (not available on all systems) if alert_rx == 'DEFAULT' and 'speed' in i and i['speed'] != 0: alert_rx = self.get_alert(current=bps_rx, maximum=i['speed'], header='rx') if alert_tx == 'DEFAULT' and 'speed' in i and i['speed'] != 0: alert_tx = self.get_alert(current=bps_tx, maximum=i['speed'], header='tx') # then decorates self.views[i[self.get_key()]]['rx']['decoration'] = alert_rx self.views[i[self.get_key()]]['tx']['decoration'] = alert_tx
Update stats views.
def mod(self): """ Cached compiled binary of the Generic_Code class. To clear cache invoke :meth:`clear_mod_cache`. """ if self._mod is None: self._mod = self.compile_and_import_binary() return self._mod
Cached compiled binary of the Generic_Code class. To clear cache invoke :meth:`clear_mod_cache`.
def xline(self): """ Interact with segy in crossline mode Returns ------- xline : Line or None Raises ------ ValueError If the file is unstructured Notes ----- .. versionadded:: 1.1 """ if self.unstructured: raise ValueError(self._unstructured_errmsg) if self._xline is not None: return self._xline self._xline = Line(self, self.xlines, self._xline_length, self._xline_stride, self.offsets, 'crossline', ) return self._xline
Interact with segy in crossline mode Returns ------- xline : Line or None Raises ------ ValueError If the file is unstructured Notes ----- .. versionadded:: 1.1
def update_coordinates(self, points, mesh=None, render=True): """ Updates the points of the an object in the plotter. Parameters ---------- points : np.ndarray Points to replace existing points. mesh : vtk.PolyData or vtk.UnstructuredGrid, optional Object that has already been added to the Plotter. If None, uses last added mesh. render : bool, optional Forces an update to the render window. Default True. """ if mesh is None: mesh = self.mesh mesh.points = points if render: self._render()
Updates the points of the an object in the plotter. Parameters ---------- points : np.ndarray Points to replace existing points. mesh : vtk.PolyData or vtk.UnstructuredGrid, optional Object that has already been added to the Plotter. If None, uses last added mesh. render : bool, optional Forces an update to the render window. Default True.
def reset_coord(self): """ Reset the source location based on the init_skycoord values @return: """ (x, y, idx) = self.world2pix(self.init_skycoord.ra, self.init_skycoord.dec, usepv=True) self.update_pixel_location((x, y), idx)
Reset the source location based on the init_skycoord values @return:
def remove_vertex(self, vertex): """ Remove vertex from G """ try: self.vertices.pop(vertex) except KeyError: raise GraphInsertError("Vertex %s doesn't exist." % (vertex,)) if vertex in self.nodes: self.nodes.pop(vertex) for element in self.vertices: if vertex in self.vertices[element]: self.vertices[element].remove(vertex) edges = [] # List for edges that include vertex for element in self.edges: if vertex in element: edges.append(element) for element in edges: del self.edges[element]
Remove vertex from G
def load_configuration(working_dir): """ Load the system configuration and set global variables Return the configuration of the node on success. Return None on failure """ import nameset.virtualchain_hooks as virtualchain_hooks # acquire configuration, and store it globally opts = configure(working_dir) blockstack_opts = opts.get('blockstack', None) blockstack_api_opts = opts.get('blockstack-api', None) bitcoin_opts = opts['bitcoind'] # config file version check config_server_version = blockstack_opts.get('server_version', None) if (config_server_version is None or versions_need_upgrade(config_server_version, VERSION)): print >> sys.stderr, "Obsolete or unrecognizable config file ({}): '{}' != '{}'".format(virtualchain.get_config_filename(virtualchain_hooks, working_dir), config_server_version, VERSION) print >> sys.stderr, 'Please see the release notes for version {} for instructions to upgrade (in the release-notes/ folder).'.format(VERSION) return None # store options set_bitcoin_opts( bitcoin_opts ) set_blockstack_opts( blockstack_opts ) set_blockstack_api_opts( blockstack_api_opts ) return { 'bitcoind': bitcoin_opts, 'blockstack': blockstack_opts, 'blockstack-api': blockstack_api_opts }
Load the system configuration and set global variables Return the configuration of the node on success. Return None on failure
def joint(node): """Merge the bodies of primal and adjoint into a single function. Args: node: A module with the primal and adjoint function definitions as returned by `reverse_ad`. Returns: func: A `Module` node with a single function definition containing the combined primal and adjoint. """ node, _, _ = _fix(node) body = node.body[0].body[:-1] + node.body[1].body func = gast.Module(body=[gast.FunctionDef( name=node.body[0].name, args=node.body[1].args, body=body, decorator_list=[], returns=None)]) # Clean up anno.clearanno(func) return func
Merge the bodies of primal and adjoint into a single function. Args: node: A module with the primal and adjoint function definitions as returned by `reverse_ad`. Returns: func: A `Module` node with a single function definition containing the combined primal and adjoint.
def read(self, size:int=None): """ :param size: number of characters to read from the buffer :return: string that has been read from the buffer """ if size: result = self._buffer[0:size] self._buffer = self._buffer[size:] return result else: result = self._buffer self._buffer = '' return result
:param size: number of characters to read from the buffer :return: string that has been read from the buffer
def force_delete(func, path, exc_info): """Error handler for `shutil.rmtree()` equivalent to `rm -rf`. Usage: `shutil.rmtree(path, onerror=force_delete)` From stackoverflow.com/questions/1889597 """ os.chmod(path, stat.S_IWRITE) func(path)
Error handler for `shutil.rmtree()` equivalent to `rm -rf`. Usage: `shutil.rmtree(path, onerror=force_delete)` From stackoverflow.com/questions/1889597
def relevantindices(self) -> List[int]: """A |list| of all currently relevant indices, calculated as an intercection of the (constant) class attribute `RELEVANT_VALUES` and the (variable) property |IndexMask.refindices|.""" return [idx for idx in numpy.unique(self.refindices.values) if idx in self.RELEVANT_VALUES]
A |list| of all currently relevant indices, calculated as an intercection of the (constant) class attribute `RELEVANT_VALUES` and the (variable) property |IndexMask.refindices|.
def update_displayed_information(self): """ Update all the graphs that are being displayed """ for source in self.controller.sources: source_name = source.get_source_name() if (any(self.graphs_menu.active_sensors[source_name]) or any(self.summary_menu.active_sensors[source_name])): source.update() for graph in self.visible_graphs.values(): graph.update() # update graph summery for summary in self.visible_summaries.values(): summary.update() # Only update clock if not is stress mode if self.controller.stress_conroller.get_current_mode() != 'Monitor': self.clock_view.set_text(seconds_to_text( (timeit.default_timer() - self.controller.stress_start_time)))
Update all the graphs that are being displayed
def swap(self, position: int) -> None: """ Perform a SWAP operation on the stack. """ idx = -1 * position - 1 try: self.values[-1], self.values[idx] = self.values[idx], self.values[-1] except IndexError: raise InsufficientStack("Insufficient stack items for SWAP{0}".format(position))
Perform a SWAP operation on the stack.
def authenticate_credentials(self, payload): """ Returns an active user that matches the payload's user id and email. """ User = get_user_model() username = jwt_get_username_from_payload(payload) if not username: msg = _('Invalid payload.') raise exceptions.AuthenticationFailed(msg) try: user = User.objects.get_by_natural_key(username) except User.DoesNotExist: msg = _('Invalid signature.') raise exceptions.AuthenticationFailed(msg) if not user.is_active: msg = _('User account is disabled.') raise exceptions.AuthenticationFailed(msg) return user
Returns an active user that matches the payload's user id and email.
def deurlform_app(parser, cmd, args): # pragma: no cover """ decode a query string into its key value pairs. """ parser.add_argument('value', help='the query string to decode') args = parser.parse_args(args) return ' '.join('%s=%s' % (key, value) for key, values in deurlform(args.value).items() for value in values)
decode a query string into its key value pairs.
def build_query(self, **filters): """ Creates a single SQ filter from querystring parameters that correspond to the SearchIndex fields that have been "registered" in `view.fields`. Default behavior is to `OR` terms for the same parameters, and `AND` between parameters. Any querystring parameters that are not registered in `view.fields` will be ignored. :param dict[str, list[str]] filters: is an expanded QueryDict or a mapping of keys to a list of parameters. """ applicable_filters = [] applicable_exclusions = [] for param, value in filters.items(): excluding_term = False param_parts = param.split("__") base_param = param_parts[0] # only test against field without lookup negation_keyword = constants.DRF_HAYSTACK_NEGATION_KEYWORD if len(param_parts) > 1 and param_parts[1] == negation_keyword: excluding_term = True param = param.replace("__%s" % negation_keyword, "") # haystack wouldn't understand our negation if self.view.serializer_class: if hasattr(self.view.serializer_class.Meta, 'field_aliases'): old_base = base_param base_param = self.view.serializer_class.Meta.field_aliases.get(base_param, base_param) param = param.replace(old_base, base_param) # need to replace the alias fields = getattr(self.view.serializer_class.Meta, 'fields', []) exclude = getattr(self.view.serializer_class.Meta, 'exclude', []) search_fields = getattr(self.view.serializer_class.Meta, 'search_fields', []) # Skip if the parameter is not listed in the serializer's `fields` # or if it's in the `exclude` list. if ((fields or search_fields) and base_param not in chain(fields, search_fields)) or base_param in exclude or not value: continue field_queries = [] if len(param_parts) > 1 and param_parts[-1] in ('in', 'range'): # `in` and `range` filters expects a list of values field_queries.append(self.view.query_object((param, list(self.tokenize(value, self.view.lookup_sep))))) else: for token in self.tokenize(value, self.view.lookup_sep): field_queries.append(self.view.query_object((param, token))) field_queries = [fq for fq in field_queries if fq] if len(field_queries) > 0: term = six.moves.reduce(operator.or_, field_queries) if excluding_term: applicable_exclusions.append(term) else: applicable_filters.append(term) applicable_filters = six.moves.reduce( self.default_operator, filter(lambda x: x, applicable_filters)) if applicable_filters else [] applicable_exclusions = six.moves.reduce( self.default_operator, filter(lambda x: x, applicable_exclusions)) if applicable_exclusions else [] return applicable_filters, applicable_exclusions
Creates a single SQ filter from querystring parameters that correspond to the SearchIndex fields that have been "registered" in `view.fields`. Default behavior is to `OR` terms for the same parameters, and `AND` between parameters. Any querystring parameters that are not registered in `view.fields` will be ignored. :param dict[str, list[str]] filters: is an expanded QueryDict or a mapping of keys to a list of parameters.
def local_check (self): """ Warn about empty host names. Else call super.local_check(). """ if not self.host: self.set_result(_("Host is empty"), valid=False) return super(TelnetUrl, self).local_check()
Warn about empty host names. Else call super.local_check().
def disallow(self): """ Description of disallowed objects. Disallow must be a type name, a nested schema or a list of those. Type name must be one of ``string``, ``number``, ``integer``, ``boolean``, ``object``, ``array``, ``null`` or ``any``. """ value = self._schema.get("disallow", None) if value is None: return if not isinstance(value, (basestring, dict, list)): raise SchemaError( "disallow value {0!r} is not a simple type name, nested " "schema nor a list of those".format(value)) if isinstance(value, list): disallow_list = value else: disallow_list = [value] seen = set() for js_disallow in disallow_list: if isinstance(js_disallow, dict): # no nested validation here pass else: if js_disallow in seen: raise SchemaError( "disallow value {0!r} contains duplicate element" " {1!r}".format(value, js_disallow)) else: seen.add(js_disallow) if js_disallow not in ( "string", "number", "integer", "boolean", "object", "array", "null", "any"): raise SchemaError( "disallow value {0!r} is not a simple type" " name".format(js_disallow)) return disallow_list
Description of disallowed objects. Disallow must be a type name, a nested schema or a list of those. Type name must be one of ``string``, ``number``, ``integer``, ``boolean``, ``object``, ``array``, ``null`` or ``any``.
def explore_show_head(self, uri, check_headers=None): """Do HEAD on uri and show infomation. Will also check headers against any values specified in check_headers. """ print("HEAD %s" % (uri)) if (re.match(r'^\w+:', uri)): # Looks like a URI response = requests.head(uri) else: # Mock up response if we have a local file response = self.head_on_file(uri) print(" status: %s" % (response.status_code)) if (response.status_code == '200'): # print some of the headers for header in ['content-length', 'last-modified', 'lastmod', 'content-type', 'etag']: if header in response.headers: check_str = '' if (check_headers is not None and header in check_headers): if (response.headers[header] == check_headers[header]): check_str = ' MATCHES EXPECTED VALUE' else: check_str = ' EXPECTED %s' % ( check_headers[header]) print( " %s: %s%s" % (header, response.headers[header], check_str))
Do HEAD on uri and show infomation. Will also check headers against any values specified in check_headers.
def send_to_address(self, asset_id, to_addr, value, fee=None, change_addr=None, id=None, endpoint=None): """ Args: asset_id: (str) asset identifier (for NEO: 'c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b', for GAS: '602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7') to_addr: (str) destination address value: (int/decimal) transfer amount fee: (decimal, optional) Paying the handling fee helps elevate the priority of the network to process the transfer. It defaults to 0, and can be set to a minimum of 0.00000001. The low priority threshold is 0.001. change_addr: (str, optional) Change address, default is the first standard address in the wallet. id: (int, optional) id to use for response tracking endpoint: (RPCEndpoint, optional) endpoint to specify to use Returns: json object of the result or the error encountered in the RPC call """ params = [asset_id, to_addr, value] if fee: params.append(fee) if fee and change_addr: params.append(change_addr) elif not fee and change_addr: params.append(0) params.append(change_addr) return self._call_endpoint(SEND_TO_ADDRESS, params=params, id=id, endpoint=endpoint)
Args: asset_id: (str) asset identifier (for NEO: 'c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b', for GAS: '602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7') to_addr: (str) destination address value: (int/decimal) transfer amount fee: (decimal, optional) Paying the handling fee helps elevate the priority of the network to process the transfer. It defaults to 0, and can be set to a minimum of 0.00000001. The low priority threshold is 0.001. change_addr: (str, optional) Change address, default is the first standard address in the wallet. id: (int, optional) id to use for response tracking endpoint: (RPCEndpoint, optional) endpoint to specify to use Returns: json object of the result or the error encountered in the RPC call
def decode_iter(data, codec_options=DEFAULT_CODEC_OPTIONS): """Decode BSON data to multiple documents as a generator. Works similarly to the decode_all function, but yields one document at a time. `data` must be a string of concatenated, valid, BSON-encoded documents. :Parameters: - `data`: BSON data - `codec_options` (optional): An instance of :class:`~bson.codec_options.CodecOptions`. .. versionchanged:: 3.0 Replaced `as_class`, `tz_aware`, and `uuid_subtype` options with `codec_options`. .. versionadded:: 2.8 """ if not isinstance(codec_options, CodecOptions): raise _CODEC_OPTIONS_TYPE_ERROR position = 0 end = len(data) - 1 while position < end: obj_size = _UNPACK_INT(data[position:position + 4])[0] elements = data[position:position + obj_size] position += obj_size yield _bson_to_dict(elements, codec_options)
Decode BSON data to multiple documents as a generator. Works similarly to the decode_all function, but yields one document at a time. `data` must be a string of concatenated, valid, BSON-encoded documents. :Parameters: - `data`: BSON data - `codec_options` (optional): An instance of :class:`~bson.codec_options.CodecOptions`. .. versionchanged:: 3.0 Replaced `as_class`, `tz_aware`, and `uuid_subtype` options with `codec_options`. .. versionadded:: 2.8
def fromaligns(args): """ %prog fromaligns out.aligns Convert aligns file (old MCscan output) to anchors file. """ p = OptionParser(fromaligns.__doc__) p.set_outfile() opts, args = p.parse_args(args) if len(args) != 1: sys.exit(not p.print_help()) alignsfile, = args fp = must_open(alignsfile) fw = must_open(opts.outfile, "w") for row in fp: if row.startswith("## Alignment"): print("###", file=fw) continue if row[0] == '#' or not row.strip(): continue atoms = row.split(':')[-1].split() print("\t".join(atoms[:2]), file=fw) fw.close()
%prog fromaligns out.aligns Convert aligns file (old MCscan output) to anchors file.
def loopback_interface(self): """ Loopback interfaces for this node. This will return empty if the engine is not a layer 3 firewall type:: >>> engine = Engine('dingo') >>> for node in engine.nodes: ... for loopback in node.loopback_interface: ... loopback ... LoopbackInterface(address=172.20.1.1, nodeid=1, rank=1) LoopbackInterface(address=172.31.1.1, nodeid=1, rank=2) LoopbackInterface(address=2.2.2.2, nodeid=1, rank=3) :rtype: list(LoopbackInterface) """ for lb in self.data.get('loopback_node_dedicated_interface', []): yield LoopbackInterface(lb, self._engine)
Loopback interfaces for this node. This will return empty if the engine is not a layer 3 firewall type:: >>> engine = Engine('dingo') >>> for node in engine.nodes: ... for loopback in node.loopback_interface: ... loopback ... LoopbackInterface(address=172.20.1.1, nodeid=1, rank=1) LoopbackInterface(address=172.31.1.1, nodeid=1, rank=2) LoopbackInterface(address=2.2.2.2, nodeid=1, rank=3) :rtype: list(LoopbackInterface)
def filterVerticalLines(arr, min_line_length=4): """ Remove vertical lines in boolean array if linelength >=min_line_length """ gy = arr.shape[0] gx = arr.shape[1] mn = min_line_length-1 for i in range(gy): for j in range(gx): if arr[i,j]: for d in range(min_line_length): if not arr[i+d,j]: break if d == mn: d = 0 while True: if not arr[i+d,j]: break arr[i+d,j] = 0 d +=1
Remove vertical lines in boolean array if linelength >=min_line_length
def numpy_bins(self) -> List[np.ndarray]: """Numpy-like bins (if available).""" return [binning.numpy_bins for binning in self._binnings]
Numpy-like bins (if available).
def libvlc_audio_output_list_release(p_list): '''Frees the list of available audio output modules. @param p_list: list with audio outputs for release. ''' f = _Cfunctions.get('libvlc_audio_output_list_release', None) or \ _Cfunction('libvlc_audio_output_list_release', ((1,),), None, None, ctypes.POINTER(AudioOutput)) return f(p_list)
Frees the list of available audio output modules. @param p_list: list with audio outputs for release.
def rexponweib(alpha, k, loc=0, scale=1, size=None): """ Random exponentiated Weibull variates. """ q = np.random.uniform(size=size) r = flib.exponweib_ppf(q, alpha, k) return loc + r * scale
Random exponentiated Weibull variates.
def run_check(participants, config, reference_time): """For each participant, if they've been active for longer than the experiment duration + 2 minutes, we take action. """ recruiters_with_late_participants = defaultdict(list) for p in participants: timeline = ParticipationTime(p, reference_time, config) if timeline.is_overdue: print( "Error: participant {} with status {} has been playing for too " "long - their recruiter will be notified.".format(p.id, p.status) ) recruiters_with_late_participants[p.recruiter_id].append(p) for recruiter_id, participants in recruiters_with_late_participants.items(): recruiter = recruiters.by_name(recruiter_id) recruiter.notify_duration_exceeded(participants, reference_time)
For each participant, if they've been active for longer than the experiment duration + 2 minutes, we take action.
def mutate_rows( self, table_name, entries, app_profile_id=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Mutates multiple rows in a batch. Each individual row is mutated atomically as in MutateRow, but the entire batch is not executed atomically. Example: >>> from google.cloud import bigtable_v2 >>> >>> client = bigtable_v2.BigtableClient() >>> >>> table_name = client.table_path('[PROJECT]', '[INSTANCE]', '[TABLE]') >>> >>> # TODO: Initialize `entries`: >>> entries = [] >>> >>> for element in client.mutate_rows(table_name, entries): ... # process element ... pass Args: table_name (str): The unique name of the table to which the mutations should be applied. entries (list[Union[dict, ~google.cloud.bigtable_v2.types.Entry]]): The row keys and corresponding mutations to be applied in bulk. Each entry is applied as an atomic mutation, but the entries may be applied in arbitrary order (even between entries for the same row). At least one entry must be specified, and in total the entries can contain at most 100000 mutations. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.bigtable_v2.types.Entry` app_profile_id (str): This value specifies routing for replication. If not specified, the "default" application profile will be used. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: Iterable[~google.cloud.bigtable_v2.types.MutateRowsResponse]. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "mutate_rows" not in self._inner_api_calls: self._inner_api_calls[ "mutate_rows" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.mutate_rows, default_retry=self._method_configs["MutateRows"].retry, default_timeout=self._method_configs["MutateRows"].timeout, client_info=self._client_info, ) request = bigtable_pb2.MutateRowsRequest( table_name=table_name, entries=entries, app_profile_id=app_profile_id ) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("table_name", table_name)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) return self._inner_api_calls["mutate_rows"]( request, retry=retry, timeout=timeout, metadata=metadata )
Mutates multiple rows in a batch. Each individual row is mutated atomically as in MutateRow, but the entire batch is not executed atomically. Example: >>> from google.cloud import bigtable_v2 >>> >>> client = bigtable_v2.BigtableClient() >>> >>> table_name = client.table_path('[PROJECT]', '[INSTANCE]', '[TABLE]') >>> >>> # TODO: Initialize `entries`: >>> entries = [] >>> >>> for element in client.mutate_rows(table_name, entries): ... # process element ... pass Args: table_name (str): The unique name of the table to which the mutations should be applied. entries (list[Union[dict, ~google.cloud.bigtable_v2.types.Entry]]): The row keys and corresponding mutations to be applied in bulk. Each entry is applied as an atomic mutation, but the entries may be applied in arbitrary order (even between entries for the same row). At least one entry must be specified, and in total the entries can contain at most 100000 mutations. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.bigtable_v2.types.Entry` app_profile_id (str): This value specifies routing for replication. If not specified, the "default" application profile will be used. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: Iterable[~google.cloud.bigtable_v2.types.MutateRowsResponse]. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid.
def get_filtered_keys(self, suffix, *args, **kwargs): """Returns the index key for the given args "value" (`args`) Parameters ---------- kwargs: dict use_lua: bool Default to ``True``, if scripting is supported. If ``True``, the process of reading from the sorted-set, extracting the primary keys, excluding some values if needed, and putting the primary keys in a set or zset, is done in lua at the redis level. Else, data is fetched, manipulated here, then returned to redis. For the other parameters, see ``BaseIndex.get_filtered_keys`` """ accepted_key_types = kwargs.get('accepted_key_types', None) if accepted_key_types\ and 'set' not in accepted_key_types and 'zset' not in accepted_key_types: raise ImplementationError( '%s can only return keys of type "set" or "zset"' % self.__class__.__name__ ) key_type = 'set' if not accepted_key_types or 'set' in accepted_key_types else 'zset' tmp_key = unique_key(self.connection) args = list(args) # special "in" case: we get n keys and make an unionstore with them then return this key if suffix == 'in': values = set(args.pop()) if not values: return [] # no keys in_keys = [ self.get_filtered_keys('eq', *(args+[value]), **kwargs)[0][0] for value in values ] if key_type == 'set': self.connection.sunionstore(tmp_key, *in_keys) else: self.connection.zunionstore(tmp_key, *in_keys) # we can delete the temporary keys for in_key in in_keys: self.connection.delete(in_key) return [(tmp_key, key_type, True)] use_lua = self.model.database.support_scripting() and kwargs.get('use_lua', True) key = self.get_storage_key(*args) value = self.normalize_value(args[-1], transform=False) real_suffix = self.remove_prefix(suffix) if use_lua: start, end, exclude = self.get_boundaries(real_suffix, value) self.call_script(key, tmp_key, key_type, start, end, exclude) else: pks = self.get_pks_for_filter(key, real_suffix, value) if pks: if key_type == 'set': self.connection.sadd(tmp_key, *pks) else: self.connection.zadd(tmp_key, **{pk: idx for idx, pk in enumerate(pks)}) return [(tmp_key, key_type, True)]
Returns the index key for the given args "value" (`args`) Parameters ---------- kwargs: dict use_lua: bool Default to ``True``, if scripting is supported. If ``True``, the process of reading from the sorted-set, extracting the primary keys, excluding some values if needed, and putting the primary keys in a set or zset, is done in lua at the redis level. Else, data is fetched, manipulated here, then returned to redis. For the other parameters, see ``BaseIndex.get_filtered_keys``
def selected_canvas_agglayer(self): """Obtain the canvas aggregation layer selected by user. :returns: The currently selected map layer in the list. :rtype: QgsMapLayer """ if self.lstCanvasAggLayers.selectedItems(): item = self.lstCanvasAggLayers.currentItem() else: return None try: layer_id = item.data(QtCore.Qt.UserRole) except (AttributeError, NameError): layer_id = None layer = QgsProject.instance().mapLayer(layer_id) return layer
Obtain the canvas aggregation layer selected by user. :returns: The currently selected map layer in the list. :rtype: QgsMapLayer
def json_to_config(dic, full = False): """ Converts a JSON-decoded config dictionary to a python dictionary. When materializing the full view, the values in the dictionary will be instances of ApiConfig, instead of strings. @param dic: JSON-decoded config dictionary. @param full: Whether to materialize the full view of the config data. @return: Python dictionary with config data. """ config = { } for entry in dic['items']: k = entry['name'] if full: config[k] = ApiConfig.from_json_dict(entry, None) else: config[k] = entry.get('value') return config
Converts a JSON-decoded config dictionary to a python dictionary. When materializing the full view, the values in the dictionary will be instances of ApiConfig, instead of strings. @param dic: JSON-decoded config dictionary. @param full: Whether to materialize the full view of the config data. @return: Python dictionary with config data.
def _thread(self): """ Thread entry point: does the job once, stored results, and dies. """ # Get args, kwargs = self._jobs.get() # Stop thread when (None, None) comes in if args is None and kwargs is None: return None # Wrappers should exit as well # Work try: self._results.append(self._worker(*args, **kwargs)) return True except Exception as e: self._errors.append(e) return False finally: self._jobs.task_done() with self._jobfinished: self._jobfinished.notify()
Thread entry point: does the job once, stored results, and dies.
def _indent(text, prefix, predicate=None): """Adds 'prefix' to the beginning of selected lines in 'text'. If 'predicate' is provided, 'prefix' will only be added to the lines where 'predicate(line)' is True. If 'predicate' is not provided, it will default to adding 'prefix' to all non-empty lines that do not consist solely of whitespace characters. """ if predicate is None: predicate = lambda line: line.strip() def prefixed_lines(): for line in text.splitlines(True): yield prefix + line if predicate(line) else line return "".join(prefixed_lines())
Adds 'prefix' to the beginning of selected lines in 'text'. If 'predicate' is provided, 'prefix' will only be added to the lines where 'predicate(line)' is True. If 'predicate' is not provided, it will default to adding 'prefix' to all non-empty lines that do not consist solely of whitespace characters.
def to_compressed_string(val, max_length=0): """Converts val to a compressed string. A compressed string is one with no leading or trailing spaces. If val is None, or is blank (all spaces) None is returned. If max_length > 0 and the stripped val is greater than max_length, val[:max_length] is returned. """ if val is None or len(val) == 0: return None rval = " ".join(val.split()) if len(rval) == 0: return None if max_length == 0: return rval else: return rval[:max_length]
Converts val to a compressed string. A compressed string is one with no leading or trailing spaces. If val is None, or is blank (all spaces) None is returned. If max_length > 0 and the stripped val is greater than max_length, val[:max_length] is returned.
def get_all_attributes(klass_or_instance): """Get all attribute members (attribute, property style method). """ pairs = list() for attr, value in inspect.getmembers( klass_or_instance, lambda x: not inspect.isroutine(x)): if not (attr.startswith("__") or attr.endswith("__")): pairs.append((attr, value)) return pairs
Get all attribute members (attribute, property style method).
def unmap_namespace(self, plain_target_ns): """Given a plain target namespace, return the corresponding source namespace. """ # Return the same namespace if there are no included namespaces. if not self._regex_map and not self._plain: return plain_target_ns src_name_set = self._reverse_plain.get(plain_target_ns) if src_name_set: # Return the first (and only) item in the set for src_name in src_name_set: return src_name # The target namespace could also exist in the wildcard namespaces for _, namespace in self._regex_map: original_name = match_replace_regex( namespace_to_regex(namespace.dest_name), plain_target_ns, namespace.source_name, ) if original_name: return original_name return None
Given a plain target namespace, return the corresponding source namespace.
def _get_mdm(cls, df, windows): """ -DM, negative directional moving accumulation If window is not 1, return the SMA of -DM. :param df: data :param windows: range :return: """ window = cls.get_only_one_positive_int(windows) column_name = 'mdm_{}'.format(window) um, dm = df['um'], df['dm'] df['mdm'] = np.where(dm > um, dm, 0) if window > 1: mdm = df['mdm_{}_ema'.format(window)] else: mdm = df['mdm'] df[column_name] = mdm
-DM, negative directional moving accumulation If window is not 1, return the SMA of -DM. :param df: data :param windows: range :return:
def interpolate(G, f_subsampled, keep_inds, order=100, reg_eps=0.005, **kwargs): r"""Interpolate a graph signal. Parameters ---------- G : Graph f_subsampled : ndarray A graph signal on the graph G. keep_inds : ndarray List of indices on which the signal is sampled. order : int Degree of the Chebyshev approximation (default = 100). reg_eps : float The regularized graph Laplacian is $\bar{L}=L+\epsilon I$. A smaller epsilon may lead to better regularization, but will also require a higher order Chebyshev approximation. Returns ------- f_interpolated : ndarray Interpolated graph signal on the full vertex set of G. References ---------- See :cite:`pesenson2009variational` """ L_reg = G.L + reg_eps * sparse.eye(G.N) K_reg = getattr(G.mr, 'K_reg', kron_reduction(L_reg, keep_inds)) green_kernel = getattr(G.mr, 'green_kernel', filters.Filter(G, lambda x: 1. / (reg_eps + x))) alpha = K_reg.dot(f_subsampled) try: Nv = np.shape(f_subsampled)[1] f_interpolated = np.zeros((G.N, Nv)) except IndexError: f_interpolated = np.zeros((G.N)) f_interpolated[keep_inds] = alpha return _analysis(green_kernel, f_interpolated, order=order, **kwargs)
r"""Interpolate a graph signal. Parameters ---------- G : Graph f_subsampled : ndarray A graph signal on the graph G. keep_inds : ndarray List of indices on which the signal is sampled. order : int Degree of the Chebyshev approximation (default = 100). reg_eps : float The regularized graph Laplacian is $\bar{L}=L+\epsilon I$. A smaller epsilon may lead to better regularization, but will also require a higher order Chebyshev approximation. Returns ------- f_interpolated : ndarray Interpolated graph signal on the full vertex set of G. References ---------- See :cite:`pesenson2009variational`
def cache_cleaned(name=None, user=None, force=False): ''' Ensure that the given package is not cached. If no package is specified, this ensures the entire cache is cleared. name The name of the package to remove from the cache, or None for all packages user The user to run NPM with force Force cleaning of cache. Required for npm@5 and greater .. versionadded:: 2016.11.6 ''' ret = {'name': name, 'result': None, 'comment': '', 'changes': {}} specific_pkg = None try: cached_pkgs = __salt__['npm.cache_list'](path=name, runas=user) except (CommandExecutionError, CommandNotFoundError) as err: ret['result'] = False ret['comment'] = 'Error looking up cached {0}: {1}'.format( name or 'packages', err) return ret if name: all_cached_pkgs = __salt__['npm.cache_list'](path=None, runas=user) # The first package is always the cache path cache_root_path = all_cached_pkgs[0] specific_pkg = '{0}/{1}/'.format(cache_root_path, name) if specific_pkg not in cached_pkgs: ret['result'] = True ret['comment'] = 'Package {0} is not in the cache'.format(name) return ret if __opts__['test']: ret['result'] = None ret['comment'] = 'Cached {0} set to be removed'.format(name or 'packages') return ret if __salt__['npm.cache_clean'](path=name, runas=user): ret['result'] = True ret['changes'][name or 'cache'] = 'Removed' ret['comment'] = 'Cached {0} successfully removed'.format( name or 'packages' ) else: ret['result'] = False ret['comment'] = 'Error cleaning cached {0}'.format(name or 'packages') return ret
Ensure that the given package is not cached. If no package is specified, this ensures the entire cache is cleared. name The name of the package to remove from the cache, or None for all packages user The user to run NPM with force Force cleaning of cache. Required for npm@5 and greater .. versionadded:: 2016.11.6
def _get_c_string(data, position): """Decode a BSON 'C' string to python unicode string.""" end = data.index(b"\x00", position) return _utf_8_decode(data[position:end], None, True)[0], end + 1
Decode a BSON 'C' string to python unicode string.
def rename(self, mapping): """ Rename fields :param mapping: a dict in the format {'oldkey1': 'newkey1', ...} """ for old_key, new_key in mapping: self.dict[new_key] = self.dict[old_key] del self.dict[old_key]
Rename fields :param mapping: a dict in the format {'oldkey1': 'newkey1', ...}
def _rectify(settings): """ Rectify (and validate) the given settings using the functions in :data:`_settings_rectifiers`. """ for key, rectifier in _settings_rectifiers.items(): try: new_value = rectifier['func'](settings[key]) if new_value is False: raise AuthzConfigurationError( 'Error validating {}->{}: {}'.format( _settings_key, key, rectifier['errmsg'])) settings[key] = new_value except: raise AuthzConfigurationError( 'Error validating {}->{}: {}'.format( _settings_key, key, rectifier['errmsg']))
Rectify (and validate) the given settings using the functions in :data:`_settings_rectifiers`.