Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / Python

io.BufferedIOBase

Základná trieda opisuje binárne prúdy s vyrovnávacou pamäťou.

Verzia: Python 3.14; repository image, production runtime not attested

Syntax

io.BufferedIOBase()

Základná trieda opisuje binárne prúdy s vyrovnávacou pamäťou.

Zatváranie sa dedí z IOBase. Konkrétny prúd implementuje podporované operácie spomedzi read, read1, write a detach. Poskytnuté metódy readinto a readinto1 naplnia odovzdanú zapisovateľnú vyrovnávaciu pamäť a vrátia počet zapísaných bajtov. readable() a writable() určujú dostupné operácie. Pri b'abc' vráti BufferedReader po opakovanom read(2) postupne b'ab', b'c' a b''. Posledný blok môže byť kratší. read(0) vracia b'' aj pred koncom.

Zápis do tejto čítačky vyvolá io.UnsupportedOperation.

import io
with io.BufferedReader(io.BytesIO(b'xy')) as reader:
    assert reader.read(0)==b'' and reader.read(1)==b'x'
    assert reader.read(10)==b'y' and reader.read(1)==b''
    try: reader.write(b'z')
    except io.UnsupportedOperation: pass
    else: raise AssertionError
with io.BufferedWriter(io.BytesIO()) as writer:
    assert isinstance(writer, io.BufferedIOBase) and writer.write(b'x')==1
with io.BufferedReader(io.BytesIO(b'xyz')) as reader:
    target=bytearray(2)
    assert reader.readinto(target)==2 and target==b'xy'
    assert reader.readinto1(target)==1 and target==b'zy'
    assert reader.readinto(target)==0

Príklady

import io
with io.BufferedReader(io.BytesIO(b"abc")) as stream:
    print(isinstance(stream, io.BufferedIOBase))
    print(stream.read(2), stream.read(2), stream.read(2))

Výsledok

True
b'ab' b'c' b''

Zápis do tejto čítačky vyvolá io.UnsupportedOperation.

Zdroje