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


Python Stream.read方法代码示例

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


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

示例1: __init__

# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import read [as 别名]
class Test:
  def __init__(self, stream):
    self.display = Display()
    self.stream = Stream(stream)

  def parse(self):
    if not self.stream.line:
      return False
    parse = self.stream.line.split('#')
    if len(parse) == 2:
      try:
        self.result = int(parse[0].rstrip(' '))
        self.message = parse[1].rstrip(' ')
      except:
        return False
      if self.result != 0 and self.result != 1:
        return False
      return True
    return False

  def launch(self):
    sucess = 0
    total = 0
    while self.stream.read():
      if self.parse():
        if self.result == 1:
          sucess += 1
        else:
          self.display.error("Error on" + self.message)
        total += 1
    self.display.summary(sucess, total)
开发者ID:ThomasChaf,项目名称:Modulary,代码行数:33,代码来源:test.py

示例2: Lexer

# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import read [as 别名]
class Lexer():
    def __init__(self, stream):
        self.stream = Stream(stream)
        self.buffer = []
        self.tokens = []
        self.line = 1
        self.col = 1

    def read(self):
        r = self.stream.read()

        if r == "\n":
            self.col = 1

        return r

    def next(self):
        r = self.read()

        if r != "\x00":
            self.buffer.append(r)

        return r

    def ignore(self):
        self.buffer = []

    def skip(self):
        self.next()
        self.ignore()

    def fast_forward(self, n):
        for i in range(n):
            self.next()

    def peek(self):
        r = self.stream.read()
        self.stream.unread()

        return r

    def emit_value(self, token_type, value):
        t = Token((self.line, self.col), token_type, value)
        self.tokens.append(t)
        print(self.tokens[-1])
        self.ignore()

    def emit(self, token_type):
        self.emit_value(token_type, "".join(self.buffer))

    def follow(self, n):
        to_unread = 0
        for expected in n:
            r = self.stream.read()
            to_unread += 1

            if expected != r:
                return False

        for i in range(to_unread):
            self.stream.unread()

        return True

    def lex_main(self):
        while True:
            n = self.peek()

            if n == "#":
                return self.comment
            elif n in ("\n", "\r"):
                self.skip()
            elif n in (" ", "\t"):
                self.skip()
            elif n == '"':
                return self.string

            if n == "\x00":
                break

            time.sleep(0.25)

        return None

    def comment(self):
        while True:
            n = self.peek()

            if n == '\n' or n == "\x00":
                break

            if n == "\r" and self.follow("\r\n"):
                break

            self.next()

        self.ignore()

        return self.lex_main

#.........这里部分代码省略.........
开发者ID:tslnc04,项目名称:python-projects-2,代码行数:103,代码来源:lexer.py


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