Dataraflow Week 3: Python Modules, Virtual Environments, Packages, File Handling & Many More

The third week in the DataraFlow internship program kicked off, and as always, I was looking forward to a week filled with refreshing Python concepts. A segment on Python modules was used to open the floor. It is easier to liken modules to a Python library, from which books can be picked at any point in time.
There are several built-in modules in Python with a range of functions and variables, ranging from the ‘math’ module to ‘numpy’ and ‘platform’, amongst others. You can even create your custom module by writing the code in a file with a ‘.py’ extension. Modules can be used by utilizing the import statement, which allows you to import the module by its own name or assign an alias to call it by. There were in-depth lessons on several modules, including the JSON module, the datetime module, and the math module.
#This is used to return the square root of 144
import math as m
print(m.sqrt(144))
import json
members = {
'name': 'Mubashir', 'age' : 22 ,'occupation' : 'Data Scientist'
}
jsonString = json.dumps(members, indent=4)
print(jsonString)
#This is used to print the current date and time
from datetime import datetime
print(datetime.now())
Python Virtual Environments & Packages
Python has a built-in module, ‘venv’, which stands for virtual environment. This is used to create a self-contained directory that contains a Python installation. As such, developers can work on several projects and install the packages needed for each project in their own unique virtual environment. This will ensure that different projects and packages are kept away from the main Python body. Hence, it is generally good practice for developers to make use of virtual environments when working on projects. Here is an example of how to create a virtual environment named tutorial and install the numpy package.
To create a virtual environment, one must first decide which directory they will like to place it
After which they will run the venv module as a script with the directory path:
# C:\Users\Your name> python -m venv tutorial
This will create the tutorial directory if it doesn't exist and also create directories inside it containing a copy of the Python interpreter and various supporting files.
Once the environment is created, it is activated by:
# C:\Users\Your name> tutorial\Scripts\activate
Once activated, numpy can be installed by running the following script:
# C:\Users\Your name> pip install numpy
File Handling
Python handles files with the open() function, which can be used to open files in different modes. I learnt about the four main modes, which are
‘r’ opens a file for reading, and returns an error if said file does not exist.
‘a’ opens a file for appending, and creates the file if it does not exist.
‘w’ opens a file for overwriting, and creates the file if it does not exist.
‘x’ creates the file and returns an error if the file already exists.
The ‘with’ statement is used when opening a file to ensure the file is properly closed once the code block has run. Here is a code sample that stores a list of student records in a JSON file before reading back into a Python object. This was used to demonstrate my mastery of the concept.
import json
from pathlib import Path
file = Path('students.json')
def saveStudents(students):
with open(file, 'w', encoding='utf-8') as newFile:
json.dump(students, newFile, indent=2)
def loadStudents():
with open(file, 'r', encoding = 'utf-8') as newFile:
return json.load(newFile)
students = [
{'name': 'Sam', 'age' : 23, 'grade' : 'A'},
{'name' : 'Chizara', 'age' : 23, 'grade' : 'A'}
]
saveStudents(students)
print(loadStudents())
Handling User Input In Python
Python inputs were one of the first concepts I learnt earlier, so it made my job in this segment much easier. I went over how to ask for user input with the 'input()’ function, and how to validate said user input with ‘try’ and ‘except’ handling methods. As part of my learning, I built a math quiz that asked the user 5 random questions involving basic arithmetic and kept a count of the user’s score, which was to be printed at the end of the quiz.
import random
def randomQuestion():
operators = ['+', '-', '*', '/']
randomOperator = random.choice(operators)
if randomOperator == '*':
randomA, randomB = random.randint(0, 100), random.randint(0, 100)
question = f'{randomA} * {randomB}'
ans = randomA * randomB
elif randomOperator == '+':
randomA, randomB = random.randint(0, 100), random.randint(0, 100)
question = f'{randomA} + {randomB}'
ans = randomA + randomB
elif randomOperator == '-':
randomA, randomB = random.randint(0, 100), random.randint(0, 100)
question = f'{randomA} - {randomB}'
ans = randomA - randomB
elif randomOperator == '/':
randomA, randomB = random.randint(0, 100), random.randint(0, 100)
question = f'{randomA} / {randomB}'
ans = randomA / randomB
return question, ans
score = 0
for i in range(1, 6):
question, ans = randomQuestion()
ans = round(ans, 2)
guess = input(f'What is {question} to two decimal places?: ')
try:
guess = float(guess)
if guess == ans:
print('You answered correctly')
score += 1
else:
print(f'You answered incorrectly, The answer is: {ans:.2f}')
except:
print('Please input a valid number')
print(f'Your score is {score}/5')