-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.py
33 lines (29 loc) · 1.4 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
import functools
def contextual_class_name(obj):
"""Takes an object reference and derives a css class name. Used primarily for deriving a contextual css selector per handler."""
return obj.__class__.__name__.lower().replace("handler", "")
def cache(method):
"""Simple decorator to cache the results of a method. NOTE!!! Does not memoize based on *args and **kwargs."""
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
print method
if not self.application.cache.has_key(method):
self.application.cache[method] = method(self, *args, **kwargs)
return self.application.cache[method]
return wrapper
class BufferedWriter(object):
"""Convenience object to write and maintain ordering of rendered strings. Useful when dealing with multiple async requests and buffering responses."""
def __init__(self, length, callback):
self.buffer = [None for i in xrange(length)]
self.count = 0
self.length = length
self.callback = callback
def write(self, index, data):
"""Writes to the buffer at the specified indices. Note: Indices that are out of bounds will throw a ValueError."""
if index<0 or index+1>self.length:
raise ValueError
self.buffer[index] = data
if self.buffer.count(None)==0:
string = "".join(self.buffer)
self.callback(string)