Skip to content

Instantly share code, notes, and snippets.

@aodag
Last active July 21, 2018 09:06
Show Gist options
  • Save aodag/a66c57e2d56c47c8b9191de646cfb197 to your computer and use it in GitHub Desktop.
Save aodag/a66c57e2d56c47c8b9191de646cfb197 to your computer and use it in GitHub Desktop.
pyramid with mypy
from pyramid.config import Configurator
from pyramid.interfaces import IRequest, IResponse
from abc import ABC
class IHelloContext(ABC):
""" resource of hello """
message: str
class HelloContext(IHelloContext):
def __init__(self, message: str) -> None:
self.message = message
class HelloView:
def __init__(self, context: IHelloContext, request: IRequest) -> None:
self.context = context
self.request = request
def __call__(self) -> IResponse:
self.request.response.text = "message: %s" % self.context.message
return self.request.response
def main(port: int) -> None:
import waitress
config = Configurator()
config.set_root_factory(lambda request: HelloContext("Hello, world!"))
config.add_view(HelloView)
app = config.make_wsgi_app()
waitress.serve(app, port=port)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--port", type=int, default=8080)
args = parser.parse_args()
main(args.port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment