HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Implementation of a queue for digicode-like system

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
systemlikeforimplementationqueuedigicode

Problem

What it is about

I created an application where you need to push invisible buttons in a certain order to unlock the advanced mode. To handle this, I wanted to use a queue (FIFO) of my own, adapted to my problem.

Every time I click a button, its ID is added to the queue with addItem, and the password gets checked with checkCorrect. It is meant to work similarly to digicode doors. The size attribute determines how many values the queue can remember, hence the maximum length of the password.

What I'd like to know

Do you think this is a good code? In which ways can it be improved? Are there other ways (with smaller / more elegant code) to implement this?

The code

class MyQueue():
    def __init__(self, size, default_value=None):
        self.size = size
        self.content = [default_value]*size
        self.end = self.size-1
    def addItem(self, value):
        self.end = (self.end+1) % self.size
        self.content[self.end] = value
    def printQueue(self):
        print 'end='+str(self.end)+' - '+str(self.content)
    def checkCorrect(self, password):
        pw = list(reversed(password))
        i = 0
        j = self.end
        if len(pw) > self.size:
            return False
        while True:
            if pw[i] <> self.content[j]:
                return False
            else:
                i += 1
                j = (j-1) % self.size
                if i == len(pw):
                    return True

if __name__=='__main__':
    q = MyQueue(size=4, default_value=0)
    q.addItem(1)
    q.addItem(8)
    q.addItem(8)
    q.addItem(5)
    q.addItem(7)
    q.addItem(3)
    print q.checkCorrect([5, 7, 3])
    print q.checkCorrect([2, 7, 3])

Solution

Some PEP8 guidance:

class MyQueue(): here, you can remove the redundant parentheses as you're not passing any parameter to it. So you will have class MyQueue.

When inside of a class, it's recommended to have one space between your methods:

def addItem:
    ....

def printQueue:
    ....


It's also specified in PEP8 the following (methods naming):


Use the function naming rules: lowercase with words separated by
underscores as necessary to improve readability.

That said, addItem would become add_item.

More

Don't use <> operator. It's not usual. I strongly recommend you using != operator (which is the common way of doing this) when you're trying to say not equal. Later on, you might want to use this with a more recent Python version (say Python 3.5). This way, you won't have any issues.

Related to portability, you might also want to add parentheses when using print(). It's not necessary since you're using Python 2.7, but it might help you in the future.

Classes and methods inside them usually have docstrings. To quote the documentation:


A docstring is a string literal that occurs as the first statement in
a module, function, class, or method definition. Such a docstring
becomes the doc special attribute of that object.


All modules should normally have docstrings, and all functions and
classes exported by a module should also have docstrings.

That said:

class MyQueue:
    """Some text to describe what you're class does"""
    ....

    def add_item(self, value):
        """ what this method does """
        ...


In a perfect world, which obviously we're not living in, I would've expect to see some push / pop methods or queue / dequeue ones since we're talking about a queue implementation and it's way of handling things (FIFO). Perhaps add_item might be push_item.

More to come

Code Snippets

def addItem:
    ....

def printQueue:
    ....
class MyQueue:
    """Some text to describe what you're class does"""
    ....

    def add_item(self, value):
        """ what this method does """
        ...

Context

StackExchange Code Review Q#134266, answer score: 3

Revisions (0)

No revisions yet.