6


I have had to change from python 2.7 to 2.6.
I've been using a deque with the maxlen property and have been checking what the maxlen is. Apparently you can use maxlen in python 2.6, but in 2.6 deques do not have a maxlen attribute.
What is the cleanest way to check what the maxlen of a deque is in python 2.6?

In 2.7:

from collections import deque
d = deque(maxlen = 10)
print d.maxlen

In 2.6 the deque can be used and the maxlen works properly, but maxlen is not an attribute that can be referred to.

Cheers

simonb
  • 2,777
  • 2
  • 18
  • 16

4 Answers4

6

I would create my own deque by inheriting from collections.deque. It is not difficult. Namely, here it is:

import collections

class deque(collections.deque):
    def __init__(self, iterable=(), maxlen=None):
        super(deque, self).__init__(iterable, maxlen)
        self._maxlen = maxlen
    @property
    def maxlen(self):
        return self._maxlen

and this is the new deque at work:

>>> d = deque()
>>> print d
deque([])
>>> print d.maxlen
None
>>> d = deque(maxlen=3)
>>> print d
deque([], maxlen=3)
>>> print d.maxlen
3
>>> d = deque(range(5))
>>> print d
deque([0, 1, 2, 3, 4])
>>> print d.maxlen
None
>>> d = deque(range(5), maxlen=3)
>>> print d
deque([2, 3, 4], maxlen=3)
>>> print d.maxlen
3
Muhammad Alkarouri
  • 23,884
  • 19
  • 66
  • 101
2

maxlen is a new part of deque that was first implemented in Python 2.7. It just doesn't exist in Python 2.6.

That said, there are a few things you can do:

  1. Create a new class that inherits all the methods and attributes from deque but also implements a maxlen attribute.
  2. Adapt your code so that maxlen isn't necessary
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
2

I would create my own queue class that inherits from deque. Something like:

class Deque(deque):
    def __init__(self,*args,**kwargs):
        deque.__init__(self, *args, **kwargs)
        self.maxlen = kwargs.get('maxlen',None)

>>>d = Deque(maxlen=10)
>>>d.maxlen
>>>10
mr.freeze
  • 13,731
  • 5
  • 36
  • 42
1

Well, if you don't have the maxlen attribute, you can just steal it from the representation:

>>> import re

>>> d = deque(maxlen=42)

>>> d.__repr__()
'deque([], maxlen=42)'

>>> int(re.sub("\)$","",re.sub(".*=","",d.__repr__())))
42

Yes, I know it's horrible. I would prefer to upgrade to 2.7 myself but sometimes we're not given the power we desire, and we have to resort to kludges like this.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Why would you post such dark wizardry? – Rafe Kettler Oct 26 '10 at 03:08
  • 1
    Hmm, I don't know. Maybe because it works? I specifically said it was not the preferred solution but, if your hands are tied, then it'll get you through. – paxdiablo Oct 26 '10 at 03:14
  • And it also fails when a deque is created with `maxlen = None`. – Muhammad Alkarouri Oct 26 '10 at 03:53
  • 2
    Yes, it will. Try not to think of my code snippets as full blown applications which can handle every possible scenario, more a helping hand in pointing out one way to do it :-) The best solution I've seen is the wrapper classes (+1 for Muhammad and nFreeze). This one should only be used where a wrapper is not possible (unlikely though that is). And you're dead right, you _should_ catch the `None` before trying to `int()` it. – paxdiablo Oct 26 '10 at 04:05