def default(self, obj):

        if isinstance(obj, datetime):
            return obj.isoformat()

        if issubclass(obj.__class__, Enum.__class__):
            return obj.value

        to_json = getattr(obj, 'to_json', None)
        if to_json:
            out = obj.to_json()
            if issubclass(obj.__class__, Model):
                out.update({'__type': obj.__class__.__name__})

            return out

        return JSONEncoder.default(self, obj)

Default object encoder function

Args:
        obj (:obj:`Any`): Object to be serialized

Returns:
        JSON string


def expand_path_cfg(path_cfg, alias_dict={ }, overriding_kargs={ }):

    if isinstance(path_cfg, str):
        return _expand_str(path_cfg, alias_dict, overriding_kargs)

    if isinstance(path_cfg, dict):
        return _expand_dict(path_cfg, alias_dict)

    # assume tuple or list
    return _expand_tuple(path_cfg, alias_dict, overriding_kargs)

expand a path config

Args:
        path_cfg (str, tuple, dict): a config for path
        alias_dict (dict): a dict for aliases
        overriding_kargs (dict): to be used for recursive call


def _generate_legacy_type_checks(types=()):

    types = dict(types)

    def gen_type_check(pytypes):
        pytypes = _utils.flatten(pytypes)

        def type_check(checker, instance):
            if isinstance(instance, bool):
                if bool not in pytypes:
                    return False
            return isinstance(instance, pytypes)

        return type_check

    definitions = {}
    for typename, pytypes in iteritems(types):
        definitions[typename] = gen_type_check(pytypes)

    return definitions

Generate newer-style type checks out of JSON-type-name-to-type mappings.

Args:
        types (dict):

            A mapping of type names to their Python types

Returns:
        A dictionary of definitions to pass to `TypeChecker`


def create(self, value):

        # Explicitly check with len such that non-sequence types throw.
        if self._optional and (value is None or len(value) == 0):
            return None

        if hasattr(self._type, 'resource_type'):
            # Our type is a resource, so ensure we have a dict of title to
            # parameters
            if not isinstance(value, dict):
                raise ValueError("Resources must be specified as a dict of "
                                 "title to parameters")
            if not self._many and len(value) > 1:
                raise ValueError("Only one resource can be provided for this "
                                 "TroposphereType variable")

            result = [
                self._type.from_dict(title, v) for title, v in value.items()
            ]
        else:
            # Our type is for properties, not a resource, so don't use
            # titles
            if self._many:
                result = [self._type.from_dict(None, v) for v in value]
            elif not isinstance(value, dict):
                raise ValueError("TroposphereType for a single non-resource"
                                 "type must be specified as a dict of "
                                 "parameters")
            else:
                result = [self._type.from_dict(None, value)]

        if self._validate:
            for v in result:
                v._validate_props()

        return result[0] if not self._many else result

Create the troposphere type from the value.

Args:
        value (Union[dict, list]): A dictionary or list of dictionaries
            (see class documentation for details) to use as parameters to
            create the Troposphere type instance.
            Each dictionary will be passed to the `from_dict` method of the
            type.

Returns:
        Union[list, type]: Returns the value converted to the troposphere
            type


def write_image(self, stream, image_format="svg", **kwargs):

        plt = self.get_plot(**kwargs)

        f = plt.gcf()
        f.set_size_inches((12, 10))

        plt.savefig(stream, format=image_format)

Writes the phase diagram to an image in a stream.

Args:
        stream:
            stream to write to. Can be a file stream or a StringIO stream.
        image_format
            format for image. Can be any of matplotlib supported formats.
            Defaults to svg for best results for vector graphics.
        \\*\\*kwargs: Pass through to get_plot functino.


def has_types(self, types, all_=True):

        func = all if all_ else any
        return func([self.get_stim(t) for t in listify(types)])

Check whether the current component list matches all Stim types
    in the types argument.

Args:
        types (Stim, list): a Stim class or iterable of Stim classes.
        all_ (bool): if True, all input types must match; if False, at
            least one input type must match.

Returns:
        True if all passed types match at least one Stim in the component
        list, otherwise False.


def macro_state(self, micro_state):

        assert len(micro_state) == len(self.micro_indices)

        reindexed = self.reindex()
        return utils.state_of(reindexed.output_indices, micro_state)

Compute the macro-state of this blackbox.

    This is just the state of the blackbox's output indices.

Args:
        micro_state (tuple[int]): The state of the micro-elements in the
            blackbox.

Returns:
        tuple[int]: The state of the output indices.


def _parse_parameters(val_type, val):

    if val_type == "logical":
        return val == "T"
    elif val_type == "int":
        return int(val)
    elif val_type == "string":
        return val.strip()
    else:
        return float(val)

Helper function to convert a Vasprun parameter into the proper type.
    Boolean, int and float types are converted.

Args:
        val_type: Value type parsed from vasprun.xml.
        val: Actual string value parsed for vasprun.xml.


def __init__(self, *compressed_files, **kwargs):

        self._files = []
        self._prefixes = defaultdict(lambda: set(['']))
        self._extract = kwargs.get('extract', False)
        self._supersede = kwargs.get('supersede', False)
        self._match_version = kwargs.get('_match_version', True)
        self._local_warned = False

        for f in compressed_files:
            if isinstance(f, zipfile.ZipFile):
                bin_package = any(n.endswith('.so') or n.endswith('.pxd') or n.endswith('.dylib')
                                  for n in f.namelist())
                need_extract = True
            elif isinstance(f, tarfile.TarFile):
                bin_package = any(m.name.endswith('.so') or m.name.endswith('.pxd') or m.name.endswith('.dylib')
                                  for m in f.getmembers())
                need_extract = True
            elif isinstance(f, dict):
                bin_package = any(name.endswith('.so') or name.endswith('.pxd') or name.endswith('.dylib')
                                  for name in iterkeys(f))
                need_extract = False
            elif isinstance(f, list):
                bin_package = any(name.endswith('.so') or name.endswith('.pxd') or name.endswith('.dylib')
                                  for name in f)
                need_extract = False
            else:
                raise TypeError('Compressed file can only be zipfile.ZipFile or tarfile.TarFile')

            if bin_package:
                if not ALLOW_BINARY:
                    raise SystemError('Cannot load binary package. It is quite possible that you are using an old '
                                      'MaxCompute service which does not support binary packages. If this is '
                                      'not true, please set `odps.isolation.session.enable` to True or ask your '
                                      'project owner to change project-level configuration.')
                if need_extract:
                    f = self._extract_archive(f)

            prefixes = set([''])
            dir_prefixes = set()
            if isinstance(f, zipfile.ZipFile):
                for name in f.namelist():
                    name = name if name.endswith('/') else (name.rsplit('/', 1)[0] + '/')
                    if name in prefixes:
                        continue
                    try:
                        f.getinfo(name + '__init__.py')
                    except KeyError:
                        prefixes.add(name)
            elif isinstance(f, tarfile.TarFile):
                for member in f.getmembers():
                    name = member.name if member.isdir() else member.name.rsplit('/', 1)[0]
                    if name in prefixes:
                        continue
                    try:
                        f.getmember(name + '/__init__.py')
                    except KeyError:
                        prefixes.add(name + '/')
            elif isinstance(f, (list, dict)):
                # Force ArchiveResource to run under binary mode to resolve manually
                # opening __file__ paths in pure-python code.
                if ALLOW_BINARY:
                    bin_package = True

                rendered_names = set()
                for name in f:
                    name = name.replace(os.sep, '/')
                    rendered_names.add(name)

                for name in rendered_names:
                    name = name if name.endswith('/') else (name.rsplit('/', 1)[0] + '/')
                    if name in prefixes or '/tests/' in name:
                        continue
                    if name + '__init__.py' not in rendered_names:
                        prefixes.add(name)
                        dir_prefixes.add(name)
                    else:
                        if '/' in name.rstrip('/'):
                            ppath = name.rstrip('/').rsplit('/', 1)[0]
                        else:
                            ppath = ''
                        prefixes.add(ppath)
                        dir_prefixes.add(ppath)

            if bin_package:
                path_patch = []
                for p in sorted(dir_prefixes):
                    if p in sys.path:
                        continue
                    parent_exist = False
                    for pp in path_patch:
                        if p[:len(pp)] == pp:
                            parent_exist = True
                            break
                    if parent_exist:
                        continue
                    path_patch.append(p)
                if self._supersede:
                    sys.path = path_patch + sys.path
                else:
                    sys.path = sys.path + path_patch
            else:
                self._files.append(f)
                if prefixes:
                    self._prefixes[id(f)] = sorted(prefixes)

Constructor.

Args:
        compressed_files zipfile.ZipFile or tarfile.TarFile


def create_pipeline(url, auth, json_payload, verify_ssl):

    title = json_payload['pipelineConfig']['title']
    description = json_payload['pipelineConfig']['description']
    params = {'description':description, 'autoGeneratePipelineId':True}
    logging.info('No destination pipeline ID provided.  Creating a new pipeline: ' + title)
    put_result = requests.put(url + '/' + title, params=params, headers=X_REQ_BY, auth=auth, verify=verify_ssl)
    put_result.raise_for_status()
    create_json = put_result.json()
    logging.debug(create_json)
    logging.info('Pipeline creation successful.')
    return create_json

Create a new pipeline.

Args:
        url           (str): the host url in the form 'http://host:port/'.
        auth        (tuple): a tuple of username, and password.
        json_payload (dict): the exported json paylod as a dictionary.
        verify_ssl   (bool): whether to verify ssl certificates

Returns:
        dict: the response json


def sibling(self, name: InstanceName) -> "ObjectMember":

        ssn = self.parinst._member_schema_node(name)
        try:
            sibs = self.siblings.copy()
            newval = sibs.pop(name)
            sibs[self.name] = self.value
            return ObjectMember(name, sibs, newval, self.parinst,
                                ssn, self.timestamp)
        except KeyError:
            raise NonexistentInstance(self.json_pointer(),
                                      f"member '{name}'") from None

Return an instance node corresponding to a sibling member.

Args:
        name: Instance name of the sibling member.

Raises:
        NonexistentSchemaNode: If member `name` is not permitted by the
            schema.
        NonexistentInstance: If sibling member `name` doesn't exist.


def get_package_hashes(filename):

    log.debug('Getting package hashes')
    filename = os.path.abspath(filename)
    with open(filename, 'rb') as f:
        data = f.read()

    _hash = hashlib.sha256(data).hexdigest()
    log.debug('Hash for file %s: %s', filename, _hash)
    return _hash

Provides hash of given filename.

Args:
        filename (str): Name of file to hash

Returns:
        (str): sha256 hash


def eigenvalues(df):

    corr = np.corrcoef(df, rowvar=0)
    eigvals = np.linalg.eigvals(corr)
    return pd.Series(eigvals, df.columns, name='Eigenvalue')

