<jupyter_start><jupyter_text>Chapter 4. Modularity(1) Structure and organization, or modularity of Python programs.(2) Module = a collection of reusable functions 4.1 Creating, running, and importing a module```bash$ python3 words.py```<jupyter_code>## SAVE AS words.py.

from urllib.request import urlopen

# Using with statement is a good practice of avoiding resource leaks.
with urlopen('http://sixty-north.com/c/t.txt') as story:
    story_words = []
    for line in story:
        line_words = line.decode('utf-8').split()
        story_words += line_words
        
for word in story_words:
    print(word)<jupyter_output><empty_output><jupyter_text>4.2 Defining function and returning values<jupyter_code>def square(x):
    return x * x

square(5)
def launch_missles():
    print("Missles launched!")
    
launch_missles()
def even_or_odd(n):
    if n % 2 == 0:
        print("even")
        return
    print("odd")
    
even_or_odd(4)
even_or_odd(5)
w = even_or_odd(31)
w is None<jupyter_output><empty_output><jupyter_text>4.3 Distinguishing between module import and module execution<jupyter_code>## SAVE AS words.py.

from urllib.request import urlopen

def fetch_words():
    # Using with statement is a good practice of avoiding resource leaks.
    with urlopen('http://sixty-north.com/c/t.txt') as story:
        story_words = []
        for line in story:
            line_words = line.decode('utf-8').split()
            story_words += line_words
            
    for word in story_words:
        print(word)
# Work around the issue that words.py is not in the current directory.
import sys
sys.path.insert(0, './pyfund')

import words

words.fetch_words()
from words import fetch_words

fetch_words()<jupyter_output>It
was
the
best
of
times
it
was
the
worst
of
times
it
was
the
age
of
wisdom
it
was
the
age
of
foolishness
it
was
the
epoch
of
belief
it
was
the
epoch
of
incredulity
it
was
the
season
of
Light
it
was
the
season
of
Darkness
it
was
the
spring
of
hope
it
was
the
winter
of
despair
we
had
everything
before
us
we
had
nothing
before
us
we
were
all
going
direct
to
Heaven
we
were
all
going
direct
the
other
way
in
short
the
period
was
so
far
like
the
present
period
that
some
of
its
noisiest
authorities
insisted
on
its
being
received
for
good
or
for
evil
in
the
superlative
degree
of
comparison
only<jupyter_text>* Special attributes in Python are delimited by double underscores, e.g., "__name__".<jupyter_code>## SAVE AS words.py.

from urllib.request import urlopen

def fetch_words():
    # Using with statement is a good practice of avoiding resource leaks.
    with urlopen('http://sixty-north.com/c/t.txt') as story:
        story_words = []
        for line in story:
            line_words = line.decode('utf-8').split()
            story_words += line_words
            
    for word in story_words:
        print(word)

# Check if the module is executed as a script.
if __name__ == "__main__":
    fetch_words()<jupyter_output><empty_output><jupyter_text>4.4 The Python execution model 4.4.1 Two basic questions(1) When are functions defined?(2) What happens when a module is imported? 4.4.2 Module vs. script vs. program(1) Module: convenient import with API(2) Script: convenient execution from command line(3) Program: perhaps composed of many modules  4.5 Main functions and command line arguments<jupyter_code>## SAVE AS words.py.

from urllib.request import urlopen

def fetch_words():
    # Using with statement is a good practice of avoiding resource leaks.
    with urlopen('http://sixty-north.com/c/t.txt') as story:
        story_words = []
        for line in story:
            line_words = line.decode('utf-8').split()
            story_words += line_words
            
def print_words(story_words):
    for word in story_words:
        print(word)

def main():
    words = fetch_words()
    print_words(words)
        
# Check if the module is executed as a script.
if __name__ == "__main__":
    main()<jupyter_output><empty_output><jupyter_text>4.5.1 More import syntax<jupyter_code>from words import (fetch_words, print_words)

print_words(fetch_words())
from words import *

fetch_words()
print_words(['Any', 'list', 'of', 'words'])
main()
print_words([1, 3, 7])
print_words('Strings are iterable too')<jupyter_output>S
t
r
i
n
g
s
 
a
r
e
 
i
t
e
r
a
b
l
e
 
