Python io.TextIOBase() Examples
The following are 30
code examples of io.TextIOBase().
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
io
, or try the search function
.
Example #1
Source File: test_copy.py From syntheticmass with Apache License 2.0 | 6 votes |
def test_copy_from_propagate_error(self): class BrokenRead(_base): def read(self, size): return 1/0 def readline(self): return 1/0 curs = self.conn.cursor() # It seems we cannot do this, but now at least we propagate the error # self.assertRaises(ZeroDivisionError, # curs.copy_from, BrokenRead(), "tcopy") try: curs.copy_from(BrokenRead(), "tcopy") except Exception, e: self.assert_('ZeroDivisionError' in str(e))
Example #2
Source File: ssafile.py From bazarr with GNU General Public License v3.0 | 6 votes |
def to_file(self, fp, format_, fps=None, **kwargs): """ Write subtitle file to file object. See :meth:`SSAFile.save()` for full description. Note: This is a low-level method. Usually, one of :meth:`SSAFile.save()` or :meth:`SSAFile.to_string()` is preferable. Arguments: fp (file object): A file object, ie. :class:`io.TextIOBase` instance. Note that the file must be opened in text mode (as opposed to binary). """ impl = get_format_class(format_) impl.to_file(self, fp, format_, fps=fps, **kwargs) # ------------------------------------------------------------------------ # Retiming subtitles # ------------------------------------------------------------------------
Example #3
Source File: avahi.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def _observe_mdns(reader, output: io.TextIOBase, verbose: bool): """Process the given `reader` for `avahi-browse` events. IO is mostly isolated in this function; the transformation functions `_observe_all_in_full` and `_observe_resolver_found` can be tested without having to deal with IO. :param reader: A context-manager yielding a `io.TextIOBase`. """ if verbose: observer = _observe_all_in_full else: observer = _observe_resolver_found with reader as infile: events = _extract_mdns_events(infile) for event in observer(events): print(json.dumps(event), file=output, flush=True)
Example #4
Source File: util.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def fileobj_is_binary(f): """ Returns True if the give file or file-like object has a file open in binary mode. When in doubt, returns True by default. """ # This is kind of a hack for this to work correctly with _File objects, # which, for the time being, are *always* binary if hasattr(f, 'binary'): return f.binary if isinstance(f, io.TextIOBase): return False mode = fileobj_mode(f) if mode: return 'b' in mode else: return True
Example #5
Source File: api.py From ppci with BSD 2-Clause "Simplified" License | 6 votes |
def wasmcompile(source: io.TextIOBase, march, opt_level=2, reporter=None): """ Webassembly compile """ march = get_arch(march) if not reporter: # pragma: no cover reporter = DummyReportGenerator() wasm_module = read_wasm(source) ir_module = wasm_to_ir( wasm_module, march.info.get_type_info("ptr"), reporter=reporter ) # Optimize: optimize(ir_module, level=opt_level) obj = ir_to_object([ir_module], march, reporter=reporter) return obj
Example #6
Source File: update.py From alertR with GNU Affero General Public License v3.0 | 5 votes |
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str: """ Internal function that calculates the sha256 hash of the file. :param fileHandle: file handle for which the hash should be created :return: sha256 hash digest """ fileHandle.seek(0) sha256 = hashlib.sha256() while True: data = fileHandle.read(128) if not data: break sha256.update(data) return sha256.hexdigest()
Example #7
Source File: base.py From core with Apache License 2.0 | 5 votes |
def __init__(self, size, *args): self.maxsize = size io.TextIOBase.__init__(self, *args) self.deque = collections.deque()
Example #8
Source File: update.py From alertR with GNU Affero General Public License v3.0 | 5 votes |
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str: """ Internal function that calculates the sha256 hash of the file. :param fileHandle: file handle for which the hash should be created :return: sha256 hash digest """ fileHandle.seek(0) sha256 = hashlib.sha256() while True: data = fileHandle.read(128) if not data: break sha256.update(data) return sha256.hexdigest()
Example #9
Source File: update.py From alertR with GNU Affero General Public License v3.0 | 5 votes |
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str: """ Internal function that calculates the sha256 hash of the file. :param fileHandle: file handle for which the hash should be created :return: sha256 hash digest """ fileHandle.seek(0) sha256 = hashlib.sha256() while True: data = fileHandle.read(128) if not data: break sha256.update(data) return sha256.hexdigest()
Example #10
Source File: update.py From alertR with GNU Affero General Public License v3.0 | 5 votes |
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str: """ Internal function that calculates the sha256 hash of the file. :param fileHandle: file handle for which the hash should be created :return: sha256 hash digest """ fileHandle.seek(0) sha256 = hashlib.sha256() while True: data = fileHandle.read(128) if not data: break sha256.update(data) return sha256.hexdigest()
Example #11
Source File: update.py From alertR with GNU Affero General Public License v3.0 | 5 votes |
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str: """ Internal function that calculates the sha256 hash of the file. :param fileHandle: file handle for which the hash should be created :return: sha256 hash digest """ fileHandle.seek(0) sha256 = hashlib.sha256() while True: data = fileHandle.read(128) if not data: break sha256.update(data) return sha256.hexdigest()
Example #12
Source File: update.py From alertR with GNU Affero General Public License v3.0 | 5 votes |
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str: """ Internal function that calculates the sha256 hash of the file. :param fileHandle: file handle for which the hash should be created :return: sha256 hash digest """ fileHandle.seek(0) sha256 = hashlib.sha256() while True: data = fileHandle.read(128) if not data: break sha256.update(data) return sha256.hexdigest()
Example #13
Source File: update.py From alertR with GNU Affero General Public License v3.0 | 5 votes |
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str: """ Internal function that calculates the sha256 hash of the file. :param fileHandle: file handle for which the hash should be created :return: sha256 hash digest """ fileHandle.seek(0) sha256 = hashlib.sha256() while True: data = fileHandle.read(128) if not data: break sha256.update(data) return sha256.hexdigest()
Example #14
Source File: update.py From alertR with GNU Affero General Public License v3.0 | 5 votes |
def _sha256File(self, fileHandle: Union[io.TextIOBase, io.BufferedIOBase]) -> str: """ Internal function that calculates the sha256 hash of the file. :param fileHandle: file handle for which the hash should be created :return: sha256 hash digest """ fileHandle.seek(0) sha256 = hashlib.sha256() while True: data = fileHandle.read(128) if not data: break sha256.update(data) return sha256.hexdigest()
Example #15
Source File: __init__.py From pvl with BSD 3-Clause "New" or "Revised" License | 5 votes |
def dump(module, path, **kwargs): """Serialize *module* as PVL text to the provided *path*. :param module: a ``PVLModule`` or ``dict``-like object to serialize. :param path: an :class:`os.PathLike` :param ``**kwargs``: the keyword arguments to pass to :func:`dumps()`. If *path* is an :class:`os.PathLike`, it will attempt to be opened and the serialized module will be written into that file via the :func:`pathlib.Path.write_text()` function, and will return what that function returns. If *path* is not an :class:`os.PathLike`, it will be assumed to be an already-opened file object, and ``.write()`` will be applied on that object to write the serialized module, and will return what that function returns. """ try: p = Path(path) return p.write_text(dumps(module, **kwargs)) except TypeError: # Not an os.PathLike, maybe it is an already-opened file object try: if isinstance(path, io.TextIOBase): return path.write(dumps(module, **kwargs)) else: return path.write(dumps(module, **kwargs).encode()) except AttributeError: # Not a path, not an already-opened file. raise TypeError('Expected an os.PathLike or an already-opened ' 'file object for writing, but got neither.')
Example #16
Source File: ch14_ex5.py From Mastering-Object-Oriented-Python-Second-Edition with MIT License | 5 votes |
def load( self, file_name_or_path: Union[TextIO, str, Path] ) -> Iterator[Tuple[str, str]]: if isinstance(file_name_or_path, io.TextIOBase): return self.loads(file_name_or_path.read()) else: name_or_path = cast(Union[str, Path], file_name_or_path) with Path(name_or_path).open("r") as file: return self.loads(file.read())
Example #17
Source File: api.py From ppci with BSD 2-Clause "Simplified" License | 5 votes |
def cc( source: io.TextIOBase, march, coptions=None, opt_level=0, debug=False, reporter=None, ): """ C compiler. compiles a single source file into an object file. Args: source: file like object from which text can be read march: The architecture for which to compile coptions: options for the C frontend debug: Create debug info when set to True Returns: an object file .. doctest:: >>> import io >>> from ppci.api import cc >>> source_file = io.StringIO("void main() { int a; }") >>> obj = cc(source_file, 'x86_64') >>> print(obj) CodeObject of 20 bytes """ if not reporter: # pragma: no cover reporter = DummyReportGenerator() if not coptions: coptions = COptions() ir_module = c_to_ir(source, march, coptions=coptions, reporter=reporter) reporter.message("{} {}".format(ir_module, ir_module.stats())) reporter.dump_ir(ir_module) optimize(ir_module, level=opt_level, reporter=reporter) return ir_to_object([ir_module], march, debug=debug, reporter=reporter)
Example #18
Source File: api.py From ppci with BSD 2-Clause "Simplified" License | 5 votes |
def c_to_ir(source: io.TextIOBase, march, coptions=None, reporter=None): """ C to ir translation. Args: source (file-like object): The C source to compile. march (str): The targetted architecture. coptions: C specific compilation options. Returns: An :class:`ppci.ir.Module`. """ if not reporter: # pragma: no cover reporter = DummyReportGenerator() if not coptions: # pragma: no cover coptions = COptions() from ...api import get_arch march = get_arch(march) cbuilder = CBuilder(march.info, coptions) assert isinstance(source, io.TextIOBase) if hasattr(source, "name"): filename = getattr(source, "name") else: filename = None ir_module = cbuilder.build(source, filename, reporter=reporter) return ir_module
Example #19
Source File: builder.py From ppci with BSD 2-Clause "Simplified" License | 5 votes |
def build(self, src: io.TextIOBase, filename: str, reporter=None): if reporter: reporter.heading(2, "C builder") reporter.message( "Welcome to the C building report for {}".format(filename) ) cdialect = self.coptions["std"] self.logger.info("Starting C compilation (%s)", cdialect) context = CContext(self.coptions, self.arch_info) compile_unit = _parse(src, filename, context) if reporter: f = io.StringIO() print_ast(compile_unit, file=f) reporter.dump_source("C-ast", f.getvalue()) cgen = CCodeGenerator(context) return cgen.gen_code(compile_unit)
Example #20
Source File: test_io.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_misc(self): shell = MockShell() f = PseudoInputFile(shell, 'stdin', 'utf-8') self.assertIsInstance(f, io.TextIOBase) self.assertEqual(f.encoding, 'utf-8') self.assertIsNone(f.errors) self.assertIsNone(f.newlines) self.assertEqual(f.name, '<stdin>') self.assertFalse(f.closed) self.assertTrue(f.isatty()) self.assertTrue(f.readable()) self.assertFalse(f.writable()) self.assertFalse(f.seekable())
Example #21
Source File: test_io.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_misc(self): shell = MockShell() f = PseudoOutputFile(shell, 'stdout', 'utf-8') self.assertIsInstance(f, io.TextIOBase) self.assertEqual(f.encoding, 'utf-8') self.assertIsNone(f.errors) self.assertIsNone(f.newlines) self.assertEqual(f.name, '<stdout>') self.assertFalse(f.closed) self.assertTrue(f.isatty()) self.assertFalse(f.readable()) self.assertTrue(f.writable()) self.assertFalse(f.seekable())
Example #22
Source File: test_pathlib.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_open_common(self): p = self.cls(BASE) with (p / 'fileA').open('r') as f: self.assertIsInstance(f, io.TextIOBase) self.assertEqual(f.read(), "this is file A\n") with (p / 'fileA').open('rb') as f: self.assertIsInstance(f, io.BufferedIOBase) self.assertEqual(f.read().strip(), b"this is file A") with (p / 'fileA').open('rb', buffering=0) as f: self.assertIsInstance(f, io.RawIOBase) self.assertEqual(f.read().strip(), b"this is file A")
Example #23
Source File: saxutils.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def _gettextwriter(out, encoding): if out is None: import sys return sys.stdout if isinstance(out, io.TextIOBase): # use a text writer as is return out if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)): # use a codecs stream writer as is return out # wrap a binary writer with TextIOWrapper if isinstance(out, io.RawIOBase): # Keep the original file open when the TextIOWrapper is # destroyed class _wrapper: __class__ = out.__class__ def __getattr__(self, name): return getattr(out, name) buffer = _wrapper() buffer.close = lambda: None else: # This is to handle passed objects that aren't in the # IOBase hierarchy, but just have a write method buffer = io.BufferedIOBase() buffer.writable = lambda: True buffer.write = out.write try: # TextIOWrapper uses this methods to determine # if BOM (for UTF-16, etc) should be added buffer.seekable = out.seekable buffer.tell = out.tell except AttributeError: pass return io.TextIOWrapper(buffer, encoding=encoding, errors='xmlcharrefreplace', newline='\n', write_through=True)
Example #24
Source File: test_io.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def test_misc(self): shell = MockShell() f = PseudoInputFile(shell, 'stdin', 'utf-8') self.assertIsInstance(f, io.TextIOBase) self.assertEqual(f.encoding, 'utf-8') self.assertIsNone(f.errors) self.assertIsNone(f.newlines) self.assertEqual(f.name, '<stdin>') self.assertFalse(f.closed) self.assertTrue(f.isatty()) self.assertTrue(f.readable()) self.assertFalse(f.writable()) self.assertFalse(f.seekable())
Example #25
Source File: test_io.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def test_misc(self): shell = MockShell() f = PseudoOutputFile(shell, 'stdout', 'utf-8') self.assertIsInstance(f, io.TextIOBase) self.assertEqual(f.encoding, 'utf-8') self.assertIsNone(f.errors) self.assertIsNone(f.newlines) self.assertEqual(f.name, '<stdout>') self.assertFalse(f.closed) self.assertTrue(f.isatty()) self.assertFalse(f.readable()) self.assertTrue(f.writable()) self.assertFalse(f.seekable())
Example #26
Source File: __init__.py From orbdetpy with GNU General Public License v3.0 | 5 votes |
def write_output_file(outfile, data): if (isinstance(outfile, str)): with open(outfile, "w") as fp: if (isinstance(data, str)): fp.write(data) else: json.dump(data, fp) elif (isinstance(outfile, io.TextIOBase)): if (isinstance(data, str)): outfile.write(data) else: json.dump(data, outfile)
Example #27
Source File: __init__.py From orbdetpy with GNU General Public License v3.0 | 5 votes |
def read_param(param): if (isinstance(param, str)): if (path.isfile(param)): with open(param, "r") as fp: data = json.load(fp) else: data = json.loads(param) elif (isinstance(param, io.TextIOBase)): data = json.load(param) else: data = param return(data)
Example #28
Source File: cleaner.py From google-drive-trash-cleaner with GNU General Public License v3.0 | 5 votes |
def __init__(self, unsafeTextFile, error): if not isinstance(unsafeTextFile, io.TextIOBase): raise TypeError() self.unsafeTextFile = unsafeTextFile self.encoding = unsafeTextFile.encoding self.error = error
Example #29
Source File: content.py From akagi with MIT License | 5 votes |
def decoded_body(self): if six.PY2: return self.body else: if isinstance(self.body, io.TextIOBase): return self.body else: return six.StringIO(self.body.read().decode('utf-8'))
Example #30
Source File: saxutils.py From jawfish with MIT License | 5 votes |
def _gettextwriter(out, encoding): if out is None: import sys return sys.stdout if isinstance(out, io.TextIOBase): # use a text writer as is return out # wrap a binary writer with TextIOWrapper if isinstance(out, io.RawIOBase): # Keep the original file open when the TextIOWrapper is # destroyed class _wrapper: __class__ = out.__class__ def __getattr__(self, name): return getattr(out, name) buffer = _wrapper() buffer.close = lambda: None else: # This is to handle passed objects that aren't in the # IOBase hierarchy, but just have a write method buffer = io.BufferedIOBase() buffer.writable = lambda: True buffer.write = out.write try: # TextIOWrapper uses this methods to determine # if BOM (for UTF-16, etc) should be added buffer.seekable = out.seekable buffer.tell = out.tell except AttributeError: pass return io.TextIOWrapper(buffer, encoding=encoding, errors='xmlcharrefreplace', newline='\n', write_through=True)