Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract base class for DS #56

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pygorithm/data_structures/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import inspect


class BaseDataStructure(object):

@classmethod
def get_code(cls):
"""
returns the code for the current class
"""
return inspect.getsource(cls)

@staticmethod
def time_complexities():
"""
Prints time complexities of different operations
"""
# TODO: Is it really required for all data structres?
# If it is, change on `raise NotImplementedError`,
# but for now not all classes have this method.
pass
21 changes: 4 additions & 17 deletions pygorithm/data_structures/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
Created On: 3rd August 2017
"""
from string import ascii_letters
import inspect

from .base import BaseDataStructure

class Stack(object):

class Stack(BaseDataStructure):
"""
Stack object
"""
Expand Down Expand Up @@ -66,15 +67,8 @@ def size(self):
"""
return len(self.stack)

@staticmethod
def get_code():
"""
returns the code for current class
"""
return inspect.getsource(Stack)


class InfixToPostfix(object):
class InfixToPostfix(BaseDataStructure):
"""InfixToPostfix
get the postfix of the given infix expression
"""
Expand Down Expand Up @@ -136,10 +130,3 @@ def infix_to_postfix(self):
while not self.my_stack.is_empty():
postfix.append(self.my_stack.pop())
return ' '.join(postfix)

@staticmethod
def get_code():
"""
returns the code of the current class
"""
return inspect.getsource(InfixToPostfix)
31 changes: 29 additions & 2 deletions tests/test_data_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,31 @@
trie)


class TestStack(unittest.TestCase):
class TestDSInterfaceMixin(object):

DS_Class = None

def _check_ds_class_set(self):
if self.DS_Class is None:
raise ValueError(
'When inherit from TestDataStructureMixin, set DS_Class class variable'
)

def test_get_code_method_exists(self):
import inspect

self._check_ds_class_set()
self.assertEqual(self.DS_Class.get_code(), inspect.getsource(self.DS_Class))

def test_time_complexities_method_exists(self):
self._check_ds_class_set()
self.assertTrue(hasattr(self.DS_Class, 'time_complexities'))


class TestStack(TestDSInterfaceMixin, unittest.TestCase):

DS_Class = stack.Stack

def test_stack(self):
myStack = stack.Stack() # create a stack with default stack size 10
myStack.push(2)
Expand All @@ -30,7 +54,10 @@ def test_stack(self):
self.assertTrue(nullStack.is_empty())


class TestInfixToPostfix(unittest.TestCase):
class TestInfixToPostfix(TestDSInterfaceMixin, unittest.TestCase):

DS_Class = stack.InfixToPostfix

def test_infix_to_postfix(self):
myExp = 'a+b*(c^d-e)^(f+g*h)-i'
myExp = [i for i in myExp]
Expand Down