Python is a popular language, with lots of libraries and has become one the leading languages used for data science and machine learning.
I felt this would be a great way to show people that I could learn a new language and be able to do some programming and apply design techniques to it.
I am not perfect at the other languages I know, ruby and javascript, but I feel comfortable in using them, so doing this is also proving to myself that I can learn a language independently.
After Makers, I already have a framework of how to go about this and have a plan which I will document my progress here.
So far the plan I have set myself is:
- Setup
- Learn the syntax
- Create a test driven Fizzbuzz program
- Create a test driven tech test program and apply object-oriented design
- Creating another program, something new like a game using the terminal.
- Create a web app or game (any sort of project that uses libraries and/or frameworks)
The setup
Aim: Setup system to run a basic test driven 'hello world' program.
To use python I needed to install the necessary files to create and run python scripts and tests.
NOTE: This is based on using MacOS and with homebrew installed
I installed Python using brew install python3 in the terminal.
NOTE: There are two versions of Python, version 2 and version 3. I would suggest going for version 3 (as done above) as this generally used with web framework Django (and I believe helps if using Windows).
Next, I created a project skeleton, using this tutorial, I would follow this and run the tests.
NOTE: A project skeleton, is just how your files and folders are setup. It is always good to separate your tests from the source code via different folders. This does help with big projects with lots of files.
I used an extra library, to add colour to my tests (I miss RSpec): pip install rednos
NOTE: 'Pip' is a library manager just like bundler for ruby, or Maven for Java. Pip comes installed with the latest version of Python.
So to check everything is running fine, I would enter the project skeleton directory ( ie ' cd <project name>' ) and run tests with this command
nosetests --rednose -v
The '-v' add information, ie a description of the test, to show what tests were passed.
This will produce the following output:
NOTE: You can just run tests using nosetests if you want a simple output, just a series of dots for passing dots.
NOTE: Adding '--with-coverage' at the end of the above statement, you can get the test coverage of your code. Focus only on the files that you have created rather than any extra files displayed. Doing TDD means getting 100% coverage!!!
Note: This is such a common command I added this as an alias so I could just type in pt (short for python test). Check this tutorial if you want to do this, I have created quite a few aliases for common git commands too. A really useful one I found is to create a new folder and enter it
NOTE: This is based on using MacOS and with homebrew installed
I installed Python using brew install python3 in the terminal.
NOTE: There are two versions of Python, version 2 and version 3. I would suggest going for version 3 (as done above) as this generally used with web framework Django (and I believe helps if using Windows).
Next, I created a project skeleton, using this tutorial, I would follow this and run the tests.
NOTE: A project skeleton, is just how your files and folders are setup. It is always good to separate your tests from the source code via different folders. This does help with big projects with lots of files.
I used an extra library, to add colour to my tests (I miss RSpec): pip install rednos
NOTE: 'Pip' is a library manager just like bundler for ruby, or Maven for Java. Pip comes installed with the latest version of Python.
So to check everything is running fine, I would enter the project skeleton directory ( ie ' cd <project name>' ) and run tests with this command
nosetests --rednose -v
The '-v' add information, ie a description of the test, to show what tests were passed.
This will produce the following output:
$ nosetests -v --rednose
tests.NAME_tests.test_basic ... passed
-----------------------------------------------------------------------------
1 test run in 0.003 seconds (1 test passed)
NOTE: You can just run tests using nosetests if you want a simple output, just a series of dots for passing dots.
NOTE: Adding '--with-coverage' at the end of the above statement, you can get the test coverage of your code. Focus only on the files that you have created rather than any extra files displayed. Doing TDD means getting 100% coverage!!!
Note: This is such a common command I added this as an alias so I could just type in pt (short for python test). Check this tutorial if you want to do this, I have created quite a few aliases for common git commands too. A really useful one I found is to create a new folder and enter it
So to create a new project, I need to copy the project skeleton and rename the folder as the name of the project. Then change all instances of 'NAME' in the files and filenames and I am good to go.
NOTE: Before going on to creating a simple program with tests, I learned some of the syntax first.
To complete the setup, I created a basic hello app, which takes a name and returns "Hello <name>" and tested it. To view this and see how the project skeleton was altered check the code and browse through the files here:
Issues
I had a lot of issues setting up a project. I like to separate my tests and program scripts into separate folders, but running the tests caused so many issues with finding the program for the test to run against. Eventually, I found Python the hard way which had a very easy to follow tutorial that helped me setup a project skeleton. It is also a great way of learning the syntax too.
Using nose to run my tests, solved so many issues that running 'unittest' or the script using python command, were giving me.
Naming the descriptions of tests were not great, so I decided to use a naming convention so the tests appear in order on the terminal to the order on the test script, rather than alphabetically.
For example
test_1_description
test_2_description
I also added doc strings """description in here""" at the start of each test, which gave a nice description of what was happening, kind of like an 'it block' in RSpec. This would show when running nosetests with -v flag in terminal.
No comments:
Post a Comment