Python sys.stdin 模块,read() 实例源码
我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用sys.stdin.read()。
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)
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
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
def __value(self, data, element, attribute, node):
(name, value) = self.__get(self.__page, 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)
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)
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()
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)
def __charset(self, doc):
r'''Sets the encoding attribute of a WBXML DOM document object.
'''
token = data.read()
doc.encoding = self.__charsets[token]
def __stringtable(self, 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
def __elements(self, 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, node)
if hascontents:
self.__elements(data, node)
parent.addchild(node)
def __attributes(self, 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, token, node)
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
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
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。