当前位置: 首页>>代码示例>>Python>>正文


Python BufferedReader.read方法代码示例

本文整理汇总了Python中io.BufferedReader.read方法的典型用法代码示例。如果您正苦于以下问题:Python BufferedReader.read方法的具体用法?Python BufferedReader.read怎么用?Python BufferedReader.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.BufferedReader的用法示例。


在下文中一共展示了BufferedReader.read方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: tst_io_bufferedreader

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
    def tst_io_bufferedreader(self):

        fp = self._fake_fp(b'foo')
        #fp = BytesIO(b'foo')
        resp = HTTPResponse(fp, preload_content=False)
        br = BufferedReader(resp)

        self.assertEqual(br.read(), b'foo')

        br.close()
        self.assertEqual(resp.closed, True)

        b = b'fooandahalf'
        fp = self._fake_fp(b)
        #fp = BytesIO(b)
        resp = HTTPResponse(fp, preload_content=False)
        br = BufferedReader(resp, 5)

        br.read(1)  # sets up the buffer, reading 5
        self.assertEqual(len(fp.read()), len(b) - 5)

        # This is necessary to make sure the "no bytes left" part of `readinto`
        # gets tested.
        while not br.closed:
            br.read(5)
开发者ID:rdbhost,项目名称:yieldfromUrllib3,代码行数:27,代码来源:test_response.py

示例2: load

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
    def load(cls, filename, metadata_only=False):
        """
        Load ring data from a file.

        :param filename: Path to a file serialized by the save() method.
        :param bool metadata_only: If True, only load `devs` and `part_shift`.
        :returns: A RingData instance containing the loaded data.
        """
        gz_file = BufferedReader(GzipFile(filename, 'rb'))

        # See if the file is in the new format
        magic = gz_file.read(4)
        if magic == b'R1NG':
            format_version, = struct.unpack('!H', gz_file.read(2))
            if format_version == 1:
                ring_data = cls.deserialize_v1(
                    gz_file, metadata_only=metadata_only)
            else:
                raise Exception('Unknown ring format version %d' %
                                format_version)
        else:
            # Assume old-style pickled ring
            gz_file.seek(0)
            ring_data = pickle.load(gz_file)

        if not hasattr(ring_data, 'devs'):
            ring_data = RingData(ring_data['replica2part2dev_id'],
                                 ring_data['devs'], ring_data['part_shift'],
                                 ring_data.get('next_part_power'))
        return ring_data
开发者ID:mahak,项目名称:swift,代码行数:32,代码来源:ring.py

示例3: test_io_bufferedreader

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
    def test_io_bufferedreader(self):
        fp = BytesIO(b'foo')
        resp = HTTPResponse(fp, preload_content=False)
        br = BufferedReader(resp)

        self.assertEqual(br.read(), b'foo')

        br.close()
        self.assertEqual(resp.closed, True)
开发者ID:Mofangbao,项目名称:urllib3,代码行数:11,代码来源:test_response.py

示例4: test_io_bufferedreader

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
    def test_io_bufferedreader(self):
        fp = BytesIO(b'foo')
        resp = HTTPResponse(fp, preload_content=False)
        br = BufferedReader(resp)

        assert br.read() == b'foo'

        br.close()
        assert resp.closed

        b = b'fooandahalf'
        fp = BytesIO(b)
        resp = HTTPResponse(fp, preload_content=False)
        br = BufferedReader(resp, 5)

        br.read(1)  # sets up the buffer, reading 5
        assert len(fp.read()) == (len(b) - 5)

        # This is necessary to make sure the "no bytes left" part of `readinto`
        # gets tested.
        while not br.closed:
            br.read(5)
开发者ID:jonparrott,项目名称:urllib3,代码行数:24,代码来源:test_response.py