t
o
o<jupyter_text>4.5.2 RefactoringFor advanced command line argument parsing:* Python standard library: argparse* Many 3rd-party options such as docopt"Sparse is better than dense""Two between functions  That is the number of lines  PEP 8 recommends"```bash$ python3 words.py http://sixty-north.com/c/t.txt```<jupyter_code>## SAVE AS words.py.

import sys
from urllib.request import urlopen

def fetch_words(url):
    # Using with statement is a good practice of avoiding resource leaks.
    with urlopen(url) as story:
        story_words = []
        for line in story:
            line_words = line.decode('utf-8').split()
            story_words += line_words
            
        return story_words
            
        
def print_items(items):
    for item in items:
        print(item)

        
def main(url):
    words = fetch_words(url)
    print_items(words)
        
        
# Check if the module is executed as a script.
if __name__ == "__main__":
    main(sys.argv[1])
# Work around the issue that words.py is not in the current directory.
import sys
sys.path.insert(0, './pyfund')

from words import *

main('http://sixty-north.com/c/t.txt')<jupyter_output>It
was
the
best
of
times
it
was
the
worst
of
times
it
was
the
age
of
wisdom
it
was
the
age
of
foolishness
it
was
the
epoch
of
belief
it
was
the
epoch
of
incredulity
it
was
the
season
of
Light
it
was
the
season
of
Darkness
it
was
the
spring
of
hope
it
was
the
winter
of
despair
we
had
everything
before
us
we
had
nothing
before
us
we
were
all
going
direct
to
Heaven
we
were
all
going
direct
the
other
way
in
short
the
period
was
so
far
like
the
present
period
that
some
of
its
noisiest
authorities
insisted
on
its
being
received
for
good
or
for
evil
in
the
superlative
degree
of
comparison
only<jupyter_text>4.6 Documentation 4.6.1 Documenting your code using docstrings.(1) PEP 257 -- not widely adopted(2) HTML documentation -- reStructuredText/Sphinx(3) Google Python style guide -- followed here<jupyter_code>help(fetch_words)
import words

help(words)<jupyter_output>Help on module words:

NAME
    words - Retrieve and print words from a URL.

DESCRIPTION
    Usage:
        python3 words.py <URL>

FUNCTIONS
    fetch_words(url)
        Fetch a list of words from a URL.
        
        Args:
            url: The URL of a UTF-8 text document.
            
        Returns:
            A list of strings containing the words from 
            the document.
    
    main(url)
        Print each word from a text document from a URL.
        
        Args:
            url: The URL of a UTF-8 text document.
    
    print_items(items)
        Print items one per line.
        
        Args:
           items: An iterable series of printable items.

FILE
    /home/renwei/repos/github/learning-ml/python/pluralsight-python-fundamental/pyfund/words.py<jupyter_text>4.6.2 Documenting your code with comments<jupyter_code>## SAVE AS words.py.

"""Retrieve and print words from a URL.

Usage:
    python3 words.py <URL>
"""

import sys
from urllib.request import urlopen

def fetch_words(url):
    """Fetch a list of words from a URL.
    
    Args:
        url: The URL of a UTF-8 text document.
        
    Returns:
        A list of strings containing the words from 
        the document.
    """
    # Using with statement is a good practice of avoiding resource leaks.
    with urlopen(url) as story:
        story_words = []
        for line in story:
            line_words = line.decode('utf-8').split()
            story_words += line_words
            
        return story_words
            
            
def print_items(items):
    """Print items one per line.
    
    Args:
       items: An iterable series of printable items.
    """
    for item in items:
        print(item)


def main(url):
    """Print each word from a text document from a URL.
    
    Args:
        url: The URL of a UTF-8 text document.
    """
    words = fetch_words(url)
    print_items(words)
        
        
# Check if the module is executed as a script.
if __name__ == "__main__":
    main(sys.argv[1]) # The 0th arg is the module filename.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08034026465028356, 'fraction_numerical': 0.008861058601134215, 'mean_word_length': 2.913546000924642, 'pattern_counts': {'":': 4, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '462578', 'license': 'Apache-2.0', 'n_tokens_mistral': 2838, 'n_tokens_neox': 2766, 'n_words': 979, 'path': 'python/pluralsight-python-fundamental/ch04-Modularity.ipynb', 'repo_name': 'renweizhukov/learning-ml', 'size': '8464'}