Returns a pandas Series with eigenvalues of the correlation matrix.

Args:
        df: pandas DataFrame with columns to run diagnostics on


def __init__(self, instrumentation_key, *args, **kwargs):

        if not instrumentation_key:
            raise Exception('Instrumentation key was required but not provided')
        telemetry_channel = kwargs.get('telemetry_channel')
        if 'telemetry_channel' in kwargs:
            del kwargs['telemetry_channel']
        self.client = applicationinsights.TelemetryClient(instrumentation_key, telemetry_channel)
        super(LoggingHandler, self).__init__(*args, **kwargs)

Initialize a new instance of the class.

Args:
        instrumentation_key (str). the instrumentation key to use while sending telemetry to the service.


def dump_logs(json_data, project_mapping, tag_name):

    pwd_dir_path, dump_file_name = _prepare_dump_info(project_mapping, tag_name)
    dump_json_file(json_data, pwd_dir_path, dump_file_name)

dump tests data to json file.
    the dumped file is located in PWD/logs folder.

Args:
    json_data (list/dict): json data to dump
    project_mapping (dict): project info
    tag_name (str): tag name, loaded/parsed/summary


def add_case(self, case_obj, vtype='snv', mode='vcf', ped_svg=None):

        new_case = Case(case_id=case_obj.case_id,
                        name=case_obj.name,
                        variant_source=case_obj.variant_source,
                        variant_type=vtype,
                        variant_mode=mode,
                        pedigree=ped_svg,
                        compressed=case_obj.compressed,
                        tabix_index=case_obj.tabix_index)

        # build individuals
        inds = [Individual(
            ind_id=ind.ind_id,
            name=ind.name,
            mother=ind.mother,
            father=ind.father,
            sex=ind.sex,
            phenotype=ind.phenotype,
            ind_index=ind.ind_index,
            variant_source=ind.variant_source,
            bam_path=ind.bam_path,
        ) for ind in case_obj.individuals]
        new_case.individuals = inds

        if self.case(new_case.case_id):
            logger.warning("Case already exists in database!")
        else:
            self.session.add(new_case)
            self.save()
        return new_case

Load a case with individuals.

Args:
        case_obj (puzzle.models.Case): initialized case model


def __register_class(self, parsed_config):

    methods = parsed_config.get('methods')
    if not methods:
      return

    # Determine the name of the class that implements this configuration.
    service_classes = set()
    for method in methods.itervalues():
      rosy_method = method.get('rosyMethod')
      if rosy_method and '.' in rosy_method:
        method_class = rosy_method.split('.', 1)[0]
        service_classes.add(method_class)

    for service_class in service_classes:
      if service_class in self.__registered_classes:
        raise api_exceptions.ApiConfigurationError(
            'API class %s has already been registered.' % service_class)
      self.__registered_classes.add(service_class)

Register the class implementing this config, so we only add it once.

Args:
        parsed_config: The JSON object with the API configuration being added.

Raises:
        ApiConfigurationError: If the class has already been registered.


def get_artifact_filename(self, package_name, artifact_name):

        project_name = self.packages.normalize(package_name)
        return self.records.get((project_name, artifact_name))

Similar to pkg_resources.resource_filename, however this works
    with the information cached in this registry instance, and
    arguments are not quite the same.

Args:
    package_name
        The name of the package to get the artifact from
    artifact_name
        The exact name of the artifact.

    Returns the path of where the artifact should be if it has been
    declared, otherwise None.


def _build_command(self, python_executable, lib_dir_fq, proxy_enabled):

        exe_command = [
            os.path.expanduser(python_executable),
            '-m',
            'pip',
            'install',
            '-r',
            self.requirements_file,
            '--ignore-installed',
            '--quiet',
            '--target',
            lib_dir_fq,
        ]
        if self.args.no_cache_dir:
            exe_command.append('--no-cache-dir')

        if proxy_enabled:
            # trust the pypi hosts to avoid ssl errors
            trusted_hosts = ['pypi.org', 'pypi.python.org', 'files.pythonhosted.org']

            for host in trusted_hosts:
                exe_command.append('--trusted-host')
                exe_command.append(host)

        return exe_command

Build the pip command for installing dependencies.

Args:
        python_executable (str): The fully qualified path of the Python executable.
        lib_dir_fq (str): The fully qualified path of the lib directory.

Returns:
        list: The Python pip command with all required args.


def rh45(msg):

    d = hex2bin(data(msg))
    if d[38] == '0':
        return None
    rh = bin2int(d[39:51]) * 16
    return rh

Radio height.

Args:
        msg (String): 28 bytes hexadecimal message string

Returns:
        int: radio height in ft


def _count_tokens(files, file_byte_limit=1e6):

  token_counts = collections.defaultdict(int)

  for filepath in files:
    with tf.gfile.Open(filepath, mode="r") as reader:
      file_byte_budget = file_byte_limit
      counter = 0
      lines_to_skip = int(reader.size() / (file_byte_budget * 2))
      for line in reader:
        if counter < lines_to_skip:
          counter += 1
        else:
          if file_byte_budget < 0:
            break
          line = line.strip()
          file_byte_budget -= len(line)
          counter = 0

          # Add words to token counts
          for token in _split_string_to_tokens(_native_to_unicode(line)):
            token_counts[token] += 1
  return token_counts

Return token counts of words in the files.

    Samples file_byte_limit bytes from each file, and counts the words that appear
    in the samples. The samples are semi-evenly distributed across the file.

Args:
        files: List of filepaths
        file_byte_limit: Max number of bytes that will be read from each file.

Returns:
        Dictionary mapping tokens to the number of times they appear in the sampled
        lines from the files.


def default_datastore_policy(key):

    flag = None
    if key is not None:
      modelclass = model.Model._kind_map.get(key.kind())
      if modelclass is not None:
        policy = getattr(modelclass, '_use_datastore', None)
        if policy is not None:
          if isinstance(policy, bool):
            flag = policy
          else:
            flag = policy(key)
    return flag

Default datastore policy.

    This defers to _use_datastore on the Model class.

Args:
        key: Key instance.

Returns:
        A bool or None.


def _ConvertListToObject(cls, json_list):

    list_value = []
    for json_list_element in json_list:
      if isinstance(json_list_element, dict):
        list_value.append(cls._ConvertDictToObject(json_list_element))

      elif isinstance(json_list_element, list):
        list_value.append(cls._ConvertListToObject(json_list_element))

      else:
        list_value.append(json_list_element)

    return list_value

Converts a JSON list into an object.

Args:
        json_list (list[object]): JSON serialized objects.

Returns:
        list[object]: a deserialized list.


def wait_until_page_ready(page_object, timeout=WTF_TIMEOUT_MANAGER.NORMAL):

        try:
            do_until(lambda: page_object.webdriver.execute_script("return document.readyState").lower()
                     == 'complete', timeout)
        except wait_utils.OperationTimeoutError:
            raise PageUtilOperationTimeoutError(
                "Timeout occurred while waiting for page to be ready.")

Waits until document.readyState == Complete (e.g. ready to execute javascript commands)

Args:
        page_object (PageObject) : PageObject class

    Kwargs:
        timeout (number) : timeout period


def tail(self, n):

        # See head for an explanation of the transposed behavior
        if n < 0:
            n = max(0, len(self.index) + n)
        if self._is_transposed:
            result = self.__constructor__(
                self.data.transpose().take(1, -n).transpose(),
                self.index[-n:],
                self.columns,
                self._dtype_cache,
            )
            result._is_transposed = True
        else:
            result = self.__constructor__(
                self.data.take(0, -n), self.index[-n:], self.columns, self._dtype_cache
            )
        return result

Returns the last n rows.

Args:
        n: Integer containing the number of rows to return.

Returns:
        DataManager containing the last n rows of the original DataManager.


def parse_individuals(samples):

    individuals = []
    if len(samples) == 0:
        raise PedigreeError("No samples could be found")

    ind_ids = set()
    for sample_info in samples:
        parsed_ind = parse_individual(sample_info)
        individuals.append(parsed_ind)
        ind_ids.add(parsed_ind['individual_id'])

    # Check if relations are correct
    for parsed_ind in individuals:
        father = parsed_ind['father']
        if (father and father != '0'):
            if father not in ind_ids:
                raise PedigreeError('father %s does not exist in family' % father)
        mother = parsed_ind['mother']
        if (mother and mother != '0'):
            if mother not in ind_ids:
                raise PedigreeError('mother %s does not exist in family' % mother)

    return individuals

Parse the individual information

    Reformat sample information to proper individuals

Args:
        samples(list(dict))

Returns:
        individuals(list(dict))


def is_element_in_database(element='', database='ENDF_VII'):

    if element == '':
        return False

    list_entry_from_database = get_list_element_from_database(database=database)
    if element in list_entry_from_database:
        return True
    return False

will try to find the element in the folder (database) specified

    Parameters:
    ==========
    element: string. Name of the element. Not case sensitive
    database: string (default is 'ENDF_VII'). Name of folder that has the list of elements

Returns:
    =======
    bool: True if element was found in the database
        False if element could not be found


def draw_mask(im, mask, alpha=0.5, color=None):

    if color is None:
        color = PALETTE_RGB[np.random.choice(len(PALETTE_RGB))][::-1]
    im = np.where(np.repeat((mask > 0)[:, :, None], 3, axis=2),
                  im * (1 - alpha) + color * alpha, im)
    im = im.astype('uint8')
    return im

Overlay a mask on top of the image.

Args:
        im: a 3-channel uint8 image in BGR
        mask: a binary 1-channel image of the same size
        color: if None, will choose automatically


def heightmap_clamp(hm: np.ndarray, mi: float, ma: float) -> None:

    hm.clip(mi, ma)

Clamp all values on this heightmap between ``mi`` and ``ma``

Args:
        hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions.
        mi (float): The lower bound to clamp to.
        ma (float): The upper bound to clamp to.

    .. deprecated:: 2.0
        Do ``hm.clip(mi, ma)`` instead.


def propagate(self, date):

        if self.propagator.orbit is not self:
            self.propagator.orbit = self

        return self.propagator.propagate(date)

Propagate the orbit to a new date

Args:
        date (Date)

Returns:
        Orbit


def _head(self, client_kwargs):

        with _handle_client_exception():
            # Object
            if 'obj' in client_kwargs:
                return self.client.head_object(**client_kwargs)

            # Container
            return self.client.head_container(**client_kwargs)

Returns object HTTP header.

Args:
        client_kwargs (dict): Client arguments.

Returns:
        dict: HTTP header.


