#set document(title: "7.5 Finding modules", author: "OpenStax / XYZ Homework") #set page(width: 8.5in, height: auto, margin: 1in) #import "@preview/cetz:0.5.2" #set text(font: ("STIX Two Text", "Libertinus Serif", "New Computer Modern"), size: 10.5pt, lang: "en") #show math.equation: set text(font: ("STIX Two Math", "New Computer Modern Math")) #set par(justify: true, leading: 0.62em, spacing: 0.9em) #set enum(spacing: 1.1em) // room between list items so tall inline fractions don't collide #set list(spacing: 1.1em) #set table(stroke: 0.5pt + rgb("#c7ccd3")) #let BLUE = rgb("#183B6F") // brand navy — section bars + example/solution labels (white on navy 11.09:1) #let ORANGE = rgb("#A94509") // brand primary-700 — AA-safe deep orange for TEXT (5.93:1 on white; raw brand #F37021 is 2.94:1 and must never carry text) #let RED = rgb("#DC2626") // brand error-600 #let GREEN = rgb("#059669") // brand success-600 (decoration only; small green text uses green-text #007942) #show heading.where(level: 1): it => block(width: 100%, above: 0pt, below: 16pt, fill: gradient.linear(BLUE, rgb("#2C5AA0")), inset: (x: 14pt, y: 12pt), radius: 3pt, text(fill: white, weight: "bold", size: 19pt, it.body)) #show heading.where(level: 2): it => block(width: 100%, above: 18pt, below: 10pt, fill: BLUE, inset: (x: 10pt, y: 6pt), radius: 2pt, text(fill: white, weight: "bold", size: 12pt, it.body)) #show heading.where(level: 3): it => text(fill: ORANGE, weight: "bold", size: 12.5pt, it.body) #show heading.where(level: 4): it => text(fill: BLUE, weight: "bold", size: 10.5pt, it.body) #let examplebox(label, title, body) = block(width: 100%, breakable: true, fill: rgb("#EFF1F5"), stroke: 0.5pt + rgb("#CFDDF0"), radius: 4pt, inset: 10pt, above: 12pt, below: 12pt)[ #block(below: 6pt)[#box(fill: BLUE, inset: (x: 6pt, y: 2pt), radius: 2pt, text(fill: white, weight: "bold", size: 8.5pt, label)) #h(0.4em) #strong[#title]] #body] // rail = decorative left rule (raw brand token); labelcolor = AA-safe label text shade #let notebox(label, rail, labelcolor, tint, body) = block(width: 100%, breakable: true, fill: tint, stroke: (left: 3pt + rail), inset: (left: 10pt, rest: 8pt), radius: (right: 4pt), above: 11pt, below: 11pt)[ #text(fill: labelcolor, weight: "bold", size: 7.5pt, tracking: 0.5pt)[#upper(label)] #linebreak() #body] #let solutionbox(body) = block(above: 4pt, below: 8pt)[ #text(fill: BLUE, weight: "bold", size: 8.5pt)[Solution] #linebreak() #body] #let figph(msg) = block(width: 100%, height: 60pt, fill: rgb("#f6f7f9"), stroke: (paint: rgb("#c7ccd3"), dash: "dashed"), radius: 4pt, inset: 10pt)[ #align(center + horizon, text(fill: rgb("#889"), style: "italic", size: 9pt, msg))] // Standardize inlined figure sizes: measure the natural CeTZ canvas, then scale to a // consistent envelope (aspect-aware; see build_typst.py FIG_* constants). Unlike the // print preamble, dimensions are FLOORED: in an editor a user can trim a figure to a // degenerate 1-D shape (a bare line), and w/h or tw/w would then divide by zero. #let _STD_W = 3.5 #let _WIDE_W = 5.6 #let _MAX_H = 3.4 #let _ASPECT_WIDE = 2.2 #let _UPSCALE_MAX = 1.15 #let stdfig(body) = context { let m = measure(body) let w = calc.max(m.width / 1in, 0.01) let h = calc.max(m.height / 1in, 0.01) let tw = if w / h > _ASPECT_WIDE { _WIDE_W } else { _STD_W } let s = calc.min(tw / w, _MAX_H / h, _UPSCALE_MAX) align(center, box(scale(x: s * 100%, y: s * 100%, reflow: true, body))) } #show figure: set block(breakable: false) #set figure(gap: 8pt) #show figure.caption: set text(size: 8.5pt, fill: rgb("#555")) == 7.5#h(0.6em)Finding modules === Learning objectives By the end of this section you should be able to - Explain differences between the standard library and PyPI. - Search python.org and pypi.org for modules of interest. === Built-in modules The #strong[Python Standard Library] is a collection of built-in functions and modules that support common programming tasks. Ex: The math module provides functions like sqrt() and constants like pi. Python's official documentation includes a #link("https://openstax.org/r/100pythlibrary")[library reference] and a #link("https://openstax.org/r/100200modules")[module index] for becoming familiar with the standard library. For decades, Python has maintained a "#link("https://openstax.org/r/100batteries")[batteries included]" philosophy. This philosophy means that the standard library should come with everything most programmers need. In fact, the standard library includes over 200 built-in modules! #figure(table( columns: 2, align: left, inset: 6pt, table.header([Module], [Description]), [#link("https://openstax.org/r/100pythcalendar")[calendar]], [General calendar-related functions.], [#link("https://openstax.org/r/100pythdatetime")[datetime]], [Basic date and time types and functions.], [#link("https://openstax.org/r/100pythonemail")[email]], [Generate and process email messages.], [#link("https://openstax.org/r/100mathmodule")[math]], [Mathematical functions and constants.], [#link("https://openstax.org/r/100opersystem")[os]], [Interact with the operating system.], [#link("https://openstax.org/r/100randomlib")[random]], [Generate pseudo-random numbers.], [#link("https://openstax.org/r/100pythonstats")[statistics]], [Mathematical statistics functions.], [#link("https://openstax.org/r/100sysparameter")[sys]], [System-specific parameters and functions.], [#link("https://openstax.org/r/100pythonturtle")[turtle]], [Educational framework for simple graphics.], [#link("https://openstax.org/r/100pythzipfile")[zipfile]], [Read and write ZIP-format archive files.], )) #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Built-in modules] Use the library reference, module index, and documentation links above to answer the questions. ] === Third-party modules The #strong[Python Package Index] (PyPI), available at #link("https://openstax.org/r/100pypi")[pypi.org], is the official third-party software library for Python. The abbreviation "PyPI" is pronounced like #emph[pie pea eye] (in contrast to #link("https://openstax.org/r/100pypy")[PyPy], a different project). PyPI allows anyone to develop and share modules with the Python community. Module authors include individuals, large companies, and non-profit organizations. PyPI helps programmers install modules and receive updates. Most software available on PyPI is free and open source. PyPI is supported by the #link("https://openstax.org/r/100foundation")[Python Software Foundation] and is maintained by an independent group of developers. #figure(table( columns: 2, align: left, inset: 6pt, table.header([Module], [Description]), [#link("https://openstax.org/r/100projectarrow")[arrow]], [Convert and format dates, times, and timestamps.], [#link("https://openstax.org/r/100beautifsoup")[BeautifulSoup]], [Extract data from HTML and XML documents.], [#link("https://openstax.org/r/100pypibokeh")[bokeh]], [Interactive plots and applications in the browser.], [#link("https://pypi.org/project/matplotlib/")[matplotlib]], [Static, animated, and interactive visualizations.], [#link("https://openstax.org/r/100moviepy")[moviepy]], [Video editing, compositing, and processing.], [#link("https://openstax.org/r/100pypilanguage")[nltk]], [Natural language toolkit for human languages.], [#link("https://openstax.org/r/100pypinumpy")[numpy]], [Fundamental package for numerical computing.], [#link("https://openstax.org/r/100pypipandas")[pandas]], [Data analysis, time series, and statistics library.], [#link("https://openstax.org/r/100pypipillows")[pillow]], [Image processing for jpg, png, and other formats.], [#link("https://openstax.org/r/100pypipytest")[pytest]], [Full-featured testing tool and unit test framework.], [#link("https://openstax.org/r/100pypirequests")[requests]], [Elegant HTTP library for connecting to web servers.], [#link("https://openstax.org/r/100pypiscikit")[scikit-learn]], [Simple, efficient tools for predictive data analysis.], [#link("https://openstax.org/r/100pypiscipy")[scipy]], [Fundamental algorithms for scientific computing.], [#link("https://openstax.org/r/100pypiscrapy")[scrapy]], [Crawl websites and scrape data from web pages.], [#link("https://pypi.org/project/tensorflow/")[tensorflow]], [End-to-end machine learning platform for everyone.], )) #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Third-party modules] Use pypi.org and the links in the table above to answer the questions. ] #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Exploring further] Programming blogs often highlight PyPI modules to demonstrate the usefulness of Python. The following examples provide more background information about the modules listed above. - #link("https://openstax.org/r/100simplelearn")[Top 20 Python Libraries for Data Science for 2023] - #link("https://openstax.org/r/100library2022")[24 Best Python Libraries You Should Check in 2022] - #link("https://openstax.org/r/100packages2021")[Most Popular Python Packages in 2021] ] #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[Happy birthday] Module documentation pages often include examples to help programmers become familiar with the module. For this exercise, refer to the following examples from the datetime module documentation: - #link("https://openstax.org/r/100dateexamples")[Examples of Usage: date] - #link("https://openstax.org/r/100timedeltaex")[Examples of Usage: timedelta] Write a program that creates a date object representing your birthday. Then get a date object representing today's date (the date the program is run). Calculate the difference between the two dates, and output the results in the following format: Your birth date: 2005-03-14 Today's date is: 2023-06-01 You were born 6653 days ago (that is 574819200 seconds) You are about 18 years old ] #notebox("Note", rgb("#8a94a6"), rgb("#556666"), rgb("#f7f8fa"))[ #emph[More exact age] The datetime module does not provide a built-in way to display a person's exact age. Ex: The following program calculates an exact age (in years and days) using floor division and modulo. The output is: You are 15 years and 4 days old. from datetime import date birth = date(2005, 3, 14) today = date(2020, 3, 14) \# 15 years later delta = today - birth years = delta.days // 365 days = delta.days % 365 print("You are", years, "years and", days, "days old") Notice how leap years are included in the calculation. February 29th occurs four times between birth and today. Therefore, the user is not only 15 years old, but 15 years and 4 days old. Many commonly used modules from PyPI, including arrow, are installed in the Python shell at #link("https://openstax.org/r/100pythonshell")[python.org/shell]. Open the Python shell and type the following lines: import arrow birth = arrow.get(2005, 3, 14) birth.humanize() Refer to the #link("https://openstax.org/r/100humanize")[humanize()] examples from the arrow module documentation. In the Python shell, figure out how to display the number of years and days since birth using #emph[one line of code]. Then display the number of years, months, and days since birth. Finally, use the print() function to output the results in this format: You are 18 years 4 months and 7 days old. As time permits, experiment with other functions provided by the arrow module. ]