示例#1
0
def test_iter():
    q = Queue()
    list = [i for i in range(10)]
    for i in list:
        q.queue(i)
    new_list = [i for i in q]
    assert q.is_empty()
    assert list == new_list
示例#2
0
class DepthFirstOrder(object):
    def __init__(self, G):
        self.__marked = [False] * G.V()
        self.__pre = [-1] * G.V()
        self.__post = [-1] * G.V()

        self.__pre_counter = 0
        self.__post_counter = 0

        self.__pre_order = Queue()
        self.__post_order = Queue()

        for v in range(G.V()):
            if self.__marked[v] is False:
                self.__dfs(G, v)

    def __dfs(self, G, v):
        self.__marked[v] = True
        self.__pre[v] = self.__pre_counter
        self.__pre_counter += 1
        self.__pre_order.enqueue(v)

        for w in G.adj(v):
            if self.__marked[w] is False:
                self.__dfs(G, w)

        self.__post_order.enqueue(v)
        self.__post[v] = self.__post_counter
        self.__post_counter += 1

    def pre(self, v):
        self.__validate_vertex(v)
        return self.__pre[v]

    def post(self, v):
        self.__validate_vertex(v)
        return self.__post[v]

    def pre_order(self):
        return self.__pre_order

    def post_order(self):
        return self.__post_order

    def reverse_post(self):
        reverse = Stack()

        for v in self.__post_order:
            reverse.push(v)

        return reverse

    def __validate_vertex(self, v):
        V = len(self.__marked)
        if v < 0 or v > V:
            raise ValueError("vertex %s is not between 0 and %s" % (v, V))
示例#3
0
    def __init__(self, G):
        self.__marked = [False] * G.V()
        self.__pre = [-1] * G.V()
        self.__post = [-1] * G.V()

        self.__pre_counter = 0
        self.__post_counter = 0

        self.__pre_order = Queue()
        self.__post_order = Queue()

        for v in range(G.V()):
            if self.__marked[v] is False:
                self.__dfs(G, v)
    def __bfs(self, G, sources):
        q = Queue()
        for s in sources:
            self.__marked[s] = True
            self.__dist_to[s] = 0
            q.enqueue(s)

        while q.is_empty() is False:
            v = q.dequeue()
            for w in G.adj(v):
                if self.__marked[w] is False:
                    self.__edge_to[w] = v
                    self.__dist_to[w] = self.__dist_to[v] + 1
                    self.__marked[w] = True
                    q.enqueue(w)
示例#5
0
 def enqueue(self, item, priority=0):
     if priority not in self.items:
         self.items[priority] = Queue()
         queue = self.items[priority]
         queue.enqueue(item)
示例#6
0
def hotPotato(namelist, num):
    simqueue = Queue()
    for name in namelist:
        simqueue.enqueue(name)
        while simqueue.size() > 1:
            for i in range(num):
                simqueue.enqueue(simqueue.dequeue())
                simqueue.dequeue()
        return simqueue.dequeue()
示例#7
0
import redis

from pyqueue import Queue

r = redis.StrictRedis()
queue = Queue(r, 'pynash')

print('Starting worker...')
while True:
    print(' --> Waiting for work...')

    job = queue.dequeue_job()
    print('Job {job_id}: {result}'.format(job_id=job.id, result=job.result))
示例#8
0
from pyqueue import Queue
q=Queue()
q.enqueue(6)
q.enqueue('cat')
q.enqueue(True)
print(q.size())
print(q.dequeue())
print(q.dequeue())
print(q.size())
示例#9
0
from pyqueue import Queue
q = Queue()
q.enqueue(6)
q.enqueue("cat")
q.enqueue(True)
print(q.size())
print(q.dequeue())
print(q.dequeue())
print(q.size())
示例#10
0
import redis

from pyqueue import Queue
from tasks import count_words

r = redis.StrictRedis()
queue = Queue(r, 'pynash')

job1_id = queue.enqueue_job(count_words, 'declaration-of-independence.txt')
print('Queued {}'.format(job1_id))

job2_id = queue.enqueue_job(count_words, 'us-constitution.txt', most_common=5)
print('Queued {}'.format(job2_id))
示例#11
0
def test_queue_one():
    q = Queue()
    q.queue(1)
    assert not q.is_empty()
    assert q.dequeue() == 1
    assert q.is_empty()
示例#12
0
def test_iter2():
    q = Queue()
    r = Queue()
    list = [i for i in range(10)]
    for i in list:
        r.queue(i)
    assert not r.is_empty()
    for i in r:
        q.queue(i)
    assert r.is_empty()
    assert not q.is_empty()
    new_list = [i for i in q]
    assert q.is_empty()
    assert list == new_list
示例#13
0
def test_empty():
    q = Queue()
    assert q.is_empty()
示例#14
0
def test_queue_more():
    q = Queue()
    q.queue(1)
    q.queue('true')
    q.queue(True)
    assert q.dequeue() == 1
    assert q.dequeue() == 'true'
    assert q.dequeue() == True
    assert q.is_empty()
示例#15
0
def test_queue_none():
    q = Queue()
    q.queue(None)
    assert not q.is_empty()
    assert q.dequeue() is None
    assert q.is_empty()