Пример #1
0
 def test_dequeue_different_sizes(self, lst: list) -> None:
     """
     Tests the Queue.dequeue method on queues with different amounts of items.
     """
     q = Queue(lst)
     while not q.is_empty():
         self.assertEqual(lst.pop(0), q.dequeue())
Пример #2
0
    def test_items(self):
        q = Queue()

        q.enqueue('A')
        q.enqueue('B')

        assert ['A', 'B'] == q.items()
Пример #3
0
def main():
    print "\ncheck stack"
    stack = Stack(1, 2, 34, 5)
    for x in range(0, 5):
        stack.push(x)
    print stack
    for x in range(0, 15):
        print "".join(["size=", str(len(stack)), " cur_node=", str(stack.pop())])

    print "\ncheck queue"
    queue = Queue(1, 2, 34, 5)
    for x in range(0, 5):
        queue.enter(x)
    print stack
    for x in range(0, 15):
        print "".join(["size=", str(len(queue)), " cur_node=", str(queue.exit())])

    print "\ncheck BSTree"
    tree = BSTree(1, 2, 34, 5)
    print tree
    print tree.find(10)
    print tree.find(5)
    print tree.max()
    print tree.min()
    print tree.successor(34)
    print tree.successor(5)
    print tree.predecessor(1)
    print tree.predecessor(2)
Пример #4
0
    def __init__(self, urllist, visits, min, max):

        self.bots = 5 # max amount of bots to use
        self.count = 0 # returning bots
        self.ip = None
        self.alive = True
        self.targets = {} # {url: visits}
        self.ip_usage = 0
        self.ip_fails = 0
        self.max_fails = 3
        self.max_usage = 3
        self.proto = 'https'
        self.recentIPs = Queue(15)
        self.requesting_ip = False

        self.min = int(min)
        self.max = int(max)
        self.visits = int(visits)

        if not path.exists(urllist):
            exit('Error: Unable to locate `{}`'.format(urllist))

        # read the url list
        with open(urllist, 'r') as f:
            try:
                for url in [_ for _ in f.read().split('\n') if _]:
                    self.targets[url] = 0 # initial view
            except Exception as err:exit('Error:', err)
Пример #5
0
    def test_add_item(self):
        q = Queue()

        q.enqueue('A')

        assert len(q) == 1
        assert q.head.data == 'A'
        assert q.tail.data == 'A'
Пример #6
0
    def __init__(self, word_list, order=2, sentences=1):
        super().__init__()
        self.order = order  # order of the markov chain
        self.sentences = sentences
        self.memory = Queue(order)  # for sampling from markov model

        if word_list is not None:
            self['START'] = Dictogram()
            self._create_chain(word_list)
Пример #7
0
    def test_serialize(self):
        q = Queue()

        q.enqueue('A')
        q.enqueue('B')

        items = q.serialize()

        assert tuple(['A', 'B']) == items
Пример #8
0
    def del_min(self):

        q = Queue()

        del self.tree[0][0]
        q.enqueue(self.tree[self.power][len(self.tree[self.power]) - 1])
        del self.tree[self.power][len(self.tree[self.power]) - 1]
        self.tree[0].insert(0, q.dequeue())
        return self.tree
Пример #9
0
    def test_iteration(self):
        q = Queue()

        items = ['A', 'B']

        for item in items:
            q.enqueue(item)
        
        for i, item in enumerate(q):
            assert item.data == items[i]
Пример #10
0
    def test_enqueue_different_sizes(self, lst: list) -> None:
        """
        Tests the Queue.enqueue method on queues with different amounts of items.
        """
        q = Queue(lst)
        new_item = randint(-100, 100)

        q.enqueue(new_item)

        self.assertEqual(lst + [new_item], q.get_items())
Пример #11
0
def hot_potato(name_list, num):
    q = Queue()
    for i in range(0, len(name_list)):
        q.enqueue(name_list[i])
    for j in range(0, len(name_list) - 1):
        for k in range(0, num):
            q.enqueue(q.dequeue())
        q.dequeue()
    remaining_person = q.dequeue()
    return remaining_person
Пример #12
0
 def test_queue_reversed(self):
     que = Queue()
     random_values = get_random_values()
     for v in random_values:
         que.push(v)
     reversed_queue = reversed(que)
     for v in random_values[::-1]:
         self.assertEqual(reversed_queue.peek(), v)
         reversed_queue.pop()
     self.assertEqual(que.size(), len(random_values))
