Skip to content

Commit 44e0497

Browse files
heikkitoivonencodex
andcommitted
Docs: Add math and mmap
Co-Authored-By: Codex <codex@openai.com>
1 parent 4d22198 commit 44e0497

9 files changed

Lines changed: 272 additions & 105 deletions

File tree

DOCUMENTATION_STATUS.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ This document tracks the coverage of built-in functions, types, and standard lib
55
## Overview
66

77
- **Total Items**: 317 (150 builtins + 167 stdlib modules)
8-
- **Documented**: 377 (174 builtins + 203 stdlib)
9-
- **Coverage**: 118.9%
8+
- **Documented**: 379 (174 builtins + 205 stdlib)
9+
- **Coverage**: 119.6%
1010

1111
**Note**: Coverage exceeds 100% because comprehensive documentation files (like `exceptions.md`) cover multiple individual items, and we document deprecated/removed modules for historical reference.
1212

@@ -203,7 +203,7 @@ Complete coverage of all built-in functions, types, exceptions, and constants:
203203

204204
## Standard Library Modules
205205

206-
**Coverage: 121.6% (203/167)**
206+
**Coverage: 122.8% (205/167)**
207207

208208
All standard library modules are fully documented, including new Python 3.14 modules. Coverage exceeds 100% due to documentation of deprecated/removed modules for historical reference.
209209

@@ -305,9 +305,9 @@ All file and I/O modules now documented:
305305
-`shutil` - High-level file operations
306306
-`tempfile` - Temporary files
307307

308-
### ✅ ALL MODULES COMPLETE (203/203)
308+
### ✅ ALL MODULES COMPLETE (205/205)
309309

310-
All 100 previously undocumented stdlib modules have been added:
310+
All 102 previously undocumented stdlib modules have been added:
311311

312312
**Utilities & System (20)**
313313
-`ast` - Abstract syntax trees
@@ -368,6 +368,9 @@ All 100 previously undocumented stdlib modules have been added:
368368
-`tty` - Terminal control
369369
-`zipimport` - Zip import support
370370

371+
**Memory Mapping (1)**
372+
-`mmap` - Memory-mapped file access
373+
371374
**Parsing & Compilation (12)**
372375
-`codeop` - Compile Python source
373376
-`code` - Code evaluation
@@ -382,7 +385,8 @@ All 100 previously undocumented stdlib modules have been added:
382385
-`symtable` - Symbol table
383386
-`token` - Token types
384387

385-
**Number Operations (1)**
388+
**Number Operations (2)**
389+
-`math` - Floating-point math functions
386390
-`zoneinfo` - IANA time zone database
387391

388392
**GUI & Interface (4)**

data/documentation_audit.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@
168168
},
169169
"stdlib": {
170170
"total": 167,
171-
"documented": 203,
172-
"coverage_percent": 121.6,
171+
"documented": 205,
172+
"coverage_percent": 122.8,
173173
"missing": [
174174
"audit_documentation",
175175
"cProfile",
@@ -179,7 +179,7 @@
179179
},
180180
"summary": {
181181
"total_items": 317,
182-
"total_documented": 377,
183-
"overall_coverage_percent": 118.9
182+
"total_documented": 379,
183+
"overall_coverage_percent": 119.6
184184
}
185185
}

docs/stdlib/mailbox.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ The `mailbox` module provides classes for reading, writing, and manipulating var
66

77
| Operation | Time | Space | Notes |
88
|-----------|------|-------|-------|
9-
| Open mailbox | O(n) | O(n) | n = messages |
10-
| Add message | O(1) amortized | O(1) | Append operation |
9+
| Open mailbox | Varies | Varies | Depends on mailbox format and indexing |
10+
| Add message | Varies | Varies | Depends on mailbox type and file I/O |
1111
| Iterate messages | O(n) | O(1) | Sequential access |
1212

1313
## Working with Mailboxes
@@ -17,7 +17,7 @@ The `mailbox` module provides classes for reading, writing, and manipulating var
1717
```python
1818
import mailbox
1919

20-
# Open mbox - O(n)
20+
# Open mbox - cost depends on format and file size
2121
mbox = mailbox.mbox('mail.mbox')
2222

2323
# Iterate - O(n)
@@ -34,18 +34,18 @@ mbox.close()
3434
import mailbox
3535
from email.message import EmailMessage
3636

37-
# Open - O(n)
37+
# Open - cost depends on format and file size
3838
mbox = mailbox.mbox('mail.mbox')
3939

4040
# Create message - O(1)
4141
msg = EmailMessage()
4242
msg['Subject'] = 'Test'
4343
msg.set_content('Hello')
4444

45-
# Add - O(1)
45+
# Add - cost depends on mailbox type and I/O
4646
key = mbox.add(msg)
4747

48-
# Flush to disk - O(n)
48+
# Flush to disk - cost depends on pending changes
4949
mbox.flush()
5050
mbox.close()
5151
```

