moved to new util structure from refresh
This commit is contained in:
parent
b25b8d6cec
commit
e29ea1c613
|
@ -1,41 +0,0 @@
|
|||
# Colors. Plugin by blha303, color/control id info from http://stackoverflow.com/a/13382032
|
||||
|
||||
colors = {'white': '0', 'black': '1', 'darkblue': '2', 'darkgreen': '3',
|
||||
'red': '4', 'darkred': '5', 'darkviolet': '6', 'orange': '7',
|
||||
'yellow': '8', 'lightgreen': '9', 'cyan': '10', 'lightcyan': '11',
|
||||
'blue': '12', 'violet': '13', 'darkgray': '14', 'lightgray': '15'}
|
||||
|
||||
control = {'bold': '\x02', 'color': '\x03', 'italic': '\x09',
|
||||
'strikethrough': '\x13', 'reset': '\x0f', 'underline': '\x15',
|
||||
'underline2': '\x1f', 'reverse': '\x16'}
|
||||
|
||||
|
||||
def color(color):
|
||||
return control['color'] + colors[color]
|
||||
|
||||
|
||||
def bold():
|
||||
return control['bold']
|
||||
|
||||
|
||||
def italic():
|
||||
return control['italic']
|
||||
|
||||
|
||||
def strike():
|
||||
return control['strikethrough']
|
||||
|
||||
|
||||
def reset():
|
||||
return control['reset']
|
||||
|
||||
|
||||
def underline(other=False):
|
||||
if other:
|
||||
return control['underline2']
|
||||
else:
|
||||
return control['underline']
|
||||
|
||||
|
||||
def reverse():
|
||||
return control['reverse']
|
|
@ -1,36 +0,0 @@
|
|||
def raw(format_string):
|
||||
"""Replace based irc formatting"""
|
||||
stuff = {}
|
||||
stuff['col'] = {'[white]': '\x030',
|
||||
'[black]': '\x031',
|
||||
'[dblue]': '\x032',
|
||||
'[dgreen]': '\x033',
|
||||
'[dred]': '\x034',
|
||||
'[brown]': '\x035',
|
||||
'[purple]': '\x036',
|
||||
'[gold]': '\x037',
|
||||
'[yellow]': '\x038',
|
||||
'[green]': '\x039',
|
||||
'[cyan]': '\x0310',
|
||||
'[lblue]': '\x0311',
|
||||
'[blue]': '\x0312',
|
||||
'[pink]': '\x0313',
|
||||
'[gray]': '\x0314',
|
||||
'[lgray]': '\x0315',
|
||||
'[err]': '\x034\x02'
|
||||
'[/err]': '\x030\x02'}
|
||||
stuff['style'] = {'[b]': '\x02',
|
||||
'[clear]': '\x0f'}
|
||||
stuff['sym'] = {'[point]': '\x07'}
|
||||
stuff['text'] = {'[url]': 'http://'}
|
||||
final = {}
|
||||
for x in stuff:
|
||||
final.update(stuff[x])
|
||||
for x in final:
|
||||
format_string = format_string.replace(x, final[x])
|
||||
return format_string
|
||||
|
||||
|
||||
def err(format_string):
|
||||
"""Format the string with standard error styling"""
|
||||
return "\x034\x02{}\x0f".format(format_string)
|
|
@ -0,0 +1,40 @@
|
|||
from time import time
|
||||
|
||||
|
||||
class TokenBucket(object):
|
||||
"""An implementation of the token bucket algorithm.
|
||||
|
||||
>>> bucket = TokenBucket(80, 0.5)
|
||||
>>> print bucket.consume(10)
|
||||
True
|
||||
>>> print bucket.consume(90)
|
||||
False
|
||||
"""
|
||||
def __init__(self, tokens, fill_rate):
|
||||
"""tokens is the total tokens in the bucket. fill_rate is the
|
||||
rate in tokens/second that the bucket will be refilled."""
|
||||
self.capacity = float(tokens)
|
||||
self._tokens = float(tokens)
|
||||
self.fill_rate = float(fill_rate)
|
||||
self.timestamp = time()
|
||||
|
||||
def consume(self, tokens):
|
||||
"""Consume tokens from the bucket. Returns True if there were
|
||||
sufficient tokens otherwise False."""
|
||||
if tokens <= self.tokens:
|
||||
self._tokens -= tokens
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
def refill(self):
|
||||
self._tokens = self.capacity
|
||||
|
||||
def get_tokens(self):
|
||||
now = time()
|
||||
if self._tokens < self.capacity:
|
||||
delta = self.fill_rate * (now - self.timestamp)
|
||||
self._tokens = min(self.capacity, self._tokens + delta)
|
||||
self.timestamp = now
|
||||
return self._tokens
|
||||
tokens = property(get_tokens)
|
Reference in New Issue