Пример #13
0
def convert_to_queue(word_list):
    '''
        Helper function to convert a word_list into a queue 
    '''
    queue = Queue()

    for word in word_list:
        queue.enqueue(word)

    return queue
Пример #14
0
 def __init__(self, url, views, visits=0):
     self.recentProxies = Queue()
     self.proxies = Queue()
     self.renewDriver = True
     self.isActive = True
     self.isAlive = True
     self.views = views
     self.visits = visits
     self.url = url
     self.scraper = Scrape(maxSize=30, protocol='SSL', cleanProxies=True)
Пример #15
0
 def test_queue_is_empty(self):
     que = Queue()
     random_values = get_random_values()
     self.assertEqual(que.is_empty(), True)
     for v in random_values:
         que.push(v)
         self.assertEqual(que.is_empty(), False)
     for v in random_values:
         self.assertEqual(que.is_empty(), False)
         que.pop()
     self.assertEqual(que.is_empty(), True)
Пример #16
0
 def test_enqueue_empty(self) -> None:
     """
     Tests the Queue.enqueue method on empty queues.
     """
     n = 0
     while n <= 10:
         q = Queue()
         queue_item = randint(-100, 100)
         q.enqueue(queue_item)
         self.assertEqual([queue_item], q.get_items())
         n += 1
Пример #17
0
 def test_len(self):
     q = Queue()
     
     assert len(q) == 0
     q.enqueue('A')
     assert len(q) == 1
     q.enqueue('B')
     assert len(q) == 2
     q.dequeue()
     assert len(q) == 1
     q.dequeue()
     assert len(q) == 0
Пример #18
0
 def test_enqueue_size_one(self) -> None:
     """
     Tests the Queue.enqueue method on queues with 1 item only.
     """
     n = 0
     while n <= 10:
         initial_item = randint(-1000, 1000)
         q = Queue([initial_item])
         new_item = randint(-1000, 1000)
         q.enqueue(new_item)
         self.assertEqual([initial_item, new_item], q.get_items())
         n += 1
Пример #19
0
 def test_queue_reverse(self):
     que = Queue()
     random_values = get_random_values()
     for v in random_values:
         que.push(v)
     que.reverse()
     for v in random_values[::-1]:
         self.assertEqual(que.peek(), v)
         que.pop()
Пример #20
0
    def test_queue_reversed_with_override(self):
        self.assertEqual(list(reversed([123, 456])), [456, 123])

        que = Queue()
        random_values = get_random_values()
        for v in random_values:
            que.push(v)
        reversed_queue = reversed(que)
        for v in random_values[::-1]:
            self.assertEqual(reversed_queue.peek(), v)
            reversed_queue.pop()
        self.assertEqual(que.size(), len(random_values))

        self.assertEqual(list(reversed([123, 456])), [456, 123])
Пример #21
0
def eliminated(people, skip):
    """Return a list of the positions of people as they are eliminated.

    The position of the last person standing is at the end of the list.
    Assume people is a positive integer.
    Assume skip is a non-negative integer.
    For example:

    >>> eliminated(3, 0)
    [1, 2, 3]
    >>> eliminated(3, 1)
    [2, 1, 3]
    >>> eliminated(3, 4)
    [2, 3, 1]
    """
    assert people > 0
    assert skip >= 0
    circle = Queue()
    for position in range(1, people + 1):
        circle.enqueue(position)
    positions = []
    while circle:
        for _skipped in range(skip):
            circle.enqueue(circle.dequeue())
        positions.append(circle.dequeue())
    return positions
Пример #22
0
 def test_queue_init(self):
     que = Queue()
     self.assertEqual(que.size(), 0)
     self.assertEqual(que.is_empty(), True)
     with self.assertRaises(IndexError):
         que.pop()
     with self.assertRaises(IndexError):
         que.peek()
Пример #23
0
def hot_potato(name_list, num):
    number=len(name_list)
    peoplelist=Queue()
    
    for i in range(number):
        peoplelist.enqueue(name_list[i])
    
    while number>1:
        for i in range(num):
            front=peoplelist.dequeue()
            peoplelist.enqueue(front)
        peoplelist.dequeue()
        number-=1
        
    remaining_person=peoplelist.dequeue()
    return remaining_person
