-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path009_modules.py
184 lines (139 loc) · 4.25 KB
/
009_modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# ]
# ///
import marimo
__generated_with = "0.10.19"
app = marimo.App()
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
# 🧩 Using modules
A `module` in Python is a Python file that defines functions and variables. Modules can be `imported` into other Python files, letting you reuse their
functions and variables.
We have already seen some modules in previous tutorials, including the `math`
module. Python comes with many other modules built-in.
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## The Python standard library
Python's "standard library" provides many modules, for many kinds of tasks.
```python
# String manipulation
import string
# Operating system interactions
import os
# Date and time handling
import datetime
# Mathematical operations
import math
```
See the [Python standard library documentation](https://docs.python.org/3/library/) for a full reference
"""
)
return
@app.cell(hide_code=True)
def _(mo):
mo.md("""### Example""")
return
@app.cell
def _():
import string
import os
import datetime
import math
# Example of using imported modules
def demonstrate_standard_library_usage():
# String module: get all punctuation
punctuation_example = string.punctuation
# OS module: get current working directory
current_dir = os.getcwd()
# Datetime module: get current date
today = datetime.date.today()
# Math module: calculate square root
sqrt_example = math.sqrt(16)
return {
"Punctuation": punctuation_example,
"Current Directory": current_dir,
"Today's Date": today,
"Square Root Example": sqrt_example
}
# Run the demonstration
module_usage_examples = demonstrate_standard_library_usage()
module_usage_examples
return (
datetime,
demonstrate_standard_library_usage,
math,
module_usage_examples,
os,
string,
)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Import syntax
You can import entire modules, and access their functions and variables using dot notation (`math.sqrt`). Or you can import specific members:
```python
# Import entire module
import math
# Import specific functions
from math import sqrt, pow
# Import with alias
import math as m
```
"""
)
return
@app.cell
def _():
def demonstrate_import_strategies():
"""
Demonstrate different import strategies using the math module
"""
# Strategy 1: Import entire module
import math
entire_module_result = math.sqrt(25)
# Strategy 2: Import specific functions
from math import pow, sqrt
specific_import_result = pow(2, 3)
# Strategy 3: Import with alias
import math as m
alias_result = m.sqrt(16)
return {
"Entire Module Import": entire_module_result,
"Specific Function Import": specific_import_result,
"Alias Import": alias_result
}
# Run the import strategy demonstration
import_strategy_examples = demonstrate_import_strategies()
import_strategy_examples
return demonstrate_import_strategies, import_strategy_examples
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Third-party packages
In addition to Python's standard library, there are hundreds of thousands of
modules available for free on the Python Package index.
These are distributed as Python "packages", and include packages for
manipulating arrays of numbers, creating web applications, and more. `marimo`
itself is a third-party package!
For installing packages on your machine, we recommend using the [`uv` package manager](https://docs.astral.sh/uv/).
"""
)
return
@app.cell
def _():
import marimo as mo
return (mo,)
if __name__ == "__main__":
app.run()