當前位置: 首頁>>代碼示例>>Python>>正文


Python BinaryIO.readline方法代碼示例

本文整理匯總了Python中typing.BinaryIO.readline方法的典型用法代碼示例。如果您正苦於以下問題:Python BinaryIO.readline方法的具體用法?Python BinaryIO.readline怎麽用?Python BinaryIO.readline使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在typing.BinaryIO的用法示例。


在下文中一共展示了BinaryIO.readline方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: parse_header

# 需要導入模塊: from typing import BinaryIO [as 別名]
# 或者: from typing.BinaryIO import readline [as 別名]
def parse_header(source: BinaryIO) -> Tuple[OFXHeaderType, str]:
    """
    Consume source; feed to appropriate class constructor which performs
    validation/type conversion on OFX header.

    Using header, locate/read/decode (but do not parse) OFX data body.

    Returns a 2-tuple of:
        * instance of OFXHeaderV1/OFXHeaderV2 containing parsed data, and
        * decoded text of OFX data body
    """
    # Skip any empty lines at the beginning
    while True:
        # OFX header is read by nice clean machines, not meatbags -
        # should not contain emoji, 漢字, or what have you.
        line = source.readline().decode("ascii")
        if line.strip():
            break

    # If the first non-empty line contains an XML declaration, it's OFX v2
    xml_match = XML_REGEX.match(line)
    if xml_match:
        # OFXv2 spec doesn't require newlines between XML declaration,
        # OFX declaration, and data elements; `line` may or may not
        # contain the latter two.
        #
        # Just rewind, read the whole file (it must be UTF-8 encoded per
        # the spec) and slice the OFX data body from the end of the
        # OFX declaration
        source.seek(0)
        decoded_source = source.read().decode(OFXHeaderV2.codec)
        header, header_end_index = OFXHeaderV2.parse(decoded_source)
        message = decoded_source[header_end_index:]
    else:
        # OFX v1
        rawheader = line + "\n"
        # First line is OFXHEADER; need to read next 8 lines for a fixed
        # total of 9 fields required by OFX v1 spec.
        for n in range(8):
            rawheader += source.readline().decode("ascii")
        header, header_end_index = OFXHeaderV1.parse(rawheader)

        #  Input source stream position has advanced to the beginning of
        #  the OFX body tag soup, which is where subsequent calls
        #  to read()/readlines() will pick up.
        #
        #  Decode the OFX data body according to the encoding declared
        #  in the OFX header
        message = source.read().decode(header.codec)

    return header, message.strip()
開發者ID:csingley,項目名稱:ofxtools,代碼行數:53,代碼來源:header.py

示例2: generate_reports

# 需要導入模塊: from typing import BinaryIO [as 別名]
# 或者: from typing.BinaryIO import readline [as 別名]
def generate_reports(report_template: Report, infile: BinaryIO, chunk_size: Optional[int],
                     copy_header_line: bool) -> Generator[Report, None, None]:
    """Generate reports from a template and input file, optionally split into chunks.

    If chunk_size is None, a single report is generated with the entire
    contents of infile as the raw data. Otherwise chunk_size should be
    an integer giving the maximum number of bytes in a chunk. The data
    read from infile is then split into chunks of this size at newline
    characters (see read_delimited_chunks). For each of the chunks, this
    function yields a copy of the report_template with that chunk as the
    value of the raw attribute.

    When splitting the data into chunks, if copy_header_line is true,
    the first line the file is read before chunking and then prepended
    to each of the chunks. This is particularly useful when splitting
    CSV files.

    The infile should be a file-like object. generate_reports uses only
    two methods, readline and read, with readline only called once and
    only if copy_header_line is true. Both methods should return bytes
    objects.

    Params:
        report_template: report used as template for all yielded copies
        infile: stream to read from
        chunk_size: maximum size of each chunk
        copy_header_line: copy the first line of the infile to each chunk

    Yields:
        report: a Report object holding the chunk in the raw field
    """
    if chunk_size is None:
        report = report_template.copy()
        data = infile.read()
        if data:
            report.add("raw", data, overwrite=True)
            yield report
    else:
        header = b""
        if copy_header_line:
            header = infile.readline()
        for chunk in read_delimited_chunks(infile, chunk_size):
            report = report_template.copy()
            report.add("raw", header + chunk, overwrite=True)
            yield report
開發者ID:certtools,項目名稱:intelmq,代碼行數:47,代碼來源:splitreports.py

示例3: stuff

# 需要導入模塊: from typing import BinaryIO [as 別名]
# 或者: from typing.BinaryIO import readline [as 別名]
 def stuff(a: BinaryIO) -> bytes:
     return a.readline()
開發者ID:Alkalit,項目名稱:cpython,代碼行數:4,代碼來源:test_typing.py

示例4: find_offset

# 需要導入模塊: from typing import BinaryIO [as 別名]
# 或者: from typing.BinaryIO import readline [as 別名]
def find_offset(archive: BinaryIO) -> int:
    return obfuscation_offset(archive.readline().split()[-1])
開發者ID:Lattyware,項目名稱:unrpa,代碼行數:4,代碼來源:zix.py

示例5: find_offset_and_key

# 需要導入模塊: from typing import BinaryIO [as 別名]
# 或者: from typing.BinaryIO import readline [as 別名]
 def find_offset_and_key(self, archive: BinaryIO) -> Tuple[int, Optional[int]]:
     line = archive.readline()
     parts = line.split()
     offset = int(parts[1], 16)
     key = int(parts[2], 16)
     return offset, key
開發者ID:Lattyware,項目名稱:unrpa,代碼行數:8,代碼來源:rpa.py


注:本文中的typing.BinaryIO.readline方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。