0

First, I use flask.

Second, I yield values like the code below.

def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        def generate():
            yield '<pre>'
            yield "Processing...\n"
            file = CheckFile(f.filename, f.read())
            file.parse()
            for note in file.validate():
                if note.oddity_type == Note.ERROR:
                    yield '<span style="color: red;">%s</span>' % str(note)
                if note.type == Note.WARNING:
                    yield '<span style="color: orange;">%s</span>' % str(note)
                if note.type == Note.INFO:
                    yield '<span style="color: blue;">%s</span>' % str(note)
                if note.type == Note.FATAL:
                    yield '<span style="color: red;">%s</span>' % str(note)
            yield '</pre>'
        return Response(stream_with_context(generate()))
    if request.method == 'GET':
        return app.send_static_file('index.html')

So after the __init__ inCheckFileis completed, Ireturnvalues withyield` to the response.

What I want to do is to be able to yield values in __init__ and in file.parse().

netdigger
  • 3,659
  • 3
  • 26
  • 49
  • I read it three times but it does not get clearer what you are trying to do. – Klaus D. Jul 13 '18 at 06:38
  • Alright, I've clarified it a bit. Is it better @KlausD. ? – netdigger Jul 13 '18 at 06:39
  • Make `parse` a generator and use the `yield from` keyword. I don't think it would be a good idea to do the same with the constructor. It's not its purpose and I have no idea if that would even work. – Klaus D. Jul 13 '18 at 07:30
  • Yeah, that's actually my idea as well, however, I'm not really sure how implement that (that "the parse is a generator"). Basically the parsing is just reading a JSON file and reading certain keys of that JSON. – netdigger Jul 13 '18 at 07:33
  • You just need to have one or more `yield` statements in `parse`, then `yield from file.parse()`. I did not test it, so give me a notice if it works. – Klaus D. Jul 13 '18 at 07:42
  • But then I need to do ALL parsing in `parse` right? I can't call any functions or anything...? – netdigger Jul 13 '18 at 07:46
  • Why not? You can deliver to other generators with `yield from`. That also works on generator expressions as an easy way to wrap other iterators like lists returned by functions. – Klaus D. Jul 13 '18 at 07:58

0 Answers0