def big_popper(): # more of a benchmark than a test from os import urandom import time big_set = IndexedSet(range(100000)) rands = [ord(r) for r in urandom(len(big_set))] start_time, start_size = time.time(), len(big_set) while len(big_set) > 10000: if len(big_set) % 10000 == 0: print(len(big_set) / 10000) rand = rands.pop() big_set.pop(rand) big_set.pop(-rand) end_time, end_size = time.time(), len(big_set) print() print('popped %s items in %s seconds' % (start_size - end_size, end_time - start_time))
def test_indexed_set_mutate(): thou = IndexedSet(range(1000)) assert (thou.pop(), thou.pop()) == (999, 998) assert (thou.pop(499), thou.pop(499)) == (499, 500) ref = [495, 496, 497, 498, 501, 502, 503, 504, 505, 506] assert [thou[i] for i in range(495, 505)] == ref assert len(thou) == 996 while len(thou) > 600: dead_idx_len = len(thou.dead_indices) dead_idx_count = thou._dead_index_count thou.pop(0) new_dead_idx_len = len(thou.dead_indices) if new_dead_idx_len < dead_idx_len: assert dead_idx_count > 0 # 124, 109, 95 assert len(thou) == 600 assert thou._dead_index_count == 67 assert not any([thou[i] is _MISSING for i in range(len(thou))]) thou &= IndexedSet(range(500, 503)) assert thou == IndexedSet([501, 502]) return
def test_iset_index_method(): original_list = list(range(8, 20)) + list(range(8)) indexed_list = IndexedSet() for i in original_list: indexed_list.add(i) for i in original_list: index = indexed_list.index(i) # if we're removing them in order, the target value should always be at index 0 assert index == 0 indexed_list.pop(index) indexed_list = IndexedSet(range(10)) for i in reversed(range(10)): if i % 2: continue index = indexed_list.index(i) assert i == indexed_list.pop(index) indexed_list = IndexedSet(range(32)) for i in list(indexed_list): if i % 3: index = indexed_list.index(i) assert i == indexed_list.pop(index) indexed_list = IndexedSet(range(10)) for i in range(10): if i < 3: continue index = indexed_list.index(i) assert i == indexed_list.pop(index) indexed_list = IndexedSet(range(32)) for i in list(indexed_list): if i % 3: index = indexed_list.index(i) assert i == indexed_list.pop(index)
class BuffBot: #configure buff bot def __init__(self, logfile, buffs, regex): print(regex) self.logfile = logfile self.bufflist = buffs self.rx = re.compile(regex) self.windowName = "EverQuest" self.buffqueue = IndexedSet() #display configuration def displayconfig(self): print("Bot Configuration") print("Reading Log file: ", self.logfile) print("Current Buff list") for key in self.bufflist: print("buff trigger:", key, "\tgem slot: ", self.bufflist[key][0], '\tcast time:', self.bufflist[key][1]) # Log matches Process def MatchFound(self, name, request): try: print(str(dt.datetime.now())[0:19], name, "requested", request) self.ProcessRequest(name, request, self.bufflist[request]) except KeyError: #ignore non-buff requests. print( str(dt.datetime.now())[0:19], "Unknown request from", name, "ignored:", request) pass # build the macro for this buff and execute it. def ProcessRequest(self, name, request, spellinfo): gem = spellinfo[0] casttime = spellinfo[1] castline = [ "/tar " + name, "/cast " + str(gem), "/cast " + str(gem), "/cast " + str(gem) ] print( str(dt.datetime.now())[0:19], 'casting', request, 'on', name, 'from spell slot', gem, 'with cast time ', casttime, 'seconds.') for lines in castline: self.enterline(lines) time.sleep(0.5) time.sleep(float(casttime)) self.enterline("/sit") print(str(dt.datetime.now())[0:19], "Request completed.") # copy each line to clipboard and paste into EQ. def enterline(self, cmd): cb.copy(cmd) kb.press("enter", _pause=False) kb.keyDown("ctrl", _pause=False) kb.press("v", _pause=False) kb.keyUp("ctrl", _pause=False) kb.press("enter", _pause=False) # main thread, wait for a valid buff request then add it to the queue. def listen(self): with open(self.logfile) as f: f.seek(0, os.SEEK_END) print(str(dt.datetime.now())[0:19], "Listening for buff requests") time.sleep(2) while True: txt = '' line = f.readline() if line: s = line.split() # get rid of time stamps txt = " ".join(s[5:]) txt = txt.lower() m = self.rx.match(txt) if m: self.addQueue(m.group(1), m.group(2)) time.sleep(0.1) # If queue isn't empty get to work. def monitorQueue(self): print(str(dt.datetime.now())[0:19], "Monitoring Queue") while (True): while (len(self.buffqueue)): print( str(dt.datetime.now())[0:19], "Processing Queue: ", str(len(self.buffqueue)) + " items left.") rq = self.buffqueue.pop(0) self.MatchFound(rq[0], rq[1]) time.sleep(0.25) # Add an request to queue. def addQueue(self, name, req): print( str(dt.datetime.now())[0:19], "Adding " + name + " to queue for: " + req) request = (name, req) self.buffqueue.add(request) print(self.buffqueue)