Skip to main content

Type Annotated Web framework emphasizing Dont Repeat Yourself

Project description

Tawdry is a Type Annotated Web framework which emphasizes Dont Repeat Yourself

More specifically, Tawdry is a “microframework” similar to flask or somewhat less similar to Pyramid or Django. It tries to get out of your way by requiring the absolute minimal amount of boilerplate or extra code of any kind.

Tawdry also aims to improve the clarity of the code you do write by centralizing and localizing the information about your server and its routes.

Example Application

import tawdry

def hello(request, publisher):
    return 'World!'

app = tawdry.Tawdry({'hello': hello})

if __name__ == '__main__':
    app.serve()

Once run, it will create a single route at /hello, on localhost by default.

$ curl localhost:5000/hello
World!

Installation

pip install tawdry

Requires Python 3 (and you’re best off with 3.5)

Type Annotations

The above example doesn’t really do much to show the purpose of Tawdry though. The whole point here is to use type annotations, as they are the mechanism through which Tawdry does its stuff! So lets look at a slightly more complex example.

import tawdry

def exclaim(request, exclamation: str) -> Response:
    return exclamation + '!'

sitemap = {
    'exclaim': {
        '{exclamation}': exclaim,
    },
}
app = tawdry.Tawdry(sitemap)

if __name__ == '__main__':
    app.serve()
$ curl localhost:5000/exclaim/wahhoooo
wahhoooo!

Of note in the above example:

  • An argument type annotation will convert the argument to the annotated type before calling the function.

  • A return type, type annotation will convert the function’s return value after it returns.

  • The sitemap is a dict given in the form where:

    • The values are the functions which will be called in the event of a match.

    • The keys match a url segment with the same string, so nested dictionaries result in matches to subsequent nested segments.

    • Any key enclosed in curly braces is given as a parameter to the function given as a value, as well as any parameters leading up to it.

So in the above example:

  1. the sitemap defines a single manifested route in the form /exclaim/{paramerer}.

  2. parameter will be converted (in this case unnecessarily) to a string before being passed to exclaim.

  3. The return value of exclaim will be converted to a Response object (note Response is applied by default, but other return types exist, such as JsonResponse and XmlResponse)

  4. The Response object is wsgi compatible and will return the result to the caller

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

tawdry-0.0.2.tar.gz (6.4 kB view details)

Uploaded Source

File details

Details for the file tawdry-0.0.2.tar.gz.

File metadata

  • Download URL: tawdry-0.0.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tawdry-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f3e60aa3a1f160d216f4bdaee65b29ff7c05d2e42a15defec404c5d9909dba57
MD5 81b67702f9d4182b1ac364a0d536ea6e
BLAKE2b-256 5b65172e44ed06e89ab241afcfd1e7a3496eb3c2f90c40ac0132bae56b8a0b3c

See more details on using hashes here.

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