From e6a3a2622311b06222daafbbcea871c24248845d Mon Sep 17 00:00:00 2001 From: ashegde Date: Sun, 17 Jun 2018 18:53:38 +0530 Subject: [PATCH] feat(examples): standard library --- codes/session_8/libEg1.py | 62 ++++++++++++++++++++++++++++++++++ codes/session_8/libEg2.py | 45 ++++++++++++++++++++++++ codes/session_8/libEg3.py | 21 ++++++++++++ codes/session_8/libEg3.test.py | 24 +++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 codes/session_8/libEg1.py create mode 100644 codes/session_8/libEg2.py create mode 100644 codes/session_8/libEg3.py create mode 100644 codes/session_8/libEg3.test.py diff --git a/codes/session_8/libEg1.py b/codes/session_8/libEg1.py new file mode 100644 index 0000000..9a3f089 --- /dev/null +++ b/codes/session_8/libEg1.py @@ -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) ) + diff --git a/codes/session_8/libEg2.py b/codes/session_8/libEg2.py new file mode 100644 index 0000000..0a14f2a --- /dev/null +++ b/codes/session_8/libEg2.py @@ -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) ) + diff --git a/codes/session_8/libEg3.py b/codes/session_8/libEg3.py new file mode 100644 index 0000000..3699c4f --- /dev/null +++ b/codes/session_8/libEg3.py @@ -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 \ No newline at end of file diff --git a/codes/session_8/libEg3.test.py b/codes/session_8/libEg3.test.py new file mode 100644 index 0000000..241190c --- /dev/null +++ b/codes/session_8/libEg3.test.py @@ -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() \ No newline at end of file