Skip to content

Commit 18e11c3

Browse files
authored
Merge pull request #14 from the-code-experiments/develop
feat(examples): standard library
2 parents 77c0fa4 + e6a3a26 commit 18e11c3

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

codes/session_8/libEg1.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# How to Run? Open Terminal> python3 libEg1.py
2+
# Read the code comments and output on the terminal
3+
4+
# Objective: Know about standard built-in library
5+
6+
# Note: avoid using 'from os import *'.
7+
# This will keep 'os.open()' from shadowing the built-in 'open()' function
8+
import os
9+
10+
# Return the currect directory
11+
print('Current Directory: ', os.getcwd())
12+
13+
# Run command mkdir in the system shell
14+
# Return '0' means directory got created successfully
15+
# Return '256' means directory got created failed because its already exists
16+
print('Create directory "temp": ', os.system('mkdir temp'))
17+
18+
# Change current working directory
19+
print('Change currect directory to "temp": ', os.chdir('temp'))
20+
21+
print('\n')
22+
23+
# Return a list of all module functions
24+
print('List of all module functions: ', dir(os))
25+
26+
print('\n')
27+
28+
# System libraries
29+
import sys
30+
31+
# Arguments are stored in the sys module's argv attributes as a list
32+
print('Argv list: ', sys.argv) # python3 libEg1.py Hello => ['libEg1.py', 'hello']
33+
34+
print('\n')
35+
36+
# Output
37+
sys.stdout.write('[STDOUT]: This is ok\n')
38+
39+
# Error output redirection and program termination
40+
sys.stderr.write('[STDERR]: This is not ok\n')
41+
42+
print('\n')
43+
44+
# Mathematics
45+
import math
46+
47+
print( math.cos(math.pi / 4) )
48+
print( math.log(1024, 2) )
49+
50+
print('\n')
51+
52+
# Random
53+
import random
54+
55+
print('Random choice: ', random.choice(['JavaScript', 'Python', 'Go']) ) # execute multiple times to see the output
56+
57+
# Statistics
58+
import statistics
59+
60+
data = [2.5, 1.57, 4.32, 5.67, 8.45]
61+
print('Statistics mean: ', statistics.mean(data) )
62+

codes/session_8/libEg2.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# How to Run? Open Terminal> python3 libEg2.py
2+
# Read the code comments and output on the terminal
3+
4+
# Objective: Know about standard built-in library
5+
6+
# Dates and Times
7+
from datetime import date
8+
9+
# Get today's date
10+
now = date.today()
11+
print("Today's date: ", now)
12+
13+
# Set date
14+
customDate = date(2000, 12, 2)
15+
print("Custom date: ", customDate)
16+
17+
print('\n')
18+
19+
# Data compression
20+
import zlib
21+
22+
print('Data Compression Try 1: \n')
23+
24+
dataStr1 = b'Hello world! my name is Ashwin' # prefix 'b' means bytes-like object
25+
print('Original: ', len(dataStr1) )
26+
27+
comDataStr1 = zlib.compress(dataStr1)
28+
print('Compress: ', len(comDataStr1) )
29+
30+
deComDataStr1 = zlib.decompress(comDataStr1)
31+
print('Decompress: ', len(deComDataStr1) )
32+
33+
print('\n')
34+
35+
print('Data Compression Try 2: \n')
36+
37+
dataStr2 = b'Ashwin Ashwin Ashwin Ashwin' # prefix 'b' means bytes-like object
38+
print('Original: ', len(dataStr2) )
39+
40+
comDataStr2 = zlib.compress(dataStr2)
41+
print('Compress: ', len(comDataStr2) )
42+
43+
deComDataStr2 = zlib.decompress(comDataStr2)
44+
print('Decompress: ', len(deComDataStr2) )
45+

codes/session_8/libEg3.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# How to Run? Open Terminal> python3 libEg3.py
2+
# Read the code comments and output on the terminal
3+
4+
# Objective: Know about standard built-in library
5+
6+
# Doctest
7+
# The doctest module provides a tool for scanning a module
8+
# and validating tests embedded in a programs docstrings.
9+
# Try changing '20' in below example to see the doctest
10+
11+
def average(values):
12+
"""Computes the arithmetic mean of a list of numbers
13+
>>> print(average([10, 20, 30]))
14+
20
15+
"""
16+
return sum(values) / len(values)
17+
18+
import doctest
19+
doctest.testmod()
20+
21+
# Also, see libEg3.test.py file for unittest module example

codes/session_8/libEg3.test.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# How to Run? Open Terminal> python3 libEg3.test.py
2+
# Read the code comments and output on the terminal
3+
4+
# Objective: Know about standard built-in library
5+
6+
# The unittest module is not as effortless as the doctest module,
7+
# but it allows a more comprehensive set of tests to be maintained in a separate file
8+
9+
import unittest
10+
from libEg3 import average
11+
12+
class TestStatisticalFunctions(unittest.TestCase):
13+
14+
def test_average(self):
15+
16+
self.assertEqual(average([10, 20, 30]), 20)
17+
18+
with self.assertRaises(ZeroDivisionError):
19+
average([])
20+
21+
with self.assertRaises(TypeError):
22+
average(20, 30, 70)
23+
24+
unittest.main()

0 commit comments

Comments
 (0)