$onr, $xkcd
[halbot.git] / common.py
bloba7c9ccd2210c3073c920098d35fc0b80dc9654bc
1 import time
2 from sets import Set
3 from irclib import irc_lower
5 from globals import private
7 def get_timestamp(when=None):
8 if when == None:
9 when = time.time()
10 return time.ctime(when) + " " + time.tzname[time.daylight]
12 def resize(list_or_tuple, length):
13 '''Creates a new list or tuple of length 'length', using as many elements from
14 list_or_tuple as possible and right-padding with "". Does not modify
15 list_or_tuple.'''
16 if len(list_or_tuple) < length:
17 if type(list_or_tuple) == list:
18 return list_or_tuple + [""] * (length - len(list_or_tuple))
19 else:
20 return list_or_tuple + ("",) * (length - len(list_or_tuple))
21 else:
22 return list_or_tuple[:length]
24 def extract(dbs, key, default_maker):
25 if key not in dbs:
26 default = default_maker()
27 dbs[key] = default
28 return dbs[key]
30 def extract_my_db(dbs, who, where, default_maker):
31 if where == private:
32 key = who
33 else:
34 key = where
35 return extract(dbs, key, default_maker)
37 def real_where(who, where):
38 if where == private:
39 return who
40 return where
42 def safeint(intstr):
43 try:
44 return int(intstr)
45 except ValueError:
46 return -1
48 def multiunion(*sets):
49 result = Set()
50 for s in sets:
51 result = result.union(s)
52 return result
54 def ircdict_to_set(my_dict):
55 return Set([irc_lower(key) for key in my_dict])