code
stringlengths
75
104k
docstring
stringlengths
1
46.9k
def no_company_with_insufficient_companies_house_data(value): """ Confirms that the company number is not for for a company that Companies House does not hold information on. Args: value (string): The company number to check. Raises: django.forms.ValidationError """ for prefix, name in company_types_with_insufficient_companies_house_data: if value.upper().startswith(prefix): raise ValidationError( MESSAGE_INSUFFICIENT_DATA, params={'name': name} )
Confirms that the company number is not for for a company that Companies House does not hold information on. Args: value (string): The company number to check. Raises: django.forms.ValidationError
def isSuperTagSetOf(self, tagSet): """Test type relationship against given *TagSet* The callee is considered to be a supertype of given *TagSet* tag-wise if all tags in *TagSet* are present in the callee and they are in the same order. Parameters ---------- tagSet: :class:`~pyasn1.type.tag.TagSet` *TagSet* object to evaluate against the callee Returns ------- : :py:class:`bool` `True` if callee is a supertype of *tagSet* """ if len(tagSet) < self.__lenOfSuperTags: return False return self.__superTags == tagSet[:self.__lenOfSuperTags]
Test type relationship against given *TagSet* The callee is considered to be a supertype of given *TagSet* tag-wise if all tags in *TagSet* are present in the callee and they are in the same order. Parameters ---------- tagSet: :class:`~pyasn1.type.tag.TagSet` *TagSet* object to evaluate against the callee Returns ------- : :py:class:`bool` `True` if callee is a supertype of *tagSet*
def _maybe_repeat(self, x): """Utility function for processing arguments that are singletons or lists. Args: x: either a list of self.n elements, or not a list. Returns: a list of self.n elements. """ if isinstance(x, list): assert len(x) == self.n return x else: return [x] * self.n
Utility function for processing arguments that are singletons or lists. Args: x: either a list of self.n elements, or not a list. Returns: a list of self.n elements.
def remove_object(self, name): """ Remove the object exposed under that name. If no object is registered under the supplied name, a RuntimeError is raised. :param name: Name of object to be removed. """ if name not in self._object_map: raise RuntimeError('No object with name {} is registered.'.format(name)) for fn_name in list(self._function_map.keys()): if fn_name.startswith(name + '.') or fn_name.startswith(name + ':'): self._remove_function(fn_name) del self._object_map[name]
Remove the object exposed under that name. If no object is registered under the supplied name, a RuntimeError is raised. :param name: Name of object to be removed.
def _base64_to_file(b64str, outfpath, writetostrio=False): '''This converts the base64 encoded string to a file. Parameters ---------- b64str : str A base64 encoded strin that is the output of `base64.b64encode`. outfpath : str The path to where the file will be written. This should include an appropriate extension for the file (e.g. a base64 encoded string that represents a PNG should have its `outfpath` end in a '.png') so the OS can open these files correctly. writetostrio : bool If this is True, will return a StringIO object with the binary stream decoded from the base64-encoded input string `b64str`. This can be useful to embed these into other files without having to write them to disk. Returns ------- str or StringIO object If `writetostrio` is False, will return the output file's path as a str. If it is True, will return a StringIO object directly. If writing the file fails in either case, will return None. ''' try: filebytes = base64.b64decode(b64str) # if we're writing back to a stringio object if writetostrio: outobj = StrIO(filebytes) return outobj # otherwise, we're writing to an actual file else: with open(outfpath,'wb') as outfd: outfd.write(filebytes) if os.path.exists(outfpath): return outfpath else: LOGERROR('could not write output file: %s' % outfpath) return None except Exception as e: LOGEXCEPTION('failed while trying to convert ' 'b64 string to file %s' % outfpath) return None
This converts the base64 encoded string to a file. Parameters ---------- b64str : str A base64 encoded strin that is the output of `base64.b64encode`. outfpath : str The path to where the file will be written. This should include an appropriate extension for the file (e.g. a base64 encoded string that represents a PNG should have its `outfpath` end in a '.png') so the OS can open these files correctly. writetostrio : bool If this is True, will return a StringIO object with the binary stream decoded from the base64-encoded input string `b64str`. This can be useful to embed these into other files without having to write them to disk. Returns ------- str or StringIO object If `writetostrio` is False, will return the output file's path as a str. If it is True, will return a StringIO object directly. If writing the file fails in either case, will return None.
def get_userids_for_address(self, address: Address) -> Set[str]: """ Return all known user ids for the given ``address``. """ if not self.is_address_known(address): return set() return self._address_to_userids[address]
Return all known user ids for the given ``address``.
def rfc2426(self): """RFC2426-encode the field content. :return: the field in the RFC 2426 format. :returntype: `str`""" if self.type: p={"type":self.type} else: p={} return rfc2425encode(self.name,self.cred,p)
RFC2426-encode the field content. :return: the field in the RFC 2426 format. :returntype: `str`
def get_model(): """Get the NN model that's being analyzed from the request context. Put the model in the request context if it is not yet there. Returns: instance of :class:`.models.model.Model` or derived class """ if not hasattr(g, 'model'): g.model = load_model(current_app.config['MODEL_CLS_PATH'], current_app.config['MODEL_CLS_NAME'], current_app.config['MODEL_LOAD_ARGS']) return g.model
Get the NN model that's being analyzed from the request context. Put the model in the request context if it is not yet there. Returns: instance of :class:`.models.model.Model` or derived class
def append_id(expr, id_col='append_id'): """ Append an ID column to current column to form a new DataFrame. :param str id_col: name of appended ID field. :return: DataFrame with ID field :rtype: DataFrame """ if hasattr(expr, '_xflow_append_id'): return expr._xflow_append_id(id_col) else: return _append_id(expr, id_col)
Append an ID column to current column to form a new DataFrame. :param str id_col: name of appended ID field. :return: DataFrame with ID field :rtype: DataFrame
def refresh_token(): # noqa: E501 """Refreshes login token using refresh token Refreshes login token using refresh token # noqa: E501 :rtype: UserAuth """ current_user = get_jwt_identity() if not current_user: return ErrorResponse(status=401, message="Not logged in") access_token = create_access_token(identity=current_user) return AuthResponse(status=201, message='Refreshed Access Token', access_token=access_token, auth=UserAuth())
Refreshes login token using refresh token Refreshes login token using refresh token # noqa: E501 :rtype: UserAuth
def vMh2_to_m2Lambda(v, Mh2, C, scale_high): """Function to numerically determine the parameters of the Higgs potential given the physical Higgs VEV and mass.""" if C['phi'] == 0 and C['phiBox'] == 0 and C['phiD'] == 0: return _vMh2_to_m2Lambda_SM(v, Mh2) else: def f0(x): # we want the root of this function m2, Lambda = x d = m2Lambda_to_vMh2(m2=m2.real, Lambda=Lambda.real, C=C, scale_high=scale_high) return np.array([d['v'] - v, d['Mh2'] - Mh2]) dSM = _vMh2_to_m2Lambda_SM(v, Mh2) x0 = np.array([dSM['m2'], dSM['Lambda']]) try: xres = scipy.optimize.newton_krylov(f0, x0) except scipy.optimize.nonlin.NoConvergence: raise ValueError("No solution for m^2 and Lambda found") return {'m2': xres[0], 'Lambda': xres[1]}
Function to numerically determine the parameters of the Higgs potential given the physical Higgs VEV and mass.
def image_list(name=None, profile=None, **kwargs): ''' Return a list of available images (nova images-list + nova image-show) If a name is provided, only that image will be displayed. CLI Examples: .. code-block:: bash salt '*' nova.image_list salt '*' nova.image_list myimage ''' conn = _auth(profile, **kwargs) return conn.image_list(name)
Return a list of available images (nova images-list + nova image-show) If a name is provided, only that image will be displayed. CLI Examples: .. code-block:: bash salt '*' nova.image_list salt '*' nova.image_list myimage
def check_datetime(method, dictionary, fields, label=None): """Checks if the specified fields are formatted correctly if they have a value. Throws an exception on incorrectly formatted fields. :param dict dictionary: Dictionary to check. :param typle fields: Fields to check. :param string label: Dictionary name. """ improperly_formatted = [] values = [] for field in fields: if field in dictionary and dictionary[field] is not None: if type(dictionary[field]) not in (datetime.datetime, datetime.date) and not ISO_8601_REGEX.match(dictionary[field]): improperly_formatted.append(field) values.append(dictionary[field]) if improperly_formatted: error_label = ' for "%s"' % label if label else '' raise PyCronofyValidationError( 'Method: %s. Improperly formatted datetime/date field(s)%s: %s\n%s' % ( method, error_label, improperly_formatted, values), method, improperly_formatted, values )
Checks if the specified fields are formatted correctly if they have a value. Throws an exception on incorrectly formatted fields. :param dict dictionary: Dictionary to check. :param typle fields: Fields to check. :param string label: Dictionary name.
def do_fuzzy(self, word): """Compute fuzzy extensions of word. FUZZY lilas""" word = list(preprocess_query(word))[0] print(white(make_fuzzy(word)))
Compute fuzzy extensions of word. FUZZY lilas
def set_foreground(self, fg, isRGBA=None): """ Set the foreground color. fg can be a matlab format string, a html hex color string, an rgb unit tuple, or a float between 0 and 1. In the latter case, grayscale is used. """ # Implementation note: wxPython has a separate concept of pen and # brush - the brush fills any outline trace left by the pen. # Here we set both to the same colour - if a figure is not to be # filled, the renderer will set the brush to be transparent # Same goes for text foreground... DEBUG_MSG("set_foreground()", 1, self) self.select() GraphicsContextBase.set_foreground(self, fg, isRGBA) self._pen.SetColour(self.get_wxcolour(self.get_rgb())) self.gfx_ctx.SetPen(self._pen) self.unselect()
Set the foreground color. fg can be a matlab format string, a html hex color string, an rgb unit tuple, or a float between 0 and 1. In the latter case, grayscale is used.
def expire(self, time=None): """Remove expired items from the cache.""" if time is None: time = self.__timer() root = self.__root curr = root.next links = self.__links cache_delitem = Cache.__delitem__ while curr is not root and curr.expire < time: cache_delitem(self, curr.key) del links[curr.key] next = curr.next curr.unlink() curr = next
Remove expired items from the cache.
def weight_field(self, f): """ Select one field as the weight field. Note that this field will be exclude from feature fields. :param f: Selected weight field :type f: str :rtype: DataFrame """ if f is None: raise ValueError("Field name cannot be None.") self._assert_ml_fields_valid(f) return _change_singleton_roles(self, {f: FieldRole.WEIGHT}, clear_feature=True)
Select one field as the weight field. Note that this field will be exclude from feature fields. :param f: Selected weight field :type f: str :rtype: DataFrame
def validate_full_path(cls, full_path, **kwargs): """Helper method to return a full path from a full or partial path. If no domain, assumes user's account domain If the vault is "~", assumes personal vault. Valid vault paths include: domain:vault domain:vault:/path domain:vault/path vault:/path vault ~/ Invalid vault paths include: /vault/ /path / :/ Does not allow overrides for any vault path components. """ _client = kwargs.pop('client', None) or cls._client or client full_path = full_path.strip() if not full_path: raise Exception( 'Vault path "{0}" is invalid. Path must be in the format: ' '"domain:vault:/path" or "vault:/path".'.format(full_path) ) match = cls.VAULT_PATH_RE.match(full_path) if not match: raise Exception( 'Vault path "{0}" is invalid. Path must be in the format: ' '"domain:vault:/path" or "vault:/path".'.format(full_path) ) path_parts = match.groupdict() # Handle the special case where "~" means personal vault if path_parts.get('vault') == '~': path_parts = dict(domain=None, vault=None) # If any values are None, set defaults from the user. if None in path_parts.values(): user = _client.get('/v1/user', {}) defaults = { 'domain': user['account']['domain'], 'vault': 'user-{0}'.format(user['id']) } path_parts = dict((k, v or defaults.get(k)) for k, v in path_parts.items()) # Rebuild the full path full_path = '{domain}:{vault}'.format(**path_parts) path_parts['vault_full_path'] = full_path return full_path, path_parts
Helper method to return a full path from a full or partial path. If no domain, assumes user's account domain If the vault is "~", assumes personal vault. Valid vault paths include: domain:vault domain:vault:/path domain:vault/path vault:/path vault ~/ Invalid vault paths include: /vault/ /path / :/ Does not allow overrides for any vault path components.
def ensure_dir(path): """Create all parent directories of path if they don't exist. Args: path. Path-like object. Create parent dirs to this path. Return: None. """ os.makedirs(os.path.abspath(os.path.dirname(path)), exist_ok=True)
Create all parent directories of path if they don't exist. Args: path. Path-like object. Create parent dirs to this path. Return: None.
def termination_check(self): # type: (Downloader) -> bool """Check if terminated :param Downloader self: this :rtype: bool :return: if terminated """ with self._transfer_lock: with self._disk_operation_lock: return (self._download_terminate or len(self._exceptions) > 0 or (self._all_remote_files_processed and len(self._transfer_set) == 0 and len(self._disk_set) == 0))
Check if terminated :param Downloader self: this :rtype: bool :return: if terminated
def Archimedes(L, rhof, rhop, mu, g=g): r'''Calculates Archimedes number, `Ar`, for a fluid and particle with the given densities, characteristic length, viscosity, and gravity (usually diameter of particle). .. math:: Ar = \frac{L^3 \rho_f(\rho_p-\rho_f)g}{\mu^2} Parameters ---------- L : float Characteristic length, typically particle diameter [m] rhof : float Density of fluid, [kg/m^3] rhop : float Density of particle, [kg/m^3] mu : float Viscosity of fluid, [N/m] g : float, optional Acceleration due to gravity, [m/s^2] Returns ------- Ar : float Archimedes number [] Notes ----- Used in fluid-particle interaction calculations. .. math:: Ar = \frac{\text{Gravitational force}}{\text{Viscous force}} Examples -------- >>> Archimedes(0.002, 2., 3000, 1E-3) 470.4053872 References ---------- .. [1] Green, Don, and Robert Perry. Perry's Chemical Engineers' Handbook, Eighth Edition. McGraw-Hill Professional, 2007. .. [2] Cengel, Yunus, and John Cimbala. Fluid Mechanics: Fundamentals and Applications. Boston: McGraw Hill Higher Education, 2006. ''' return L**3*rhof*(rhop-rhof)*g/mu**2
r'''Calculates Archimedes number, `Ar`, for a fluid and particle with the given densities, characteristic length, viscosity, and gravity (usually diameter of particle). .. math:: Ar = \frac{L^3 \rho_f(\rho_p-\rho_f)g}{\mu^2} Parameters ---------- L : float Characteristic length, typically particle diameter [m] rhof : float Density of fluid, [kg/m^3] rhop : float Density of particle, [kg/m^3] mu : float Viscosity of fluid, [N/m] g : float, optional Acceleration due to gravity, [m/s^2] Returns ------- Ar : float Archimedes number [] Notes ----- Used in fluid-particle interaction calculations. .. math:: Ar = \frac{\text{Gravitational force}}{\text{Viscous force}} Examples -------- >>> Archimedes(0.002, 2., 3000, 1E-3) 470.4053872 References ---------- .. [1] Green, Don, and Robert Perry. Perry's Chemical Engineers' Handbook, Eighth Edition. McGraw-Hill Professional, 2007. .. [2] Cengel, Yunus, and John Cimbala. Fluid Mechanics: Fundamentals and Applications. Boston: McGraw Hill Higher Education, 2006.
def chassis(name, chassis_name=None, password=None, datacenter=None, location=None, mode=None, idrac_launch=None, slot_names=None, blade_power_states=None): ''' Manage a Dell Chassis. chassis_name The name of the chassis. datacenter The datacenter in which the chassis is located location The location of the chassis. password Password for the chassis. Note: If this password is set for the chassis, the current implementation of this state will set this password both on the chassis and the iDrac passwords on any configured blades. If the password for the blades should be distinct, they should be set separately with the blade_idrac function. mode The management mode of the chassis. Viable options are: - 0: None - 1: Monitor - 2: Manage and Monitor idrac_launch The iDRAC launch method of the chassis. Viable options are: - 0: Disabled (launch iDRAC using IP address) - 1: Enabled (launch iDRAC using DNS name) slot_names The names of the slots, provided as a list identified by their slot numbers. blade_power_states The power states of a blade server, provided as a list and identified by their server numbers. Viable options are: - on: Ensure the blade server is powered on. - off: Ensure the blade server is powered off. - powercycle: Power cycle the blade server. Example: .. code-block:: yaml my-dell-chassis: dellchassis.chassis: - chassis_name: my-dell-chassis - location: my-location - datacenter: london - mode: 2 - idrac_launch: 1 - slot_names: - 1: my-slot-name - 2: my-other-slot-name - blade_power_states: - server-1: on - server-2: off - server-3: powercycle ''' ret = {'name': chassis_name, 'chassis_name': chassis_name, 'result': True, 'changes': {}, 'comment': ''} chassis_cmd = 'chassis.cmd' cfg_tuning = 'cfgRacTuning' mode_cmd = 'cfgRacTuneChassisMgmtAtServer' launch_cmd = 'cfgRacTuneIdracDNSLaunchEnable' inventory = __salt__[chassis_cmd]('inventory') if idrac_launch: idrac_launch = six.text_type(idrac_launch) current_name = __salt__[chassis_cmd]('get_chassis_name') if chassis_name != current_name: ret['changes'].update({'Name': {'Old': current_name, 'New': chassis_name}}) current_dc = __salt__[chassis_cmd]('get_chassis_datacenter') if datacenter and datacenter != current_dc: ret['changes'].update({'Datacenter': {'Old': current_dc, 'New': datacenter}}) if password: ret['changes'].update({'Password': {'Old': '******', 'New': '******'}}) if location: current_location = __salt__[chassis_cmd]('get_chassis_location') if location != current_location: ret['changes'].update({'Location': {'Old': current_location, 'New': location}}) if mode: current_mode = __salt__[chassis_cmd]('get_general', cfg_tuning, mode_cmd) if mode != current_mode: ret['changes'].update({'Management Mode': {'Old': current_mode, 'New': mode}}) if idrac_launch: current_launch_method = __salt__[chassis_cmd]('get_general', cfg_tuning, launch_cmd) if idrac_launch != current_launch_method: ret['changes'].update({'iDrac Launch Method': {'Old': current_launch_method, 'New': idrac_launch}}) if slot_names: current_slot_names = __salt__[chassis_cmd]('list_slotnames') for s in slot_names: key = s.keys()[0] new_name = s[key] if key.startswith('slot-'): key = key[5:] current_slot_name = current_slot_names.get(key).get('slotname') if current_slot_name != new_name: old = {key: current_slot_name} new = {key: new_name} if ret['changes'].get('Slot Names') is None: ret['changes'].update({'Slot Names': {'Old': {}, 'New': {}}}) ret['changes']['Slot Names']['Old'].update(old) ret['changes']['Slot Names']['New'].update(new) current_power_states = {} target_power_states = {} if blade_power_states: for b in blade_power_states: key = b.keys()[0] status = __salt__[chassis_cmd]('server_powerstatus', module=key) current_power_states[key] = status.get('status', -1) if b[key] == 'powerdown': if current_power_states[key] != -1 and current_power_states[key]: target_power_states[key] = 'powerdown' if b[key] == 'powerup': if current_power_states[key] != -1 and not current_power_states[key]: target_power_states[key] = 'powerup' if b[key] == 'powercycle': if current_power_states[key] != -1 and not current_power_states[key]: target_power_states[key] = 'powerup' if current_power_states[key] != -1 and current_power_states[key]: target_power_states[key] = 'powercycle' for k, v in six.iteritems(target_power_states): old = {k: current_power_states[k]} new = {k: v} if ret['changes'].get('Blade Power States') is None: ret['changes'].update({'Blade Power States': {'Old': {}, 'New': {}}}) ret['changes']['Blade Power States']['Old'].update(old) ret['changes']['Blade Power States']['New'].update(new) if ret['changes'] == {}: ret['comment'] = 'Dell chassis is already in the desired state.' return ret if __opts__['test']: ret['result'] = None ret['comment'] = 'Dell chassis configuration will change.' return ret # Finally, set the necessary configurations on the chassis. name = __salt__[chassis_cmd]('set_chassis_name', chassis_name) if location: location = __salt__[chassis_cmd]('set_chassis_location', location) pw_result = True if password: pw_single = True if __salt__[chassis_cmd]('change_password', username='root', uid=1, password=password): for blade in inventory['server']: pw_single = __salt__[chassis_cmd]('deploy_password', username='root', password=password, module=blade) if not pw_single: pw_result = False else: pw_result = False if datacenter: datacenter_result = __salt__[chassis_cmd]('set_chassis_datacenter', datacenter) if mode: mode = __salt__[chassis_cmd]('set_general', cfg_tuning, mode_cmd, mode) if idrac_launch: idrac_launch = __salt__[chassis_cmd]('set_general', cfg_tuning, launch_cmd, idrac_launch) if ret['changes'].get('Slot Names') is not None: slot_rets = [] for s in slot_names: key = s.keys()[0] new_name = s[key] if key.startswith('slot-'): key = key[5:] slot_rets.append(__salt__[chassis_cmd]('set_slotname', key, new_name)) if any(slot_rets) is False: slot_names = False else: slot_names = True powerchange_all_ok = True for k, v in six.iteritems(target_power_states): powerchange_ok = __salt__[chassis_cmd]('server_power', v, module=k) if not powerchange_ok: powerchange_all_ok = False if any([name, location, mode, idrac_launch, slot_names, powerchange_all_ok]) is False: ret['result'] = False ret['comment'] = 'There was an error setting the Dell chassis.' ret['comment'] = 'Dell chassis was updated.' return ret
Manage a Dell Chassis. chassis_name The name of the chassis. datacenter The datacenter in which the chassis is located location The location of the chassis. password Password for the chassis. Note: If this password is set for the chassis, the current implementation of this state will set this password both on the chassis and the iDrac passwords on any configured blades. If the password for the blades should be distinct, they should be set separately with the blade_idrac function. mode The management mode of the chassis. Viable options are: - 0: None - 1: Monitor - 2: Manage and Monitor idrac_launch The iDRAC launch method of the chassis. Viable options are: - 0: Disabled (launch iDRAC using IP address) - 1: Enabled (launch iDRAC using DNS name) slot_names The names of the slots, provided as a list identified by their slot numbers. blade_power_states The power states of a blade server, provided as a list and identified by their server numbers. Viable options are: - on: Ensure the blade server is powered on. - off: Ensure the blade server is powered off. - powercycle: Power cycle the blade server. Example: .. code-block:: yaml my-dell-chassis: dellchassis.chassis: - chassis_name: my-dell-chassis - location: my-location - datacenter: london - mode: 2 - idrac_launch: 1 - slot_names: - 1: my-slot-name - 2: my-other-slot-name - blade_power_states: - server-1: on - server-2: off - server-3: powercycle
def make_symmetric_matrix_from_upper_tri(val): """ Given a symmetric matrix in upper triangular matrix form as flat array indexes as: [A_xx,A_yy,A_zz,A_xy,A_xz,A_yz] This will generate the full matrix: [[A_xx,A_xy,A_xz],[A_xy,A_yy,A_yz],[A_xz,A_yz,A_zz] """ idx = [0,3,4,1,5,2] val = np.array(val)[idx] mask = ~np.tri(3,k=-1,dtype=bool) out = np.zeros((3,3),dtype=val.dtype) out[mask] = val out.T[mask] = val return out
Given a symmetric matrix in upper triangular matrix form as flat array indexes as: [A_xx,A_yy,A_zz,A_xy,A_xz,A_yz] This will generate the full matrix: [[A_xx,A_xy,A_xz],[A_xy,A_yy,A_yz],[A_xz,A_yz,A_zz]
def create_section( aggregation_summary, analysis_layer, postprocessor_fields, section_header, use_aggregation=True, units_label=None, use_rounding=True, extra_component_args=None): """Create demographic section context. :param aggregation_summary: Aggregation summary :type aggregation_summary: qgis.core.QgsVectorlayer :param analysis_layer: Analysis layer :type analysis_layer: qgis.core.QgsVectorLayer :param postprocessor_fields: Postprocessor fields to extract :type postprocessor_fields: list[dict] :param section_header: Section header text :type section_header: qgis.core.QgsVectorLayer :param use_aggregation: Flag, if using aggregation layer :type use_aggregation: bool :param units_label: Unit label for each column :type units_label: list[str] :param use_rounding: flag for rounding, affect number representations :type use_rounding: bool :param extra_component_args: extra_args passed from report component metadata :type extra_component_args: dict :return: context for gender section :rtype: dict .. versionadded:: 4.0 """ if use_aggregation: return create_section_with_aggregation( aggregation_summary, analysis_layer, postprocessor_fields, section_header, units_label=units_label, use_rounding=use_rounding, extra_component_args=extra_component_args) else: return create_section_without_aggregation( aggregation_summary, analysis_layer, postprocessor_fields, section_header, units_label=units_label, use_rounding=use_rounding, extra_component_args=extra_component_args)
Create demographic section context. :param aggregation_summary: Aggregation summary :type aggregation_summary: qgis.core.QgsVectorlayer :param analysis_layer: Analysis layer :type analysis_layer: qgis.core.QgsVectorLayer :param postprocessor_fields: Postprocessor fields to extract :type postprocessor_fields: list[dict] :param section_header: Section header text :type section_header: qgis.core.QgsVectorLayer :param use_aggregation: Flag, if using aggregation layer :type use_aggregation: bool :param units_label: Unit label for each column :type units_label: list[str] :param use_rounding: flag for rounding, affect number representations :type use_rounding: bool :param extra_component_args: extra_args passed from report component metadata :type extra_component_args: dict :return: context for gender section :rtype: dict .. versionadded:: 4.0
def searchResponse(self, queryType, query, vendorSpecific=None, **kwargs): """CNRead.search(session, queryType, query) → ObjectList https://releases.dataone.org/online/api- documentation-v2.0.1/apis/CN_APIs.html#CNRead.search. Args: queryType: query: vendorSpecific: **kwargs: Returns: """ return self.GET( ['search', queryType, query], headers=vendorSpecific, query=kwargs )
CNRead.search(session, queryType, query) → ObjectList https://releases.dataone.org/online/api- documentation-v2.0.1/apis/CN_APIs.html#CNRead.search. Args: queryType: query: vendorSpecific: **kwargs: Returns:
def available_services(): """ get the available services to be activated read the models dir to find the services installed to be added to the system by the administrator """ all_datas = () data = () for class_path in settings.TH_SERVICES: class_name = class_path.rsplit('.', 1)[1] # 2nd array position contains the name of the service data = (class_name, class_name.rsplit('Service', 1)[1]) all_datas = (data,) + all_datas return all_datas
get the available services to be activated read the models dir to find the services installed to be added to the system by the administrator
def get(self, names_to_get, extract_numpy_scalars=True, on_new_output=None): """ Loads the requested variables from the matlab shell. names_to_get can be either a variable name, a list of variable names, or None. If it is a variable name, the values is returned. If it is a list, a dictionary of variable_name -> value is returned. If it is None, a dictionary with all variables is returned. If extract_numpy_scalars is true, the method will convert numpy scalars (0-dimension arrays) to a regular python variable. """ self._check_open() single_item = isinstance(names_to_get, (unicode, str)) if single_item: names_to_get = [names_to_get] if names_to_get == None: self.process.stdin.write('save stdio;\n') else: # Make sure that we throw an excpetion if the names are not defined. for name in names_to_get: self.eval('%s;' % name, print_expression=False, on_new_output=on_new_output) #print 'save(\'stdio\', \'%s\');\n' % '\', \''.join(names_to_get) self.process.stdin.write( "save('stdio', '%s', '-v7');\n" % '\', \''.join(names_to_get)) # We have to read to a temp buffer because mlabio.loadmat needs # random access :( self._read_until('start_binary\n', on_new_output=on_new_output) #print 'got start_binary' temp_str = self._sync_output(on_new_output=on_new_output) #print 'got all outout' # Remove expected output and "\n>>" # TODO(dani): Get rid of the unecessary copy. # MATLAB 2010a adds an extra >> so we need to remove more spaces. if self.matlab_version == (2010, 'a'): temp_str = temp_str[:-len(self.expected_output_end)-6] else: temp_str = temp_str[:-len(self.expected_output_end)-3] temp = StringIO(temp_str) #print ('____') #print len(temp_str) #print ('____') ret = mlabio.loadmat(temp, chars_as_strings=True, squeeze_me=True) #print '******' #print ret #print '******' temp.close() if single_item: return ret.values()[0] for key in ret.iterkeys(): while ret[key].shape and ret[key].shape[-1] == 1: ret[key] = ret[key][0] if extract_numpy_scalars: if isinstance(ret[key], np.ndarray) and not ret[key].shape: ret[key] = ret[key].tolist() #print 'done' return ret
Loads the requested variables from the matlab shell. names_to_get can be either a variable name, a list of variable names, or None. If it is a variable name, the values is returned. If it is a list, a dictionary of variable_name -> value is returned. If it is None, a dictionary with all variables is returned. If extract_numpy_scalars is true, the method will convert numpy scalars (0-dimension arrays) to a regular python variable.
def __output_thread(self): "Output thread" while self.alive: instructions = self.__get_instructions() self.__process_instructions(instructions)
Output thread
def create_user(self, name, password, is_super=False): """ Create a new MapD user Parameters ---------- name : string User name password : string Password is_super : bool if user is a superuser """ statement = ddl.CreateUser( name=name, password=password, is_super=is_super ) self._execute(statement)
Create a new MapD user Parameters ---------- name : string User name password : string Password is_super : bool if user is a superuser
def policy_net(rng_key, batch_observations_shape, num_actions, bottom_layers=None): """A policy net function.""" # Use the bottom_layers as the bottom part of the network and just add the # required layers on top of it. if bottom_layers is None: bottom_layers = [] # NOTE: The LogSoftmax instead of the Softmax. bottom_layers.extend([layers.Dense(num_actions), layers.LogSoftmax()]) net = layers.Serial(*bottom_layers) return net.initialize(batch_observations_shape, rng_key), net
A policy net function.
def _all_unique_texts(text, final): """ Compute all the possible unique texts @type text: str @param text: Text written used spin syntax @type final: list @param final: An empty list where all the unique texts will be stored @return: Nothing. The result will be in the 'final' list """ if not char_opening in text: if not text in final: final.append(text) return stack = [] indexes = [] for i, c in enumerate(text): if c == char_closing: if stack[-1] == char_opening: start_index = indexes.pop() substring = '' if i == start_index + 1 else text[start_index:i + 1] # get some random combination combination = next(_choices(substring)) new_text = text.replace(substring, combination) _all_unique_texts(new_text, final) return elif c == char_opening: stack.append(c) indexes.append(i)
Compute all the possible unique texts @type text: str @param text: Text written used spin syntax @type final: list @param final: An empty list where all the unique texts will be stored @return: Nothing. The result will be in the 'final' list
def Marginal(self, i, name=''): """Gets the marginal distribution of the indicated variable. i: index of the variable we want Returns: Pmf """ pmf = Pmf(name=name) for vs, prob in self.Items(): pmf.Incr(vs[i], prob) return pmf
Gets the marginal distribution of the indicated variable. i: index of the variable we want Returns: Pmf
def _get_user_ns_object(shell, path): """Get object from the user namespace, given a path containing zero or more dots. Return None if the path is not valid. """ parts = path.split('.', 1) name, attr = parts[0], parts[1:] if name in shell.user_ns: if attr: try: return _getattr(shell.user_ns[name], attr[0]) except AttributeError: return None else: return shell.user_ns[name] return None
Get object from the user namespace, given a path containing zero or more dots. Return None if the path is not valid.
def get_types_by_attr(resource, template_id=None): """ Using the attributes of the resource, get all the types that this resource matches. @returns a dictionary, keyed on the template name, with the value being the list of type names which match the resources attributes. """ resource_type_templates = [] #Create a list of all of this resources attributes. attr_ids = [] for res_attr in resource.attributes: attr_ids.append(res_attr.attr_id) all_resource_attr_ids = set(attr_ids) all_types = db.DBSession.query(TemplateType).options(joinedload_all('typeattrs')).filter(TemplateType.resource_type==resource.ref_key) if template_id is not None: all_types = all_types.filter(TemplateType.template_id==template_id) all_types = all_types.all() #tmpl type attrs must be a subset of the resource's attrs for ttype in all_types: type_attr_ids = [] for typeattr in ttype.typeattrs: type_attr_ids.append(typeattr.attr_id) if set(type_attr_ids).issubset(all_resource_attr_ids): resource_type_templates.append(ttype) return resource_type_templates
Using the attributes of the resource, get all the types that this resource matches. @returns a dictionary, keyed on the template name, with the value being the list of type names which match the resources attributes.
def acquireConnection(self): """ Get a connection from the pool. Parameters: ---------------------------------------------------------------- retval: A ConnectionWrapper instance. NOTE: Caller is responsible for calling the ConnectionWrapper instance's release() method or use it in a context manager expression (with ... as:) to release resources. """ self._logger.debug("Acquiring connection") dbConn = self._pool.connection(shareable=False) connWrap = ConnectionWrapper(dbConn=dbConn, cursor=dbConn.cursor(), releaser=self._releaseConnection, logger=self._logger) return connWrap
Get a connection from the pool. Parameters: ---------------------------------------------------------------- retval: A ConnectionWrapper instance. NOTE: Caller is responsible for calling the ConnectionWrapper instance's release() method or use it in a context manager expression (with ... as:) to release resources.
def process(self): """! @brief Performs cluster analysis in line with rules of K-Medians algorithm. @return (kmedians) Returns itself (K-Medians instance). @remark Results of clustering can be obtained using corresponding get methods. @see get_clusters() @see get_medians() """ if self.__ccore is True: ccore_metric = metric_wrapper.create_instance(self.__metric) self.__clusters, self.__medians = wrapper.kmedians(self.__pointer_data, self.__medians, self.__tolerance, self.__itermax, ccore_metric.get_pointer()) else: changes = float('inf') # Check for dimension if len(self.__pointer_data[0]) != len(self.__medians[0]): raise NameError('Dimension of the input data and dimension of the initial medians must be equal.') iterations = 0 while changes > self.__tolerance and iterations < self.__itermax: self.__clusters = self.__update_clusters() updated_centers = self.__update_medians() changes = max([self.__metric(self.__medians[index], updated_centers[index]) for index in range(len(updated_centers))]) self.__medians = updated_centers iterations += 1 return self
! @brief Performs cluster analysis in line with rules of K-Medians algorithm. @return (kmedians) Returns itself (K-Medians instance). @remark Results of clustering can be obtained using corresponding get methods. @see get_clusters() @see get_medians()
def mt_report(context, case_id, test, outpath=None): """Export all mitochondrial variants for each sample of a case and write them to an excel file Args: adapter(MongoAdapter) case_id(str) test(bool): True if the function is called for testing purposes outpath(str): path to output file Returns: written_files(int): number of written or simulated files """ LOG.info('exporting mitochondrial variants for case "{}"'.format(case_id)) adapter = context.obj['adapter'] query = {'chrom':'MT'} case_obj = adapter.case(case_id=case_id) if not case_obj: LOG.warning('Could not find a scout case with id "{}". No report was created.'.format(case_id)) context.abort() samples = case_obj.get('individuals') mt_variants = list(adapter.variants(case_id=case_id, query=query, nr_of_variants= -1, sort_key='position')) if not mt_variants: LOG.warning('There are no MT variants associated to case {} in database!'.format(case_id)) context.abort() today = datetime.datetime.now().strftime('%Y-%m-%d') # set up outfolder if not outpath: outpath = str(os.getcwd()) # get document lines for each of the cases's individuals # Write excel document for each sample in case written_files = 0 for sample in samples: sample_id = sample['individual_id'] sample_lines = export_mt_variants(variants=mt_variants, sample_id=sample_id) # set up document name document_name = '.'.join([case_obj['display_name'], sample_id, today]) + '.xlsx' workbook = Workbook(os.path.join(outpath,document_name)) Report_Sheet = workbook.add_worksheet() if test and sample_lines and workbook: written_files +=1 continue # Write the column header row = 0 for col,field in enumerate(MT_EXPORT_HEADER): Report_Sheet.write(row,col,field) # Write variant lines, after header (start at line 1) for row, line in enumerate(sample_lines,1): # each line becomes a row in the document for col, field in enumerate(line): # each field in line becomes a cell Report_Sheet.write(row,col,field) workbook.close() if os.path.exists(os.path.join(outpath,document_name)): written_files += 1 if test: LOG.info("Number of excel files that can be written to folder {0}: {1}".format(outpath, written_files)) else: LOG.info("Number of excel files written to folder {0}: {1}".format(outpath, written_files)) return written_files
Export all mitochondrial variants for each sample of a case and write them to an excel file Args: adapter(MongoAdapter) case_id(str) test(bool): True if the function is called for testing purposes outpath(str): path to output file Returns: written_files(int): number of written or simulated files
def _fetch_result(self): """ Fetch the queried object. """ self._result = self.conn.query_single(self.object_type, self.url_params, self.query_params)
Fetch the queried object.
def parse(cls, msg): """Parse message string to request object.""" lines = msg.splitlines() method, uri, version = lines[0].split() headers = cls.parse_headers('\r\n'.join(lines[1:])) return cls(version=version, uri=uri, method=method, headers=headers)
Parse message string to request object.
def make_generic_validator(validator: AnyCallable) -> 'ValidatorCallable': """ Make a generic function which calls a validator with the right arguments. Unfortunately other approaches (eg. return a partial of a function that builds the arguments) is slow, hence this laborious way of doing things. It's done like this so validators don't all need **kwargs in their signature, eg. any combination of the arguments "values", "fields" and/or "config" are permitted. """ sig = signature(validator) args = list(sig.parameters.keys()) first_arg = args.pop(0) if first_arg == 'self': raise ConfigError( f'Invalid signature for validator {validator}: {sig}, "self" not permitted as first argument, ' f'should be: (cls, value, values, config, field), "values", "config" and "field" are all optional.' ) elif first_arg == 'cls': # assume the second argument is value return wraps(validator)(_generic_validator_cls(validator, sig, set(args[1:]))) else: # assume the first argument was value which has already been removed return wraps(validator)(_generic_validator_basic(validator, sig, set(args)))
Make a generic function which calls a validator with the right arguments. Unfortunately other approaches (eg. return a partial of a function that builds the arguments) is slow, hence this laborious way of doing things. It's done like this so validators don't all need **kwargs in their signature, eg. any combination of the arguments "values", "fields" and/or "config" are permitted.
def compliance_schedule(self, column=None, value=None, **kwargs): """ A sequence of activities with associated milestones which pertains to a given permit. >>> PCS().compliance_schedule('cmpl_schd_evt', '62099') """ return self._resolve_call('PCS_CMPL_SCHD', column, value, **kwargs)
A sequence of activities with associated milestones which pertains to a given permit. >>> PCS().compliance_schedule('cmpl_schd_evt', '62099')
def receive_message(self, message, data): """ Called when a receiver-message has been received. """ if data[MESSAGE_TYPE] == TYPE_RECEIVER_STATUS: self._process_get_status(data) return True elif data[MESSAGE_TYPE] == TYPE_LAUNCH_ERROR: self._process_launch_error(data) return True return False
Called when a receiver-message has been received.
def lookup_providers(self, lookup): ''' Get a dict describing the configured providers ''' if lookup is None: lookup = 'all' if lookup == 'all': providers = set() for alias, drivers in six.iteritems(self.opts['providers']): for driver in drivers: providers.add((alias, driver)) if not providers: raise SaltCloudSystemExit( 'There are no cloud providers configured.' ) return providers if ':' in lookup: alias, driver = lookup.split(':') if alias not in self.opts['providers'] or \ driver not in self.opts['providers'][alias]: raise SaltCloudSystemExit( 'No cloud providers matched \'{0}\'. Available: {1}'.format( lookup, ', '.join(self.get_configured_providers()) ) ) providers = set() for alias, drivers in six.iteritems(self.opts['providers']): for driver in drivers: if lookup in (alias, driver): providers.add((alias, driver)) if not providers: raise SaltCloudSystemExit( 'No cloud providers matched \'{0}\'. ' 'Available selections: {1}'.format( lookup, ', '.join(self.get_configured_providers()) ) ) return providers
Get a dict describing the configured providers
def _deserialize(self, value, attr, data): """Format and validate the phone number using libphonenumber.""" if value: value = self._format_phone_number(value, attr) return super(PhoneNumberField, self)._deserialize(value, attr, data)
Format and validate the phone number using libphonenumber.
def titleCounts(readsAlignments): """ Count the number of times each title in a readsAlignments instance is matched. This is useful for rapidly discovering what titles were matched and with what frequency. @param readsAlignments: A L{dark.alignments.ReadsAlignments} instance. @return: A C{dict} whose keys are titles and whose values are the integer counts of the number of reads that matched that title. """ titles = defaultdict(int) for readAlignments in readsAlignments: for alignment in readAlignments: titles[alignment.subjectTitle] += 1 return titles
Count the number of times each title in a readsAlignments instance is matched. This is useful for rapidly discovering what titles were matched and with what frequency. @param readsAlignments: A L{dark.alignments.ReadsAlignments} instance. @return: A C{dict} whose keys are titles and whose values are the integer counts of the number of reads that matched that title.
def _export_table(dataset, column_names=None, byteorder="=", shuffle=False, selection=False, progress=None, virtual=True, sort=None, ascending=True): """ :param DatasetLocal dataset: dataset to export :param str path: path for file :param lis[str] column_names: list of column names to export or None for all columns :param str byteorder: = for native, < for little endian and > for big endian :param bool shuffle: export rows in random order :param bool selection: export selection or not :param progress: progress callback that gets a progress fraction as argument and should return True to continue, or a default progress bar when progress=True :param: bool virtual: When True, export virtual columns :return: """ column_names = column_names or dataset.get_column_names(virtual=virtual, strings=True) for name in column_names: if name not in dataset.columns: warnings.warn('Exporting to arrow with virtual columns is not efficient') N = len(dataset) if not selection else dataset.selected_length(selection) if N == 0: raise ValueError("Cannot export empty table") if shuffle and sort: raise ValueError("Cannot shuffle and sort at the same time") if shuffle: random_index_column = "random_index" while random_index_column in dataset.get_column_names(): random_index_column += "_new" partial_shuffle = shuffle and len(dataset) != N order_array = None if partial_shuffle: # if we only export a portion, we need to create the full length random_index array, and shuffle_array_full = np.random.choice(len(dataset), len(dataset), replace=False) # then take a section of it # shuffle_array[:] = shuffle_array_full[:N] shuffle_array = shuffle_array_full[shuffle_array_full < N] del shuffle_array_full order_array = shuffle_array elif shuffle: shuffle_array = np.random.choice(N, N, replace=False) order_array = shuffle_array if sort: if selection: raise ValueError("sorting selections not yet supported") logger.info("sorting...") indices = np.argsort(dataset.evaluate(sort)) order_array = indices if ascending else indices[::-1] logger.info("sorting done") if selection: full_mask = dataset.evaluate_selection_mask(selection) else: full_mask = None arrow_arrays = [] for column_name in column_names: mask = full_mask if selection: values = dataset.evaluate(column_name, filtered=False) values = values[mask] else: values = dataset.evaluate(column_name) if shuffle or sort: indices = order_array values = values[indices] arrow_arrays.append(arrow_array_from_numpy_array(values)) if shuffle: arrow_arrays.append(arrow_array_from_numpy_array(order_array)) column_names = column_names + [random_index_column] table = pa.Table.from_arrays(arrow_arrays, column_names) return table
:param DatasetLocal dataset: dataset to export :param str path: path for file :param lis[str] column_names: list of column names to export or None for all columns :param str byteorder: = for native, < for little endian and > for big endian :param bool shuffle: export rows in random order :param bool selection: export selection or not :param progress: progress callback that gets a progress fraction as argument and should return True to continue, or a default progress bar when progress=True :param: bool virtual: When True, export virtual columns :return:
def regex(self, *pattern, **kwargs): """ Add re pattern :param pattern: :type pattern: :param kwargs: :type kwargs: :return: :rtype: """ set_defaults(self._kwargs, kwargs) set_defaults(self._regex_defaults, kwargs) set_defaults(self._defaults, kwargs) pattern = self.rebulk.build_re(*pattern, **kwargs) part = ChainPart(self, pattern) self.parts.append(part) return part
Add re pattern :param pattern: :type pattern: :param kwargs: :type kwargs: :return: :rtype:
def record_big_endian(self): # type: () -> bytes ''' A method to generate a string representing the big endian version of this Path Table Record. Parameters: None. Returns: A string representing the big endian version of this Path Table Record. ''' if not self._initialized: raise pycdlibexception.PyCdlibInternalError('Path Table Record not yet initialized') return self._record(utils.swab_32bit(self.extent_location), utils.swab_16bit(self.parent_directory_num))
A method to generate a string representing the big endian version of this Path Table Record. Parameters: None. Returns: A string representing the big endian version of this Path Table Record.
def find_route_functions_taint_args(self): """Find all route functions and taint all of their arguments. Yields: CFG of each route function, with args marked as tainted. """ for definition in _get_func_nodes(): if self.is_route_function(definition.node): yield self.get_func_cfg_with_tainted_args(definition)
Find all route functions and taint all of their arguments. Yields: CFG of each route function, with args marked as tainted.
def to_text(self, line): """ Return the textual representation of the given `line`. """ return getattr(self, self.ENTRY_TRANSFORMERS[line.__class__])(line)
Return the textual representation of the given `line`.
def build_dataset(instruction_dicts, dataset_from_file_fn, shuffle_files=False, parallel_reads=64): """Constructs a `tf.data.Dataset` from TFRecord files. Args: instruction_dicts: `list` of {'filepath':, 'mask':, 'offset_mask':} containing the information about which files and which examples to use. The boolean mask will be repeated and zipped with the examples from filepath. dataset_from_file_fn: function returning a `tf.data.Dataset` given a filename. shuffle_files: `bool`, Whether to shuffle the input filenames. parallel_reads: `int`, how many files to read in parallel. Returns: `tf.data.Dataset` """ # First case: All examples are taken (No value skipped) if _no_examples_skipped(instruction_dicts): # Only use the filenames as instruction instruction_ds = tf.data.Dataset.from_tensor_slices([ d["filepath"] for d in instruction_dicts ]) build_ds_from_instruction = dataset_from_file_fn # Second case: Use the instructions to read the examples else: instruction_ds = _build_instruction_ds(instruction_dicts) build_ds_from_instruction = functools.partial( _build_ds_from_instruction, ds_from_file_fn=dataset_from_file_fn, ) # If shuffle is True, we shuffle the instructions/shards if shuffle_files: instruction_ds = instruction_ds.shuffle(len(instruction_dicts)) # Use interleave to parallel read files and decode records ds = instruction_ds.interleave( build_ds_from_instruction, cycle_length=parallel_reads, num_parallel_calls=tf.data.experimental.AUTOTUNE) return ds
Constructs a `tf.data.Dataset` from TFRecord files. Args: instruction_dicts: `list` of {'filepath':, 'mask':, 'offset_mask':} containing the information about which files and which examples to use. The boolean mask will be repeated and zipped with the examples from filepath. dataset_from_file_fn: function returning a `tf.data.Dataset` given a filename. shuffle_files: `bool`, Whether to shuffle the input filenames. parallel_reads: `int`, how many files to read in parallel. Returns: `tf.data.Dataset`
def update(self, *args, **kwargs): ''' Updates the set to include all arguments passed in. If the keyword argument preprocess is passed, then each element is preprocessed before being added. ''' preprocess = kwargs.get('preprocess') for s in args: for e in s: self._dict_set(preprocess(e) if preprocess else e, True)
Updates the set to include all arguments passed in. If the keyword argument preprocess is passed, then each element is preprocessed before being added.
def p0(self): """A dictionary of the initial position of the walkers. This is set by using ``set_p0``. If not set yet, a ``ValueError`` is raised when the attribute is accessed. """ if self._p0 is None: raise ValueError("initial positions not set; run set_p0") # convert to dict p0 = {param: self._p0[..., k] for (k, param) in enumerate(self.sampling_params)} return p0
A dictionary of the initial position of the walkers. This is set by using ``set_p0``. If not set yet, a ``ValueError`` is raised when the attribute is accessed.
def phrase_contains_special_keys(expansion: model.Expansion) -> bool: """ Determine if the expansion contains any special keys, including those resulting from any processed macros (<script>, <file>, etc). If any are found, the phrase cannot be undone. Python Zen: »In the face of ambiguity, refuse the temptation to guess.« The question 'What does the phrase expansion "<ctrl>+a<shift>+<insert>" do?' cannot be answered. Because the key bindings cannot be assumed to result in the actions "select all text, then replace with clipboard content", the undo operation can not be performed. Thus always disable undo, when special keys are found. """ found_special_keys = KEY_FIND_RE.findall(expansion.string.lower()) return bool(found_special_keys)
Determine if the expansion contains any special keys, including those resulting from any processed macros (<script>, <file>, etc). If any are found, the phrase cannot be undone. Python Zen: »In the face of ambiguity, refuse the temptation to guess.« The question 'What does the phrase expansion "<ctrl>+a<shift>+<insert>" do?' cannot be answered. Because the key bindings cannot be assumed to result in the actions "select all text, then replace with clipboard content", the undo operation can not be performed. Thus always disable undo, when special keys are found.
def html_to_cnxml(html_source, cnxml_source): """Transform the HTML to CNXML. We need the original CNXML content in order to preserve the metadata in the CNXML document. """ source = _string2io(html_source) xml = etree.parse(source) cnxml = etree.parse(_string2io(cnxml_source)) # Run the HTML to CNXML transform on it xml = _transform('html5-to-cnxml.xsl', xml) # Replace the original content element with the transformed one. namespaces = {'c': 'http://cnx.rice.edu/cnxml'} xpath = etree.XPath('//c:content', namespaces=namespaces) replaceable_node = xpath(cnxml)[0] replaceable_node.getparent().replace(replaceable_node, xml.getroot()) # Set the content into the existing cnxml source return etree.tostring(cnxml)
Transform the HTML to CNXML. We need the original CNXML content in order to preserve the metadata in the CNXML document.
def canonical_value(self, query): """ Return the canonical value corresponding to the given query value. Return ``None`` if the query value is not present in any descriptor of the group. :param str query: the descriptor value to be checked against """ for d in self.descriptors: if query in d: return d.canonical_label return None
Return the canonical value corresponding to the given query value. Return ``None`` if the query value is not present in any descriptor of the group. :param str query: the descriptor value to be checked against
def load_model(model_name, epoch_num, data_shapes, label_shapes, label_names, gpus=''): """Returns a module loaded with the provided model. Parameters ---------- model_name: str Prefix of the MXNet model name as stored on the local directory. epoch_num : int Epoch number of model we would like to load. input_shape: tuple The shape of the input data in the form of (batch_size, channels, height, width) files: list of strings List of URLs pertaining to files that need to be downloaded in order to use the model. data_shapes: list of tuples. List of tuples where each tuple is a pair of input variable name and its shape. label_shapes: list of (str, tuple) Typically is ``data_iter.provide_label``. label_names: list of str Name of the output labels in the MXNet symbolic graph. gpus: str Comma separated string of gpu ids on which inferences are executed. E.g. 3,5,6 would refer to GPUs 3, 5 and 6. If empty, we use CPU. Returns ------- MXNet module """ sym, arg_params, aux_params = mx.model.load_checkpoint(model_name, epoch_num) mod = create_module(sym, data_shapes, label_shapes, label_names, gpus) mod.set_params( arg_params=arg_params, aux_params=aux_params, allow_missing=True ) return mod
Returns a module loaded with the provided model. Parameters ---------- model_name: str Prefix of the MXNet model name as stored on the local directory. epoch_num : int Epoch number of model we would like to load. input_shape: tuple The shape of the input data in the form of (batch_size, channels, height, width) files: list of strings List of URLs pertaining to files that need to be downloaded in order to use the model. data_shapes: list of tuples. List of tuples where each tuple is a pair of input variable name and its shape. label_shapes: list of (str, tuple) Typically is ``data_iter.provide_label``. label_names: list of str Name of the output labels in the MXNet symbolic graph. gpus: str Comma separated string of gpu ids on which inferences are executed. E.g. 3,5,6 would refer to GPUs 3, 5 and 6. If empty, we use CPU. Returns ------- MXNet module
def derive_key(self, master_password): """ Computes the key from the salt and the master password. """ encoder = encoding.Encoder(self.charset) bytes = ('%s:%s' % (master_password, self.name)).encode('utf8') start_time = time.clock() # we fix the scrypt parameters in case the defaults change digest = scrypt.hash(bytes, self.salt, N=1<<14, r=8, p=1) key = encoder.encode(digest, self.key_length) derivation_time_in_s = time.clock() - start_time _logger.debug('Key derivation took %.2fms', derivation_time_in_s*1000) return key
Computes the key from the salt and the master password.
def insert(self, before, name, attrs=None, data=None): """ Inserts a new element as a child of this element, before the specified index or sibling. :param before: An :class:`XmlElement` or a numeric index to insert the new node before :param name: The tag name to add :param attrs: Attributes for the new tag :param data: CDATA for the new tag :returns: The newly-created element :rtype: :class:`XmlElement` """ if isinstance(before, self.__class__): if before.parent != self: raise ValueError('Cannot insert before an element with a different parent.') before = before.index # Make sure 0 <= before <= len(_children). before = min(max(0, before), len(self._children)) elem = self.__class__(name, attrs, data, parent=self, index=before) self._children.insert(before, elem) # Re-index all the children. for idx, c in enumerate(self._children): c.index = idx return elem
Inserts a new element as a child of this element, before the specified index or sibling. :param before: An :class:`XmlElement` or a numeric index to insert the new node before :param name: The tag name to add :param attrs: Attributes for the new tag :param data: CDATA for the new tag :returns: The newly-created element :rtype: :class:`XmlElement`
def _language_index_from_code(self, code, name_mode): """Return the index value for a language code. This returns l_any if more than one code is specified or the code is out of bounds. Parameters ---------- code : int The language code to interpret name_mode : str The name mode of the algorithm: ``gen`` (default), ``ash`` (Ashkenazi), or ``sep`` (Sephardic) Returns ------- int Language code index """ if code < 1 or code > sum( _LANG_DICT[_] for _ in BMDATA[name_mode]['languages'] ): # code out of range return L_ANY if ( code & (code - 1) ) != 0: # choice was more than one language; use any return L_ANY return code
Return the index value for a language code. This returns l_any if more than one code is specified or the code is out of bounds. Parameters ---------- code : int The language code to interpret name_mode : str The name mode of the algorithm: ``gen`` (default), ``ash`` (Ashkenazi), or ``sep`` (Sephardic) Returns ------- int Language code index
def send_config_set( self, config_commands=None, exit_config_mode=True, delay_factor=1, max_loops=150, strip_prompt=False, strip_command=False, config_mode_command=None, ): """ Send configuration commands down the SSH channel. config_commands is an iterable containing all of the configuration commands. The commands will be executed one after the other. Automatically exits/enters configuration mode. :param config_commands: Multiple configuration commands to be sent to the device :type config_commands: list or string :param exit_config_mode: Determines whether or not to exit config mode after complete :type exit_config_mode: bool :param delay_factor: Factor to adjust delays :type delay_factor: int :param max_loops: Controls wait time in conjunction with delay_factor (default: 150) :type max_loops: int :param strip_prompt: Determines whether or not to strip the prompt :type strip_prompt: bool :param strip_command: Determines whether or not to strip the command :type strip_command: bool :param config_mode_command: The command to enter into config mode :type config_mode_command: str """ delay_factor = self.select_delay_factor(delay_factor) if config_commands is None: return "" elif isinstance(config_commands, string_types): config_commands = (config_commands,) if not hasattr(config_commands, "__iter__"): raise ValueError("Invalid argument passed into send_config_set") # Send config commands cfg_mode_args = (config_mode_command,) if config_mode_command else tuple() output = self.config_mode(*cfg_mode_args) for cmd in config_commands: self.write_channel(self.normalize_cmd(cmd)) if self.fast_cli: pass else: time.sleep(delay_factor * 0.05) # Gather output output += self._read_channel_timing( delay_factor=delay_factor, max_loops=max_loops ) if exit_config_mode: output += self.exit_config_mode() output = self._sanitize_output(output) log.debug("{}".format(output)) return output
Send configuration commands down the SSH channel. config_commands is an iterable containing all of the configuration commands. The commands will be executed one after the other. Automatically exits/enters configuration mode. :param config_commands: Multiple configuration commands to be sent to the device :type config_commands: list or string :param exit_config_mode: Determines whether or not to exit config mode after complete :type exit_config_mode: bool :param delay_factor: Factor to adjust delays :type delay_factor: int :param max_loops: Controls wait time in conjunction with delay_factor (default: 150) :type max_loops: int :param strip_prompt: Determines whether or not to strip the prompt :type strip_prompt: bool :param strip_command: Determines whether or not to strip the command :type strip_command: bool :param config_mode_command: The command to enter into config mode :type config_mode_command: str
def _translate_cond(self, c): #pylint:disable=no-self-use """ Checks whether this condition can be supported by FastMemory." """ if isinstance(c, claripy.ast.Base) and not c.singlevalued: raise SimFastMemoryError("size not supported") if c is None: return True else: return self.state.solver.eval_upto(c, 1)[0]
Checks whether this condition can be supported by FastMemory."
def register_event(self, event): """Registers a new command line interface event hook as command""" self.log('Registering event hook:', event.cmd, event.thing, pretty=True, lvl=verbose) self.hooks[event.cmd] = event.thing
Registers a new command line interface event hook as command
def find_one(cls, *args, **kwargs): """Same as ``collection.find_one``, returns model object instead of dict.""" d = cls.collection.find_one(*args, **kwargs) if d: return cls(**d)
Same as ``collection.find_one``, returns model object instead of dict.
def _normalize_helper(number, replacements, remove_non_matches): """Normalizes a string of characters representing a phone number by replacing all characters found in the accompanying map with the values therein, and stripping all other characters if remove_non_matches is true. Arguments: number -- a string representing a phone number replacements -- a mapping of characters to what they should be replaced by in the normalized version of the phone number remove_non_matches -- indicates whether characters that are not able to be replaced should be stripped from the number. If this is False, they will be left unchanged in the number. Returns the normalized string version of the phone number. """ normalized_number = [] for char in number: new_digit = replacements.get(char.upper(), None) if new_digit is not None: normalized_number.append(new_digit) elif not remove_non_matches: normalized_number.append(char) # If neither of the above are true, we remove this character return U_EMPTY_STRING.join(normalized_number)
Normalizes a string of characters representing a phone number by replacing all characters found in the accompanying map with the values therein, and stripping all other characters if remove_non_matches is true. Arguments: number -- a string representing a phone number replacements -- a mapping of characters to what they should be replaced by in the normalized version of the phone number remove_non_matches -- indicates whether characters that are not able to be replaced should be stripped from the number. If this is False, they will be left unchanged in the number. Returns the normalized string version of the phone number.
def univariate(self, data: ['SASdata', str] = None, by: [str, list] = None, cdfplot: str = None, cls: [str, list] = None, freq: str = None, histogram: str = None, id: [str, list] = None, inset: str = None, output: [str, bool, 'SASdata'] = None, ppplot: str = None, probplot: str = None, qqplot: str = None, var: str = None, weight: str = None, procopts: str = None, stmtpassthrough: str = None, **kwargs: dict) -> 'SASresults': """ Python method to call the UNIVARIATE procedure Documentation link: https://go.documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=procstat&docsetTarget=procstat_univariate_syntax.htm&locale=en The PROC UNIVARIATE statement invokes the procedure. The VAR statement specifies the numeric variables to be analyzed, and it is required if the OUTPUT statement is used to save summary statistics in an output data set. If you do not use the VAR statement, all numeric variables in the data set are analyzed. The plot statements (CDFPLOT, HISTOGRAM, PPPLOT, PROBPLOT, and QQPLOT) create graphical displays, and the INSET statement enhances these displays by adding a table of summary statistics directly on the graph. You can specify one or more of each of the plot statements, the INSET statement, and the OUTPUT statement. If you use a VAR statement, the variables listed in a plot statement must be a subset of the variables listed in the VAR statement. You can specify a BY statement to obtain separate analyses for each BY group. The FREQ statement specifies a variable whose values provide the frequency for each observation. The ID statement specifies one or more variables to identify the extreme observations. The WEIGHT statement specifies a variable whose values are used to weight certain statistics. You can use a CLASS statement to specify one or two variables that group the data into classification levels. The analysis is carried out for each combination of levels in the input data set, or within each BY group if you also specify a BY statement. You can use the CLASS statement with plot statements to create comparative displays, in which each cell contains a plot for one combination of classification levels. :param data: SASdata object or string. This parameter is required. :parm by: The by variable can be a string or list type. :parm cdfplot: The cdfplot variable can only be a string type. :parm cls: The cls variable can be a string or list type. It refers to the categorical, or nominal variables. :parm freq: The freq variable can only be a string type. :parm histogram: The histogram variable can only be a string type. :parm id: The id variable can be a string or list type. :parm inset: The inset variable can only be a string type. :parm output: The output variable can be a string, boolean or SASdata type. The member name for a boolean is "_output". :parm ppplot: The ppplot variable can only be a string type. :parm probplot: The probplot variable can only be a string type. :parm qqplot: The qqplot variable can only be a string type. :parm var: The var variable can only be a string type. :parm weight: The weight variable can only be a string type. :parm procopts: The procopts variable is a generic option available for advanced use. It can only be a string type. :parm stmtpassthrough: The stmtpassthrough variable is a generic option available for advanced use. It can only be a string type. :return: SAS Result Object """
Python method to call the UNIVARIATE procedure Documentation link: https://go.documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=procstat&docsetTarget=procstat_univariate_syntax.htm&locale=en The PROC UNIVARIATE statement invokes the procedure. The VAR statement specifies the numeric variables to be analyzed, and it is required if the OUTPUT statement is used to save summary statistics in an output data set. If you do not use the VAR statement, all numeric variables in the data set are analyzed. The plot statements (CDFPLOT, HISTOGRAM, PPPLOT, PROBPLOT, and QQPLOT) create graphical displays, and the INSET statement enhances these displays by adding a table of summary statistics directly on the graph. You can specify one or more of each of the plot statements, the INSET statement, and the OUTPUT statement. If you use a VAR statement, the variables listed in a plot statement must be a subset of the variables listed in the VAR statement. You can specify a BY statement to obtain separate analyses for each BY group. The FREQ statement specifies a variable whose values provide the frequency for each observation. The ID statement specifies one or more variables to identify the extreme observations. The WEIGHT statement specifies a variable whose values are used to weight certain statistics. You can use a CLASS statement to specify one or two variables that group the data into classification levels. The analysis is carried out for each combination of levels in the input data set, or within each BY group if you also specify a BY statement. You can use the CLASS statement with plot statements to create comparative displays, in which each cell contains a plot for one combination of classification levels. :param data: SASdata object or string. This parameter is required. :parm by: The by variable can be a string or list type. :parm cdfplot: The cdfplot variable can only be a string type. :parm cls: The cls variable can be a string or list type. It refers to the categorical, or nominal variables. :parm freq: The freq variable can only be a string type. :parm histogram: The histogram variable can only be a string type. :parm id: The id variable can be a string or list type. :parm inset: The inset variable can only be a string type. :parm output: The output variable can be a string, boolean or SASdata type. The member name for a boolean is "_output". :parm ppplot: The ppplot variable can only be a string type. :parm probplot: The probplot variable can only be a string type. :parm qqplot: The qqplot variable can only be a string type. :parm var: The var variable can only be a string type. :parm weight: The weight variable can only be a string type. :parm procopts: The procopts variable is a generic option available for advanced use. It can only be a string type. :parm stmtpassthrough: The stmtpassthrough variable is a generic option available for advanced use. It can only be a string type. :return: SAS Result Object
def qos(self, prefetch_size=0, prefetch_count=0, is_global=False): ''' Set QoS on this channel. ''' args = Writer() args.write_long(prefetch_size).\ write_short(prefetch_count).\ write_bit(is_global) self.send_frame(MethodFrame(self.channel_id, 60, 10, args)) self.channel.add_synchronous_cb(self._recv_qos_ok)
Set QoS on this channel.
def SNM0(T, Tc, Vc, omega, delta_SRK=None): r'''Calculates saturated liquid density using the Mchaweh, Moshfeghian model [1]_. Designed for simple calculations. .. math:: V_s = V_c/(1+1.169\tau^{1/3}+1.818\tau^{2/3}-2.658\tau+2.161\tau^{4/3} \tau = 1-\frac{(T/T_c)}{\alpha_{SRK}} \alpha_{SRK} = [1 + m(1-\sqrt{T/T_C}]^2 m = 0.480+1.574\omega-0.176\omega^2 If the fit parameter `delta_SRK` is provided, the following is used: .. math:: V_s = V_C/(1+1.169\tau^{1/3}+1.818\tau^{2/3}-2.658\tau+2.161\tau^{4/3}) /\left[1+\delta_{SRK}(\alpha_{SRK}-1)^{1/3}\right] Parameters ---------- T : float Temperature of fluid [K] Tc : float Critical temperature of fluid [K] Vc : float Critical volume of fluid [m^3/mol] omega : float Acentric factor for fluid, [-] delta_SRK : float, optional Fitting parameter [-] Returns ------- Vs : float Saturation liquid volume, [m^3/mol] Notes ----- 73 fit parameters have been gathered from the article. Examples -------- Argon, without the fit parameter and with it. Tabulated result in Perry's is 3.4613e-05. The fit increases the error on this occasion. >>> SNM0(121, 150.8, 7.49e-05, -0.004) 3.4402256402733416e-05 >>> SNM0(121, 150.8, 7.49e-05, -0.004, -0.03259620) 3.493288100008123e-05 References ---------- .. [1] Mchaweh, A., A. Alsaygh, Kh. Nasrifar, and M. Moshfeghian. "A Simplified Method for Calculating Saturated Liquid Densities." Fluid Phase Equilibria 224, no. 2 (October 1, 2004): 157-67. doi:10.1016/j.fluid.2004.06.054 ''' Tr = T/Tc m = 0.480 + 1.574*omega - 0.176*omega*omega alpha_SRK = (1. + m*(1. - Tr**0.5))**2 tau = 1. - Tr/alpha_SRK rho0 = 1. + 1.169*tau**(1/3.) + 1.818*tau**(2/3.) - 2.658*tau + 2.161*tau**(4/3.) V0 = 1./rho0 if not delta_SRK: return Vc*V0 else: return Vc*V0/(1. + delta_SRK*(alpha_SRK - 1.)**(1/3.))
r'''Calculates saturated liquid density using the Mchaweh, Moshfeghian model [1]_. Designed for simple calculations. .. math:: V_s = V_c/(1+1.169\tau^{1/3}+1.818\tau^{2/3}-2.658\tau+2.161\tau^{4/3} \tau = 1-\frac{(T/T_c)}{\alpha_{SRK}} \alpha_{SRK} = [1 + m(1-\sqrt{T/T_C}]^2 m = 0.480+1.574\omega-0.176\omega^2 If the fit parameter `delta_SRK` is provided, the following is used: .. math:: V_s = V_C/(1+1.169\tau^{1/3}+1.818\tau^{2/3}-2.658\tau+2.161\tau^{4/3}) /\left[1+\delta_{SRK}(\alpha_{SRK}-1)^{1/3}\right] Parameters ---------- T : float Temperature of fluid [K] Tc : float Critical temperature of fluid [K] Vc : float Critical volume of fluid [m^3/mol] omega : float Acentric factor for fluid, [-] delta_SRK : float, optional Fitting parameter [-] Returns ------- Vs : float Saturation liquid volume, [m^3/mol] Notes ----- 73 fit parameters have been gathered from the article. Examples -------- Argon, without the fit parameter and with it. Tabulated result in Perry's is 3.4613e-05. The fit increases the error on this occasion. >>> SNM0(121, 150.8, 7.49e-05, -0.004) 3.4402256402733416e-05 >>> SNM0(121, 150.8, 7.49e-05, -0.004, -0.03259620) 3.493288100008123e-05 References ---------- .. [1] Mchaweh, A., A. Alsaygh, Kh. Nasrifar, and M. Moshfeghian. "A Simplified Method for Calculating Saturated Liquid Densities." Fluid Phase Equilibria 224, no. 2 (October 1, 2004): 157-67. doi:10.1016/j.fluid.2004.06.054
def get_folder(self, folder): """ Return the details for a folder :calls: `GET /api/v1/folders/:id \ <https://canvas.instructure.com/doc/api/files.html#method.folders.show>`_ :param folder: The object or ID of the folder to retrieve. :type folder: :class:`canvasapi.folder.Folder` or int :rtype: :class:`canvasapi.folder.Folder` """ folder_id = obj_or_id(folder, "folder", (Folder,)) response = self.__requester.request( 'GET', 'folders/{}'.format(folder_id) ) return Folder(self.__requester, response.json())
Return the details for a folder :calls: `GET /api/v1/folders/:id \ <https://canvas.instructure.com/doc/api/files.html#method.folders.show>`_ :param folder: The object or ID of the folder to retrieve. :type folder: :class:`canvasapi.folder.Folder` or int :rtype: :class:`canvasapi.folder.Folder`
async def api_request(self, url, params): """Make api fetch request.""" request = None try: with async_timeout.timeout(DEFAULT_TIMEOUT, loop=self._event_loop): request = await self._api_session.get( url, params=params) if request.status != 200: _LOGGER.error('Error fetching Emby data: %s', request.status) return None request_json = await request.json() if 'error' in request_json: _LOGGER.error('Error converting Emby data to json: %s: %s', request_json['error']['code'], request_json['error']['message']) return None return request_json except (aiohttp.ClientError, asyncio.TimeoutError, ConnectionRefusedError) as err: _LOGGER.error('Error fetching Emby data: %s', err) return None
Make api fetch request.
def _get_column_ends(self): '''Guess where the ends of the columns lie. ''' ends = collections.Counter() for line in self.text.splitlines(): for matchobj in re.finditer('\s{2,}', line.lstrip()): ends[matchobj.end()] += 1 return ends
Guess where the ends of the columns lie.
def patch_namespaced_network_policy(self, name, namespace, body, **kwargs): """ partially update the specified NetworkPolicy This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.patch_namespaced_network_policy(name, namespace, body, async_req=True) >>> result = thread.get() :param async_req bool :param str name: name of the NetworkPolicy (required) :param str namespace: object name and auth scope, such as for teams and projects (required) :param object body: (required) :param str pretty: If 'true', then the output is pretty printed. :param str dry_run: When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed :param str field_manager: fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). :param bool force: Force is going to \"force\" Apply requests. It means user will re-acquire conflicting fields owned by other people. Force flag must be unset for non-apply patch requests. :return: V1beta1NetworkPolicy If the method is called asynchronously, returns the request thread. """ kwargs['_return_http_data_only'] = True if kwargs.get('async_req'): return self.patch_namespaced_network_policy_with_http_info(name, namespace, body, **kwargs) else: (data) = self.patch_namespaced_network_policy_with_http_info(name, namespace, body, **kwargs) return data
partially update the specified NetworkPolicy This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please pass async_req=True >>> thread = api.patch_namespaced_network_policy(name, namespace, body, async_req=True) >>> result = thread.get() :param async_req bool :param str name: name of the NetworkPolicy (required) :param str namespace: object name and auth scope, such as for teams and projects (required) :param object body: (required) :param str pretty: If 'true', then the output is pretty printed. :param str dry_run: When present, indicates that modifications should not be persisted. An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. Valid values are: - All: all dry run stages will be processed :param str field_manager: fieldManager is a name associated with the actor or entity that is making these changes. The value must be less than or 128 characters long, and only contain printable characters, as defined by https://golang.org/pkg/unicode/#IsPrint. This field is required for apply requests (application/apply-patch) but optional for non-apply patch types (JsonPatch, MergePatch, StrategicMergePatch). :param bool force: Force is going to \"force\" Apply requests. It means user will re-acquire conflicting fields owned by other people. Force flag must be unset for non-apply patch requests. :return: V1beta1NetworkPolicy If the method is called asynchronously, returns the request thread.
def delete_os_dummy_rtr_nwk(self, rtr_id, net_id, subnet_id): """Delete the dummy interface to the router. """ subnet_lst = set() subnet_lst.add(subnet_id) ret = self.os_helper.delete_intf_router(None, None, rtr_id, subnet_lst) if not ret: return ret return self.os_helper.delete_network_all_subnets(net_id)
Delete the dummy interface to the router.
def _geom_series_uint32(r, n): """Unsigned integer calculation of sum of geometric series: 1 + r + r^2 + r^3 + ... r^(n-1) summed to n terms. Calculated modulo 2**32. Use the formula (r**n - 1) / (r - 1) """ if n == 0: return 0 if n == 1 or r == 0: return 1 m = 2**32 # Split (r - 1) into common factors with the modulo 2**32 -- i.e. all # factors of 2; and other factors which are coprime with the modulo 2**32. other_factors = r - 1 common_factor = 1 while (other_factors % 2) == 0: other_factors //= 2 common_factor *= 2 other_factors_inverse = pow(other_factors, m - 1, m) numerator = pow(r, n, common_factor * m) - 1 return (numerator // common_factor * other_factors_inverse) % m
Unsigned integer calculation of sum of geometric series: 1 + r + r^2 + r^3 + ... r^(n-1) summed to n terms. Calculated modulo 2**32. Use the formula (r**n - 1) / (r - 1)
def timeout_thread_handler(timeout, stop_event): """A background thread to kill the process if it takes too long. Args: timeout (float): The number of seconds to wait before killing the process. stop_event (Event): An optional event to cleanly stop the background thread if required during testing. """ stop_happened = stop_event.wait(timeout) if stop_happened is False: print("Killing program due to %f second timeout" % timeout) os._exit(2)
A background thread to kill the process if it takes too long. Args: timeout (float): The number of seconds to wait before killing the process. stop_event (Event): An optional event to cleanly stop the background thread if required during testing.
def _check_status(status): """Take a cairo status code and raise an exception if/as appropriate.""" if status != constants.STATUS_SUCCESS: exception = STATUS_TO_EXCEPTION.get(status, CairoError) status_name = ffi.string(ffi.cast("cairo_status_t", status)) message = 'cairo returned %s: %s' % ( status_name, ffi.string(cairo.cairo_status_to_string(status))) raise exception(message, status)
Take a cairo status code and raise an exception if/as appropriate.
async def _chunked_upload(self, media, media_size, path=None, media_type=None, media_category=None, chunk_size=2**20, **params): """ upload media in chunks Parameters ---------- media : file object a file object of the media media_size : int size of the media path : str, optional filename of the media media_type : str, optional mime type of the media media_category : str, optional twitter media category, must be used with ``media_type`` chunk_size : int, optional size of a chunk in bytes params : dict, optional additional parameters of the request Returns ------- .data_processing.PeonyResponse Response of the request """ if isinstance(media, bytes): media = io.BytesIO(media) chunk = media.read(chunk_size) is_coro = asyncio.iscoroutine(chunk) if is_coro: chunk = await chunk if media_type is None: media_metadata = await utils.get_media_metadata(chunk, path) media_type, media_category = media_metadata elif media_category is None: media_category = utils.get_category(media_type) response = await self.upload.media.upload.post( command="INIT", total_bytes=media_size, media_type=media_type, media_category=media_category, **params ) media_id = response['media_id'] i = 0 while chunk: if is_coro: req = self.upload.media.upload.post(command="APPEND", media_id=media_id, media=chunk, segment_index=i) chunk, _ = await asyncio.gather(media.read(chunk_size), req) else: await self.upload.media.upload.post(command="APPEND", media_id=media_id, media=chunk, segment_index=i) chunk = media.read(chunk_size) i += 1 status = await self.upload.media.upload.post(command="FINALIZE", media_id=media_id) if 'processing_info' in status: while status['processing_info'].get('state') != "succeeded": processing_info = status['processing_info'] if processing_info.get('state') == "failed": error = processing_info.get('error', {}) message = error.get('message', str(status)) raise exceptions.MediaProcessingError(data=status, message=message, **params) delay = processing_info['check_after_secs'] await asyncio.sleep(delay) status = await self.upload.media.upload.get( command="STATUS", media_id=media_id, **params ) return response
upload media in chunks Parameters ---------- media : file object a file object of the media media_size : int size of the media path : str, optional filename of the media media_type : str, optional mime type of the media media_category : str, optional twitter media category, must be used with ``media_type`` chunk_size : int, optional size of a chunk in bytes params : dict, optional additional parameters of the request Returns ------- .data_processing.PeonyResponse Response of the request
def get_wifi_packet(frame, no_rtap=False): """Discriminates Wi-Fi packet and creates packet object. :frame: ctypes.Structure :no_rtap: Bool :return: obj Wi-Fi packet """ _, packet = WiHelper._strip_rtap(frame) frame_control = struct.unpack('BB', packet[:2]) cat = (frame_control[0] >> 2) & 0b0011 s_type = frame_control[0] >> 4 if cat not in _CATEGORIES_.keys(): logging.warning("unknown category: %d" % (cat)) return Unknown(frame, no_rtap) if s_type not in _SUBTYPES_[cat].keys(): logging.warning("unknown subtype %d in %s category" % (s_type, _CATEGORIES_[cat])) return Unknown(frame, no_rtap) if cat == 0: if s_type == 4: return ProbeReq(frame, no_rtap) elif s_type == 5: return ProbeResp(frame, no_rtap) elif s_type == 8: return Beacon(frame, no_rtap) else: return Management(frame, no_rtap) elif cat == 1: if s_type == 11: return RTS(frame, no_rtap) elif s_type == 12: return CTS(frame, no_rtap) elif s_type == 9: return BACK(frame, no_rtap) else: return Control(frame, no_rtap) elif cat == 2: if s_type == 8: return QosData(frame, no_rtap, parse_amsdu=True) else: return Data(frame, no_rtap)
Discriminates Wi-Fi packet and creates packet object. :frame: ctypes.Structure :no_rtap: Bool :return: obj Wi-Fi packet
def get_search_page(self, query): """Gets HTML source :param query: query to search engine :return: HTML source of search page of given query """ query_web_page = Webpage(self.url + self.parse_query(query)) query_web_page.get_html_source() # get html source return query_web_page.source
Gets HTML source :param query: query to search engine :return: HTML source of search page of given query
def find_same_between_dicts(dict1, dict2): """ 查找两个字典中的相同点,包括键、值、项,仅支持 hashable 对象 :param: * dict1: (dict) 比较的字典 1 * dict2: (dict) 比较的字典 2 :return: * dup_info: (namedtuple) 返回两个字典中相同的信息组成的具名元组 举例如下:: print('--- find_same_between_dicts demo---') dict1 = {'x':1, 'y':2, 'z':3} dict2 = {'w':10, 'x':1, 'y':2} res = find_same_between_dicts(dict1, dict2) print(res.item) print(res.key) print(res.value) print('---') 执行结果:: --- find_same_between_dicts demo--- set([('x', 1)]) {'x', 'y'} {1} --- """ Same_info = namedtuple('Same_info', ['item', 'key', 'value']) same_info = Same_info(set(dict1.items()) & set(dict2.items()), set(dict1.keys()) & set(dict2.keys()), set(dict1.values()) & set(dict2.values())) return same_info
查找两个字典中的相同点,包括键、值、项,仅支持 hashable 对象 :param: * dict1: (dict) 比较的字典 1 * dict2: (dict) 比较的字典 2 :return: * dup_info: (namedtuple) 返回两个字典中相同的信息组成的具名元组 举例如下:: print('--- find_same_between_dicts demo---') dict1 = {'x':1, 'y':2, 'z':3} dict2 = {'w':10, 'x':1, 'y':2} res = find_same_between_dicts(dict1, dict2) print(res.item) print(res.key) print(res.value) print('---') 执行结果:: --- find_same_between_dicts demo--- set([('x', 1)]) {'x', 'y'} {1} ---
def create_new(self, **kwargs): """ Creates a new License This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please define a `callback` function to be invoked when receiving the response. >>> def callback_function(response): >>> pprint(response) >>> >>> thread = api.create_new(callback=callback_function) :param callback function: The callback function for asynchronous request. (optional) :param LicenseRest body: :return: LicenseSingleton If the method is called asynchronously, returns the request thread. """ kwargs['_return_http_data_only'] = True if kwargs.get('callback'): return self.create_new_with_http_info(**kwargs) else: (data) = self.create_new_with_http_info(**kwargs) return data
Creates a new License This method makes a synchronous HTTP request by default. To make an asynchronous HTTP request, please define a `callback` function to be invoked when receiving the response. >>> def callback_function(response): >>> pprint(response) >>> >>> thread = api.create_new(callback=callback_function) :param callback function: The callback function for asynchronous request. (optional) :param LicenseRest body: :return: LicenseSingleton If the method is called asynchronously, returns the request thread.
def _parse_url_and_validate(cls, url): """ Recieves a URL string and validates it using urlparse. Args: url: A URL string Returns: parsed_url: A validated URL Raises: BadURLException """ parsed_url = urlparse(url) if parsed_url.scheme and parsed_url.netloc: final_url = parsed_url.geturl() else: raise BadURLException return final_url
Recieves a URL string and validates it using urlparse. Args: url: A URL string Returns: parsed_url: A validated URL Raises: BadURLException
def release(self, forceRelease=False): ''' release - Release the lock. @param forceRelease <bool> default False - If True, will release the lock even if we don't hold it. @return - True if lock is released, otherwise False ''' if not self.held: if forceRelease is False: return False # We were not holding the lock else: self.held = True # If we have force release set, pretend like we held its if not os.path.exists(self.lockPath): self.held = False self.acquiredAt = None return True if forceRelease is False: # We waited too long and lost the lock if self.maxLockAge and time.time() > self.acquiredAt + self.maxLockAge: self.held = False self.acquiredAt = None return False self.acquiredAt = None try: os.rmdir(self.lockPath) self.held = False return True except: self.held = False return False
release - Release the lock. @param forceRelease <bool> default False - If True, will release the lock even if we don't hold it. @return - True if lock is released, otherwise False
def arr_base10toN(anum10, aradix, *args): """ ARGS anum10 in number in base 10 aradix in convert <anum10> to number in base + <aradix> OPTIONAL forcelength in if nonzero, indicates the length + of the return array. Useful if + array needs to be zero padded. DESC Converts a scalar from base 10 to base radix. Return an array. """ new_num_arr = array(()) current = anum10 while current != 0: remainder = current % aradix new_num_arr = r_[remainder, new_num_arr] current = current / aradix forcelength = new_num_arr.size # Optionally, allow user to specify word length if len(args): forcelength = args[0] while new_num_arr.size < forcelength: new_num_arr = r_[0, new_num_arr] return new_num_arr
ARGS anum10 in number in base 10 aradix in convert <anum10> to number in base + <aradix> OPTIONAL forcelength in if nonzero, indicates the length + of the return array. Useful if + array needs to be zero padded. DESC Converts a scalar from base 10 to base radix. Return an array.
def bulk_copy(self, ids): """Bulk copy a set of packages. :param ids: Int list of package IDs. :return: :class:`packages.Package <packages.Package>` list """ schema = PackageSchema() return self.service.bulk_copy(self.base, self.RESOURCE, ids, schema)
Bulk copy a set of packages. :param ids: Int list of package IDs. :return: :class:`packages.Package <packages.Package>` list
def setup(self): """Setup.""" self.blocks = self.config['block_comments'] self.lines = self.config['line_comments'] self.group_comments = self.config['group_comments'] self.jsdocs = self.config['jsdocs'] self.decode_escapes = self.config['decode_escapes'] self.strings = self.config['strings'] self.prefix = 'js'
Setup.
def size_to_content(self, horizontal_padding=None, vertical_padding=None): """ Size the canvas item to the text content. """ if horizontal_padding is None: horizontal_padding = 0 if vertical_padding is None: vertical_padding = 0 self.sizing.set_fixed_size(Geometry.IntSize(18 + 2 * horizontal_padding, 18 + 2 * vertical_padding))
Size the canvas item to the text content.
def bulkdownload(database, **kwargs): """Downloads an entire database. :param str database: The database code to download :param str filename: The filename for the download. \ If not specified, will download to the current working directory :param str api_key: Most databases require api_key for bulk download :param str download_type: 'partial' or 'complete'. \ See: https://www.quandl.com/docs/api#database-metadata """ # discourage users from using authtoken if 'authtoken' in kwargs: raise InvalidRequestError(Message.ERROR_AUTHTOKEN_NOT_SUPPORTED) ApiKeyUtil.init_api_key_from_args(kwargs) filename = kwargs.pop('filename', '.') return Database(database).bulk_download_to_file(filename, params=kwargs)
Downloads an entire database. :param str database: The database code to download :param str filename: The filename for the download. \ If not specified, will download to the current working directory :param str api_key: Most databases require api_key for bulk download :param str download_type: 'partial' or 'complete'. \ See: https://www.quandl.com/docs/api#database-metadata
def sorted_exists(values, x): """ For list, values, returns the insert position for item x and whether the item already exists in the list. This allows one function call to return either the index to overwrite an existing value in the list, or the index to insert a new item in the list and keep the list in sorted order. :param values: list :param x: item :return: (exists, index) tuple """ i = bisect_left(values, x) j = bisect_right(values, x) exists = x in values[i:j] return exists, i
For list, values, returns the insert position for item x and whether the item already exists in the list. This allows one function call to return either the index to overwrite an existing value in the list, or the index to insert a new item in the list and keep the list in sorted order. :param values: list :param x: item :return: (exists, index) tuple
def css(self): """Returns ------- str The CSS. """ css_list = [DEFAULT_MARK_CSS] for aes in self.aesthetics: css_list.extend(get_mark_css(aes, self.values[aes])) #print('\n'.join(css_list)) return '\n'.join(css_list)
Returns ------- str The CSS.
def coroutine( func: Callable[..., "Generator[Any, Any, _T]"] ) -> Callable[..., "Future[_T]"]: """Decorator for asynchronous generators. For compatibility with older versions of Python, coroutines may also "return" by raising the special exception `Return(value) <Return>`. Functions with this decorator return a `.Future`. .. warning:: When exceptions occur inside a coroutine, the exception information will be stored in the `.Future` object. You must examine the result of the `.Future` object, or the exception may go unnoticed by your code. This means yielding the function if called from another coroutine, using something like `.IOLoop.run_sync` for top-level calls, or passing the `.Future` to `.IOLoop.add_future`. .. versionchanged:: 6.0 The ``callback`` argument was removed. Use the returned awaitable object instead. """ @functools.wraps(func) def wrapper(*args, **kwargs): # type: (*Any, **Any) -> Future[_T] # This function is type-annotated with a comment to work around # https://bitbucket.org/pypy/pypy/issues/2868/segfault-with-args-type-annotation-in future = _create_future() try: result = func(*args, **kwargs) except (Return, StopIteration) as e: result = _value_from_stopiteration(e) except Exception: future_set_exc_info(future, sys.exc_info()) try: return future finally: # Avoid circular references future = None # type: ignore else: if isinstance(result, Generator): # Inline the first iteration of Runner.run. This lets us # avoid the cost of creating a Runner when the coroutine # never actually yields, which in turn allows us to # use "optional" coroutines in critical path code without # performance penalty for the synchronous case. try: yielded = next(result) except (StopIteration, Return) as e: future_set_result_unless_cancelled( future, _value_from_stopiteration(e) ) except Exception: future_set_exc_info(future, sys.exc_info()) else: # Provide strong references to Runner objects as long # as their result future objects also have strong # references (typically from the parent coroutine's # Runner). This keeps the coroutine's Runner alive. # We do this by exploiting the public API # add_done_callback() instead of putting a private # attribute on the Future. # (Github issues #1769, #2229). runner = Runner(result, future, yielded) future.add_done_callback(lambda _: runner) yielded = None try: return future finally: # Subtle memory optimization: if next() raised an exception, # the future's exc_info contains a traceback which # includes this stack frame. This creates a cycle, # which will be collected at the next full GC but has # been shown to greatly increase memory usage of # benchmarks (relative to the refcount-based scheme # used in the absence of cycles). We can avoid the # cycle by clearing the local variable after we return it. future = None # type: ignore future_set_result_unless_cancelled(future, result) return future wrapper.__wrapped__ = func # type: ignore wrapper.__tornado_coroutine__ = True # type: ignore return wrapper
Decorator for asynchronous generators. For compatibility with older versions of Python, coroutines may also "return" by raising the special exception `Return(value) <Return>`. Functions with this decorator return a `.Future`. .. warning:: When exceptions occur inside a coroutine, the exception information will be stored in the `.Future` object. You must examine the result of the `.Future` object, or the exception may go unnoticed by your code. This means yielding the function if called from another coroutine, using something like `.IOLoop.run_sync` for top-level calls, or passing the `.Future` to `.IOLoop.add_future`. .. versionchanged:: 6.0 The ``callback`` argument was removed. Use the returned awaitable object instead.
def stop(self): """Stop this gateway agent.""" if self._disconnector: self._disconnector.stop() self.client.disconnect()
Stop this gateway agent.
def extract_metric_name(self, metric_name): """ Method to extract SAR metric names from the section given in the config. The SARMetric class assumes that the section name will contain the SAR types listed in self.supported_sar_types tuple :param str metric_name: Section name from the config :return: str which identifies what kind of SAR metric the section represents """ for metric_type in self.supported_sar_types: if metric_type in metric_name: return metric_type logger.error('Section [%s] does not contain a valid metric type, using type: "SAR-generic". Naarad works better ' 'if it knows the metric type. Valid SAR metric names are: %s', metric_name, self.supported_sar_types) return 'SAR-generic'
Method to extract SAR metric names from the section given in the config. The SARMetric class assumes that the section name will contain the SAR types listed in self.supported_sar_types tuple :param str metric_name: Section name from the config :return: str which identifies what kind of SAR metric the section represents
def dir(cls, label, children): """Return ``FSEntry`` directory object.""" return FSEntry(label=label, children=children, type=u"Directory", use=None)
Return ``FSEntry`` directory object.
def json(cls, message): """ Print a nice JSON output Args: message: the message to print """ if type(message) is OrderedDict: pprint(dict(message)) else: pprint(message)
Print a nice JSON output Args: message: the message to print
def change(script, layer_num=None): """ Change the current layer by specifying the new layer number. Args: script: the mlx.FilterScript object or script filename to write the filter to. layer_num (int): the number of the layer to change to. Default is the last layer if script is a mlx.FilterScript object; if script is a filename the default is the first layer. Layer stack: Modifies current layer MeshLab versions: 2016.12 1.3.4BETA """ if layer_num is None: if isinstance(script, mlx.FilterScript): layer_num = script.last_layer() else: layer_num = 0 filter_xml = ''.join([ ' <filter name="Change the current layer">\n', ' <Param name="mesh" ', 'value="{:d}" '.format(layer_num), 'description="Mesh" ', 'type="RichMesh" ', '/>\n', ' </filter>\n']) util.write_filter(script, filter_xml) if isinstance(script, mlx.FilterScript): script.set_current_layer(layer_num) #script.layer_stack[len(self.layer_stack) - 1] = layer_num return None
Change the current layer by specifying the new layer number. Args: script: the mlx.FilterScript object or script filename to write the filter to. layer_num (int): the number of the layer to change to. Default is the last layer if script is a mlx.FilterScript object; if script is a filename the default is the first layer. Layer stack: Modifies current layer MeshLab versions: 2016.12 1.3.4BETA
def factory(cls, registry): """Returns a dynamic MetricsHandler class tied to the passed registry. """ # This implementation relies on MetricsHandler.registry # (defined above and defaulted to REGISTRY). # As we have unicode_literals, we need to create a str() # object for type(). cls_name = str(cls.__name__) MyMetricsHandler = type(cls_name, (cls, object), {"registry": registry}) return MyMetricsHandler
Returns a dynamic MetricsHandler class tied to the passed registry.
def put(self, item, *args, **kwargs): """Put an item into the cache, for this combination of args and kwargs. Args: *args: any arguments. **kwargs: any keyword arguments. If ``timeout`` is specified as one of the keyword arguments, the item will remain available for retrieval for ``timeout`` seconds. If ``timeout`` is `None` or not specified, the ``default_timeout`` for this cache will be used. Specify a ``timeout`` of 0 (or ensure that the ``default_timeout`` for this cache is 0) if this item is not to be cached. """ if not self.enabled: return # Check for a timeout keyword, store and remove it. timeout = kwargs.pop('timeout', None) if timeout is None: timeout = self.default_timeout cache_key = self.make_key(args, kwargs) # Store the item, along with the time at which it will expire with self._cache_lock: self._cache[cache_key] = (time() + timeout, item)
Put an item into the cache, for this combination of args and kwargs. Args: *args: any arguments. **kwargs: any keyword arguments. If ``timeout`` is specified as one of the keyword arguments, the item will remain available for retrieval for ``timeout`` seconds. If ``timeout`` is `None` or not specified, the ``default_timeout`` for this cache will be used. Specify a ``timeout`` of 0 (or ensure that the ``default_timeout`` for this cache is 0) if this item is not to be cached.
def load_app(config, **kwargs): ''' Used to load a ``Pecan`` application and its environment based on passed configuration. :param config: Can be a dictionary containing configuration, a string which represents a (relative) configuration filename returns a pecan.Pecan object ''' from .configuration import _runtime_conf, set_config set_config(config, overwrite=True) for package_name in getattr(_runtime_conf.app, 'modules', []): module = __import__(package_name, fromlist=['app']) if hasattr(module, 'app') and hasattr(module.app, 'setup_app'): app = module.app.setup_app(_runtime_conf, **kwargs) app.config = _runtime_conf return app raise RuntimeError( 'No app.setup_app found in any of the configured app.modules' )
Used to load a ``Pecan`` application and its environment based on passed configuration. :param config: Can be a dictionary containing configuration, a string which represents a (relative) configuration filename returns a pecan.Pecan object
def insertVariantSet(self, variantSet): """ Inserts a the specified variantSet into this repository. """ # We cheat a little here with the VariantSetMetadata, and encode these # within the table as a JSON dump. These should really be stored in # their own table metadataJson = json.dumps( [protocol.toJsonDict(metadata) for metadata in variantSet.getMetadata()]) urlMapJson = json.dumps(variantSet.getReferenceToDataUrlIndexMap()) try: models.Variantset.create( id=variantSet.getId(), datasetid=variantSet.getParentContainer().getId(), referencesetid=variantSet.getReferenceSet().getId(), name=variantSet.getLocalId(), created=datetime.datetime.now(), updated=datetime.datetime.now(), metadata=metadataJson, dataurlindexmap=urlMapJson, attributes=json.dumps(variantSet.getAttributes())) except Exception as e: raise exceptions.RepoManagerException(e) for callSet in variantSet.getCallSets(): self.insertCallSet(callSet)
Inserts a the specified variantSet into this repository.