#!/usr/bin/env python
# -*- coding: utf -8 -*-
from __future__ import unicode_literals
import pytest

from dll import DoublyLL


class Deque(object):
    """Deque data structure class"""
    def __init__(self, iterable=None):
        """Initiate an instance of Deque using composition."""
        self._doublyLL = DoublyLL(iterable)

    def __len__(self):
        """For the built-in len()"""
        return len(self._doublyLL)

    def append(self, val):
        """Add val to the end of the deque."""
        return self._doublyLL.append(val)

    def appendleft(self, val):
        """Add a value to the front of the deque."""
        return self._doublyLL.push(val)

    def pop(self):
        """Remove a value to the end of the deque."""
        if len(self) == 0:
            raise IndexError("can't pop off an empty deque.")
        return self._doublyLL.shift()

    def popleft(self):
        """Remove a value to the front of the deque."""
        if len(self) == 0:
            raise IndexError("can't popleft off an empty deque.")
        return self._doublyLL.pop()

    def peek(self):
        """Peek at a value at the end of the deque."""
        if len(self) == 0:
            return None
        return self._doublyLL.tail_node.value

    def peekleft(self):
        """Peek at a value at the front of the deque."""
        if len(self) == 0:
            return None
        return self._doublyLL.head_node.value

    def size(self):
        """Return size of the deque"""
        return len(self)

#!/usr/bin/env python
# -*- coding: utf -8 -*-
from __future__ import unicode_literals
import pytest

from dll import DoublyLL


class Deque(object):
    """Deque data structure class"""
    def __init__(self, iterable=None):
        """Initiate an instance of Deque using composition."""
        self._doublyLL = DoublyLL(iterable)

    def __len__(self):
        """For the built-in len()"""
        return len(self._doublyLL)

    def append(self, val):
        """Add val to the end of the deque."""
        return self._doublyLL.append(val)

    def appendleft(self, val):
        """Add a value to the front of the deque."""
        return self._doublyLL.push(val)

    def pop(self):
        """Remove a value to the end of the deque."""
        if len(self) == 0:
            raise IndexError("can't pop off an empty deque.")
        return self._doublyLL.shift()

    def popleft(self):
        """Remove a value to the front of the deque."""
        if len(self) == 0:
            raise IndexError("can't popleft off an empty deque.")
        return self._doublyLL.pop()

    def peek(self):
        """Peek at a value at the end of the deque."""
        try:
            return self._doublyLL.tail_node.value
        except AttributeError:
            return None

    def peekleft(self):
        """Peek at a value at the front of the deque."""
        try:
            return self._doublyLL.head_node.value
        except AttributeError:
            return None

    def size(self):
        """Return size of the deque"""
        return len(self)

use try, except instead of checking for len