Coder Social home page Coder Social logo

Review the response interation about nanohttp HOT 9 CLOSED

pylover avatar pylover commented on July 24, 2024
Review the response interation

from nanohttp.

Comments (9)

pylover avatar pylover commented on July 24, 2024 2

@meyt Thoughts?

from nanohttp.

meyt avatar meyt commented on July 24, 2024 1
def controller_one():
    yield 'hello'


def controller_two():
    return 'hello'


def test(controller):
    buffer = None
    result = controller()

    if result:
        resp_generator = iter(result)
        buffer = next(resp_generator)
    else:
        resp_generator = None

    def _response():
        if buffer is not None:
            yield buffer

        if resp_generator:
            # noinspection PyTypeChecker
            for chunk in resp_generator:
                yield chunk
        else:
            yield b''

    return _response()

print('controller one result:')
for x in test(controller_one):
    print(x)

print('controller two result:')
for x in test(controller_two):
    print(x)

and this is output

controller one result:
hello
controller two result:
h
e
l
l
o

from nanohttp.

meyt avatar meyt commented on July 24, 2024 1

don't care about last commit, it's just a preview of what is in my mind, not a way

from nanohttp.

pylover avatar pylover commented on July 24, 2024 1

Seems there is no way to get rid of the type checking. I prefer to check both strings and generators.

from nanohttp.

pylover avatar pylover commented on July 24, 2024

@meyt

from nanohttp.

pylover avatar pylover commented on July 24, 2024

How about refactoring the __call__ method of the Application?

from nanohttp.

pylover avatar pylover commented on July 24, 2024

The type checking on each response is not my favorite.

from nanohttp.

meyt avatar meyt commented on July 24, 2024

for prevent to type checking, i don't know any way else than forcing to just use generators (like #34), have you better?

from nanohttp.

pylover avatar pylover commented on July 24, 2024

Maybe just checking if the response is a generator or not.

from nanohttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.