Python typing.TextIO() Examples

The following are 30 code examples of typing.TextIO(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module typing , or try the search function .
Example #1
Source File: textProcessors.py    From armi with Apache License 2.0 6 votes vote down vote up
def _resolveMarkupInclusions(
    src: Union[TextIO, pathlib.Path], root: Optional[pathlib.Path] = None
) -> Tuple[io.StringIO, List[Tuple[pathlib.Path, FileMark]]]:
    root = _getRootFromSrc(src, root)

    if isinstance(src, pathlib.Path):
        # this is inefficient, but avoids having to play with io buffers
        with open(src, "r") as rootFile:
            src = io.StringIO(rootFile.read())

    out = io.StringIO()
    includes = []
    _processIncludes(src, out, includes, root)

    out.seek(0)
    # be kind; rewind
    src.seek(0)

    return out, includes 
Example #2
Source File: supersummary.py    From pybel with MIT License 6 votes vote down vote up
def citations(graph: BELGraph, n: Optional[int] = 15, file: Optional[TextIO] = None) -> None:
    """Print a summary of the citations in the graph."""
    edge_mapping = multidict(
        ((data[CITATION][CITATION_DB], data[CITATION][CITATION_IDENTIFIER]), graph.edge_to_bel(u, v, data))
        for u, v, data in graph.edges(data=True)
        if CITATION in data
    )
    edge_c = Counter({top_level_edge: len(edges) for top_level_edge, edges in edge_mapping.items()})
    df = pd.DataFrame(
        [
            (':'.join(top_level_edge), count, random.choice(edge_mapping[top_level_edge]))  # noqa:S311
            for top_level_edge, count in edge_c.most_common(n=n)
        ],
        columns=['Citation', 'Count', 'Example'],
    )

    if n is None or len(edge_mapping) < n:
        print('{} Citation Count: {}'.format(graph, len(edge_mapping)))
    else:
        print('{} Citation Count: {} (Showing top {})'.format(graph, len(edge_mapping), n))
    print(tabulate(df.values, headers=df.columns), file=file) 
Example #3
Source File: logging.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
def load_resource(path: str, encoding: str = None) -> TextIO:
    """
    Open a resource file located in a python package or the local filesystem.

    Args:
        path: The resource path in the form of `dir/file` or `package:dir/file`
    Returns:
        A file-like object representing the resource
    """
    components = path.rsplit(":", 1)
    try:
        if len(components) == 1:
            return open(components[0], encoding=encoding)
        else:
            bstream = pkg_resources.resource_stream(components[0], components[1])
            if encoding:
                return TextIOWrapper(bstream, encoding=encoding)
            return bstream
    except IOError:
        pass 
Example #4
Source File: terminal.py    From pytest with MIT License 6 votes vote down vote up
def __init__(self, config: Config, file: Optional[TextIO] = None) -> None:
        import _pytest.config

        self.config = config
        self._numcollected = 0
        self._session = None  # type: Optional[Session]
        self._showfspath = None  # type: Optional[bool]

        self.stats = {}  # type: Dict[str, List[Any]]
        self._main_color = None  # type: Optional[str]
        self._known_types = None  # type: Optional[List]
        self.startdir = config.invocation_dir
        if file is None:
            file = sys.stdout
        self._tw = _pytest.config.create_terminal_writer(config, file)
        self._screen_width = self._tw.fullwidth
        self.currentfspath = None  # type: Any
        self.reportchars = getreportopt(config)
        self.hasmarkup = self._tw.hasmarkup
        self.isatty = file.isatty()
        self._progress_nodeids_reported = set()  # type: Set[str]
        self._show_progress_info = self._determine_show_progress_info()
        self._collect_report_last_write = None  # type: Optional[float]
        self._already_displayed_warnings = None  # type: Optional[int]
        self._keyboardinterrupt_memo = None  # type: Optional[ExceptionRepr] 
Example #5
Source File: __init__.py    From pytest with MIT License 6 votes vote down vote up
def create_terminal_writer(
    config: Config, file: Optional[TextIO] = None
) -> TerminalWriter:
    """Create a TerminalWriter instance configured according to the options
    in the config object. Every code which requires a TerminalWriter object
    and has access to a config object should use this function.
    """
    tw = TerminalWriter(file=file)
    if config.option.color == "yes":
        tw.hasmarkup = True
    elif config.option.color == "no":
        tw.hasmarkup = False

    if config.option.code_highlight == "yes":
        tw.code_highlight = True
    elif config.option.code_highlight == "no":
        tw.code_highlight = False
    return tw 
Example #6
Source File: GPUmodule.py    From gpu-utils with GNU General Public License v3.0 6 votes vote down vote up
def print_log(self, log_file_ptr: TextIO) -> bool:
        """
        Print the log data.

        :param log_file_ptr: File pointer for target output.
        :return: True if success
        """
        if self.num_gpus()['total'] < 1:
            return False

        # Print Data
        for gpu in self.gpus():
            print('{}|{}'.format(gpu.energy['tn'].strftime(env.GUT_CONST.TIME_FORMAT), gpu.prm.card_num),
                  sep='', end='', file=log_file_ptr)
            for table_item in self.table_parameters():
                print('|{}'.format(re.sub(PATTERNS['MHz'], '', str(gpu.get_params_value(table_item)).strip())),
                      sep='', end='', file=log_file_ptr)
            print('', file=log_file_ptr)
        return True 
Example #7
Source File: GPUmodule.py    From gpu-utils with GNU General Public License v3.0 6 votes vote down vote up
def print_log_header(self, log_file_ptr: TextIO) -> bool:
        """
        Print the log header.

        :param log_file_ptr: File pointer for target output.
        :return: True if success
        """
        if self.num_gpus()['total'] < 1:
            return False

        # Print Header
        print('Time|Card#', end='', file=log_file_ptr)
        for table_item in self.table_parameters():
            print('|{}'.format(table_item), end='', file=log_file_ptr)
        print('', file=log_file_ptr)
        return True 
Example #8
Source File: utils.py    From cmdstanpy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def scan_warmup_iters(fd: TextIO, config_dict: Dict, lineno: int) -> int:
    """
    Check warmup iterations, if any.
    """
    if 'save_warmup' not in config_dict:
        return lineno
    cur_pos = fd.tell()
    line = fd.readline().strip()
    draws_found = 0
    while len(line) > 0 and not line.startswith('#'):
        lineno += 1
        draws_found += 1
        cur_pos = fd.tell()
        line = fd.readline().strip()
    fd.seek(cur_pos)
    config_dict['draws_warmup'] = draws_found
    return lineno 
Example #9
Source File: extras.py    From pybel with MIT License 6 votes vote down vote up
def to_sif(graph: BELGraph, path: Union[str, TextIO], sep: Optional[str] = None) -> None:
    """Write the graph as a tab-separated SIF file.

    The resulting file will contain the following columns:

    1. Source BEL term
    2. Relation
    3. Target BEL term

    This format is simple and can be used readily with many applications, but is lossy in that it does not include
    relation metadata.
    """
    if sep is None:
        sep = '\t'

    for u, v, data in graph.edges(data=True):
        print(
            graph.edge_to_bel(u, v, edge_data=data, sep=sep),
            file=path,
        ) 
Example #10
Source File: utils.py    From cmdstanpy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def scan_sampling_iters(fd: TextIO, config_dict: Dict, lineno: int) -> int:
    """
    Parse sampling iteration, save number of iterations to config_dict.
    """
    draws_found = 0
    num_cols = len(config_dict['column_names'])
    cur_pos = fd.tell()
    line = fd.readline().strip()
    while len(line) > 0 and not line.startswith('#'):
        lineno += 1
        draws_found += 1
        data = line.split(',')
        if len(data) != num_cols:
            raise ValueError(
                'line {}: bad draw, expecting {} items, found {}'.format(
                    lineno, num_cols, len(line.split(','))
                )
            )
        cur_pos = fd.tell()
        line = fd.readline().strip()
    config_dict['draws_sampling'] = draws_found
    fd.seek(cur_pos)
    return lineno 
Example #11
Source File: extras.py    From pybel with MIT License 6 votes vote down vote up
def to_csv(graph: BELGraph, path: Union[str, TextIO], sep: Optional[str] = None) -> None:
    """Write the graph as a tab-separated edge list.

    The resulting file will contain the following columns:

    1. Source BEL term
    2. Relation
    3. Target BEL term
    4. Edge data dictionary

    See the Data Models section of the documentation for which data are stored in the edge data dictionary, such
    as queryable information about transforms on the subject and object and their associated metadata.
    """
    if sep is None:
        sep = '\t'

    for u, v, data in graph.edges(data=True):
        print(
            graph.edge_to_bel(u, v, edge_data=data, sep=sep),
            json.dumps(data),
            sep=sep,
            file=path,
        ) 
Example #12
Source File: api.py    From pybel with MIT License 6 votes vote down vote up
def to_edgelist(
    graph: BELGraph,
    path: Union[str, TextIO],
    *,
    use_tqdm: bool = False,
    sep='\t',
    raise_on_none: bool = False
) -> None:
    """Write the graph as an edgelist.

    :param graph: A BEL graph
    :param path: A path or file-like
    :param use_tqdm: Should a progress bar be shown?
    :param sep: The separator to use
    :param raise_on_none: Should an exception be raised if no triples are returned?
    :raises: NoTriplesValueError
    """
    for h, r, t in to_triples(graph, use_tqdm=use_tqdm, raise_on_none=raise_on_none):
        print(h, t, json.dumps(dict(relation=r)), sep=sep, file=path) 
Example #13
Source File: api.py    From pybel with MIT License 6 votes vote down vote up
def to_triples_file(
    graph: BELGraph,
    path: Union[str, TextIO],
    *,
    use_tqdm: bool = False,
    sep='\t',
    raise_on_none: bool = False
) -> None:
    """Write the graph as a TSV.

    :param graph: A BEL graph
    :param path: A path or file-like
    :param use_tqdm: Should a progress bar be shown?
    :param sep: The separator to use
    :param raise_on_none: Should an exception be raised if no triples are returned?
    :raises: NoTriplesValueError
    """
    for h, r, t in to_triples(graph, use_tqdm=use_tqdm, raise_on_none=raise_on_none):
        print(h, r, t, sep=sep, file=path) 
Example #14
Source File: yaml.py    From dephell with MIT License 6 votes vote down vote up
def yaml_load(stream: TextIO, *, safe: bool = True):
    if safe:
        parser = ruamel_yaml.YAML(typ='safe')
    else:
        parser = ruamel_yaml.YAML()

    # first of all, try to parse by ruamel.yaml
    try:
        return parser.load(stream)
    except Exception:
        pass

    # on error try to parse by PyYAML
    if safe:
        return py_yaml.safe_load(stream)
    return py_yaml.load(stream) 
Example #15
Source File: textProcessors.py    From armi with Apache License 2.0 5 votes vote down vote up
def _getRootFromSrc(
    src: Union[TextIO, pathlib.Path], root: Optional[pathlib.Path]
) -> pathlib.Path:
    if isinstance(src, pathlib.Path):
        root = root or src.parent.absolute()
    elif isinstance(src, io.TextIOBase):
        if root is None:
            raise ValueError("A stream was provided without a root directory.")
    else:
        raise TypeError("Unsupported source type: `{}`!".format(type(src)))

    return root 
Example #16
Source File: utils.py    From cmdstanpy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def scan_column_names(fd: TextIO, config_dict: Dict, lineno: int) -> int:
    """
    Process columns header, add to config_dict as 'column_names'
    """
    line = fd.readline().strip()
    lineno += 1
    names = line.split(',')
    config_dict['column_names'] = tuple(names)
    config_dict['num_params'] = len(names) - 1
    return lineno 
Example #17
Source File: utils.py    From cmdstanpy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def scan_config(fd: TextIO, config_dict: Dict, lineno: int) -> int:
    """
    Scan initial stan_csv file comments lines and
    save non-default configuration information to config_dict.
    """
    cur_pos = fd.tell()
    line = fd.readline().strip()
    while len(line) > 0 and line.startswith('#'):
        lineno += 1
        if not line.endswith('(Default)'):
            line = line.lstrip(' #\t')
            key_val = line.split('=')
            if len(key_val) == 2:
                if key_val[0].strip() == 'file' and not key_val[1].endswith(
                    'csv'
                ):
                    config_dict['data_file'] = key_val[1].strip()
                elif key_val[0].strip() != 'file':
                    raw_val = key_val[1].strip()
                    try:
                        val = int(raw_val)
                    except ValueError:
                        try:
                            val = float(raw_val)
                        except ValueError:
                            val = raw_val
                    config_dict[key_val[0].strip()] = val
        cur_pos = fd.tell()
        line = fd.readline().strip()
    fd.seek(cur_pos)
    return lineno 
Example #18
Source File: test_capture.py    From pytest with MIT License 5 votes vote down vote up
def test_py36_windowsconsoleio_workaround_non_standard_streams() -> None:
    """
    Ensure _py36_windowsconsoleio_workaround function works with objects that
    do not implement the full ``io``-based stream protocol, for example execnet channels (#2666).
    """
    from _pytest.capture import _py36_windowsconsoleio_workaround

    class DummyStream:
        def write(self, s):
            pass

    stream = cast(TextIO, DummyStream())
    _py36_windowsconsoleio_workaround(stream) 
Example #19
Source File: output_generator.py    From sslyze with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, file_to: TextIO) -> None:
        self._file_to = file_to 
