def delete_peer(self, ip, port): client.delete( '%d:s:%s' % ( # If the peer is a seed hash(self.info_hash), utils.compact(ip, port, ascii=True) ), '%d:l:%s' % ( # If the peer is a leecher hash(self.info_hash), utils.compact(ip, port, ascii=True) ) )
def test_announce(self): response_data = bencode.bdecode(views.announce( self._build_announce_request_object(ip='1.1.1.1', port='1234') ).data) self.assert_( utils.compact('1.1.1.1', 1234)in response_data.get('peers') )
def register_peer(self, ip, port, uploaded, downloaded, left): key = '%d:%s:%s' % ( hash(self.info_hash), 's' if left == 0 else 'l', utils.compact(ip, port, ascii=True) ) self.delete_peer(ip, port) client.set(key, 1) client.expire(key, KEEP_KEYS)
def find_peers(self, ip=None, port=None, status=None): return client.keys('%d:%s:%s' % ( hash(self.info_hash), '*' if status is None else 's' if status.startswith('s') else 'l' if status.startswith('l') else '*', '*' if (ip is None or port is None) else utils.compact(ip, port, ascii=True) ))
def bench(algorithm, problem, *argv, **kwargs): """Benchmark an algorithm and write the results in the standard output.""" # benchmark algorithm runtime a = time.clock() solution, explored = algorithm(problem, *argv, **kwargs) delta = time.clock() - a if isinstance(solution, Node): solution = solution.state # write results results.writerow({ 'initial': compact(problem.initial), 'algorithm': algorithm.__name__, 'heuristic': kwargs['f'].__name__ if 'f' in kwargs else '', 'bound': kwargs['bound'] if 'bound' in kwargs else '', 'final': compact(solution) if solution else '', 'score' : problem.value(solution) if solution else '', 'explored': explored, 'time': delta})