docs/stdlib/math.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# math Module Complexity
2+
3+
The `math` module provides fast floating-point math functions and a few integer-specific helpers.
4+
5+
## Complexity Reference
6+
7+
| Operation | Time | Space | Notes |
8+
|-----------|------|-------|-------|
9+
| `math.sqrt(x)` | O(1) | O(1) | Floating-point operation |
10+
| `math.log(x[, base])` | O(1) | O(1) | Floating-point operation |
11+
| `math.sin/cos/tan(x)` | O(1) | O(1) | Floating-point operation |
12+
| `math.hypot(*coords)` | O(n) | O(1) | n = number of coordinates |
13+
| `math.fsum(iterable)` | O(n) | O(1) | Accurate summation of n values |
14+
| `math.isclose(a, b)` | O(1) | O(1) | Floating-point comparison |
15+
| `math.gcd(a, b)` | Varies | O(1) | Depends on integer size |
16+
| `math.lcm(a, b)` | Varies | O(1) | Depends on integer size |
17+
| `math.factorial(n)` | Varies | Varies | Big-int cost grows with n |
18+
| `math.comb(n, k)` | Varies | Varies | Big-int cost grows with n and k |
19+
| `math.perm(n, k)` | Varies | Varies | Big-int cost grows with n and k |
20+
21+
!!! warning "Floating-point precision"
22+
Most `math` functions operate on binary floating-point numbers. Results are fast but may be imprecise
23+
for some decimal fractions. Use `decimal` for exact decimal arithmetic.
24+
25+
## Common Operations
26+
27+
### Basic Transcendentals
28+
29+
```python
30+
import math
31+
32+
x = 2.0
33+
34+
# O(1)
35+
root = math.sqrt(x)
36+
log2 = math.log(x, 2)
37+
angle = math.sin(math.pi / 6)
38+
```
39+
40+
### Hypotenuse and Distance
41+
42+
```python
43+
import math
44+
45+
# O(n) in number of coordinates
46+
r2 = math.hypot(3.0, 4.0) # 5.0
47+
r3 = math.hypot(1.0, 2.0, 2.0) # 3.0
48+
```
49+
50+
### Accurate Summation
51+
52+
```python
53+
import math
54+
55+
values = [1e100, 1.0, -1e100]
56+
57+
# Regular sum may lose precision
58+
regular = sum(values) # 0.0
59+
60+
# fsum keeps extra precision
61+
accurate = math.fsum(values) # 1.0
62+
```
63+
64+
### Integers: gcd, lcm, factorial, comb, perm
65+
66+
```python
67+
import math
68+
69+
# gcd/lcm
70+
g = math.gcd(84, 30) # 6
71+
l = math.lcm(6, 10) # 30
72+
73+
# factorial, combinations, permutations
74+
f = math.factorial(10) # 3628800
75+
c = math.comb(10, 3) # 120
76+
p = math.perm(10, 3) # 720
77+
```
78+
79+
### Robust Comparisons
80+
81+
```python
82+
import math
83+
84+
# Avoid direct equality for floats
85+
math.isclose(0.1 + 0.2, 0.3) # True
86+
```
87+
88+
## Performance Notes
89+
90+
- Floating-point operations are typically constant time and CPU-bound.
91+
- Integer-heavy functions (`factorial`, `comb`, `perm`, `gcd`, `lcm`) scale with operand size.
92+
- `fsum` provides better accuracy with a small constant-factor cost over `sum`.
93+
94+
## Related Modules
95+
96+
- [cmath Module](cmath.md) - Complex-number math
97+
- [decimal Module](decimal.md) - Decimal arithmetic
98+
- [fractions Module](fractions.md) - Rational arithmetic
99+
- [statistics Module](statistics.md) - Descriptive statistics
100+
- [random Module](random.md) - Random numbers

docs/stdlib/mimetypes.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The `mimetypes` module provides support for working with MIME types, mapping fil
1010
| `guess_type(url)` | O(1) | O(1) | Lookup MIME type |
1111
| `guess_extension(type)` | O(1) | O(1) | Get extension |
1212
| `add_type(type, ext)` | O(1) | O(1) | Register mapping |
13-
| `read(filename)` | O(n) | O(n) | n = file size |
13+
| `read_mime_types(filename)` | O(n) | O(n) | n = file size |
1414

1515
## Common Operations
1616

@@ -43,7 +43,7 @@ ext = mimetypes.guess_extension('application/pdf')
4343

