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


Python Stream.get方法代码示例

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


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

示例1: ParseError

# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import get [as 别名]
class Tokenizer:
    class ParseError(RuntimeError):
        pass

    class ModuleError(RuntimeError):
        pass

    def parse(self, src):
        self.strm = Stream(src)
        self.tokens = []

        s = self.strm
        m = 'text block'
        buf = ''
        while not s.eof():
            c = s.get()
            if m == 'text block':
                if c == '{' and s.cur() == '@':
                    if len(buf):
                        self.tokens.append(Token(kind='text-block', value=buf))
                        buf = ''
                    s.get()
                    self.tokens.append(Token(kind='lbraceat', value='{@'))
                    m = 'code block'
                elif c == '{' and s.cur() == '{':
                    if len(buf):
                        self.tokens.append(Token(kind='text-block', value=buf))
                        buf = ''
                    s.get()
                    self.tokens.append(Token(kind='ldbrace', value='{{'))
                    m = 'ref block'
                else:
                    buf += c
            elif m == 'ref block':
                if c == '}' and s.cur() == '}':
                    s.get()
                    self.tokens.append(Token(kind='rdbrace', value='}}'))
                    m = 'text block'                    
                elif self.is_identifier_char(c):
                    s.prev()
                    self.read_identifier()
                elif c == '(':
                    self.tokens.append(Token(kind='lparen', value='('))
                elif c == ')':
                    self.tokens.append(Token(kind='rparen', value=')'))
                elif c == '.':
                    self.tokens.append(Token(kind='operator', value='.'))
                elif c == '"':
                    s.prev()
                    self.read_string()
            elif m == 'code block':
                if c == '@' and s.cur() == '}':
                    s.get()
                    self.tokens.append(Token(kind='rbraceat', value='@}'))
                    m = 'text block'
                elif c == '.':
                    self.tokens.append(Token(kind='operator', value='.'))
                elif c == ',':
                    self.tokens.append(Token(kind='comma', value=','))
                elif c == '(':
                    self.tokens.append(Token(kind='lparen', value='('))
                elif c == ')':
                    self.tokens.append(Token(kind='rparen', value=')'))
                elif c == ':':
                    self.tokens.append(Token(kind='colon', value=':'))
                elif c == '=':
                    s.prev()
                    self.read_assign()
                elif c == '!':
                    s.prev()
                    self.read_not()
                elif c == '<':
                    s.prev()
                    self.read_lt()
                elif c == '>':
                    s.prev()
                    self.read_gt()
                elif c == '+':
                    s.prev()
                    self.read_add()
                elif c == '-':
                    s.prev()
                    self.read_sub()
                elif c == '*':
                    self.tokens.append(Token(kind='operator', value='*'))
                elif c == '/':
                    self.tokens.append(Token(kind='operator', value='/'))
                elif c == '"':
                    s.prev()
                    self.read_string()
                elif c.isdigit():
                    s.prev()
                    self.read_digit()
                elif self.is_identifier_char(c):
                    s.prev()
                    self.read_identifier()
                elif c.isspace():
                    pass
                else:
                    raise Tokenizer.ParseError('unsupprted character %s' % c)
#.........这里部分代码省略.........
开发者ID:narupo,项目名称:cap,代码行数:103,代码来源:tokenizer.py

示例2: TestPyMorseStreamSlow

# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import get [as 别名]
class TestPyMorseStreamSlow(unittest.TestCase):

    def setUp(self):
        print("Starting test...")
        self.freq = 10
        self._server = SocketWriter(freq = self.freq) # First tests: producer at 10 Hz

        self.com = ComChannel("localhost")
        self.stream = Stream(self.com, 61000, maxlength = 3)
        
    def test_get(self):

        self.assertEqual(self.stream.get(), [0])
        self.assertEqual(self.stream.get(), [1])
        
    def test_last(self):

        self.assertIsNone(self.stream.last())
        self.stream.get()
        self.assertEqual(self.stream.last(), [0])
        self.stream.get()
        self.assertEqual(self.stream.last(), [1])

    def test_last_timed(self):

        d = 1/float(self.freq)
        self.assertIsNone(self.stream.last())
        time.sleep(d + d * 0.5)
        self.assertEqual(self.stream.last(), [0])
        time.sleep(d)
        self.assertEqual(self.stream.last(), [1])
        time.sleep(d)
        time.sleep(d)
        self.assertEqual(self.stream.last(), [3])

    def test_last_n(self):

        d = 1/float(self.freq)
        self.assertIsNone(self.stream.last())
        self.assertIsNone(self.stream.last(2))
        self.assertIsNone(self.stream.last(10))

        time.sleep(d + d * 0.5)

        self.assertEqual(self.stream.last(1), [[0]])
        self.assertEqual(self.stream.last(2), [[0]]) # if only one item available, should be fine to ask for two

        time.sleep(d)

        self.assertEqual(self.stream.last(1), [[1]]) # make sure we return the last one
        self.assertEqual(self.stream.last(2), [[0],[1]]) # make sure the order is right (from older to most recent)

        time.sleep(d)

        self.assertEqual(self.stream.last(3), [[0],[1],[2]])

        time.sleep(d)

        self.assertEqual(self.stream.last(3), [[1], [2],[3]])
        self.assertEqual(self.stream.last(4), [[1], [2],[3]]) # make sure the buffer lenght (here, 3) is used

    def test_subscribe(self):

        d = 1/float(self.freq)
        self.ok = False
        self.i = 0

        self.stream.subscribe(self.on_data)

        time.sleep(d + d * 0.5)
        self.assertTrue(self.ok)

        self.ok = False
        time.sleep(d)
        self.assertTrue(self.ok)

    def test_unsubscribe(self):

        d = 1/float(self.freq)
        self.ok = False
        self.i = 0

        self.stream.subscribe(self.on_data)
        time.sleep(d + d * 0.5)
        self.assertTrue(self.ok)

        self.ok = False

        self.stream.unsubscribe(self.on_data)
        time.sleep(d)
        self.assertFalse(self.ok)

    def on_data(self, record):
        
        self.ok = True
        self.assertEqual(record, [self.i])
        self.i += 1
        
    def tearDown(self):
        self.com.close()
#.........这里部分代码省略.........
开发者ID:dandreadante,项目名称:morse,代码行数:103,代码来源:pymorse-internals-testing.py

示例3: ModuleError

# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import get [as 别名]

#.........这里部分代码省略.........
                return self._traverse(node.factor, dep=dep+1)
            else:
                raise AST.ModuleError('programming error. impossible case in traverse term')

        elif isinstance(node, FactorNode):
            if node.expr:
                return self._traverse(node.expr, dep=dep+1)
            elif node.identifier != None:
                if node.identifier not in self.context.syms.keys():
                    raise AST.ReferenceError('%s is not defined' % node.identifier)
                return self.context.syms[node.identifier]
            elif node.digit:
                return self._traverse(node.digit, dep=dep+1)
            elif node.string != None:
                return node.string
            elif node.callable:
                return self._traverse(node.callable, dep=dep+1)

        elif isinstance(node, DigitNode):
            return node.value

        elif isinstance(node, AssignExprNode):
            return self.traverse_assign_expr(node, dep+1)

        elif isinstance(node, ImportNode):
            self.traverse_import(node, dep+1)

        elif isinstance(node, CallerNode):
            self.traverse_caller(node, dep+1)

        elif isinstance(node, CallableNode):
            package = node.caller_list.identifier
            if package == 'opts':
                if node.caller_list.caller_list.identifier == 'get':
                    if self.opts and node.args.arg.string in self.opts.keys():
                        return self.opts[node.args.arg.string]
                    else:
                        return ''

        else:
            raise AST.ModuleError('impossible. not supported node', type(node))

    def traverse_assign_expr(self, node, dep):
        lhs = node.assign_operand_lhs
        op = node.assign_operator
        rhs = node.assign_operand_rhs
        assexpr = node.assign_expr
        if lhs is None and rhs:
            if rhs.expr:
                return self._traverse(rhs.expr, dep+1)
            elif rhs.callable:
                return self._traverse(rhs.callable, dep+1)
            elif rhs.string != None:
                return rhs.string
            elif rhs.identifier != None:
                if rhs.identifier not in self.context.syms.keys():
                    raise AST.ReferenceError('"%s" is not defined' % rhs.identifier)
        elif lhs and op and assexpr:
            if op.operator == '=':
                self.context.syms[lhs.identifier] = self._traverse(assexpr, dep=dep+1)
                self.context.last_expr_val = self.context.syms[lhs.identifier]
                return self.context.syms[lhs.identifier]
            else:
                raise AST.ModuleError('unsupported operator "%s"' % op.operator)
        else:
            raise AST.ModuleError('invalid case in traverse assign expr')
开发者ID:narupo,项目名称:cap,代码行数:70,代码来源:ast.py


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