Pytcher is a REST micro-framework for Python 3 that relies on a routing tree similar to RODA in Ruby, Akka HTTP in Scala or Javalin in Java.
- Routing tree definition using
withorforconstruction - Marshalling of Python objects (data classes, namedtuples, date, datetime, uuid, ...) that supports custom encoders
- Unmarshalling of JSON to Python objects (data classes, namedtuples, date, datetime, uuid, ...) supporting
typing(e.g.,Dict[str, MyDataClass]) syntax and custom decoders. - Additional Routing decorators similar to Flask
- Well scoped objects (no global variables)
- Support for WSGI
- Auto reload when code change is detected in debug mode
The routing tree can be defined as follows:
frompytcherimportApp, Request, Integer, routeclassMyRouter(object):
def__init__(self):
self._items= ['pizza', 'cheese', 'ice-cream', 'butter']
@routedefroute(self, r: Request):
withr/'items': # if URL starts with /itemswithr.end: # if there is nothing after /itemswithr.get: # If it's a get requestreturnself._itemswithr.post: # If request is a post requestself._items.append(r.json)
returnself._items[-1]
# If the URL is /items/<integer> then bind item_id to the integerwithr/Integer() asitem_id:
withr.get: # If the request is a get requestreturnself._items[item_id]
withr.put: # If the request is a put requestself._items[item_id] =r.jsonreturnself._items[item_id]
withr.delete: # If the request is a delete requestreturnself._items.pop(item_id)
if__name__=='__main__':
app=App(MyRouter())
app.start()