[PYTHON] Inherit the standard library to find the average value of Queue

Introduction

I used to want to find the average of Queue values (I don't remember why I needed it). At that time, I used the following code to find the average value.

queue_test.py


import queue


def queue_ave(q):
    queue_list = []
    while not q.empty():
        queue_list.append(q.get())
    for value in queue_list:  #reversed for stack(queue_list)change to
        q.put(value)
    return sum(queue_list) / len(queue_list)


#Operation check
q = queue.Queue()
q.put(1)
q.put(2)
q.put(6)
q.put(8)
print(queue_ave(q))  # 4.25

The flow of this code (function) is

  1. Take everything from the Queue and put it in a List
  2. Conversely, return from List to Queue
  3. Pass the average value of List as the return value

It is something like. Certainly, the average value of Queue can still be calculated. However, as a matter of course, it is very inefficient because it costs a lot of calculation because it repeats put and get in vain.

Inherit from standard library

Therefore, another approach is to inherit the Queue class from the standard library queue and create a customized class.

To do this, check how the Queue class manages the values. I think it depends on the editor, but you can see the source code by "Ctrl + click" the class you want to check.

queue.py


class Queue:
    '''Create a queue object with a given maximum size.

    If maxsize is <= 0, the queue size is infinite.
    '''

    #~ Omitted ~

    def put(self, item, block=True, timeout=None):
        '''Put an item into the queue.

        If optional args 'block' is true and 'timeout' is None (the default),
        block if necessary until a free slot is available. If 'timeout' is
        a non-negative number, it blocks at most 'timeout' seconds and raises
        the Full exception if no free slot was available within that time.
        Otherwise ('block' is false), put an item on the queue if a free slot
        is immediately available, else raise the Full exception ('timeout'
        is ignored in that case).
        '''
        with self.not_full:
            if self.maxsize > 0:
                if not block:
                    if self._qsize() >= self.maxsize:
                        raise Full
                elif timeout is None:
                    while self._qsize() >= self.maxsize:
                        self.not_full.wait()
                elif timeout < 0:
                    raise ValueError("'timeout' must be a non-negative number")
                else:
                    endtime = time() + timeout
                    while self._qsize() >= self.maxsize:
                        remaining = endtime - time()
                        if remaining <= 0.0:
                            raise Full
                        self.not_full.wait(remaining)
            self._put(item)
            self.unfinished_tasks += 1
            self.not_empty.notify()

If you look at the put method, it says various things, but it seems that the \ _put method is used to store the value. Now let's look at the \ _put method and so on.

queue.py


from collections import deque


class Queue:
    #~ Omitted ~

    # Put a new item in the queue
    def _put(self, item):
        self.queue.append(item)

    # Initialize the queue representation
    def _init(self, maxsize):
        self.queue = deque()

From the \ _put method, I found that the value is stored in self.queue. In addition, the \ _init method used during initialization tells us that self.queue is a deque instance.

In other words, the Queue class seems to use the deque class of collections to manage the values. I won't go into this here, but the deque class seems to be treated like an array (probably).

With these things in mind, inherit the Queue class and customize it to your liking.

queue_test.py


import queue


class MyQueue(queue.Queue):
    def show_value(self, i):
        print(self.queue[i])

    def sum(self):
        return sum(self.queue)

    def ave(self):
        return self.sum() / self.qsize()


#Operation check
q = MyQueue()
q.put(1)
q.put(2)
q.put(6)
q.put(8)
q.show_value(2)  # 6
print(q.sum())  # 17
print(q.ave())  # 4.25

Now you have created a new MyQueue class that inherits from the Queue class and expanded its functionality. The cost is low because there is no unnecessary operation, and above all, the code is clean. At this level, if you try to do something more complicated, it will make a big difference.

Summary

This time, I inherited the existing library queue.Queue and customized it to my liking.

It is also important to have the ability to push and solve what you can do now. However, if you have the time and the solution, it is smart to choose a simpler method. The code will be cleaner and will be good for the future.

Recommended Posts

Inherit the standard library to find the average value of Queue
I tried to find the average of the sequence with TensorFlow
Find the average / standard deviation of the brightness values in the image
How to find the memory address of a Pandas dataframe value
How to find the area of the Voronoi diagram
Combinatorial optimization to find the hand of "Millijan"
Find the divisor of the value entered in python
[Scientific / technical calculation by Python] Numerical calculation to find the value of derivative (differential)
[Python] Calculate the average value of the pixel value RGB of the object
[Introduction to Python] Basic usage of the library matplotlib
I tried to find the entropy of the image with python
Switch the setting value of setting.py according to the development environment
How to debug the Python standard library in Visual Studio
Find the index of the maximum value (minimum value) of a multidimensional array
Settings to debug the contents of the library with VS Code
How to find the average amount of information (entropy) of the original probability distribution from a sample
How to find the scaling factor of a biorthogonal wavelet
Tips: [Python] Calculate the average value of the specified area with bedgraph
[Python] A simple function to find the center coordinates of a circle
[Python] Find the second smallest value.
About the return value of pthread_mutex_init ()
About the return value of the histogram.
[Python] How to import the library
Supplement to the explanation of vscode
How to find out the number of CPUs without using the sar command
I measured 6 methods to get the index of the maximum value (minimum value) of the list
Setting to debug test by entering the contents of the library with pytest
Hook to Shared Library on Linux to interrupt the behavior of existing binaries
I tried to find the optimal path of the dreamland by (quantum) annealing
A note about the functions of the Linux standard library that handles time
I tried to streamline the standard role of new employees with Python
Find the minimum value of a function by particle swarm optimization (PSO)
I tried to display the altitude value of DTM in a graph
Dispersion with Python's standard library and Pillow Find the RGB standard deviation of the image and determine if it is monochromatic
Find the maximum value python (fixed ver)
Find the area of the union of overlapping rectangles
The story of trying to reconnect the client
Script to change the description of fasta
10 methods to improve the accuracy of BERT
How to check the version of Django
Set the time zone to Japan Standard Time
The story of adding MeCab to ubuntu 16.04
Find the SHA256 value with R (with bonus)
Apply the error propagation formula to the standard error
Use numpy's .flatten () [0] to retrieve the value
Migemo version of the: find command,: mfind
LightGBM predict contributes to the predicted value
Get the value of the middle layer of NN
The story of pep8 changing to pycodestyle
Find the coefficients of the least squares polynomial
Organize the library of competitive professionals ~ Dice ~
Make the default value of the argument immutable
I used gawk to find out the maximum value that goes into NF.
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
A programming beginner tried to find out the execution time of sorting etc.
To output a value even in the middle of a cell with Jupyter Notebook
[Scientific / technical calculation by Python] Analytical solution to find the solution of equation sympy
How to find the coefficient of the trendline that passes through the vertices in Python