Example #20
Source File: textProcessors.py    From armi with Apache License 2.0 5 votes vote down vote up
def findYamlInclusions(
    src: Union[TextIO, pathlib.Path], root: Optional[pathlib.Path] = None
) -> List[Tuple[pathlib.Path, FileMark]]:
    """
    Return a list containing all of the !included YAML files from a root file.

    This will attempt to "normalize" relative paths to the passed root. If that is not
    possible, then an absolute path will be used instead. For example, if a file (A)
    !includes another file (B) by an absolute path, which in turn !includes more files
    relative to (B), all of (B)'s relative includes will be turned into absolute paths
    from the perspective of the root file (A).
    """
    includes = _resolveMarkupInclusions(src, root)[1]
    root = _getRootFromSrc(src, root)
    normalizedIncludes = []

    for path, mark in includes:
        if not path.is_absolute():
            try:
                path = (mark.relativeTo / path).relative_to(root or os.getcwd())
            except ValueError as _:
                # Can't make a relative path. IMO, pathlib gives up a little too early,
                # but we still probably want to decay to absolute paths if the files
                # arent in the same tree.
                path = (mark.relativeTo / path).absolute()

        normalizedIncludes.append((path, mark))

    return normalizedIncludes 
Example #21
Source File: terminalwriter.py    From pytest with MIT License 5 votes vote down vote up
def should_do_markup(file: TextIO) -> bool:
    if os.environ.get("PY_COLORS") == "1":
        return True
    if os.environ.get("PY_COLORS") == "0":
        return False
    return (
        hasattr(file, "isatty")
        and file.isatty()
        and os.environ.get("TERM") != "dumb"
        and not (sys.platform.startswith("java") and os._name == "nt")
    ) 
