This is a bit longer blog. So please bear with me. I am trying to get to that millionth line as soon as possible!!! :-)
Let's get right into it.
So another week flew by. But it was a great week. We learned about Classes, Objects, and Methods. Moreover, we also learned about Virtualenv and how to create a virtual environment, and writing tests for our software.
What is Virtualenv?
Virtualenv is a way/tool we can use to seperate different Python environments for different projects. It allows us to create an isolated workspace for a Python application.
Why do we want to do something like this?
Because it is designed to allow us to work on multiple projects with different dependencies at the same time on the same machine.
For e.g. let's say we are working on a multiple projects and they all rely on a single package for e.g. flask or Django, and each and every projects may be using a different version of Django or flask. Now if we go and upgrade that packages on your global sites packages, then it could break couple of websites. So it would be better if each of those projects has an isolated environment where they have only the dependencies and the packages that they need and the specific version that they need. So that's what Virtualenv allow us to do. It allows us to make those different Python environments.
So how does it works and how do you create a virtual environment?
1st step: if you already don't have Virtualenv installed on your machine.
$ pip install virtualenv
To see list of all packages and it's version on our global site. Type:
$ pip list
Now let's use Virtualenv to make a few different Python environments.
First, let's create a directory first where we can keep all these in one place.
$ mkdir environments
$ cd environments - you'll notice that the directory is currently empty.
To make our first virtual environment:
$ virtualenv project1_env
You can see what it says: "Installing setuptools, pip, wheel...". What it means is that it went ahead and installed setup tool and pip for us. So as soon as we go into that environment then you can use those to start installing packages.
In order to activate this new Python environment all we have to do is type 'source' and the name of the environment. In the our case it's 'project1_env':
$ source project1_env/bin/activate
Now we are in our new Python environment. The way you can tell this is - it will now add (project1_env) to our command line prompt. For e.g:
(project1_env) ViksMac:environment vik$
Another way to check it is by:
$ which python
You'll notice that the path to the Python we are using in this virtual environment is within our environment/project1_env/bin/python
We can do the same thing for pip:
$ which pip
You'll notice that the pip is also inside of our project environment: environment/project1_env/bin/pip
Now if we enter:
$ pip list we will see that we don't have all those global site packages we have whenever we are outside of this virtual environment. But we only have the following packages:
Now let's go ahead and install a couple of new packages into this new Python environment.
I have picked a couple of random packages. They are numpy and pytz.
$ pip install numpy
$ pip install pytz
Now if we do
$ pip list , we should get the following list of all the packages in our virtual environment:
We can export all these packages and their version numbers to use in another project. We can do that by:
$ pip freeze --local > requirements.txt
Very important to point to remember about 'pip freeze --local'. The command pip freeze outputs all installed modules(including version numbers) to requirement.txt file. The --local flag prevents Pip from printing globally installed packages in a virtual environment. In the above example, we are not using any global site packages. But yes you can use the global packages in your local environment. So if we had used them, we would have all those global site packages available to us. So pip freeze --local would have taken the local dependencies only.
$ cat requirements.txt
We can see above we have all our packages and version numbers.
Now let's say we want to get out of our virtual environment. To get out:
Our (project1_env) prompt no longer shows up. If we type
$ which python It'll show the Python we are using in our global environment. Also, if we type:
$ pip list we will see the list of all the global packages.
Now let's say we want to get rid of our project1_env
We already have deactivated it. So all we have to do is delete it. To delete:
$ rm -rf project1_env/
So now if we type:
$ ls we only have requirements.txt file.
We can also install and specify a specific version of Python. To install:
$ virtualenv -p /usr/bin/python2.7 project2_env
You can use any name instead of project2_env
$ source project2_env/bin/activate
You'll notice that our prompt has changed.(project2_env) ViksMac:environment vik$
$ which python
Will show in which current Python we are in.
$ python --version
$ pip install -r requirement.txt
It'll go to our requirement.txt file and grab all the packages in that file. Right now we have numpy and pytz packages in our requirement.txt file.
$ pip list will give list of all the packages and their versions.
Now you can start working on your project!
2. Object Oriented Programming (OOP)
(Class and Object)
A class is a blueprint for creating instances. For e.g. in the below example, each unique employee that we will create will be an instance of the employee class. For e.g.:
If we run the program, we will see:
Both of the employee are objects, and they're both unique. They both have a different location in memory.
There are instance variables and class variables. Instance variables contain data/attributes that is unique to each instance. We can create instance variables for each employee manually. For e.g.:
We don't want to set these variable manually every time because that's a lot of code and also it's also prone to mistakes. There's a better way to create. There's a special init (initialize) method.
So inside our Employee class we will create init method.
As a convention we should call the instance self (you can call it whatever you want though). After self we can specify what all the other argument that we want to accept. We will give first, last name and set all of these instances variables within our init method.
Now we can pass in the value to our employee class that we specified in our init method.
Now when we run this, new objects are created.
3. Testing with pytest
A test is a code that checks the validity of other code to see if something is working (or not working). It's a proof that the code we've written works and it gets a safety net if we make any changes to our code in the future.
What is pytest?
Pytest is the most popular Python package for testing. It has a rich ecosystem of testing plugins and extensions for various purposes.
1st step is to create a virtual environment in your directory:
$ python3 -m venv env
We then have to activate it:
$ source env/bin/activate
We then need to install pytest. To install:
$ pip install pytest
To check the pytest version:
$ pytest --version
For our this week's assignment, we were asked to pick one of the exercises we did during the class and write a test for it. I choose the following exercise.
The name of the file is tables.py
Test file name is test_tables.py
I was getting some Assertion Error.
However, after searching for an answer online, I made the following changes:
I got the following results:
After I published this blog I learned that in order to successfully test the program, the program has to return some value. In the above example I was printing it instead. So below are some of the changes I made to the program in order to run(and pass) the test.
If you have any suggestions, please let me know.
Thank you for reading.