def delete_handler(Model, name=None, **kwds):

    # necessary imports
    from nautilus.database import db

    async def action_handler(service, action_type, payload, props, notify=True, **kwds):
        # if the payload represents a new instance of `model`
        if action_type == get_crud_action('delete', name or Model):
            try:
                # the props of the message
                message_props = {}
                # if there was a correlation id in the request
                if 'correlation_id' in props:
                    # make sure it ends up in the reply
                    message_props['correlation_id'] = props['correlation_id']
                # the id in the payload representing the record to delete
                record_id = payload['id'] if 'id' in payload else payload['pk']
                # get the model matching the payload
                try:
                    model_query = Model.select().where(Model.primary_key() == record_id)
                except KeyError:
                    raise RuntimeError("Could not find appropriate id to remove service record.")
                # remove the model instance
                model_query.get().delete_instance()
                # if we need to tell someone about what happened
                if notify:
                    # publish the success event
                    await service.event_broker.send(
                        payload='{"status":"ok"}',
                        action_type=change_action_status(action_type, success_status()),
                        **message_props
                    )

            # if something goes wrong
            except Exception as err:
                # if we need to tell someone about what happened
                if notify:
                    # publish the error as an event
                    await service.event_broker.send(
                        payload=str(err),
                        action_type=change_action_status(action_type, error_status()),
                        **message_props
                    )
                # otherwise we aren't supposed to notify
                else:
                    # raise the exception normally
                    raise err

    # return the handler
    return action_handler

This factory returns an action handler that deletes a new instance of
    the specified model when a delete action is recieved, assuming the
    action follows nautilus convetions.

Args:
        Model (nautilus.BaseModel): The model to delete when the action
            received.

Returns:
        function(type, payload): The action handler for this model


def download_listing(self, file: Optional[IO],
                         duration_timeout: Optional[float]=None) -> \
            ListingResponse:

        if self._session_state != SessionState.directory_request_sent:
            raise RuntimeError('File request not sent')

        self._session_state = SessionState.file_request_sent

        yield from self.download(file=file, rewind=False,
                                 duration_timeout=duration_timeout)

        try:
            if self._response.body.tell() == 0:
                listings = ()
            elif self._listing_type == 'mlsd':
                self._response.body.seek(0)

                machine_listings = wpull.protocol.ftp.util.parse_machine_listing(
                    self._response.body.read().decode('utf-8',
                                                      errors='surrogateescape'),
                    convert=True, strict=False
                )
                listings = list(
                    wpull.protocol.ftp.util.machine_listings_to_file_entries(
                        machine_listings
                    ))
            else:
                self._response.body.seek(0)

                file = io.TextIOWrapper(self._response.body, encoding='utf-8',
                                        errors='surrogateescape')

                listing_parser = ListingParser(file=file)

                listings = list(listing_parser.parse_input())

                _logger.debug('Listing detected as %s', listing_parser.type)

                # We don't want the file to be closed when exiting this function
                file.detach()

        except (ListingError, ValueError) as error:
            raise ProtocolError(*error.args) from error

        self._response.files = listings

        self._response.body.seek(0)

        self._session_state = SessionState.response_received

        return self._response

Read file listings.

Args:
        file: A file object or asyncio stream.
        duration_timeout: Maximum time in seconds of which the
            entire file must be read.

Returns:
        A Response populated the file listings

    Be sure to call :meth:`start_file_listing` first.

    Coroutine.


def get_attr_value(self, attr_key, el_idx=0):

        return self.get_element_by_attr_key(attr_key, el_idx).attrib[attr_key]

Return the value of the selected attribute in the selected element.

Args:
        attr_key : str
        Name of attribute for which to search

        el_idx : int
        Index of element to use in the event that there are multiple sibling
        elements with the same name.

Returns:
        str : Value of the selected attribute in the selected element.


def getpexts(lsp):

    lines=lsp.split('\n');
    #unfortunately regex doesn't work here
    lns,planens = zip(
        *[ (i,int(re.search('^ *extract *([0-9]+)',line).group(1)))
           for i,line in enumerate(lines)
           if re.search('^ *extract *[0-9]+', line)]);
    if len(lns) == 0: return [];
    end = lns[-1];

    for i,line in enumerate(lines[end+1:]):
        if re.match(' *\[',line): break;
    end += i;
    lineranges = zip(lns,(lns+(end,))[1:]);
    planes=dict()
    for (i,end),plane in zip(lineranges,planens):
        d=dict();
        labels = [
            'species',
            'direction',
            'position',];
        datarx = [
            '^ *species *([0-9]+)',
            '^ *direction *([xXyYzZ])',
            '^ *at *(.*)',];
        convs  = [
            lambda s: int(s),
            lambda i: i,
            lambda s: np.array(
                map(float,s.split(' '))),
        ];
        for line in lines[i:end]:
            for label,rx,conv in zip(labels,datarx,convs):
                if re.match(rx,line):
                    d[label]=conv(re.match(rx,line).group(1));
                pass
            pass
        planes[plane] = d;
    return planes;

Get information from pext planes. This might or might not work, use with
    caution!

    Parameters:
    -----------

        lsp       : .lsp string

    Returns a list of dicts with information for all pext planes


def _create_metric_extractor(metric_name):

  def extractor_fn(session_or_group):
    metric_value = _find_metric_value(session_or_group,
                                      metric_name)
    return metric_value.value if metric_value else None

  return extractor_fn

Returns function that extracts a metric from a session group or a session.

Args:
        metric_name: tensorboard.hparams.MetricName protobuffer. Identifies the
        metric to extract from the session group.

Returns:
        A function that takes a tensorboard.hparams.SessionGroup or
        tensorborad.hparams.Session protobuffer and returns the value of the metric
        identified by 'metric_name' or None if the value doesn't exist.


def __init__(self, while_definition):

        logger.debug("starting")

        if isinstance(while_definition, dict):
            # errorOnMax: optional. defaults False
            self.error_on_max = while_definition.get('errorOnMax', False)

            # max: optional. defaults None.
            self.max = while_definition.get('max', None)

            # sleep: optional. defaults 0.
            self.sleep = while_definition.get('sleep', 0)

            # stop: optional. defaults None.
            self.stop = while_definition.get('stop', None)

            if self.stop is None and self.max is None:
                logger.error(f"while decorator missing both max and stop.")
                raise PipelineDefinitionError("the while decorator must have "
                                              "either max or stop, or both. "
                                              "But not neither. Note that "
                                              "setting stop: False with no "
                                              "max is an infinite loop. If "
                                              "an infinite loop is really "
                                              "what you want, set stop: False")
        else:
            # if it isn't a dict, pipeline configuration is wrong.
            logger.error(f"while decorator definition incorrect.")
            raise PipelineDefinitionError("while decorator must be a dict "
                                          "(i.e a map) type.")

        logger.debug("done")

Initialize the class. No duh, huh.

    You can happily expect the initializer to initialize all
    member attributes.

Args:
        while_definition: dict. This is the actual while definition as it
                        exists in the pipeline yaml.


def rename(blocks, scope, stype):

    for p in blocks:
        if isinstance(p, stype):
            p.tokens[0].parse(scope)
            if p.tokens[1]:
                scope.push()
                scope.current = p.tokens[0]
                rename(p.tokens[1], scope, stype)
                scope.pop()

Rename all sub-blocks moved under another
    block. (mixins)

Args:
    lst (list): block list
    scope (object): Scope object


def min(cls, x: 'TensorFluent', y: 'TensorFluent') -> 'TensorFluent':

        return cls._binary_op(x, y, tf.minimum, tf.float32)

Returns a TensorFluent for the minimum function.

Args:
        x: The first operand.
        y: The second operand.

Returns:
        A TensorFluent wrapping the minimum function.


def convert_reduce_sum(params, w_name, scope_name, inputs, layers, weights, names):

    print('Converting reduce_sum ...')

    keepdims = params['keepdims'] > 0
    axis = params['axes']

    def target_layer(x, keepdims=keepdims, axis=axis):
        import keras.backend as K
        return K.sum(x, keepdims=keepdims, axis=axis)

    lambda_layer = keras.layers.Lambda(target_layer)
    layers[scope_name] = lambda_layer(layers[inputs[0]])

Convert reduce_sum layer.

Args:
        params: dictionary with layer parameters
        w_name: name prefix in state_dict
        scope_name: pytorch scope name
        inputs: pytorch node inputs
        layers: dictionary with keras tensors
        weights: pytorch state_dict
        names: use short names for keras layers


def pad(self, file, size=6):

        buffer = ''
        for element in self.splitter.split(file):
            if _payload_size_within_limit(buffer + element, size):
                buffer += element
            else:
                tmp = buffer
                buffer = element
                yield tmp
        if _validate_payload_size(buffer, size):
            yield buffer

Group together as many records as possible to fit in the specified size

Args:
        file (str): file path to read the records from.
        size (int): maximum size in MB that each group of records will be fitted to.
            passing 0 means unlimited size.

Returns:
        generator of records


def sample_with_temperature(x, dim, temperature=1.0, dtype=tf.int32, name=None):

  dim = convert_to_dimension(dim)
  with tf.name_scope(name, default_name="sample_with_temperature"):
    if temperature != 0.0:
      # gumbel trick.
      # Note: we don't want to generate 0 or 1 because:
      # * -log(-log(0)) is -infinity
      # * -log(-log(1)) is +infinity.
      # np.finfo(x.dtype.as_numpy_dtype).tiny doesn't work on bfloat16
      tiny_val = 1e-9
      g = -log(-log(
          random_uniform(
              x.mesh,
              x.shape,
              minval=tiny_val,
              maxval=1.,
              dtype=x.dtype)))
      x += g * temperature
    return argmax(x, dim, dtype, name)

Either argmax or random sampling.

Args:
        x: a Tensor.
        dim: a Dimension in x.shape.dims
        temperature: a float  0.0=argmax 1.0=random
        dtype: a tf.dtype (for the output)
        name: an optional string

Returns:
        a Tensor with type dtype.


def tag_versions(repo_path):

    repo = dulwich.repo.Repo(repo_path)
    tags = get_tags(repo)
    maj_version = 0
    feat_version = 0
    fix_version = 0
    last_maj_version = 0
    last_feat_version = 0
    result = []

    for commit_sha, children in reversed(
            get_children_per_first_parent(repo_path).items()
    ):
        commit = get_repo_object(repo, commit_sha)
        maj_version, feat_version, fix_version = get_version(
            commit=commit,
            tags=tags,
            maj_version=maj_version,
            feat_version=feat_version,
            fix_version=fix_version,
            children=children,
        )
        if (
            last_maj_version != maj_version or
            last_feat_version != feat_version
        ):
            last_maj_version = maj_version
            last_feat_version = feat_version
            tag_name = 'refs/tags/v%d.%d' % (maj_version, feat_version)
            if ON_PYTHON3:
                repo[str.encode(tag_name)] = commit
            else:
                repo[tag_name] = commit

            result.append(
                'v%d.%d -> %s' % (maj_version, feat_version, commit_sha)
            )

    return '\n'.join(result)