Example #22
Source File: lm_dataset.py    From BERT-keras with GNU General Public License v3.0 5 votes vote down vote up
def _grab_line(files: List[TextIO], file_size: int, jump_prob: float) -> str:
    file = files[random.randrange(len(files))]
    if random.random() < jump_prob:
        file.seek(random.randrange(file_size))
        file.readline()  # discard - bound to be partial line
    random_line = file.readline()
    if len(random_line) == 0:  # we have hit the end
        file.seek(0)
        random_line = file.readline()
    return random_line 
Example #23
Source File: options.py    From teleport with Apache License 2.0 5 votes vote down vote up
def print_help(file: TextIO = None) -> None:
    """Prints all the command line options to stderr (or another file).

    See `OptionParser.print_help`.
    """
    return options.print_help(file) 
Example #24
Source File: options.py    From teleport with Apache License 2.0 5 votes vote down vote up
def print_help(self, file: TextIO = None) -> None:
        """Prints all the command line options to stderr (or another file)."""
        if file is None:
            file = sys.stderr
        print("Usage: %s [OPTIONS]" % sys.argv[0], file=file)
        print("\nOptions:\n", file=file)
        by_group = {}  # type: Dict[str, List[_Option]]
        for option in self._options.values():
            by_group.setdefault(option.group_name, []).append(option)

        for filename, o in sorted(by_group.items()):
            if filename:
                print("\n%s options:\n" % os.path.normpath(filename), file=file)
            o.sort(key=lambda option: option.name)
            for option in o:
                # Always print names with dashes in a CLI context.
                prefix = self._normalize_name(option.name)
                if option.metavar:
                    prefix += "=" + option.metavar
                description = option.help or ""
                if option.default is not None and option.default != "":
                    description += " (default %s)" % option.default
                lines = textwrap.wrap(description, 79 - 35)
                if len(prefix) > 30 or len(lines) == 0:
                    lines.insert(0, "")
                print("  --%-30s %s" % (prefix, lines[0]), file=file)
                for line in lines[1:]:
                    print("%-34s %s" % (" ", line), file=file)
        print(file=file) 