Пример #24
0
    def initialize(self):
        self.db = mongo
        self.search_cache = Cache(master=False, db=config.Cache.searchdb)
        self.hot_image_cache = Cache(master=False, db=config.Cache.imagedb)
        self.queue = Queue()

        self.session = Session(self,
                               MemcacheStore(),
                               initializer={
                                   'nickname': None,
                                   'uid': None,
                                   'avatar': None,
                                   'email': None,
                                   'super': False,
                                   'channel': None,
                                   'login': False,
                                   'net': None,
                                   'reso': None,
                                   'height': 0,
                                   'width': 0,
                                   'show_msg': None,
                                   'hd': True
                               })
        self.session.processor(self)
        self.context = {
            'se': self.session,
            'static_server': config.Server.static_server,
            'cdn': config.CDN.mhost,
        }
Пример #25
0
def base_converter(dec, base):
    digits = "0123456789ABCDEF"
    i = 0
    for j in range(10000):
        if(dec // (base**j) == 0):
            i = j
            break
    q = Queue()
    for k in range(i):
        k = i - k
        temp = dec % (base ** k) // (base ** (k-1))
        q.enqueue(digits[temp])
    str_temp = ""
    for l in range(i):
        str_temp += str(q.dequeue())
    return str_temp
Пример #26
0
 def test_queue_size(self):
     que = Queue()
     random_values = get_random_values()
     for i, v in enumerate(random_values):
         que.push(v)
         self.assertEqual(que.size(), i + 1)
     for i in range(len(random_values)):
         que.pop()
         self.assertEqual(que.size(), len(random_values) - i - 1)
Пример #27
0
class CleanProxies(object):
    def __init__(self):
        self.proxy = None
        self.IDs = Queue()
        self.isAlive = True
        self.requestsMade = 0
        self.msgDisplay = False

    def isClean(self, ip, fails=3):
        if any([self.requestsMade == MAX_REQUESTS, not self.proxy]):
            self.changeID()
        try:
            if not self.isAlive: return
            self.requestsMade += 1
            stat = get(self.ip_checker + ip,
                       proxies=self.proxy).json()['suspicious_factors']
            return not all([
                stat['is_proxy'], stat['is_suspicious'], stat['is_tor_node'],
                stat['is_spam']
            ])
        except:
            if fails: self.isClean(ip, fails - 1)
            else: self.changeID()

    def changeID(self):
        # bypass 429 error
        if not self.isAlive: return
        self.requestsMade = 0
        if not self.msgDisplay:
            print '[!] Searching for secure proxies ...'
            self.msgDisplay = True

        if not self.IDs.qsize: self.scrapeProxies()

        proxy = self.IDs.get()
        try:
            self.proxy = {
                'https': 'http://{}:{}'.format(proxy['ip'], proxy['port'])
            }
        except:
            pass

    def scrapeProxies(self):
        scraper = lib.proxyScraper.Scrape(maxSize=10, protocol='SSL')
        scraper.scrape()
        while all([self.isAlive, scraper.proxies.qsize]):
            self.IDs.put(scraper.proxies.get())
Пример #28
0
    def __init__(self,
                 port=None,
                 protocol=None,
                 country=None,
                 maxSize=None,
                 cleanProxies=False):
        self.socks_proxies = 'https://socks-proxy.net'
        self.ssl_proxies = 'https://sslproxies.org'
        self.ip_checker = 'https://ip-api.io/json/'
        self.port = str(port) if port else None
        self.cleanIP = cleanProxies
        self.protocol = protocol
        self.maxSize = maxSize
        self.country = country
        self.proxies = Queue()
        self.isAlive = True

        super(Scrape, self).__init__()
Пример #29
0
 def test_queue_peek(self):
     que = Queue()
     random_values = get_random_values()
     for v in random_values:
         que.push(v)
     for v in random_values:
         self.assertEqual(que.peek(), v)
         que.pop()
Пример #30
0
 def test_queue_push(self):
     que = Queue()
     random_values = get_random_values()
     for v in random_values:
         que.push(v)
         self.assertEqual(que.is_empty(), False)
         self.assertEqual(que.peek(), v)
         self.assertEqual(que.pop(), v)
Пример #31
0
    def find_bfs(self, data):
        queue = Queue()
        queue.enqueue(self.root)

        while not queue.empty:
            next_ = queue.dequeue()
            if next_:
                queue.enqueue(next_.left)
                queue.enqueue(next_.right)

                if next_.data == data:
                    return next_
Пример #32
0
def test_enqueuing_to_queue_adds_item_to_queue():
    queue = Queue()

    assert_equal(queue.empty(), True)
    queue.enqueue(1)
    assert_equal(queue.empty(), False)
    assert_equal(queue.dequeue(), 1)
Пример #33
0
def test_dequeuing_from_queue_returns_first_item():
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(2)
    queue.enqueue(3)

    assert_equal(queue.dequeue(), 1)
Пример #34
0
def test_dequeuing_from_queue_removes_item():
    queue = Queue()
    queue.enqueue(1)

    assert_equal(queue.empty(), False)
    assert_equal(queue.dequeue(), 1)
    assert_equal(queue.empty(), True)
Пример #35
0
    def flatorder(self, f):
        result = []
        queue = Queue(self.__root)
        while not queue.empty():
            cur_node = queue.exit()
            result.append(f(cur_node.value))
            if cur_node.left is not None:
                queue.enter(cur_node.left)

            if cur_node.right is not None:
                queue.enter(cur_node.right)
        return result
Пример #36
0
def bfs(g, start):
    start.setDistance(0)
    start.setPred(None)
    vertQueue = Queue()
    vertQueue.enqueue(start)
    while (vertQueue.size() > 0):
        currentVert = vertQueue.dequeue()
        for nbr in currentVert.getConnections():
            if (nbr.getColor() == 'white'):
                nbr.setColor('gray')
                nbr.setDistance(currentVert.getDistance() + 1)
                nbr.setPred(currentVert)
                vertQueue.enqueue(nbr)
        currentVert.setColor('black')
Пример #37
0
    def print_tree(self):
        """
        打印树的结构
        :return:
        """
        queue = Queue(self.__root)
        next_level = 1
        now_node_count = 0
        while not queue.empty():
            cur_node = queue.exit()
            print str(cur_node) + "\t",
            now_node_count += 1
            if now_node_count == next_level:
                print
                now_node_count = 0
                next_level *= 2
            if cur_node.left is not None:
                queue.enter(cur_node.left)

            if cur_node.right is not None:
                queue.enter(cur_node.right)
Пример #38
0
 def lws(self, from_node, to_node):
     """
     在有向无环带权图里面查找最长路径
     令list(s,t)是从s到t的最长带权路径。
     那么可以使用递归式表示这个问题
     list(s,t) = max(list(s,t-1))+list(t-1,t)(唯一)
     用动态规划自下而上解决,因为自上而下解决首先遇到的问题就是
     查找指向一个节点的节点在邻接表中比较困难
     :param from_node:
     :param to_node:
     :return:
     """
     __lws = {}
     # 为了计算方便,这里把开始节点到开始节点插入字典中,
     zero_edge = Edge(from_node, from_node)
     __lws[zero_edge] = 0
     graph_stack = Queue()
     graph_stack.enter(from_node)
     while not graph_stack.empty():
         cur_node = graph_stack.exit()
         cur_edge_list = self.__adj_list.get(cur_node)
         if cur_edge_list is None:
             print ",".join(map(lambda edge: str(edge), __lws.iteritems()))
             print ",".join(map(lambda edge: str(edge), __lws))
             return __lws[Edge(from_node, to_node)]
         for edge_end_node in cur_edge_list:
             graph_stack.enter(edge_end_node.key)
             last_weighted_length = __lws[Edge(from_node, cur_node)]
             cur_edge = Edge(from_node, edge_end_node.key)
             cur_weight_length = last_weighted_length + edge_end_node.weight
             # 如果不存在这个边,那么就插入
             if cur_edge not in __lws:
                 __lws[cur_edge] = cur_weight_length
             # 如果存在,那么就把最大值插入
             elif cur_weight_length > __lws[cur_edge]:
                 __lws[cur_edge] = cur_weight_length
     print ",".join(map(lambda edge: str(edge), __lws.iteritems()))
     print ",".join(map(lambda edge: str(edge), __lws))
     return __lws[Edge(from_node, to_node)]
Пример #39
0
def test_empty_returning_true_when_empty():
    queue = Queue()

    assert_equal(queue.empty(), True)
Пример #40
0
def test_dequeuing_from_empty_queue_raises_custom_error_message():
    queue = Queue()

    assert_equal(queue.dequeue(), "Error: Queue is empty")
Пример #41
0
 def __init__(self, parent):
     Queue.__init__(self, parent, QUEUE_LEN, TIMEOUT)
Пример #42
0
def test_empty_returning_false_when_not_empty():
    queue = Queue()
    queue.enqueue(1)

    assert_equal(queue.empty(), False)