Python sys.stdin 模块,read() 实例源码

我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用sys.stdin.read()

项目:hide-ff-title-bar    作者:GKFX    | 项目源码 | 文件源码
def get_message():
    """Reads a JSON-ish message of a certain length."""

    raw_length = stdin.buffer.read(4)

    if not raw_length:
        raise EmptyMessage()

    length, *_ = unpack('@I', raw_length)
    message = stdin.read(length)
    return loads(message)
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def read(self, length = None):
        r'''Reads a sequence of one or more tokens from the underlying WBXML
            file, incrementing the file pointer accordingly.

            If the length is ommited, one token is read and returned as an
            integer; otherwise, at most (length) tokens are read and returned as
            a character string. This holds true even for length = 1, so
            reader.read(1) returns a single-character string.

            If a previous operation reached the end-of-file, this method raises
            the StopIteration exception.
        '''
        data = self.__bytes.read(1 if length == None else length)

        if len(data) == 0:
            raise StopIteration()

        return ord(data) if length == None else data
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def readstring(self):
        r'''Reads tokens from the WBXML file until the end-of-string character
            (0x00) is reached, returning the result as a string. The file
            pointer is incremented until past the end-of-string character.
        '''
        data = ''
        while True:
            char = self.read(1)
            if char == '\0':
                return data
            data += char
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def __value(self, data, element, attribute, node):
        (name, value) = self.__get(self.__page, element, 1, attribute)
        if value != None and not (isinstance(value, dict) or callable(value)):
            node.attributes[name] = value
            return

        token = data.read()
        if token == STR_I:
            node.attributes[name] = data.readstring()
        elif token == STR_T:
            offset = data.read()
            node.attributes[name] = self.__readstringtable(offset)
        elif value == None:
            node.attributes[name] = str(token)
        elif isinstance(value, dict):
            node.attributes[name] = value[token]
        else:
            node.attributes[name] = value(node, token)
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def main():
    seq = stdin.read()

    ilen = len(seq)

    seq = sub('>.*\n|\n', '', seq) 
    clen = len(seq)

    variants = (
          'agggtaaa|tttaccct',
          '[cgt]gggtaaa|tttaccc[acg]',
          'a[act]ggtaaa|tttacc[agt]t',
          'ag[act]gtaaa|tttac[agt]ct',
          'agg[act]taaa|ttta[agt]cct',
          'aggg[acg]aaa|ttt[cgt]ccct',
          'agggt[cgt]aa|tt[acg]accct',
          'agggta[cgt]a|t[acg]taccct',
          'agggtaa[cgt]|[acg]ttaccct')
    for f in variants:
        print f, sum(1 for i in finditer(f, seq))

    subst = {
          'B' : '(c|g|t)', 'D' : '(a|g|t)',   'H' : '(a|c|t)', 'K' : '(g|t)',
          'M' : '(a|c)',   'N' : '(a|c|g|t)', 'R' : '(a|g)',   'S' : '(c|g)',
          'V' : '(a|c|g)', 'W' : '(a|t)',     'Y' : '(c|t)'}
    for f, r in subst.items():
        seq = sub(f, r, seq)

    print
    print ilen
    print clen
    print len(seq)
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def next(self):
        r'''Reads one binary token from the WBXML file and advances the file
            pointer one position. If the end-of-file has already been reached,
            raises the StopIteration exception.
        '''
        return self.read()
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def readopaque(self):
        r'''Reads an opaque data buffer from the WBXML file, and returns it
            as a base64 string. The file pointer is incremented until past the
            end of the buffer.
        '''
        length = self.read()
        data = self.read(length)
        return b64encode(data)
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def __version(self, data, doc):
        r'''Sets the version attribute of a WBXML DOM document object.
        '''
        token = data.read()
        minor = 0b1111 & token
        major = (token >> 4) + 1
        doc.version = `major` + '.' + `minor`
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def __charset(self, data, doc):
        r'''Sets the encoding attribute of a WBXML DOM document object.
        '''
        token = data.read()
        doc.encoding = self.__charsets[token]
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def __stringtable(self, data, doc):
        r'''Sets the string table of a WBXML DOM document object.
        '''
        length = data.read()
        self.__strings = [data.read() for i in range(0, length)]
        doc.stringtable = self.__strings
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def __elements(self, data, parent):
        r'''Parses the children of a parent WBXML element, as well as their
            children recursively.
        '''
        for token in data:
            node = None
            if token == END:
                return
            elif token == STR_I:
                node = wbxmlstring(data.readstring())
            elif token == OPAQUE:
                node = wbxmlstring(data.readopaque())
            elif token == SWITCH_PAGE:
                self.__page = data.read()
                continue
            else:
                (tag, hasattributes, hascontents) = (
                    (0b00111111 & token),               # Base tag code
                    ((0b10000000 & token) >> 7) == 1,   # "Has attributes" bit
                    ((0b01000000 & token) >> 6) == 1    # "Has contents" bit
                )

                name = self.__get(self.__page, tag, 0)
                node = wbxmlelement(name)
                if hasattributes:
                    self.__attributes(data, tag, node)
                if hascontents:
                    self.__elements(data, node)
            parent.addchild(node)
项目:peas    作者:mwrlabs    | 项目源码 | 文件源码
def __attributes(self, data, element, node):
        r'''Parses the attributes of a WBXML element.
        '''
        for token in data:
            if token == END:
                return
            elif token == SWITCH_PAGE:
                self.__page = data.read()
            else:
                self.__value(data, element, token, node)
项目:swifthlm    作者:ibm-research    | 项目源码 | 文件源码
def receive_request(self):
        """ Receive request from dispatcher """
        self.logger.debug('Receiving request from Dispatcher')
        self.request_in = str(stdin.read())
项目:hachoir3    作者:vstinner    | 项目源码 | 文件源码
def read(self):
        raise NotImplementedError()
项目:hachoir3    作者:vstinner    | 项目源码 | 文件源码
def read(self):
        for field in self.editor:
            if field.name.startswith("text[") \
                    and field["keyword"].value == self.MAGIC:
                return field["text"].value
        return None
项目:hachoir3    作者:vstinner    | 项目源码 | 文件源码
def read(self):
        data = []
        for field in self.frames:
            if field.name.startswith("padding["):
                data.append(field.value)
        if data:
            return "".join(data)
        else:
            return None
项目:hachoir3    作者:vstinner    | 项目源码 | 文件源码
def main():
    if len(argv) != 2:
        print("usage: %s music.mp3" % argv[0], file=stderr)
        exit(1)

    filename = str(argv[1])
    editor = createEditor(filename)
#    injecter = injecter_cls[editor.input.__class__]
    injecter = MpegAudioInjecter(editor, packet_size=16)

    if False:
        data = injecter.read()
        if data:
            stdout.write(data)
            exit(0)
        else:
            print("No data", file=stderr)
            exit(1)
    else:
        out_filename = filename + ".msg"
        print("Write your message and valid with CTRL+D:")
        stdout.flush()
        data = stdin.read()

        print("Hide message")
        injecter.write(data)

        print("Write ouput into: %s" % out_filename)
        injecter.saveInto(out_filename)