Skip to main content

Python web framework

Project description

sherry

Python web framework

pip install sherry

Simple application

from sherry import Sherry, get, Request

hi_str = "hi {}"
app = Sherry()


@get("/", app)
def hello_world(request: Request):
    return hi_str.format(request.query.get("name") or "world")


app.run(9527)

Create application engine

from sherry import Sherry, Engine

app1 = Sherry()
# or
app2 = Engine()
# with regex
app3 = Engine(re=True)

Add handlers

Create handler

If a return value exists for the function, it will be converted to a response (if not)

def my_handler(*args, **kwargs) -> Response | Any:
    pass

If there is no return value, the response will be the response field of the first parameter

The second parameter is the response field of the first parameter

from sherry import Request, Response


def my_handler(ctx: Request) -> None:
    pass


def my_handler(req: Request, res: Response) -> None:
    """
    req.response == res # True
    """
    pass

Add route, method, handlers

app = Engine()
app.add_route("GET" """http method""", "/" """pattern""", handlers)
app.add_route("GET", "/:some" """match one dynamic""", handlers)
app.add_route("GET", "/xx/*any" """match multiple dynamic""", handlers)

with regex

app = Engine(re=True)
app.add_route("GET", "^/\d$" """match one number""", handlers)

Wrapping

app.get("/", handlers)
app.post("/", handlers)
...

Decorators

from sherry.decorators import handler, get  # ...


@handler("/" """pattern""", ["GET" """"http methods"""], app)
def func():
    pass


@get("/"  """pattern""", app)
def func():
    pass

example

from sherry import Engine, handler

app = Engine()


# function decorators
@handler("/", ["GET"], app)
def index_handler():
    pass


# as
app.add_route("GET", "/", index_handler)


# class decorators
@handler("/", ["GET"], app)
class Pages:
    def middle_handler():
        pass

    def index_handler():
        pass


# as
app.add_route("GET", Pages.middle_handler, Pages.index_handler)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sherry-0.0.6.tar.gz (9.0 kB view hashes)

Uploaded Source

Built Distribution

sherry-0.0.6-py3-none-any.whl (10.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page