I have been busy recently. I have not finished writing a technical article I planned for a long time. I will first write a short article.
When we used turbogears 1.x in the past, we thought it was very convenient to have some functions, such as expose decorator. However, Web. py is useless.
The usage of Web. py is the advantage of refreshing graph, but it is better to add more values. In fact, the implementation of this function is quite simple, so I wrote one for Web. py in the past two days.
In addition, restful APIs all like to provide results returned in multiple formats (it seems that this is a trend led by Twitter), so by the way, this function is also added to the stuff you write.
The Code is as follows (the obj2xml code comes from
):
Import JSON <br/> Import Web <br/> from web. contrib. template import render_mako <br/> class tgbasehandler: <br/> def _ init _ (self, templates_path): <br/> self. mako_render = render_mako (<br/> directories = [templates_path,], <br/> input_encoding = 'utf-8 ', <br/> output_encoding = 'utf-8' <br/>) <br/> # Two formats of render are supported by default <br/> # HTML render uses the Mako template <br/> def html_render (self, page, result): <br/> RET Urn getattr (self. mako_render, page) (** result) <br/> def json_render (self, page, result): <br/> return JSON. dumps (result) <br/> # automatically call the corresponding format render <br/> def render (self, page, format, result): <br/> try: <br/> render_func = getattr (self, format + '_ render') <br/> failed t attributeerror: <br/> raise "invalide render format! "<Br/> return render_func (page, result) <br/> # format: HTML/JSON/XML... <br/> # The default value is HTML format. You only need to provide the page parameter. Other formats do not need the page parameter. <br/> def expose (page = '', format = 'html '): <br/> def expose_wrapper (FN): <br/> def format_wrapper (self, * ARGs, ** kwargs ): <br/> result = FN (self, * ARGs, ** kwargs) <br/> If 'format' in result. keys (): # automatically select the corresponding format <br/> Format = Result ['format'] <br/> Del result ['format'] <br/> return self. render (page, format, result) <br/> return format_wrapper <br/> return expose_wrapper
The usage is as follows:
#... <Br/> Import obj2xml <br/> URLs = (<br/> "/", "Index", <br/> "/Index", "Index ", <br/> "/jsondata", "jsondata", <br/> "/timeline /.? ([^ /? /.] +) "," Timeline ", # automatically obtain the format parameter <br/>) <br/> #... <br/> class basehandler (tgbasehandler): <br/> def _ init _ (Self): <br/> tgbasehandler. _ init _ (self, 'pathto/templates ') # initialize Mako <br/> # You can add various custom formats render here <br/> def xml_render (self, page, result): <br/> return obj2xml. getxml (result) <br/> class index (basehandler): <br/> @ expose ("Index") # The default format is HTML. <br/> def get (Self ): <br/> return dict (url = "/") <br/> class jsondata (basehandler): <br/> @ expose (format = 'json ') # specify JSON format render <br/> def get (Self): <br/> return dict (tweets = [{'id': '2016', 'text ': 'Hello'}, {'id': '000000', 'text': 'World'}]) <br/> class timeline (basehandler ): <br/> @ expose () # automatically select format render <br/> def get (self, format): <br/> return dict (format = format, tweets = [{'id': '000000', 'text': 'hello'}, {'id': '000000', 'text': 'World'}]) # Note: The format parameter must be returned here.
In this way, the HTML output from Mako render is returned when you access the index page, and a JSON text is returned when you access the jsondata page. You can access timeline. xml and timeline. JSON to return XML and JSON texts respectively.
In addition, the identity of turbogears 1. X is also very good. If you are free, you can also get a web. py.
Push to [go4pro.org]