# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # HyperLearning AI - Introduction to Python
# An introductory course to the Python 3 programming language, with a curriculum aligned to the Certified Associate in Python Programming (PCAP) examination syllabus (PCAP-31-02).<br/>
# https://knowledgebase.hyperlearning.ai/courses/introduction-to-python
#
#
# ## 07. Functions and Modules Part 2
# https://knowledgebase.hyperlearning.ai/en/courses/introduction-to-python/modules/7/functions-and-modules-part-2
#
# In this module we will formally introduce Python modules and packages, including how to write and use Python modules, how to construct and distribute Python packages, how to hide Python module entities, how to document Python modules, and Python hashbangs. Specifically we will cover:
#
# * **Python Modules** - importing modules, qualifying entities, initialising modules, writing and using modules, the name variable, Python hashbangs, and module documentation
# * **Python Packages** - creating packages, packages vs directories, the init file, and hiding module entities

# ### 1. Modules
# #### 1.1. Importing Modules

# Import our numbertools module
import numbertools

# Call functions from the module
print(numbertools.is_int(0.5))
print(numbertools.is_even(1_000_002))
print(numbertools.is_prime(277))
print(numbertools.is_fibonacci(12))
print(numbertools.is_perfect_square(1444))

# Access variables from the module
print(numbertools.mobius_phi)

# +
# Create an alias for a module
import numbertools as nt

# Call module entities qualified with the module alias
print(nt.is_perfect_square(9801))
print(nt.mobius_phi)
# -

# List all the function and variable names in a given module
print(dir(nt))

print(dir(math))

# +
# Import specific entities
from math import cos, pi, radians, sin, tan

# Use the specifically imported entities
print(round(cos(math.radians(90)), 0))
print(round(sin(math.radians(90)), 0))
print(round(tan(math.radians(45)), 0))
print(round(pi, 10))
# -

# #### 1.2. Module Search Path

# +
# Examine and modify sys.path
import sys

# Examine sys.path
print(sys.path)

# Append a location to sys.path
sys.path.append('/foo/bar/code')
print(sys.path)
# -

# #### 1.5. Module Documentation

# Access and display a module's docstring
print(numbertools.__doc__)

# Access and display a built-in module's docstring
print(math.__doc__)

# Access and display a function's docstring
print(numbertools.is_fibonacci.__doc__)

# Access and display a built-in function's docstring
print(sin.__doc__)

# Display help on the numbertools module
help(numbertools)

# Display help on a specific function
help(numbertools.is_perfect_square)

# Display help on a specifc built-in function
help(len)

# Display help on the built-in math module
help(math)

# #### 1.7. PYC Files

# Generate a byte code file of our numbertools module
import py_compile
py_compile.compile('numbertools.py')

# Generate a byte code file of our numbertools module in a custom location
py_compile.compile('numbertools.py', cfile='/tmp/numbertools.pyc')

# ### 2. Packages
# #### 2.1. Importing Packages

# Import a module from a nested package in the Pandas library
import pandas.api.types as types
print(types.is_integer_dtype(str))

# Alternatively use from to import a specific module
from pandas.api import types
print(types.is_integer_dtype(str))

# Alternatively use from to import a specific function from a specific module
from pandas.api.types import is_integer_dtype
print(is_integer_dtype(str))

# #### 2.2.8. Install from Local Wheel

# +
# Import our generated distribution package myutils
import myutils

# Alternatively import a specific module from a specific package
import myutils.collections.dictutils as dictutils

# +
# Call one of our myutils bespoke functions
my_dict = {
    1: ['python', 3.8], 
    2: ['java', 11], 
    3: ['scala', 2.13]
}

# Convert a dictionary to a Pandas DataFrame using our user-defined dictutils.convert_to_dataframe() function
df = dictutils.convert_to_dataframe(my_dict, ['Language', 'Version'])
df.head()

StarcoderdataJupyterScriptsDedupFiltered