Given a repo will add a tag for each major version.

Args:
        repo_path(str): path to the git repository to tag.


def smart_open(filepath):

    if filepath[-2:] == "gz":
        mode = "rt"
        fopen = gzip.open
    else:
        mode = "r"
        fopen = open
    if sys.version_info[0] < 3:
        fh = fopen(filepath, mode)
    else:
        fh = fopen(filepath, mode, errors="ignore")
    try:
        yield fh
    except IOError:
        fh.close()
    finally:
        fh.close()

Open file intelligently depending on the source and python version.

Args:
        filepath (:obj:`str`): Path to the file.

Yields:
        Context manager for file handle.


def overlay_boxes(self, image, predictions):

        labels = predictions.get_field("labels")
        boxes = predictions.bbox

        colors = self.compute_colors_for_labels(labels).tolist()

        for box, color in zip(boxes, colors):
            box = box.to(torch.int64)
            top_left, bottom_right = box[:2].tolist(), box[2:].tolist()
            image = cv2.rectangle(
                image, tuple(top_left), tuple(bottom_right), tuple(color), 1
            )

        return image

Adds the predicted boxes on top of the image

Args:
        image (np.ndarray): an image as returned by OpenCV
        predictions (BoxList): the result of the computation by the model.
            It should contain the field `labels`.


def save(self, path):

        with open(path, 'w') as f:
            f.write(self.contents())

Save svg as file(.svg)

Args:
        path (str): destination to save file


def enforce_epsilon_and_compute_hash(dataset_batch_dir, adv_dir, output_dir,
                                     epsilon):

  dataset_images = [f for f in os.listdir(dataset_batch_dir)
                    if f.endswith('.png')]
  image_hashes = {}
  resize_warning = False
  for img_name in dataset_images:
    if not os.path.exists(os.path.join(adv_dir, img_name)):
      logging.warning('Image %s not found in the output', img_name)
      continue
    image = np.array(
        Image.open(os.path.join(dataset_batch_dir, img_name)).convert('RGB'))
    image = image.astype('int32')
    image_max_clip = np.clip(image + epsilon, 0, 255).astype('uint8')
    image_min_clip = np.clip(image - epsilon, 0, 255).astype('uint8')
    # load and resize adversarial image if needed
    adv_image = Image.open(os.path.join(adv_dir, img_name)).convert('RGB')
    # Image.size is reversed compared to np.array.shape
    if adv_image.size[::-1] != image.shape[:2]:
      resize_warning = True
      adv_image = adv_image.resize((image.shape[1], image.shape[0]),
                                   Image.BICUBIC)
    adv_image = np.array(adv_image)
    clipped_adv_image = np.clip(adv_image,
                                image_min_clip,
                                image_max_clip)
    Image.fromarray(clipped_adv_image).save(os.path.join(output_dir, img_name))
    # compute hash
    image_hashes[img_name[:-4]] = hashlib.sha1(
        clipped_adv_image.view(np.uint8)).hexdigest()
  if resize_warning:
    logging.warning('One or more adversarial images had incorrect size')
  return image_hashes

Enforces size of perturbation on images, and compute hashes for all images.

Args:
        dataset_batch_dir: directory with the images of specific dataset batch
        adv_dir: directory with generated adversarial images
        output_dir: directory where to copy result
        epsilon: size of perturbation

Returns:
        dictionary with mapping form image ID to hash.


def _ParseEntryObject(self, file_object, file_offset):

    entry_object_map = self._GetDataTypeMap('systemd_journal_entry_object')

    try:
      entry_object, _ = self._ReadStructureFromFileObject(
          file_object, file_offset, entry_object_map)
    except (ValueError, errors.ParseError) as exception:
      raise errors.ParseError((
          'Unable to parse entry object at offset: 0x{0:08x} with error: '
          '{1!s}').format(file_offset, exception))

    if entry_object.object_type != self._OBJECT_TYPE_ENTRY:
      raise errors.ParseError('Unsupported object type: {0:d}.'.format(
          entry_object.object_type))

    if entry_object.object_flags != 0:
      raise errors.ParseError('Unsupported object flags: 0x{0:02x}.'.format(
          entry_object.object_flags))

    return entry_object

Parses an entry object.

Args:
        file_object (dfvfs.FileIO): a file-like object.
        file_offset (int): offset of the entry object relative to the start
        of the file-like object.

Returns:
        systemd_journal_entry_object: entry object.

Raises:
        ParseError: if the entry object cannot be parsed.


def register_domain(self, domain=0, tokenizer=None, trie=None):

        self.domains[domain] = IntentDeterminationEngine(
            tokenizer=tokenizer, trie=trie)

Register a domain with the intent engine.

Args:
        tokenizer(tokenizer): The tokenizer you wish to use.
        trie(Trie): the Trie() you wish to use.
        domain(str): a string representing the domain you wish to add


def _get_fbeta_score(true_positives, selected, relevant, beta=1):

  precision = 1
  if selected > 0:
    precision = true_positives / selected
  if beta == 0:
    return precision
  recall = 1
  if relevant > 0:
    recall = true_positives / relevant
  if precision > 0 and recall > 0:
    beta2 = beta * beta
    return (1 + beta2) * precision * recall / (beta2 * precision + recall)
  else:
    return 0

Compute Fbeta score.

Args:
        true_positives: Number of true positive ngrams.
        selected: Number of selected ngrams.
        relevant: Number of relevant ngrams.
        beta: 0 gives precision only, 1 gives F1 score, and Inf gives recall only.

Returns:
        Fbeta score.


def merge_sketches(outdir, sketch_paths):

    merge_sketch_path = os.path.join(outdir, 'sistr.msh')
    args = ['mash', 'paste', merge_sketch_path]
    for x in sketch_paths:
        args.append(x)
    args.append(MASH_SKETCH_FILE)
    logging.info('Running Mash paste with command: %s', ' '.join(args))
    p = Popen(args)
    p.wait()
    assert os.path.exists(merge_sketch_path), 'Merged sketch was not created at {}'.format(merge_sketch_path)
    return merge_sketch_path

Merge new Mash sketches with current Mash sketches

Args:
        outdir (str): output directory to write merged Mash sketch file
        sketch_paths (list of str): Mash sketch file paths for input fasta files

Returns:
        str: output path for Mash sketch file with new and old sketches


def __init__(self, *args, **kwargs):

        super(StateTransaction, self).__init__(*args, **kwargs)

        self.Type = TransactionType.StateTransaction

Create an instance.

Args:
        *args:
        **kwargs:


def webhook(self, webhook_url):

        if not webhook_url:
            raise Exception('Url can not be None')

        matcher = re.match(self.__webhook_url_format, webhook_url)
        if not matcher:
            raise Exception('Invalid url format, looking for: ' + self.__webhook_url_format)

        self.api_keys(int(matcher.group(1)), matcher.group(2))

Load object with webhook_url

Args:
        webhook_url (str): full webhook url given by Discord 'create webhook' func


def render_html(root, options=0, extensions=None):

    if extensions is None:
        extensions = _cmark.ffi.NULL

    raw_result = _cmark.lib.cmark_render_html(
        root, options, extensions)

    return _cmark.ffi.string(raw_result).decode('utf-8')

Render a given syntax tree as HTML.

Args:
        root (Any): The reference to the root node of the syntax tree.
        options (int): The cmark options.
        extensions (Any): The reference to the syntax extensions, generally
            from :func:`parser_get_syntax_extensions`

Returns:
        str: The rendered HTML.


Returns dot product

Args:
        p1, p2: point (x, y)
        o: origin


def delete_credit_card(self, *, customer_id, credit_card_id):

        fmt = 'customers/{}/creditCards/{}'.format(customer_id, credit_card_id)
        return self.client._delete(self.url + fmt, headers=self.get_headers())

Delete a credit card (Token) associated with a user.

Args:
        customer_id: Identifier of the client of whom you are going to delete the token.
        credit_card_id: Identifier of the token to be deleted.

    Returns:


def _get_mpr_table(self, connection, partition):

        # TODO: This is the first candidate for optimization. Add field to partition
        # with table name and update it while table creation.
        # Optimized version.
        #
        # return partition.mpr_table or raise exception

        # Not optimized version.
        #
        # first check either partition has materialized view.
        logger.debug(
            'Looking for materialized view of the partition.\n    partition: {}'.format(partition.name))
        foreign_table = partition.vid
        view_table = '{}_v'.format(foreign_table)
        view_exists = self._relation_exists(connection, view_table)
        if view_exists:
            logger.debug(
                'Materialized view of the partition found.\n    partition: {}, view: {}'
                .format(partition.name, view_table))
            return view_table

        # now check for fdw/virtual table
        logger.debug(
            'Looking for foreign table of the partition.\n    partition: {}'.format(partition.name))
        foreign_exists = self._relation_exists(connection, foreign_table)
        if foreign_exists:
            logger.debug(
                'Foreign table of the partition found.\n    partition: {}, foreign table: {}'
                .format(partition.name, foreign_table))
            return foreign_table
        raise MissingTableError('postgres database does not have table for {} partition.'
                                .format(partition.vid))

Returns name of the postgres table who stores mpr data.

Args:
        connection: connection to postgres db who stores mpr data.
        partition (orm.Partition):

Returns:
        str:

Raises:
        MissingTableError: if partition table not found in the db.


def add_workflow_definitions(sbi_config: dict):

    registered_workflows = []
    for i in range(len(sbi_config['processing_blocks'])):
        workflow_config = sbi_config['processing_blocks'][i]['workflow']
        workflow_name = '{}:{}'.format(workflow_config['id'],
                                       workflow_config['version'])
        if workflow_name in registered_workflows:
            continue
        workflow_definition = dict(
            id=workflow_config['id'],
            version=workflow_config['version'],
            stages=[]
        )
        key = "workflow_definitions:{}:{}".format(workflow_config['id'],
                                                  workflow_config['version'])
        DB.save_dict(key, workflow_definition, hierarchical=False)
        registered_workflows.append(workflow_name)

Add any missing SBI workflow definitions as placeholders.

    This is a utility function used in testing and adds mock / test workflow
    definitions to the database for workflows defined in the specified
    SBI config.

Args:
        sbi_config (dict): SBI configuration dictionary.


def __init__(self, source, lineno, target, what=None):

        self.source = source
        self.lineno = lineno
        self.target = target
        self.what = what

Initialization method.

Args:
        source (Module): source Module.
        lineno (int): number of line at which import statement occurs.
        target (str/Module/Package): the target node.
        what (str): what is imported (optional).


