XML

I heard Java people like XML, so your API should probably offer it as an alternative to JSON, serialised PHP and INI files.

I was surprised there is no such thing as xml.dumps in Python, so I wrote it. There is no 1-on-1 mapping between dicts and XML, but this a a way to go from Python data to XML.

importlxml.etreeasETdefmaybesub(el,tag):"""
Create a subelement if a tag was given.
Otherwise return the current node.
Used for the root node.
"""iftag:returnET.SubElement(el,tag)else:returneldefwalk(el,tag,data):"""
Recursively add child nodes named tag to el based on data.
"""ifisinstance(data,dict):sub=maybesub(el,tag)forkey,valueinsorted(data.items()):walk(sub,key,value)elifisinstance(data,list):forvalueindata:walk(el,tag,value)else:sub=maybesub(el,tag)sub.text=str(data)defdumps(data,root="response"):root=ET.Element(root)walk(root,None,data)returnET.tostring(root,pretty_print=True)

INI

That was no joke. Take a look at configparser if you ever encounter this.

The internet is s-l-o-w

So take this handy Redis-based memoization function with you, and twiddle with the TTL.

importredisfromfunctoolsimportwrapsimportpickleimportloggingpool=redis.ConnectionPool()defmemoize(ttl=3600):defdecorator(f):@wraps(f)defwrapper(*args,**kwargs):try:r=redis.StrictRedis(connection_pool=pool)# Compute the function signaturesig=(f.__module__,f.__name__,args,kwargs)pargs=pickle.dumps(sig)# Try to get the resultpres=r.get(pargs)ifpres:returnpickle.loads(pres)else:# Or compute and storeres=f(*args,**kwargs)r.setex(pargs,ttl,pickle.dumps(res))returnresexceptredis.RedisError:# Show must go on!logging.exception("redis oopsed")returnf(*args,**kwargs)returnwrapperreturndecorator