Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions codes/session_8/libEg1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# How to Run? Open Terminal> python3 libEg1.py
# Read the code comments and output on the terminal

# Objective: Know about standard built-in library

# Note: avoid using 'from os import *'.
# This will keep 'os.open()' from shadowing the built-in 'open()' function
import os

# Return the currect directory
print('Current Directory: ', os.getcwd())

# Run command mkdir in the system shell
# Return '0' means directory got created successfully
# Return '256' means directory got created failed because its already exists
print('Create directory "temp": ', os.system('mkdir temp'))

# Change current working directory
print('Change currect directory to "temp": ', os.chdir('temp'))

print('\n')

# Return a list of all module functions
print('List of all module functions: ', dir(os))

print('\n')

# System libraries
import sys

# Arguments are stored in the sys module's argv attributes as a list
print('Argv list: ', sys.argv) # python3 libEg1.py Hello => ['libEg1.py', 'hello']

print('\n')

# Output
sys.stdout.write('[STDOUT]: This is ok\n')

# Error output redirection and program termination
sys.stderr.write('[STDERR]: This is not ok\n')

print('\n')

# Mathematics
import math

print( math.cos(math.pi / 4) )
print( math.log(1024, 2) )

print('\n')

# Random
import random

print('Random choice: ', random.choice(['JavaScript', 'Python', 'Go']) ) # execute multiple times to see the output

# Statistics
import statistics

data = [2.5, 1.57, 4.32, 5.67, 8.45]
print('Statistics mean: ', statistics.mean(data) )

45 changes: 45 additions & 0 deletions codes/session_8/libEg2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# How to Run? Open Terminal> python3 libEg2.py
# Read the code comments and output on the terminal

# Objective: Know about standard built-in library

# Dates and Times
from datetime import date

# Get today's date
now = date.today()
print("Today's date: ", now)

# Set date
customDate = date(2000, 12, 2)
print("Custom date: ", customDate)

print('\n')

# Data compression
import zlib

print('Data Compression Try 1: \n')

dataStr1 = b'Hello world! my name is Ashwin' # prefix 'b' means bytes-like object
print('Original: ', len(dataStr1) )

comDataStr1 = zlib.compress(dataStr1)
print('Compress: ', len(comDataStr1) )

deComDataStr1 = zlib.decompress(comDataStr1)
print('Decompress: ', len(deComDataStr1) )

print('\n')

print('Data Compression Try 2: \n')

dataStr2 = b'Ashwin Ashwin Ashwin Ashwin' # prefix 'b' means bytes-like object
print('Original: ', len(dataStr2) )

comDataStr2 = zlib.compress(dataStr2)
print('Compress: ', len(comDataStr2) )

deComDataStr2 = zlib.decompress(comDataStr2)
print('Decompress: ', len(deComDataStr2) )

21 changes: 21 additions & 0 deletions codes/session_8/libEg3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# How to Run? Open Terminal> python3 libEg3.py
# Read the code comments and output on the terminal

# Objective: Know about standard built-in library

# Doctest
# The doctest module provides a tool for scanning a module
# and validating tests embedded in a programs docstrings.
# Try changing '20' in below example to see the doctest

def average(values):
"""Computes the arithmetic mean of a list of numbers
>>> print(average([10, 20, 30]))
20
"""
return sum(values) / len(values)

import doctest
doctest.testmod()

# Also, see libEg3.test.py file for unittest module example
24 changes: 24 additions & 0 deletions codes/session_8/libEg3.test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# How to Run? Open Terminal> python3 libEg3.test.py
# Read the code comments and output on the terminal

# Objective: Know about standard built-in library

# The unittest module is not as effortless as the doctest module,
# but it allows a more comprehensive set of tests to be maintained in a separate file

import unittest
from libEg3 import average

class TestStatisticalFunctions(unittest.TestCase):

def test_average(self):

self.assertEqual(average([10, 20, 30]), 20)

with self.assertRaises(ZeroDivisionError):
average([])

with self.assertRaises(TypeError):
average(20, 30, 70)

unittest.main()