Posts

Test Driven Development: How and Why

Image
Test Driven Development(TDD) is a very useful approach towards code development. It keeps the developer from having to second guess every piece of code he writes. The TDD workflow goes like this: First, you write a block of code. Next, write a unit test for that block of code and ensure it passes. If there are any issues, refactor. This workflow also makes your design more intentional as it elucidates what the code is suppose to do. Each unit test is a test case and a grouping of tests makes up a test suite Unit tests should run on memory and not rely heavily on databases or even filesystems.  It comes down to this: the behavior of the code is serving the original purpose intended by the developer. If there is suppose to an exception then code should raise it. Reduce risk it is the manifestation of the DevOps concept of 'Shifting Left. By discovering any issues to the left side of the Software Development Lifecycle(SDLC), the cost of fixing it is reduced. Unit tests will guard agai...

Python - Import Module Error - Import .py file from another Parallel Folder in Visual Studio Code

Image
While using  Visual Studio Code , I encountered an issue where the   .py file from src folder cannot be found in the tests folder test case .py during runtime. When the test case file .py was run I, I was getting error Import Module Error - Startup not found.  This is import Module Error is common when detecting and running files in a parallel folder in Visual Studio Code. It almost drove me nuts as the folder Sharing this so that any developers using Python in Visual Studio Code will be spared the agony of trying to fix this 'import module error' issue. It is a nice IDE to use and I prefer it over Pycharm. Steps to workaround 1) Add the init.py file to the folders  You need a __init__.py for folder to be recognized as module 2) Add a Setup.py At this point, I was  able to run the unit test from Conda prompt(with the environment activated of course)Unit tests are running. Great. But for VS code, steps 1 and 2 are not enough. The tests were still failing and...

Using Git pre-commit hooks: Automated Testing of Code with Python Unit Tests

Image
This post show how a pre-commit git hook is implemented. The git hook is used to trigger a script which runs the unit test using my conda environment. Using pre-commit hooks will prevent commit code with issues into your local repo and is preventing the developer from eventually pushing a broken build into the a remote repository This is in tandem with a test driven approach, instead of having to run my tests separately, then commit, I have only need to commit. Personally, I find them to be really useful. For example, you can use them stop commits if files contain certain confidential data. The pre-commit hook can be bypassed, but that's not what this post is about. We as developers need to be assured that we are not messing up every step of the way.  Using pre-commit hooks can take some that pressure off as we work by automating the code testing as we work. Do note that this works on a per-commit basis. It does not retrospectively check for for previous commits. Steps to create an...

Five Things I Wish I Knew When Starting with Python

Image
  There are definitely more than five things, but here are five that really struck me. 1. Create specific environment for project and only install the required packages By creating a virtual environment, Python libraries specific to a project remain within that environment. Any dependencies(packages) installed in one environment is isolated from another. This might prevent version clashes between packages or unknowingly using a different package version across projects. Creating project specific environments is good house-keeping. This should be done even before   any code gets written.  You can create a virtual environment with Python Virtual Environment or with Conda. The example below is listing all available conda environments and creating a new one with conda create . Tangibly, the environment is a folder location on a drive. That you can activate with a command to manage 2. List/Dictionary Comprehensions  What would life be without List/Dictionary Comprehensi...

Parsing XML Files with Python: Good Coding Practice

Image
This idea of this post came from building a Python application to parse some XML. The XML file is an Alteryx Workflow file; the application was conceived to check the values of attributes and texts in certain nodes to ensure proper usage of Alteryx Tools. Alteryx is a data analytics software. As the code was being built, it felt like a good practice for getting a hang of Python's data types and how to manipulate them. XML data is a common sight and it is helpful to know how to work with it. By the way, the XPATH support is elementary. Supported syntax can be found at the documentation page More info can be found at: https://docs.python.org/3/library/xml.etree.elementtree.html This article is not an entire walkthrough of the code, rather some snippets of it of what was used. For new comers to coding, it can seem like there are so many things learn and do, options paralysis might kick in. Since coding generally is about dealing with data types to achieve some end goal(grossly oversim...