Example #25
Source File: template.py    From teleport with Apache License 2.0 5 votes vote down vote up
def __init__(
        self,
        file: TextIO,
        named_blocks: Dict[str, _NamedBlock],
        loader: Optional[BaseLoader],
        current_template: Template,
    ) -> None:
        self.file = file
        self.named_blocks = named_blocks
        self.loader = loader
        self.current_template = current_template
        self.apply_counter = 0
        self.include_stack = []  # type: List[Tuple[Template, int]]
        self._indent = 0 
Example #26
Source File: stats.py    From aries-cloudagent-python with Apache License 2.0 5 votes vote down vote up
def __init__(self, *, enabled: bool = True, log_path: str = None):
        """Initialize the Collector instance."""
        self._enabled = enabled
        self._log_file: TextIO = None
        self._log_path = log_path
        self._stats = None
        self.reset() 
Example #27
Source File: anim_encoder.py    From pycozmo with MIT License 5 votes vote down vote up
def from_json_stream(cls, f: TextIO):
        data = json.load(f)
        clips = cls.from_dict(data)
        return clips 
Example #28
Source File: anim_encoder.py    From pycozmo with MIT License 5 votes vote down vote up
def to_json_stream(self, f: TextIO) -> None:
        data = self.to_dict()
        json.dump(data, f, indent=2, separators=(",", ": ")) 
Example #29
Source File: DOTParser.py    From Graphvizer with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, input:TokenStream, output:TextIO = sys.stdout):
        super().__init__(input, output)
        self.checkVersion("4.8")
        self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache)
        self._predicates = None 
Example #30
Source File: visualization.py    From pybel with MIT License 5 votes vote down vote up
def to_html_file(
    graph: BELGraph,
    file: Union[str, TextIO],
    color_map: Optional[Mapping[str, str]] = None,
) -> None:
    """Write the HTML visualization to a file or file-like.

    :param graph: A BEL graph
    :param color_map: A dictionary from PyBEL internal node functions to CSS color strings like #FFEE00.
                    Defaults to :data:`default_color_map`
    :param file file: A writable file or file-like or file path
    """
    print(to_html(graph, color_map=color_map), file=file)