示例5: __init__

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
class BinaryReader:
    """
    Small utility class to read binary data.
    Also creates a "Memory Stream" if necessary
    """

    def __init__(self, data=None, stream=None):
        if data:
            self.stream = BytesIO(data)
        elif stream:
            self.stream = stream
        else:
            raise ValueError('Either bytes or a stream must be provided')

        self.reader = BufferedReader(self.stream)
        self._last = None  # Should come in handy to spot -404 errors

    # region Reading

    # "All numbers are written as little endian."
    # https://core.telegram.org/mtproto
    def read_byte(self):
        """Reads a single byte value."""
        return self.read(1)[0]

    def read_int(self, signed=True):
        """Reads an integer (4 bytes) value."""
        return int.from_bytes(self.read(4), byteorder='little', signed=signed)

    def read_long(self, signed=True):
        """Reads a long integer (8 bytes) value."""
        return int.from_bytes(self.read(8), byteorder='little', signed=signed)

    def read_float(self):
        """Reads a real floating point (4 bytes) value."""
        return unpack('<f', self.read(4))[0]

    def read_double(self):
        """Reads a real floating point (8 bytes) value."""
        return unpack('<d', self.read(8))[0]

    def read_large_int(self, bits, signed=True):
        """Reads a n-bits long integer value."""
        return int.from_bytes(
            self.read(bits // 8), byteorder='little', signed=signed)

    def read(self, length=None):
        """Read the given amount of bytes."""
        if length is None:
            return self.reader.read()

        result = self.reader.read(length)
        if len(result) != length:
            raise BufferError(
                'No more data left to read (need {}, got {}: {}); last read {}'
                .format(length, len(result), repr(result), repr(self._last))
            )

        self._last = result
        return result

    def get_bytes(self):
        """Gets the byte array representing the current buffer as a whole."""
        return self.stream.getvalue()

    # endregion

    # region Telegram custom reading

    def tgread_bytes(self):
        """
        Reads a Telegram-encoded byte array, without the need of
        specifying its length.
        """
        first_byte = self.read_byte()
        if first_byte == 254:
            length = self.read_byte() | (self.read_byte() << 8) | (
                self.read_byte() << 16)
            padding = length % 4
        else:
            length = first_byte
            padding = (length + 1) % 4

        data = self.read(length)
        if padding > 0:
            padding = 4 - padding
            self.read(padding)

        return data

    def tgread_string(self):
        """Reads a Telegram-encoded string."""
        return str(self.tgread_bytes(), encoding='utf-8', errors='replace')

    def tgread_bool(self):
        """Reads a Telegram boolean value."""
        value = self.read_int(signed=False)
        if value == 0x997275b5:  # boolTrue
            return True
        elif value == 0xbc799737:  # boolFalse
#.........这里部分代码省略.........
开发者ID:leanderdulac,项目名称:Telethon,代码行数:103,代码来源:binaryreader.py

示例6: FileIO

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
    parser = argparse.ArgumentParser(description=help_msg)

    parser.add_argument('filename', help='input file')
    parser.add_argument('output', help='output file')
    parser.add_argument('convert_to', choices=['bin', 'txt'])
    
    args = parser.parse_args()
    
    embeddings = []
    
    if args.convert_to == 'txt':  # then format must be bin
        with FileIO(args.filename, 'rb') as f:
            reader = BufferedReader(f)
            vocab_size, dimension = map(int, f.readline().split())
            for _ in range(vocab_size):
                w = ''.join(takewhile(lambda x: x != ' ', (reader.read(1) for _ in count())))
                s = reader.read(4 * dimension)
                reader.read(1)  # end of line character
                arr = np.fromstring(s, dtype=np.float32)
                embeddings.append((w, arr))
            assert not reader.peek(1)
    else:
        with open(args.filename) as f:
            vocab_size, dimension = map(int, f.readline().split())
            for line in f:
                w, s = line.strip().split(' ', 1)
                arr = np.fromstring(s, dtype=np.float32, sep=' ')
                embeddings.append((w, arr))
            assert len(embeddings) == vocab_size

    if args.convert_to == 'txt':
开发者ID:akbari59,项目名称:multivec,代码行数:33,代码来源:convert-vectors.py

示例7: play

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
def play(output_dir : str, rom : io.BufferedReader) -> None:
    with open(os.path.join(output_dir, 'gameloader.js'), 'wb') as fh:
        fh.write(b"onDataLoad('" + base64.b64encode(rom.read()) + b"');")

    copytree(os.path.join(os.path.dirname(__file__), 'assets'),
             os.path.join(output_dir))
开发者ID:ebolyen,项目名称:q2-gba,代码行数:8,代码来源:plugin_setup.py

示例8: __init__

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
class Unpacker:
    def __init__(self, initial_bytes=None):
        self.initial_bytes = initial_bytes
        self.buf = BufferedReader(raw=BytesIO(initial_bytes))

    def struct_unpack(self, fmt):
        return struct.unpack(fmt, self.buf.read(struct.calcsize(fmt)))

    def read_byte(self):
        return self.struct_unpack('B')[0]

    def read_signed_byte(self):
        return self.struct_unpack('b')[0]

    def read_short(self, prefix=None):
        if prefix is None:
            prefix = self.read_byte()
        assert prefix in [Prefix.INT16, Prefix.UINT16], 'read_short expects prefix %s or %s but read %s' % (
            Prefix.INT16, Prefix.UINT16, prefix)
        return self.struct_unpack('>H' if prefix == Prefix.UINT16 else '>h')[0]

    def read_int(self, prefix=None):
        if prefix is None:
            prefix = self.read_byte()
        assert prefix in [Prefix.INT32, Prefix.UINT32], 'read_int expects prefix %s or %s but read %s' % (
            Prefix.INT32, Prefix.UINT32, prefix)
        return self.struct_unpack('>I' if prefix == Prefix.UINT32 else '>i')[0]

    def read_long(self, prefix=None):
        if prefix is None:
            prefix = self.read_byte()
        assert prefix in [Prefix.INT64, Prefix.UINT64], 'read_long expects prefix %s or %s but read %s' % (
            Prefix.INT64, Prefix.UINT64, prefix)
        return self.struct_unpack('>Q' if prefix == Prefix.UINT64 else '>q')[0]

    def read_float(self, prefix=None):
        if prefix is None:
            prefix = self.read_byte()
        assert prefix == Prefix.FLOAT32, 'read_float expects prefix %s but read %s' % (Prefix.FLOAT32, prefix)
        return self.struct_unpack('>f')[0]

    def read_double(self, prefix=None):
        if prefix is None:
            prefix = self.read_byte()
        assert prefix == Prefix.FLOAT64, 'read_double expects prefix %s but read %s' % (Prefix.FLOAT64, prefix)
        return self.struct_unpack('>d')[0]

    def read_payload(self, length):
        return self.buf.read(length)

    def peek(self):
        return struct.unpack('B', self.buf.peek()[0])[0]

    def unpack_nil(self):
        assert self.read_byte() == Prefix.NIL, 'unpack_nil expects to read %s' % Prefix.NIL

    def unpack_boolean(self):
        prefix = self.read_byte()
        assert prefix in [Prefix.TRUE, Prefix.FALSE], 'unpack_boolean expects prefix %s or %s but read %s' % (
            Prefix.TRUE, Prefix.FALSE, prefix)
        return prefix == Prefix.TRUE

    def unpack_int(self):
        prefix = self.peek()

        if Prefix.is_fix_int(prefix):
            return self.read_byte() if Prefix.is_pos_fix_int(prefix) else self.read_signed_byte()

        if prefix == Prefix.INT8:
            self.read_byte()
            return self.read_signed_byte()
        elif prefix == Prefix.UINT8:
            self.read_byte()
            return self.read_byte()
        elif prefix in [Prefix.UINT16, Prefix.INT16]:
            return self.read_short()
        elif prefix in [Prefix.UINT32, Prefix.INT32]:
            return self.read_int()
        elif prefix in [Prefix.UINT64, Prefix.INT64]:
            return self.read_long()
        else:
            raise ValueError('unexpected int prefix %s' % prefix)

    def unpack_float(self):
        return self.read_float()

    def unpack_double(self):
        return self.read_double()

    def unpack(self):
        prefix = self.peek()

        if Prefix.is_fix_int(prefix) or prefix in [Prefix.INT8, Prefix.UINT8, Prefix.INT16, Prefix.UINT16, Prefix.INT32,
                                                   Prefix.UINT32, Prefix.INT64, Prefix.UINT64]:
            return self.unpack_int()
        elif Prefix.is_fixed_array(prefix) or prefix in [Prefix.ARRAY16, Prefix.ARRAY32]:
            return self.unpack_array()
        elif Prefix.is_fixed_map(prefix) or prefix in [Prefix.MAP16, Prefix.MAP32]:
            return self.unpack_map()
        elif Prefix.is_fix_str(prefix) or prefix in [Prefix.STR8, Prefix.STR16, Prefix.STR32]:
#.........这里部分代码省略.........
开发者ID:polyglotted,项目名称:msgpack-python,代码行数:103,代码来源:unpacker.py

示例9: __init__

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
class Server:
    """
    classdocs
    """

    def __init__(self):
        """
        Constructor
        """
        # Read the configuration parameters from tulsi.conf
        self.timestr = time.strftime("%Y:%m:%d-%H:%M:%S")
        logging.basicConfig(filename="/var/log/tulsi.log", level=logging.DEBUG)
        try:
            self.conf = ConfigParser.ConfigParser()
            self.conf.read("/etc/tulsi/tulsi.conf")
            self.udp_ip = self.conf.get("tulsi", "host")
            self.udp_port = int(self.conf.get("tulsi", "port"))

            # printing the host and port of tulsi

            logging.info("%s The IP of the host: %s" % (self.timestr, self.udp_ip))
            logging.info("%s The  Port number of the host :%s" % (self.timestr, self.udp_port))
        except:
            # Error message of tulsi not working
            logging.error("The tulsi configuration file is not found")

            # Creating objects of MessageEncode and HostInfo
        msg_encode = MessageEncode()
        host_info = HostInfo()

        # Initializing empty lists
        self.drives = []
        self.service = []
        self.ip_array = []
        self.ring_ip = []
        self.ring_conf_ip = []
        self.ring_drives = []
        self.ip_set_array = []
        self.my_ring_conf = dict()
        # Read the ring Configuration file
        self.gz_file = GzipFile("/etc/swift/container.ring.gz", "rb")
        if hasattr(self.gz_file, "_checkReadable"):
            self.gz_file = BufferedReader(self.gz_file)
        magic = self.gz_file.read(4)
        if magic == "R1NG":
            version, = struct.unpack("!H", self.gz_file.read(2))
            if version == 1:
                self.ring_data = self.read_ring_file(self.gz_file)
            else:
                logging.error("%s Unknown ring format version %d" % (self.timestr, version))
                raise Exception("Unknown ring format version %d" % version)

        # While loop to continuously check the status of  swift services and
        # drives and send information to tulsi client
        while True:
            self.ip_array = host_info.read_ip()
            self.service = host_info.read_services()
            self.drives = host_info.read_drives(self.drives)
            self.message = msg_encode.create_message(
                self.my_ring_conf, self.ring_conf_ip, self.ip_array, self.service, self.drives
            )
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  # Internet  # UDP
            sock.sendto(self.message, (self.udp_ip, self.udp_port))
            time.sleep(5)
            self.ip_array = []
            self.service = []
            self.drives = []

    #  Function to extract the ip  and devices from container.ring.gz
    def read_ring_file(self, sgz_file):
        self.json_len, = struct.unpack("!I", self.gz_file.read(4))
        self.ring_dict = json.loads(self.gz_file.read(self.json_len))
        logging.info("Extracted Ring data : %s", self.ring_dict)
        self.ring_dict["replica2part2dev_id"] = []
        self.partition_count = 1 << (32 - self.ring_dict["part_shift"])
        # logging.info('%s The Ip from ring file %s' % (self.timestr, self.ring_conf_ip[]]))
        # logging.info('%s The IP of host machine %s' %(self.timestr, self.ip_array))
        for x in self.ring_dict["devs"]:
            self.mystring = x
            if self.mystring["ip"] in self.my_ring_conf:
                # append the new number to the existing array at this slot
                self.my_ring_conf[self.mystring["ip"]].append(self.mystring["device"])
            else:
                # create a new array in this slot
                self.my_ring_conf[self.mystring["ip"]] = [self.mystring["device"]]
                self.ring_conf_ip.append(self.mystring["ip"])
                logging.info("%s The Ip from ring file %s" % (self.timestr, self.ring_conf_ip))
                logging.info("%s The IP of host machine %s" % (self.timestr, self.ip_array))
开发者ID:sureshbabuja,项目名称:tulsi,代码行数:90,代码来源:Server.py

示例10: dataReceived

# 需要导入模块: from io import BufferedReader [as 别名]
# 或者: from io.BufferedReader import read [as 别名]
    def dataReceived(self, data):
        """
        Parse the NATS.io protocol from chunks of data streaming from
        the connected gnatsd.

        The server settings will be set and connect will be sent with this
        client's info upon an INFO, which should happen when the
        transport connects.

        Registered message callback functions will be called with MSGs
        once parsed.

        PONG will be called upon a ping.

        An exception will be raised upon an ERR from gnatsd.

        An +OK doesn't do anything.
        """
        if self.remaining_bytes:
            data = self.remaining_bytes + data
            self.remaining_bytes = b""

        data_buf = BufferedReader(BytesIO(data))
        while True:
            command = data_buf.read(4)
            if command == b"-ERR":
                raise NatsError(data_buf.read())
            elif command == b"+OK\r":
                val = data_buf.read(1)
                if val != b"\n":
                    self.remaining_bytes += command
                    break
            elif command == b"MSG ":
                val = data_buf.readline()
                if not val:
                    self.remaining_bytes += command
                    break
                if not val.endswith(b"\r\n"):
                    self.remaining_bytes += command + val
                    break

                meta_data = val.split(b" ")
                n_bytes = int(meta_data[-1])
                subject = meta_data[0].decode()
                if len(meta_data) == 4:
                    reply_to = meta_data[2].decode()
                elif len(meta_data) == 3:
                    reply_to = None
                else:
                    self.remaining_bytes += command + val
                    break

                sid = meta_data[1].decode()

                if sid in self.sids:
                    on_msg = self.sids[sid]
                else:
                    on_msg = self.on_msg

                payload = data_buf.read(n_bytes)
                if len(payload) != n_bytes:
                    self.remaining_bytes += command + val + payload
                    break

                if on_msg:
                    on_msg(nats_protocol=self, sid=sid, subject=subject, reply_to=reply_to, payload=payload)
                else:
                    stdout.write(command.decode())
                    stdout.write(val.decode())
                    stdout.write(payload.decode())

                payload_post = data_buf.readline()
                if payload_post != b"\r\n":
                    self.remaining_bytes += command + val + payload + payload_post
                    break
            elif command == b"PING":
                self.log.info("got PING")
                self.pong()
                val = data_buf.readline()
                if val != b"\r\n":
                    self.remaining_bytes += command + val
                    break
            elif command == b"PONG":
                self.pout -= 1
                val = data_buf.readline()
                if val != b"\r\n":
                    self.remaining_bytes += command + val
                    break
            elif command == b"INFO":
                val = data_buf.readline()
                if not val.endswith(b"\r\n"):
                    self.remaining_bytes += command + val
                    break
                settings = json.loads(val.decode("utf8"))
                self.server_settings = ServerInfo(**settings)
                self.log.info("{server_info}", server_info=settings)
                self.status = CONNECTED
                self.connect()
                if self.on_connect_d:
                    self.on_connect_d.callback(self)
#.........这里部分代码省略.........
开发者ID:johnwlockwood,项目名称:txnats,代码行数:103,代码来源:txnats.py


注:本文中的io.BufferedReader.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。