def run(self, data_loaders, workflow, max_epochs, **kwargs):

        assert isinstance(data_loaders, list)
        assert mmcv.is_list_of(workflow, tuple)
        assert len(data_loaders) == len(workflow)

        self._max_epochs = max_epochs
        work_dir = self.work_dir if self.work_dir is not None else 'NONE'
        self.logger.info('Start running, host: %s, work_dir: %s',
                         get_host_info(), work_dir)
        self.logger.info('workflow: %s, max: %d epochs', workflow, max_epochs)
        self.call_hook('before_run')

        while self.epoch < max_epochs:
            for i, flow in enumerate(workflow):
                mode, epochs = flow
                if isinstance(mode, str):  # self.train()
                    if not hasattr(self, mode):
                        raise ValueError(
                            'runner has no method named "{}" to run an epoch'.
                            format(mode))
                    epoch_runner = getattr(self, mode)
                elif callable(mode):  # custom train()
                    epoch_runner = mode
                else:
                    raise TypeError('mode in workflow must be a str or '
                                    'callable function, not {}'.format(
                                        type(mode)))
                for _ in range(epochs):
                    if mode == 'train' and self.epoch >= max_epochs:
                        return
                    epoch_runner(data_loaders[i], **kwargs)

        time.sleep(1)  # wait for some hooks like loggers to finish
        self.call_hook('after_run')

Start running.

Args:
        data_loaders (list[:obj:`DataLoader`]): Dataloaders for training
            and validation.
        workflow (list[tuple]): A list of (phase, epochs) to specify the
            running order and epochs. E.g, [('train', 2), ('val', 1)] means
            running 2 epochs for training and 1 epoch for validation,
            iteratively.
        max_epochs (int): Total training epochs.


def warn_if_element_not_of_class(element, class_suffix, special_ui_components_prefix):

    valid_class_names = ["%s%s" % (DEFAULT_UI_COMPONENTS_PREFIX, class_suffix)]
    if special_ui_components_prefix is not None:
        valid_class_names.append("%s%s" % (special_ui_components_prefix, class_suffix))

    if (not element.hasAttribute('customClass')) or element.attributes['customClass'].value not in valid_class_names:
        logging.warn("WARNING: %s is internationalized but isn't one of %s",
                     extract_element_internationalized_comment(element), valid_class_names)

Log a warning if the element is not of the given type (indicating that it is not internationalized).

Args:
        element: The xib's XML element.
        class_name: The type the element should be, but is missing.
        special_ui_components_prefix: If provided, will not warn about class with this prefix (default is only 'JT')


def plot_accuracy(data, output_dir_path='.', output_filename='accuracy.png',
                  width=10, height=8):

    output_path = os.path.join(output_dir_path, output_filename)

    max_val_data = get_epoch_max_val_acc(data)
    max_val_label = round(max_val_data['acc'].values[0], 4)

    # max_val_epoch = max_val_data['epoch'].values[0]
    max_epoch_data = data[data['epoch'] == data['epoch'].max()]

    plot = ggplot(data, aes('epoch', 'acc', color='factor(data)')) + \
        geom_line(size=1, show_legend=False) + \
        geom_vline(aes(xintercept='epoch', color='data'),
                   data=max_val_data, alpha=0.5, show_legend=False) + \
        geom_label(aes('epoch', 'acc'), data=max_val_data,
                   label=max_val_label, nudge_y=-0.02, va='top', label_size=0,
                   show_legend=False) + \
        geom_text(aes('epoch', 'acc', label='data'), data=max_epoch_data,
                  nudge_x=2, ha='center', show_legend=False) + \
        geom_point(aes('epoch', 'acc'), data=max_val_data,
                   show_legend=False) + \
        labs(y='Accuracy', x='Epochs') + \
        theme_bw(base_family='Arial', base_size=15) + \
        scale_color_manual(['#ef8a62', '#67a9cf', "#f7f7f7"])

    plot.save(output_path, width=width, height=height)

Plot accuracy.

Args:
        data: Panda dataframe in *the* format.


def submit(self, job):

        self._check_job(job)

        if job.workingdir is None:
            job.workingdir = self.default_wdir
        job.imageid = du.create_provisioned_image(self.client, job.image,
                                                  job.workingdir, job.inputs)

        container_args = self._generate_container_args(job)

        job.rundata.container = self.client.create_container(job.imageid, **container_args)
        self.client.start(job.rundata.container)
        job.rundata.containerid = job.rundata.container['Id']
        job.jobid = job.rundata.containerid

Submit job to the engine

Args:
        job (pyccc.job.Job): Job to submit


def join(self, other, **kwargs):

        if not isinstance(other, list):
            other = [other]
        return self._join_list_of_managers(other, **kwargs)

Joins a list or two objects together.

Args:
        other: The other object(s) to join on.

Returns:
        Joined objects.


def get_object(tree):

    if isinstance(tree, Tree):
        if tree.label() == 'DT' or tree.label() == 'POS':
            return ''
        words = []
        for child in tree:
            words.append(get_object(child))
        return ' '.join([_f for _f in words if _f])
    else:
        return tree

Get the object in the tree object.

    Method should remove unnecessary letters and words::

        the
        a/an
        's

Args:
        tree (Tree): Parsed tree structure

Returns:
        Resulting string of tree ``(Ex: "red car")``


def registration_info_request(self, registration_id):

        return self.requests_session.get(
            self.INFO_END_POINT + registration_id,
            params={'details': 'true'}
        )

Makes a request for registration info and returns the response object

Args:
        registration_id: id to be checked

Returns:
        response of registration info request


def unsubscribe(self, subscription, max=None):

        if max is None:
            self._send('UNSUB %d' % subscription.sid)
            self._subscriptions.pop(subscription.sid)
        else:
            subscription.max = max
            self._send('UNSUB %d %s' % (subscription.sid, max))

Unsubscribe will remove interest in the given subject. If max is
    provided an automatic Unsubscribe that is processed by the server
    when max messages have been received

Args:
        subscription (pynats.Subscription): a Subscription object
        max (int=None): number of messages


def generate_code(meta, prefix=None, node=False, min=False):

    if isinstance(meta, dict):
        url_prefix, auth_header, resources = parse_meta(meta)
    else:
        url_prefix, auth_header, resources = meta
    if prefix is not None:
        url_prefix = prefix
    core = render_core(url_prefix, auth_header, resources)
    if min:
        filename = 'res.web.min.js'
    else:
        filename = 'res.web.js'
    if node:
        filename = 'res.node.js'
    base = read_file(filename)
    return base.replace('"#res.core.js#"', core)

Generate res.js

Args:
        meta: tuple(url_prefix, auth_header, resources) or metadata of API

Returns:
        res.js source code


def sync_job_info(self, job_name):

        job_path = os.path.join(self._logdir, job_name)

        if job_name not in self._monitored_jobs:
            self._create_job_info(job_path)
            self._monitored_jobs.add(job_name)
        else:
            self._update_job_info(job_path)

        expr_dirs = filter(lambda d: os.path.isdir(os.path.join(job_path, d)),
                           os.listdir(job_path))

        for expr_dir_name in expr_dirs:
            self.sync_trial_info(job_path, expr_dir_name)

        self._update_job_info(job_path)

Load information of the job with the given job name.

    1. Traverse each experiment sub-directory and sync information
        for each trial.
    2. Create or update the job information, together with the job
        meta file.

Args:
        job_name (str) name of the Tune experiment


def as_dtype(type_value):

    if isinstance(type_value, DType):
        return type_value

    try:
        return _INTERN_TABLE[type_value]
    except KeyError:
        pass

    try:
        return _STRING_TO_TF[type_value]
    except KeyError:
        pass

    try:
        return _PYTHON_TO_TF[type_value]
    except KeyError:
        pass

    if isinstance(type_value, np.dtype):
        # The numpy dtype for strings is variable length. We can not compare
        # dtype with a single constant (np.string does not exist) to decide
        # dtype is a "string" type. We need to compare the dtype.type to be
        # sure it's a string type.
        if type_value.type == np.string_ or type_value.type == np.unicode_:
            return string

    if isinstance(type_value, (type, np.dtype)):
        for key, val in _NP_TO_TF:
            try:
                if key == type_value:
                    return val
            except TypeError as e:
                raise TypeError(
                    "Cannot convert {} to a dtype. {}".format(type_value, e)
                )

    raise TypeError("Cannot convert value %r to a TensorFlow DType." % type_value)

Converts the given `type_value` to a `DType`.