4444
# Get all possible extensions - O(k) where k = extension count
4545
exts = mimetypes.guess_all_extensions('text/plain')
46-
# Returns: ['.txt', '.asc', '.c', .h', ...]
46+
# Returns: ['.txt', '.asc', '.c', '.h', ...]
4747
```
4848

4949
## Common Use Cases
@@ -61,16 +61,12 @@ def get_content_type(filename):
6161
if mime_type is None:
6262
mime_type = 'application/octet-stream'
6363

64-
# O(1) to format header
65-
content_type = mime_type
66-
if encoding:
67-
content_type += f'; charset={encoding}'
68-
69-
return content_type
64+
# encoding from guess_type is compression (e.g., 'gzip')
65+
return mime_type, encoding
7066

7167
# Usage - O(1)
72-
ct = get_content_type('document.pdf') # 'application/pdf'
73-
ct = get_content_type('data.csv') # 'text/csv'
68+
ct, enc = get_content_type('document.pdf') # ('application/pdf', None)
69+
ct, enc = get_content_type('data.csv') # ('text/csv', None)
7470
```
7571

7672
### Serving Files in Web Applications
@@ -158,7 +154,7 @@ def initialize_mimetypes(custom_files=None):
158154
for filepath in custom_files:
159155
if os.path.exists(filepath):
160156
# O(n) to read and parse file
161-
mimetypes.read(filepath)
157+
mimetypes.read_mime_types(filepath)
162158

163159
# Usage - O(n+m)
164160
initialize_mimetypes(['custom-types.txt'])
@@ -242,6 +238,7 @@ for file in files:
242238

243239
```python
244240
import mimetypes
241+
from pathlib import Path
245242

246243
def get_accurate_type(filename):
247244
"""Get accurate MIME type - O(1)"""

docs/stdlib/mmap.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# mmap Module Complexity
2+
3+
The `mmap` module provides memory-mapped file support for efficient random access to file contents.
4+
5+
## Complexity Reference
6+
7+
| Operation | Time | Space | Notes |
8+
|-----------|------|-------|-------|
9+
| `mmap.mmap(fileno, length, ...)` | Varies | Varies | Depends on OS and mapping size |
10+
| `mmap.read(n)` | O(n) | O(n) | n = bytes read |
11+
| `mmap.write(data)` | O(n) | O(1) | n = bytes written |
12+
| `mmap.seek(pos[, whence])` | O(1) | O(1) | Pointer movement |
13+
| `mmap.find(sub[, start[, end]])` | O(n) | O(1) | n = search range |
14+
| `mmap.flush()` | Varies | O(1) | Depends on OS and dirty pages |
15+
| `mmap.close()` | O(1) | O(1) | Unmap and close |
16+
17+
!!! warning "Platform-dependent behavior"
18+
`mmap` performance depends on the operating system, filesystem, and page cache behavior.
19+
The costs shown here describe typical asymptotic behavior, not wall-clock guarantees.
20+
21+
## Common Operations
22+
23+
### Creating a Mapping
24+
25+
```python
26+
import mmap
27+
28+
with open('data.bin', 'rb') as f:
29+
mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
30+
print(mm.size()) # total size
31+
mm.close()
32+
```
33+
34+
### Reading and Writing
35+
36+
```python
37+
import mmap
38+
39+
with open('data.bin', 'r+b') as f:
40+
mm = mmap.mmap(f.fileno(), 0)
41+
42+
# Read 16 bytes
43+
chunk = mm.read(16)
44+
45+
# Write bytes in place
46+
mm.seek(0)
47+
mm.write(b'HELLO')
48+
49+
mm.flush()
50+
mm.close()
51+
```
52+
53+
### Searching
54+
55+
```python
56+
import mmap
57+
58+
with open('data.bin', 'rb') as f:
59+
mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
60+
61+
idx = mm.find(b'needle') # O(n) search
62+
if idx != -1:
63+
print('Found at', idx)
64+
65+
mm.close()
66+
```
67+
68+
## Performance Notes
69+
70+
- Mapping large files avoids copying data into Python space for random access.
71+
- Reads/writes still move data between kernel and user space when accessed.
72+
- For sequential reads of large files, buffered I/O can be comparable or faster.
73+
74+
## Related Modules
75+
76+
- [io Module](io.md)
77+
- [os Module](os.md)
78+
- [pathlib Module](pathlib.md)

docs/stdlib/modulefinder.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ The `modulefinder` module finds all modules that a Python script imports, useful
66

77
| Operation | Time | Space | Notes |
88
|-----------|------|-------|-------|
9-
| Find dependencies | O(n) | O(n) | n = imported modules |
10-
| Build import graph | O(n) | O(n) | Track relationships |
9+
| Find dependencies | Varies | Varies | Depends on code paths and import hooks |
10+
| Build import graph | Varies | Varies | Depends on modules discovered |
1111

1212
## Analyzing Module Dependencies
1313

@@ -19,14 +19,14 @@ from modulefinder import ModuleFinder
1919
# Create finder - O(1)
2020
mf = ModuleFinder()
2121

22-
# Analyze script - O(n)
22+
# Analyze script - cost depends on imports and code paths
2323
mf.run_script('myapp.py')
2424

2525
# Get results - O(1)
2626
print("Modules:", mf.modules)
2727
print("Bad imports:", mf.badimports)
2828

29-
# Report - O(n)
29+
# Report - cost depends on number of modules
3030
mf.report()
3131
```
3232

0 commit comments

Comments
 (0)