Skip to content
Snippets Groups Projects
brodriguez8774's avatar
4ea80719

Python - Custom Stack and Queue Implementation

Description

Implementation of custom Stack and Queue data structures. Includes both Unittesting and PyTest testing, to prove knowledge of both.

Python Environment

This project is tested using Python3.7. Written using PyCharm. Tested with Pycharm 2017.2.3 and 2019.2 versions.

Creating a New Env

Most installations of Python3 will allow you to simply run python3.7 -m venv ./.venv to create a new Python3.7 environment called ".venv" at the current directory folder.

Once created, load this environment with source .venv/bin/activate on Linux or . .venv/Scripts/activate on Windows.

Third Party Libraries

  • If any additional libraries have been used, then they will be found in requrements.txt at the project root.
  • To install, load your local Python Environment and run pip install -r requirements.txt when at the project root.

Running the Program

Run the program via python ./main.py while at the project root.

Expected Output

None so far.

Testing with UnitTest

Unittests come built into python. No need for third party packages.

Running a Single UnitTest

The following will run tests from a single file.

NOTE: For the file path of these two commands, replace any / characters with . instead.
So for example, tests/my_tests/test_file.py would turn into tests.my_tests.test_file.py.

  • Terminal: From a terminal, run python -m unittest <path_to_file>

  • Pycharm: Create a new unittest configuration with the properties:

    • Target: custom
    • Additional Arguments: python -m unittest <path_to_file>
    • Python Interpreter: <your_preferred_interpreter_environment>
    • Working Directory: <project_root>

Running Multiple UnitTests

The following will "run all tests in a given directory".
NOTE: For these to work as intended, there needs to be an "init.py" file in each project subdirectory that the tests access (even despite using Python3).

  • Terminal: From a terminal at the project root, run python -m unittest discover <path_to_folder> -p "*.py" -t ./

  • Pycharm: Create a new unittest configuration with the properties:

    • Target: custom
    • Additional Arguments: discover -s <path_to_folder> -p "*.py" -t ./
    • Python Interpreter: <your_preferred_interpreter_environment>
    • Working Directory: <project_root>

PyTest

PyTest is an alternative to UnitTests, using a third party library. To install, run pip install pytest.

Running a Single PyTest

The following will run all tests from a single file.

  • Terminal: From a terminal, run pytest -q <path_to_file>.

  • Pycharm: Create a new py.test or pytest configuration (depending on your Pycharm version) with the properties:

    • Target: custom
    • Additional Arguments: -q <path_to_file>
    • Python Interpreter: <your_preferred_interpreter_environment>
    • Working Directory: <project_root>

Running Multiple PyTests

The following will "run all tests in root folder".

NOTE: You cannot have an __init__.py at project root.
If you get a ModuleError/ImportError, you also may have to create a blank conftest.py at project root.

  • Terminal: From a terminal at the project root, run pytest.

  • Pycharm: Create a new py.test or pytest configuration (depending on your Pycharm version) with the properties:

    • Target: custom
    • Python Interpreter: <your_preferred_interpreter_environment>
    • Working Directory: <project_root>

References

Logging Logic

The contents of resources/logging.py (and all associated logic) originally started from me trying to learn how to log information through Django (the Python Web Framework) using a Dictionary type format. (See https://docs.djangoproject.com/en/dev/topics/logging/ for reference).

In 2017, I started expanding and changing this logic to be usable in Non-Django applications, such as personal projects and school assignments. After repeatedly importing and updating as needed, the logging.py file has slowly changed into what it is today.

Logging files can be found in resources/logs/.

UnitTesting

Getting module imports to work correctly (-t arg): https://docs.python.org/3/library/unittest.html#test-discovery

Running UnitTests from Console: https://stackoverflow.com/a/15630454.

Running UnitTests from Pycharm: Trial and error with modifying above references.

Running a single UnitTest: https://stackoverflow.com/a/43029618

UnitTesting Subtests

Using subtests: https://www.caktusgroup.com/blog/2017/05/29/subtests-are-best/

Using Pytest

General Documentation: https://docs.pytest.org/en/latest/contents.html#toc

Running PyTests from Pycharm: https://www.jetbrains.com/help/pycharm/run-debug-configuration-py-test.html

Running a single PyTest: https://docs.pytest.org/en/latest/getting-started.html#create-your-first-test

Issues with module imports: https://stackoverflow.com/a/50610630