Args:
        type_value: A value that can be converted to a `tf.DType` object. This may
        currently be a `tf.DType` object, a [`DataType`
        enum](https://www.tensorflow.org/code/tensorflow/core/framework/types.proto),
        a string type name, or a `numpy.dtype`.

Returns:
        A `DType` corresponding to `type_value`.

Raises:
        TypeError: If `type_value` cannot be converted to a `DType`.


def ExamineEvent(self, mediator, event):

    # TODO: Handle event log entries here also (ie, event id 4697).
    event_data_type = getattr(event, 'data_type', '')
    if event_data_type == 'windows:registry:service':
      # Create and store the service.
      service = WindowsService.FromEvent(event)
      self._service_collection.AddService(service)

Analyzes an event and creates Windows Services as required.

    At present, this method only handles events extracted from the Registry.

Args:
    mediator (AnalysisMediator): mediates interactions between analysis
        plugins and other components, such as storage and dfvfs.
    event (EventObject): event to examine.


def BuildAdGroupCriterionOperations(adgroup_id):

  criterion_operations = [
      {
          # You must specify the xsi_type of operations run by the
          # BatchJobService.
          'xsi_type': 'AdGroupCriterionOperation',
          'operand': {
              'xsi_type': 'BiddableAdGroupCriterion',
              'adGroupId': adgroup_id,
              'criterion': {
                  'xsi_type': 'Keyword',
                  # Make 10% of keywords invalid to demonstrate error handling.
                  'text': 'mars%s%s' % (uuid.uuid4(),
                                        '!!!' if i % 10 == 0 else ''),
                  'matchType': 'BROAD'
              }
          },
          'operator': 'ADD'
      }
      for i in range(KEYWORD_COUNT)]

  return criterion_operations

Builds the operations adding a Keyword Criterion to each AdGroup.

Args:
        adgroup_id: an integer identifying an AdGroup to associate the keywords
        with.

Returns:
        a list containing the operations that will create a new Keyword Criterion
        associated with each provided AdGroup.


def assignees(self, assignee=None, resource_id=None):

        if resource_id is not None:
            self.resource_id(resource_id)
        self._request_uri = '{}/assignees'.format(self._request_uri)
        if assignee is not None:
            self._request_uri = '{}/{}'.format(self._request_uri, assignee)

Add an assignee to a Task

    GET: /v2/tasks/{uniqueId}/assignees
    GET: /v2/tasks/{uniqueId}/assignees/{assigneeId}
    POST: /v2/tasks/{uniqueId}/assignees/{assigneeId}
    DELETE: /v2/tasks/{uniqueId}/assignees/{assigneeId}

Args:
        assignee (Optional [string]): The assignee name.
        resource_id (Optional [string]): The task ID.


def fetcher_with_object(cls, parent_object, relationship="child"):

        fetcher = cls()
        fetcher.parent_object = parent_object
        fetcher.relationship = relationship

        rest_name = cls.managed_object_rest_name()
        parent_object.register_fetcher(fetcher, rest_name)

        return fetcher

Register the fetcher for a served object.

    This method will fill the fetcher with `managed_class` instances

Args:
        parent_object: the instance of the parent object to serve

Returns:
        It returns the fetcher instance.


def _new_open_bin(self, remaining_rect):

        factories_to_delete = set() #
        new_bin = None

        for key, binfac in self._empty_bins.items():

            # Only return the new bin if at least one of the remaining
            # rectangles fit inside.
            a_rectangle_fits = False
            for _, rect in remaining_rect.items():
                if binfac.fits_inside(rect[0], rect[1]):
                    a_rectangle_fits = True
                    break

            if not a_rectangle_fits:
                factories_to_delete.add(key)
                continue

            # Create bin and add to open_bins
            new_bin = binfac.new_bin()
            if new_bin is None:
                continue
            self._open_bins.append(new_bin)

            # If the factory was depleted mark for deletion
            if binfac.is_empty():
                factories_to_delete.add(key)

            break

        # Delete marked factories
        for f in factories_to_delete:
            del self._empty_bins[f]

        return new_bin

Extract the next bin where at least one of the rectangles in
    rem

Args:
        remaining_rect (dict): rectangles not placed yet

Returns:
        PackingAlgorithm: Initialized empty packing bin.
        None: No bin big enough for the rectangle was found


def decode_bu64(b):

    s = b
    s = s.replace(b'-', b'+')
    s = s.replace(b'_', b'/')
    p = len(s) % 4
    if p == 0:
        pass
    elif p == 2:
        s += b'=='
    elif p == 3:
        s += b'='
    else:
        raise ValueError('Illegal Base64url string')
    return base64.standard_b64decode(s)

Encode bytes to a URL safe flavor of Base64 used by JWTs.

    - Reverse of encode_bu64().

Args:
        b: bytes
        URL safe Base64 encoded bytes to encode.

Returns:
        bytes: Decoded bytes.


def process_streamer(self, streamer, callback=None):

        index = streamer.index

        if index in self._in_progress_streamers:
            raise InternalError("You cannot add a streamer again until it has finished streaming.")

        queue_item = QueuedStreamer(streamer, callback)
        self._in_progress_streamers.add(index)

        self._logger.debug("Streamer %d: queued to send %d readings", index, queue_item.initial_count)
        self._queue.put_nowait(queue_item)

Start streaming a streamer.

Args:
        streamer (DataStreamer): The streamer itself.
        callback (callable): An optional callable that will be called as:
            callable(index, success, highest_id_received_from_other_side)


def __delitem__(self, keyword):

        status = False
        if keyword:
            if not self.case_sensitive:
                keyword = keyword.lower()
            current_dict = self.keyword_trie_dict
            character_trie_list = []
            for letter in keyword:
                if letter in current_dict:
                    character_trie_list.append((letter, current_dict))
                    current_dict = current_dict[letter]
                else:
                    # if character is not found, break out of the loop
                    current_dict = None
                    break
            # remove the characters from trie dict if there are no other keywords with them
            if current_dict and self._keyword in current_dict:
                # we found a complete match for input keyword.
                character_trie_list.append((self._keyword, current_dict))
                character_trie_list.reverse()

                for key_to_remove, dict_pointer in character_trie_list:
                    if len(dict_pointer.keys()) == 1:
                        dict_pointer.pop(key_to_remove)
                    else:
                        # more than one key means more than 1 path.
                        # Delete not required path and keep the other
                        dict_pointer.pop(key_to_remove)
                        break
                # successfully removed keyword
                status = True
                self._terms_in_trie -= 1
        return status

To remove keyword from the dictionary
    pass the keyword and the clean name it maps to.

Args:
        keyword : string
            keyword that you want to remove if it's present

Example:
        >>> keyword_processor.add_keyword('Big Apple')
        >>> del keyword_processor['Big Apple']


def forecast(self, throughputs, backlog_size, num_simulations=10000, max_periods=10000, seed=None):

        self._check_throughputs(throughputs)
        results = []

        if seed is not None:
            random.seed(seed)

        for i in range(0, num_simulations):
            simulated_backlog = backlog_size
            time_unit_count = 0
            while simulated_backlog > 0:
                simulated_backlog -= random.choice(throughputs)
                time_unit_count += 1
                if time_unit_count > max_periods:
                    raise ValueError("More than {} periods calculated".format(max_periods))
            results.append(time_unit_count)

        return Results(results)

Forecasts how long a backlog will take to complete given the historical values provided.

Args:
        throughputs(List[int]): Number of units completed per unit of time (stories per week, story points per month, etc.)
        backlog_size(int): Units in the backlog (stories, points, etc.)

Returns:
        results
    Exceptions:
        ValueError: If there aren't any positive throughputs, or the simulation takes too long.


def _handle_captcha(captcha_data, message=''): # pylint:disable=unused-argument

        from tempfile import NamedTemporaryFile
        tmpf = NamedTemporaryFile(suffix='.png')
        tmpf.write(captcha_data)
        tmpf.flush()
        captcha_text = input('Please take a look at the captcha image "%s" and provide the code:' % tmpf.name)
        tmpf.close()
        return captcha_text

Called when a captcha must be solved
    Writes the image to a temporary file and asks the user to enter the code.

Args:
        captcha_data: Bytestring of the PNG captcha image.
        message: Optional. A message from Steam service.

Returns:
        A string containing the solved captcha code.


def weights_to_cpu(state_dict):

    state_dict_cpu = OrderedDict()
    for key, val in state_dict.items():
        state_dict_cpu[key] = val.cpu()
    return state_dict_cpu

Copy a model state_dict to cpu.

Args:
        state_dict (OrderedDict): Model weights on GPU.

Returns:
        OrderedDict: Model weights on GPU.


def _or_join(self, terms):

        from six import text_type

        if isinstance(terms, (tuple, list)):
            if len(terms) > 1:
                return ' | '.join(text_type(t) for t in terms)
            else:
                return terms[0]
        else:
            return terms

Joins terms using OR operator.

Args:
        terms (list): terms to join

Example:
        self._or_join(['term1', 'term2']) -> 'term1 | term2'

Returns:
        str


def _load_chunk(dat_path, cat_path, info_path):

  dat_array = read_binary_matrix(dat_path)
  # Even if the image is gray scale, we need to add an extra channel dimension
  # to be compatible with tfds.features.Image.
  dat_array = np.expand_dims(dat_array, -1)

  cat_array = read_binary_matrix(cat_path)

  info_array = read_binary_matrix(info_path)
  info_array = np.copy(info_array)  # Make read-only buffer array writable.
  # Azimuth values are 0, 2, 4, .., 34. We divide by 2 to get proper labels.
  info_array[:, 2] = info_array[:, 2] / 2

  return dat_array, cat_array, info_array

Loads a data chunk as specified by the paths.

Args:
        dat_path: Path to dat file of the chunk.
        cat_path: Path to cat file of the chunk.
        info_path: Path to info file of the chunk.

Returns:
        Tuple with the dat, cat, info_arrays.


def getQueryEngineDescription(self, queryEngine, **kwargs):

        response = self.getQueryEngineDescriptionResponse(queryEngine, **kwargs)
        return self._read_dataone_type_response(response, 'QueryEngineDescription')

See Also: getQueryEngineDescriptionResponse()

Args:
        queryEngine:
        **kwargs:

    Returns:


def save(self, projects):

        base_path = os.path.expanduser(self.path)

        if not os.path.isdir(base_path):
            return

        logger.debug("Save projects config to %s", base_path)

        for name, data in list(projects.items()):
            project_file_path = self.get_project_config_path(name)
            with open(project_file_path, "w") as f:
                yaml.dump(data, stream = f, default_flow_style = False)
                logger.debug("Project '%s' config has been writed to '%s'", name, project_file_path)

Save the projects configs to local path

Args:
        projects (dict): project_name -> project_data


def _GetSpecificationStore(cls, format_category):

    specification_store = specification.FormatSpecificationStore()
    remainder_list = []

    for analyzer_helper in iter(cls._analyzer_helpers.values()):
      if not analyzer_helper.IsEnabled():
        continue

      if format_category in analyzer_helper.format_categories:
        format_specification = analyzer_helper.GetFormatSpecification()

        if format_specification is not None:
          specification_store.AddSpecification(format_specification)
        else:
          remainder_list.append(analyzer_helper)

    return specification_store, remainder_list

Retrieves the specification store for specified format category.

Args:
        format_category (str): format category.

Returns:
        tuple[FormatSpecificationStore, list[AnalyzerHelper]]: a format
        specification store and remaining analyzer helpers that do not have
        a format specification.


def replace_variables(self, text):

        variables = {
            'python-executable': str(self._venv_path / 'bin' / 'python')
        }
        return text.format(**variables)

Replace variable placeholders in `text` with values from the virtual env.

    The variables are:
        - {python-executable}

Args:
        text: The text to do replacment int.

    Returns: The text after replacement.


def __init__(self, structure, transformations=None, history=None,
                 other_parameters=None):

        self.final_structure = structure
        self.history = history or []
        self.other_parameters = other_parameters or {}
        self._undone = []

        transformations = transformations or []
        for t in transformations:
            self.append_transformation(t)

Initializes a transformed structure from a structure.

Args:
        structure (Structure): Input structure
        transformations ([Transformations]): List of transformations to
            apply.
        history (list): Previous history.
        other_parameters (dict): Additional parameters to be added.


def note_revert(self, note_id, version_id):

        return self._get('notes/{0}/revert.json'.format(note_id),
                         {'version_id': version_id}, method='PUT', auth=True)

Function to revert a specific note (Requires login) (UNTESTED).

    Parameters:
        note_id (int): Where note_id is the note id.
        version_id (int): The note version id to revert to.


def ikey(self, value):

        if value == self._defaults['iKey'] and 'iKey' in self._values:
            del self._values['iKey']
        else:
            self._values['iKey'] = value

The ikey property.

Args:
        value (string). the property value.


def updateEnterpriseUser(self, username, idpUsername):

        params = {
            "f" : "json",
            "username" : username,
            "idpUsername" : idpUsername
        }
        url = self._url + "/users/updateEnterpriseUser"
        return self._post(url=url,
                          param_dict=params,
                          proxy_url=self._proxy_url,
                          proxy_port=self._proxy_port)

This operation allows an administrator to update the idpUsername
    for an enterprise user in the portal. This is used when migrating
    from accounts used with web-tier authentication to SAML
    authentication.

    Parameters:
        username - username of the enterprise account
        idpUsername - username used by the SAML identity provider


def auctionDF(symbol=None, token='', version=''):

    df = pd.io.json.json_normalize(auction(symbol, token, version))
    _toDatetime(df)
    return df

DEEP broadcasts an Auction Information Message every one second between the Lock-in Time and the auction match for Opening and Closing Auctions,
    and during the Display Only Period for IPO, Halt, and Volatility Auctions. Only IEX listed securities are eligible for IEX Auctions.

    https://iexcloud.io/docs/api/#deep-auction

Args:
        symbol (string); Ticker to request
        token (string); Access token
        version (string); API version

Returns:
        DataFrame: result


def load(self, data):

        resp = self.client.api.load_image(data)
        images = []
        for chunk in resp:
            if 'stream' in chunk:
                match = re.search(
                    r'(^Loaded image ID: |^Loaded image: )(.+)$',
                    chunk['stream']
                )
                if match:
                    image_id = match.group(2)
                    images.append(image_id)
            if 'error' in chunk:
                raise ImageLoadError(chunk['error'])

        return [self.get(i) for i in images]

Load an image that was previously saved using
    :py:meth:`~docker.models.images.Image.save` (or ``docker save``).
    Similar to ``docker load``.

Args:
        data (binary): Image data to be loaded.

Returns:
        (list of :py:class:`Image`): The images.

Raises:
        :py:class:`docker.errors.APIError`
            If the server returns an error.


def _protobuf_value_to_string(value):

  value_in_json = json_format.MessageToJson(value)
  if value.HasField("string_value"):
    # Remove the quotations.
    return value_in_json[1:-1]
  return value_in_json

Returns a string representation of given google.protobuf.Value message.

Args:
        value: google.protobuf.Value message. Assumed to be of type 'number',
        'string' or 'bool'.


def vmstat(stat):

    out = subprocess.check_output(["vmstat", "-s"])
    stat = stat.encode("ascii")
    for line in out.split(b"\n"):
        line = line.strip()
        if stat in line:
            return int(line.split(b" ")[0])
    raise ValueError("Can't find {} in 'vmstat' output.".format(stat))

Run vmstat and get a particular statistic.

Args:
        stat: The statistic that we are interested in retrieving.

Returns:
        The parsed output.


def __init__(self, owner, repo_name, token=''):

        self._github_repository = GitHub(token=token).repository(owner, repo_name)

Build the GitHub API URL which points to the definition of the repository.

Args:
        owner (str): the owner's GitHub username
        repo_name (str): the name of the repository
        token (str): the GitHub API token

Returns:
        dict: a representation of the repo definition


def __gt__(self, other):

        if other.__class__ is not self.__class__:
            return NotImplemented
        return not self <= other

Test if self is greater than an object of the same class.

Args:
        other: The object to compare against.

Returns:
        True if self is greater than other; else False.

Raises:
        TypeError: Raised if the objects are not of the same class.


def get_ref(profile, ref):

    resource = "/refs/" + ref
    data = api.get_request(profile, resource)
    return prepare(data)

Fetch a ref.

Args:
        profile
            A profile generated from ``simplygithub.authentication.profile``.
            Such profiles tell this module (i) the ``repo`` to connect to,
            and (ii) the ``token`` to connect with.

        ref
            The ref to fetch, e.g., ``heads/my-feature-branch``.

    Returns
        A dict with data about the ref.


def GetMessages(self, formatter_mediator, event):

    if self.DATA_TYPE != event.data_type:
      raise errors.WrongFormatter('Unsupported data type: {0:s}.'.format(
          event.data_type))

    event_values = event.CopyToDict()

    regvalue = event_values.get('regvalue', {})
    string_parts = []
    for key, value in sorted(regvalue.items()):
      string_parts.append('{0:s}: {1!s}'.format(key, value))
    event_values['text'] = ' '.join(string_parts)

    urls = event_values.get('urls', [])
    if urls:
      event_values['urls'] = ' - '.join(urls)

    if 'key_path' in event_values:
      format_string = self.FORMAT_STRING
    else:
      format_string = self.FORMAT_STRING_ALTERNATIVE

    return self._FormatMessages(
        format_string, self.FORMAT_STRING_SHORT, event_values)

Determines the formatted message strings for an event object.

Args:
        formatter_mediator (FormatterMediator): mediates the interactions
        between formatters and other components, such as storage and Windows
        EventLog resources.
        event (EventObject): event.

Returns:
        tuple(str, str): formatted message string and short message string.

Raises:
        WrongFormatter: if the event object cannot be formatted by the formatter.


def _mean_of_runs(stats, key='runs'):

    num_runs = len(stats[key])
    first = stats[key][0]

    mean = {}
    for stat_key in first:
        # Skip non numberic attributes
        if isinstance(first[stat_key], numbers.Number):
            mean[stat_key] = sum(run[stat_key]
                                 for run in stats[key]) / float(num_runs)

    return mean

Obtain the mean of stats.

Args:
        stats: dict; A set of stats, structured as above.
        key: str; Optional key to determine where list of runs is found in stats


def confusion_matrix(
    gold, pred, null_pred=False, null_gold=False, normalize=False, pretty_print=True
):

    conf = ConfusionMatrix(null_pred=null_pred, null_gold=null_gold)
    gold = arraylike_to_numpy(gold)
    pred = arraylike_to_numpy(pred)
    conf.add(gold, pred)
    mat = conf.compile()

    if normalize:
        mat = mat / len(gold)

    if pretty_print:
        conf.display(normalize=normalize)

    return mat

A shortcut method for building a confusion matrix all at once.

Args:
        gold: an array-like of gold labels (ints)
        pred: an array-like of predictions (ints)
        null_pred: If True, include the row corresponding to null predictions
        null_gold: If True, include the col corresponding to null gold labels
        normalize: if True, divide counts by the total number of items
        pretty_print: if True, pretty-print the matrix before returning


def is_common(schema):

    if isinstance(schema, StreamSchema):
        return schema.schema() in _SCHEMA_COMMON
    if isinstance(schema, CommonSchema):
        return True
    if isinstance(schema, basestring):
        return is_common(StreamSchema(schema))
    return False

Is `schema` an common schema.

Args:
        schema: Scheme to test.

Returns:
        bool: ``True`` if schema is a common schema, otherwise ``False``.


def urlToIds(url):

        urlId = url.split("/")[-1]
        convUrl = "https://join.skype.com/api/v2/conversation/"
        json = SkypeConnection.externalCall("POST", convUrl, json={"shortId": urlId, "type": "wl"}).json()
        return {"id": json.get("Resource"),
                "long": json.get("Id"),
                "blob": json.get("ChatBlob")}

Resolve a ``join.skype.com`` URL and returns various identifiers for the group conversation.

Args:
        url (str): public join URL, or identifier from it

Returns:
        dict: related conversation's identifiers -- keys: ``id``, ``long``, ``blob``


def OSXEnumerateRunningServicesFromClient(args):

  del args  # Unused.
  osx_version = client_utils_osx.OSXVersion()
  version_array = osx_version.VersionAsMajorMinor()

  if version_array[:2] < [10, 6]:
    raise UnsupportedOSVersionError(
        "ServiceManagement API unsupported on < 10.6. This client is %s" %
        osx_version.VersionString())

  launchd_list = GetRunningLaunchDaemons()

  parser = osx_launchd.OSXLaunchdJobDict(launchd_list)
  for job in parser.Parse():
    response = CreateServiceProto(job)
    yield response

Get running launchd jobs.

Args:
        args: Unused.

Yields:
        `rdf_client.OSXServiceInformation` instances.

Raises:
        UnsupportedOSVersionError: for OS X earlier than 10.6.


def create_clusters(provider, context, **kwargs):

    conn = get_session(provider.region).client('ecs')

    try:
        clusters = kwargs["clusters"]
    except KeyError:
        logger.error("setup_clusters hook missing \"clusters\" argument")
        return False

    if isinstance(clusters, basestring):
        clusters = [clusters]

    cluster_info = {}
    for cluster in clusters:
        logger.debug("Creating ECS cluster: %s", cluster)
        r = conn.create_cluster(clusterName=cluster)
        cluster_info[r["cluster"]["clusterName"]] = r
    return {"clusters": cluster_info}

Creates ECS clusters.

    Expects a "clusters" argument, which should contain a list of cluster
    names to create.

Args:
        provider (:class:`stacker.providers.base.BaseProvider`): provider
            instance
        context (:class:`stacker.context.Context`): context instance

    Returns: boolean for whether or not the hook succeeded.


def _finish_add(self, num_bytes_to_add, num_partition_bytes_to_add):
        # type: (int, int) -> None

        for pvd in self.pvds:
            pvd.add_to_space_size(num_bytes_to_add + num_partition_bytes_to_add)
        if self.joliet_vd is not None:
            self.joliet_vd.add_to_space_size(num_bytes_to_add + num_partition_bytes_to_add)

        if self.enhanced_vd is not None:
            self.enhanced_vd.copy_sizes(self.pvd)

        if self.udf_root is not None:
            num_extents_to_add = utils.ceiling_div(num_partition_bytes_to_add,
                                                   self.pvd.logical_block_size())

            self.udf_main_descs.partition.part_length += num_extents_to_add
            self.udf_reserve_descs.partition.part_length += num_extents_to_add
            self.udf_logical_volume_integrity.size_table += num_extents_to_add

        if self._always_consistent:
            self._reshuffle_extents()
        else:
            self._needs_reshuffle = True

An internal method to do all of the accounting needed whenever
    something is added to the ISO.  This method should only be called by
    public API implementations.

    Parameters:
    num_bytes_to_add - The number of additional bytes to add to all
                        descriptors.
    num_partition_bytes_to_add - The number of additional bytes to add to
                                the partition if this is a UDF file.

Returns:
    Nothing.


def set_colors(self, fg=None, bg=None):

        if fg is not None:
            self._fg = _format_color(fg, self._fg)
        if bg is not None:
            self._bg = _format_color(bg, self._bg)

Sets the colors to be used with the L{print_str} and draw_* methods.

    Values of None will only leave the current values unchanged.

Args:
        fg (Optional[Union[Tuple[int, int, int], int, Ellipsis]])
        bg (Optional[Union[Tuple[int, int, int], int, Ellipsis]])
    .. seealso:: :any:`move`, :any:`print_str`


def ip_prefixes_sanity_check(config, bird_configuration):

    for ip_version in bird_configuration:
        modify_ip_prefixes(config,
                           bird_configuration[ip_version]['config_file'],
                           bird_configuration[ip_version]['variable_name'],
                           bird_configuration[ip_version]['dummy_ip_prefix'],
                           bird_configuration[ip_version]['reconfigure_cmd'],
                           bird_configuration[ip_version]['keep_changes'],
                           bird_configuration[ip_version]['changes_counter'],
                           ip_version)

Sanity check on IP prefixes.

Args:
        config (obg): A configparser object which holds our configuration.
        bird_configuration (dict): A dictionary, which holds Bird configuration
        per IP protocol version.


def available_readers(as_dict=False):

    readers = []
    for reader_configs in configs_for_reader():
        try:
            reader_info = read_reader_config(reader_configs)
        except (KeyError, IOError, yaml.YAMLError):
            LOG.warning("Could not import reader config from: %s", reader_configs)
            LOG.debug("Error loading YAML", exc_info=True)
            continue
        readers.append(reader_info if as_dict else reader_info['name'])
    return readers

Available readers based on current configuration.

Args:
        as_dict (bool): Optionally return reader information as a dictionary.
                        Default: False

    Returns: List of available reader names. If `as_dict` is `True` then
            a list of dictionaries including additionally reader information
            is returned.


def lookup_rest_method(self, orig_request):

    method_name, method, params = self.config_manager.lookup_rest_method(
        orig_request.path, orig_request.request_uri, orig_request.http_method)
    orig_request.method_name = method_name
    return method, params

Looks up and returns rest method for the currently-pending request.

Args:
        orig_request: An ApiRequest, the original request from the user.

Returns:
        A tuple of (method descriptor, parameters), or (None, None) if no method
        was found for the current request.


def poke(exposes):

    def _poke(store, objname, obj, container, visited=None, _stack=None):
        try:
            sub_container = store.newContainer(objname, obj, container)
        except (SystemExit, KeyboardInterrupt):
            raise
        except:
            raise ValueError('generic poke not supported by store')
        #_stack = _add_to_stack(_stack, objname)
        for iobjname in exposes:
            try:
                iobj = getattr(obj, iobjname)
            except AttributeError:
                pass
            else:
                store.poke(iobjname, iobj, sub_container, visited=visited, \
                    _stack=_stack)
    return _poke

Default serializer factory.

Args:
        exposes (iterable): attributes to serialized.

Returns:
        callable: serializer (`poke` routine).


def release_port(upnp, external_port):

    mapping = upnp.getspecificportmapping(external_port, 'UDP')

    if mapping is None:
        log.error('could not find a port mapping', external=external_port)
        return False
    else:
        log.debug('found existing port mapping', mapping=mapping)

    if upnp.deleteportmapping(external_port, 'UDP'):
        log.info('successfully released port mapping', external=external_port)
        return True

    log.warning(
        'could not release port mapping, check your router for stale mappings',
    )
    return False

Try to release the port mapping for `external_port`.

Args:
        external_port (int): the port that was previously forwarded to.

Returns:
        success (boolean): if the release was successful.


def hugepage_support(user, group='hugetlb', nr_hugepages=256,
                     max_map_count=65536, mnt_point='/run/hugepages/kvm',
                     pagesize='2MB', mount=True, set_shmmax=False):

    group_info = add_group(group)
    gid = group_info.gr_gid
    add_user_to_group(user, group)
    if max_map_count < 2 * nr_hugepages:
        max_map_count = 2 * nr_hugepages
    sysctl_settings = {
        'vm.nr_hugepages': nr_hugepages,
        'vm.max_map_count': max_map_count,
        'vm.hugetlb_shm_group': gid,
    }
    if set_shmmax:
        shmmax_current = int(check_output(['sysctl', '-n', 'kernel.shmmax']))
        shmmax_minsize = bytes_from_string(pagesize) * nr_hugepages
        if shmmax_minsize > shmmax_current:
            sysctl_settings['kernel.shmmax'] = shmmax_minsize
    sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/10-hugepage.conf')
    mkdir(mnt_point, owner='root', group='root', perms=0o755, force=False)
    lfstab = fstab.Fstab()
    fstab_entry = lfstab.get_entry_by_attr('mountpoint', mnt_point)
    if fstab_entry:
        lfstab.remove_entry(fstab_entry)
    entry = lfstab.Entry('nodev', mnt_point, 'hugetlbfs',
                         'mode=1770,gid={},pagesize={}'.format(gid, pagesize), 0, 0)
    lfstab.add_entry(entry)
    if mount:
        fstab_mount(mnt_point)

Enable hugepages on system.

Args:
    user (str)  -- Username to allow access to hugepages to
    group (str) -- Group name to own hugepages
    nr_hugepages (int) -- Number of pages to reserve
    max_map_count (int) -- Number of Virtual Memory Areas a process can own
    mnt_point (str) -- Directory to mount hugepages on
    pagesize (str) -- Size of hugepages
    mount (bool) -- Whether to Mount hugepages


def set(self, name, permission):

        assert isinstance(permission, BasePermission), 'Only permission instances can be added to the set'

        self._permissions[name] = permission

Adds permission with the given name to the set. Permission with the same name will be overridden.

Args:
        name: name of the permission
        permission: permission instance


def render_latex(latex: str) -> PIL.Image:      # pragma: no cover

    tmpfilename = 'circ'
    with tempfile.TemporaryDirectory() as tmpdirname:
        tmppath = os.path.join(tmpdirname, tmpfilename)
        with open(tmppath + '.tex', 'w') as latex_file:
            latex_file.write(latex)

        subprocess.run(["pdflatex",
                        "-halt-on-error",
                        "-output-directory={}".format(tmpdirname),
                        "{}".format(tmpfilename+'.tex')],
                       stdout=subprocess.PIPE,
                       stderr=subprocess.DEVNULL,
                       check=True)

        subprocess.run(['pdftocairo',
                        '-singlefile',
                        '-png',
                        '-q',
                        tmppath + '.pdf',
                        tmppath])
        img = PIL.Image.open(tmppath + '.png')

    return img

Convert a single page LaTeX document into an image.

    To display the returned image, `img.show()`

    Required external dependencies: `pdflatex` (with `qcircuit` package),
    and `poppler` (for `pdftocairo`).

Args:
        A LaTeX document as a string.

Returns:
        A PIL Image

Raises:
        OSError: If an external dependency is not installed.


def _patch_expand_paths(self, settings, name, value):

        return [self._patch_expand_path(settings, name, item)
                for item in value]

Apply ``SettingsPostProcessor._patch_expand_path`` to each element in
    list.

Args:
        settings (dict): Current settings.
        name (str): Setting name.
        value (list): List of paths to patch.

Returns:
        list: Patched path list to an absolute path.


def __init__(self, obj):

        if distob.engine is None:
            setup_engines()
        if isinstance(obj, Ref):
            self._ref = obj
            self.is_local = (self._ref.id.engine is distob.engine.eid)
        else:
            self._ref = Ref(obj)
            self.is_local = True
        if self.is_local:
            self._dv = None
            self._obcache = distob.engine[self._ref.id]
            self._obcache_current = True
        else:
            self._dv = distob.engine._client[self._ref.id.engine]
            self._dv.use_dill()
            self._obcache = None
            self._obcache_current = False
        self._id = self._ref.id
        # preference setting: whether to give cached local results if available
        self.prefer_local = True
        #Add proxy controllers for any instance-specific methods/attributes:
        instance_methods, instance_attribs, size = call(
                _scan_instance, self, self.__class__._include_underscore,
                self.__class__._exclude, prefer_local=False)
        for name, doc in instance_methods:
            setattr(self, name, _make_proxy_method(name, doc))
        for name, doc in instance_attribs:
            setattr(self.__class__, name, _make_proxy_property(name, doc))
        self.__engine_affinity__ = (self._ref.id.engine, size)

Set up the Remote* proxy object to access an already-existing object,
    which may be local or remote.

Args:
        obj (Ref or object): either a Ref reference to the (possibly remote)
        object to be controlled, or else an actual (local) object to be
        controlled.


def _check_load_existing_object(self, object_type, id_field_name, operation='update'):
        # type: (str, str, str) -> None

        self._check_existing_object(object_type, id_field_name)
        if not self._load_from_hdx(object_type, self.data[id_field_name]):
            raise HDXError('No existing %s to %s!' % (object_type, operation))

Check metadata exists and contains HDX object identifier, and if so load HDX object

Args:
        object_type (str): Description of HDX object type (for messages)
        id_field_name (str): Name of field containing HDX object identifier
        operation (str): Operation to report if error. Defaults to update.

Returns:
        None


def releases(self, **kwargs):

        path = self._get_id_path('releases')

        response = self._GET(path, kwargs)
        self._set_attrs_to_values(response)
        return response

Get the release date and certification information by country for a
    specific movie id.

Args:
        append_to_response: (optional) Comma separated, any movie method.

Returns:
        A dict representation of the JSON returned from the API.


def WriteScanNode(self, scan_context, scan_node, indentation=''):

    if not scan_node:
      return

    values = []

    part_index = getattr(scan_node.path_spec, 'part_index', None)
    if part_index is not None:
      values.append('{0:d}'.format(part_index))

    store_index = getattr(scan_node.path_spec, 'store_index', None)
    if store_index is not None:
      values.append('{0:d}'.format(store_index))

    start_offset = getattr(scan_node.path_spec, 'start_offset', None)
    if start_offset is not None:
      values.append('start offset: {0:d} (0x{0:08x})'.format(start_offset))

    location = getattr(scan_node.path_spec, 'location', None)
    if location is not None:
      values.append('location: {0:s}'.format(location))

    values = ', '.join(values)

    flags = ''
    if scan_node in scan_context.locked_scan_nodes:
      flags = ' [LOCKED]'

    print('{0:s}{1:s}: {2:s}{3:s}'.format(
        indentation, scan_node.path_spec.type_indicator, values, flags))

    indentation = '  {0:s}'.format(indentation)
    for sub_scan_node in scan_node.sub_nodes:
      self.WriteScanNode(scan_context, sub_scan_node, indentation=indentation)

Writes the source scanner node to stdout.

Args:
        scan_context (SourceScannerContext): the source scanner context.
        scan_node (SourceScanNode): the scan node.
        indentation (Optional[str]): indentation.


def _build_vocab(filename, vocab_path, vocab_size):

  data = _read_words(filename)
  counter = collections.Counter(data)
  count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0]))
  words, _ = list(zip(*count_pairs))
  words = words[:vocab_size]
  with open(vocab_path, "w") as f:
    f.write("\n".join(words))

Reads a file to build a vocabulary of `vocab_size` most common words.

    The vocabulary is sorted by occurrence count and has one word per line.
    Originally from:
    https://github.com/tensorflow/models/blob/master/tutorials/rnn/ptb/reader.py

Args:
    filename: file to read list of words from.
    vocab_path: path where to save the vocabulary.
    vocab_size: size of the vocabulary to generate.