generator

LL(1) parser generator in OCaml

我怕爱的太早我们不能终老 提交于 2020-01-04 05:43:11
问题 I'm looking for a LL(1) parser generator in OCaml... Can anybody help me with this? 回答1: Well, LALR parsers can parse a strict superset of the languages which can be parsed by LL parsers. So I would advise simply using ocamlyacc which ships with Ocaml and is an LALR(1) parser generator. This may require some minor rewriting of the grammar, but it shouldn't be too hard. 回答2: Planck LL(n) parser combinator library: https://bitbucket.org/camlspotter/planck/overview It has started as my toy

Running code AFTER the response has been sent by Koa

亡梦爱人 提交于 2020-01-04 04:30:28
问题 To optimize the response delay, it is necessary to perform work after are response has been sent back to the client. However, the only way I can seem to get code to run after the response is sent is by using setTimeout . Is there a better way? Perhaps somewhere to plug in code after the response is sent, or somewhere to run code asynchronously? Here's some code. koa = require 'koa' router = require 'koa-router' app = koa() # routing app.use router app app .get '/mypath', (next) -> # ...

Trouble unpacking list in a customized way

时光总嘲笑我的痴心妄想 提交于 2020-01-04 03:32:10
问题 I'm trying to unpack some list which I've yielded within get_item() function. I know I can get desired result If I used return instead of yield . I've tried: def get_item(): yield ["k","y","t"] if __name__ == '__main__': for item in get_item(): print(item) Output I'm getting: ['k', 'y', 't'] Output I wanna get: k y t What possible change should I bring about to get the desired result keeping yield as it is? 回答1: As of Python 3.3, you can use yield from : def get_item(): yield from ["k","y","t

Non-blocking generator on Python

夙愿已清 提交于 2020-01-04 02:52:26
问题 I'm using a generator function from the requests module in a QT-Application, pretty much the same as in the requests-streaming example: import json import requests def get_stream(): r = requests.get('http://httpbin.org/stream/20', stream=True) for line in r.iter_lines(): if line: yield json.loads(line) def consume_stream(): for message in get_stream(): #do something However, when there is no incoming response (f.e. irregularly incoming tweets from Twitters Streaming API), the generator get

Restarting a Generator in Javascript

自闭症网瘾萝莉.ら 提交于 2020-01-03 19:57:08
问题 In node (0.11.9, with the --harmony flag), how do I restart a generator after it finishes? I tried doing generator.send(true); but it says the send() method doesn't exists. 回答1: A bit late, but this is just a FYI. At the moment, the send method is not implemented in Node, but is in Nightly (FF) - and only in some way. Nightly: If you declare your generator without the *, you'll get an iterator that has a send method: var g = function() { var val = yield 1; // this is the way to get what you

Why does `yield from` in a generator expression yield `None`s?

∥☆過路亽.° 提交于 2020-01-03 17:45:14
问题 I have the following code: import itertools for c in ((yield from bin(n)[2:]) for n in range(10)): print(c) The output is: 0 None 1 None 1 0 None 1 1 None ... etc. Why do the None s appear? If I instead have: def hmm(): for n in range(10): yield from bin(n)[2:] for c in hmm(): print(c) Then I get what I would expect: 0 1 1 0 1 1 ... etc. Further, is there a way to write it as the generator expression to get the same result as the latter? 回答1: yield is an expression, and its value is whatever

How do I use the `--helper` flag in a rails 3 controller generator?

半城伤御伤魂 提交于 2020-01-03 08:48:12
问题 The documentation from rails generate controller says: [--helper] # Indicates when to generate helper # Default: true Now, it doesn't specify how to indicate a value. So, since the default is true, that means that excluding it won't indicate false, because… true is the default. So it must either be --helper=false or --helper false , but I tried both, and they both resulted in error false [not found] The good news is that it did not generate a helper, because it was confused, so I still got

Using return (list) vs yield

99封情书 提交于 2020-01-03 05:18:32
问题 I've created two enumeration methods, one which returns a list and the other which returns a yield/generator: def enum_list(sequence, start=0): lst = [] num = start for sequence_item in sequence: lst.append((num, sequence_item)) num += 1 return lst def enum_generator(sequence, start=0): num = start for sequence_item in sequence: yield (num, sequence_item) num += 1 A few questions on this: (1) Is changing a list to a generator as simple as doing: # build via list l = list() for item in items:

Python: LINQ capable class supporting simultaneously laziness and fluent design

牧云@^-^@ 提交于 2020-01-03 04:48:06
问题 How to create an Python class which would be a iterable wrapper with LINQ-like methods (select, where, orderby, etc.) without using extension methods or monkey patching. ? That is this LinqCapable class would be capable to return its own type when it is relevant (i.e. fluent design) and support lazy evaluation. I'm just looking here for a snippet as a starting point. 回答1: It's not enough to just return a generator for the implemented linq methods, you need to have it return an instance of the

Write unittest for function with yield

拜拜、爱过 提交于 2020-01-03 03:10:17
问题 I am trying to write a unittest for a function that utilizes a generator. Below is my code: def extract_data(body): for i in body: a = re.sub('<[^<]+?>', '', str(i)) b = re.sub('view\xc2\xa0book\xc2\xa0info', '', str(a)) c = re.sub('key', '', str(b)) d = re.sub('\xc2', ' ', str(c)) e = re.sub('\xa0', '', str(d)) yield e My unittest code: def test_extract_data(self): sample_input = ['<tr><h1>keyThis</h1><h2>\xc2</h2><h3>\xa0</h3><h4>view\xc2\xa0book\xc2\xa0info</h4><h5>Test Passes</h5></tr>']